diff --git a/server/src/internalClusterTest/java/org/opensearch/indices/IndicesRequestCacheIT.java b/server/src/internalClusterTest/java/org/opensearch/indices/IndicesRequestCacheIT.java index ca09eb90343f4..2d80144fe432b 100644 --- a/server/src/internalClusterTest/java/org/opensearch/indices/IndicesRequestCacheIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/indices/IndicesRequestCacheIT.java @@ -62,6 +62,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; +import java.util.concurrent.TimeUnit; import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS; import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS; @@ -731,11 +732,13 @@ public void testStaleKeysCleanup_LowStaleThresholdShouldCleanUpStaleKeysFromCach // force refresh so that it creates 1 stale key flushAndRefresh(index2); // sleep until cache cleaner would have cleaned up the stale key from index 2 - Thread.sleep(1_000); - // cache cleaner should have cleaned up the stale key from index 2 - assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes()); - // cache cleaner should NOT have cleaned from index 1 - assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes()); + assertBusy(() -> { + // cache cleaner should have cleaned up the stale key from index 2 + assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes()); + // cache cleaner should NOT have cleaned from index 1 + assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes()); + }, 1, TimeUnit.SECONDS); + // sleep until cache cleaner would have cleaned up the stale key from index 2 } // when staleness threshold is equal to staleness, it should clean the stale keys from cache @@ -771,11 +774,12 @@ public void testStaleKeysCleanup_EqualThresholdAndStalenessShouldCleanUpStaleKey // force refresh so that it creates 1 stale key flushAndRefresh(index2); // sleep until cache cleaner would have cleaned up the stale key from index 2 - Thread.sleep(1_000); - // cache cleaner should have cleaned up the stale key from index 2 - assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes()); - // cache cleaner should NOT have cleaned from index 1 - assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes()); + assertBusy(() -> { + // cache cleaner should have cleaned up the stale key from index 2 + assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes()); + // cache cleaner should NOT have cleaned from index 1 + assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes()); + }, 1, TimeUnit.SECONDS); } // when staleness threshold is higher than staleness, it should NOT clean the cache @@ -811,11 +815,12 @@ public void testStaleKeysCleanup_HighStaleThresholdShouldSkipCleanUp() throws Ex // force refresh so that it creates 1 stale key flushAndRefresh(index2); // sleep until cache cleaner would have cleaned up the stale key from index 2 - Thread.sleep(1_000); - // cache cleaner should NOT have cleaned up the stale key from index 2 - assertTrue(getRequestCacheStats(client, index2).getMemorySizeInBytes() > 0); - // cache cleaner should NOT have cleaned from index 1 - assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes()); + assertBusy(() -> { + // cache cleaner should NOT have cleaned up the stale key from index 2 + assertTrue(getRequestCacheStats(client, index2).getMemorySizeInBytes() > 0); + // cache cleaner should NOT have cleaned from index 1 + assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes()); + }, 1, TimeUnit.SECONDS); } // when staleness threshold is explicitly set to 0, cache cleaner regularly cleans up stale keys. @@ -850,11 +855,12 @@ public void testStaleKeysCleanup_ZeroStaleThresholdShouldCleanUpStaleKeysFromCac // force refresh so that it creates 1 stale key flushAndRefresh(index2); // sleep until cache cleaner would have cleaned up the stale key from index 2 - Thread.sleep(1_000); - // cache cleaner should have cleaned up the stale key from index 2 - assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes()); - // cache cleaner should NOT have cleaned from index 1 - assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes()); + assertBusy(() -> { + // cache cleaner should have cleaned up the stale key from index 2 + assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes()); + // cache cleaner should NOT have cleaned from index 1 + assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes()); + }, 1, TimeUnit.SECONDS); } // when staleness threshold is not explicitly set, cache cleaner regularly cleans up stale keys @@ -888,11 +894,12 @@ public void testStaleKeysCleanup_NoStaleThresholdShouldCleanUpStaleKeysFromCache // force refresh so that it creates 1 stale key flushAndRefresh(index2); // sleep until cache cleaner would have cleaned up the stale key from index 2 - Thread.sleep(1_000); - // cache cleaner should have cleaned up the stale key from index 2 - assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes()); - // cache cleaner should NOT have cleaned from index 1 - assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes()); + assertBusy(() -> { + // cache cleaner should have cleaned up the stale key from index 2 + assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes()); + // cache cleaner should NOT have cleaned from index 1 + assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes()); + }, 1, TimeUnit.SECONDS); } // when cache cleaner interval setting is not set, cache cleaner is configured appropriately with the fall-back setting @@ -926,11 +933,12 @@ public void testStaleKeysCleanup_NoIntervalSettingFallsBackAppropriately() throw // force refresh so that it creates 1 stale key flushAndRefresh(index2); // sleep until cache cleaner would have cleaned up the stale key from index 2 - Thread.sleep(1_000); - // cache cleaner should have cleaned up the stale key from index 2 - assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes()); - // cache cleaner should NOT have cleaned from index 1 - assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes()); + assertBusy(() -> { + // cache cleaner should have cleaned up the stale key from index 2 + assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes()); + // cache cleaner should NOT have cleaned from index 1 + assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes()); + }, 1, TimeUnit.SECONDS); } // staleness threshold dynamic updates should take effect in cleaning @@ -1022,11 +1030,12 @@ public void testStaleKeysCleanup_CleanUpStaleKeysDeletesAppropriatelyAcrossMulti // force refresh index 2 so that it creates 1 stale key flushAndRefresh(index2); // sleep until cache cleaner would have cleaned up the stale key from index 2 - Thread.sleep(1_000); - // cache cleaner should have cleaned up the stale key from index 2 - assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes()); - // cache cleaner should have only cleaned up the stale entities - assertEquals(memorySizeOfLatestEntryForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes()); + assertBusy(() -> { + // cache cleaner should have cleaned up the stale key from index 2 + assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes()); + // cache cleaner should have only cleaned up the stale entities + assertEquals(memorySizeOfLatestEntryForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes()); + }, 1, TimeUnit.SECONDS); } private void setupIndex(Client client, String index) throws Exception {