Skip to content

Commit cc0a9c9

Browse files
Update IndicesRequestCacheIT.java
Signed-off-by: Kiran Prakash <awskiran@amazon.com>
1 parent f8e149a commit cc0a9c9

File tree

1 file changed

+91
-26
lines changed

1 file changed

+91
-26
lines changed

server/src/internalClusterTest/java/org/opensearch/indices/IndicesRequestCacheIT.java

+91-26
Original file line numberDiff line numberDiff line change
@@ -698,17 +698,16 @@ public void testCacheWithInvalidation() throws Exception {
698698
assertCacheState(client, index, 1, 2);
699699
}
700700

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
702702
public void testStaleKeysCleanup_LowStaleThresholdShouldCleanUpStaleKeysFromCache() throws Exception {
703-
704703
String node = internalCluster().startNode(
705704
Settings.builder()
706705
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING_KEY, 0.10)
707706
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING_KEY, TimeValue.timeValueMillis(1_000))
708707
);
708+
Client client = client(node);
709709
String index1 = "index1";
710710
String index2 = "index2";
711-
Client client = client(node);
712711
setupIndex(client, index1);
713712
setupIndex(client, index2);
714713

@@ -721,7 +720,8 @@ public void testStaleKeysCleanup_LowStaleThresholdShouldCleanUpStaleKeysFromCach
721720
// create second cache entry in index1
722721
createCacheEntry(client, index1, "there");
723722
assertCacheState(client, index1, 0, 2);
724-
assertTrue(getRequestCacheStats(client, index1).getMemorySizeInBytes() > memorySizeForIndex1);
723+
long finalMemorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
724+
assertTrue(finalMemorySizeForIndex1 > memorySizeForIndex1);
725725

726726
// create first cache entry in index2
727727
createCacheEntry(client, index2, "hello");
@@ -734,18 +734,20 @@ public void testStaleKeysCleanup_LowStaleThresholdShouldCleanUpStaleKeysFromCach
734734
Thread.sleep(1_000);
735735
// cache cleaner should have cleaned up the stale key from index 2
736736
assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes());
737+
// cache cleaner should NOT have cleaned from index 1
738+
assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes());
737739
}
738740

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
740742
public void testStaleKeysCleanup_EqualThresholdAndStalenessShouldCleanUpStaleKeysFromCache() throws Exception {
741743
String node = internalCluster().startNode(
742744
Settings.builder()
743745
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING_KEY, 0.33)
744746
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING_KEY, TimeValue.timeValueMillis(1_000))
745747
);
748+
Client client = client(node);
746749
String index1 = "index1";
747750
String index2 = "index2";
748-
Client client = client(node);
749751
setupIndex(client, index1);
750752
setupIndex(client, index2);
751753

@@ -758,7 +760,8 @@ public void testStaleKeysCleanup_EqualThresholdAndStalenessShouldCleanUpStaleKey
758760
// create second cache entry in index1
759761
createCacheEntry(client, index1, "there");
760762
assertCacheState(client, index1, 0, 2);
761-
assertTrue(getRequestCacheStats(client, index1).getMemorySizeInBytes() > memorySizeForIndex1);
763+
long finalMemorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
764+
assertTrue(finalMemorySizeForIndex1 > memorySizeForIndex1);
762765

763766
// create first cache entry in index2
764767
createCacheEntry(client, index2, "hello");
@@ -771,18 +774,20 @@ public void testStaleKeysCleanup_EqualThresholdAndStalenessShouldCleanUpStaleKey
771774
Thread.sleep(1_000);
772775
// cache cleaner should have cleaned up the stale key from index 2
773776
assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes());
777+
// cache cleaner should NOT have cleaned from index 1
778+
assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes());
774779
}
775780

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
777782
public void testStaleKeysCleanup_HighStaleThresholdShouldSkipCleanUp() throws Exception {
778783
String node = internalCluster().startNode(
779784
Settings.builder()
780785
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING_KEY, 0.90)
781786
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING_KEY, TimeValue.timeValueMillis(1_000))
782787
);
788+
Client client = client(node);
783789
String index1 = "index1";
784790
String index2 = "index2";
785-
Client client = client(node);
786791
setupIndex(client, index1);
787792
setupIndex(client, index2);
788793

@@ -795,7 +800,8 @@ public void testStaleKeysCleanup_HighStaleThresholdShouldSkipCleanUp() throws Ex
795800
// create second cache entry in index1
796801
createCacheEntry(client, index1, "there");
797802
assertCacheState(client, index1, 0, 2);
798-
assertTrue(getRequestCacheStats(client, index1).getMemorySizeInBytes() > memorySizeForIndex1);
803+
long finalMemorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
804+
assertTrue(finalMemorySizeForIndex1 > memorySizeForIndex1);
799805

800806
// create first cache entry in index2
801807
createCacheEntry(client, index2, "hello");
@@ -808,31 +814,33 @@ public void testStaleKeysCleanup_HighStaleThresholdShouldSkipCleanUp() throws Ex
808814
Thread.sleep(1_000);
809815
// cache cleaner should NOT have cleaned up the stale key from index 2
810816
assertTrue(getRequestCacheStats(client, index2).getMemorySizeInBytes() > 0);
817+
// cache cleaner should NOT have cleaned from index 1
818+
assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes());
811819
}
812820

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.
814822
public void testStaleKeysCleanup_ZeroStaleThresholdShouldCleanUpStaleKeysFromCache() throws Exception {
815823
String node = internalCluster().startNode(
816824
Settings.builder()
817825
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING_KEY, 0)
818826
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING_KEY, TimeValue.timeValueMillis(1_000))
819827
);
828+
Client client = client(node);
820829
String index1 = "index1";
821830
String index2 = "index2";
822-
Client client = client(node);
823831
setupIndex(client, index1);
824832
setupIndex(client, index2);
825833

826834
// 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+
}
831842

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();
836844

837845
// create first cache entry in index2
838846
createCacheEntry(client, index2, "hello");
@@ -845,9 +853,11 @@ public void testStaleKeysCleanup_ZeroStaleThresholdShouldCleanUpStaleKeysFromCac
845853
Thread.sleep(1_000);
846854
// cache cleaner should have cleaned up the stale key from index 2
847855
assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes());
856+
// cache cleaner should NOT have cleaned from index 1
857+
assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes());
848858
}
849859

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
851861
public void testStaleKeysCleanup_NoStaleThresholdShouldCleanUpStaleKeysFromCache() throws Exception {
852862
String node = internalCluster().startNode(
853863
Settings.builder().put(IndicesRequestCache.INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING_KEY, TimeValue.timeValueMillis(1_000))
@@ -867,7 +877,8 @@ public void testStaleKeysCleanup_NoStaleThresholdShouldCleanUpStaleKeysFromCache
867877
// create second cache entry in index1
868878
createCacheEntry(client, index1, "there");
869879
assertCacheState(client, index1, 0, 2);
870-
assertTrue(getRequestCacheStats(client, index1).getMemorySizeInBytes() > memorySizeForIndex1);
880+
long finalMemorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
881+
assertTrue(finalMemorySizeForIndex1 > memorySizeForIndex1);
871882

872883
// create first cache entry in index2
873884
createCacheEntry(client, index2, "hello");
@@ -880,16 +891,18 @@ public void testStaleKeysCleanup_NoStaleThresholdShouldCleanUpStaleKeysFromCache
880891
Thread.sleep(1_000);
881892
// cache cleaner should have cleaned up the stale key from index 2
882893
assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes());
894+
// cache cleaner should NOT have cleaned from index 1
895+
assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes());
883896
}
884897

885898
// when cache cleaner interval setting is not set, cache cleaner is configured appropriately with the fall-back setting
886899
public void testStaleKeysCleanup_NoIntervalSettingFallsBackAppropriately() throws Exception {
887900
String node = internalCluster().startNode(
888901
Settings.builder().put(INDICES_CACHE_CLEAN_INTERVAL_SETTING_KEY, TimeValue.timeValueMillis(1_000))
889902
);
903+
Client client = client(node);
890904
String index1 = "index1";
891905
String index2 = "index2";
892-
Client client = client(node);
893906
setupIndex(client, index1);
894907
setupIndex(client, index2);
895908

@@ -902,7 +915,8 @@ public void testStaleKeysCleanup_NoIntervalSettingFallsBackAppropriately() throw
902915
// create second cache entry in index1
903916
createCacheEntry(client, index1, "there");
904917
assertCacheState(client, index1, 0, 2);
905-
assertTrue(getRequestCacheStats(client, index1).getMemorySizeInBytes() > memorySizeForIndex1);
918+
long finalMemorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
919+
assertTrue(finalMemorySizeForIndex1 > memorySizeForIndex1);
906920

907921
// create first cache entry in index2
908922
createCacheEntry(client, index2, "hello");
@@ -915,6 +929,8 @@ public void testStaleKeysCleanup_NoIntervalSettingFallsBackAppropriately() throw
915929
Thread.sleep(1_000);
916930
// cache cleaner should have cleaned up the stale key from index 2
917931
assertEquals(0, getRequestCacheStats(client, index2).getMemorySizeInBytes());
932+
// cache cleaner should NOT have cleaned from index 1
933+
assertEquals(finalMemorySizeForIndex1, getRequestCacheStats(client, index1).getMemorySizeInBytes());
918934
}
919935

920936
// staleness threshold dynamic updates should take effect in cleaning
@@ -924,9 +940,9 @@ public void testStaleKeysCleanup_ThresholdUpdatesShouldTakeEffectAndCleanAppropr
924940
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING_KEY, 0.90)
925941
.put(IndicesRequestCache.INDICES_REQUEST_CACHE_CLEAN_INTERVAL_SETTING_KEY, TimeValue.timeValueMillis(1_000))
926942
);
943+
Client client = client(node);
927944
String index1 = "index1";
928945
String index2 = "index2";
929-
Client client = client(node);
930946
setupIndex(client, index1);
931947
setupIndex(client, index2);
932948

@@ -944,7 +960,8 @@ public void testStaleKeysCleanup_ThresholdUpdatesShouldTakeEffectAndCleanAppropr
944960
// create first cache entry in index2
945961
createCacheEntry(client, index2, "hello");
946962
assertCacheState(client, index2, 0, 1);
947-
assertTrue(getRequestCacheStats(client, index2).getMemorySizeInBytes() > 0);
963+
long finalMemorySizeForIndex1 = getRequestCacheStats(client, index1).getMemorySizeInBytes();
964+
assertTrue(finalMemorySizeForIndex1 > 0);
948965

949966
// force refresh so that it creates 1 stale key
950967
flushAndRefresh(index2);
@@ -962,6 +979,54 @@ public void testStaleKeysCleanup_ThresholdUpdatesShouldTakeEffectAndCleanAppropr
962979
Thread.sleep(1_000);
963980
// cache cleaner should have cleaned up the stale key from index 2
964981
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());
9651030
}
9661031

9671032
private void setupIndex(Client client, String index) throws Exception {

0 commit comments

Comments
 (0)