Move all ChronicleMap closing logic into independent ChronicleHashResources. Reference contexts from ThreadLocals via ContextHolders and clear references to contexts on closing ChronicleMap. Make some closing procedures more paranoid than they were. Cache ChronicleMap's identityString and always return the same String from toIdentityString(). Add Throwables.returnOrSuppress().

This commit is contained in:
leventov
2016-12-10 21:03:20 -06:00
parent 9e88392db6
commit e0b534af23
12 changed files with 424 additions and 242 deletions
@@ -1545,9 +1545,16 @@ public final class ChronicleMapBuilder<K, V> implements
return result;
} catch (Throwable throwable) {
try {
resources.releaseManually();
} catch (Exception e) {
throwable.addSuppressed(e);
try {
resources.setChronicleHashIdentityString(
"ChronicleHash{name=" + name + ", file=" + file + "}");
} catch (Throwable t) {
throwable.addSuppressed(t);
} finally {
resources.releaseManually();
}
} catch (Throwable t) {
throwable.addSuppressed(t);
}
throw Throwables.propagateNotWrapping(throwable, IOException.class);
}
@@ -1555,6 +1562,7 @@ public final class ChronicleMapBuilder<K, V> implements
private void prepareMapPublication(VanillaChronicleMap map) throws IOException {
establishReplication(map);
map.setResourcesName();
map.registerCleaner();
// Ensure safe publication of a ChronicleMap
OS.memory().storeFence();
@@ -1729,9 +1737,16 @@ public final class ChronicleMapBuilder<K, V> implements
return map;
} catch (Throwable throwable) {
try {
resources.releaseManually();
} catch (Exception e) {
throwable.addSuppressed(e);
try {
resources.setChronicleHashIdentityString(
"ChronicleHash{name=" + name + ", file=null}");
} catch (Throwable t) {
throwable.addSuppressed(t);
} finally {
resources.releaseManually();
}
} catch (Throwable t) {
throwable.addSuppressed(t);
}
throw Throwables.propagate(throwable);
}
@@ -17,7 +17,8 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.algo.bitset.*;
import net.openhft.chronicle.algo.bitset.BitSetFrame;
import net.openhft.chronicle.algo.bitset.SingleThreadedFlatBitSetFrame;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.hash.Data;
import net.openhft.chronicle.hash.VanillaGlobalMutableState;
@@ -34,11 +35,9 @@ import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Closeable;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReferenceArray;
@@ -106,7 +105,6 @@ public class ReplicatedChronicleMap<K, V, R> extends VanillaChronicleMap<K, V, R
* Default value is 0, that corresponds to "unset" identifier value (valid ids are positive)
*/
private transient byte localIdentifier;
transient Set<Closeable> closeables;
/**
* Idiomatically {@code assignedModificationIterators} should be a {@link CopyOnWriteArraySet},
@@ -181,7 +179,6 @@ public class ReplicatedChronicleMap<K, V, R> extends VanillaChronicleMap<K, V, R
//noinspection unchecked
assignedModificationIterators = new ReplicatedChronicleMap.ModificationIterator[0];
modificationIterators = new AtomicReferenceArray<>(128);
closeables = new CopyOnWriteArraySet<>();
tierModIterFrame = new SingleThreadedFlatBitSetFrame(computeTierModIterBitSetSizeInBits());
remoteNodeCouldBootstrapFrom = new long[128];
}
@@ -259,26 +256,6 @@ public class ReplicatedChronicleMap<K, V, R> extends VanillaChronicleMap<K, V, R
bs.zeroOut(super.mapHeaderInnerSize(), this.mapHeaderInnerSize());
}
void addCloseable(Closeable closeable) {
closeables.add(closeable);
}
@Override
protected void doClose(Throwable thrown) {
for (Closeable closeable : closeables) {
try {
closeable.close();
} catch (Throwable t) {
if (thrown == null) {
thrown = t;
} else {
thrown.addSuppressed(t);
}
}
}
super.doClose(thrown);
}
@Override
public byte identifier() {
byte id = localIdentifier;
@@ -560,16 +537,10 @@ public class ReplicatedChronicleMap<K, V, R> extends VanillaChronicleMap<K, V, R
LOG.debug(message + "value=" + value + ")");
}
}
private ChainingInterface q() {
//noinspection unchecked
ChainingInterface queryContext = cxt.get();
if (queryContext == null) {
queryContext = new CompiledReplicatedMapQueryContext<>(this);
addContext(queryContext);
cxt.set(queryContext);
}
return queryContext;
@Override
ChainingInterface newQueryContext() {
return new CompiledReplicatedMapQueryContext<>(this);
}
@Override
@@ -599,15 +570,9 @@ public class ReplicatedChronicleMap<K, V, R> extends VanillaChronicleMap<K, V, R
}
}
private ChainingInterface i() {
//noinspection unchecked
ChainingInterface iterContext = cxt.get();
if (iterContext == null) {
iterContext = new CompiledReplicatedMapIterationContext<>(this);
addContext(iterContext);
cxt.set(iterContext);
}
return iterContext;
@Override
ChainingInterface newIterationContext() {
return new CompiledReplicatedMapIterationContext<>(this);
}
public CompiledReplicatedMapIterationContext<K, V, R> iterationContext() {
@@ -22,6 +22,7 @@ import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.bytes.PointerBytesStore;
import net.openhft.chronicle.core.io.Closeable;
import net.openhft.chronicle.hash.ChronicleHashClosedException;
import net.openhft.chronicle.hash.Data;
import net.openhft.chronicle.hash.impl.*;
import net.openhft.chronicle.hash.impl.stage.entry.LocksInterface;
@@ -68,6 +69,7 @@ public class VanillaChronicleMap<K, V, R>
/////////////////////////////////////////////////
private transient String name;
private transient String identityString;
public transient boolean couldNotDetermineAlignmentBeforeAllocation;
@@ -86,7 +88,7 @@ public class VanillaChronicleMap<K, V, R>
private transient boolean defaultEntryOperationsAndMethods;
public transient DefaultValueProvider<K, V> defaultValueProvider;
transient ThreadLocal<ChainingInterface> cxt;
transient ThreadLocal<ContextHolder> cxt;
public VanillaChronicleMap(ChronicleMapBuilder<K, V> builder) throws IOException {
super(builder);
@@ -162,6 +164,7 @@ public class VanillaChronicleMap<K, V, R>
}
private void initOwnTransients() {
identityString = makeIdentityString();
couldNotDetermineAlignmentBeforeAllocation =
greatestCommonDivisor((int) chunkSize, alignment) != alignment;
cxt = new ThreadLocal<>();
@@ -252,6 +255,10 @@ public class VanillaChronicleMap<K, V, R>
@Override
public String toIdentityString() {
return identityString;
}
private String makeIdentityString() {
if (chronicleSet != null)
return chronicleSet.toIdentityString();
return "ChronicleMap{" +
@@ -283,15 +290,25 @@ public class VanillaChronicleMap<K, V, R>
return (addr + alignment - 1) & ~(alignment - 1L);
}
private ChainingInterface q() {
//noinspection unchecked
ChainingInterface queryContext = cxt.get();
if (queryContext == null) {
queryContext = new CompiledMapQueryContext<>(this);
addContext(queryContext);
cxt.set(queryContext);
final ChainingInterface q() {
ContextHolder contextHolder = cxt.get();
if (contextHolder == null) {
ChainingInterface queryContext = newQueryContext();
contextHolder = new ContextHolder(queryContext);
addContext(contextHolder);
cxt.set(contextHolder);
return queryContext;
}
return queryContext;
ChainingInterface queryContext = contextHolder.get();
if (queryContext != null) {
return queryContext;
} else {
throw new ChronicleHashClosedException(this);
}
}
ChainingInterface newQueryContext() {
return new CompiledMapQueryContext<>(this);
}
public QueryContextInterface<K, V, R> mapContext() {
@@ -303,15 +320,25 @@ public class VanillaChronicleMap<K, V, R>
(c, m) -> new CompiledMapQueryContext<K, V, R>(c, m), this);
}
private ChainingInterface i() {
//noinspection unchecked
ChainingInterface iterContext = cxt.get();
if (iterContext == null) {
iterContext = new CompiledMapIterationContext<>(this);
addContext(iterContext);
cxt.set(iterContext);
final ChainingInterface i() {
ContextHolder contextHolder = cxt.get();
if (contextHolder == null) {
ChainingInterface iterationContext = newIterationContext();
contextHolder = new ContextHolder(iterationContext);
addContext(contextHolder);
cxt.set(contextHolder);
return iterationContext;
}
return iterContext;
ChainingInterface iterContext = contextHolder.get();
if (iterContext != null) {
return iterContext;
} else {
throw new ChronicleHashClosedException(this);
}
}
ChainingInterface newIterationContext() {
return new CompiledMapIterationContext<>(this);
}
public IterationContext<K, V, R> iterationContext() {