Skip to content

Commit

Permalink
SAI-5261: fix CaffeineCache.put ramBytes decrement (#240)
Browse files Browse the repository at this point in the history
(cherry picked from commit caff168)
  • Loading branch information
magibney committed Dec 17, 2024
1 parent 85c89cb commit 9d1b4a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion solr/core/src/java/org/apache/solr/search/CaffeineCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,15 @@ public V computeIfAbsent(K key, IOFunction<? super K, ? extends V> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9d1b4a8

Please sign in to comment.