Skip to content

Commit f2e58a8

Browse files
create IndicesRequestCacheCleanupManager & settings and validators
Signed-off-by: Kiran Prakash <awskiran@amazon.com>
1 parent 6ad0336 commit f2e58a8

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

server/src/main/java/org/opensearch/indices/IndicesRequestCache.java

+41
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.lucene.index.IndexReader;
3939
import org.apache.lucene.util.Accountable;
4040
import org.apache.lucene.util.RamUsageEstimator;
41+
import org.opensearch.OpenSearchParseException;
4142
import org.opensearch.common.CheckedSupplier;
4243
import org.opensearch.common.cache.CacheType;
4344
import org.opensearch.common.cache.ICache;
@@ -50,6 +51,7 @@
5051
import org.opensearch.common.settings.Setting;
5152
import org.opensearch.common.settings.Setting.Property;
5253
import org.opensearch.common.settings.Settings;
54+
import org.opensearch.common.unit.RatioValue;
5355
import org.opensearch.common.unit.TimeValue;
5456
import org.opensearch.common.util.concurrent.ConcurrentCollections;
5557
import org.opensearch.core.common.bytes.BytesReference;
@@ -117,6 +119,17 @@ public final class IndicesRequestCache implements RemovalListener<IndicesRequest
117119
new TimeValue(0),
118120
Property.NodeScope
119121
);
122+
public static final Setting<TimeValue> INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING = Setting.positiveTimeSetting(
123+
"indices.requests.cache.cleanup.interval",
124+
TimeValue.timeValueMinutes(1),
125+
Property.NodeScope
126+
);
127+
public static final Setting<String> INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING = new Setting<>(
128+
"indices.requests.cache.cleanup.staleness_threshold",
129+
"0%",
130+
IndicesRequestCache::validateStalenessSetting,
131+
Property.NodeScope
132+
);
120133

121134
private final static long BASE_RAM_BYTES_USED = RamUsageEstimator.shallowSizeOfInstance(Key.class);
122135

@@ -131,6 +144,7 @@ public final class IndicesRequestCache implements RemovalListener<IndicesRequest
131144
this.expire = INDICES_CACHE_QUERY_EXPIRE.exists(settings) ? INDICES_CACHE_QUERY_EXPIRE.get(settings) : null;
132145
long sizeInBytes = size.getBytes();
133146
ToLongBiFunction<Key, BytesReference> weigher = (k, v) -> k.ramBytesUsed() + v.ramBytesUsed();
147+
this.cacheCleanupManager = new IndicesRequestCacheCleanupManager(getStalenessThreshold(settings));
134148
this.cacheEntityLookup = cacheEntityFunction;
135149
this.cache = cacheService.createCache(
136150
new CacheConfig.Builder<Key, BytesReference>().setSettings(settings)
@@ -150,6 +164,11 @@ public void close() {
150164
cache.invalidateAll();
151165
}
152166

167+
private double getStalenessThreshold(Settings settings) {
168+
String threshold = INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING.get(settings);
169+
return RatioValue.parseRatioValue(threshold).getAsRatio();
170+
}
171+
153172
void clear(CacheEntity entity) {
154173
cacheCleanupManager.enqueueCleanupKey(new CleanupKey(entity, null));
155174
cacheCleanupManager.forceCleanCache();
@@ -658,4 +677,26 @@ long count() {
658677
int numRegisteredCloseListeners() { // for testing
659678
return registeredClosedListeners.size();
660679
}
680+
681+
/**
682+
* Validates the staleness setting for the cache cleanup threshold.
683+
*
684+
* <p>This method checks if the provided staleness threshold is a valid percentage or a valid double value.
685+
* If the staleness threshold is not valid, it throws an OpenSearchParseException.
686+
*
687+
* @param staleThreshold The staleness threshold to validate.
688+
* @return The validated staleness threshold.
689+
* @throws OpenSearchParseException If the staleness threshold is not a valid percentage or double value.
690+
*
691+
* <p>package private for testing
692+
*/
693+
static String validateStalenessSetting(String staleThreshold) {
694+
try {
695+
RatioValue.parseRatioValue(staleThreshold);
696+
} catch (OpenSearchParseException e) {
697+
e.addSuppressed(e);
698+
throw e;
699+
}
700+
return staleThreshold;
701+
}
661702
}

0 commit comments

Comments
 (0)