Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOLR-17597: Fix CaffeineCache.put() ramBytes decrement #2917

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions solr/core/src/java/org/apache/solr/search/CaffeineCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private V computeAsync(K key, IOFunction<? super K, ? extends V> mappingFunction
// We reserved the slot, so we do the work
V value = mappingFunction.apply(key);
future.complete(value); // This will update the weight and expiration
recordRamBytes(key, null, value);
recordRamBytes(key, value);
inserts.increment();
return value;
} catch (Error | RuntimeException | IOException e) {
Expand Down Expand Up @@ -262,7 +262,7 @@ public V computeIfAbsent(K key, IOFunction<? super K, ? extends V> mappingFuncti
if (value == null) {
return null;
}
recordRamBytes(key, null, value);
recordRamBytes(key, value);
inserts.increment();
return value;
});
Expand All @@ -275,30 +275,34 @@ 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, val);
}
return old;
}

/**
* Update the estimate of used memory
* Update the estimate of used memory.
*
* <p>NOTE: old value (in the event of replacement) adjusts {@link #ramBytes} via {@link
* #onRemoval(Object, Object, RemovalCause)}
*
* @param key the cache key
* @param oldValue the old cached value to decrement estimate (can be null)
* @param newValue the new cached value to increment estimate
*/
private void recordRamBytes(K key, V oldValue, V newValue) {
private void recordRamBytes(K key, V newValue) {
ramBytes.add(
RamUsageEstimator.sizeOfObject(newValue, RamUsageEstimator.QUERY_DEFAULT_RAM_BYTES_USED));
if (oldValue == null) {
ramBytes.add(
RamUsageEstimator.sizeOfObject(key, RamUsageEstimator.QUERY_DEFAULT_RAM_BYTES_USED));
ramBytes.add(RamUsageEstimator.LINKED_HASHTABLE_RAM_BYTES_PER_ENTRY);
if (async) ramBytes.add(RAM_BYTES_PER_FUTURE);
} else {
ramBytes.add(
-RamUsageEstimator.sizeOfObject(
oldValue, RamUsageEstimator.QUERY_DEFAULT_RAM_BYTES_USED));
}
ramBytes.add(
RamUsageEstimator.sizeOfObject(key, RamUsageEstimator.QUERY_DEFAULT_RAM_BYTES_USED));
ramBytes.add(RamUsageEstimator.LINKED_HASHTABLE_RAM_BYTES_PER_ENTRY);
if (async) ramBytes.add(RAM_BYTES_PER_FUTURE);
}

@Override
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
Loading