diff --git a/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/Alloc.java b/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/Alloc.java index 3ff9bee52..4b129543c 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/Alloc.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/Alloc.java @@ -19,10 +19,14 @@ package net.openhft.chronicle.hash.impl.stage.entry; public interface Alloc { /** - * Allocates a position in a segment tier. + * Allocates a block of specified number of chunks in a segment tier, optionally clears the + * previous allocation. * * @param chunks chunks to allocate - * @return the position, if allocation is successful, or -1 + * @param prevPos the previous position to clear, -1 if not needed + * @param prevChunks the size of the previous allocation to clear, 0 if not needed + * @return the new allocation position + * @throws RuntimeException if fails to allocate a block */ - long alloc(int chunks); + long alloc(int chunks, long prevPos, int prevChunks); } diff --git a/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/AllocatedChunks.java b/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/AllocatedChunks.java index 322a3895a..52945be82 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/AllocatedChunks.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/AllocatedChunks.java @@ -41,18 +41,19 @@ public class AllocatedChunks { /** * @return {@code true} is tier has changed */ - public boolean initEntryAndKeyCopying(long entrySize, long bytesToCopy) { + public boolean initEntryAndKeyCopying( + long entrySize, long bytesToCopy, long prevPos, int prevChunks) { 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(); - long oldSegmentTierBaseAddr = s.segmentBaseAddr; + long oldSegmentTierBaseAddr = s.tierBaseAddr; long oldKeySizeAddr = oldSegmentTierBaseAddr + entry.keySizeOffset; long oldKeyAddr = oldSegmentTierBaseAddr + entry.keyOffset; - int tierBeforeAllocation = s.segmentTier; - long pos = alloc.alloc(allocatedChunks); + int tierBeforeAllocation = s.tier; + long pos = alloc.alloc(allocatedChunks, prevPos, prevChunks); entry.copyExistingEntry(pos, bytesToCopy, oldKeyAddr, oldKeySizeAddr); - return s.segmentTier != tierBeforeAllocation; + return s.tier != tierBeforeAllocation; } } diff --git a/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashKeyCrc32PayloadChecksumStrategy.java b/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashKeyCrc32PayloadChecksumStrategy.java index 511eb0986..96335f244 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashKeyCrc32PayloadChecksumStrategy.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashKeyCrc32PayloadChecksumStrategy.java @@ -44,7 +44,7 @@ public class HashKeyCrc32PayloadChecksumStrategy implements ChecksumStrategy { long checksum; if (len > 0) { - long addr = s.segmentBaseAddr + keyEnd; + long addr = s.tierBaseAddr + keyEnd; int payloadCrc = Crc32.compute(addr, len); checksum = hash8To16Bytes(e.keySize, keyHashCode, payloadCrc); } else { diff --git a/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashLookupPos.java b/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashLookupPos.java index 73db99b4e..b056f4fd0 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashLookupPos.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashLookupPos.java @@ -32,9 +32,9 @@ public abstract class HashLookupPos { public abstract boolean hashLookupPosInit(); 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 - assert s.segmentTier >= 0; + // validation + make hashLookupPos a dependant of tier. This is needed, because after + // tier change should re-perform hashLookupSearch, starting from the searchStartPos + assert s.tier >= 0; s.innerReadLock.lock(); this.hashLookupPos = hls.searchStartPos; } @@ -56,6 +56,6 @@ public abstract class HashLookupPos { public void putValueVolatile(long newValue) { CompactOffHeapLinearHashTable hashLookup = hh.h().hashLookup; hashLookup.checkValueForPut(newValue); - hashLookup.putValueVolatile(s.segmentBaseAddr, hashLookupPos, newValue); + hashLookup.putValueVolatile(s.tierBaseAddr, hashLookupPos, newValue); } } diff --git a/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashLookupSearch.java b/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashLookupSearch.java index b70685980..91d27a0b1 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashLookupSearch.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashLookupSearch.java @@ -46,7 +46,7 @@ public abstract class HashLookupSearch { } private long addr() { - return s.segmentBaseAddr; + return s.tierBaseAddr; } public long nextPos() { diff --git a/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/SegmentStages.java b/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/SegmentStages.java index 51bab364c..e9f5d016a 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/SegmentStages.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/SegmentStages.java @@ -75,7 +75,7 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface { } long nextPosToSearchFrom() { - if (segmentTier == 0) { + if (tier == 0) { return segmentHeader.nextPosToSearchFrom(segmentHeaderAddress); } else { return nextPosToSearchFromTiered(); @@ -83,7 +83,7 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface { } public void nextPosToSearchFrom(long nextPosToSearchFrom) { - if (segmentTier == 0) { + if (tier == 0) { segmentHeader.nextPosToSearchFrom(segmentHeaderAddress, nextPosToSearchFrom); } else { nextPosToSearchFromTiered(nextPosToSearchFrom); @@ -479,34 +479,34 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface { return innerWriteLock; } - @Stage("SegmentTier") public int segmentTier = -1; + @Stage("SegmentTier") public int tier = -1; @Stage("SegmentTier") public long tierIndex; - @Stage("SegmentTier") public long segmentBaseAddr; + @Stage("SegmentTier") public long tierBaseAddr; public void initSegmentTier() { tierIndex = segmentIndex + 1; // tiers are 1-counted - segmentBaseAddr = hh.h().segmentBaseAddr(segmentIndex); + tierBaseAddr = 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; + tier = 0; } private void initSegmentTier(int tier, long tierIndex) { - segmentTier = tier; + this.tier = tier; this.tierIndex = tierIndex; assert tierIndex > 0; - this.segmentBaseAddr = hh.h().tierIndexToBaseAddr(tierIndex); + this.tierBaseAddr = hh.h().tierIndexToBaseAddr(tierIndex); } public void initSegmentTier(int tier, long tierIndex, long tierBaseAddr) { - segmentTier = tier; + this.tier = tier; this.tierIndex = tierIndex; - this.segmentBaseAddr = tierBaseAddr; + this.tierBaseAddr = tierBaseAddr; } public long tierCountersAreaAddr() { - return segmentBaseAddr + hh.h().tierHashLookupOuterSize; + return tierBaseAddr + hh.h().tierHashLookupOuterSize; } public long nextTierIndex() { @@ -537,13 +537,13 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface { VanillaChronicleHash h = hh.h(); long nextTierIndex = nextTierIndex(); if (nextTierIndex == 0) { - nextTierIndex = h.allocateTier(segmentIndex, segmentTier + 1); + nextTierIndex = h.allocateTier(segmentIndex, tier + 1); nextTierIndex(nextTierIndex); long currentTierIndex = tierIndex; - initSegmentTier(segmentTier + 1, nextTierIndex); + initSegmentTier(tier + 1, nextTierIndex); prevTierIndex(currentTierIndex); } else { - initSegmentTier(segmentTier + 1, nextTierIndex); + initSegmentTier(tier + 1, nextTierIndex); } } @@ -552,9 +552,9 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface { } public void prevTier() { - if (segmentTier == 0) + if (tier == 0) throw new IllegalStateException("first tier doesn't have previous"); - initSegmentTier(segmentTier - 1, prevTierIndex()); + initSegmentTier(tier - 1, prevTierIndex()); } public void goToLastTier() { @@ -564,7 +564,7 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface { } public void goToFirstTier() { - while (segmentTier != 0) { + while (tier != 0) { prevTier(); } } @@ -583,7 +583,7 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface { void initSegment() { VanillaChronicleHash h = hh.h(); - long segmentBaseAddr = this.segmentBaseAddr; + long segmentBaseAddr = this.tierBaseAddr; segmentBS.set(segmentBaseAddr, h.tierSize); segmentBytes.clear(); diff --git a/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/HashSegmentIteration.java b/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/HashSegmentIteration.java index b87d397cc..2a6c938cd 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/HashSegmentIteration.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/HashSegmentIteration.java @@ -70,8 +70,8 @@ public abstract class HashSegmentIteration> return true; s.goToLastTier(); while (true) { - int currentTier = s.segmentTier; - long currentTierBaseAddr = s.segmentBaseAddr; + int currentTier = s.tier; + long currentTierBaseAddr = s.tierBaseAddr; long currentTierIndex = s.tierIndex; size = forEachTierWhile(action, size, currentTier, currentTierBaseAddr, currentTierIndex); @@ -127,7 +127,7 @@ public abstract class HashSegmentIteration> } finally { // if doReplaceValue() -> relocation() -> alloc() -> nextTier() // was called, restore the tier we were iterating over - if (s.segmentTier != currentTier) { + if (s.tier != currentTier) { s.initSegmentTier_WithBaseAddr( currentTier, currentTierBaseAddr, tierIndex); // To cover shift deleted slot, at the next step forward. @@ -171,7 +171,7 @@ public abstract class HashSegmentIteration> public void iterationRemove() { // this condition mean -- some other entry taken place of the removed one - if (hh.h().hashLookup.remove(s.segmentBaseAddr, hlp.hashLookupPos) != hlp.hashLookupPos) { + if (hh.h().hashLookup.remove(s.tierBaseAddr, 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)); diff --git a/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationAlloc.java b/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationAlloc.java index 97a73098f..aa156e26a 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationAlloc.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationAlloc.java @@ -30,14 +30,17 @@ public class IterationAlloc implements Alloc { * Move only to next tiers, to avoid double visiting of relocated entries during iteration */ @Override - public long alloc(int chunks) { + public long alloc(int chunks, long prevPos, int prevChunks) { + long ret = s.allocReturnCode(chunks); + if (prevPos >= 0) + s.free(prevPos, prevChunks); + if (ret >= 0) + return ret; while (true) { - long ret = s.allocReturnCode(chunks); - if (ret >= 0) { + s.nextTier(); + ret = s.allocReturnCode(chunks); + if (ret >= 0) return ret; - } else { - s.nextTier(); - } } } } diff --git a/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationKeyHashCode.java b/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationKeyHashCode.java index 1870b0b3a..7e67ac222 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationKeyHashCode.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationKeyHashCode.java @@ -32,7 +32,7 @@ public class IterationKeyHashCode implements KeyHashCode { long keyHash = 0; void initKeyHash() { - long addr = s.segmentBaseAddr + e.keyOffset; + long addr = s.tierBaseAddr + e.keyOffset; long len = e.keySize; keyHash = LongHashFunction.city_1_1().hashMemory(addr, len); } diff --git a/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationSegmentStages.java b/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationSegmentStages.java index 0ff9fc843..a72d07c72 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationSegmentStages.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationSegmentStages.java @@ -46,9 +46,9 @@ public abstract class IterationSegmentStages extends SegmentStages { } public void initSegmentTier_WithBaseAddr(int tier, long tierBaseAddr, long tierIndex) { - segmentTier = tier; + this.tier = tier; this.tierIndex = tierIndex; - segmentBaseAddr = tierBaseAddr; + this.tierBaseAddr = tierBaseAddr; } @Override diff --git a/src/main/java/net/openhft/chronicle/hash/impl/stage/query/HashQuery.java b/src/main/java/net/openhft/chronicle/hash/impl/stage/query/HashQuery.java index 6d0b21e62..a455dae5e 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/stage/query/HashQuery.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/stage/query/HashQuery.java @@ -102,16 +102,16 @@ public abstract class HashQuery implements HashEntry { } private boolean tieredEntryPresent() { - int firstTier = s.segmentTier; - long firstTierBaseAddr = s.segmentBaseAddr; + int firstTier = s.tier; + long firstTierBaseAddr = s.tierBaseAddr; while (true) { if (s.hasNextTier()) { s.nextTier(); } else { - if (s.segmentTier != 0) + if (s.tier != 0) s.initSegmentTier(); // loop to the root tier } - if (s.segmentBaseAddr == firstTierBaseAddr) + if (s.tierBaseAddr == firstTierBaseAddr) break; if (ks.searchStatePresent()) return true; diff --git a/src/main/java/net/openhft/chronicle/hash/impl/stage/query/QueryAlloc.java b/src/main/java/net/openhft/chronicle/hash/impl/stage/query/QueryAlloc.java index 687082c19..2b1786c5a 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/stage/query/QueryAlloc.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/stage/query/QueryAlloc.java @@ -27,14 +27,16 @@ public class QueryAlloc implements Alloc { @StageRef public SegmentStages s; @Override - public long alloc(int chunks) { + public long alloc(int chunks, long prevPos, int prevChunks) { long ret = s.allocReturnCode(chunks); + if (prevPos >= 0) + s.free(prevPos, prevChunks); if (ret >= 0) return ret; - int alreadyAttemptedTier = s.segmentTier; + int alreadyAttemptedTier = s.tier; s.goToFirstTier(); while (true) { - if (s.segmentTier != alreadyAttemptedTier) { + if (s.tier != alreadyAttemptedTier) { ret = s.allocReturnCode(chunks); if (ret >= 0) return ret; diff --git a/src/main/java/net/openhft/chronicle/hash/impl/stage/query/SearchAllocatedChunks.java b/src/main/java/net/openhft/chronicle/hash/impl/stage/query/SearchAllocatedChunks.java index 24ad23d91..8b3844dce 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/stage/query/SearchAllocatedChunks.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/stage/query/SearchAllocatedChunks.java @@ -43,9 +43,9 @@ public class SearchAllocatedChunks extends AllocatedChunks { // call incrementSegmentEntriesIfNeeded() before entry.writeNewEntry(), because the latter // clears out searchState, and it performs the search again, but in inconsistent state incrementSegmentEntriesIfNeeded(); - int tierBeforeAllocation = s.segmentTier; - long pos = alloc.alloc(allocatedChunks); + int tierBeforeAllocation = s.tier; + long pos = alloc.alloc(allocatedChunks, -1, 0); entry.writeNewEntry(pos, ks.inputKey); - return s.segmentTier != tierBeforeAllocation; + return s.tier != tierBeforeAllocation; } } diff --git a/src/main/java/net/openhft/chronicle/map/impl/stage/entry/MapEntryStages.java b/src/main/java/net/openhft/chronicle/map/impl/stage/entry/MapEntryStages.java index 316156340..5cbc75c74 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/stage/entry/MapEntryStages.java +++ b/src/main/java/net/openhft/chronicle/map/impl/stage/entry/MapEntryStages.java @@ -216,17 +216,14 @@ public abstract class MapEntryStages extends HashEntryStages protected void relocation(Data newValue, long newSizeOfEverythingBeforeValue) { s.innerWriteLock.lock(); - // TODO free bits only after another range is allocated, to ensure they doesn't overlap, - // because overlapping copy is dangerous, platform dependent - s.free(pos, entrySizeInChunks); long entrySize = innerEntrySize(newSizeOfEverythingBeforeValue, newValue.size()); // need to copy, because in initEntryAndKeyCopying(), in alloc(), nextTier() called -> // hashLookupPos cleared, as a dependant long oldHashLookupPos = hlp.hashLookupPos; - long oldHashLookupAddr = s.segmentBaseAddr; + long oldHashLookupAddr = s.tierBaseAddr; - boolean tierHasChanged = - allocatedChunks.initEntryAndKeyCopying(entrySize, valueSizeOffset - keySizeOffset); + boolean tierHasChanged = allocatedChunks.initEntryAndKeyCopying( + entrySize, valueSizeOffset - keySizeOffset, pos, entrySizeInChunks); if (tierHasChanged) { // implicitly inits key search, locating hashLookupPos on the empty slot @@ -241,7 +238,7 @@ public abstract class MapEntryStages extends HashEntryStages CompactOffHeapLinearHashTable hl = hh.h().hashLookup; long oldEntry = hl.readEntry(oldHashLookupAddr, oldHashLookupPos); hl.checkValueForPut(pos); - hl.writeEntryVolatile(s.segmentBaseAddr, hlp.hashLookupPos, + hl.writeEntryVolatile(s.tierBaseAddr, hlp.hashLookupPos, oldEntry, hl.key(oldEntry), pos); if (tierHasChanged) hl.remove(oldHashLookupAddr, oldHashLookupPos); diff --git a/src/main/java/net/openhft/chronicle/map/impl/stage/iter/ReplicatedMapSegmentIteration.java b/src/main/java/net/openhft/chronicle/map/impl/stage/iter/ReplicatedMapSegmentIteration.java index f27d61db6..d1f642f62 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/stage/iter/ReplicatedMapSegmentIteration.java +++ b/src/main/java/net/openhft/chronicle/map/impl/stage/iter/ReplicatedMapSegmentIteration.java @@ -74,13 +74,13 @@ public abstract class ReplicatedMapSegmentIteration extends MapSegmentI boolean interrupted = false; long startPos = 0L; CompactOffHeapLinearHashTable hashLookup = hh.h().hashLookup; - while (!hashLookup.empty(hashLookup.readEntry(s.segmentBaseAddr, startPos))) { + while (!hashLookup.empty(hashLookup.readEntry(s.tierBaseAddr, startPos))) { startPos = hashLookup.step(startPos); } hlp.initHashLookupPos(startPos); do { hlp.setHashLookupPos(hashLookup.step(hlp.hashLookupPos)); - long entry = hashLookup.readEntry(s.segmentBaseAddr, hlp.hashLookupPos); + long entry = hashLookup.readEntry(s.tierBaseAddr, hlp.hashLookupPos); if (!hashLookup.empty(entry)) { e.readExistingEntry(hashLookup.value(entry)); ReplicableEntry e = !this.e.entryDeleted() ? entryDelegating : diff --git a/src/main/java/net/openhft/chronicle/map/impl/stage/query/QueryMapEntryStages.java b/src/main/java/net/openhft/chronicle/map/impl/stage/query/QueryMapEntryStages.java index 9b1f1dc78..ab3f276b5 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/stage/query/QueryMapEntryStages.java +++ b/src/main/java/net/openhft/chronicle/map/impl/stage/query/QueryMapEntryStages.java @@ -18,7 +18,6 @@ package net.openhft.chronicle.map.impl.stage.query; import net.openhft.chronicle.hash.Data; import net.openhft.chronicle.hash.impl.stage.entry.HashLookupSearch; -import net.openhft.chronicle.map.VanillaChronicleMap; import net.openhft.chronicle.map.impl.stage.entry.MapEntryStages; import net.openhft.sg.StageRef; import net.openhft.sg.Staged; @@ -66,13 +65,14 @@ public abstract class QueryMapEntryStages extends MapEntryStages { long entrySize = innerEntrySize(newSizeOfEverythingBeforeValue, newValue.size()); if (newValueSizeIsDifferent) { allocatedChunks.initEntryAndKeyCopying( - entrySize, valueSizeOffset - entryStartOffset); + entrySize, valueSizeOffset - entryStartOffset, -1, 0); initValue(newValue); } else { long oldValueSizeOffset = valueSizeOffset; long oldValueSize = valueSize; long oldValueOffset = valueOffset; - allocatedChunks.initEntryAndKeyCopying(entrySize, valueOffset - entryStartOffset); + allocatedChunks.initEntryAndKeyCopying( + entrySize, valueOffset - entryStartOffset, -1, 0); initValue_WithoutSize(newValue, oldValueSizeOffset, oldValueSize, oldValueOffset); } freeExtraAllocatedChunks(); diff --git a/src/main/java/net/openhft/chronicle/map/impl/stage/replication/ReplicatedQueryAlloc.java b/src/main/java/net/openhft/chronicle/map/impl/stage/replication/ReplicatedQueryAlloc.java index 4a9ac9a91..fffe3ec00 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/stage/replication/ReplicatedQueryAlloc.java +++ b/src/main/java/net/openhft/chronicle/map/impl/stage/replication/ReplicatedQueryAlloc.java @@ -23,6 +23,7 @@ import net.openhft.chronicle.hash.replication.ReplicableEntry; import net.openhft.chronicle.map.MapAbsentEntry; import net.openhft.chronicle.map.MapSegmentContext; import net.openhft.chronicle.map.ReplicatedChronicleMap; +import net.openhft.chronicle.map.impl.IterationContext; import net.openhft.chronicle.map.impl.ReplicatedChronicleMapHolder; import net.openhft.sg.StageRef; import net.openhft.sg.Staged; @@ -39,11 +40,14 @@ public class ReplicatedQueryAlloc extends QueryAlloc { /** * Returns {@code true} if at least one old deleted entry was removed. + * @param prevPos position to skip during cleanup (because cleaned up separately) */ - public boolean forcedOldDeletedEntriesCleanup() { + public boolean forcedOldDeletedEntriesCleanup(long prevPos) { ReplicatedChronicleMap map = mh.m(); try (MapSegmentContext sc = map.segmentContext(s.segmentIndex)) { cleanupAction.removedCompletely = 0; + cleanupAction.posToSkip = prevPos; + cleanupAction.iterationContext = (IterationContext) sc; ((ReplicatedHashSegmentContext) sc) .forEachSegmentReplicableEntry(cleanupAction); return cleanupAction.removedCompletely > 0; @@ -52,11 +56,13 @@ public class ReplicatedQueryAlloc extends QueryAlloc { private class CleanupAction implements Consumer { int removedCompletely; + long posToSkip; + IterationContext iterationContext; @Override public void accept(ReplicableEntry e) { ReplicatedChronicleMap map = mh.m(); - if (e instanceof MapAbsentEntry) { + if (e instanceof MapAbsentEntry && iterationContext.pos() != posToSkip) { long deleteTimeout = map.timeProvider.currentTime() - e.originTimestamp(); map.timeProvider.systemTimeIntervalBetween(e.originTimestamp(), map.timeProvider.currentTime(), map.cleanupTimeoutUnit); @@ -69,26 +75,41 @@ public class ReplicatedQueryAlloc extends QueryAlloc { } @Override - public long alloc(int chunks) { + public long alloc(int chunks, long prevPos, int prevChunks) { long ret = s.allocReturnCode(chunks); - if (ret >= 0) + if (ret >= 0) { + if (prevPos >= 0) + s.free(prevPos, prevChunks); return ret; - int alreadyAttemptedTier; - if (!forcedOldDeletedEntriesCleanup()) { - alreadyAttemptedTier = s.segmentTier; - } else { - // If forced old deleted entries cleanup deleted something, don't skip the first queried - // tier, because it could have new empty chunks now. segment tier is >= 0, so set to -1 - // to make the condition with alreadyAttemptedTier always false - alreadyAttemptedTier = -1; } + int firstAttemptedTier = s.tier; + long firstAttemptedTierIndex = s.tierIndex; + long firstAttemptedTierBaseAddr = s.tierBaseAddr; + boolean cleanedFirstAttemptedTier = forcedOldDeletedEntriesCleanup(prevPos); s.goToFirstTier(); while (true) { - if (s.segmentTier != alreadyAttemptedTier) { + boolean visitingFirstAttemptedTier = s.tier == firstAttemptedTier; + if (cleanedFirstAttemptedTier || !visitingFirstAttemptedTier) { ret = s.allocReturnCode(chunks); - if (ret >= 0) + if (ret >= 0) { + if (prevPos >= 0) { + if (visitingFirstAttemptedTier) { + s.free(prevPos, prevChunks); + } else if (s.tier < firstAttemptedTier) { + int currentTier = s.tier; + long currentTierIndex = s.tierIndex; + long currentTierBaseAddr = s.tierBaseAddr; + s.initSegmentTier(firstAttemptedTier, firstAttemptedTierIndex, + firstAttemptedTierBaseAddr); + s.free(prevPos, prevChunks); + s.initSegmentTier(currentTier, currentTierIndex, currentTierBaseAddr); + } + } return ret; + } } + if (visitingFirstAttemptedTier && prevPos >= 0) + s.free(prevPos, prevChunks); s.nextTier(); } }