6666@ OpenSearchIntegTestCase .ClusterScope (scope = OpenSearchIntegTestCase .Scope .TEST , numDataNodes = 0 , supportsDedicatedMasters = false )
6767public class IndicesRequestCacheCleanupIT extends OpenSearchIntegTestCase {
6868
69+ private static final long MAX_ITERATIONS = 5 ;
70+
6971 @ Override
7072 protected Collection <Class <? extends Plugin >> nodePlugins () {
7173 return Arrays .asList (InternalSettingsPlugin .class );
@@ -74,23 +76,7 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
7476 public void testCacheWithInvalidation () throws Exception {
7577 Client client = client ();
7678 String index = "index" ;
77- assertAcked (
78- client .admin ()
79- .indices ()
80- .prepareCreate (index )
81- .setMapping ("k" , "type=keyword" )
82- .setSettings (
83- Settings .builder ()
84- .put (IndicesRequestCache .INDEX_CACHE_REQUEST_ENABLED_SETTING .getKey (), true )
85- .put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 )
86- .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 0 )
87- .put ("index.refresh_interval" , -1 )
88- // Disable index refreshing to avoid cache being invalidated mid-test
89- .put (IndexSettings .INDEX_REFRESH_INTERVAL_SETTING .getKey (), TimeValue .timeValueMillis (-1 ))
90- )
91- .get ()
92- );
93- indexRandom (false , client .prepareIndex (index ).setSource ("k" , "hello" ));
79+ setupIndex (client , index );
9480 ensureSearchable (index );
9581 // Force merge the index to ensure there can be no background merges during the subsequent searches that would invalidate the cache
9682 forceMerge (client , index );
@@ -125,8 +111,8 @@ public void testCacheClearAPIRemovesStaleKeysWhenStalenessThresholdIsLow() throw
125111 .put (IndicesRequestCache .INDICES_REQUEST_CACHE_CLEANUP_STALENESS_THRESHOLD_SETTING_KEY , 0.10 )
126112 .put (
127113 IndicesRequestCache .INDICES_REQUEST_CACHE_CLEANUP_INTERVAL_SETTING_KEY ,
128- // setting intentionally high to avoid cache cleaner interfering
129- TimeValue .timeValueMillis ( 300 )
114+ // Set interval much larger than test timeout to effectively disable it
115+ TimeValue .timeValueDays ( 1 )
130116 )
131117 );
132118 Client client = client (node );
@@ -210,7 +196,7 @@ public void testStaleKeysCleanupWithLowThreshold() throws Exception {
210196 assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
211197 // cache cleaner should NOT have cleaned from index 1
212198 assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
213- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
199+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
214200 // sleep until cache cleaner would have cleaned up the stale key from index 2
215201 }
216202
@@ -260,7 +246,7 @@ public void testCacheCleanupOnEqualStalenessAndThreshold() throws Exception {
260246 assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
261247 // cache cleaner should NOT have cleaned from index 1
262248 assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
263- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
249+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
264250 }
265251
266252 // when staleness threshold is higher than staleness, it should NOT clean the cache
@@ -308,7 +294,7 @@ public void testCacheCleanupSkipsWithHighStalenessThreshold() throws Exception {
308294 assertTrue (getRequestCacheStats (client , index2 ).getMemorySizeInBytes () > 0 );
309295 // cache cleaner should NOT have cleaned from index 1
310296 assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
311- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
297+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
312298 }
313299
314300 // when staleness threshold is explicitly set to 0, cache cleaner regularly cleans up stale keys.
@@ -356,7 +342,7 @@ public void testCacheCleanupOnZeroStalenessThreshold() throws Exception {
356342 assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
357343 // cache cleaner should NOT have cleaned from index 1
358344 assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
359- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
345+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
360346 }
361347
362348 // when staleness threshold is not explicitly set, cache cleaner regularly cleans up stale keys
@@ -403,7 +389,7 @@ public void testStaleKeysRemovalWithoutExplicitThreshold() throws Exception {
403389 assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
404390 // cache cleaner should NOT have cleaned from index 1
405391 assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
406- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
392+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
407393 }
408394
409395 // when cache cleaner interval setting is not set, cache cleaner is configured appropriately with the fall-back setting
@@ -447,7 +433,7 @@ public void testCacheCleanupWithDefaultSettings() throws Exception {
447433 assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
448434 // cache cleaner should NOT have cleaned from index 1
449435 assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
450- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
436+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
451437 }
452438
453439 // staleness threshold updates flows through to the cache cleaner
@@ -490,7 +476,7 @@ public void testDynamicStalenessThresholdUpdate() throws Exception {
490476 assertBusy (() -> {
491477 // cache cleaner should NOT have cleaned up the stale key from index 2
492478 assertTrue (getRequestCacheStats (client , index2 ).getMemorySizeInBytes () > 0 );
493- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
479+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
494480
495481 // Update indices.requests.cache.cleanup.staleness_threshold to "10%"
496482 ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest ();
@@ -505,7 +491,7 @@ public void testDynamicStalenessThresholdUpdate() throws Exception {
505491 assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
506492 // cache cleaner should NOT have cleaned from index 1
507493 assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
508- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
494+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
509495 }
510496
511497 // staleness threshold dynamic updates should throw exceptions on invalid input
@@ -557,7 +543,7 @@ public void testCacheClearanceAfterIndexClosure() throws Exception {
557543 assertBusy (() -> {
558544 // cache cleaner should have cleaned up the stale keys from index
559545 assertEquals (0 , getNodeCacheStats (client ).getMemorySizeInBytes ());
560- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
546+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
561547 }
562548
563549 // deleting the Index after caching will clean up from Indices Request Cache
@@ -598,7 +584,7 @@ public void testCacheCleanupAfterIndexDeletion() throws Exception {
598584 assertBusy (() -> {
599585 // cache cleaner should have cleaned up the stale keys from index
600586 assertEquals (0 , getNodeCacheStats (client ).getMemorySizeInBytes ());
601- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
587+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
602588 }
603589
604590 // when staleness threshold is lower than staleness, it should clean the cache from all indices having stale keys
@@ -645,7 +631,7 @@ public void testStaleKeysCleanupWithMultipleIndices() throws Exception {
645631 // Assert cache is cleared up
646632 assertBusy (
647633 () -> { assertEquals (0 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ()); },
648- cacheCleanIntervalInMillis * 2 ,
634+ cacheCleanIntervalInMillis * MAX_ITERATIONS ,
649635 TimeUnit .MILLISECONDS
650636 );
651637
@@ -667,7 +653,7 @@ public void testStaleKeysCleanupWithMultipleIndices() throws Exception {
667653 long currentMemorySizeInBytesForIndex1 = getRequestCacheStats (client , index1 ).getMemorySizeInBytes ();
668654 // assert the memory size of index1 to only contain 1 entry added after flushAndRefresh
669655 assertEquals (memorySizeForIndex1With1Entries , currentMemorySizeInBytesForIndex1 );
670- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
656+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
671657 }
672658
673659 private void setupIndex (Client client , String index ) throws Exception {
0 commit comments