Repository navigation
HBASE-30584 LruBlockCache and LruAdaptiveBlockCache leak size accounting when an existing block is replaced - #8939
petersomogyi wants to merge 2 commits into
Conversation
…ing when an existing block is replaced When cacheBlock replaces an already cached block (see BlockCacheUtil#shouldReplaceExistingCacheBlock), the new block's heap size was added to the size counter but the replaced block's size was never subtracted, and the element counters were incremented again. Each replace inflated the cache size by one block that eviction cannot free, until the cache reached its hard limit and rejected inserts while holding few or no blocks. Use the return value of map.put and, when a block was replaced, subtract its size, decrement the element counters and release the cache's reference to it. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
The replacement accounting is still incorrect when eviction races with a same-key replacement. I reproduced this on Both focused tests produced: This buffer shape can occur on the normal heap path: cache-on-write creates a block for the current content, while the reader allocates room for the next block header and then narrows the current block's limit. The blocks can therefore have identical serialized content but different Minimal reproductionAdd this to private static HFileBlock createExclusiveIndexBlock(
long offset, int nextBlockOnDiskSize, int extraCapacity) {
int size = 100;
int serializedSize = HConstants.HFILEBLOCK_HEADER_SIZE + size;
ByteBuffer buf = ByteBuffer.allocate(serializedSize + extraCapacity);
buf.limit(serializedSize);
HFileContext meta = new HFileContextBuilder().build();
return new ExclusiveMemHFileBlock(
BlockType.LEAF_INDEX, size, size, -1, ByteBuff.wrap(buf),
HFileBlock.FILL_HEADER, offset, nextBlockOnDiskSize, -1, meta, HEAP);
}
@Test
public void testEvictingStaleReplacementUsesRemovedBlockSize() {
LruAdaptiveBlockCache cache =
new LruAdaptiveBlockCache(1024 * 1024, 1024, false);
long emptySize = cache.getCurrentSize();
BlockCacheKey key = new BlockCacheKey("file", 0);
HFileBlock original = createExclusiveIndexBlock(0, -1, 0);
cache.cacheBlock(key, original);
LruCachedBlock staleEvictionCandidate =
cache.getMapForTests().get(key);
HFileBlock replacement =
createExclusiveIndexBlock(0, 52, 256);
cache.cacheBlock(key, replacement);
assertTrue(replacement.heapSize() > original.heapSize());
cache.evictBlock(staleEvictionCandidate, false);
assertEquals(0, cache.getBlockCount());
assertEquals(emptySize, cache.getCurrentSize(),
"eviction must subtract the block that was actually removed");
}Changing the cache type to The residual size has no map entry, so later eviction cannot remove it. Repeated occurrences can eventually reach the hard limit and recreate the rejected-insert behavior this PR is intended to fix. Could both eviction paths use the block actually removed from the map for size, type, timestamp, victim handoff, and return value, and add this ordering as a regression test? AI assisted with the code review, reproduction test design, and wording. I ran the focused tests against the commit above and reviewed the results. |
|
Thanks for the review and the reproduction @shihchengwei-lab, you're right. This predates the PR, but it's the same class of bug, so I'm fixing it here. Both |
…accounting Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
When cacheBlock replaces an already cached block (see BlockCacheUtil#shouldReplaceExistingCacheBlock), the new block's heap size was added to the size counter but the replaced block's size was never subtracted, and the element counters were incremented again. Each replace inflated the cache size by one block that eviction cannot free, until the cache reached its hard limit and rejected inserts while holding few or no blocks.
Use the return value of map.put and, when a block was replaced, subtract its size, decrement the element counters and release the cache's reference to it.