Skip to content

HBASE-30584 LruBlockCache and LruAdaptiveBlockCache leak size accounting when an existing block is replaced - #8939

Open
petersomogyi wants to merge 2 commits into
apache:masterfrom
petersomogyi:HBASE-30584
Open

petersomogyi wants to merge 2 commits into
apache:masterfrom
petersomogyi:HBASE-30584

Conversation

@petersomogyi

Copy link
Copy Markdown
Contributor

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.

…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>
@shihchengwei-lab

Copy link
Copy Markdown

The replacement accounting is still incorrect when eviction races with a same-key replacement.

I reproduced this on 60ba99a3fe4f1a4d6de097296cf73c07b3676a90 in both LruBlockCache and LruAdaptiveBlockCache. If eviction selects A, then B replaces the same key before eviction runs, the map removes B but the counters subtract A. When B.heapSize() > A.heapSize(), the difference remains permanently counted after the map is empty.

Both focused tests produced:

expected: <50176> but was: <50432>
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

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 heapSize() values.

Minimal reproduction

Add this to TestLruAdaptiveBlockCache:

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 LruBlockCache produces the same failure.

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.

@petersomogyi

Copy link
Copy Markdown
Contributor Author

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 evictBlock implementations now do their accounting with the block actually removed from the map. I also added testEvictingStaleEntryUsesRemovedBlockSize to both test classes, based on your reproduction. I'll push the change shortly.

…accounting

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants