HCOLL-360 Add segment tiers

This commit is contained in:
leventov
2015-09-17 20:58:52 +03:00
parent 4606b64a02
commit 2eed65e80e
39 changed files with 1182 additions and 285 deletions
@@ -66,6 +66,10 @@ public final class BigSegmentHeader implements SegmentHeader {
@Override
public void size(long address, long size) {
if (size >= (1L << 32)) {
throw new IllegalStateException("segment size overflow: up to " + UNSIGNED_INT_MASK +
" supported, " + size + " given");
}
UNSAFE.putInt(address + SIZE_OFFSET, (int) size);
}
@@ -76,6 +80,10 @@ public final class BigSegmentHeader implements SegmentHeader {
@Override
public void deleted(long address, long deleted) {
if (deleted >= (1L << 32)) {
throw new IllegalStateException("segment deleted entries count overflow: up to " +
UNSIGNED_INT_MASK + " supported, " + deleted + " given");
}
UNSAFE.putInt(address + DELETED_OFFSET, (int) deleted);
}
@@ -0,0 +1,70 @@
/*
* 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;
import static net.openhft.lang.io.NativeBytes.UNSAFE;
public enum TierData {
;
public static final long NEXT_TIER_INDEX_OFFSET = 0L;
public static final long PREV_TIER_INDEX_OFFSET = NEXT_TIER_INDEX_OFFSET + 8L;
public static final long NEXT_POS_TO_SEARCH_FROM_TIERED_OFFSET = PREV_TIER_INDEX_OFFSET + 8L;
public static final long SEGMENT_INDEX_OFFSET = NEXT_POS_TO_SEARCH_FROM_TIERED_OFFSET + 8L;
public static final long TIER_OFFSET = SEGMENT_INDEX_OFFSET + 4L;
public static long nextTierIndex(long address) {
return UNSAFE.getLong(address + NEXT_TIER_INDEX_OFFSET);
}
public static void nextTierIndex(long address, long nextTierIndex) {
UNSAFE.putLong(address + NEXT_TIER_INDEX_OFFSET, nextTierIndex);
}
public static long nextPosToSearchFromTiered(long address) {
return UNSAFE.getLong(address + NEXT_POS_TO_SEARCH_FROM_TIERED_OFFSET);
}
public static void nextPosToSearchFromTiered(long address, long nextPosToSearchFrom) {
UNSAFE.putLong(address + NEXT_POS_TO_SEARCH_FROM_TIERED_OFFSET,
nextPosToSearchFrom);
}
public static long prevTierIndex(long address) {
return UNSAFE.getLong(address + PREV_TIER_INDEX_OFFSET);
}
public static void prevTierIndex(long address, long prevTierIndex) {
UNSAFE.putLong(address + PREV_TIER_INDEX_OFFSET, prevTierIndex);
}
public static int segmentIndex(long address) {
return UNSAFE.getInt(address + SEGMENT_INDEX_OFFSET);
}
public static void segmentIndex(long address, int segmentIndex) {
UNSAFE.putInt(address + SEGMENT_INDEX_OFFSET, segmentIndex);
}
public static int tier(long address) {
return UNSAFE.getInt(address + TIER_OFFSET);
}
public static void tier(long address, int tier) {
UNSAFE.putInt(address + TIER_OFFSET, tier);
}
}
@@ -16,6 +16,8 @@
package net.openhft.chronicle.hash.impl;
import net.openhft.chronicle.bytes.NativeBytesStore;
import net.openhft.chronicle.core.Maths;
import net.openhft.chronicle.hash.*;
import net.openhft.chronicle.hash.impl.util.BuildVersion;
import net.openhft.chronicle.hash.serialization.BytesReader;
@@ -24,10 +26,7 @@ import net.openhft.chronicle.hash.serialization.internal.MetaBytesInterop;
import net.openhft.chronicle.hash.serialization.internal.MetaProvider;
import net.openhft.chronicle.hash.serialization.internal.SerializationBuilder;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.BytesStore;
import net.openhft.lang.io.MappedStore;
import net.openhft.lang.io.NativeBytes;
import net.openhft.lang.io.*;
import net.openhft.lang.io.serialization.BytesMarshallableSerializer;
import net.openhft.lang.threadlocal.Provider;
@@ -36,14 +35,14 @@ import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
import static java.lang.Long.numberOfTrailingZeros;
import static java.lang.Math.max;
import static net.openhft.chronicle.hash.impl.CompactOffHeapLinearHashTable.entrySize;
import static net.openhft.chronicle.hash.impl.CompactOffHeapLinearHashTable.keyBits;
import static net.openhft.chronicle.hash.impl.CompactOffHeapLinearHashTable.valueBits;
import static net.openhft.chronicle.hash.impl.CompactOffHeapLinearHashTable.*;
import static net.openhft.lang.MemoryUnit.*;
import static net.openhft.lang.io.NativeBytes.UNSAFE;
public abstract class VanillaChronicleHash<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
C extends HashEntry<K>, SC extends HashSegmentContext<K, ?>,
@@ -101,13 +100,44 @@ public abstract class VanillaChronicleHash<K, KI, MKI extends MetaBytesInterop<K
public final int segmentEntrySpaceInnerOffset;
final long segmentEntrySpaceOuterSize;
final long segmentSize;
public final long segmentSize;
final long maxExtraTiers;
final long tierBulkBytesSize;
final long tierBulkInnerOffsetToTiers;
protected final long numberOfTiersInBulk;
protected final int log2NumberOfTiersInBulk;
/////////////////////////////////////////////////
// Bytes Store (essentially, the base address) and serialization-dependent offsets
public transient BytesStore ms;
protected transient BytesStore ms;
transient Bytes bytes;
public static class TierBulkData {
public BytesStore langBytesStore;
public Bytes langBytes;
public net.openhft.chronicle.bytes.BytesStore chBytesBytesStore;
public long offset;
public TierBulkData(BytesStore langBytesStore, long offset) {
this.langBytesStore = langBytesStore;
langBytes = langBytesStore.bytes();
chBytesBytesStore = new NativeBytesStore<>(
langBytes.address(), langBytes.capacity(), null, false);
this.offset = offset;
}
public TierBulkData(TierBulkData data, long offset) {
this.langBytesStore = data.langBytesStore;
this.langBytes = data.langBytes;
this.chBytesBytesStore = data.chBytesBytesStore;
this.offset = offset;
}
}
public transient List<TierBulkData> tierBulkOffsets;
public transient long headerSize;
transient long segmentHeadersOffset;
transient long segmentsOffset;
@@ -167,12 +197,17 @@ public abstract class VanillaChronicleHash<K, KI, MKI extends MetaBytesInterop<K
segmentEntrySpaceInnerOffset + segmentEntrySpaceInnerSize, BYTES);
segmentSize = segmentSize();
maxExtraTiers = privateAPI.maxExtraTiers(replicated);
numberOfTiersInBulk = computeNumberOfTiersInBulk();
log2NumberOfTiersInBulk = Maths.intLog2(numberOfTiersInBulk);
tierBulkInnerOffsetToTiers = computeTierBulkInnerOffsetToTiers(numberOfTiersInBulk);
tierBulkBytesSize = computeTierBulkBytesSize();
}
private long segmentSize() {
long ss = segmentHashLookupOuterSize
+ segmentFreeListOuterSize
+ segmentEntrySpaceOuterSize;
long ss = segmentHashLookupOuterSize + 64 /* Tier cache line */ +
segmentFreeListOuterSize + segmentEntrySpaceOuterSize;
if ((ss & 63L) != 0)
throw new AssertionError();
return breakL1CacheAssociativityContention(ss);
@@ -190,6 +225,25 @@ public abstract class VanillaChronicleHash<K, KI, MKI extends MetaBytesInterop<K
return segmentSize;
}
private long computeNumberOfTiersInBulk() {
// TODO review heuristics
int tiersInBulk = actualSegments / 8;
tiersInBulk = Maths.nextPower2(tiersInBulk, 1);
while (segmentSize * tiersInBulk < UNSAFE.pageSize()) {
tiersInBulk *= 2;
}
return tiersInBulk;
}
private long computeTierBulkBytesSize() {
return roundUpRuntimePageSize(tierBulkInnerOffsetToTiers +
numberOfTiersInBulk * segmentSize);
}
protected long computeTierBulkInnerOffsetToTiers(long numberOfTiersInBulk) {
return 0L;
}
public void initTransients() {
ownInitTransients();
}
@@ -209,16 +263,39 @@ public abstract class VanillaChronicleHash<K, KI, MKI extends MetaBytesInterop<K
segmentHeadersOffset = mapHeaderOuterSize();
long segmentHeadersSize = actualSegments * segmentHeaderSize;
segmentsOffset = segmentHeadersOffset + segmentHeadersSize;
initTierBulks(bytesStore.size());
}
public final void createMappedStoreAndSegments(File file) throws IOException {
// TODO this method had been moved -- not clear where
//OS.warnOnWindows(sizeInBytes());
//OS.warnOnWindows(sizeInBytesWithoutTiers());
createMappedStoreAndSegments(new MappedStore(file, FileChannel.MapMode.READ_WRITE,
sizeInBytes(), BytesMarshallableSerializer.create()));
// file.length() > sizeInBytesWithoutTiers() means there are some tiered segments
Math.max(sizeInBytesWithoutTiers(), file.length()),
BytesMarshallableSerializer.create()));
// newly-extended file contents are not guaranteed to be zero
if (created)
if (created) {
// TODO zero out only hash lookup tables and bit sets, not entry data areas
bytes.zeroOut(headerSize, bytes.capacity(), true);
// zero out tier data
bytes.zeroOut(headerSize, headerSize + 64, true);
}
}
private void initTierBulks(long byteStoreSize) {
tierBulkOffsets = new ArrayList<>();
long tierBulkOffset = roundUpRuntimePageSize(sizeInBytesWithoutTiers());
if (byteStoreSize >= tierBulkOffset + tierBulkBytesSize) {
TierBulkData firstTierBulkData = new TierBulkData(ms, tierBulkOffset);
tierBulkOffsets.add(firstTierBulkData);
while (true) {
tierBulkOffset += tierBulkBytesSize;
if (byteStoreSize < tierBulkOffset + tierBulkBytesSize)
break;
tierBulkOffsets.add(new TierBulkData(firstTierBulkData, tierBulkOffset));
}
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
@@ -246,12 +323,16 @@ public abstract class VanillaChronicleHash<K, KI, MKI extends MetaBytesInterop<K
private long mapHeaderOuterSize() {
// Align segment headers on page boundary to minimize number of pages that
// segment headers span
long pageMask = NativeBytes.UNSAFE.pageSize() - 1L;
return (mapHeaderInnerSize() + pageMask) & ~pageMask;
return roundUpRuntimePageSize(mapHeaderInnerSize());
}
private static long roundUpRuntimePageSize(long size) {
long pageMask = UNSAFE.pageSize() - 1;
return (size + pageMask) & ~pageMask;
}
public long mapHeaderInnerSize() {
return headerSize;
return headerSize + 64; /* tier data cache line */
}
@Override
@@ -259,7 +340,7 @@ public abstract class VanillaChronicleHash<K, KI, MKI extends MetaBytesInterop<K
return ms.file();
}
public final long sizeInBytes() {
public final long sizeInBytesWithoutTiers() {
return mapHeaderOuterSize() + actualSegments * (segmentHeaderSize + segmentSize);
}
@@ -295,17 +376,21 @@ public abstract class VanillaChronicleHash<K, KI, MKI extends MetaBytesInterop<K
public final long[] segmentSizes() {
long[] sizes = new long[actualSegments];
for (int i = 0; i < actualSegments; i++) {
sizes[i] = BigSegmentHeader.INSTANCE.size(ms.address() + segmentHeaderOffset(i));
sizes[i] = BigSegmentHeader.INSTANCE.size(segmentHeaderAddress(i));
}
return sizes;
}
public final long segmentHeaderOffset(int segmentIndex) {
return segmentHeadersOffset + ((long) segmentIndex) * segmentHeaderSize;
public final long segmentHeaderAddress(int segmentIndex) {
return ms.address() + segmentHeadersOffset + ((long) segmentIndex) * segmentHeaderSize;
}
public final long segmentOffset(int segmentIndex) {
return segmentsOffset + ((long) segmentIndex) * segmentSize;
public final long segmentBaseAddr(int segmentIndex) {
return ms.address() + msBytesOffset(segmentIndex);
}
private long msBytesOffset(long segmentIndex) {
return segmentsOffset + segmentIndex * segmentSize;
}
public final int inChunks(long sizeInBytes) {
@@ -323,7 +408,7 @@ public abstract class VanillaChronicleHash<K, KI, MKI extends MetaBytesInterop<K
public final long longSize() {
long result = 0L;
for (int i = 0; i < actualSegments; i++) {
long segmentHeaderAddress = ms.address() + segmentHeaderOffset(i);
long segmentHeaderAddress = segmentHeaderAddress(i);
result += BigSegmentHeader.INSTANCE.size(segmentHeaderAddress) -
BigSegmentHeader.INSTANCE.deleted(segmentHeaderAddress);
}
@@ -339,4 +424,188 @@ public abstract class VanillaChronicleHash<K, KI, MKI extends MetaBytesInterop<K
public int segments() {
return actualSegments;
}
private static final long TIER_DATA_LOCK_OFFSET = 0L;
private static final long EXTRA_ALLOCATED_TIER_BULK_COUNT_OFFSET = TIER_DATA_LOCK_OFFSET + 8L;
private static final long FIRST_FREE_TIER_INDEX_OFFSET =
EXTRA_ALLOCATED_TIER_BULK_COUNT_OFFSET + 8L;
private static final long TIERS_IN_USE_OFFSET = FIRST_FREE_TIER_INDEX_OFFSET + 8L;
private long tierDataAddress() {
return bytes.address() + headerSize;
}
private void tierDataLock() {
// TODO this is quick access to locking functionality, eligible only because lock word
// in the segment header has offset = 0
BigSegmentHeader.INSTANCE.writeLock(tierDataAddress() + TIER_DATA_LOCK_OFFSET);
}
private void tierDataUnlock() {
BigSegmentHeader.INSTANCE.writeUnlock(tierDataAddress() + TIER_DATA_LOCK_OFFSET);
}
protected long extraAllocatedTierBulkCount() {
return bytes.readLong(headerSize + EXTRA_ALLOCATED_TIER_BULK_COUNT_OFFSET);
}
private void extraAllocatedTierBulkCount(long extraAllocatedTierBulkCount) {
bytes.writeLong(headerSize + EXTRA_ALLOCATED_TIER_BULK_COUNT_OFFSET,
extraAllocatedTierBulkCount);
}
private long firstFreeTierIndex() {
return bytes.readLong(headerSize + FIRST_FREE_TIER_INDEX_OFFSET);
}
private void firstFreeTierIndex(long firstFreeTierIndex) {
bytes.writeLong(headerSize + FIRST_FREE_TIER_INDEX_OFFSET, firstFreeTierIndex);
}
private long tiersInUse() {
return bytes.readLong(headerSize + TIERS_IN_USE_OFFSET);
}
private void tiersInUse(long tiersInUse) {
bytes.writeLong(headerSize + TIERS_IN_USE_OFFSET, tiersInUse);
}
public long allocateTier(int forSegmentIndex, int tier) {
tierDataLock();
try {
long tiersInUse = tiersInUse();
if (tiersInUse == maxExtraTiers) {
throw new IllegalStateException("Attempt to allocate " + (maxExtraTiers + 1) +
"th extra segment tier, " + maxExtraTiers + " is maximum.\n" +
"Possible reasons include:\n" +
" - you have forgotten to configure (or configured wrong) " +
"builder.entries() number\n" +
" - same regarding other sizing Chronicle Hash configurations, most " +
"likely maxBloatFactor(), averageKeySize(), or averageValueSize()\n" +
" - keys, inserted into the ChronicleHash, are distributed suspiciously " +
"bad. This might be a DOS attack");
}
while (true) {
long firstFreeTierIndex = firstFreeTierIndex();
if (firstFreeTierIndex > 0) {
tiersInUse(tiersInUse + 1);
long tierBaseAddr = tierIndexToBaseAddr(firstFreeTierIndex);
long tierDataAddr = tierBaseAddr + segmentHashLookupOuterSize;
long nextFreeTierIndex = TierData.nextTierIndex(tierDataAddr);
// now, when this tier will be a part of the map, the next tier designates
// the next tier in the data structure, should be 0
TierData.nextTierIndex(tierDataAddr, 0);
TierData.segmentIndex(tierDataAddr, forSegmentIndex);
TierData.tier(tierDataAddr, tier);
firstFreeTierIndex(nextFreeTierIndex);
return firstFreeTierIndex;
} else {
allocateTierBulk();
}
}
} finally {
tierDataUnlock();
}
}
private void allocateTierBulk() {
long alreadyAllocatedBulks = extraAllocatedTierBulkCount();
mapTiers(alreadyAllocatedBulks);
extraAllocatedTierBulkCount(alreadyAllocatedBulks + 1);
linkFreeTiersList(alreadyAllocatedBulks);
}
private void linkFreeTiersList(long alreadyAllocatedBulks) {
long alreadyAllocatedTiers = alreadyAllocatedBulks * numberOfTiersInBulk;
for (long tierIndex = alreadyAllocatedTiers;
tierIndex < alreadyAllocatedTiers + numberOfTiersInBulk - 1; tierIndex++) {
long tierBaseAddr = tierIndexToBaseAddr(tierIndex);
long tierDataAddr = tierBaseAddr + segmentHashLookupOuterSize;
TierData.nextTierIndex(tierDataAddr, tierIndex + 1);
}
}
public long tierIndexToBaseAddr(long tierIndex) {
// tiers are 1-counted, to allow tierIndex = 0 to be un-initialized in off-heap memory,
// convert into 0-based form
tierIndex -= 1;
if (tierIndex < actualSegments)
return segmentBaseAddr((int) tierIndex);
return extraTierIndexToBaseAddress(tierIndex);
}
public Bytes tierBytes(long tierIndex) {
tierIndex -= 1;
if (tierIndex < actualSegments)
return bytes;
return tierBulkData(tierIndex).langBytes;
}
public long bytesOffset(long tierIndex) {
tierIndex -= 1;
if (tierIndex < actualSegments)
return msBytesOffset(tierIndex);
return tierBulkData(tierIndex).offset;
}
private TierBulkData tierBulkData(long tierIndex) {
long extraTierIndex = tierIndex - actualSegments;
long bulkIndex = extraTierIndex >> log2NumberOfTiersInBulk;
if (bulkIndex >= tierBulkOffsets.size())
mapTiers(bulkIndex);
return tierBulkOffsets.get((int) bulkIndex);
}
private long extraTierIndexToBaseAddress(long tierIndex) {
long extraTierIndex = tierIndex - actualSegments;
long bulkIndex = extraTierIndex >> log2NumberOfTiersInBulk;
if (bulkIndex >= tierBulkOffsets.size())
mapTiers(bulkIndex);
TierBulkData tierBulkData = tierBulkOffsets.get((int) bulkIndex);
return tierBulkData.langBytes.address() + tierBulkData.offset +
tierBulkInnerOffsetToTiers +
(extraTierIndex & (numberOfTiersInBulk - 1)) * segmentSize;
}
private void mapTiers(long upToBulkIndex) {
if (ms instanceof MappedStore) {
try {
mapTiersMapped(upToBulkIndex);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
// in-memory ChMap
allocateTiers(upToBulkIndex);
}
}
private void mapTiersMapped(long upToBulkIndex) throws IOException {
int firstBulkToMap = tierBulkOffsets.size();
long bulksToMap = upToBulkIndex + 1 - firstBulkToMap;
long mapSize = bulksToMap * tierBulkBytesSize;
long mapStart = roundUpRuntimePageSize(sizeInBytesWithoutTiers()) +
firstBulkToMap * tierBulkBytesSize;
MappedStore extraStore = new MappedStore(file(), FileChannel.MapMode.READ_WRITE,
mapStart, mapSize, ms.objectSerializer());
appendTierBulkData(upToBulkIndex, firstBulkToMap, extraStore);
}
private void allocateTiers(long upToBulkIndex) {
int firstBulkToMap = tierBulkOffsets.size();
long bulksToMap = upToBulkIndex + 1 - firstBulkToMap;
long mapSize = bulksToMap * tierBulkBytesSize;
DirectStore extraStore = new DirectStore(ms.objectSerializer(), mapSize, true);
appendTierBulkData(upToBulkIndex, firstBulkToMap, extraStore);
}
private void appendTierBulkData(long upToBulkIndex, int firstBulkToMap, BytesStore extraStore) {
long offset = 0;
TierBulkData firstMappedTierBulkData = new TierBulkData(extraStore, offset);
tierBulkOffsets.add(firstMappedTierBulkData);
for (long i = firstBulkToMap + 1; i <= upToBulkIndex; i++) {
tierBulkOffsets.add(new TierBulkData(firstMappedTierBulkData,
offset += tierBulkBytesSize));
}
}
}
@@ -19,6 +19,7 @@ package net.openhft.chronicle.hash.impl.stage.data.bytes;
import net.openhft.chronicle.bytes.RandomDataInput;
import net.openhft.chronicle.hash.AbstractData;
import net.openhft.chronicle.hash.impl.stage.entry.HashEntryStages;
import net.openhft.chronicle.hash.impl.stage.entry.SegmentStages;
import net.openhft.chronicle.hash.impl.stage.hash.CheckOnEachPublicOperation;
import net.openhft.chronicle.hash.impl.stage.hash.KeyBytesInterop;
import net.openhft.sg.Stage;
@@ -29,6 +30,7 @@ import net.openhft.sg.Staged;
public class EntryKeyBytesData<K> extends AbstractData<K> {
@StageRef KeyBytesInterop<K, ?, ?> ki;
@StageRef SegmentStages s;
@StageRef HashEntryStages<K> entry;
@StageRef CheckOnEachPublicOperation checkOnEachPublicOperation;
@@ -44,7 +46,7 @@ public class EntryKeyBytesData<K> extends AbstractData<K> {
@Override
public RandomDataInput bytes() {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return entry.entryBS;
return s.segmentBS;
}
@Override
@@ -72,7 +74,7 @@ public class EntryKeyBytesData<K> extends AbstractData<K> {
}
private K innerGetUsing(K usingKey) {
entry.entryBytes.position(entry.keyOffset);
return ki.keyReader.read(entry.entryBytes, size(), usingKey);
s.segmentBytes.position(entry.keyOffset);
return ki.keyReader.read(s.segmentBytes, size(), usingKey);
}
}
@@ -37,12 +37,21 @@ public class AllocatedChunks {
// don't increment
}
public void initEntryAndKeyCopying(long entrySize, long bytesToCopy) {
/**
* @return {@code true} is tier has changed
*/
public boolean initEntryAndKeyCopying(long entrySize, long bytesToCopy) {
initAllocatedChunks(hh.h().inChunks(entrySize));
// call incrementSegmentEntriesIfNeeded() before entry.copyExistingEntry(), because
// the latter clears out searchState, and it performs the search again, but in inconsistent
// state
incrementSegmentEntriesIfNeeded();
entry.copyExistingEntry(s.alloc(allocatedChunks), bytesToCopy);
long oldSegmentTierBaseAddr = s.segmentBaseAddr;
long oldKeySizeAddr = oldSegmentTierBaseAddr + entry.keySizeOffset;
long oldKeyAddr = oldSegmentTierBaseAddr + entry.keyOffset;
int tierBeforeAllocation = s.segmentTier;
long pos = s.alloc(allocatedChunks);
entry.copyExistingEntry(pos, bytesToCopy, oldKeyAddr, oldKeySizeAddr);
return s.segmentTier > tierBeforeAllocation;
}
}
@@ -16,6 +16,7 @@
package net.openhft.chronicle.hash.impl.stage.entry;
import net.openhft.chronicle.algo.bytes.Access;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.bytes.NativeBytesStore;
import net.openhft.chronicle.hash.Data;
@@ -29,6 +30,9 @@ import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
import org.jetbrains.annotations.NotNull;
import static net.openhft.chronicle.algo.bytes.Access.checkedBytesStoreAccess;
import static net.openhft.chronicle.algo.bytes.Access.nativeAccess;
@Staged
public abstract class HashEntryStages<K> implements HashEntry<K> {
@@ -37,10 +41,6 @@ public abstract class HashEntryStages<K> implements HashEntry<K> {
@StageRef public SegmentStages s;
@StageRef public CheckOnEachPublicOperation checkOnEachPublicOperation;
@StageRef public HashLookupPos hlp;
public final Bytes entryBytes = hh.h().ms.bytes();
public final BytesStore entryBS =
new NativeBytesStore<>(entryBytes.address(), entryBytes.capacity(), null, false);
public long pos = -1;
@@ -54,7 +54,7 @@ public abstract class HashEntryStages<K> implements HashEntry<K> {
public void initEntryOffset() {
keySizeOffset = s.entrySpaceOffset + pos * hh.h().chunkSize;
entryBytes.limit(entryBytes.capacity());
s.segmentBytes.limit(s.segmentBytes.capacity());
}
public long keySize = -1;
@@ -71,26 +71,32 @@ public abstract class HashEntryStages<K> implements HashEntry<K> {
public void readExistingEntry(long pos) {
initPos(pos);
entryBytes.position(keySizeOffset);
initKeySize(hh.h().keySizeMarshaller.readSize(entryBytes));
initKeyOffset(entryBytes.position());
s.segmentBytes.position(keySizeOffset);
initKeySize(hh.h().keySizeMarshaller.readSize(s.segmentBytes));
initKeyOffset(s.segmentBytes.position());
}
public void writeNewEntry(long pos, Data<?> key) {
initPos(pos);
initKeySize(key.size());
entryBytes.position(keySizeOffset);
hh.h().keySizeMarshaller.writeSize(entryBytes, keySize);
initKeyOffset(entryBytes.position());
key.writeTo(entryBS, keyOffset);
s.segmentBytes.position(keySizeOffset);
hh.h().keySizeMarshaller.writeSize(s.segmentBytes, keySize);
initKeyOffset(s.segmentBytes.position());
key.writeTo(s.segmentBS, keyOffset);
}
public void copyExistingEntry(long newPos, long bytesToCopy) {
long oldKeySizeOffset = keySizeOffset;
long oldKeyOffset = keyOffset;
public void copyExistingEntry(
long newPos, long bytesToCopy, long oldKeyAddr, long oldKeySizeAddr) {
initPos(newPos);
initKeyOffset(keySizeOffset + (oldKeyOffset - oldKeySizeOffset));
entryBS.write(keySizeOffset, entryBS, oldKeySizeOffset, bytesToCopy);
initKeyOffset(keySizeOffset + (oldKeyAddr - oldKeySizeAddr));
// Calling Access.copy() which is probably slower because not of abstractions,
// because there is no BytesStore.write(off, addr, len) method. Alternative is
// to make a final BytesStore rawMemoryStore = new PointerBytesStore().set(0, Long.MAX_V)
// and here: s.segmentBS.write(keySizeOffset, rawMemoryStore, keySizeAddr, bytesToCopy)
Access.copy(
nativeAccess(), null, oldKeySizeAddr,
checkedBytesStoreAccess(), s.segmentBS, keySizeOffset,
bytesToCopy);
}
public long keyEnd() {
@@ -46,6 +46,6 @@ public abstract class HashLookupPos {
public void putValueVolatile(long newValue) {
CompactOffHeapLinearHashTable hashLookup = hh.h().hashLookup;
hashLookup.checkValueForPut(newValue);
hashLookup.putValueVolatile(s.segmentBase, hashLookupPos, newValue);
hashLookup.putValueVolatile(s.segmentBaseAddr, hashLookupPos, newValue);
}
}
@@ -14,13 +14,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.chronicle.hash.impl.stage.query;
package net.openhft.chronicle.hash.impl.stage.entry;
import net.openhft.chronicle.hash.impl.CompactOffHeapLinearHashTable;
import net.openhft.chronicle.hash.impl.VanillaChronicleHashHolder;
import net.openhft.chronicle.hash.impl.stage.entry.HashEntryStages;
import net.openhft.chronicle.hash.impl.stage.entry.HashLookupPos;
import net.openhft.chronicle.hash.impl.stage.entry.SegmentStages;
import net.openhft.chronicle.hash.impl.stage.query.HashQuery;
import net.openhft.chronicle.hash.impl.stage.query.KeySearch;
import net.openhft.sg.Stage;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@@ -31,26 +33,24 @@ import static net.openhft.chronicle.hash.impl.CompactOffHeapLinearHashTable.UNSE
public abstract class HashLookupSearch {
@StageRef SegmentStages s;
@StageRef HashEntryStages<?> e;
@StageRef HashQuery op;
@StageRef VanillaChronicleHashHolder<?, ?, ?> hh;
@StageRef public VanillaChronicleHashHolder<?, ?, ?> hh;
@StageRef HashLookupPos hlp;
@StageRef KeySearch<?> ks;
@Stage("SearchKey") long searchKey = UNSET_KEY;
@Stage("SearchKey") long searchStartPos;
@Stage("SearchKey") public long searchStartPos;
private CompactOffHeapLinearHashTable hl() {
public CompactOffHeapLinearHashTable hl() {
return hh.h().hashLookup;
}
void initSearchKey() {
searchKey = hl().maskUnsetKey(hh.h().hashSplitting.segmentHash(op.hashOfKey));
public void initSearchKey(long searchKey) {
this.searchKey = searchKey;
searchStartPos = hl().hlPos(searchKey);
}
private long addr() {
return s.segmentBase;
return s.segmentBaseAddr;
}
public long nextPos() {
@@ -83,6 +83,10 @@ public abstract class HashLookupSearch {
}
public void putNewVolatile(long value) {
// correctness check + make putNewVolatile() dependant on keySearch, this, in turn,
// is needed for hlp.hashLookupPos re-initialization after nextTier()
assert !ks.searchStatePresent();
hl().checkValueForPut(value);
long currentEntry = hl().readEntry(addr(), hlp.hashLookupPos);
hl().writeEntryVolatile(addr(), hlp.hashLookupPos, currentEntry, searchKey, value);
@@ -16,6 +16,10 @@
package net.openhft.chronicle.hash.impl.stage.entry;
import net.openhft.chronicle.algo.bitset.ReusableBitSet;
import net.openhft.chronicle.algo.bitset.SingleThreadedFlatBitSetFrame;
import net.openhft.chronicle.algo.bytes.Access;
import net.openhft.chronicle.bytes.PointerBytesStore;
import net.openhft.chronicle.hash.Data;
import net.openhft.chronicle.hash.SegmentLock;
import net.openhft.chronicle.hash.impl.*;
@@ -23,9 +27,9 @@ import net.openhft.chronicle.hash.impl.stage.hash.Chaining;
import net.openhft.chronicle.hash.impl.stage.hash.CheckOnEachPublicOperation;
import net.openhft.chronicle.hash.impl.stage.query.KeySearch;
import net.openhft.chronicle.hash.locks.InterProcessLock;
import net.openhft.chronicle.hash.serialization.internal.MetaBytesInterop;
import net.openhft.lang.collection.DirectBitSet;
import net.openhft.lang.collection.SingleThreadedDirectBitSet;
import net.openhft.lang.io.MultiStoreBytes;
import net.openhft.lang.io.Bytes;
import net.openhft.sg.Stage;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@@ -33,6 +37,8 @@ import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import static net.openhft.chronicle.algo.MemoryUnit.BITS;
import static net.openhft.chronicle.algo.MemoryUnit.LONGS;
import static net.openhft.chronicle.hash.impl.LocalLockState.UNLOCKED;
@Staged
@@ -54,7 +60,7 @@ public abstract class SegmentStages implements SegmentLock {
abstract boolean segmentHeaderInit();
private void initSegmentHeader() {
segmentHeaderAddress = hh.h().ms.address() + hh.h().segmentHeaderOffset(segmentIndex);
segmentHeaderAddress = hh.h().segmentHeaderAddress(segmentIndex);
segmentHeader = BigSegmentHeader.INSTANCE;
}
@@ -67,11 +73,19 @@ public abstract class SegmentStages implements SegmentLock {
}
long nextPosToSearchFrom() {
return segmentHeader.nextPosToSearchFrom(segmentHeaderAddress);
if (segmentTier == 0) {
return segmentHeader.nextPosToSearchFrom(segmentHeaderAddress);
} else {
return nextPosToSearchFromTiered();
}
}
public void nextPosToSearchFrom(long nextPosToSearchFrom) {
segmentHeader.nextPosToSearchFrom(segmentHeaderAddress, nextPosToSearchFrom);
if (segmentTier == 0) {
segmentHeader.nextPosToSearchFrom(segmentHeaderAddress, nextPosToSearchFrom);
} else {
nextPosToSearchFromTiered(nextPosToSearchFrom);
}
}
public long deleted() {
@@ -346,11 +360,91 @@ public abstract class SegmentStages implements SegmentLock {
checkOnEachPublicOperation.checkOnEachPublicOperation();
return innerWriteLock;
}
@Stage("SegmentTier") public int segmentTier = -1;
@Stage("SegmentTier") public long tierIndex;
@Stage("SegmentTier") public long segmentBaseAddr;
public void initSegmentTier() {
tierIndex = segmentIndex + 1; // tiers are 1-counted
segmentBaseAddr = hh.h().segmentBaseAddr(segmentIndex);
// assign the field of stage on which init() checks last, because flushed in segmentIndex()
// initialization
// TODO this is dangerous... review other stages and compilation mechanism
segmentTier = 0;
}
private void initSegmentTier(int tier, long tierIndex) {
segmentTier = tier;
this.tierIndex = tierIndex;
assert tierIndex > 0;
this.segmentBaseAddr = hh.h().tierIndexToBaseAddr(tierIndex);
}
public void initSegmentTier(int tier, long tierIndex, long tierBaseAddr) {
segmentTier = tier;
this.tierIndex = tierIndex;
this.segmentBaseAddr = tierBaseAddr;
}
public long tierDataOffset() {
return segmentBaseAddr + hh.h().segmentHashLookupOuterSize;
}
public long nextTierIndex() {
return TierData.nextTierIndex(tierDataOffset());
}
public void nextTierIndex(long nextTierIndex) {
TierData.nextTierIndex(tierDataOffset(), nextTierIndex);
}
public long nextPosToSearchFromTiered() {
return TierData.nextPosToSearchFromTiered(tierDataOffset());
}
public void nextPosToSearchFromTiered(long nextPosToSearchFrom) {
TierData.nextPosToSearchFromTiered(tierDataOffset(), nextPosToSearchFrom);
}
public long prevTierIndex() {
return TierData.prevTierIndex(tierDataOffset());
}
public void prevTierIndex(long prevTierIndex) {
TierData.prevTierIndex(tierDataOffset(), prevTierIndex);
}
public void nextTier() {
VanillaChronicleHash<?, ?, ? extends MetaBytesInterop<?, ?>, ?, ?, ?> h = hh.h();
long nextTierIndex = nextTierIndex();
if (nextTierIndex == 0) {
nextTierIndex = h.allocateTier(segmentIndex, segmentTier + 1);
nextTierIndex(nextTierIndex);
long currentTierBase = segmentBaseAddr;
initSegmentTier(segmentTier + 1, nextTierIndex);
prevTierIndex(currentTierBase);
} else {
initSegmentTier(segmentTier + 1, nextTierIndex);
}
}
public boolean hasNextTier() {
return nextTierIndex() != 0;
}
public void prevTier() {
if (segmentTier == 0)
throw new IllegalStateException("first tier doesn't have previous");
initSegmentTier(segmentTier - 1, prevTierIndex());
}
@Stage("Segment") MultiStoreBytes freeListBytes = new MultiStoreBytes();
@Stage("Segment") public SingleThreadedDirectBitSet freeList = new SingleThreadedDirectBitSet();
@Stage("Segment") public final PublicMultiStoreBytes segmentBytes = new PublicMultiStoreBytes();
@Stage("Segment") public final PointerBytesStore segmentBS = new PointerBytesStore();
@Stage("Segment") public final ReusableBitSet freeList = new ReusableBitSet(
new SingleThreadedFlatBitSetFrame(LONGS.align(hh.h().actualChunksPerSegment, BITS)),
Access.nativeAccess(), null, 0);
@Stage("Segment") long entrySpaceOffset = 0;
@Stage("Segment") public long segmentBase;
boolean segmentInit() {
return entrySpaceOffset > 0;
@@ -358,11 +452,17 @@ public abstract class SegmentStages implements SegmentLock {
void initSegment() {
VanillaChronicleHash<?, ?, ?, ?, ?, ?> h = hh.h();
long hashLookupOffset = h.segmentOffset(segmentIndex);
segmentBase = hh.h().ms.address() + hashLookupOffset;
long freeListOffset = hashLookupOffset + h.segmentHashLookupOuterSize;
freeListBytes.storePositionAndSize(h.ms, freeListOffset, h.segmentFreeListInnerSize);
freeList.reuse(freeListBytes);
PublicMultiStoreBytes segmentBytes = this.segmentBytes;
segmentBytes.setBytesOffset(h.tierBytes(tierIndex), h.bytesOffset(tierIndex));
long segmentBase = this.segmentBaseAddr;
segmentBS.set(segmentBase, h.segmentSize);
// 64 is a cache line with tier data
long freeListOffset = h.segmentHashLookupOuterSize + 64;
freeList.setOffset(segmentBase + freeListOffset);
entrySpaceOffset = freeListOffset + h.segmentFreeListOuterSize +
h.segmentEntrySpaceInnerOffset;
}
@@ -373,21 +473,22 @@ public abstract class SegmentStages implements SegmentLock {
public long allocReturnCode(int chunks) {
VanillaChronicleHash<?, ?, ?, ?, ?, ?> h = hh.h();
if (chunks > h.maxChunksPerEntry)
if (chunks > h.maxChunksPerEntry) {
throw new IllegalArgumentException("Entry is too large: requires " + chunks +
" entry size chucks, " + h.maxChunksPerEntry + " is maximum.");
}
long ret = freeList.setNextNContinuousClearBits(nextPosToSearchFrom(), chunks);
if (ret == DirectBitSet.NOT_FOUND || ret + chunks > h.actualChunksPerSegment) {
if (ret != DirectBitSet.NOT_FOUND &&
ret + chunks > h.actualChunksPerSegment && ret < h.actualChunksPerSegment) {
freeList.clear(ret, h.actualChunksPerSegment);
freeList.clearRange(ret, h.actualChunksPerSegment);
}
ret = freeList.setNextNContinuousClearBits(0L, chunks);
if (ret == DirectBitSet.NOT_FOUND || ret + chunks > h.actualChunksPerSegment) {
if (ret != DirectBitSet.NOT_FOUND &&
ret + chunks > h.actualChunksPerSegment &&
ret < h.actualChunksPerSegment) {
freeList.clear(ret, h.actualChunksPerSegment);
freeList.clearRange(ret, h.actualChunksPerSegment);
}
return -1;
}
@@ -404,24 +505,18 @@ public abstract class SegmentStages implements SegmentLock {
}
public long alloc(int chunks) {
long ret = allocReturnCode(chunks);
if (ret >= 0) {
return ret;
} else {
if (chunks == 1) {
throw new IllegalStateException(
"Segment is full, no free entries found");
while (true) {
long ret = allocReturnCode(chunks);
if (ret >= 0) {
return ret;
} else {
throw new IllegalStateException(
"Segment is full or has no ranges of " + chunks
+ " continuous free chunks"
);
nextTier();
}
}
}
public void free(long fromPos, int chunks) {
freeList.clear(fromPos, fromPos + chunks);
freeList.clearRange(fromPos, fromPos + chunks);
if (fromPos < nextPosToSearchFrom())
nextPosToSearchFrom(fromPos);
}
@@ -432,16 +527,4 @@ public abstract class SegmentStages implements SegmentLock {
nextPosToSearchFrom = 0L;
nextPosToSearchFrom(nextPosToSearchFrom);
}
public void clearSegment() {
innerWriteLock.lock();
hh.h().hashLookup.clearHashLookup(segmentBase);
freeList.clear();
nextPosToSearchFrom(0L);
entries(0L);
}
public void clear() {
clearSegment();
}
}
@@ -34,7 +34,7 @@ import java.util.function.Predicate;
public abstract class HashSegmentIteration<K, E extends HashEntry<K>>
implements HashEntry<K>, HashSegmentContext<K, E> {
@StageRef public SegmentStages s;
@StageRef public IterationSegmentStages s;
@StageRef HashEntryStages<K> e;
@StageRef VanillaChronicleHashHolder<?, ?, ?> hh;
@StageRef public CheckOnEachPublicOperation checkOnEachPublicOperation;
@@ -52,6 +52,20 @@ public abstract class HashSegmentIteration<K, E extends HashEntry<K>>
this.entryRemovedOnThisIteration = entryRemovedOnThisIteration;
}
public long hashLookupEntry = 0;
public void initHashLookupEntry(long entry) {
hashLookupEntry = entry;
}
abstract void closeHashLookupEntry();
private void goToLastTier() {
while (s.hasNextTier()) {
s.nextTier();
}
}
@Override
public boolean forEachSegmentEntryWhile(Predicate<? super E> action) {
s.innerUpdateLock.lock();
@@ -59,20 +73,46 @@ public abstract class HashSegmentIteration<K, E extends HashEntry<K>>
long size = s.size();
if (size == 0)
return true;
boolean interrupted = false;
long startPos = 0L;
CompactOffHeapLinearHashTable hashLookup = hh.h().hashLookup;
while (!hashLookup.empty(hashLookup.readEntry(s.segmentBase, startPos))) {
startPos = hashLookup.step(startPos);
goToLastTier();
while (true) {
int currentTier = s.segmentTier;
long currentTierBaseAddr = s.segmentBaseAddr;
size = forEachTierWhile(action, size, currentTier, currentTierBaseAddr);
if (size < 0) // interrupted
return false;
if (size == 0)
return true;
if (currentTier == 0)
throw new IllegalStateException("We iterated all tiers without interruption, " +
"but according to size counter there should be " + size + " more " +
"entries. Size diverged?");
s.prevTier();
}
hlp.initHashLookupPos(startPos);
do {
hlp.setHashLookupPos(hashLookup.step(hlp.hashLookupPos));
long entry = hashLookup.readEntry(s.segmentBase, hlp.hashLookupPos);
if (!hashLookup.empty(entry)) {
e.readExistingEntry(hashLookup.value(entry));
if (entryIsPresent()) {
initEntryRemovedOnThisIteration(false);
} finally {
closeHashLookupEntry();
s.innerReadLock.unlock();
initEntryRemovedOnThisIteration(false);
}
}
private long forEachTierWhile(
Predicate<? super E> action, long size, int currentTier, long currentTierBaseAddr) {
boolean interrupted = false;
long startPos = 0L;
CompactOffHeapLinearHashTable hashLookup = hh.h().hashLookup;
while (!hashLookup.empty(hashLookup.readEntry(s.segmentBaseAddr, startPos))) {
startPos = hashLookup.step(startPos);
}
hlp.initHashLookupPos(startPos);
do {
hlp.setHashLookupPos(hashLookup.step(hlp.hashLookupPos));
long entry = hashLookup.readEntry(s.segmentBaseAddr, hlp.hashLookupPos);
initHashLookupEntry(entry);
if (!hashLookup.empty(entry)) {
e.readExistingEntry(hashLookup.value(entry));
if (entryIsPresent()) {
initEntryRemovedOnThisIteration(false);
try {
if (!action.test((E) this)) {
interrupted = true;
break;
@@ -80,15 +120,16 @@ public abstract class HashSegmentIteration<K, E extends HashEntry<K>>
if (--size == 0)
break;
}
} finally {
// if doReplaceValue() -> relocation() -> alloc() -> nextTier()
// was called, restore the tier we were iterating over
if (s.segmentTier != currentTier)
s.initSegmentTier_WithBaseAddr(currentTier, currentTierBaseAddr);
}
}
} while (hlp.hashLookupPos != startPos);
return !interrupted;
} finally {
s.innerReadLock.unlock();
initEntryRemovedOnThisIteration(false);
}
}
} while (hlp.hashLookupPos != startPos);
return interrupted ? ~size : size;
}
@Override
@@ -118,7 +159,7 @@ public abstract class HashSegmentIteration<K, E extends HashEntry<K>>
public void iterationRemove() {
// this condition mean -- some other entry taken place of the removed one
if (hh.h().hashLookup.remove(s.segmentBase, hlp.hashLookupPos) != hlp.hashLookupPos) {
if (hh.h().hashLookup.remove(s.segmentBaseAddr, hlp.hashLookupPos) != hlp.hashLookupPos) {
// if so, should make step back, to compensate step forward on the next iteration,
// to consume the shifted entry
hlp.setHashLookupPos(hh.h().hashLookup.stepBack(hlp.hashLookupPos));
@@ -0,0 +1,50 @@
/*
* 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.impl.VanillaChronicleHashHolder;
import net.openhft.chronicle.hash.impl.stage.entry.SegmentStages;
import net.openhft.chronicle.hash.impl.stage.entry.HashLookupSearch;
import net.openhft.chronicle.hash.impl.stage.query.KeySearch;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@Staged
public abstract class IterationSegmentStages extends SegmentStages {
@StageRef VanillaChronicleHashHolder<?, ?, ?> hh;
@StageRef HashSegmentIteration it;
@StageRef HashLookupSearch hls;
/**
* During iteration, nextTier() is called in doReplaceValue() -> relocation() -> alloc().
* When the entry is relocated to the next tier, an entry should be inserted into hash
* lookup. To insert an entry into hashLookup, should 1) locate empty slot, see {@link
* KeySearch#initKeySearch()}, and 2) know the part of the hash code to insert, we know
* it during iteration
*/
@Override
public void nextTier() {
super.nextTier();
hls.initSearchKey(hh.h().hashLookup.key(it.hashLookupEntry));
}
public void initSegmentTier_WithBaseAddr(int tier, long tierBaseAddr) {
segmentTier = tier;
segmentBaseAddr = tierBaseAddr;
}
}
@@ -24,8 +24,10 @@ import net.openhft.chronicle.hash.impl.stage.data.bytes.InputKeyBytesData;
import net.openhft.chronicle.hash.impl.stage.data.instance.InputKeyInstanceData;
import net.openhft.chronicle.hash.impl.stage.entry.HashEntryStages;
import net.openhft.chronicle.hash.impl.stage.entry.HashLookupPos;
import net.openhft.chronicle.hash.impl.stage.entry.HashLookupSearch;
import net.openhft.chronicle.hash.impl.stage.entry.SegmentStages;
import net.openhft.chronicle.hash.impl.stage.hash.CheckOnEachPublicOperation;
import net.openhft.sg.Stage;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@@ -72,8 +74,48 @@ public abstract class HashQuery<K> implements HashEntry<K> {
hashOfKey = ks.inputKey.hash(LongHashFunction.city_1_1());
}
public enum EntryPresence {PRESENT, ABSENT}
@Stage("PresenceOfEntry") private EntryPresence entryPresence = null;
private void initPresenceOfEntry() {
if (ks.searchStatePresent() || tieredEntryPresent()) {
entryPresence = EntryPresence.PRESENT;
} else {
entryPresence = EntryPresence.ABSENT;
}
}
public void initPresenceOfEntry(EntryPresence entryPresence) {
this.entryPresence = entryPresence;
}
private boolean tieredEntryPresent() {
int firstTier = s.segmentTier;
long firstTierBaseAddr = s.segmentBaseAddr;
while (true) {
if (s.hasNextTier()) {
s.nextTier();
} else {
if (s.segmentTier != 0)
s.initSegmentTier(); // loop to the root tier
}
if (s.segmentBaseAddr == firstTierBaseAddr)
break;
if (ks.searchStatePresent())
return true;
}
// not found
if (firstTier != 0) {
// key is absent; probably are going to allocate a new entry;
// start trying from the root tier
s.initSegmentTier();
}
return false;
}
public boolean entryPresent() {
return ks.searchStatePresent();
return entryPresence == EntryPresence.PRESENT;
}
@Override
@@ -86,6 +128,7 @@ public abstract class HashQuery<K> implements HashEntry<K> {
hashLookupSearch.remove();
entry.innerRemoveEntryExceptHashLookupUpdate();
ks.setSearchState(DELETED);
initPresenceOfEntry(EntryPresence.ABSENT);
} else {
throw new IllegalStateException("Entry is absent when doRemove() is called");
}
@@ -19,6 +19,7 @@ package net.openhft.chronicle.hash.impl.stage.query;
import net.openhft.chronicle.bytes.BytesUtil;
import net.openhft.chronicle.hash.Data;
import net.openhft.chronicle.hash.impl.stage.entry.HashEntryStages;
import net.openhft.chronicle.hash.impl.stage.entry.HashLookupSearch;
import net.openhft.chronicle.hash.impl.stage.entry.SegmentStages;
import net.openhft.sg.Stage;
import net.openhft.sg.StageRef;
@@ -36,6 +37,8 @@ public abstract class KeySearch<K> {
public Data<K> inputKey = null;
abstract boolean inputKeyInit();
public void initInputKey(Data<K> inputKey) {
this.inputKey = inputKey;
}
@@ -55,21 +58,27 @@ public abstract class KeySearch<K> {
this.searchState = newSearchState;
}
void initKeySearch() {
public void initKeySearch() {
for (long pos; (pos = hashLookupSearch.nextPos()) >= 0L;) {
entry.readExistingEntry(pos);
if (!keyEquals())
continue;
hashLookupSearch.found();
keyFound();
return;
// otherwise we are inside iteration relocation.
// During iteration, key search occurs when doReplaceValue() exhausts space in
// the current segment, and insertion into the tiered segment requires to locate
// an empty slot in the hashLookup.
if (inputKeyInit()) {
entry.readExistingEntry(pos);
if (!keyEquals())
continue;
hashLookupSearch.found();
keyFound();
return;
}
}
searchState = SearchState.ABSENT;
}
boolean keyEquals() {
return inputKey.size() == entry.keySize &&
BytesUtil.bytesEqual(entry.entryBS, entry.keyOffset,
BytesUtil.bytesEqual(s.segmentBS, entry.keyOffset,
inputKey.bytes(), inputKey.offset(), entry.keySize);
}
@@ -17,6 +17,7 @@
package net.openhft.chronicle.hash.impl.stage.query;
import net.openhft.chronicle.hash.impl.stage.entry.HashLookupPos;
import net.openhft.chronicle.hash.impl.stage.entry.HashLookupSearch;
import net.openhft.chronicle.hash.impl.stage.entry.SegmentStages;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@@ -28,6 +29,10 @@ public abstract class QueryHashLookupPos extends HashLookupPos {
@StageRef HashLookupSearch hashLookupSearch;
public void initHashLookupPos() {
// validation + make hashLookupPos a dependant of segmentTier. This is needed, because
// after tier change should re-perform hashLookupSearch, starting from the searchStartPos
int segmentTier = s.segmentTier;
assert segmentTier >= 0 : "segment tier " + segmentTier;
s.innerReadLock.lock();
this.hashLookupPos = hashLookupSearch.searchStartPos;
}
@@ -0,0 +1,31 @@
/*
* 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.query;
import net.openhft.chronicle.hash.impl.stage.entry.HashLookupSearch;
import net.openhft.sg.StageRef;
import net.openhft.sg.Staged;
@Staged
public abstract class QueryHashLookupSearch extends HashLookupSearch {
@StageRef HashQuery op;
void initSearchKey() {
initSearchKey(hl().maskUnsetKey(hh.h().hashSplitting.segmentHash(op.hashOfKey)));
}
}
@@ -35,11 +35,17 @@ public class SearchAllocatedChunks extends AllocatedChunks {
}
}
public void initEntryAndKey(long entrySize) {
/**
* @return {@code true} is tier has changed
*/
public boolean initEntryAndKey(long entrySize) {
initAllocatedChunks(hh.h().inChunks(entrySize));
// call incrementSegmentEntriesIfNeeded() before entry.writeNewEntry(), because the latter
// clears out searchState, and it performs the search again, but in inconsistent state
incrementSegmentEntriesIfNeeded();
entry.writeNewEntry(s.alloc(allocatedChunks), ks.inputKey);
int tierBeforeAllocation = s.segmentTier;
long pos = s.alloc(allocatedChunks);
entry.writeNewEntry(pos, ks.inputKey);
return s.segmentTier > tierBeforeAllocation;
}
}