mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
HCOLL-378 #36 Add ChronicleHashBuilder.recoverPersistedTo() and ChronicleHashInstanceBuilder.recover() functionality
This commit is contained in:
@@ -377,4 +377,24 @@ public final class BigSegmentHeader implements SegmentHeader {
|
||||
public void downgradeWriteToReadLock(long address) {
|
||||
LOCK.downgradeWriteToReadLock(A, null, address + LOCK_OFFSET);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetLock(long address) {
|
||||
LOCK.reset(A, null, address + LOCK_OFFSET);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long resetLockState() {
|
||||
return LOCK.resetState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLockState(long address) {
|
||||
return LOCK.getState(A, null, address + LOCK_OFFSET);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String lockStateToString(long lockState) {
|
||||
return LOCK.toString(lockState);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ public abstract class CompactOffHeapLinearHashTable {
|
||||
return (entry >>> keyBits) & valueMask;
|
||||
}
|
||||
|
||||
long entry(long key, long value) {
|
||||
public long entry(long key, long value) {
|
||||
return key | (value << keyBits);
|
||||
}
|
||||
|
||||
@@ -143,9 +143,9 @@ public abstract class CompactOffHeapLinearHashTable {
|
||||
writeEntryVolatile(addr, pos, key(currentEntry), value);
|
||||
}
|
||||
|
||||
abstract void writeEntry(long addr, long pos, long newEntry);
|
||||
public abstract void writeEntry(long addr, long pos, long newEntry);
|
||||
|
||||
abstract void clearEntry(long addr, long pos);
|
||||
public abstract void clearEntry(long addr, long pos);
|
||||
|
||||
/**
|
||||
* Returns "insert" position in terms of consequent putValue()
|
||||
@@ -176,4 +176,17 @@ public abstract class CompactOffHeapLinearHashTable {
|
||||
clearEntry(addr, posToRemove);
|
||||
return posToRemove;
|
||||
}
|
||||
|
||||
public void insert(long addr, long key, long value) {
|
||||
long startPos = hlPos(key);
|
||||
long pos = startPos;
|
||||
do {
|
||||
if (empty(readEntry(addr, pos))) {
|
||||
writeEntry(addr, pos, entry(key, value));
|
||||
return;
|
||||
}
|
||||
pos = step(pos);
|
||||
} while (pos != startPos);
|
||||
throw new AssertionError("HashLookup overflow should never occur");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,12 +52,12 @@ public final class IntCompactOffHeapLinearHashTable extends CompactOffHeapLinear
|
||||
}
|
||||
|
||||
@Override
|
||||
void writeEntry(long addr, long pos, long newEntry) {
|
||||
public void writeEntry(long addr, long pos, long newEntry) {
|
||||
OS.memory().writeInt(addr + pos, (int) newEntry);
|
||||
}
|
||||
|
||||
@Override
|
||||
void clearEntry(long addr, long pos) {
|
||||
public void clearEntry(long addr, long pos) {
|
||||
OS.memory().writeInt(addr + pos, 0);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -52,12 +52,12 @@ public final class LongCompactOffHeapLinearHashTable extends CompactOffHeapLinea
|
||||
}
|
||||
|
||||
@Override
|
||||
void writeEntry(long addr, long pos, long newEntry) {
|
||||
public void writeEntry(long addr, long pos, long newEntry) {
|
||||
OS.memory().writeLong(addr + pos, newEntry);
|
||||
}
|
||||
|
||||
@Override
|
||||
void clearEntry(long addr, long pos) {
|
||||
public void clearEntry(long addr, long pos) {
|
||||
OS.memory().writeLong(addr + pos, 0L);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,4 +59,9 @@ public interface SegmentHeader {
|
||||
void writeUnlock(long address);
|
||||
void downgradeWriteToUpdateLock(long address);
|
||||
void downgradeWriteToReadLock(long address);
|
||||
|
||||
void resetLock(long address);
|
||||
long resetLockState();
|
||||
long getLockState(long address);
|
||||
String lockStateToString(long lockState);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package net.openhft.chronicle.hash.impl;
|
||||
|
||||
import net.openhft.chronicle.algo.bytes.Access;
|
||||
import net.openhft.chronicle.algo.locks.*;
|
||||
import net.openhft.chronicle.bytes.BytesStore;
|
||||
import net.openhft.chronicle.bytes.MappedBytesStoreFactory;
|
||||
@@ -30,6 +29,7 @@ import net.openhft.chronicle.hash.serialization.SizeMarshaller;
|
||||
import net.openhft.chronicle.hash.serialization.SizedReader;
|
||||
import net.openhft.chronicle.hash.serialization.impl.SerializationBuilder;
|
||||
import net.openhft.chronicle.map.ChronicleMapBuilder;
|
||||
import net.openhft.chronicle.map.impl.IterationContext;
|
||||
import net.openhft.chronicle.values.Values;
|
||||
import net.openhft.chronicle.wire.Marshallable;
|
||||
import net.openhft.chronicle.wire.WireIn;
|
||||
@@ -43,12 +43,15 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static java.lang.Long.numberOfTrailingZeros;
|
||||
import static java.lang.Math.max;
|
||||
import static java.nio.channels.FileChannel.MapMode.READ_WRITE;
|
||||
import static java.util.stream.Collectors.toSet;
|
||||
import static net.openhft.chronicle.algo.MemoryUnit.*;
|
||||
import static net.openhft.chronicle.algo.bytes.Access.nativeAccess;
|
||||
import static net.openhft.chronicle.bytes.NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity;
|
||||
import static net.openhft.chronicle.core.OS.pageAlign;
|
||||
import static net.openhft.chronicle.hash.impl.CompactOffHeapLinearHashTable.*;
|
||||
@@ -120,7 +123,7 @@ public abstract class VanillaChronicleHash<K,
|
||||
long maxExtraTiers;
|
||||
long tierBulkSizeInBytes;
|
||||
long tierBulkInnerOffsetToTiers;
|
||||
protected long tiersInBulk;
|
||||
public long tiersInBulk;
|
||||
protected int log2TiersInBulk;
|
||||
|
||||
/////////////////////////////////////////////////
|
||||
@@ -325,7 +328,7 @@ public abstract class VanillaChronicleHash<K,
|
||||
return Values.newNativeReference(VanillaGlobalMutableState.class);
|
||||
}
|
||||
|
||||
protected VanillaGlobalMutableState globalMutableState() {
|
||||
public VanillaGlobalMutableState globalMutableState() {
|
||||
return globalMutableState;
|
||||
}
|
||||
|
||||
@@ -383,7 +386,8 @@ public abstract class VanillaChronicleHash<K,
|
||||
}
|
||||
}
|
||||
|
||||
public final void initBeforeMapping(FileChannel ch, long headerSize) throws IOException {
|
||||
public final void initBeforeMapping(File file, FileChannel ch, long headerSize)
|
||||
throws IOException {
|
||||
this.headerSize = roundUpMapHeaderSize(headerSize);
|
||||
if (!createdOrInMemory) {
|
||||
// This block is for reading segmentHeadersOffset before main mapping
|
||||
@@ -394,7 +398,7 @@ public abstract class VanillaChronicleHash<K,
|
||||
if (ch.read(globalMutableStateBuffer,
|
||||
this.headerSize + GLOBAL_MUTABLE_STATE_VALUE_OFFSET +
|
||||
globalMutableStateBuffer.position()) == -1) {
|
||||
throw new RuntimeException();
|
||||
throw new RuntimeException(file + " truncated");
|
||||
}
|
||||
}
|
||||
globalMutableStateBuffer.flip();
|
||||
@@ -411,15 +415,7 @@ public abstract class VanillaChronicleHash<K,
|
||||
}
|
||||
|
||||
public final void createMappedStoreAndSegments(BytesStore bytesStore) throws IOException {
|
||||
if (bytesStore.start() != 0) {
|
||||
throw new AssertionError("bytes store " + bytesStore + " starts from " +
|
||||
bytesStore.start() + ", 0 expected");
|
||||
}
|
||||
this.bs = bytesStore;
|
||||
globalMutableState.bytesStore(bs, headerSize + GLOBAL_MUTABLE_STATE_VALUE_OFFSET,
|
||||
globalMutableState.maxSize());
|
||||
|
||||
onHeaderCreated();
|
||||
initBytesStoreAndHeadersViews(bytesStore);
|
||||
|
||||
segmentHeadersOffset = segmentHeadersOffset();
|
||||
|
||||
@@ -431,13 +427,29 @@ public abstract class VanillaChronicleHash<K,
|
||||
// write the segment headers offset after zeroing out
|
||||
globalMutableState.setSegmentHeadersOffset(segmentHeadersOffset);
|
||||
} else {
|
||||
if (globalMutableState.getAllocatedExtraTierBulks() > 0) {
|
||||
appendBulkData(0, globalMutableState.getAllocatedExtraTierBulks() - 1,
|
||||
bs, sizeInBytesWithoutTiers());
|
||||
}
|
||||
initBulks();
|
||||
}
|
||||
}
|
||||
|
||||
private void initBulks() {
|
||||
if (globalMutableState.getAllocatedExtraTierBulks() > 0) {
|
||||
appendBulkData(0, globalMutableState.getAllocatedExtraTierBulks() - 1,
|
||||
bs, sizeInBytesWithoutTiers());
|
||||
}
|
||||
}
|
||||
|
||||
private void initBytesStoreAndHeadersViews(BytesStore bytesStore) {
|
||||
if (bytesStore.start() != 0) {
|
||||
throw new AssertionError("bytes store " + bytesStore + " starts from " +
|
||||
bytesStore.start() + ", 0 expected");
|
||||
}
|
||||
this.bs = bytesStore;
|
||||
globalMutableState.bytesStore(bs, headerSize + GLOBAL_MUTABLE_STATE_VALUE_OFFSET,
|
||||
globalMutableState.maxSize());
|
||||
|
||||
onHeaderCreated();
|
||||
}
|
||||
|
||||
public final void createMappedStoreAndSegments(File file, RandomAccessFile raf)
|
||||
throws IOException {
|
||||
// TODO this method had been moved -- not clear where
|
||||
@@ -447,6 +459,50 @@ public abstract class VanillaChronicleHash<K,
|
||||
createMappedStoreAndSegments(map(raf, mapSize, 0));
|
||||
}
|
||||
|
||||
public final void basicRecover(File file, RandomAccessFile raf) throws IOException {
|
||||
this.file = file;
|
||||
long segmentHeadersOffset = computeSegmentHeadersOffset();
|
||||
long sizeInBytesWithoutTiers = computeSizeInBytesWithoutTiers(segmentHeadersOffset);
|
||||
long sizeBeyondSegments = Math.max(raf.length() - sizeInBytesWithoutTiers, 0);
|
||||
int allocatedExtraTierBulks = (int) (sizeBeyondSegments / tierBulkSizeInBytes);
|
||||
long mapSize = pageAlign(sizeInBytesWithoutTiers +
|
||||
allocatedExtraTierBulks * tierBulkSizeInBytes);
|
||||
initBytesStoreAndHeadersViews(map(raf, mapSize, 0));
|
||||
|
||||
resetGlobalMutableStateLock(file);
|
||||
recoverAllocatedExtraTierBulks(file, allocatedExtraTierBulks);
|
||||
recoverSegmentHeadersOffset(file, segmentHeadersOffset);
|
||||
initBulks();
|
||||
}
|
||||
|
||||
private void resetGlobalMutableStateLock(File file) {
|
||||
long lockAddr = globalMutableStateAddress() + GLOBAL_MUTABLE_STATE_LOCK_OFFSET;
|
||||
LockingStrategy lockingStrategy = globalMutableStateLockingStrategy;
|
||||
long lockState = lockingStrategy.getState(nativeAccess(), null, lockAddr);
|
||||
if (lockState != lockingStrategy.resetState()) {
|
||||
LOG.error("global mutable state lock of map at {} is not clear: {}",
|
||||
file, lockingStrategy.toString(lockState));
|
||||
lockingStrategy.reset(nativeAccess(), null, lockAddr);
|
||||
}
|
||||
}
|
||||
|
||||
private void recoverAllocatedExtraTierBulks(File file, int allocatedExtraTierBulks) {
|
||||
if (globalMutableState.getAllocatedExtraTierBulks() != allocatedExtraTierBulks) {
|
||||
LOG.error("allocated extra tier bulks counter corrupted, or the map file {} " +
|
||||
"is truncated. stored: {}, should be: {}", file,
|
||||
globalMutableState.getAllocatedExtraTierBulks(), allocatedExtraTierBulks);
|
||||
globalMutableState.setAllocatedExtraTierBulks(allocatedExtraTierBulks);
|
||||
}
|
||||
}
|
||||
|
||||
private void recoverSegmentHeadersOffset(File file, long segmentHeadersOffset) {
|
||||
if (globalMutableState.getSegmentHeadersOffset() != segmentHeadersOffset) {
|
||||
LOG.error("segment headers offset of map at {} corrupted. stored: {}, should be: {}",
|
||||
file, globalMutableState.getSegmentHeadersOffset(), segmentHeadersOffset);
|
||||
globalMutableState.setSegmentHeadersOffset(segmentHeadersOffset);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean persisted() {
|
||||
return file != null;
|
||||
}
|
||||
@@ -480,7 +536,7 @@ public abstract class VanillaChronicleHash<K,
|
||||
}
|
||||
|
||||
private void zeroOutNewlyMappedTier(BytesStore bytesStore, long tierOffset) {
|
||||
// Zero out hash lookup, tier data and free list bit set. Leave entry space.
|
||||
// Zero out hash lookup, tier data and free list bit set. Leave entry space dirty.
|
||||
bytesStore.zeroOut(tierOffset, tierOffset + tierSize - tierEntrySpaceOuterSize);
|
||||
}
|
||||
|
||||
@@ -501,15 +557,19 @@ public abstract class VanillaChronicleHash<K,
|
||||
|
||||
private long segmentHeadersOffset() {
|
||||
if (createdOrInMemory) {
|
||||
// Align segment headers on page boundary to minimize number of pages that
|
||||
// segment headers span
|
||||
long reserved = RESERVED_GLOBAL_MUTABLE_STATE_BYTES - globalMutableStateTotalUsedSize();
|
||||
return pageAlign(mapHeaderInnerSize() + reserved);
|
||||
return computeSegmentHeadersOffset();
|
||||
} else {
|
||||
return globalMutableState.getSegmentHeadersOffset();
|
||||
}
|
||||
}
|
||||
|
||||
private long computeSegmentHeadersOffset() {
|
||||
long reserved = RESERVED_GLOBAL_MUTABLE_STATE_BYTES - globalMutableStateTotalUsedSize();
|
||||
// Align segment headers on page boundary to minimize number of pages that
|
||||
// segment headers span
|
||||
return pageAlign(mapHeaderInnerSize() + reserved);
|
||||
}
|
||||
|
||||
public long mapHeaderInnerSize() {
|
||||
return headerSize + globalMutableStateTotalUsedSize();
|
||||
}
|
||||
@@ -520,7 +580,11 @@ public abstract class VanillaChronicleHash<K,
|
||||
}
|
||||
|
||||
public final long sizeInBytesWithoutTiers() {
|
||||
return segmentHeadersOffset() + actualSegments * (segmentHeaderSize + tierSize);
|
||||
return computeSizeInBytesWithoutTiers(segmentHeadersOffset());
|
||||
}
|
||||
|
||||
private long computeSizeInBytesWithoutTiers(long segmentHeadersOffset) {
|
||||
return segmentHeadersOffset + actualSegments * (segmentHeaderSize + tierSize);
|
||||
}
|
||||
|
||||
public final long expectedFileSize() {
|
||||
@@ -632,12 +696,12 @@ public abstract class VanillaChronicleHash<K,
|
||||
public void globalMutableStateLock() {
|
||||
globalMutableStateLockAcquisitionStrategy.acquire(
|
||||
globalMutableStateLockTryAcquireOperation, globalMutableStateLockingStrategy,
|
||||
Access.nativeAccess(), null,
|
||||
nativeAccess(), null,
|
||||
globalMutableStateAddress() + GLOBAL_MUTABLE_STATE_LOCK_OFFSET);
|
||||
}
|
||||
|
||||
public void globalMutableStateUnlock() {
|
||||
globalMutableStateLockingStrategy.unlock(Access.nativeAccess(), null,
|
||||
globalMutableStateLockingStrategy.unlock(nativeAccess(), null,
|
||||
globalMutableStateAddress() + GLOBAL_MUTABLE_STATE_LOCK_OFFSET);
|
||||
}
|
||||
|
||||
@@ -687,7 +751,7 @@ public abstract class VanillaChronicleHash<K,
|
||||
mapTierBulks(allocatedExtraTierBulks);
|
||||
|
||||
// integer overflow aware
|
||||
long firstTierIndex = actualSegments + 1L + ((long) allocatedExtraTierBulks) * tiersInBulk;
|
||||
long firstTierIndex = extraTierIndexToTierIndex(allocatedExtraTierBulks * tiersInBulk);
|
||||
BytesStore tierBytesStore = tierBytesStore(firstTierIndex);
|
||||
long firstTierOffset = tierBytesOffset(firstTierIndex);
|
||||
if (tierBulkInnerOffsetToTiers > 0) {
|
||||
@@ -695,10 +759,20 @@ public abstract class VanillaChronicleHash<K,
|
||||
tierBytesStore.zeroOut(firstTierOffset - tierBulkInnerOffsetToTiers, firstTierOffset);
|
||||
}
|
||||
|
||||
// Link newly allocated tiers into free tiers list
|
||||
long lastTierIndex = firstTierIndex + tiersInBulk - 1;
|
||||
linkAndZeroOutFreeTiers(firstTierIndex, lastTierIndex);
|
||||
|
||||
// TODO HCOLL-397 insert msync here!
|
||||
|
||||
// after we are sure the new bulk is initialized, update the global mutable state
|
||||
globalMutableState.setAllocatedExtraTierBulks(allocatedExtraTierBulks + 1);
|
||||
globalMutableState.setFirstFreeTierIndex(firstTierIndex);
|
||||
}
|
||||
|
||||
public void linkAndZeroOutFreeTiers(long firstTierIndex, long lastTierIndex) {
|
||||
for (long tierIndex = firstTierIndex; tierIndex <= lastTierIndex; tierIndex++) {
|
||||
long tierOffset = tierBytesOffset(tierIndex);
|
||||
BytesStore tierBytesStore = tierBytesStore(tierIndex);
|
||||
zeroOutNewlyMappedTier(tierBytesStore, tierOffset);
|
||||
if (tierIndex < lastTierIndex) {
|
||||
long tierCountersAreaOffset = tierOffset + tierHashLookupOuterSize;
|
||||
@@ -706,12 +780,10 @@ public abstract class VanillaChronicleHash<K,
|
||||
tierIndex + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO HCOLL-397 insert msync here!
|
||||
|
||||
// after we are sure the new bulk is initialized, update the global mutable state
|
||||
globalMutableState.setAllocatedExtraTierBulks(allocatedExtraTierBulks + 1);
|
||||
globalMutableState.setFirstFreeTierIndex(firstTierIndex);
|
||||
public long extraTierIndexToTierIndex(long extraTierIndex) {
|
||||
return actualSegments + extraTierIndex + 1;
|
||||
}
|
||||
|
||||
public long tierIndexToBaseAddr(long tierIndex) {
|
||||
|
||||
@@ -24,5 +24,9 @@ public interface ChecksumStrategy {
|
||||
|
||||
boolean innerCheckSum();
|
||||
|
||||
int computeChecksum();
|
||||
|
||||
int storedChecksum();
|
||||
|
||||
long extraEntryBytes();
|
||||
}
|
||||
|
||||
+8
-2
@@ -35,7 +35,8 @@ public class HashEntryChecksumStrategy implements ChecksumStrategy {
|
||||
s.segmentBS.writeInt(e.entryEnd(), checksum);
|
||||
}
|
||||
|
||||
private int computeChecksum() {
|
||||
@Override
|
||||
public int computeChecksum() {
|
||||
long keyHashCode = h.keyHashCode();
|
||||
|
||||
long keyEnd = e.keyEnd();
|
||||
@@ -55,11 +56,16 @@ public class HashEntryChecksumStrategy implements ChecksumStrategy {
|
||||
|
||||
@Override
|
||||
public boolean innerCheckSum() {
|
||||
int oldChecksum = s.segmentBS.readInt(e.entryEnd());
|
||||
int oldChecksum = storedChecksum();
|
||||
int checksum = computeChecksum();
|
||||
return oldChecksum == checksum;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int storedChecksum() {
|
||||
return s.segmentBS.readInt(e.entryEnd());
|
||||
}
|
||||
|
||||
@Override
|
||||
public long extraEntryBytes() {
|
||||
return CHECKSUM_STORED_BYTES;
|
||||
|
||||
@@ -108,7 +108,7 @@ public abstract class HashEntryStages<K> implements HashEntry<K>, ChecksumEntry
|
||||
return keyOffset + keySize;
|
||||
}
|
||||
|
||||
protected long entryEnd() {
|
||||
public long entryEnd() {
|
||||
return keyEnd();
|
||||
}
|
||||
|
||||
|
||||
@@ -67,9 +67,7 @@ public abstract class HashLookupSearch {
|
||||
return hl().value(entry);
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("MultiMap is full, that most likely means you " +
|
||||
"misconfigured entrySize/chunkSize, and entries tend to take less chunks than " +
|
||||
"expected");
|
||||
throw new IllegalStateException("HashLookup overflow should never occur");
|
||||
}
|
||||
|
||||
public void found() {
|
||||
|
||||
@@ -29,6 +29,16 @@ public enum NoChecksumStrategy implements ChecksumStrategy {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int computeChecksum() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int storedChecksum() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long extraEntryBytes() {
|
||||
return 0; // no extra bytes to store checksum
|
||||
|
||||
@@ -58,8 +58,8 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface {
|
||||
|
||||
public abstract boolean segmentIndexInit();
|
||||
|
||||
@Stage("SegmentHeader") long segmentHeaderAddress;
|
||||
@Stage("SegmentHeader") SegmentHeader segmentHeader = null;
|
||||
@Stage("SegmentHeader") public long segmentHeaderAddress;
|
||||
@Stage("SegmentHeader") public SegmentHeader segmentHeader = null;
|
||||
|
||||
private void initSegmentHeader() {
|
||||
segmentHeaderAddress = hh.h().segmentHeaderAddress(segmentIndex);
|
||||
@@ -82,7 +82,7 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface {
|
||||
}
|
||||
}
|
||||
|
||||
long lowestPossiblyFreeChunk() {
|
||||
public long lowestPossiblyFreeChunk() {
|
||||
if (tier == 0) {
|
||||
return segmentHeader.lowestPossiblyFreeChunk(segmentHeaderAddress);
|
||||
} else {
|
||||
@@ -513,7 +513,7 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface {
|
||||
tier = 0;
|
||||
}
|
||||
|
||||
private void initSegmentTier(int tier, long tierIndex) {
|
||||
public void initSegmentTier(int tier, long tierIndex) {
|
||||
this.tier = tier;
|
||||
this.tierIndex = tierIndex;
|
||||
assert tierIndex > 0;
|
||||
@@ -584,7 +584,7 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface {
|
||||
|
||||
@Stage("Segment") public final PointerBytesStore segmentBS = new PointerBytesStore();
|
||||
@Stage("Segment") public final Bytes segmentBytes = new VanillaBytes(segmentBS);
|
||||
@Stage("Segment") private final ReusableBitSet freeList = new ReusableBitSet(
|
||||
@Stage("Segment") public final ReusableBitSet freeList = new ReusableBitSet(
|
||||
new SingleThreadedFlatBitSetFrame(LONGS.align(hh.h().actualChunksPerSegmentTier, BITS)),
|
||||
Access.nativeAccess(), null, 0);
|
||||
@Stage("Segment") long entrySpaceOffset = 0;
|
||||
|
||||
@@ -98,7 +98,7 @@ public abstract class HashSegmentIteration<K, E extends HashEntry<K>>
|
||||
}
|
||||
}
|
||||
|
||||
private <T> boolean forEachTierEntryWhile(
|
||||
public <T> boolean forEachTierEntryWhile(
|
||||
Predicate<? super T> predicate,
|
||||
int currentTier, long currentTierBaseAddr, long tierIndex) {
|
||||
long leftEntries = tierEntriesForIteration();
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright (C) 2015 higherfrequencytrading.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.openhft.chronicle.hash.impl.stage.iter;
|
||||
|
||||
import net.openhft.chronicle.hash.VanillaGlobalMutableState;
|
||||
import net.openhft.chronicle.hash.impl.TierCountersArea;
|
||||
import net.openhft.chronicle.hash.impl.VanillaChronicleHash;
|
||||
import net.openhft.chronicle.hash.impl.VanillaChronicleHashHolder;
|
||||
import net.openhft.chronicle.hash.impl.stage.entry.SegmentStages;
|
||||
import net.openhft.chronicle.hash.impl.stage.hash.LogHolder;
|
||||
import net.openhft.chronicle.map.impl.IterationContext;
|
||||
import net.openhft.sg.StageRef;
|
||||
import net.openhft.sg.Staged;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
@Staged
|
||||
public abstract class SegmentsRecovery implements IterationContext {
|
||||
|
||||
@StageRef VanillaChronicleHashHolder<?> hh;
|
||||
@StageRef SegmentStages s;
|
||||
@StageRef TierRecovery tierRecovery;
|
||||
@StageRef LogHolder lh;
|
||||
@StageRef HashSegmentIteration iteration;
|
||||
|
||||
@Override
|
||||
public void recoverSegments() {
|
||||
Logger log = lh.LOG;
|
||||
VanillaChronicleHash<?, ?, ?, ?> h = hh.h();
|
||||
for (int segmentIndex = 0; segmentIndex < h.actualSegments; segmentIndex++) {
|
||||
s.initSegmentIndex(segmentIndex);
|
||||
resetSegmentLock();
|
||||
zeroOutFirstSegmentTierCountersArea();
|
||||
tierRecovery.recoverTier(segmentIndex);
|
||||
}
|
||||
|
||||
VanillaGlobalMutableState globalMutableState = h.globalMutableState();
|
||||
long storedExtraTiersInUse = globalMutableState.getExtraTiersInUse();
|
||||
long allocatedExtraTiers = globalMutableState.getAllocatedExtraTierBulks() * h.tiersInBulk;
|
||||
long expectedExtraTiersInUse =
|
||||
Math.max(0, Math.min(storedExtraTiersInUse, allocatedExtraTiers));
|
||||
long actualExtraTiersInUse = 0;
|
||||
long firstFreeExtraTierIndex = -1;
|
||||
for (long extraTierIndex = 0; extraTierIndex < expectedExtraTiersInUse; extraTierIndex++) {
|
||||
long tierIndex = h.extraTierIndexToTierIndex(extraTierIndex);
|
||||
// `tier` is unused in recoverTier(), 0 should be a safe value
|
||||
s.initSegmentTier(0, tierIndex);
|
||||
int segmentIndex = tierRecovery.recoverTier(-1);
|
||||
if (segmentIndex > 0) {
|
||||
long tierCountersAreaAddr = s.tierCountersAreaAddr();
|
||||
int storedSegmentIndex = TierCountersArea.segmentIndex(tierCountersAreaAddr);
|
||||
if (storedSegmentIndex != segmentIndex) {
|
||||
log.error("wrong segment index stored in tier counters area " +
|
||||
"of tier with index {}: {}, should be, based on entries: {}",
|
||||
tierIndex, storedSegmentIndex, segmentIndex);
|
||||
TierCountersArea.segmentIndex(tierCountersAreaAddr, segmentIndex);
|
||||
}
|
||||
s.nextTierIndex(0);
|
||||
|
||||
s.initSegmentIndex(segmentIndex);
|
||||
s.goToLastTier();
|
||||
s.nextTierIndex(tierIndex);
|
||||
|
||||
TierCountersArea.prevTierIndex(tierCountersAreaAddr, s.tierIndex);
|
||||
TierCountersArea.tier(tierCountersAreaAddr, s.tier + 1);
|
||||
actualExtraTiersInUse = extraTierIndex + 1;
|
||||
} else {
|
||||
firstFreeExtraTierIndex = extraTierIndex;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (storedExtraTiersInUse != actualExtraTiersInUse) {
|
||||
log.error("wrong number of actual tiers in use in global mutable state, stored: {}, " +
|
||||
"should be: {}", storedExtraTiersInUse, actualExtraTiersInUse);
|
||||
globalMutableState.setExtraTiersInUse(actualExtraTiersInUse);
|
||||
}
|
||||
|
||||
long firstFreeTierIndex;
|
||||
if (firstFreeExtraTierIndex == -1) {
|
||||
if (allocatedExtraTiers > expectedExtraTiersInUse) {
|
||||
firstFreeTierIndex = h.extraTierIndexToTierIndex(expectedExtraTiersInUse);
|
||||
|
||||
} else {
|
||||
firstFreeTierIndex = 0;
|
||||
}
|
||||
} else {
|
||||
firstFreeTierIndex = h.extraTierIndexToTierIndex(firstFreeExtraTierIndex);
|
||||
}
|
||||
if (firstFreeTierIndex > 0) {
|
||||
long lastTierIndex = h.extraTierIndexToTierIndex(allocatedExtraTiers - 1);
|
||||
h.linkAndZeroOutFreeTiers(firstFreeTierIndex, lastTierIndex);
|
||||
}
|
||||
long storedFirstFreeTierIndex = globalMutableState.getFirstFreeTierIndex();
|
||||
if (storedFirstFreeTierIndex != firstFreeTierIndex) {
|
||||
log.error("wrong first free tier index in global mutable state, stored: {}, " +
|
||||
"should be: {}", storedFirstFreeTierIndex, firstFreeTierIndex);
|
||||
}
|
||||
|
||||
removeDuplicatesInSegments();
|
||||
}
|
||||
|
||||
private void removeDuplicatesInSegments() {
|
||||
VanillaChronicleHash<?, ?, ?, ?> h = hh.h();
|
||||
for (int segmentIndex = 0; segmentIndex < h.actualSegments; segmentIndex++) {
|
||||
s.initSegmentIndex(segmentIndex);
|
||||
s.initSegmentTier();
|
||||
s.goToLastTier();
|
||||
while (true) {
|
||||
tierRecovery.removeDuplicatesInSegment();
|
||||
if (s.tier > 0) {
|
||||
s.prevTier();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void resetSegmentLock() {
|
||||
long lockState = s.segmentHeader.getLockState(s.segmentHeaderAddress);
|
||||
if (lockState != s.segmentHeader.resetLockState()) {
|
||||
lh.LOG.error("lock of segment {} is not clear: {}",
|
||||
s.segmentIndex, s.segmentHeader.lockStateToString(lockState));
|
||||
s.segmentHeader.resetLock(s.segmentHeaderAddress);
|
||||
}
|
||||
}
|
||||
|
||||
private void zeroOutFirstSegmentTierCountersArea() {
|
||||
s.nextTierIndex(0);
|
||||
if (s.prevTierIndex() != 0) {
|
||||
lh.LOG.error("stored prev tier index in first tier of segment {}: {}, should be 0",
|
||||
s.segmentIndex, s.prevTierIndex());
|
||||
s.prevTierIndex(0);
|
||||
}
|
||||
long tierCountersAreaAddr = s.tierCountersAreaAddr();
|
||||
if (TierCountersArea.segmentIndex(tierCountersAreaAddr) != 0) {
|
||||
lh.LOG.error("stored segment index in first tier of segment {}: {}, should be 0",
|
||||
s.segmentIndex, TierCountersArea.segmentIndex(tierCountersAreaAddr));
|
||||
TierCountersArea.segmentIndex(tierCountersAreaAddr, 0);
|
||||
}
|
||||
if (TierCountersArea.tier(tierCountersAreaAddr) != 0) {
|
||||
lh.LOG.error("stored tier in first tier of segment {}: {}, should be 0",
|
||||
s.segmentIndex, TierCountersArea.tier(tierCountersAreaAddr));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
/*
|
||||
* Copyright (C) 2015 higherfrequencytrading.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.openhft.chronicle.hash.impl.stage.iter;
|
||||
|
||||
import net.openhft.chronicle.bytes.BytesUtil;
|
||||
import net.openhft.chronicle.hash.ChronicleHashRecoveryFailedException;
|
||||
import net.openhft.chronicle.hash.Data;
|
||||
import net.openhft.chronicle.hash.ExternalHashQueryContext;
|
||||
import net.openhft.chronicle.hash.HashEntry;
|
||||
import net.openhft.chronicle.hash.impl.CompactOffHeapLinearHashTable;
|
||||
import net.openhft.chronicle.hash.impl.VanillaChronicleHash;
|
||||
import net.openhft.chronicle.hash.impl.stage.entry.SegmentStages;
|
||||
import net.openhft.chronicle.hash.impl.stage.hash.LogHolder;
|
||||
import net.openhft.chronicle.map.ExternalMapQueryContext;
|
||||
import net.openhft.chronicle.map.MapEntry;
|
||||
import net.openhft.chronicle.map.VanillaChronicleMap;
|
||||
import net.openhft.chronicle.map.impl.VanillaChronicleMapHolder;
|
||||
import net.openhft.chronicle.map.impl.stage.entry.MapEntryStages;
|
||||
import net.openhft.sg.StageRef;
|
||||
import net.openhft.sg.Staged;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
@Staged
|
||||
public class TierRecovery {
|
||||
|
||||
@StageRef LogHolder lh;
|
||||
@StageRef VanillaChronicleMapHolder<?, ?, ?> mh;
|
||||
@StageRef SegmentStages s;
|
||||
@StageRef MapEntryStages<?, ?> e;
|
||||
@StageRef IterationKeyHashCode khc;
|
||||
|
||||
public int recoverTier(int segmentIndex) {
|
||||
s.freeList.clearAll();
|
||||
|
||||
Logger log = lh.LOG;
|
||||
VanillaChronicleHash<?, ?, ?, ?> h = mh.h();
|
||||
CompactOffHeapLinearHashTable hl = h.hashLookup;
|
||||
long hlAddr = s.tierBaseAddr;
|
||||
|
||||
long hlPos = 0;
|
||||
do {
|
||||
long hlEntry = hl.readEntry(hlAddr, hlPos);
|
||||
nextHlPos:
|
||||
if (!hl.empty(hlEntry)) {
|
||||
hl.clearEntry(hlAddr, hlPos);
|
||||
|
||||
long searchKey = hl.key(hlEntry);
|
||||
long entryPos = hl.value(hlEntry);
|
||||
int si = checkEntry(searchKey, entryPos, segmentIndex);
|
||||
if (si < 0) {
|
||||
break nextHlPos;
|
||||
} else {
|
||||
segmentIndex = si;
|
||||
}
|
||||
|
||||
// The entry has passed all checks, re-insert:
|
||||
long startInsertPos = hl.hlPos(searchKey);
|
||||
long insertPos = startInsertPos;
|
||||
do {
|
||||
long hlInsertEntry = hl.readEntry(hlAddr, insertPos);
|
||||
if (hl.empty(hlInsertEntry)) {
|
||||
hl.writeEntry(hlAddr, insertPos, hl.entry(searchKey, entryPos));
|
||||
break nextHlPos;
|
||||
}
|
||||
if (insertPos == hlPos) {
|
||||
throw new ChronicleHashRecoveryFailedException(
|
||||
"Concurrent modification of ChronicleMap at " + h.file() +
|
||||
" while recovery procedure is in progress");
|
||||
}
|
||||
if (hl.key(hlInsertEntry) == searchKey) {
|
||||
long anotherEntryPos = hl.value(hlInsertEntry);
|
||||
if (anotherEntryPos == entryPos)
|
||||
break nextHlPos;
|
||||
long currentKeyOffset = e.keyOffset;
|
||||
long currentKeySize = e.keySize;
|
||||
int currentEntrySizeInChunks = e.entrySizeInChunks;
|
||||
if (checkEntry(searchKey, anotherEntryPos, segmentIndex) > 0) {
|
||||
if (e.keySize == currentKeySize &&
|
||||
BytesUtil.bytesEqual(s.segmentBS, currentKeyOffset,
|
||||
s.segmentBS, e.keyOffset, currentKeySize)) {
|
||||
log.error("Entries with duplicate keys within a tier: " +
|
||||
"at pos {} and {} with key {}, first value is {}",
|
||||
entryPos, anotherEntryPos, e.key(), e.value());
|
||||
s.freeList.clearRange(
|
||||
entryPos, entryPos + currentEntrySizeInChunks);
|
||||
break nextHlPos;
|
||||
}
|
||||
}
|
||||
}
|
||||
insertPos = hl.step(insertPos);
|
||||
} while (insertPos != startInsertPos);
|
||||
throw new ChronicleHashRecoveryFailedException(
|
||||
"HashLookup overflow should never occur. " +
|
||||
"It might also be concurrent access to ChronicleMap at " +
|
||||
h.file() + " while recovery procedure is in progress");
|
||||
}
|
||||
hlPos = hl.step(hlPos);
|
||||
} while (hlPos != 0);
|
||||
return segmentIndex;
|
||||
}
|
||||
|
||||
public void removeDuplicatesInSegment() {
|
||||
long startHlPos = 0L;
|
||||
VanillaChronicleMap<?, ?, ?> m = mh.m();
|
||||
CompactOffHeapLinearHashTable hashLookup = m.hashLookup;
|
||||
long currentTierBaseAddr = s.tierBaseAddr;
|
||||
while (!hashLookup.empty(hashLookup.readEntry(currentTierBaseAddr, startHlPos))) {
|
||||
startHlPos = hashLookup.step(startHlPos);
|
||||
}
|
||||
long hlPos = startHlPos;
|
||||
int steps = 0;
|
||||
long entries = 0;
|
||||
tierIteration:
|
||||
do {
|
||||
hlPos = hashLookup.step(hlPos);
|
||||
steps++;
|
||||
long entry = hashLookup.readEntry(currentTierBaseAddr, hlPos);
|
||||
if (!hashLookup.empty(entry)) {
|
||||
e.readExistingEntry(hashLookup.value(entry));
|
||||
Data key = (Data) e.key();
|
||||
try (ExternalMapQueryContext<?, ?, ?> c = m.queryContext(key)) {
|
||||
MapEntry<?, ?> entry2 = c.entry();
|
||||
Data<?> key2 = entry2.key();
|
||||
if (key2.bytes().address(key2.offset()) != key.bytes().address(key.offset())) {
|
||||
lh.LOG.error("entries with duplicate key {} in segment {}: " +
|
||||
"with values {} and {}, removing the latter",
|
||||
key, c.segmentIndex(), entry2.value(), e.value());
|
||||
if (hashLookup.remove(currentTierBaseAddr, hlPos) != hlPos) {
|
||||
hlPos = hashLookup.stepBack(hlPos);
|
||||
steps--;
|
||||
}
|
||||
continue tierIteration;
|
||||
}
|
||||
}
|
||||
entries++;
|
||||
}
|
||||
// the `steps == 0` condition and this variable updates in the loop fix the bug, when
|
||||
// shift deletion occurs on the first entry of the tier, and the hlPos
|
||||
// becomes equal to start pos without making the whole loop, but only visiting a single
|
||||
// entry
|
||||
} while (hlPos != startHlPos || steps == 0);
|
||||
|
||||
recoverTierEntriesCounter(entries);
|
||||
recoverLowestPossibleFreeChunkTiered();
|
||||
}
|
||||
|
||||
private void recoverTierEntriesCounter(long entries) {
|
||||
if (s.tierEntries() != entries) {
|
||||
lh.LOG.error("Wrong number of entries counter for tier with index {}, " +
|
||||
"stored: {}, should be: {}", s.tierIndex, s.tierEntries(), entries);
|
||||
s.tierEntries(entries);
|
||||
}
|
||||
}
|
||||
|
||||
private void recoverLowestPossibleFreeChunkTiered() {
|
||||
long lowestFreeChunk = s.freeList.nextClearBit(0);
|
||||
if (lowestFreeChunk == -1)
|
||||
lowestFreeChunk = mh.m().actualChunksPerSegmentTier;
|
||||
if (s.lowestPossiblyFreeChunk() != lowestFreeChunk) {
|
||||
lh.LOG.error("wrong lowest free chunk for tier with index {}, " +
|
||||
"stored: {}, should be: {}",
|
||||
s.tierIndex, s.lowestPossiblyFreeChunk(), lowestFreeChunk);
|
||||
s.lowestPossiblyFreeChunk(lowestFreeChunk);
|
||||
}
|
||||
}
|
||||
|
||||
private int checkEntry(long searchKey, long entryPos, int segmentIndex) {
|
||||
Logger log = lh.LOG;
|
||||
VanillaChronicleHash<?, ?, ?, ?> h = mh.h();
|
||||
if (entryPos < 0 || entryPos >= h.actualChunksPerSegmentTier) {
|
||||
log.error("Entry pos is out of range: {}, should be 0-{}",
|
||||
entryPos, h.actualChunksPerSegmentTier - 1);
|
||||
}
|
||||
try {
|
||||
e.readExistingEntry(entryPos);
|
||||
} catch (Exception e) {
|
||||
log.error("Exception while reading entry key size: {}", e);
|
||||
return -1;
|
||||
}
|
||||
if (e.keyEnd() > s.segmentBytes.capacity()) {
|
||||
log.error("Wrong key size: {}", e.keySize);
|
||||
return -1;
|
||||
}
|
||||
|
||||
long keyHashCode = khc.keyHashCode();
|
||||
int segmentIndexFromKey = h.hashSplitting.segmentIndex(keyHashCode);
|
||||
if (segmentIndexFromKey < 0 || segmentIndexFromKey >= h.actualSegments) {
|
||||
log.error("Segment index from the entry key hash code is out of range: {}, " +
|
||||
"should be 0-{}, entry key: {}",
|
||||
segmentIndexFromKey, h.actualSegments - 1, e.key());
|
||||
return -1;
|
||||
}
|
||||
|
||||
long segmentHashFromKey = h.hashSplitting.segmentHash(keyHashCode);
|
||||
long searchKeyFromKey = h.hashLookup.maskUnsetKey(segmentHashFromKey);
|
||||
if (searchKey != searchKeyFromKey) {
|
||||
log.error("HashLookup searchKey: {}, HashLookup searchKey " +
|
||||
"from the entry key hash code: {}, entry key: {}",
|
||||
searchKey, searchKeyFromKey, e.key());
|
||||
return -1;
|
||||
}
|
||||
|
||||
try {
|
||||
long entryAndChecksumEnd = e.entryEnd() + e.checksumStrategy.extraEntryBytes();
|
||||
if (entryAndChecksumEnd > s.segmentBytes.capacity()) {
|
||||
log.error("Wrong value size: {}, key: {}", e.valueSize, e.key());
|
||||
return -1;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.error("Exception while reading entry value size: {}, key: {}", ex, e.key());
|
||||
return -1;
|
||||
}
|
||||
|
||||
int storedChecksum = e.checksumStrategy.storedChecksum();
|
||||
int checksumFromEntry = e.checksumStrategy.computeChecksum();
|
||||
if (storedChecksum != checksumFromEntry) {
|
||||
log.error("Checksum doesn't match, stored: {}, should be from " +
|
||||
"the entry bytes: {}, key: {}, value: {}",
|
||||
storedChecksum, checksumFromEntry, e.key(), e.value());
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!s.freeList.isRangeClear(entryPos, entryPos + e.entrySizeInChunks)) {
|
||||
log.error("Overlapping entry: positions {}-{}, key: {}, value: {}",
|
||||
entryPos, entryPos + e.entrySizeInChunks - 1, e.key(), e.value());
|
||||
}
|
||||
s.freeList.setRange(entryPos, entryPos + e.entrySizeInChunks);
|
||||
|
||||
if (segmentIndex < 0) {
|
||||
return segmentIndexFromKey;
|
||||
} else {
|
||||
if (segmentIndex != segmentIndexFromKey) {
|
||||
log.error("Expected segment index: {}, segment index from the entry key: {}, " +
|
||||
"key: {}, value: {}", segmentIndex, searchKeyFromKey, e.key(),
|
||||
e.value());
|
||||
return -1;
|
||||
} else {
|
||||
return segmentIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user