shihchengwei-lab commented on PR #8939:
URL: https://github.com/apache/hbase/pull/8939#issuecomment-7234199937

   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:
   
   ```text
   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.
   
   <details>
   <summary>Minimal reproduction</summary>
   
   Add this to `TestLruAdaptiveBlockCache`:
   
   ```java
   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.
   
   </details>
   
   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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to