petersomogyi commented on code in PR #8939:
URL: https://github.com/apache/hbase/pull/8939#discussion_r4739754109
##########
hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/LruAdaptiveBlockCache.java:
##########
@@ -538,7 +538,19 @@ public void cacheBlock(BlockCacheKey cacheKey, Cacheable
buf, boolean inMemory)
buf = asReferencedHeapBlock(buf);
cb = new LruCachedBlock(cacheKey, buf, count.incrementAndGet(), inMemory);
long newSize = updateSizeMetrics(cb, false);
- map.put(cacheKey, cb);
+ LruCachedBlock previous = map.put(cacheKey, cb);
+ if (previous != null) {
+ // We replaced an existing block (see
BlockCacheUtil#shouldReplaceExistingCacheBlock), so
+ // take the replaced block out of the size and element counters,
otherwise it is counted
+ // forever and the cache fills up with blocks it no longer holds.
+ newSize = updateSizeMetrics(previous, true);
+ elements.decrementAndGet();
+ if (previous.getBuffer().getBlockType().isData()) {
+ dataBlockElements.decrement();
+ }
+ // The cache no longer references the replaced block.
+ previous.getBuffer().release();
+ }
Review Comment:
I don't think this can leak. The calculation uses the return value of
map.put, which is atomic per key: the first thread gets the original block back
and the second gets the first thread's block. So each removed block is
subtracted once, and the counter updates themselves are atomic adds. The only
effect is a short over-count between adding the new block and subtracting the
old one.
What do you think?
--
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]