@@ -1119,6 +1119,10 @@ public void testCacheClearanceAfterIndexClosure() throws Exception {
1119
1119
String index = "index" ;
1120
1120
setupIndex (client , index );
1121
1121
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 ());
1122
1126
// create first cache entry in index
1123
1127
createCacheEntry (client , index , "hello" );
1124
1128
assertCacheState (client , index , 0 , 1 );
@@ -1136,7 +1140,7 @@ public void testCacheClearanceAfterIndexClosure() throws Exception {
1136
1140
// sleep until cache cleaner would have cleaned up the stale key from index
1137
1141
assertBusy (() -> {
1138
1142
// cache cleaner should have cleaned up the stale keys from index
1139
- assertFalse ( getNodeCacheStats (client ).getMemorySizeInBytes () > 0 );
1143
+ assertEquals ( 0 , getNodeCacheStats (client ).getMemorySizeInBytes ());
1140
1144
}, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
1141
1145
}
1142
1146
@@ -1155,6 +1159,10 @@ public void testCacheCleanupAfterIndexDeletion() throws Exception {
1155
1159
String index = "index" ;
1156
1160
setupIndex (client , index );
1157
1161
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 ());
1158
1166
// create first cache entry in index
1159
1167
createCacheEntry (client , index , "hello" );
1160
1168
assertCacheState (client , index , 0 , 1 );
@@ -1173,13 +1181,13 @@ public void testCacheCleanupAfterIndexDeletion() throws Exception {
1173
1181
// sleep until cache cleaner would have cleaned up the stale key from index
1174
1182
assertBusy (() -> {
1175
1183
// cache cleaner should have cleaned up the stale keys from index
1176
- assertFalse ( getNodeCacheStats (client ).getMemorySizeInBytes () > 0 );
1184
+ assertEquals ( 0 , getNodeCacheStats (client ).getMemorySizeInBytes ());
1177
1185
}, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
1178
1186
}
1179
1187
1180
1188
// when staleness threshold is lower than staleness, it should clean the cache from all indices having stale keys
1181
1189
public void testStaleKeysCleanupWithMultipleIndices () throws Exception {
1182
- int cacheCleanIntervalInMillis = 300 ;
1190
+ int cacheCleanIntervalInMillis = 10 ;
1183
1191
String node = internalCluster ().startNode (
1184
1192
Settings .builder ()
1185
1193
.put (IndicesRequestCache .INDICES_REQUEST_CACHE_CLEANUP_STALENESS_THRESHOLD_SETTING_KEY , 0.10 )
@@ -1194,37 +1202,41 @@ public void testStaleKeysCleanupWithMultipleIndices() throws Exception {
1194
1202
setupIndex (client , index1 );
1195
1203
setupIndex (client , index2 );
1196
1204
1205
+ // assert cache is empty for index1
1206
+ assertEquals (0 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
1197
1207
// create first cache entry in index1
1198
1208
createCacheEntry (client , index1 , "hello" );
1199
1209
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 );
1202
1212
1203
1213
// create second cache entry in index1
1204
1214
createCacheEntry (client , index1 , "there" );
1205
1215
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 );
1208
1218
1219
+ // assert cache is empty for index2
1220
+ assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
1209
1221
// create first cache entry in index2
1210
1222
createCacheEntry (client , index2 , "hello" );
1211
1223
assertCacheState (client , index2 , 0 , 1 );
1212
1224
assertTrue (getRequestCacheStats (client , index2 ).getMemorySizeInBytes () > 0 );
1213
1225
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.
1217
1229
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
1223
1231
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
1225
1233
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 );
1228
1240
}, cacheCleanIntervalInMillis * 2 , TimeUnit .MILLISECONDS );
1229
1241
}
1230
1242
0 commit comments