forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndicesRequestCacheIT.java
1291 lines (1167 loc) · 63 KB
/
IndicesRequestCacheIT.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 com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
import org.opensearch.action.admin.cluster.node.stats.NodeStats;
import org.opensearch.action.admin.cluster.node.stats.NodesStatsResponse;
import org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
import org.opensearch.action.admin.indices.alias.Alias;
import org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest;
import org.opensearch.action.admin.indices.forcemerge.ForceMergeResponse;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.action.search.SearchType;
import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.time.DateFormatter;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.FeatureFlags;
import org.opensearch.index.IndexNotFoundException;
import org.opensearch.index.cache.request.RequestCacheStats;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder;
import org.opensearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.opensearch.search.aggregations.bucket.histogram.Histogram;
import org.opensearch.search.aggregations.bucket.histogram.Histogram.Bucket;
import org.opensearch.test.OpenSearchIntegTestCase;
import org.opensearch.test.ParameterizedStaticSettingsOpenSearchIntegTestCase;
import org.opensearch.test.hamcrest.OpenSearchAssertions;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS;
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS;
import static org.opensearch.indices.IndicesRequestCache.INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING;
import static org.opensearch.indices.IndicesService.INDICES_CACHE_CLEANUP_INTERVAL_SETTING_KEY;
import static org.opensearch.search.SearchService.CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING;
import static org.opensearch.search.aggregations.AggregationBuilders.dateHistogram;
import static org.opensearch.search.aggregations.AggregationBuilders.dateRange;
import static org.opensearch.search.aggregations.AggregationBuilders.filter;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0, supportsDedicatedMasters = false)
public class IndicesRequestCacheIT extends ParameterizedStaticSettingsOpenSearchIntegTestCase {
public IndicesRequestCacheIT(Settings settings) {
super(settings);
}
@ParametersFactory
public static Collection<Object[]> parameters() {
return Arrays.asList(
new Object[] { Settings.builder().put(CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING.getKey(), false).build() },
new Object[] { Settings.builder().put(CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING.getKey(), true).build() },
new Object[] { Settings.builder().put(FeatureFlags.PLUGGABLE_CACHE, "true").build() },
new Object[] { Settings.builder().put(FeatureFlags.PLUGGABLE_CACHE, "false").build() }
);
}
@Override
protected boolean useRandomReplicationStrategy() {
return true;
}
// One of the primary purposes of the query cache is to cache aggs results
public void testCacheAggs() throws Exception {
Client client = client();
String index = "index";
assertAcked(
client.admin()
.indices()
.prepareCreate(index)
.setMapping("f", "type=date")
.setSettings(
Settings.builder()
.put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)
.put(SETTING_NUMBER_OF_SHARDS, 1)
.put(SETTING_NUMBER_OF_REPLICAS, 0)
)
.get()
);
indexRandom(
true,
client.prepareIndex(index).setSource("f", "2014-03-10T00:00:00.000Z"),
client.prepareIndex(index).setSource("f", "2014-05-13T00:00:00.000Z")
);
ensureSearchable(index);
// This is not a random example: serialization with time zones writes shared strings
// which used to not work well with the query cache because of the handles stream output
// see #9500
final SearchResponse r1 = client.prepareSearch(index)
.setSize(0)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.addAggregation(
dateHistogram("histo").field("f")
.timeZone(ZoneId.of("+01:00"))
.minDocCount(0)
.dateHistogramInterval(DateHistogramInterval.MONTH)
)
.get();
assertSearchResponse(r1);
// The cached is actually used
assertThat(
client.admin().indices().prepareStats(index).setRequestCache(true).get().getTotal().getRequestCache().getMemorySizeInBytes(),
greaterThan(0L)
);
for (int i = 0; i < 10; ++i) {
final SearchResponse r2 = client.prepareSearch(index)
.setSize(0)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.addAggregation(
dateHistogram("histo").field("f")
.timeZone(ZoneId.of("+01:00"))
.minDocCount(0)
.dateHistogramInterval(DateHistogramInterval.MONTH)
)
.get();
assertSearchResponse(r2);
Histogram h1 = r1.getAggregations().get("histo");
Histogram h2 = r2.getAggregations().get("histo");
final List<? extends Bucket> buckets1 = h1.getBuckets();
final List<? extends Bucket> buckets2 = h2.getBuckets();
assertEquals(buckets1.size(), buckets2.size());
for (int j = 0; j < buckets1.size(); ++j) {
final Bucket b1 = buckets1.get(j);
final Bucket b2 = buckets2.get(j);
assertEquals(b1.getKey(), b2.getKey());
assertEquals(b1.getDocCount(), b2.getDocCount());
}
}
}
public void testQueryRewrite() throws Exception {
Client client = client();
String index = "index";
assertAcked(
client.admin()
.indices()
.prepareCreate(index)
.setMapping("s", "type=date")
.setSettings(
Settings.builder()
.put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 5)
.put("index.number_of_routing_shards", 5)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
)
.get()
);
indexRandom(
true,
client.prepareIndex(index).setId("1").setRouting("1").setSource("s", "2016-03-19"),
client.prepareIndex(index).setId("2").setRouting("1").setSource("s", "2016-03-20"),
client.prepareIndex(index).setId("3").setRouting("1").setSource("s", "2016-03-21"),
client.prepareIndex(index).setId("4").setRouting("2").setSource("s", "2016-03-22"),
client.prepareIndex(index).setId("5").setRouting("2").setSource("s", "2016-03-23"),
client.prepareIndex(index).setId("6").setRouting("2").setSource("s", "2016-03-24"),
client.prepareIndex(index).setId("7").setRouting("3").setSource("s", "2016-03-25"),
client.prepareIndex(index).setId("8").setRouting("3").setSource("s", "2016-03-26"),
client.prepareIndex(index).setId("9").setRouting("3").setSource("s", "2016-03-27")
);
ensureSearchable(index);
assertCacheState(client, index, 0, 0);
// Force merge the index to ensure there can be no background merges during the subsequent searches that would invalidate the cache
ForceMergeResponse forceMergeResponse = client.admin().indices().prepareForceMerge(index).setFlush(true).get();
OpenSearchAssertions.assertAllSuccessful(forceMergeResponse);
refreshAndWaitForReplication();
ensureSearchable(index);
assertCacheState(client, index, 0, 0);
final SearchResponse r1 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-19").lte("2016-03-25"))
// to ensure that query is executed even if it rewrites to match_no_docs
.addAggregation(new GlobalAggregationBuilder("global"))
.get();
OpenSearchAssertions.assertAllSuccessful(r1);
assertThat(r1.getHits().getTotalHits().value, equalTo(7L));
assertCacheState(client, index, 0, 5);
final SearchResponse r2 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-20").lte("2016-03-26"))
.addAggregation(new GlobalAggregationBuilder("global"))
.get();
OpenSearchAssertions.assertAllSuccessful(r2);
assertThat(r2.getHits().getTotalHits().value, equalTo(7L));
assertCacheState(client, index, 3, 7);
final SearchResponse r3 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-21").lte("2016-03-27"))
.addAggregation(new GlobalAggregationBuilder("global"))
.get();
OpenSearchAssertions.assertAllSuccessful(r3);
assertThat(r3.getHits().getTotalHits().value, equalTo(7L));
assertCacheState(client, index, 6, 9);
}
public void testQueryRewriteMissingValues() throws Exception {
Client client = client();
String index = "index";
assertAcked(
client.admin()
.indices()
.prepareCreate(index)
.setMapping("s", "type=date")
.setSettings(
Settings.builder()
.put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
)
.get()
);
indexRandom(
true,
client.prepareIndex(index).setId("1").setSource("s", "2016-03-19"),
client.prepareIndex(index).setId("2").setSource("s", "2016-03-20"),
client.prepareIndex(index).setId("3").setSource("s", "2016-03-21"),
client.prepareIndex(index).setId("4").setSource("s", "2016-03-22"),
client.prepareIndex(index).setId("5").setSource("s", "2016-03-23"),
client.prepareIndex(index).setId("6").setSource("s", "2016-03-24"),
client.prepareIndex(index).setId("7").setSource("other", "value"),
client.prepareIndex(index).setId("8").setSource("s", "2016-03-26"),
client.prepareIndex(index).setId("9").setSource("s", "2016-03-27")
);
ensureSearchable(index);
assertCacheState(client, index, 0, 0);
// Force merge the index to ensure there can be no background merges during the subsequent searches that would invalidate the cache
ForceMergeResponse forceMergeResponse = client.admin().indices().prepareForceMerge(index).setFlush(true).get();
OpenSearchAssertions.assertAllSuccessful(forceMergeResponse);
refreshAndWaitForReplication();
ensureSearchable(index);
assertCacheState(client, index, 0, 0);
final SearchResponse r1 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-19").lte("2016-03-28"))
.get();
OpenSearchAssertions.assertAllSuccessful(r1);
assertThat(r1.getHits().getTotalHits().value, equalTo(8L));
assertCacheState(client, index, 0, 1);
final SearchResponse r2 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-19").lte("2016-03-28"))
.get();
OpenSearchAssertions.assertAllSuccessful(r2);
assertThat(r2.getHits().getTotalHits().value, equalTo(8L));
assertCacheState(client, index, 1, 1);
final SearchResponse r3 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-19").lte("2016-03-28"))
.get();
OpenSearchAssertions.assertAllSuccessful(r3);
assertThat(r3.getHits().getTotalHits().value, equalTo(8L));
assertCacheState(client, index, 2, 1);
}
public void testQueryRewriteDates() throws Exception {
Client client = client();
String index = "index";
assertAcked(
client.admin()
.indices()
.prepareCreate(index)
.setMapping("d", "type=date")
.setSettings(
Settings.builder()
.put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
)
.get()
);
indexRandom(
true,
client.prepareIndex(index).setId("1").setSource("d", "2014-01-01T00:00:00"),
client.prepareIndex(index).setId("2").setSource("d", "2014-02-01T00:00:00"),
client.prepareIndex(index).setId("3").setSource("d", "2014-03-01T00:00:00"),
client.prepareIndex(index).setId("4").setSource("d", "2014-04-01T00:00:00"),
client.prepareIndex(index).setId("5").setSource("d", "2014-05-01T00:00:00"),
client.prepareIndex(index).setId("6").setSource("d", "2014-06-01T00:00:00"),
client.prepareIndex(index).setId("7").setSource("d", "2014-07-01T00:00:00"),
client.prepareIndex(index).setId("8").setSource("d", "2014-08-01T00:00:00"),
client.prepareIndex(index).setId("9").setSource("d", "2014-09-01T00:00:00")
);
ensureSearchable(index);
assertCacheState(client, index, 0, 0);
// Force merge the index to ensure there can be no background merges during the subsequent searches that would invalidate the cache
ForceMergeResponse forceMergeResponse = client.admin().indices().prepareForceMerge(index).setFlush(true).get();
OpenSearchAssertions.assertAllSuccessful(forceMergeResponse);
refreshAndWaitForReplication();
ensureSearchable(index);
assertCacheState(client, index, 0, 0);
final SearchResponse r1 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("d").gte("2013-01-01T00:00:00").lte("now"))
// to ensure that query is executed even if it rewrites to match_no_docs
.addAggregation(new GlobalAggregationBuilder("global"))
.get();
OpenSearchAssertions.assertAllSuccessful(r1);
assertThat(r1.getHits().getTotalHits().value, equalTo(9L));
assertCacheState(client, index, 0, 1);
final SearchResponse r2 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("d").gte("2013-01-01T00:00:00").lte("now"))
.addAggregation(new GlobalAggregationBuilder("global"))
.get();
OpenSearchAssertions.assertAllSuccessful(r2);
assertThat(r2.getHits().getTotalHits().value, equalTo(9L));
assertCacheState(client, index, 1, 1);
final SearchResponse r3 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("d").gte("2013-01-01T00:00:00").lte("now"))
.addAggregation(new GlobalAggregationBuilder("global"))
.get();
OpenSearchAssertions.assertAllSuccessful(r3);
assertThat(r3.getHits().getTotalHits().value, equalTo(9L));
assertCacheState(client, index, 2, 1);
}
public void testQueryRewriteDatesWithNow() throws Exception {
Client client = client();
Settings settings = Settings.builder()
.put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
.build();
assertAcked(client.admin().indices().prepareCreate("index-1").setMapping("d", "type=date").setSettings(settings).get());
assertAcked(client.admin().indices().prepareCreate("index-2").setMapping("d", "type=date").setSettings(settings).get());
assertAcked(client.admin().indices().prepareCreate("index-3").setMapping("d", "type=date").setSettings(settings).get());
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
DateFormatter formatter = DateFormatter.forPattern("strict_date_optional_time");
indexRandom(
true,
client.prepareIndex("index-1").setId("1").setSource("d", formatter.format(now)),
client.prepareIndex("index-1").setId("2").setSource("d", formatter.format(now.minusDays(1))),
client.prepareIndex("index-1").setId("3").setSource("d", formatter.format(now.minusDays(2))),
client.prepareIndex("index-2").setId("4").setSource("d", formatter.format(now.minusDays(3))),
client.prepareIndex("index-2").setId("5").setSource("d", formatter.format(now.minusDays(4))),
client.prepareIndex("index-2").setId("6").setSource("d", formatter.format(now.minusDays(5))),
client.prepareIndex("index-3").setId("7").setSource("d", formatter.format(now.minusDays(6))),
client.prepareIndex("index-3").setId("8").setSource("d", formatter.format(now.minusDays(7))),
client.prepareIndex("index-3").setId("9").setSource("d", formatter.format(now.minusDays(8)))
);
ensureSearchable("index-1", "index-2", "index-3");
assertCacheState(client, "index-1", 0, 0);
assertCacheState(client, "index-2", 0, 0);
assertCacheState(client, "index-3", 0, 0);
// Force merge the index to ensure there can be no background merges during the subsequent searches that would invalidate the cache
ForceMergeResponse forceMergeResponse = client.admin()
.indices()
.prepareForceMerge("index-1", "index-2", "index-3")
.setFlush(true)
.get();
OpenSearchAssertions.assertAllSuccessful(forceMergeResponse);
refreshAndWaitForReplication();
ensureSearchable("index-1", "index-2", "index-3");
assertCacheState(client, "index-1", 0, 0);
assertCacheState(client, "index-2", 0, 0);
assertCacheState(client, "index-3", 0, 0);
final SearchResponse r1 = client.prepareSearch("index-*")
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("d").gte("now-7d/d").lte("now"))
.get();
OpenSearchAssertions.assertAllSuccessful(r1);
assertThat(r1.getHits().getTotalHits().value, equalTo(8L));
assertCacheState(client, "index-1", 0, 1);
assertCacheState(client, "index-2", 0, 1);
// Because the query will INTERSECT with the 3rd index it will not be
// rewritten and will still contain `now` so won't be recorded as a
// cache miss or cache hit since queries containing now can't be cached
assertCacheState(client, "index-3", 0, 0);
final SearchResponse r2 = client.prepareSearch("index-*")
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("d").gte("now-7d/d").lte("now"))
.get();
OpenSearchAssertions.assertAllSuccessful(r2);
assertThat(r2.getHits().getTotalHits().value, equalTo(8L));
assertCacheState(client, "index-1", 1, 1);
assertCacheState(client, "index-2", 1, 1);
assertCacheState(client, "index-3", 0, 0);
final SearchResponse r3 = client.prepareSearch("index-*")
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("d").gte("now-7d/d").lte("now"))
.get();
OpenSearchAssertions.assertAllSuccessful(r3);
assertThat(r3.getHits().getTotalHits().value, equalTo(8L));
assertCacheState(client, "index-1", 2, 1);
assertCacheState(client, "index-2", 2, 1);
assertCacheState(client, "index-3", 0, 0);
}
public void testCanCache() throws Exception {
Client client = client();
Settings settings = Settings.builder()
.put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2)
.put("index.number_of_routing_shards", 2)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
.build();
String index = "index";
assertAcked(client.admin().indices().prepareCreate(index).setMapping("s", "type=date").setSettings(settings).get());
indexRandom(
true,
client.prepareIndex(index).setId("1").setRouting("1").setSource("s", "2016-03-19"),
client.prepareIndex(index).setId("2").setRouting("1").setSource("s", "2016-03-20"),
client.prepareIndex(index).setId("3").setRouting("1").setSource("s", "2016-03-21"),
client.prepareIndex(index).setId("4").setRouting("2").setSource("s", "2016-03-22"),
client.prepareIndex(index).setId("5").setRouting("2").setSource("s", "2016-03-23"),
client.prepareIndex(index).setId("6").setRouting("2").setSource("s", "2016-03-24"),
client.prepareIndex(index).setId("7").setRouting("3").setSource("s", "2016-03-25"),
client.prepareIndex(index).setId("8").setRouting("3").setSource("s", "2016-03-26"),
client.prepareIndex(index).setId("9").setRouting("3").setSource("s", "2016-03-27")
);
ensureSearchable(index);
assertCacheState(client, index, 0, 0);
// Force merge the index to ensure there can be no background merges during the subsequent searches that would invalidate the cache
ForceMergeResponse forceMergeResponse = client.admin().indices().prepareForceMerge(index).setFlush(true).get();
OpenSearchAssertions.assertAllSuccessful(forceMergeResponse);
refreshAndWaitForReplication();
ensureSearchable(index);
assertCacheState(client, index, 0, 0);
// If size > 0 we should no cache by default
final SearchResponse r1 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(1)
.setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-19").lte("2016-03-25"))
.get();
OpenSearchAssertions.assertAllSuccessful(r1);
assertThat(r1.getHits().getTotalHits().value, equalTo(7L));
assertCacheState(client, index, 0, 0);
// If search type is DFS_QUERY_THEN_FETCH we should not cache
final SearchResponse r2 = client.prepareSearch(index)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-20").lte("2016-03-26"))
.get();
OpenSearchAssertions.assertAllSuccessful(r2);
assertThat(r2.getHits().getTotalHits().value, equalTo(7L));
assertCacheState(client, index, 0, 0);
// If search type is DFS_QUERY_THEN_FETCH we should not cache even if
// the cache flag is explicitly set on the request
final SearchResponse r3 = client.prepareSearch(index)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setSize(0)
.setRequestCache(true)
.setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-20").lte("2016-03-26"))
.get();
OpenSearchAssertions.assertAllSuccessful(r3);
assertThat(r3.getHits().getTotalHits().value, equalTo(7L));
assertCacheState(client, index, 0, 0);
// If the request has an non-filter aggregation containing now we should not cache
final SearchResponse r5 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setRequestCache(true)
.setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-20").lte("2016-03-26"))
.addAggregation(dateRange("foo").field("s").addRange("now-10y", "now"))
.get();
OpenSearchAssertions.assertAllSuccessful(r5);
assertThat(r5.getHits().getTotalHits().value, equalTo(7L));
assertCacheState(client, index, 0, 0);
// If size > 1 and cache flag is set on the request we should cache
final SearchResponse r6 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(1)
.setRequestCache(true)
.setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-21").lte("2016-03-27"))
.get();
OpenSearchAssertions.assertAllSuccessful(r6);
assertThat(r6.getHits().getTotalHits().value, equalTo(7L));
assertCacheState(client, index, 0, 2);
// If the request has a filter aggregation containing now we should cache since it gets rewritten
final SearchResponse r4 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setRequestCache(true)
.setQuery(QueryBuilders.rangeQuery("s").gte("2016-03-20").lte("2016-03-26"))
.addAggregation(filter("foo", QueryBuilders.rangeQuery("s").from("now-10y").to("now")))
.get();
OpenSearchAssertions.assertAllSuccessful(r4);
assertThat(r4.getHits().getTotalHits().value, equalTo(7L));
assertCacheState(client, index, 0, 4);
}
public void testCacheWithFilteredAlias() throws InterruptedException {
Client client = client();
Settings settings = Settings.builder()
.put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
.build();
String index = "index";
assertAcked(
client.admin()
.indices()
.prepareCreate(index)
.setMapping("created_at", "type=date")
.setSettings(settings)
.addAlias(new Alias("last_week").filter(QueryBuilders.rangeQuery("created_at").gte("now-7d/d")))
.get()
);
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
client.prepareIndex(index).setId("1").setRouting("1").setSource("created_at", DateTimeFormatter.ISO_LOCAL_DATE.format(now)).get();
// Force merge the index to ensure there can be no background merges during the subsequent searches that would invalidate the cache
ForceMergeResponse forceMergeResponse = client.admin().indices().prepareForceMerge(index).setFlush(true).get();
OpenSearchAssertions.assertAllSuccessful(forceMergeResponse);
refreshAndWaitForReplication();
indexRandomForConcurrentSearch(index);
assertCacheState(client, index, 0, 0);
SearchResponse r1 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("created_at").gte("now-7d/d"))
.get();
OpenSearchAssertions.assertAllSuccessful(r1);
assertThat(r1.getHits().getTotalHits().value, equalTo(1L));
assertCacheState(client, index, 0, 1);
r1 = client.prepareSearch(index)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setSize(0)
.setQuery(QueryBuilders.rangeQuery("created_at").gte("now-7d/d"))
.get();
OpenSearchAssertions.assertAllSuccessful(r1);
assertThat(r1.getHits().getTotalHits().value, equalTo(1L));
assertCacheState(client, index, 1, 1);
r1 = client.prepareSearch("last_week").setSearchType(SearchType.QUERY_THEN_FETCH).setSize(0).get();
OpenSearchAssertions.assertAllSuccessful(r1);
assertThat(r1.getHits().getTotalHits().value, equalTo(1L));
assertCacheState(client, index, 1, 2);
r1 = client.prepareSearch("last_week").setSearchType(SearchType.QUERY_THEN_FETCH).setSize(0).get();
OpenSearchAssertions.assertAllSuccessful(r1);
assertThat(r1.getHits().getTotalHits().value, equalTo(1L));
assertCacheState(client, index, 2, 2);
}
public void testProfileDisableCache() throws Exception {
Client client = client();
String index = "index";
assertAcked(
client.admin()
.indices()
.prepareCreate(index)
.setMapping("k", "type=keyword")
.setSettings(
Settings.builder()
.put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
)
.get()
);
indexRandom(true, client.prepareIndex(index).setSource("k", "hello"));
ensureSearchable(index);
int expectedHits = 0;
int expectedMisses = 0;
for (int i = 0; i < 5; i++) {
boolean profile = i % 2 == 0;
SearchResponse resp = client.prepareSearch(index)
.setRequestCache(true)
.setProfile(profile)
.setQuery(QueryBuilders.termQuery("k", "hello"))
.get();
assertSearchResponse(resp);
OpenSearchAssertions.assertAllSuccessful(resp);
assertThat(resp.getHits().getTotalHits().value, equalTo(1L));
if (profile == false) {
if (i == 1) {
expectedMisses++;
} else {
expectedHits++;
}
}
assertCacheState(client, index, expectedHits, expectedMisses);
}
}
public void testCacheWithInvalidation() throws Exception {
Client client = client();
String index = "index";
assertAcked(
client.admin()
.indices()
.prepareCreate(index)
.setMapping("k", "type=keyword")
.setSettings(
Settings.builder()
.put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
.put("index.refresh_interval", -1)
)
.get()
);
indexRandom(true, client.prepareIndex(index).setSource("k", "hello"));
ensureSearchable(index);
SearchResponse resp = client.prepareSearch(index).setRequestCache(true).setQuery(QueryBuilders.termQuery("k", "hello")).get();
assertSearchResponse(resp);
OpenSearchAssertions.assertAllSuccessful(resp);
assertThat(resp.getHits().getTotalHits().value, equalTo(1L));
assertCacheState(client, index, 0, 1);
// Index but don't refresh
indexRandom(false, client.prepareIndex(index).setSource("k", "hello2"));
resp = client.prepareSearch(index).setRequestCache(true).setQuery(QueryBuilders.termQuery("k", "hello")).get();
assertSearchResponse(resp);
// Should expect hit as here as refresh didn't happen
assertCacheState(client, index, 1, 1);
// Explicit refresh would invalidate cache
refreshAndWaitForReplication();
// Hit same query again
resp = client.prepareSearch(index).setRequestCache(true).setQuery(QueryBuilders.termQuery("k", "hello")).get();
assertSearchResponse(resp);
// Should expect miss as key has changed due to change in IndexReader.CacheKey (due to refresh)
assertCacheState(client, index, 1, 2);
}
// calling cache clear api, when staleness threshold is lower than staleness, it should clean the stale keys from cache
public void testCacheClearAPIRemovesStaleKeysWhenStalenessThresholdIsLow() throws Exception {
String node = internalCluster().startNode(
Settings.builder()
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_CLEANUP_STALENESS_THRESHOLD_SETTING_KEY, 0.10)
.put(
IndicesRequestCache.INDICES_REQUEST_CACHE_CLEANUP_INTERVAL_SETTING_KEY,
// setting intentionally high to avoid cache cleaner interfering
TimeValue.timeValueMillis(300)
)
);
Client client = client(node);
String index1 = "index1";
String index2 = "index2";
setupIndex(client, index1);
setupIndex(client, index2);
// create first cache entry in index1
createCacheEntry(client, index1, "hello");
assertCacheState(client, index1, 0, 1);
long memorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
assertTrue(memorySizeForIndex1 > 0);
// create second cache entry in index1
createCacheEntry(client, index1, "there");
assertCacheState(client, index1, 0, 2);
long finalMemorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
assertTrue(finalMemorySizeForIndex1 > memorySizeForIndex1);
// create first cache entry in index2
createCacheEntry(client, index2, "hello");
assertCacheState(client, index2, 0, 1);
assertTrue(getRequestCacheStats(client, index2).getMemorySizeInBytes() > 0);
ClearIndicesCacheRequest clearIndicesCacheRequest = new ClearIndicesCacheRequest(index2);
client.admin().indices().clearCache(clearIndicesCacheRequest).actionGet();
// cache cleaner should have cleaned up the stale key from index 2
assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes());
// cache cleaner should NOT have cleaned from index 1
assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes());
}
// when staleness threshold is lower than staleness, it should clean the stale keys from cache
public void testStaleKeysCleanupWithLowThreshold() throws Exception {
int cacheCleanIntervalInMillis = 1;
String node = internalCluster().startNode(
Settings.builder()
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_CLEANUP_STALENESS_THRESHOLD_SETTING_KEY, 0.10)
.put(
IndicesRequestCache.INDICES_REQUEST_CACHE_CLEANUP_INTERVAL_SETTING_KEY,
TimeValue.timeValueMillis(cacheCleanIntervalInMillis)
)
);
Client client = client(node);
String index1 = "index1";
String index2 = "index2";
setupIndex(client, index1);
setupIndex(client, index2);
// create first cache entry in index1
createCacheEntry(client, index1, "hello");
assertCacheState(client, index1, 0, 1);
long memorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
assertTrue(memorySizeForIndex1 > 0);
// create second cache entry in index1
createCacheEntry(client, index1, "there");
assertCacheState(client, index1, 0, 2);
long finalMemorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
assertTrue(finalMemorySizeForIndex1 > memorySizeForIndex1);
// create first cache entry in index2
createCacheEntry(client, index2, "hello");
assertCacheState(client, index2, 0, 1);
assertTrue(getRequestCacheStats(client, index2).getMemorySizeInBytes() > 0);
// force refresh so that it creates 1 stale key
flushAndRefresh(index2);
// sleep until cache cleaner would have cleaned up the stale key from index 2
assertBusy(() -> {
// cache cleaner should have cleaned up the stale key from index 2
assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes());
// cache cleaner should NOT have cleaned from index 1
assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes());
}, cacheCleanIntervalInMillis * 2, TimeUnit.MILLISECONDS);
// sleep until cache cleaner would have cleaned up the stale key from index 2
}
// when staleness threshold is equal to staleness, it should clean the stale keys from cache
public void testCacheCleanupOnEqualStalenessAndThreshold() throws Exception {
int cacheCleanIntervalInMillis = 1;
String node = internalCluster().startNode(
Settings.builder()
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_CLEANUP_STALENESS_THRESHOLD_SETTING_KEY, 0.33)
.put(
IndicesRequestCache.INDICES_REQUEST_CACHE_CLEANUP_INTERVAL_SETTING_KEY,
TimeValue.timeValueMillis(cacheCleanIntervalInMillis)
)
);
Client client = client(node);
String index1 = "index1";
String index2 = "index2";
setupIndex(client, index1);
setupIndex(client, index2);
// create first cache entry in index1
createCacheEntry(client, index1, "hello");
assertCacheState(client, index1, 0, 1);
long memorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
assertTrue(memorySizeForIndex1 > 0);
// create second cache entry in index1
createCacheEntry(client, index1, "there");
assertCacheState(client, index1, 0, 2);
long finalMemorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
assertTrue(finalMemorySizeForIndex1 > memorySizeForIndex1);
// create first cache entry in index2
createCacheEntry(client, index2, "hello");
assertCacheState(client, index2, 0, 1);
assertTrue(getRequestCacheStats(client, index2).getMemorySizeInBytes() > 0);
// force refresh so that it creates 1 stale key
flushAndRefresh(index2);
// sleep until cache cleaner would have cleaned up the stale key from index 2
assertBusy(() -> {
// cache cleaner should have cleaned up the stale key from index 2
assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes());
// cache cleaner should NOT have cleaned from index 1
assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes());
}, cacheCleanIntervalInMillis * 2, TimeUnit.MILLISECONDS);
}
// when staleness threshold is higher than staleness, it should NOT clean the cache
public void testCacheCleanupSkipsWithHighStalenessThreshold() throws Exception {
int cacheCleanIntervalInMillis = 1;
String node = internalCluster().startNode(
Settings.builder()
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_CLEANUP_STALENESS_THRESHOLD_SETTING_KEY, 0.90)
.put(
IndicesRequestCache.INDICES_REQUEST_CACHE_CLEANUP_INTERVAL_SETTING_KEY,
TimeValue.timeValueMillis(cacheCleanIntervalInMillis)
)
);
Client client = client(node);
String index1 = "index1";
String index2 = "index2";
setupIndex(client, index1);
setupIndex(client, index2);
// create first cache entry in index1
createCacheEntry(client, index1, "hello");
assertCacheState(client, index1, 0, 1);
long memorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
assertTrue(memorySizeForIndex1 > 0);
// create second cache entry in index1
createCacheEntry(client, index1, "there");
assertCacheState(client, index1, 0, 2);
long finalMemorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
assertTrue(finalMemorySizeForIndex1 > memorySizeForIndex1);
// create first cache entry in index2
createCacheEntry(client, index2, "hello");
assertCacheState(client, index2, 0, 1);
assertTrue(getRequestCacheStats(client, index2).getMemorySizeInBytes() > 0);
// force refresh so that it creates 1 stale key
flushAndRefresh(index2);
// sleep until cache cleaner would have cleaned up the stale key from index 2
assertBusy(() -> {
// cache cleaner should NOT have cleaned up the stale key from index 2
assertTrue(getRequestCacheStats(client, index2).getMemorySizeInBytes() > 0);
// cache cleaner should NOT have cleaned from index 1
assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes());
}, cacheCleanIntervalInMillis * 2, TimeUnit.MILLISECONDS);
}
// when staleness threshold is explicitly set to 0, cache cleaner regularly cleans up stale keys.
public void testCacheCleanupOnZeroStalenessThreshold() throws Exception {
int cacheCleanIntervalInMillis = 50;
String node = internalCluster().startNode(
Settings.builder()
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_CLEANUP_STALENESS_THRESHOLD_SETTING_KEY, 0)
.put(
IndicesRequestCache.INDICES_REQUEST_CACHE_CLEANUP_INTERVAL_SETTING_KEY,
TimeValue.timeValueMillis(cacheCleanIntervalInMillis)
)
);
Client client = client(node);
String index1 = "index1";
String index2 = "index2";
setupIndex(client, index1);
setupIndex(client, index2);
// create 10 index1 cache entries
for (int i = 1; i <= 10; i++) {
long cacheSizeBefore = getRequestCacheStats(client, index1).getMemorySizeInBytes();
createCacheEntry(client, index1, "hello" + i);
assertCacheState(client, index1, 0, i);
long cacheSizeAfter = getRequestCacheStats(client, index1).getMemorySizeInBytes();
assertTrue(cacheSizeAfter > cacheSizeBefore);
}
long finalMemorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
// create first cache entry in index2
createCacheEntry(client, index2, "hello");
assertCacheState(client, index2, 0, 1);
assertTrue(getRequestCacheStats(client, index2).getMemorySizeInBytes() > 0);
// force refresh so that it creates 1 stale key
flushAndRefresh(index2);
// sleep until cache cleaner would have cleaned up the stale key from index 2
assertBusy(() -> {
// cache cleaner should have cleaned up the stale key from index 2
assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes());
// cache cleaner should NOT have cleaned from index 1
assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes());
}, cacheCleanIntervalInMillis * 2, TimeUnit.MILLISECONDS);
}
// when staleness threshold is not explicitly set, cache cleaner regularly cleans up stale keys
public void testStaleKeysRemovalWithoutExplicitThreshold() throws Exception {
int cacheCleanIntervalInMillis = 1;
String node = internalCluster().startNode(
Settings.builder()
.put(
IndicesRequestCache.INDICES_REQUEST_CACHE_CLEANUP_INTERVAL_SETTING_KEY,
TimeValue.timeValueMillis(cacheCleanIntervalInMillis)
)
);
String index1 = "index1";
String index2 = "index2";
Client client = client(node);
setupIndex(client, index1);
setupIndex(client, index2);
// create first cache entry in index1
createCacheEntry(client, index1, "hello");
assertCacheState(client, index1, 0, 1);
long memorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
assertTrue(memorySizeForIndex1 > 0);
// create second cache entry in index1
createCacheEntry(client, index1, "there");
assertCacheState(client, index1, 0, 2);
long finalMemorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
assertTrue(finalMemorySizeForIndex1 > memorySizeForIndex1);
// create first cache entry in index2
createCacheEntry(client, index2, "hello");
assertCacheState(client, index2, 0, 1);
assertTrue(getRequestCacheStats(client, index2).getMemorySizeInBytes() > 0);
// force refresh so that it creates 1 stale key
flushAndRefresh(index2);
// sleep until cache cleaner would have cleaned up the stale key from index 2
assertBusy(() -> {
// cache cleaner should have cleaned up the stale key from index 2
assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes());
// cache cleaner should NOT have cleaned from index 1
assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes());
}, cacheCleanIntervalInMillis * 2, TimeUnit.MILLISECONDS);
}
// when cache cleaner interval setting is not set, cache cleaner is configured appropriately with the fall-back setting
public void testCacheCleanupWithDefaultSettings() throws Exception {
int cacheCleanIntervalInMillis = 1;
String node = internalCluster().startNode(
Settings.builder().put(INDICES_CACHE_CLEANUP_INTERVAL_SETTING_KEY, TimeValue.timeValueMillis(cacheCleanIntervalInMillis))
);
Client client = client(node);
String index1 = "index1";
String index2 = "index2";
setupIndex(client, index1);
setupIndex(client, index2);
// create first cache entry in index1
createCacheEntry(client, index1, "hello");
assertCacheState(client, index1, 0, 1);
long memorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
assertTrue(memorySizeForIndex1 > 0);
// create second cache entry in index1
createCacheEntry(client, index1, "there");
assertCacheState(client, index1, 0, 2);
long finalMemorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
assertTrue(finalMemorySizeForIndex1 > memorySizeForIndex1);
// create first cache entry in index2
createCacheEntry(client, index2, "hello");
assertCacheState(client, index2, 0, 1);