forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndicesRequestCacheTests.java
1584 lines (1404 loc) · 75.7 KB
/
IndicesRequestCacheTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.indices;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.opensearch.Version;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.cluster.routing.RecoverySource;
import org.opensearch.cluster.routing.ShardRouting;
import org.opensearch.cluster.routing.ShardRoutingHelper;
import org.opensearch.cluster.routing.UnassignedInfo;
import org.opensearch.common.CheckedSupplier;
import org.opensearch.common.cache.ICacheKey;
import org.opensearch.common.cache.RemovalNotification;
import org.opensearch.common.cache.RemovalReason;
import org.opensearch.common.cache.module.CacheModule;
import org.opensearch.common.cache.stats.ImmutableCacheStats;
import org.opensearch.common.cache.stats.ImmutableCacheStatsHolder;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.lucene.index.OpenSearchDirectoryReader;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.FeatureFlags;
import org.opensearch.common.util.io.IOUtils;
import org.opensearch.core.common.bytes.AbstractBytesReference;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.unit.ByteSizeValue;
import org.opensearch.core.index.Index;
import org.opensearch.core.index.shard.ShardId;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.XContentHelper;
import org.opensearch.index.IndexNotFoundException;
import org.opensearch.index.IndexService;
import org.opensearch.index.cache.request.RequestCacheStats;
import org.opensearch.index.cache.request.ShardRequestCache;
import org.opensearch.index.query.TermQueryBuilder;
import org.opensearch.index.seqno.RetentionLeaseSyncer;
import org.opensearch.index.shard.IndexShard;
import org.opensearch.index.shard.IndexShardState;
import org.opensearch.index.shard.IndexShardTestCase;
import org.opensearch.index.shard.ShardNotFoundException;
import org.opensearch.indices.replication.checkpoint.SegmentReplicationCheckpointPublisher;
import org.opensearch.node.Node;
import org.opensearch.test.ClusterServiceUtils;
import org.opensearch.test.OpenSearchSingleNodeTestCase;
import org.opensearch.threadpool.ThreadPool;
import org.junit.After;
import org.junit.Before;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static org.opensearch.indices.IndicesRequestCache.INDEX_DIMENSION_NAME;
import static org.opensearch.indices.IndicesRequestCache.INDICES_CACHE_QUERY_SIZE;
import static org.opensearch.indices.IndicesRequestCache.INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING;
import static org.opensearch.indices.IndicesRequestCache.SHARD_ID_DIMENSION_NAME;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class IndicesRequestCacheTests extends OpenSearchSingleNodeTestCase {
private ThreadPool threadPool;
private IndexWriter writer;
private Directory dir;
private IndicesRequestCache cache;
private IndexShard indexShard;
private ThreadPool getThreadPool() {
return new ThreadPool(Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "default tracer tests").build());
}
@Before
public void setup() throws IOException {
dir = newDirectory();
writer = new IndexWriter(dir, newIndexWriterConfig());
indexShard = createIndex("test").getShard(0);
}
@After
public void cleanup() throws IOException {
IOUtils.close(writer, dir, cache);
terminate(threadPool);
}
public void testBasicOperationsCache() throws Exception {
threadPool = getThreadPool();
cache = getIndicesRequestCache(Settings.EMPTY);
writer.addDocument(newDoc(0, "foo"));
DirectoryReader reader = getReader(writer, indexShard.shardId());
// initial cache
IndicesService.IndexShardCacheEntity entity = new IndicesService.IndexShardCacheEntity(indexShard);
Loader loader = new Loader(reader, 0);
BytesReference value = cache.getOrCompute(entity, loader, reader, getTermBytes());
assertEquals("foo", value.streamInput().readString());
ShardRequestCache requestCacheStats = indexShard.requestCache();
assertEquals(0, requestCacheStats.stats().getHitCount());
assertEquals(1, requestCacheStats.stats().getMissCount());
assertEquals(0, requestCacheStats.stats().getEvictions());
assertFalse(loader.loadedFromCache);
assertEquals(1, cache.count());
// cache hit
entity = new IndicesService.IndexShardCacheEntity(indexShard);
loader = new Loader(reader, 0);
value = cache.getOrCompute(entity, loader, reader, getTermBytes());
assertEquals("foo", value.streamInput().readString());
requestCacheStats = indexShard.requestCache();
assertEquals(1, requestCacheStats.stats().getHitCount());
assertEquals(1, requestCacheStats.stats().getMissCount());
assertEquals(0, requestCacheStats.stats().getEvictions());
assertTrue(loader.loadedFromCache);
assertEquals(1, cache.count());
assertTrue(requestCacheStats.stats().getMemorySize().bytesAsInt() > value.length());
assertEquals(1, cache.numRegisteredCloseListeners());
// Closing the cache doesn't modify an already returned CacheEntity
if (randomBoolean()) {
reader.close();
} else {
indexShard.close("test", true, true); // closed shard but reader is still open
cache.clear(entity);
}
cache.cacheCleanupManager.cleanCache();
assertEquals(1, requestCacheStats.stats().getHitCount());
assertEquals(1, requestCacheStats.stats().getMissCount());
assertEquals(0, requestCacheStats.stats().getEvictions());
assertTrue(loader.loadedFromCache);
assertEquals(0, cache.count());
assertEquals(0, requestCacheStats.stats().getMemorySize().bytesAsInt());
IOUtils.close(reader);
assertEquals(0, cache.numRegisteredCloseListeners());
}
public void testBasicOperationsCacheWithFeatureFlag() throws Exception {
threadPool = getThreadPool();
Settings settings = Settings.builder().put(super.featureFlagSettings()).put(FeatureFlags.PLUGGABLE_CACHE, "true").build();
cache = getIndicesRequestCache(settings);
writer.addDocument(newDoc(0, "foo"));
DirectoryReader reader = getReader(writer, indexShard.shardId());
// initial cache
IndicesService.IndexShardCacheEntity entity = new IndicesService.IndexShardCacheEntity(indexShard);
Loader loader = new Loader(reader, 0);
BytesReference value = cache.getOrCompute(entity, loader, reader, getTermBytes());
assertEquals("foo", value.streamInput().readString());
ShardRequestCache requestCacheStats = indexShard.requestCache();
assertEquals(0, requestCacheStats.stats().getHitCount());
assertEquals(1, requestCacheStats.stats().getMissCount());
assertEquals(0, requestCacheStats.stats().getEvictions());
assertFalse(loader.loadedFromCache);
assertEquals(1, cache.count());
// cache hit
entity = new IndicesService.IndexShardCacheEntity(indexShard);
loader = new Loader(reader, 0);
value = cache.getOrCompute(entity, loader, reader, getTermBytes());
assertEquals("foo", value.streamInput().readString());
requestCacheStats = indexShard.requestCache();
assertEquals(1, requestCacheStats.stats().getHitCount());
assertEquals(1, requestCacheStats.stats().getMissCount());
assertEquals(0, requestCacheStats.stats().getEvictions());
assertTrue(loader.loadedFromCache);
assertEquals(1, cache.count());
assertTrue(requestCacheStats.stats().getMemorySize().bytesAsInt() > value.length());
assertEquals(1, cache.numRegisteredCloseListeners());
// Closing the cache doesn't modify an already returned CacheEntity
if (randomBoolean()) {
reader.close();
} else {
indexShard.close("test", true, true); // closed shard but reader is still open
cache.clear(entity);
}
cache.cacheCleanupManager.cleanCache();
assertEquals(1, requestCacheStats.stats().getHitCount());
assertEquals(1, requestCacheStats.stats().getMissCount());
assertEquals(0, requestCacheStats.stats().getEvictions());
assertTrue(loader.loadedFromCache);
assertEquals(0, cache.count());
assertEquals(0, requestCacheStats.stats().getMemorySize().bytesAsInt());
IOUtils.close(reader);
assertEquals(0, cache.numRegisteredCloseListeners());
}
public void testCacheDifferentReaders() throws Exception {
threadPool = getThreadPool();
cache = getIndicesRequestCache(Settings.EMPTY);
writer.addDocument(newDoc(0, "foo"));
DirectoryReader reader = getReader(writer, indexShard.shardId());
if (randomBoolean()) {
writer.flush();
IOUtils.close(writer);
writer = new IndexWriter(dir, newIndexWriterConfig());
}
writer.updateDocument(new Term("id", "0"), newDoc(0, "bar"));
DirectoryReader secondReader = getReader(writer, indexShard.shardId());
// initial cache
IndicesService.IndexShardCacheEntity entity = new IndicesService.IndexShardCacheEntity(indexShard);
Loader loader = new Loader(reader, 0);
BytesReference value = cache.getOrCompute(entity, loader, reader, getTermBytes());
ShardRequestCache requestCacheStats = entity.stats();
assertEquals("foo", value.streamInput().readString());
assertEquals(0, requestCacheStats.stats().getHitCount());
assertEquals(1, requestCacheStats.stats().getMissCount());
assertEquals(0, requestCacheStats.stats().getEvictions());
assertFalse(loader.loadedFromCache);
assertEquals(1, cache.count());
assertTrue(requestCacheStats.stats().getMemorySize().bytesAsInt() > value.length());
final int cacheSize = requestCacheStats.stats().getMemorySize().bytesAsInt();
assertEquals(1, cache.numRegisteredCloseListeners());
// cache the second
IndicesService.IndexShardCacheEntity secondEntity = new IndicesService.IndexShardCacheEntity(indexShard);
loader = new Loader(secondReader, 0);
value = cache.getOrCompute(entity, loader, secondReader, getTermBytes());
requestCacheStats = entity.stats();
assertEquals("bar", value.streamInput().readString());
assertEquals(0, requestCacheStats.stats().getHitCount());
assertEquals(2, requestCacheStats.stats().getMissCount());
assertEquals(0, requestCacheStats.stats().getEvictions());
assertFalse(loader.loadedFromCache);
assertEquals(2, cache.count());
assertTrue(requestCacheStats.stats().getMemorySize().bytesAsInt() > cacheSize + value.length());
assertEquals(2, cache.numRegisteredCloseListeners());
secondEntity = new IndicesService.IndexShardCacheEntity(indexShard);
loader = new Loader(secondReader, 0);
value = cache.getOrCompute(secondEntity, loader, secondReader, getTermBytes());
requestCacheStats = entity.stats();
assertEquals("bar", value.streamInput().readString());
assertEquals(1, requestCacheStats.stats().getHitCount());
assertEquals(2, requestCacheStats.stats().getMissCount());
assertEquals(0, requestCacheStats.stats().getEvictions());
assertTrue(loader.loadedFromCache);
assertEquals(2, cache.count());
entity = new IndicesService.IndexShardCacheEntity(indexShard);
loader = new Loader(reader, 0);
value = cache.getOrCompute(entity, loader, reader, getTermBytes());
assertEquals("foo", value.streamInput().readString());
requestCacheStats = entity.stats();
assertEquals(2, requestCacheStats.stats().getHitCount());
assertEquals(2, requestCacheStats.stats().getMissCount());
assertEquals(0, requestCacheStats.stats().getEvictions());
assertTrue(loader.loadedFromCache);
assertEquals(2, cache.count());
// Closing the cache doesn't change returned entities
reader.close();
cache.cacheCleanupManager.cleanCache();
assertEquals(2, requestCacheStats.stats().getMissCount());
assertEquals(0, requestCacheStats.stats().getEvictions());
assertTrue(loader.loadedFromCache);
assertEquals(1, cache.count());
assertEquals(cacheSize, requestCacheStats.stats().getMemorySize().bytesAsInt());
assertEquals(1, cache.numRegisteredCloseListeners());
// release
if (randomBoolean()) {
secondReader.close();
} else {
indexShard.close("test", true, true); // closed shard but reader is still open
cache.clear(secondEntity);
}
cache.cacheCleanupManager.cleanCache();
assertEquals(2, requestCacheStats.stats().getMissCount());
assertEquals(0, requestCacheStats.stats().getEvictions());
assertTrue(loader.loadedFromCache);
assertEquals(0, cache.count());
assertEquals(0, requestCacheStats.stats().getMemorySize().bytesAsInt());
IOUtils.close(secondReader);
assertEquals(0, cache.numRegisteredCloseListeners());
}
public void testCacheCleanupThresholdSettingValidator_Valid_Percentage() {
String s = IndicesRequestCache.validateStalenessSetting("50%");
assertEquals("50%", s);
}
public void testCacheCleanupThresholdSettingValidator_Valid_Double() {
String s = IndicesRequestCache.validateStalenessSetting("0.5");
assertEquals("0.5", s);
}
public void testCacheCleanupThresholdSettingValidator_Valid_DecimalPercentage() {
String s = IndicesRequestCache.validateStalenessSetting("0.5%");
assertEquals("0.5%", s);
}
public void testCacheCleanupThresholdSettingValidator_InValid_MB() {
assertThrows(IllegalArgumentException.class, () -> { IndicesRequestCache.validateStalenessSetting("50mb"); });
}
public void testCacheCleanupThresholdSettingValidator_Invalid_Percentage() {
assertThrows(IllegalArgumentException.class, () -> { IndicesRequestCache.validateStalenessSetting("500%"); });
}
// when staleness threshold is zero, stale keys should be cleaned up every time cache cleaner is invoked.
public void testCacheCleanupBasedOnZeroThreshold() throws Exception {
threadPool = getThreadPool();
Settings settings = Settings.builder().put(INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING.getKey(), "0%").build();
cache = getIndicesRequestCache(settings);
writer.addDocument(newDoc(0, "foo"));
DirectoryReader reader = getReader(writer, indexShard.shardId());
DirectoryReader secondReader = getReader(writer, indexShard.shardId());
// Get 2 entries into the cache
cache.getOrCompute(getEntity(indexShard), getLoader(reader), reader, getTermBytes());
assertEquals(1, cache.count());
cache.getOrCompute(getEntity(indexShard), getLoader(secondReader), secondReader, getTermBytes());
assertEquals(2, cache.count());
// Close the reader, to be enqueued for cleanup
// 1 out of 2 keys ie 50% are now stale.
reader.close();
// cache count should not be affected
assertEquals(2, cache.count());
// clean cache with 0% staleness threshold
cache.cacheCleanupManager.cleanCache();
// cleanup should remove the stale-key
assertEquals(1, cache.count());
IOUtils.close(secondReader);
}
// when staleness count is higher than stale threshold, stale keys should be cleaned up.
public void testCacheCleanupBasedOnStaleThreshold_StalenessHigherThanThreshold() throws Exception {
threadPool = getThreadPool();
Settings settings = Settings.builder().put(INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING.getKey(), "0.49").build();
cache = getIndicesRequestCache(settings);
writer.addDocument(newDoc(0, "foo"));
DirectoryReader reader = getReader(writer, indexShard.shardId());
DirectoryReader secondReader = getReader(writer, indexShard.shardId());
// Get 2 entries into the cache
cache.getOrCompute(getEntity(indexShard), getLoader(reader), reader, getTermBytes());
assertEquals(1, cache.count());
cache.getOrCompute(getEntity(indexShard), getLoader(secondReader), secondReader, getTermBytes());
assertEquals(2, cache.count());
// no stale keys so far
assertEquals(0, cache.cacheCleanupManager.getStaleKeysCount().get());
// Close the reader, to be enqueued for cleanup
reader.close();
// 1 out of 2 keys ie 50% are now stale.
assertEquals(1, cache.cacheCleanupManager.getStaleKeysCount().get());
// cache count should not be affected
assertEquals(2, cache.count());
// clean cache with 49% staleness threshold
cache.cacheCleanupManager.cleanCache();
// cleanup should have taken effect with 49% threshold
assertEquals(1, cache.count());
assertEquals(0, cache.cacheCleanupManager.getStaleKeysCount().get());
IOUtils.close(secondReader);
}
// when staleness count equal to stale threshold, stale keys should be cleaned up.
public void testCacheCleanupBasedOnStaleThreshold_StalenessEqualToThreshold() throws Exception {
threadPool = getThreadPool();
Settings settings = Settings.builder().put(INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING.getKey(), "0.5").build();
cache = getIndicesRequestCache(settings);
writer.addDocument(newDoc(0, "foo"));
DirectoryReader reader = getReader(writer, indexShard.shardId());
DirectoryReader secondReader = getReader(writer, indexShard.shardId());
// Get 2 entries into the cache
cache.getOrCompute(getEntity(indexShard), getLoader(reader), reader, getTermBytes());
assertEquals(1, cache.count());
cache.getOrCompute(getEntity(indexShard), getLoader(secondReader), secondReader, getTermBytes());
assertEquals(2, cache.count());
// Close the reader, to be enqueued for cleanup
reader.close();
// 1 out of 2 keys ie 50% are now stale.
assertEquals(1, cache.cacheCleanupManager.getStaleKeysCount().get());
// cache count should not be affected
assertEquals(2, cache.count());
// clean cache with 50% staleness threshold
cache.cacheCleanupManager.cleanCache();
// cleanup should have taken effect
assertEquals(0, cache.cacheCleanupManager.getStaleKeysCount().get());
assertEquals(1, cache.count());
IOUtils.close(secondReader);
}
// when a cache entry that is Stale is evicted for any reason, we have to deduct the count from our staleness count
public void testStaleCount_OnRemovalNotificationOfStaleKey_DecrementsStaleCount() throws Exception {
threadPool = getThreadPool();
Settings settings = Settings.builder().put(INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING.getKey(), "0.51").build();
cache = getIndicesRequestCache(settings);
writer.addDocument(newDoc(0, "foo"));
ShardId shardId = indexShard.shardId();
DirectoryReader reader = getReader(writer, indexShard.shardId());
DirectoryReader secondReader = getReader(writer, indexShard.shardId());
// Get 2 entries into the cache from 2 different readers
cache.getOrCompute(getEntity(indexShard), getLoader(reader), reader, getTermBytes());
assertEquals(1, cache.count());
cache.getOrCompute(getEntity(indexShard), getLoader(secondReader), secondReader, getTermBytes());
assertEquals(2, cache.count());
// assert no stale keys are accounted so far
assertEquals(0, cache.cacheCleanupManager.getStaleKeysCount().get());
// Close the reader, this should create a stale key
reader.close();
// 1 out of 2 keys ie 50% are now stale.
assertEquals(1, cache.cacheCleanupManager.getStaleKeysCount().get());
// cache count should not be affected
assertEquals(2, cache.count());
IndicesRequestCache.Key key = new IndicesRequestCache.Key(
indexShard.shardId(),
getTermBytes(),
getReaderCacheKeyId(reader),
indexShard.hashCode()
);
// test the mapping
ConcurrentHashMap<ShardId, ConcurrentHashMap<String, Integer>> cleanupKeyToCountMap = cache.cacheCleanupManager
.getCleanupKeyToCountMap();
// shard id should exist
assertTrue(cleanupKeyToCountMap.containsKey(shardId));
// reader CacheKeyId should NOT exist
assertFalse(cleanupKeyToCountMap.get(shardId).containsKey(getReaderCacheKeyId(reader)));
// secondReader CacheKeyId should exist
assertTrue(cleanupKeyToCountMap.get(shardId).containsKey(getReaderCacheKeyId(secondReader)));
cache.onRemoval(
new RemovalNotification<ICacheKey<IndicesRequestCache.Key>, BytesReference>(
new ICacheKey<>(key),
getTermBytes(),
RemovalReason.EVICTED
)
);
// test the mapping, it should stay the same
// shard id should exist
assertTrue(cleanupKeyToCountMap.containsKey(shardId));
// reader CacheKeyId should NOT exist
assertFalse(cleanupKeyToCountMap.get(shardId).containsKey(getReaderCacheKeyId(reader)));
// secondReader CacheKeyId should exist
assertTrue(cleanupKeyToCountMap.get(shardId).containsKey(getReaderCacheKeyId(secondReader)));
// eviction of previous stale key from the cache should decrement staleKeysCount in iRC
assertEquals(0, cache.cacheCleanupManager.getStaleKeysCount().get());
IOUtils.close(secondReader);
}
// when a cache entry that is NOT Stale is evicted for any reason, staleness count should NOT be deducted
public void testStaleCount_OnRemovalNotificationOfNonStaleKey_DoesNotDecrementsStaleCount() throws Exception {
threadPool = getThreadPool();
Settings settings = Settings.builder().put(INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING.getKey(), "0.51").build();
cache = getIndicesRequestCache(settings);
writer.addDocument(newDoc(0, "foo"));
ShardId shardId = indexShard.shardId();
DirectoryReader reader = getReader(writer, indexShard.shardId());
DirectoryReader secondReader = getReader(writer, indexShard.shardId());
// Get 2 entries into the cache
cache.getOrCompute(getEntity(indexShard), getLoader(reader), reader, getTermBytes());
assertEquals(1, cache.count());
cache.getOrCompute(getEntity(indexShard), getLoader(secondReader), secondReader, getTermBytes());
assertEquals(2, cache.count());
// Close the reader, to be enqueued for cleanup
reader.close();
AtomicInteger staleKeysCount = cache.cacheCleanupManager.getStaleKeysCount();
// 1 out of 2 keys ie 50% are now stale.
assertEquals(1, staleKeysCount.get());
// cache count should not be affected
assertEquals(2, cache.count());
// evict entry from second reader (this reader is not closed)
IndicesRequestCache.Key key = new IndicesRequestCache.Key(
indexShard.shardId(),
getTermBytes(),
getReaderCacheKeyId(secondReader),
indexShard.hashCode()
);
// test the mapping
ConcurrentHashMap<ShardId, ConcurrentHashMap<String, Integer>> cleanupKeyToCountMap = cache.cacheCleanupManager
.getCleanupKeyToCountMap();
// shard id should exist
assertTrue(cleanupKeyToCountMap.containsKey(shardId));
// reader CacheKeyId should NOT exist
assertFalse(cleanupKeyToCountMap.get(shardId).containsKey(getReaderCacheKeyId(reader)));
// secondReader CacheKeyId should exist
assertTrue(cleanupKeyToCountMap.get(shardId).containsKey(getReaderCacheKeyId(secondReader)));
cache.onRemoval(
new RemovalNotification<ICacheKey<IndicesRequestCache.Key>, BytesReference>(
new ICacheKey<>(key),
getTermBytes(),
RemovalReason.EVICTED
)
);
// test the mapping, shardId entry should be cleaned up
// shard id should NOT exist
assertFalse(cleanupKeyToCountMap.containsKey(shardId));
staleKeysCount = cache.cacheCleanupManager.getStaleKeysCount();
// eviction of NON-stale key from the cache should NOT decrement staleKeysCount in iRC
assertEquals(1, staleKeysCount.get());
IOUtils.close(secondReader);
}
// when a cache entry that is NOT Stale is evicted WITHOUT its reader closing, we should NOT deduct it from staleness count
public void testStaleCount_WithoutReaderClosing_DecrementsStaleCount() throws Exception {
threadPool = getThreadPool();
Settings settings = Settings.builder().put(INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING.getKey(), "0.51").build();
cache = getIndicesRequestCache(settings);
writer.addDocument(newDoc(0, "foo"));
DirectoryReader reader = getReader(writer, indexShard.shardId());
DirectoryReader secondReader = getReader(writer, indexShard.shardId());
// Get 2 entries into the cache from 2 different readers
cache.getOrCompute(getEntity(indexShard), getLoader(reader), reader, getTermBytes());
assertEquals(1, cache.count());
cache.getOrCompute(getEntity(indexShard), getLoader(secondReader), secondReader, getTermBytes());
assertEquals(2, cache.count());
// no keys are stale
assertEquals(0, cache.cacheCleanupManager.getStaleKeysCount().get());
// create notification for removal of non-stale entry
IndicesRequestCache.Key key = new IndicesRequestCache.Key(
indexShard.shardId(),
getTermBytes(),
getReaderCacheKeyId(reader),
indexShard.hashCode()
);
cache.onRemoval(
new RemovalNotification<ICacheKey<IndicesRequestCache.Key>, BytesReference>(
new ICacheKey<>(key),
getTermBytes(),
RemovalReason.EVICTED
)
);
// stale keys count should stay zero
assertEquals(0, cache.cacheCleanupManager.getStaleKeysCount().get());
IOUtils.close(reader, secondReader);
}
// test staleness count based on removal notifications
public void testStaleCount_OnRemovalNotifications() throws Exception {
threadPool = getThreadPool();
Settings settings = Settings.builder().put(INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING.getKey(), "0.51").build();
cache = getIndicesRequestCache(settings);
writer.addDocument(newDoc(0, "foo"));
DirectoryReader reader = getReader(writer, indexShard.shardId());
// Get 5 entries into the cache
int totalKeys = 5;
IndicesService.IndexShardCacheEntity entity = null;
TermQueryBuilder termQuery = null;
BytesReference termBytes = null;
for (int i = 1; i <= totalKeys; i++) {
termQuery = new TermQueryBuilder("id", "" + i);
termBytes = XContentHelper.toXContent(termQuery, MediaTypeRegistry.JSON, false);
entity = new IndicesService.IndexShardCacheEntity(indexShard);
Loader loader = new Loader(reader, 0);
cache.getOrCompute(entity, loader, reader, termBytes);
assertEquals(i, cache.count());
}
// no keys are stale yet
assertEquals(0, cache.cacheCleanupManager.getStaleKeysCount().get());
// closing the reader should make all keys stale
reader.close();
assertEquals(totalKeys, cache.cacheCleanupManager.getStaleKeysCount().get());
String readerCacheKeyId = getReaderCacheKeyId(reader);
IndexShard indexShard = (IndexShard) entity.getCacheIdentity();
IndicesRequestCache.Key key = new IndicesRequestCache.Key(indexShard.shardId(), termBytes, readerCacheKeyId, indexShard.hashCode());
int staleCount = cache.cacheCleanupManager.getStaleKeysCount().get();
// Notification for Replaced should not deduct the staleCount
cache.onRemoval(
new RemovalNotification<ICacheKey<IndicesRequestCache.Key>, BytesReference>(
new ICacheKey<>(key),
getTermBytes(),
RemovalReason.REPLACED
)
);
// stale keys count should stay the same
assertEquals(staleCount, cache.cacheCleanupManager.getStaleKeysCount().get());
// Notification for all but Replaced should deduct the staleCount
RemovalReason[] reasons = { RemovalReason.INVALIDATED, RemovalReason.EVICTED, RemovalReason.EXPLICIT, RemovalReason.CAPACITY };
for (RemovalReason reason : reasons) {
cache.onRemoval(
new RemovalNotification<ICacheKey<IndicesRequestCache.Key>, BytesReference>(new ICacheKey<>(key), getTermBytes(), reason)
);
assertEquals(--staleCount, cache.cacheCleanupManager.getStaleKeysCount().get());
}
}
// when staleness count less than the stale threshold, stale keys should NOT be cleaned up.
public void testCacheCleanupBasedOnStaleThreshold_StalenessLesserThanThreshold() throws Exception {
threadPool = getThreadPool();
Settings settings = Settings.builder().put(INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING.getKey(), "51%").build();
cache = getIndicesRequestCache(settings);
writer.addDocument(newDoc(0, "foo"));
DirectoryReader reader = getReader(writer, indexShard.shardId());
DirectoryReader secondReader = getReader(writer, indexShard.shardId());
// Get 2 entries into the cache
cache.getOrCompute(getEntity(indexShard), getLoader(reader), reader, getTermBytes());
assertEquals(1, cache.count());
cache.getOrCompute(getEntity(indexShard), getLoader(secondReader), secondReader, getTermBytes());
assertEquals(2, cache.count());
// Close the reader, to be enqueued for cleanup
reader.close();
// 1 out of 2 keys ie 50% are now stale.
assertEquals(1, cache.cacheCleanupManager.getStaleKeysCount().get());
// cache count should not be affected
assertEquals(2, cache.count());
// clean cache with 51% staleness threshold
cache.cacheCleanupManager.cleanCache();
// cleanup should have been ignored
assertEquals(1, cache.cacheCleanupManager.getStaleKeysCount().get());
assertEquals(2, cache.count());
IOUtils.close(secondReader);
}
// test the cleanupKeyToCountMap are set appropriately when both readers are closed
public void testCleanupKeyToCountMapAreSetAppropriately() throws Exception {
threadPool = getThreadPool();
Settings settings = Settings.builder().put(INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING.getKey(), "0.51").build();
cache = getIndicesRequestCache(settings);
writer.addDocument(newDoc(0, "foo"));
ShardId shardId = indexShard.shardId();
DirectoryReader reader = getReader(writer, shardId);
DirectoryReader secondReader = getReader(writer, shardId);
// Get 2 entries into the cache from 2 different readers
cache.getOrCompute(getEntity(indexShard), getLoader(reader), reader, getTermBytes());
assertEquals(1, cache.count());
// test the mappings
ConcurrentHashMap<ShardId, ConcurrentHashMap<String, Integer>> cleanupKeyToCountMap = cache.cacheCleanupManager
.getCleanupKeyToCountMap();
assertEquals(1, (int) cleanupKeyToCountMap.get(shardId).get(getReaderCacheKeyId(reader)));
cache.getOrCompute(getEntity(indexShard), getLoader(secondReader), secondReader, getTermBytes());
// test the mapping
assertEquals(2, cache.count());
assertEquals(1, (int) cleanupKeyToCountMap.get(shardId).get(getReaderCacheKeyId(secondReader)));
// create another entry for the second reader
cache.getOrCompute(getEntity(indexShard), getLoader(secondReader), secondReader, getTermBytes("id", "1"));
// test the mapping
assertEquals(3, cache.count());
assertEquals(2, (int) cleanupKeyToCountMap.get(shardId).get(getReaderCacheKeyId(secondReader)));
// Close the reader, to create stale entries
reader.close();
// cache count should not be affected
assertEquals(3, cache.count());
// test the mapping, first reader's entry should be removed from the mapping and accounted for in the staleKeysCount
assertFalse(cleanupKeyToCountMap.get(shardId).containsKey(getReaderCacheKeyId(reader)));
assertEquals(1, cache.cacheCleanupManager.getStaleKeysCount().get());
// second reader's mapping should not be affected
assertEquals(2, (int) cleanupKeyToCountMap.get(shardId).get(getReaderCacheKeyId(secondReader)));
// send removal notification for first reader
IndicesRequestCache.Key key = new IndicesRequestCache.Key(
indexShard.shardId(),
getTermBytes(),
getReaderCacheKeyId(reader),
indexShard.hashCode()
);
cache.onRemoval(
new RemovalNotification<ICacheKey<IndicesRequestCache.Key>, BytesReference>(
new ICacheKey<>(key),
getTermBytes(),
RemovalReason.EVICTED
)
);
// test the mapping, it should stay the same
assertFalse(cleanupKeyToCountMap.get(shardId).containsKey(getReaderCacheKeyId(reader)));
// staleKeysCount should be decremented
assertEquals(0, cache.cacheCleanupManager.getStaleKeysCount().get());
// second reader's mapping should not be affected
assertEquals(2, (int) cleanupKeyToCountMap.get(shardId).get(getReaderCacheKeyId(secondReader)));
// Without closing the secondReader send removal notification of one of its key
key = new IndicesRequestCache.Key(indexShard.shardId(), getTermBytes(), getReaderCacheKeyId(secondReader), indexShard.hashCode());
cache.onRemoval(
new RemovalNotification<ICacheKey<IndicesRequestCache.Key>, BytesReference>(
new ICacheKey<>(key),
getTermBytes(),
RemovalReason.EVICTED
)
);
// staleKeysCount should be the same as before
assertEquals(0, cache.cacheCleanupManager.getStaleKeysCount().get());
// secondReader's readerCacheKeyId count should be decremented by 1
assertEquals(1, (int) cleanupKeyToCountMap.get(shardId).get(getReaderCacheKeyId(secondReader)));
// Without closing the secondReader send removal notification of its last key
key = new IndicesRequestCache.Key(indexShard.shardId(), getTermBytes(), getReaderCacheKeyId(secondReader), indexShard.hashCode());
cache.onRemoval(
new RemovalNotification<ICacheKey<IndicesRequestCache.Key>, BytesReference>(
new ICacheKey<>(key),
getTermBytes(),
RemovalReason.EVICTED
)
);
// staleKeysCount should be the same as before
assertEquals(0, cache.cacheCleanupManager.getStaleKeysCount().get());
// since all the readers of this shard is closed, the cleanupKeyToCountMap should have no entries
assertEquals(0, cleanupKeyToCountMap.size());
IOUtils.close(secondReader);
}
// test adding to cleanupKeyToCountMap with multiple threads
public void testAddingToCleanupKeyToCountMapWorksAppropriatelyWithMultipleThreads() throws Exception {
threadPool = getThreadPool();
Settings settings = Settings.builder().put(INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING.getKey(), "51%").build();
cache = getIndicesRequestCache(settings);
int numberOfThreads = 10;
int numberOfIterations = 1000;
Phaser phaser = new Phaser(numberOfThreads + 1); // +1 for the main thread
AtomicBoolean concurrentModificationExceptionDetected = new AtomicBoolean(false);
ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);
for (int i = 0; i < numberOfThreads; i++) {
executorService.submit(() -> {
phaser.arriveAndAwaitAdvance(); // Ensure all threads start at the same time
try {
for (int j = 0; j < numberOfIterations; j++) {
cache.cacheCleanupManager.addToCleanupKeyToCountMap(indexShard.shardId(), UUID.randomUUID().toString());
}
} catch (ConcurrentModificationException e) {
logger.error("ConcurrentModificationException detected in thread : " + e.getMessage());
concurrentModificationExceptionDetected.set(true); // Set flag if exception is detected
}
});
}
phaser.arriveAndAwaitAdvance(); // Start all threads
// Main thread iterates over the map
executorService.submit(() -> {
try {
for (int j = 0; j < numberOfIterations; j++) {
cache.cacheCleanupManager.getCleanupKeyToCountMap().forEach((k, v) -> {
v.forEach((k1, v1) -> {
// Accessing the map to create contention
v.get(k1);
});
});
}
} catch (ConcurrentModificationException e) {
logger.error("ConcurrentModificationException detected in main thread : " + e.getMessage());
concurrentModificationExceptionDetected.set(true); // Set flag if exception is detected
}
});
executorService.shutdown();
assertTrue(executorService.awaitTermination(60, TimeUnit.SECONDS));
assertEquals(
numberOfThreads * numberOfIterations,
cache.cacheCleanupManager.getCleanupKeyToCountMap().get(indexShard.shardId()).size()
);
assertFalse(concurrentModificationExceptionDetected.get());
}
private IndicesRequestCache getIndicesRequestCache(Settings settings) {
IndicesService indicesService = getInstanceFromNode(IndicesService.class);
return new IndicesRequestCache(
settings,
indicesService.indicesRequestCache.cacheEntityLookup,
new CacheModule(new ArrayList<>(), Settings.EMPTY).getCacheService(),
threadPool,
ClusterServiceUtils.createClusterService(threadPool)
);
}
private DirectoryReader getReader(IndexWriter writer, ShardId shardId) throws IOException {
return OpenSearchDirectoryReader.wrap(DirectoryReader.open(writer), shardId);
}
private Loader getLoader(DirectoryReader reader) {
return new Loader(reader, 0);
}
private IndicesService.IndexShardCacheEntity getEntity(IndexShard indexShard) {
return new IndicesService.IndexShardCacheEntity(indexShard);
}
private BytesReference getTermBytes() throws IOException {
TermQueryBuilder termQuery = new TermQueryBuilder("id", "0");
return XContentHelper.toXContent(termQuery, MediaTypeRegistry.JSON, false);
}
private BytesReference getTermBytes(String fieldName, String value) throws IOException {
TermQueryBuilder termQuery = new TermQueryBuilder(fieldName, value);
return XContentHelper.toXContent(termQuery, MediaTypeRegistry.JSON, false);
}
private String getReaderCacheKeyId(DirectoryReader reader) {
OpenSearchDirectoryReader.DelegatingCacheHelper delegatingCacheHelper = (OpenSearchDirectoryReader.DelegatingCacheHelper) reader
.getReaderCacheHelper();
return delegatingCacheHelper.getDelegatingCacheKey().getId();
}
public void testClosingIndexWipesStats() throws Exception {
IndicesService indicesService = getInstanceFromNode(IndicesService.class);
String[] levels = { INDEX_DIMENSION_NAME, SHARD_ID_DIMENSION_NAME };
// Create two indices each with multiple shards
int numShards = 3;
Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numShards).build();
String indexToKeepName = "test";
String indexToCloseName = "test2";
// delete all indices if already
assertAcked(client().admin().indices().prepareDelete("_all").get());
IndexService indexToKeep = createIndex(indexToKeepName, indexSettings);
IndexService indexToClose = createIndex(indexToCloseName, indexSettings);
for (int i = 0; i < numShards; i++) {
// Check we can get all the shards we expect
assertNotNull(indexToKeep.getShard(i));
assertNotNull(indexToClose.getShard(i));
}
threadPool = getThreadPool();
Settings settings = Settings.builder()
.put(INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING.getKey(), "0.001%")
.put(FeatureFlags.PLUGGABLE_CACHE, true)
.build();
cache = new IndicesRequestCache(settings, (shardId -> {
IndexService indexService = null;
try {
indexService = indicesService.indexServiceSafe(shardId.getIndex());
} catch (IndexNotFoundException ex) {
return Optional.empty();
}
try {
return Optional.of(new IndicesService.IndexShardCacheEntity(indexService.getShard(shardId.id())));
} catch (ShardNotFoundException ex) {
return Optional.empty();
}
}),
new CacheModule(new ArrayList<>(), Settings.EMPTY).getCacheService(),
threadPool,
ClusterServiceUtils.createClusterService(threadPool)
);
writer.addDocument(newDoc(0, "foo"));
TermQueryBuilder termQuery = new TermQueryBuilder("id", "0");
BytesReference termBytes = XContentHelper.toXContent(termQuery, MediaTypeRegistry.JSON, false);
if (randomBoolean()) {
writer.flush();
IOUtils.close(writer);
writer = new IndexWriter(dir, newIndexWriterConfig());
}
writer.updateDocument(new Term("id", "0"), newDoc(0, "bar"));
DirectoryReader secondReader = OpenSearchDirectoryReader.wrap(DirectoryReader.open(writer), new ShardId("foo", "bar", 1));
List<DirectoryReader> readersToClose = new ArrayList<>();
List<DirectoryReader> readersToKeep = new ArrayList<>();
// Put entries into the cache for each shard
for (IndexService indexService : new IndexService[] { indexToKeep, indexToClose }) {
for (int i = 0; i < numShards; i++) {
IndexShard indexShard = indexService.getShard(i);
IndicesService.IndexShardCacheEntity entity = new IndicesService.IndexShardCacheEntity(indexShard);
DirectoryReader reader = OpenSearchDirectoryReader.wrap(DirectoryReader.open(writer), indexShard.shardId());
if (indexService == indexToClose) {
readersToClose.add(reader);
} else {
readersToKeep.add(reader);
}
Loader loader = new Loader(reader, 0);
cache.getOrCompute(entity, loader, reader, termBytes);
}
}
// Check resulting stats
List<List<String>> initialDimensionValues = new ArrayList<>();
for (IndexService indexService : new IndexService[] { indexToKeep, indexToClose }) {
for (int i = 0; i < numShards; i++) {
ShardId shardId = indexService.getShard(i).shardId();
List<String> dimensionValues = List.of(shardId.getIndexName(), shardId.toString());
initialDimensionValues.add(dimensionValues);
ImmutableCacheStatsHolder holder = cache.stats(levels);
ImmutableCacheStats snapshot = cache.stats(levels).getStatsForDimensionValues(dimensionValues);
assertNotNull(snapshot);
// check the values are not empty by confirming entries != 0, this should always be true since the missed value is loaded
// into the cache
assertNotEquals(0, snapshot.getItems());
}
}
// Delete an index
indexToClose.close("test_deletion", true);
// This actually closes the shards associated with the readers, which is necessary for cache cleanup logic
// In this UT, manually close the readers as well; could not figure out how to connect all this up in a UT so that
// we could get readers that were properly connected to an index's directory
for (DirectoryReader reader : readersToClose) {
IOUtils.close(reader);
}
// Trigger cache cleanup
cache.cacheCleanupManager.cleanCache();
// Now stats for the closed index should be gone
for (List<String> dimensionValues : initialDimensionValues) {
ImmutableCacheStats snapshot = cache.stats(levels).getStatsForDimensionValues(dimensionValues);
if (dimensionValues.get(0).equals(indexToCloseName)) {
assertNull(snapshot);
} else {
assertNotNull(snapshot);
// check the values are not empty by confirming entries != 0, this should always be true since the missed value is loaded
// into the cache
assertNotEquals(0, snapshot.getItems());
}