2020import org .opensearch .common .cache .stats .ImmutableCacheStatsHolder ;
2121import org .opensearch .common .settings .Settings ;
2222import org .opensearch .common .unit .TimeValue ;
23+ import org .opensearch .index .IndexSettings ;
2324import org .opensearch .index .cache .request .RequestCacheStats ;
2425import org .opensearch .index .query .QueryBuilders ;
2526import org .opensearch .indices .IndicesRequestCache ;
@@ -102,8 +103,8 @@ public void testIndicesLevelAggregation() throws Exception {
102103 );
103104
104105 for (ImmutableCacheStatsHolder statsHolder : List .of (allLevelsStatsHolder , indicesOnlyStatsHolder )) {
105- assertEquals (index1ExpectedStats , statsHolder .getStatsForDimensionValues (List .of (index1Name )));
106- assertEquals (index2ExpectedStats , statsHolder .getStatsForDimensionValues (List .of (index2Name )));
106+ assertStatsEqual (index1ExpectedStats , statsHolder .getStatsForDimensionValues (List .of (index1Name )));
107+ assertStatsEqual (index2ExpectedStats , statsHolder .getStatsForDimensionValues (List .of (index2Name )));
107108 }
108109 }
109110
@@ -139,7 +140,7 @@ public void testIndicesAndTierLevelAggregation() throws Exception {
139140 values .get ("itemsOnHeapIndex1AfterTest" )
140141 )
141142 );
142- assertEquals (
143+ assertStatsEqual (
143144 index1HeapExpectedStats ,
144145 allLevelsStatsHolder .getStatsForDimensionValues (List .of (index1Name , TIER_DIMENSION_VALUE_ON_HEAP ))
145146 );
@@ -153,7 +154,7 @@ public void testIndicesAndTierLevelAggregation() throws Exception {
153154 values .get ("itemsOnHeapIndex2AfterTest" )
154155 )
155156 );
156- assertEquals (
157+ assertStatsEqual (
157158 index2HeapExpectedStats ,
158159 allLevelsStatsHolder .getStatsForDimensionValues (List .of (index2Name , TIER_DIMENSION_VALUE_ON_HEAP ))
159160 );
@@ -167,7 +168,7 @@ public void testIndicesAndTierLevelAggregation() throws Exception {
167168 values .get ("itemsOnDiskIndex1AfterTest" )
168169 )
169170 );
170- assertEquals (
171+ assertStatsEqual (
171172 index1DiskExpectedStats ,
172173 allLevelsStatsHolder .getStatsForDimensionValues (List .of (index1Name , TIER_DIMENSION_VALUE_DISK ))
173174 );
@@ -181,7 +182,7 @@ public void testIndicesAndTierLevelAggregation() throws Exception {
181182 values .get ("itemsOnDiskIndex2AfterTest" )
182183 )
183184 );
184- assertEquals (
185+ assertStatsEqual (
185186 index2DiskExpectedStats ,
186187 allLevelsStatsHolder .getStatsForDimensionValues (List .of (index2Name , TIER_DIMENSION_VALUE_DISK ))
187188 );
@@ -218,7 +219,7 @@ public void testTierLevelAggregation() throws Exception {
218219 )
219220 );
220221 ImmutableCacheStats heapStats = tiersOnlyStatsHolder .getStatsForDimensionValues (List .of (TIER_DIMENSION_VALUE_ON_HEAP ));
221- assertEquals (totalHeapExpectedStats , heapStats );
222+ assertStatsEqual (totalHeapExpectedStats , heapStats );
222223 ImmutableCacheStats totalDiskExpectedStats = returnNullIfAllZero (
223224 new ImmutableCacheStats (
224225 values .get ("hitsOnDiskIndex1" ) + values .get ("hitsOnDiskIndex2" ),
@@ -229,7 +230,7 @@ public void testTierLevelAggregation() throws Exception {
229230 )
230231 );
231232 ImmutableCacheStats diskStats = tiersOnlyStatsHolder .getStatsForDimensionValues (List .of (TIER_DIMENSION_VALUE_DISK ));
232- assertEquals (totalDiskExpectedStats , diskStats );
233+ assertStatsEqual (totalDiskExpectedStats , diskStats );
233234 }
234235
235236 public void testInvalidLevelsAreIgnored () throws Exception {
@@ -322,7 +323,7 @@ public void testStatsMatchOldApi() throws Exception {
322323 ImmutableCacheStats totalStats = getNodeCacheStatsResult (client , List .of ()).getTotalStats ();
323324
324325 // Check the new stats API values are as expected
325- assertEquals (
326+ assertStatsEqual (
326327 new ImmutableCacheStats (expectedHits , expectedMisses , 0 , expectedEntries * singleSearchSize , expectedEntries ),
327328 totalStats
328329 );
@@ -351,6 +352,8 @@ private void startIndex(Client client, String indexName) throws InterruptedExcep
351352 .put (IndicesRequestCache .INDEX_CACHE_REQUEST_ENABLED_SETTING .getKey (), true )
352353 .put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 )
353354 .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 0 )
355+ // Disable index refreshing to avoid cache being invalidated mid-test
356+ .put (IndexSettings .INDEX_REFRESH_INTERVAL_SETTING .getKey (), TimeValue .timeValueMillis (-1 ))
354357 .build ()
355358 )
356359 .get ()
@@ -498,4 +501,13 @@ private static ImmutableCacheStatsHolder getNodeCacheStatsResult(Client client,
498501 NodeCacheStats ncs = nodeStatsResponse .getNodes ().get (0 ).getNodeCacheStats ();
499502 return ncs .getStatsByCache (CacheType .INDICES_REQUEST_CACHE );
500503 }
504+
505+ // Check each stat separately for more transparency if there's a failure
506+ private void assertStatsEqual (ImmutableCacheStats expected , ImmutableCacheStats actual ) {
507+ assertEquals (expected .getHits (), actual .getHits ());
508+ assertEquals (expected .getMisses (), actual .getMisses ());
509+ assertEquals (expected .getEvictions (), actual .getEvictions ());
510+ assertEquals (expected .getSizeInBytes (), actual .getSizeInBytes ());
511+ assertEquals (expected .getItems (), actual .getItems ());
512+ }
501513}
0 commit comments