From d37cfea4e06be1cc8ff227f45206097f12c715a0 Mon Sep 17 00:00:00 2001 From: Kiran Prakash Date: Tue, 12 Mar 2024 12:20:54 -0700 Subject: [PATCH] Update IndicesRequestCache.java Signed-off-by: Kiran Prakash --- .../opensearch/indices/IndicesRequestCache.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/opensearch/indices/IndicesRequestCache.java b/server/src/main/java/org/opensearch/indices/IndicesRequestCache.java index aa4a8518d93a3..05b53d1152caa 100644 --- a/server/src/main/java/org/opensearch/indices/IndicesRequestCache.java +++ b/server/src/main/java/org/opensearch/indices/IndicesRequestCache.java @@ -465,6 +465,8 @@ public int hashCode() { * * It also keeps track of the number of stale keys in the cache (staleKeysCount) and a staleness threshold, * which is used to determine when the cache should be cleaned. + * + * If Staleness threshold is 0, we do not keep track of stale keys in the cache * */ class IndicesRequestCacheCleanupManager { private final Set keysToClean; @@ -475,10 +477,10 @@ class IndicesRequestCacheCleanupManager { private final Lock writeLock; IndicesRequestCacheCleanupManager(double stalenessThreshold) { + this.stalenessThreshold = stalenessThreshold; this.keysToClean = ConcurrentCollections.newConcurrentSet(); this.cleanupKeyToCountMap = ConcurrentCollections.newConcurrentMap(); this.staleKeysCount = new AtomicInteger(0); - this.stalenessThreshold = stalenessThreshold; ReadWriteLock rwLock = new ReentrantReadWriteLock(); this.readLock = rwLock.readLock(); this.writeLock = rwLock.writeLock(); @@ -507,7 +509,10 @@ void enqueueCleanupKey(CleanupKey cleanupKey) { * * @param cleanupKey the CleanupKey to be updated in the map */ - private void updateCleanupKeyToCountMap(CleanupKey cleanupKey) { // TODO call this oncached + private void updateCleanupKeyToCountMap(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); @@ -524,6 +529,7 @@ private void updateCleanupKeyToCountMap(CleanupKey cleanupKey) { // TODO call th /** * Updates the count of stale keys in the cache. * This method is called when a CleanupKey is added to the keysToClean set. + * * It increments the staleKeysCount by the count of the CleanupKey in the cleanupKeyToCountMap. * If the CleanupKey's readerCacheKeyId is null or the CleanupKey's entity is not open, it increments the staleKeysCount * by the total count of keys associated with the CleanupKey's ShardId in the cleanupKeyToCountMap and removes the ShardId from the map. @@ -531,6 +537,9 @@ private void updateCleanupKeyToCountMap(CleanupKey cleanupKey) { // TODO call th * @param cleanupKey the CleanupKey that has been marked for cleanup */ private void updateStaleKeysCount(CleanupKey cleanupKey) { + if(stalenessThreshold == 0.0) { + return; + } IndexShard indexShard = (IndexShard) cleanupKey.entity.getCacheIdentity(); if (indexShard == null) { logger.warn("IndexShard is null for CleanupKey: {}", cleanupKey.readerCacheKeyId); @@ -686,6 +695,9 @@ private boolean shouldRemoveKey(Key key, Set cleanupKeysFromOutdated * @return true if the cache cleanup process can be skipped, false otherwise. */ private boolean canSkipCacheCleanup(double cleanThresholdPercent) { + if(cleanThresholdPercent == 0.0) { + return false; + } readLock.lock(); try { if (staleKeysInCachePercentage() < cleanThresholdPercent) {