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