From 9d1b4a82a1448973d1f6f5a4237bce8efbae4fed Mon Sep 17 00:00:00 2001 From: Michael Gibney Date: Mon, 9 Dec 2024 15:53:33 -0500 Subject: [PATCH] SAI-5261: fix CaffeineCache.put ramBytes decrement (#240) (cherry picked from commit caff168820c87ccad5c58221f714d72e2816fef4) --- .../src/java/org/apache/solr/search/CaffeineCache.java | 10 +++++++++- .../test/org/apache/solr/search/TestCaffeineCache.java | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/search/CaffeineCache.java b/solr/core/src/java/org/apache/solr/search/CaffeineCache.java index f6633593f18..850c63d1976 100644 --- a/solr/core/src/java/org/apache/solr/search/CaffeineCache.java +++ b/solr/core/src/java/org/apache/solr/search/CaffeineCache.java @@ -275,7 +275,15 @@ public V computeIfAbsent(K key, IOFunction mappingFuncti public V put(K key, V val) { inserts.increment(); V old = cache.asMap().put(key, val); - recordRamBytes(key, old, val); + // ramBytes decrement for `old` happens via #onRemoval + if (val != old) { + // NOTE: this is conditional on `val != old` in order to work around a + // behavior in the Caffeine library: where there is reference equality + // between `val` and `old`, caffeine does _not_ invoke RemovalListener, + // so the entry is not decremented for the replaced value (hence we + // don't need to increment ram bytes for the entry either). + recordRamBytes(key, null, val); + } return old; } diff --git a/solr/core/src/test/org/apache/solr/search/TestCaffeineCache.java b/solr/core/src/test/org/apache/solr/search/TestCaffeineCache.java index 87ff29d5e6a..b4c69600fbd 100644 --- a/solr/core/src/test/org/apache/solr/search/TestCaffeineCache.java +++ b/solr/core/src/test/org/apache/solr/search/TestCaffeineCache.java @@ -308,7 +308,7 @@ public void testRamBytesSync() throws IOException { cache.put(0, "test"); long nonEmptySize = cache.ramBytesUsed(); - cache.put(0, "test"); + cache.put(0, random().nextBoolean() ? "test" : "rest"); assertEquals(nonEmptySize, cache.ramBytesUsed()); cache.remove(0); @@ -341,7 +341,7 @@ public void testRamBytesAsync() throws IOException { cache.put(0, "test"); long nonEmptySize = cache.ramBytesUsed(); - cache.put(0, "test"); + cache.put(0, random().nextBoolean() ? "test" : "rest"); assertEquals(nonEmptySize, cache.ramBytesUsed()); cache.remove(0);