Skip to content

Commit 5340268

Browse files
Fix Flaky test IndicesRequestCacheIT.testStaleKeysCleanupWithMultipleIndices (#13453) (#13459)
(cherry picked from commit 51f8872) Signed-off-by: Kiran Prakash <awskiran@amazon.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 400a8a2 commit 5340268

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

server/src/internalClusterTest/java/org/opensearch/indices/IndicesRequestCacheIT.java

+30-18
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,10 @@ public void testCacheClearanceAfterIndexClosure() throws Exception {
11181118
String index = "index";
11191119
setupIndex(client, index);
11201120

1121+
// assert there are no entries in the cache for index
1122+
assertEquals(0, getRequestCacheStats(client, index).getMemorySizeInBytes());
1123+
// assert there are no entries in the cache from other indices in the node
1124+
assertEquals(0, getNodeCacheStats(client).getMemorySizeInBytes());
11211125
// create first cache entry in index
11221126
createCacheEntry(client, index, "hello");
11231127
assertCacheState(client, index, 0, 1);
@@ -1135,7 +1139,7 @@ public void testCacheClearanceAfterIndexClosure() throws Exception {
11351139
// sleep until cache cleaner would have cleaned up the stale key from index
11361140
assertBusy(() -> {
11371141
// cache cleaner should have cleaned up the stale keys from index
1138-
assertFalse(getNodeCacheStats(client).getMemorySizeInBytes() > 0);
1142+
assertEquals(0, getNodeCacheStats(client).getMemorySizeInBytes());
11391143
}, cacheCleanIntervalInMillis * 2, TimeUnit.MILLISECONDS);
11401144
}
11411145

@@ -1154,6 +1158,10 @@ public void testCacheCleanupAfterIndexDeletion() throws Exception {
11541158
String index = "index";
11551159
setupIndex(client, index);
11561160

1161+
// assert there are no entries in the cache for index
1162+
assertEquals(0, getRequestCacheStats(client, index).getMemorySizeInBytes());
1163+
// assert there are no entries in the cache from other indices in the node
1164+
assertEquals(0, getNodeCacheStats(client).getMemorySizeInBytes());
11571165
// create first cache entry in index
11581166
createCacheEntry(client, index, "hello");
11591167
assertCacheState(client, index, 0, 1);
@@ -1172,13 +1180,13 @@ public void testCacheCleanupAfterIndexDeletion() throws Exception {
11721180
// sleep until cache cleaner would have cleaned up the stale key from index
11731181
assertBusy(() -> {
11741182
// cache cleaner should have cleaned up the stale keys from index
1175-
assertFalse(getNodeCacheStats(client).getMemorySizeInBytes() > 0);
1183+
assertEquals(0, getNodeCacheStats(client).getMemorySizeInBytes());
11761184
}, cacheCleanIntervalInMillis * 2, TimeUnit.MILLISECONDS);
11771185
}
11781186

11791187
// when staleness threshold is lower than staleness, it should clean the cache from all indices having stale keys
11801188
public void testStaleKeysCleanupWithMultipleIndices() throws Exception {
1181-
int cacheCleanIntervalInMillis = 300;
1189+
int cacheCleanIntervalInMillis = 10;
11821190
String node = internalCluster().startNode(
11831191
Settings.builder()
11841192
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_CLEANUP_STALENESS_THRESHOLD_SETTING_KEY, 0.10)
@@ -1193,37 +1201,41 @@ public void testStaleKeysCleanupWithMultipleIndices() throws Exception {
11931201
setupIndex(client, index1);
11941202
setupIndex(client, index2);
11951203

1204+
// assert cache is empty for index1
1205+
assertEquals(0, getRequestCacheStats(client, index1).getMemorySizeInBytes());
11961206
// create first cache entry in index1
11971207
createCacheEntry(client, index1, "hello");
11981208
assertCacheState(client, index1, 0, 1);
1199-
long memorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
1200-
assertTrue(memorySizeForIndex1 > 0);
1209+
long memorySizeForIndex1With1Entries = getRequestCacheStats(client, index1).getMemorySizeInBytes();
1210+
assertTrue(memorySizeForIndex1With1Entries > 0);
12011211

12021212
// create second cache entry in index1
12031213
createCacheEntry(client, index1, "there");
12041214
assertCacheState(client, index1, 0, 2);
1205-
long finalMemorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
1206-
assertTrue(finalMemorySizeForIndex1 > memorySizeForIndex1);
1215+
long memorySizeForIndex1With2Entries = getRequestCacheStats(client, index1).getMemorySizeInBytes();
1216+
assertTrue(memorySizeForIndex1With2Entries > memorySizeForIndex1With1Entries);
12071217

1218+
// assert cache is empty for index2
1219+
assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes());
12081220
// create first cache entry in index2
12091221
createCacheEntry(client, index2, "hello");
12101222
assertCacheState(client, index2, 0, 1);
12111223
assertTrue(getRequestCacheStats(client, index2).getMemorySizeInBytes() > 0);
12121224

1213-
// force refresh index 1 so that it creates 2 stale keys
1214-
flushAndRefresh(index1);
1215-
// create another cache entry in index 1, this should not be cleaned up.
1225+
// force refresh both index1 and index2
1226+
flushAndRefresh(index1, index2);
1227+
// create another cache entry in index 1 same as memorySizeForIndex1With1Entries, this should not be cleaned up.
12161228
createCacheEntry(client, index1, "hello");
1217-
// record the size of this entry
1218-
long memorySizeOfLatestEntryForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes() - finalMemorySizeForIndex1;
1219-
// force refresh index 2 so that it creates 1 stale key
1220-
flushAndRefresh(index2);
1221-
// sleep until cache cleaner would have cleaned up the stale key from index 2
1229+
// sleep until cache cleaner would have cleaned up the stale key from index2
12221230
assertBusy(() -> {
1223-
// cache cleaner should have cleaned up the stale key from index 2
1231+
// cache cleaner should have cleaned up the stale key from index2 and hence cache should be empty
12241232
assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes());
1225-
// cache cleaner should have only cleaned up the stale entities
1226-
assertEquals(memorySizeOfLatestEntryForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes());
1233+
// cache cleaner should have only cleaned up the stale entities for index1
1234+
long currentMemorySizeInBytesForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
1235+
// assert the memory size of index1 to only contain 1 entry added after flushAndRefresh
1236+
assertEquals(memorySizeForIndex1With1Entries, currentMemorySizeInBytesForIndex1);
1237+
// cache for index1 should not be empty since there was an item cached after flushAndRefresh
1238+
assertTrue(currentMemorySizeInBytesForIndex1 > 0);
12271239
}, cacheCleanIntervalInMillis * 2, TimeUnit.MILLISECONDS);
12281240
}
12291241

0 commit comments

Comments
 (0)