66
66
@ OpenSearchIntegTestCase .ClusterScope (scope = OpenSearchIntegTestCase .Scope .TEST , numDataNodes = 0 , supportsDedicatedMasters = false )
67
67
public class IndicesRequestCacheCleanupIT extends OpenSearchIntegTestCase {
68
68
69
+ private static final long MAX_ITERATIONS = 5 ;
70
+
69
71
@ Override
70
72
protected Collection <Class <? extends Plugin >> nodePlugins () {
71
73
return Arrays .asList (InternalSettingsPlugin .class );
@@ -74,23 +76,7 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
74
76
public void testCacheWithInvalidation () throws Exception {
75
77
Client client = client ();
76
78
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 );
94
80
ensureSearchable (index );
95
81
// Force merge the index to ensure there can be no background merges during the subsequent searches that would invalidate the cache
96
82
forceMerge (client , index );
@@ -125,8 +111,8 @@ public void testCacheClearAPIRemovesStaleKeysWhenStalenessThresholdIsLow() throw
125
111
.put (IndicesRequestCache .INDICES_REQUEST_CACHE_CLEANUP_STALENESS_THRESHOLD_SETTING_KEY , 0.10 )
126
112
.put (
127
113
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 )
130
116
)
131
117
);
132
118
Client client = client (node );
@@ -210,7 +196,7 @@ public void testStaleKeysCleanupWithLowThreshold() throws Exception {
210
196
assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
211
197
// cache cleaner should NOT have cleaned from index 1
212
198
assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
213
- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
199
+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
214
200
// sleep until cache cleaner would have cleaned up the stale key from index 2
215
201
}
216
202
@@ -260,7 +246,7 @@ public void testCacheCleanupOnEqualStalenessAndThreshold() throws Exception {
260
246
assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
261
247
// cache cleaner should NOT have cleaned from index 1
262
248
assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
263
- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
249
+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
264
250
}
265
251
266
252
// when staleness threshold is higher than staleness, it should NOT clean the cache
@@ -308,7 +294,7 @@ public void testCacheCleanupSkipsWithHighStalenessThreshold() throws Exception {
308
294
assertTrue (getRequestCacheStats (client , index2 ).getMemorySizeInBytes () > 0 );
309
295
// cache cleaner should NOT have cleaned from index 1
310
296
assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
311
- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
297
+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
312
298
}
313
299
314
300
// when staleness threshold is explicitly set to 0, cache cleaner regularly cleans up stale keys.
@@ -356,7 +342,7 @@ public void testCacheCleanupOnZeroStalenessThreshold() throws Exception {
356
342
assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
357
343
// cache cleaner should NOT have cleaned from index 1
358
344
assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
359
- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
345
+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
360
346
}
361
347
362
348
// when staleness threshold is not explicitly set, cache cleaner regularly cleans up stale keys
@@ -403,7 +389,7 @@ public void testStaleKeysRemovalWithoutExplicitThreshold() throws Exception {
403
389
assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
404
390
// cache cleaner should NOT have cleaned from index 1
405
391
assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
406
- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
392
+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
407
393
}
408
394
409
395
// 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 {
447
433
assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
448
434
// cache cleaner should NOT have cleaned from index 1
449
435
assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
450
- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
436
+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
451
437
}
452
438
453
439
// staleness threshold updates flows through to the cache cleaner
@@ -490,7 +476,7 @@ public void testDynamicStalenessThresholdUpdate() throws Exception {
490
476
assertBusy (() -> {
491
477
// cache cleaner should NOT have cleaned up the stale key from index 2
492
478
assertTrue (getRequestCacheStats (client , index2 ).getMemorySizeInBytes () > 0 );
493
- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
479
+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
494
480
495
481
// Update indices.requests.cache.cleanup.staleness_threshold to "10%"
496
482
ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest ();
@@ -505,7 +491,7 @@ public void testDynamicStalenessThresholdUpdate() throws Exception {
505
491
assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
506
492
// cache cleaner should NOT have cleaned from index 1
507
493
assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
508
- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
494
+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
509
495
}
510
496
511
497
// staleness threshold dynamic updates should throw exceptions on invalid input
@@ -557,7 +543,7 @@ public void testCacheClearanceAfterIndexClosure() throws Exception {
557
543
assertBusy (() -> {
558
544
// cache cleaner should have cleaned up the stale keys from index
559
545
assertEquals (0 , getNodeCacheStats (client ).getMemorySizeInBytes ());
560
- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
546
+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
561
547
}
562
548
563
549
// deleting the Index after caching will clean up from Indices Request Cache
@@ -598,7 +584,7 @@ public void testCacheCleanupAfterIndexDeletion() throws Exception {
598
584
assertBusy (() -> {
599
585
// cache cleaner should have cleaned up the stale keys from index
600
586
assertEquals (0 , getNodeCacheStats (client ).getMemorySizeInBytes ());
601
- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
587
+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
602
588
}
603
589
604
590
// 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 {
645
631
// Assert cache is cleared up
646
632
assertBusy (
647
633
() -> { assertEquals (0 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ()); },
648
- cacheCleanIntervalInMillis * 2 ,
634
+ cacheCleanIntervalInMillis * MAX_ITERATIONS ,
649
635
TimeUnit .MILLISECONDS
650
636
);
651
637
@@ -667,7 +653,7 @@ public void testStaleKeysCleanupWithMultipleIndices() throws Exception {
667
653
long currentMemorySizeInBytesForIndex1 = getRequestCacheStats (client , index1 ).getMemorySizeInBytes ();
668
654
// assert the memory size of index1 to only contain 1 entry added after flushAndRefresh
669
655
assertEquals (memorySizeForIndex1With1Entries , currentMemorySizeInBytesForIndex1 );
670
- }, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
656
+ }, cacheCleanIntervalInMillis * MAX_ITERATIONS , TimeUnit .MILLISECONDS );
671
657
}
672
658
673
659
private void setupIndex (Client client , String index ) throws Exception {
0 commit comments