be able to skip automatic closing of a hash resource

This commit is contained in:
udaysagar2177
2019-06-12 19:11:29 -07:00
parent 19ee2d2746
commit 77b678184f
8 changed files with 140 additions and 26 deletions
@@ -762,6 +762,17 @@ public interface ChronicleHashBuilder<K, H extends ChronicleHash<K, ?, ?, ?>,
*/
B setPreShutdownAction(Runnable preShutdownAction);
/**
* Skips the default automatic close configuration on the {@link ChronicleHash} created by
* this builder. By setting this to true, the caller agrees to closing the built {@link ChronicleHash}
* explicitly. Any pre-shutdown action configured via {@link this#setPreShutdownAction(Runnable)}
* won't be executed if skipCloseOnExitHook is set to true.
*
* @param skipCloseOnExitHook if {@code true}, default automatic close configuration is not enabled.
* @return this builder back
*/
B skipCloseOnExitHook(boolean skipCloseOnExitHook);
/**
* @deprecated don't use private API in the client code
*/
@@ -97,4 +97,9 @@ public interface ChronicleHashBuilderPrivateAPI<K, RO> {
* @return registered pre-shutdown action.
*/
Runnable getPreShutdownAction();
/**
* Whether to skip closing a {@link ChronicleHash} using the default shutdown hook.
*/
boolean skipCloseOnExitHook();
}
@@ -126,6 +126,7 @@ public abstract class VanillaChronicleHash<K,
public transient Identity identity;
protected int log2TiersInBulk;
private Runnable preShutdownAction;
private boolean skipCloseOnExitHook;
/////////////////////////////////////////////////
// Bytes Store (essentially, the base address) and serialization-dependent offsets
protected transient BytesStore bs;
@@ -211,6 +212,7 @@ public abstract class VanillaChronicleHash<K,
checksumEntries = privateAPI.checksumEntries();
preShutdownAction = privateAPI.getPreShutdownAction();
skipCloseOnExitHook = privateAPI.skipCloseOnExitHook();
}
public static IOException throwRecoveryOrReturnIOException(
@@ -476,7 +478,9 @@ public abstract class VanillaChronicleHash<K,
}
public void addToOnExitHook() {
ChronicleHashCloseOnExitHook.add(this);
if (!skipCloseOnExitHook) {
ChronicleHashCloseOnExitHook.add(this);
}
}
public final void createMappedStoreAndSegments(ChronicleHashResources resources)
@@ -664,7 +668,9 @@ public abstract class VanillaChronicleHash<K,
// Releases nothing after resources.releaseManually(), only removes the cleaner
// from the internal linked list of all cleaners.
cleaner.clean();
ChronicleHashCloseOnExitHook.remove(this);
if (!skipCloseOnExitHook) {
ChronicleHashCloseOnExitHook.remove(this);
}
// Make GC life easier
keyReader = null;
keyDataAccess = null;
@@ -181,6 +181,7 @@ public final class ChronicleMapBuilder<K, V> implements
MapEntryOperations<K, V, ?> entryOperations = mapEntryOperations();
MapRemoteOperations<K, V, ?> remoteOperations = mapRemoteOperations();
Runnable preShutdownAction;
boolean skipCloseOnExitHook = false;
private String name;
// not final because of cloning
private ChronicleMapBuilderPrivateAPI<K, V> privateAPI =
@@ -1607,6 +1608,12 @@ public final class ChronicleMapBuilder<K, V> implements
return this;
}
@Override
public ChronicleMapBuilder<K, V> skipCloseOnExitHook(boolean skipCloseOnExitHook) {
this.skipCloseOnExitHook = skipCloseOnExitHook;
return this;
}
@Override
public ChronicleMap<K, V> create() {
// clone() to make this builder instance thread-safe, because createWithoutFile() method
@@ -120,4 +120,9 @@ class ChronicleMapBuilderPrivateAPI<K, V>
public Runnable getPreShutdownAction() {
return b.preShutdownAction;
}
@Override
public boolean skipCloseOnExitHook() {
return b.skipCloseOnExitHook;
}
}
@@ -358,6 +358,12 @@ public final class ChronicleSetBuilder<K>
return this;
}
@Override
public ChronicleSetBuilder<K> skipCloseOnExitHook(boolean skipCloseOnExitHook) {
chronicleMapBuilder.skipCloseOnExitHook(skipCloseOnExitHook);
return this;
}
/**
* @deprecated don't use private API in the client code
*/
@@ -138,4 +138,9 @@ class ChronicleSetBuilderPrivateAPI<K>
public Runnable getPreShutdownAction() {
return mapB.getPreShutdownAction();
}
@Override
public boolean skipCloseOnExitHook() {
return mapB.skipCloseOnExitHook();
}
}