Skip to content

Commit

Permalink
Update IndicesRequestCacheIT.java
Browse files Browse the repository at this point in the history
Signed-off-by: Kiran Prakash <[email protected]>
  • Loading branch information
kiranprakash154 committed Apr 2, 2024
1 parent 88b4ae1 commit 93f67e4
Showing 1 changed file with 44 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 93f67e4

Please sign in to comment.