Skip to content

Commit 8ad8b7a

Browse files
author
Peter Alfonsi
committed
Addressed more comments
Signed-off-by: Peter Alfonsi <[email protected]>
1 parent 8e05f06 commit 8ad8b7a

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

modules/cache-common/src/main/java/org/opensearch/cache/common/tier/TieredSpilloverCache.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -261,31 +261,26 @@ public V get(ICacheKey<K> key) {
261261
public void put(ICacheKey<K> key, V value) {
262262
// First check in case the key is already present in either of tiers.
263263
Tuple<V, String> cacheValueTuple = getValueFromTieredCache(true).apply(key);
264-
if (cacheValueTuple == null) {
265-
// In case it is not present in any tier, put it inside onHeap cache by default.
266-
if (evaluatePoliciesList(value, policies)) {
264+
if (evaluatePoliciesList(value, policies)) {
265+
if (cacheValueTuple == null) {
266+
// In case it is not present in any tier, put it inside onHeap cache by default.
267267
try (ReleasableLock ignore = writeLock.acquire()) {
268268
onHeapCache.put(key, value);
269269
}
270270
updateStatsOnPut(TIER_DIMENSION_VALUE_ON_HEAP, key, value);
271271
} else {
272-
// Signal to the caller that the key didn't enter the cache by sending a removal notification.
273-
removalListener.onRemoval(new RemovalNotification<>(key, value, RemovalReason.EXPLICIT));
274-
}
275-
} else {
276-
// Put it inside desired tier.
277-
if (evaluatePoliciesList(value, policies)) {
278272
try (ReleasableLock ignore = writeLock.acquire()) {
279273
for (Map.Entry<ICache<K, V>, TierInfo> entry : this.caches.entrySet()) {
280274
if (cacheValueTuple.v2().equals(entry.getValue().tierName)) {
281275
entry.getKey().put(key, value);
282276
}
283277
}
284-
updateStatsOnPut(cacheValueTuple.v2(), key, value);
285278
}
286-
} else {
287-
removalListener.onRemoval(new RemovalNotification<>(key, value, RemovalReason.EXPLICIT));
279+
updateStatsOnPut(cacheValueTuple.v2(), key, value);
288280
}
281+
} else {
282+
// Signal to the caller that the key didn't enter the cache by sending a removal notification.
283+
removalListener.onRemoval(new RemovalNotification<>(key, value, RemovalReason.EXPLICIT));
289284
}
290285
}
291286

@@ -350,15 +345,19 @@ private Tuple<V, Boolean> compute(
350345
boolean wasCacheMiss = false;
351346
BiFunction<Tuple<ICacheKey<K>, V>, Throwable, Void> handler = (pair, ex) -> {
352347
if (pair != null) {
348+
boolean didAddToCache = false;
353349
try (ReleasableLock ignore = writeLock.acquire()) {
354350
onHeapCache.put(pair.v1(), pair.v2());
351+
didAddToCache = true;
355352
} catch (Exception e) {
356353
// TODO: Catch specific exceptions to know whether this resulted from cache or underlying removal
357354
// listeners/stats. Needs better exception handling at underlying layers.For now swallowing
358355
// exception.
359356
logger.warn("Exception occurred while putting item onto heap cache", e);
360357
}
361-
updateStatsOnPut(TIER_DIMENSION_VALUE_ON_HEAP, key, pair.v2());
358+
if (didAddToCache) {
359+
updateStatsOnPut(TIER_DIMENSION_VALUE_ON_HEAP, key, pair.v2());
360+
}
362361
} else {
363362
if (ex != null) {
364363
logger.warn("Exception occurred while trying to compute the value", ex);

0 commit comments

Comments
 (0)