38
38
import org .apache .lucene .index .IndexReader ;
39
39
import org .apache .lucene .util .Accountable ;
40
40
import org .apache .lucene .util .RamUsageEstimator ;
41
+ import org .opensearch .OpenSearchParseException ;
41
42
import org .opensearch .common .CheckedSupplier ;
42
43
import org .opensearch .common .cache .CacheType ;
43
44
import org .opensearch .common .cache .ICache ;
50
51
import org .opensearch .common .settings .Setting ;
51
52
import org .opensearch .common .settings .Setting .Property ;
52
53
import org .opensearch .common .settings .Settings ;
54
+ import org .opensearch .common .unit .RatioValue ;
53
55
import org .opensearch .common .unit .TimeValue ;
54
56
import org .opensearch .common .util .concurrent .ConcurrentCollections ;
55
57
import org .opensearch .core .common .bytes .BytesReference ;
@@ -117,6 +119,17 @@ public final class IndicesRequestCache implements RemovalListener<IndicesRequest
117
119
new TimeValue (0 ),
118
120
Property .NodeScope
119
121
);
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
+ );
120
133
121
134
private final static long BASE_RAM_BYTES_USED = RamUsageEstimator .shallowSizeOfInstance (Key .class );
122
135
@@ -131,6 +144,7 @@ public final class IndicesRequestCache implements RemovalListener<IndicesRequest
131
144
this .expire = INDICES_CACHE_QUERY_EXPIRE .exists (settings ) ? INDICES_CACHE_QUERY_EXPIRE .get (settings ) : null ;
132
145
long sizeInBytes = size .getBytes ();
133
146
ToLongBiFunction <Key , BytesReference > weigher = (k , v ) -> k .ramBytesUsed () + v .ramBytesUsed ();
147
+ this .cacheCleanupManager = new IndicesRequestCacheCleanupManager (getStalenessThreshold (settings ));
134
148
this .cacheEntityLookup = cacheEntityFunction ;
135
149
this .cache = cacheService .createCache (
136
150
new CacheConfig .Builder <Key , BytesReference >().setSettings (settings )
@@ -150,6 +164,11 @@ public void close() {
150
164
cache .invalidateAll ();
151
165
}
152
166
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
+
153
172
void clear (CacheEntity entity ) {
154
173
cacheCleanupManager .enqueueCleanupKey (new CleanupKey (entity , null ));
155
174
cacheCleanupManager .forceCleanCache ();
@@ -658,4 +677,26 @@ long count() {
658
677
int numRegisteredCloseListeners () { // for testing
659
678
return registeredClosedListeners .size ();
660
679
}
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
+ }
661
702
}
0 commit comments