Skip to content

Commit 5bb3f2a

Browse files
kiranprakash154deshsidd
authored andcommitted
Fix Flaky test IndicesRequestCacheIT.testStaleKeysCleanupWithMultipleIndices (opensearch-project#13453)
* Update IndicesRequestCacheIT.java Signed-off-by: Kiran Prakash <awskiran@amazon.com> * Update IndicesRequestCacheIT.java Signed-off-by: Kiran Prakash <awskiran@amazon.com> --------- Signed-off-by: Kiran Prakash <awskiran@amazon.com>
1 parent fbe7c12 commit 5bb3f2a

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
@@ -1119,6 +1119,10 @@ public void testCacheClearanceAfterIndexClosure() throws Exception {
11191119
String index = "index";
11201120
setupIndex(client, index);
11211121

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

@@ -1155,6 +1159,10 @@ public void testCacheCleanupAfterIndexDeletion() throws Exception {
11551159
String index = "index";
11561160
setupIndex(client, index);
11571161

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

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

1205+
// assert cache is empty for index1
1206+
assertEquals(0, getRequestCacheStats(client, index1).getMemorySizeInBytes());
11971207
// create first cache entry in index1
11981208
createCacheEntry(client, index1, "hello");
11991209
assertCacheState(client, index1, 0, 1);
1200-
long memorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
1201-
assertTrue(memorySizeForIndex1 > 0);
1210+
long memorySizeForIndex1With1Entries = getRequestCacheStats(client, index1).getMemorySizeInBytes();
1211+
assertTrue(memorySizeForIndex1With1Entries > 0);
12021212

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

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

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

0 commit comments

Comments
 (0)