@@ -698,17 +698,16 @@ public void testCacheWithInvalidation() throws Exception {
698
698
assertCacheState (client , index , 1 , 2 );
699
699
}
700
700
701
- // when staleness threshold is low , it should clean the cache
701
+ // when staleness threshold is lower than staleness , it should clean the stale keys from cache
702
702
public void testStaleKeysCleanup_LowStaleThresholdShouldCleanUpStaleKeysFromCache () throws Exception {
703
-
704
703
String node = internalCluster ().startNode (
705
704
Settings .builder ()
706
705
.put (IndicesRequestCache .INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING_KEY , 0.10 )
707
706
.put (IndicesRequestCache .INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING_KEY , TimeValue .timeValueMillis (1_000 ))
708
707
);
708
+ Client client = client (node );
709
709
String index1 = "index1" ;
710
710
String index2 = "index2" ;
711
- Client client = client (node );
712
711
setupIndex (client , index1 );
713
712
setupIndex (client , index2 );
714
713
@@ -721,7 +720,8 @@ public void testStaleKeysCleanup_LowStaleThresholdShouldCleanUpStaleKeysFromCach
721
720
// create second cache entry in index1
722
721
createCacheEntry (client , index1 , "there" );
723
722
assertCacheState (client , index1 , 0 , 2 );
724
- assertTrue (getRequestCacheStats (client , index1 ).getMemorySizeInBytes () > memorySizeForIndex1 );
723
+ long finalMemorySizeForIndex1 = getRequestCacheStats (client , index1 ).getMemorySizeInBytes ();
724
+ assertTrue (finalMemorySizeForIndex1 > memorySizeForIndex1 );
725
725
726
726
// create first cache entry in index2
727
727
createCacheEntry (client , index2 , "hello" );
@@ -734,18 +734,20 @@ public void testStaleKeysCleanup_LowStaleThresholdShouldCleanUpStaleKeysFromCach
734
734
Thread .sleep (1_000 );
735
735
// cache cleaner should have cleaned up the stale key from index 2
736
736
assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
737
+ // cache cleaner should NOT have cleaned from index 1
738
+ assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
737
739
}
738
740
739
- // when staleness threshold is equal, it should clean the cache
741
+ // when staleness threshold is equal to staleness , it should clean the stale keys from cache
740
742
public void testStaleKeysCleanup_EqualThresholdAndStalenessShouldCleanUpStaleKeysFromCache () throws Exception {
741
743
String node = internalCluster ().startNode (
742
744
Settings .builder ()
743
745
.put (IndicesRequestCache .INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING_KEY , 0.33 )
744
746
.put (IndicesRequestCache .INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING_KEY , TimeValue .timeValueMillis (1_000 ))
745
747
);
748
+ Client client = client (node );
746
749
String index1 = "index1" ;
747
750
String index2 = "index2" ;
748
- Client client = client (node );
749
751
setupIndex (client , index1 );
750
752
setupIndex (client , index2 );
751
753
@@ -758,7 +760,8 @@ public void testStaleKeysCleanup_EqualThresholdAndStalenessShouldCleanUpStaleKey
758
760
// create second cache entry in index1
759
761
createCacheEntry (client , index1 , "there" );
760
762
assertCacheState (client , index1 , 0 , 2 );
761
- assertTrue (getRequestCacheStats (client , index1 ).getMemorySizeInBytes () > memorySizeForIndex1 );
763
+ long finalMemorySizeForIndex1 = getRequestCacheStats (client , index1 ).getMemorySizeInBytes ();
764
+ assertTrue (finalMemorySizeForIndex1 > memorySizeForIndex1 );
762
765
763
766
// create first cache entry in index2
764
767
createCacheEntry (client , index2 , "hello" );
@@ -771,18 +774,20 @@ public void testStaleKeysCleanup_EqualThresholdAndStalenessShouldCleanUpStaleKey
771
774
Thread .sleep (1_000 );
772
775
// cache cleaner should have cleaned up the stale key from index 2
773
776
assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
777
+ // cache cleaner should NOT have cleaned from index 1
778
+ assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
774
779
}
775
780
776
- // when staleness threshold is high , it should NOT clean the cache
781
+ // when staleness threshold is higher than staleness , it should NOT clean the cache
777
782
public void testStaleKeysCleanup_HighStaleThresholdShouldSkipCleanUp () throws Exception {
778
783
String node = internalCluster ().startNode (
779
784
Settings .builder ()
780
785
.put (IndicesRequestCache .INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING_KEY , 0.90 )
781
786
.put (IndicesRequestCache .INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING_KEY , TimeValue .timeValueMillis (1_000 ))
782
787
);
788
+ Client client = client (node );
783
789
String index1 = "index1" ;
784
790
String index2 = "index2" ;
785
- Client client = client (node );
786
791
setupIndex (client , index1 );
787
792
setupIndex (client , index2 );
788
793
@@ -795,7 +800,8 @@ public void testStaleKeysCleanup_HighStaleThresholdShouldSkipCleanUp() throws Ex
795
800
// create second cache entry in index1
796
801
createCacheEntry (client , index1 , "there" );
797
802
assertCacheState (client , index1 , 0 , 2 );
798
- assertTrue (getRequestCacheStats (client , index1 ).getMemorySizeInBytes () > memorySizeForIndex1 );
803
+ long finalMemorySizeForIndex1 = getRequestCacheStats (client , index1 ).getMemorySizeInBytes ();
804
+ assertTrue (finalMemorySizeForIndex1 > memorySizeForIndex1 );
799
805
800
806
// create first cache entry in index2
801
807
createCacheEntry (client , index2 , "hello" );
@@ -808,31 +814,33 @@ public void testStaleKeysCleanup_HighStaleThresholdShouldSkipCleanUp() throws Ex
808
814
Thread .sleep (1_000 );
809
815
// cache cleaner should NOT have cleaned up the stale key from index 2
810
816
assertTrue (getRequestCacheStats (client , index2 ).getMemorySizeInBytes () > 0 );
817
+ // cache cleaner should NOT have cleaned from index 1
818
+ assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
811
819
}
812
820
813
- // when staleness threshold is explicitly set to 0, cache cleaner regularly cleans up.
821
+ // when staleness threshold is explicitly set to 0, cache cleaner regularly cleans up stale keys .
814
822
public void testStaleKeysCleanup_ZeroStaleThresholdShouldCleanUpStaleKeysFromCache () throws Exception {
815
823
String node = internalCluster ().startNode (
816
824
Settings .builder ()
817
825
.put (IndicesRequestCache .INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING_KEY , 0 )
818
826
.put (IndicesRequestCache .INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING_KEY , TimeValue .timeValueMillis (1_000 ))
819
827
);
828
+ Client client = client (node );
820
829
String index1 = "index1" ;
821
830
String index2 = "index2" ;
822
- Client client = client (node );
823
831
setupIndex (client , index1 );
824
832
setupIndex (client , index2 );
825
833
826
834
// create first cache entry in index1
827
- createCacheEntry (client , index1 , "hello" );
828
- assertCacheState (client , index1 , 0 , 1 );
829
- long memorySizeForIndex1 = getRequestCacheStats (client , index1 ).getMemorySizeInBytes ();
830
- assertTrue (memorySizeForIndex1 > 0 );
835
+ for (int i = 1 ; i <= 10 ; i ++) {
836
+ long cacheSizeBefore = getRequestCacheStats (client , index1 ).getMemorySizeInBytes ();
837
+ createCacheEntry (client , index1 , "hello" + i );
838
+ assertCacheState (client , index1 , 0 , i );
839
+ long cacheSizeAfter = getRequestCacheStats (client , index1 ).getMemorySizeInBytes ();
840
+ assertTrue (cacheSizeAfter > cacheSizeBefore );
841
+ }
831
842
832
- // create second cache entry in index1
833
- createCacheEntry (client , index1 , "there" );
834
- assertCacheState (client , index1 , 0 , 2 );
835
- assertTrue (getRequestCacheStats (client , index1 ).getMemorySizeInBytes () > memorySizeForIndex1 );
843
+ long finalMemorySizeForIndex1 = getRequestCacheStats (client , index1 ).getMemorySizeInBytes ();
836
844
837
845
// create first cache entry in index2
838
846
createCacheEntry (client , index2 , "hello" );
@@ -845,9 +853,11 @@ public void testStaleKeysCleanup_ZeroStaleThresholdShouldCleanUpStaleKeysFromCac
845
853
Thread .sleep (1_000 );
846
854
// cache cleaner should have cleaned up the stale key from index 2
847
855
assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
856
+ // cache cleaner should NOT have cleaned from index 1
857
+ assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
848
858
}
849
859
850
- // when staleness threshold is not explicitly set, cache cleaner regularly cleans up
860
+ // when staleness threshold is not explicitly set, cache cleaner regularly cleans up stale keys
851
861
public void testStaleKeysCleanup_NoStaleThresholdShouldCleanUpStaleKeysFromCache () throws Exception {
852
862
String node = internalCluster ().startNode (
853
863
Settings .builder ().put (IndicesRequestCache .INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING_KEY , TimeValue .timeValueMillis (1_000 ))
@@ -867,7 +877,8 @@ public void testStaleKeysCleanup_NoStaleThresholdShouldCleanUpStaleKeysFromCache
867
877
// create second cache entry in index1
868
878
createCacheEntry (client , index1 , "there" );
869
879
assertCacheState (client , index1 , 0 , 2 );
870
- assertTrue (getRequestCacheStats (client , index1 ).getMemorySizeInBytes () > memorySizeForIndex1 );
880
+ long finalMemorySizeForIndex1 = getRequestCacheStats (client , index1 ).getMemorySizeInBytes ();
881
+ assertTrue (finalMemorySizeForIndex1 > memorySizeForIndex1 );
871
882
872
883
// create first cache entry in index2
873
884
createCacheEntry (client , index2 , "hello" );
@@ -880,16 +891,18 @@ public void testStaleKeysCleanup_NoStaleThresholdShouldCleanUpStaleKeysFromCache
880
891
Thread .sleep (1_000 );
881
892
// cache cleaner should have cleaned up the stale key from index 2
882
893
assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
894
+ // cache cleaner should NOT have cleaned from index 1
895
+ assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
883
896
}
884
897
885
898
// when cache cleaner interval setting is not set, cache cleaner is configured appropriately with the fall-back setting
886
899
public void testStaleKeysCleanup_NoIntervalSettingFallsBackAppropriately () throws Exception {
887
900
String node = internalCluster ().startNode (
888
901
Settings .builder ().put (INDICES_CACHE_CLEAN_INTERVAL_SETTING_KEY , TimeValue .timeValueMillis (1_000 ))
889
902
);
903
+ Client client = client (node );
890
904
String index1 = "index1" ;
891
905
String index2 = "index2" ;
892
- Client client = client (node );
893
906
setupIndex (client , index1 );
894
907
setupIndex (client , index2 );
895
908
@@ -902,7 +915,8 @@ public void testStaleKeysCleanup_NoIntervalSettingFallsBackAppropriately() throw
902
915
// create second cache entry in index1
903
916
createCacheEntry (client , index1 , "there" );
904
917
assertCacheState (client , index1 , 0 , 2 );
905
- assertTrue (getRequestCacheStats (client , index1 ).getMemorySizeInBytes () > memorySizeForIndex1 );
918
+ long finalMemorySizeForIndex1 = getRequestCacheStats (client , index1 ).getMemorySizeInBytes ();
919
+ assertTrue (finalMemorySizeForIndex1 > memorySizeForIndex1 );
906
920
907
921
// create first cache entry in index2
908
922
createCacheEntry (client , index2 , "hello" );
@@ -915,6 +929,8 @@ public void testStaleKeysCleanup_NoIntervalSettingFallsBackAppropriately() throw
915
929
Thread .sleep (1_000 );
916
930
// cache cleaner should have cleaned up the stale key from index 2
917
931
assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
932
+ // cache cleaner should NOT have cleaned from index 1
933
+ assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
918
934
}
919
935
920
936
// staleness threshold dynamic updates should take effect in cleaning
@@ -924,9 +940,9 @@ public void testStaleKeysCleanup_ThresholdUpdatesShouldTakeEffectAndCleanAppropr
924
940
.put (IndicesRequestCache .INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING_KEY , 0.90 )
925
941
.put (IndicesRequestCache .INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING_KEY , TimeValue .timeValueMillis (1_000 ))
926
942
);
943
+ Client client = client (node );
927
944
String index1 = "index1" ;
928
945
String index2 = "index2" ;
929
- Client client = client (node );
930
946
setupIndex (client , index1 );
931
947
setupIndex (client , index2 );
932
948
@@ -944,7 +960,8 @@ public void testStaleKeysCleanup_ThresholdUpdatesShouldTakeEffectAndCleanAppropr
944
960
// create first cache entry in index2
945
961
createCacheEntry (client , index2 , "hello" );
946
962
assertCacheState (client , index2 , 0 , 1 );
947
- assertTrue (getRequestCacheStats (client , index2 ).getMemorySizeInBytes () > 0 );
963
+ long finalMemorySizeForIndex1 = getRequestCacheStats (client , index1 ).getMemorySizeInBytes ();
964
+ assertTrue (finalMemorySizeForIndex1 > 0 );
948
965
949
966
// force refresh so that it creates 1 stale key
950
967
flushAndRefresh (index2 );
@@ -962,6 +979,54 @@ public void testStaleKeysCleanup_ThresholdUpdatesShouldTakeEffectAndCleanAppropr
962
979
Thread .sleep (1_000 );
963
980
// cache cleaner should have cleaned up the stale key from index 2
964
981
assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
982
+ // cache cleaner should NOT have cleaned from index 1
983
+ assertEquals (finalMemorySizeForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
984
+ }
985
+
986
+ // when staleness threshold is lower than staleness, it should clean the cache from all indices having stale keys
987
+ public void testStaleKeysCleanup_CleanUpStaleKeysDeletesAppropriatelyAcrossMultipleIndices () throws Exception {
988
+ String node = internalCluster ().startNode (
989
+ Settings .builder ()
990
+ .put (IndicesRequestCache .INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING_KEY , 0.10 )
991
+ .put (IndicesRequestCache .INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING_KEY , TimeValue .timeValueMillis (1_000 ))
992
+ );
993
+ Client client = client (node );
994
+ String index1 = "index1" ;
995
+ String index2 = "index2" ;
996
+ setupIndex (client , index1 );
997
+ setupIndex (client , index2 );
998
+
999
+ // create first cache entry in index1
1000
+ createCacheEntry (client , index1 , "hello" );
1001
+ assertCacheState (client , index1 , 0 , 1 );
1002
+ long memorySizeForIndex1 = getRequestCacheStats (client , index1 ).getMemorySizeInBytes ();
1003
+ assertTrue (memorySizeForIndex1 > 0 );
1004
+
1005
+ // create second cache entry in index1
1006
+ createCacheEntry (client , index1 , "there" );
1007
+ assertCacheState (client , index1 , 0 , 2 );
1008
+ long finalMemorySizeForIndex1 = getRequestCacheStats (client , index1 ).getMemorySizeInBytes ();
1009
+ assertTrue (finalMemorySizeForIndex1 > memorySizeForIndex1 );
1010
+
1011
+ // create first cache entry in index2
1012
+ createCacheEntry (client , index2 , "hello" );
1013
+ assertCacheState (client , index2 , 0 , 1 );
1014
+ assertTrue (getRequestCacheStats (client , index2 ).getMemorySizeInBytes () > 0 );
1015
+
1016
+ // force refresh index 1 so that it creates 2 stale keys
1017
+ flushAndRefresh (index1 );
1018
+ // create another cache entry in index 1, this should not be cleaned up.
1019
+ createCacheEntry (client , index1 , "hello" );
1020
+ // record the size of this entry
1021
+ long memorySizeOfLatestEntryForIndex1 = getRequestCacheStats (client , index1 ).getMemorySizeInBytes () - finalMemorySizeForIndex1 ;
1022
+ // force refresh index 2 so that it creates 1 stale key
1023
+ flushAndRefresh (index2 );
1024
+ // sleep until cache cleaner would have cleaned up the stale key from index 2
1025
+ Thread .sleep (1_000 );
1026
+ // cache cleaner should have cleaned up the stale key from index 2
1027
+ assertEquals (0 , getRequestCacheStats (client , index2 ).getMemorySizeInBytes ());
1028
+ // cache cleaner should have only cleaned up the stale entities
1029
+ assertEquals (memorySizeOfLatestEntryForIndex1 , getRequestCacheStats (client , index1 ).getMemorySizeInBytes ());
965
1030
}
966
1031
967
1032
private void setupIndex (Client client , String index ) throws Exception {
0 commit comments