Skip to content

Commit

Permalink
add updateCleanupKeyToCountMapOnCacheEviction
Browse files Browse the repository at this point in the history
Signed-off-by: Kiran Prakash <awskiran@amazon.com>
  • Loading branch information
kiranprakash154 committed Mar 13, 2024
1 parent cc9bf00 commit 97251f2
Showing 1 changed file with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ void clear(CacheEntity entity) {
public void onRemoval(RemovalNotification<Key, BytesReference> notification) {
// In case this event happens for an old shard, we can safely ignore this as we don't keep track for old
// shards as part of request cache.
cacheEntityLookup.apply(notification.getKey().shardId).ifPresent(entity -> entity.onRemoval(notification));
Key key = notification.getKey();
cacheEntityLookup.apply(key.shardId).ifPresent(entity -> entity.onRemoval(notification));
cacheCleanupManager.updateCleanupKeyToCountMapOnCacheEviction(
new CleanupKey(cacheEntityLookup.apply(key.shardId).orElse(null), key.readerCacheKeyId)
);
}

BytesReference getOrCompute(
Expand Down Expand Up @@ -222,7 +226,7 @@ BytesReference getOrCompute(
OpenSearchDirectoryReader.addReaderCloseListener(reader, cleanupKey);
}
}
cacheCleanupManager.updateCleanupKeyToCountMap(cleanupKey);
cacheCleanupManager.updateCleanupKeyToCountMapOnCacheInsertion(cleanupKey);
} else {
cacheEntity.onHit();
}
Expand Down Expand Up @@ -463,7 +467,7 @@ void enqueueCleanupKey(CleanupKey cleanupKey) {
*
* @param cleanupKey the CleanupKey to be updated in the map
*/
private void updateCleanupKeyToCountMap(CleanupKey cleanupKey) {
private void updateCleanupKeyToCountMapOnCacheInsertion(CleanupKey cleanupKey) {
if (stalenessThreshold == 0.0) {
return;
}
Expand All @@ -480,6 +484,29 @@ private void updateCleanupKeyToCountMap(CleanupKey cleanupKey) {
.merge(cleanupKey.readerCacheKeyId, 1, Integer::sum);
}

private void updateCleanupKeyToCountMapOnCacheEviction(CleanupKey cleanupKey) {
if (stalenessThreshold == 0.0) {
return;
}
IndexShard indexShard = (IndexShard) cleanupKey.entity.getCacheIdentity();
if (indexShard == null) {
logger.warn("IndexShard is null for CleanupKey: {} while cleaning Indices Request Cache", cleanupKey.readerCacheKeyId);
return;
}
ShardId shardId = indexShard.shardId();

writeLock.lock();
try {
// If the key doesn't exist, ignore
ConcurrentMap<String, Integer> keyCountMap = cleanupKeyToCountMap.get(shardId);
if (keyCountMap != null) {
keyCountMap.computeIfPresent(cleanupKey.readerCacheKeyId, (key, value) -> value - 1);
}
} finally {
writeLock.unlock();
}
}

/**
* Updates the count of stale keys in the cache.
* This method is called when a CleanupKey is added to the keysToClean set.
Expand Down

0 comments on commit 97251f2

Please sign in to comment.