Read/Write ChronicleMap's boostrapping header with Wire

This commit is contained in:
leventov
2015-11-25 20:37:19 +03:00
parent d32f7e9e62
commit fe77012381
57 changed files with 816 additions and 197 deletions
@@ -16,7 +16,9 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.algo.MemoryUnit;
import net.openhft.chronicle.bytes.Byteable;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.hash.ChronicleHashBuilder;
@@ -31,11 +33,15 @@ import net.openhft.chronicle.map.replication.MapRemoteOperations;
import net.openhft.chronicle.set.ChronicleSetBuilder;
import net.openhft.chronicle.threads.NamedThreadFactory;
import net.openhft.chronicle.values.ValueModel;
import net.openhft.chronicle.wire.TextWire;
import net.openhft.chronicle.wire.Wire;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -120,6 +126,8 @@ public final class ChronicleMapBuilder<K, V> implements
return !isNaN(config);
}
private static int MAX_BOOTSTRAPPING_HEADER_SIZE = (int) MemoryUnit.KILOBYTES.toBytes(16);
// not final because of cloning
private ChronicleMapBuilderPrivateAPI<K> privateAPI = new ChronicleMapBuilderPrivateAPI<>(this);
@@ -515,7 +523,7 @@ public final class ChronicleMapBuilder<K, V> implements
if (worstAlignmentComputationRequiresValueSize(alignment)) {
long constantSizeBeforeAlignment = round(size);
if (constantlySizedValues()) {
// see segmentEntrySpaceInnerOffset()
// see tierEntrySpaceInnerOffset()
long totalDataSize = constantSizeBeforeAlignment + constantValueSize();
worstAlignment = (int) (alignAddr(totalDataSize, alignment) - totalDataSize);
} else {
@@ -1364,28 +1372,29 @@ public final class ChronicleMapBuilder<K, V> implements
for (int i = 0; i < 10; i++) {
long fileLength = file.length();
if (fileLength > 0) {
try (FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis)) {
Object m;
try {
m = ois.readObject();
} catch (ClassNotFoundException e) {
throw new AssertionError(e);
}
@SuppressWarnings("unchecked")
VanillaChronicleMap<K, V, ?> map = (VanillaChronicleMap<K, V, ?>) m;
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
FileChannel fileChannel = raf.getChannel();
ByteBuffer headerBuffer = ByteBuffer.allocate(MAX_BOOTSTRAPPING_HEADER_SIZE);
fileChannel.read(headerBuffer);
headerBuffer.flip();
Bytes<ByteBuffer> headerBytes = Bytes.wrapForRead(headerBuffer);
headerBytes.readLimit(headerBuffer.limit());
Wire wire = new TextWire(headerBytes);
VanillaChronicleMap<K, V, ?> map = wire.getValueIn().typedMarshallable();
map.initTransientsFromBuilder(this);
initTransientsFromReplication(map, singleHashReplication, channel);
map.initBeforeMapping(fis.getChannel());
fileChannel.position(headerBytes.readPosition());
map.initBeforeMapping(fileChannel);
long expectedFileLength = map.expectedFileSize();
if (expectedFileLength != fileLength) {
throw new IOException("The file " + file + " the map is serialized from " +
"has unexpected length " + fileLength + ", probably corrupted. " +
"Expected length is " + expectedFileLength);
}
map.createMappedStoreAndSegments(file);
map.createMappedStoreAndSegments(file, raf);
establishReplication(map, singleHashReplication, channel);
fis.getChannel().force(true);
fileChannel.force(true);
// TODO according to Self Boostrapping Data spec, should write "init complete"
// byte *after* the above force instruction, see HCOLL-396
return map;
@@ -1406,12 +1415,17 @@ public final class ChronicleMapBuilder<K, V> implements
VanillaChronicleMap<K, V, ?> map = newMap(singleHashReplication, channel);
try (FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(map);
oos.flush();
map.initBeforeMapping(fos.getChannel());
map.createMappedStoreAndSegments(file);
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
FileChannel fileChannel = raf.getChannel();
ByteBuffer headerBuffer = ByteBuffer.allocate(MAX_BOOTSTRAPPING_HEADER_SIZE);
Bytes<ByteBuffer> headerBytes = Bytes.wrapForWrite(headerBuffer);
Wire wire = new TextWire(headerBytes);
wire.getValueOut().typedMarshallable(map);
headerBuffer.limit((int) headerBytes.writePosition());
fileChannel.write(headerBuffer);
map.initBeforeMapping(fileChannel);
map.createMappedStoreAndSegments(file, raf);
}
return establishReplication(map, singleHashReplication, channel);
@@ -759,7 +759,7 @@ public class ReplicatedChronicleMap<K, V, R> extends VanillaChronicleMap<K, V, R
tierBulkOffsets.get(iterationMainSegmentsAreaOrTierBulk);
long tierBaseAddr = tierAddr(tierBulkData,
segmentIndexOrTierIndexOffsetWithinBulk);
long tierCountersAreaAddr = tierBaseAddr + segmentHashLookupOuterSize;
long tierCountersAreaAddr = tierBaseAddr + tierHashLookupOuterSize;
context.initSegmentIndex(
TierCountersArea.segmentIndex(tierCountersAreaAddr));
int tier = TierCountersArea.tier(tierCountersAreaAddr);
@@ -17,6 +17,7 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.IORuntimeException;
import net.openhft.chronicle.core.io.Closeable;
import net.openhft.chronicle.hash.Data;
import net.openhft.chronicle.hash.impl.VanillaChronicleHash;
@@ -30,6 +31,8 @@ import net.openhft.chronicle.map.impl.CompiledMapIterationContext;
import net.openhft.chronicle.map.impl.CompiledMapQueryContext;
import net.openhft.chronicle.map.impl.ret.InstanceReturnValue;
import net.openhft.chronicle.values.Values;
import net.openhft.chronicle.wire.WireIn;
import net.openhft.chronicle.wire.WireOut;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
@@ -49,18 +52,19 @@ public class VanillaChronicleMap<K, V, R>
/////////////////////////////////////////////////
// Value Data model
final Class<V> vClass;
public final SizeMarshaller valueSizeMarshaller;
public final SizedReader<V> originalValueReader;
public final DataAccess<V> originalValueDataAccess;
Class<V> valueClass;
public SizeMarshaller valueSizeMarshaller;
public SizedReader<V> valueReader;
public DataAccess<V> valueDataAccess;
public final boolean constantlySizedEntry;
public boolean constantlySizedEntry;
/////////////////////////////////////////////////
// Memory management and dependent fields
public final int alignment;
public final int worstAlignment;
public final boolean couldNotDetermineAlignmentBeforeAllocation;
public int alignment;
public int worstAlignment;
public transient boolean couldNotDetermineAlignmentBeforeAllocation;
/////////////////////////////////////////////////
// Behavior
@@ -78,23 +82,51 @@ public class VanillaChronicleMap<K, V, R>
public VanillaChronicleMap(ChronicleMapBuilder<K, V> builder) throws IOException {
super(builder);
SerializationBuilder<V> valueBuilder = builder.valueBuilder;
vClass = valueBuilder.tClass;
valueClass = valueBuilder.tClass;
valueSizeMarshaller = valueBuilder.sizeMarshaller();
originalValueReader = valueBuilder.reader();
originalValueDataAccess = valueBuilder.dataAccess();
valueReader = valueBuilder.reader();
valueDataAccess = valueBuilder.dataAccess();
constantlySizedEntry = builder.constantlySizedEntries();
// Concurrency (number of segments), memory management and dependent fields
alignment = builder.valueAlignment();
worstAlignment = builder.worstAlignment();
couldNotDetermineAlignmentBeforeAllocation =
greatestCommonDivisor((int) chunkSize, alignment) != alignment;
initTransientsFromBuilder(builder);
initTransients();
}
@Override
protected void readMarshallableFields(@NotNull WireIn wireIn) throws IORuntimeException {
super.readMarshallableFields(wireIn);
valueClass = wireIn.read(() -> "valueClass").typeLiteral();
valueSizeMarshaller = wireIn.read(() -> "valueSizeMarshaller").typedMarshallable();
valueReader = wireIn.read(() -> "valueReader").typedMarshallable();
valueDataAccess = wireIn.read(() -> "valueDataAccess").typedMarshallable();
constantlySizedEntry = wireIn.read(() -> "constantlySizedEntry").bool();
alignment = wireIn.read(() -> "alignment").int32();
worstAlignment = wireIn.read(() -> "worstAlignment").int32();
}
@Override
public void writeMarshallable(@NotNull WireOut wireOut) {
super.writeMarshallable(wireOut);
wireOut.write(() -> "valueClass").typeLiteral(valueClass);
wireOut.write(() -> "valueSizeMarshaller").typedMarshallable(valueSizeMarshaller);
wireOut.write(() -> "valueReader").typedMarshallable(valueReader);
wireOut.write(() -> "valueDataAccess").typedMarshallable(valueDataAccess);
wireOut.write(() -> "constantlySizedEntry").bool(constantlySizedEntry);
wireOut.write(() -> "alignment").int32(alignment);
wireOut.write(() -> "worstAlignment").int32(worstAlignment);
}
void initTransientsFromBuilder(ChronicleMapBuilder<K, V> builder) {
putReturnsNull = builder.putReturnsNull();
removeReturnsNull = builder.removeReturnsNull();
@@ -111,6 +143,8 @@ public class VanillaChronicleMap<K, V, R>
}
private void initOwnTransients() {
couldNotDetermineAlignmentBeforeAllocation =
greatestCommonDivisor((int) chunkSize, alignment) != alignment;
cxt = new ThreadLocal<>();
}
@@ -120,8 +154,8 @@ public class VanillaChronicleMap<K, V, R>
}
public final V checkValue(Object value) {
if (!vClass.isInstance(value)) {
throw new ClassCastException("Value must be a " + vClass.getName() +
if (!valueClass.isInstance(value)) {
throw new ClassCastException("Value must be a " + valueClass.getName() +
" but was a " + value.getClass());
}
return (V) value;
@@ -129,12 +163,12 @@ public class VanillaChronicleMap<K, V, R>
@Override
public Class<K> keyClass() {
return kClass;
return keyClass;
}
@Override
public Class<V> valueClass() {
return vClass;
return valueClass;
}
static <T> T newInstance(Class<T> aClass, boolean isKey) {
@@ -28,7 +28,7 @@ public class WrappedValueInstanceDataHolder<V> {
@StageRef VanillaChronicleMapHolder<?, V, ?> mh;
private final DataAccess<V> wrappedValueDataAccess = mh.m().originalValueDataAccess.copy();
private final DataAccess<V> wrappedValueDataAccess = mh.m().valueDataAccess.copy();
private WrappedValueInstanceDataHolder<V> next;
@@ -28,6 +28,6 @@ public class ValueBytesInterop<V> {
@StageRef VanillaChronicleMapHolder<?, V, ?> mh;
public final SizedReader<V> valueReader = copyIfNeeded(mh.m().originalValueReader);
public final SizedReader<V> valueReader = copyIfNeeded(mh.m().valueReader);
}
@@ -55,7 +55,7 @@ public abstract class MapQuery<K, V, R> extends HashQuery<K>
@StageRef public MapAbsentEntryHolder<K, V> absent;
final DataAccess<V> innerInputValueDataAccess = mh.m().originalValueDataAccess.copy();
final DataAccess<V> innerInputValueDataAccess = mh.m().valueDataAccess.copy();
/** Same as {@link #inputKeyDataAccessInitialized} */
@Stage("InputValueDataAccess") private boolean inputValueDataAccessInitialized = false;