Skip to content

Commit

Permalink
Update IndicesRequestCache.java
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 12, 2024
1 parent 1ce58fb commit d37cfea
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CleanupKey> keysToClean;
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -524,13 +529,17 @@ 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.
*
* @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);
Expand Down Expand Up @@ -686,6 +695,9 @@ private boolean shouldRemoveKey(Key key, Set<CleanupKey> 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) {
Expand Down

0 comments on commit d37cfea

Please sign in to comment.