wchevreuil commented on code in PR #8939:
URL: https://github.com/apache/hbase/pull/8939#discussion_r4739374392
##########
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:
Can this still suffer from race condition? Say two read threads reach #541,
first one get previous and puts its block, then second threads gets there too,
puts the block from first thread in the previous ref and the original previous
is never decremented. Maybe not a big deal, there might not be a significant
difference in the accounted heap size, and adding locking on a hot path would
probably have a negative impact. Could do would just a comment?
--
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]