Skip to content

Commit 150c574

Browse files
author
Peter Alfonsi
committed
Fix flaky TSC stats tests
Signed-off-by: Peter Alfonsi <[email protected]>
1 parent 23458a4 commit 150c574

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

modules/cache-common/src/internalClusterTest/java/org/opensearch/cache/common/tier/TieredSpilloverCacheStatsIT.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.opensearch.common.cache.stats.ImmutableCacheStatsHolder;
2121
import org.opensearch.common.settings.Settings;
2222
import org.opensearch.common.unit.TimeValue;
23+
import org.opensearch.index.IndexSettings;
2324
import org.opensearch.index.cache.request.RequestCacheStats;
2425
import org.opensearch.index.query.QueryBuilders;
2526
import 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
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
import org.opensearch.common.cache.stats.ImmutableCacheStats;
2424
import org.opensearch.common.cache.stats.ImmutableCacheStatsHolder;
2525
import org.opensearch.common.settings.Settings;
26+
import org.opensearch.common.unit.TimeValue;
2627
import org.opensearch.common.util.FeatureFlags;
2728
import org.opensearch.common.xcontent.XContentFactory;
2829
import org.opensearch.common.xcontent.XContentHelper;
2930
import org.opensearch.core.xcontent.MediaTypeRegistry;
3031
import org.opensearch.core.xcontent.ToXContent;
3132
import org.opensearch.core.xcontent.XContentBuilder;
33+
import org.opensearch.index.IndexSettings;
3234
import org.opensearch.index.cache.request.RequestCacheStats;
3335
import org.opensearch.index.query.QueryBuilders;
3436
import org.opensearch.test.OpenSearchIntegTestCase;
@@ -266,6 +268,8 @@ private void startIndex(Client client, String indexName) throws InterruptedExcep
266268
.put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)
267269
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
268270
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
271+
// Disable index refreshing to avoid cache being invalidated mid-test
272+
.put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), TimeValue.timeValueMillis(-1))
269273
)
270274
.get()
271275
);

0 commit comments

Comments
 (0)