Skip to content

Commit 291f686

Browse files
opensearch-trigger-bot[bot]github-actions[bot]Peter Alfonsi
authored
Fix flaky test CacheStatsAPIIndicesRequestCacheIT.testNullLevels() (#13457) (#13475)
* Fix flaky test * Initialize CommonStatsFlags with empty array for levels * Fixes tests using incorrect null levels * rerun --------- (cherry picked from commit 5d61ac2) Signed-off-by: Peter Alfonsi <petealft@amazon.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Peter Alfonsi <petealft@amazon.com>
1 parent 068722f commit 291f686

File tree

8 files changed

+29
-21
lines changed

8 files changed

+29
-21
lines changed

plugins/cache-ehcache/src/test/java/org/opensearch/cache/store/disk/EhCacheDiskCacheTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -829,15 +829,16 @@ public void testInvalidateWithDropDimensions() throws Exception {
829829

830830
ICacheKey<String> keyToDrop = keysAdded.get(0);
831831

832-
ImmutableCacheStats snapshot = ehCacheDiskCachingTier.stats().getStatsForDimensionValues(keyToDrop.dimensions);
832+
String[] levels = dimensionNames.toArray(new String[0]);
833+
ImmutableCacheStats snapshot = ehCacheDiskCachingTier.stats(levels).getStatsForDimensionValues(keyToDrop.dimensions);
833834
assertNotNull(snapshot);
834835

835836
keyToDrop.setDropStatsForDimensions(true);
836837
ehCacheDiskCachingTier.invalidate(keyToDrop);
837838

838839
// Now assert the stats are gone for any key that has this combination of dimensions, but still there otherwise
839840
for (ICacheKey<String> keyAdded : keysAdded) {
840-
snapshot = ehCacheDiskCachingTier.stats().getStatsForDimensionValues(keyAdded.dimensions);
841+
snapshot = ehCacheDiskCachingTier.stats(levels).getStatsForDimensionValues(keyAdded.dimensions);
841842
if (keyAdded.dimensions.equals(keyToDrop.dimensions)) {
842843
assertNull(snapshot);
843844
} else {

server/src/main/java/org/opensearch/action/admin/indices/stats/CommonStatsFlags.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class CommonStatsFlags implements Writeable, Cloneable {
6767
private boolean includeOnlyTopIndexingPressureMetrics = false;
6868
// Used for metric CACHE_STATS, to determine which caches to report stats for
6969
private EnumSet<CacheType> includeCaches = EnumSet.noneOf(CacheType.class);
70-
private String[] levels;
70+
private String[] levels = new String[0];
7171

7272
/**
7373
* @param flags flags to set. If no flags are supplied, default flags will be set.
@@ -150,7 +150,7 @@ public CommonStatsFlags all() {
150150
includeAllShardIndexingPressureTrackers = false;
151151
includeOnlyTopIndexingPressureMetrics = false;
152152
includeCaches = EnumSet.noneOf(CacheType.class);
153-
levels = null;
153+
levels = new String[0];
154154
return this;
155155
}
156156

@@ -167,7 +167,7 @@ public CommonStatsFlags clear() {
167167
includeAllShardIndexingPressureTrackers = false;
168168
includeOnlyTopIndexingPressureMetrics = false;
169169
includeCaches = EnumSet.noneOf(CacheType.class);
170-
levels = null;
170+
levels = new String[0];
171171
return this;
172172
}
173173

server/src/main/java/org/opensearch/common/cache/ICache.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public interface ICache<K, V> extends Closeable {
4545

4646
void refresh();
4747

48-
// Return all stats without aggregation.
48+
// Return total stats only
4949
default ImmutableCacheStatsHolder stats() {
5050
return stats(null);
5151
}
5252

53-
// Return stats aggregated by the provided levels. If levels is null, do not aggregate and return all stats.
53+
// Return stats aggregated by the provided levels. If levels is null or an empty array, return total stats only.
5454
ImmutableCacheStatsHolder stats(String[] levels);
5555

5656
/**

server/src/main/java/org/opensearch/common/cache/stats/DefaultCacheStatsHolder.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.HashMap;
1313
import java.util.List;
1414
import java.util.Map;
15+
import java.util.Objects;
1516
import java.util.concurrent.ConcurrentHashMap;
1617
import java.util.concurrent.locks.Lock;
1718
import java.util.concurrent.locks.ReentrantLock;
@@ -170,7 +171,8 @@ private boolean internalIncrementHelper(
170171
*/
171172
@Override
172173
public ImmutableCacheStatsHolder getImmutableCacheStatsHolder(String[] levels) {
173-
return new ImmutableCacheStatsHolder(this.statsRoot, levels, dimensionNames, storeName);
174+
String[] nonNullLevels = Objects.requireNonNullElseGet(levels, () -> new String[0]);
175+
return new ImmutableCacheStatsHolder(this.statsRoot, nonNullLevels, dimensionNames, storeName);
174176
}
175177

176178
@Override

server/src/main/java/org/opensearch/indices/IndicesRequestCache.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -827,8 +827,8 @@ long count() {
827827
/**
828828
* Returns the current cache stats. Pkg-private for testing.
829829
*/
830-
ImmutableCacheStatsHolder stats() {
831-
return cache.stats();
830+
ImmutableCacheStatsHolder stats(String[] levels) {
831+
return cache.stats(levels);
832832
}
833833

834834
int numRegisteredCloseListeners() { // for testing

server/src/test/java/org/opensearch/common/cache/stats/ImmutableCacheStatsHolderTests.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ public class ImmutableCacheStatsHolderTests extends OpenSearchTestCase {
2929

3030
public void testSerialization() throws Exception {
3131
List<String> dimensionNames = List.of("dim1", "dim2", "dim3");
32+
String[] levels = dimensionNames.toArray(new String[0]);
3233
DefaultCacheStatsHolder statsHolder = new DefaultCacheStatsHolder(dimensionNames, storeName);
3334
Map<String, List<String>> usedDimensionValues = DefaultCacheStatsHolderTests.getUsedDimensionValues(statsHolder, 10);
3435
DefaultCacheStatsHolderTests.populateStats(statsHolder, usedDimensionValues, 100, 10);
35-
ImmutableCacheStatsHolder stats = statsHolder.getImmutableCacheStatsHolder(null);
36+
ImmutableCacheStatsHolder stats = statsHolder.getImmutableCacheStatsHolder(levels);
3637
assertNotEquals(0, stats.getStatsRoot().children.size());
3738

3839
BytesStreamOutput os = new BytesStreamOutput();
@@ -57,19 +58,20 @@ public void testSerialization() throws Exception {
5758

5859
public void testEquals() throws Exception {
5960
List<String> dimensionNames = List.of("dim1", "dim2", "dim3");
61+
String[] levels = dimensionNames.toArray(new String[0]);
6062
DefaultCacheStatsHolder statsHolder = new DefaultCacheStatsHolder(dimensionNames, storeName);
6163
DefaultCacheStatsHolder differentStoreNameStatsHolder = new DefaultCacheStatsHolder(dimensionNames, "nonMatchingStoreName");
6264
DefaultCacheStatsHolder nonMatchingStatsHolder = new DefaultCacheStatsHolder(dimensionNames, storeName);
6365
Map<String, List<String>> usedDimensionValues = DefaultCacheStatsHolderTests.getUsedDimensionValues(statsHolder, 10);
6466
DefaultCacheStatsHolderTests.populateStats(List.of(statsHolder, differentStoreNameStatsHolder), usedDimensionValues, 100, 10);
6567
DefaultCacheStatsHolderTests.populateStats(nonMatchingStatsHolder, usedDimensionValues, 100, 10);
66-
ImmutableCacheStatsHolder stats = statsHolder.getImmutableCacheStatsHolder(null);
68+
ImmutableCacheStatsHolder stats = statsHolder.getImmutableCacheStatsHolder(levels);
6769

68-
ImmutableCacheStatsHolder secondStats = statsHolder.getImmutableCacheStatsHolder(null);
70+
ImmutableCacheStatsHolder secondStats = statsHolder.getImmutableCacheStatsHolder(levels);
6971
assertEquals(stats, secondStats);
70-
ImmutableCacheStatsHolder nonMatchingStats = nonMatchingStatsHolder.getImmutableCacheStatsHolder(null);
72+
ImmutableCacheStatsHolder nonMatchingStats = nonMatchingStatsHolder.getImmutableCacheStatsHolder(levels);
7173
assertNotEquals(stats, nonMatchingStats);
72-
ImmutableCacheStatsHolder differentStoreNameStats = differentStoreNameStatsHolder.getImmutableCacheStatsHolder(null);
74+
ImmutableCacheStatsHolder differentStoreNameStats = differentStoreNameStatsHolder.getImmutableCacheStatsHolder(levels);
7375
assertNotEquals(stats, differentStoreNameStats);
7476
}
7577

server/src/test/java/org/opensearch/common/cache/store/OpenSearchOnHeapCacheTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,16 @@ public void testInvalidateWithDropDimensions() throws Exception {
145145
}
146146

147147
ICacheKey<String> keyToDrop = keysAdded.get(0);
148-
149-
ImmutableCacheStats snapshot = cache.stats().getStatsForDimensionValues(keyToDrop.dimensions);
148+
String[] levels = dimensionNames.toArray(new String[0]);
149+
ImmutableCacheStats snapshot = cache.stats(levels).getStatsForDimensionValues(keyToDrop.dimensions);
150150
assertNotNull(snapshot);
151151

152152
keyToDrop.setDropStatsForDimensions(true);
153153
cache.invalidate(keyToDrop);
154154

155155
// Now assert the stats are gone for any key that has this combination of dimensions, but still there otherwise
156156
for (ICacheKey<String> keyAdded : keysAdded) {
157-
snapshot = cache.stats().getStatsForDimensionValues(keyAdded.dimensions);
157+
snapshot = cache.stats(levels).getStatsForDimensionValues(keyAdded.dimensions);
158158
if (keyAdded.dimensions.equals(keyToDrop.dimensions)) {
159159
assertNull(snapshot);
160160
} else {

server/src/test/java/org/opensearch/indices/IndicesRequestCacheTests.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@
8989
import java.util.concurrent.ConcurrentMap;
9090
import java.util.concurrent.atomic.AtomicInteger;
9191

92+
import static org.opensearch.indices.IndicesRequestCache.INDEX_DIMENSION_NAME;
9293
import static org.opensearch.indices.IndicesRequestCache.INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING;
94+
import static org.opensearch.indices.IndicesRequestCache.SHARD_ID_DIMENSION_NAME;
9395
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
9496
import static org.mockito.Mockito.mock;
9597
import static org.mockito.Mockito.when;
@@ -799,6 +801,7 @@ private String getReaderCacheKeyId(DirectoryReader reader) {
799801

800802
public void testClosingIndexWipesStats() throws Exception {
801803
IndicesService indicesService = getInstanceFromNode(IndicesService.class);
804+
String[] levels = { INDEX_DIMENSION_NAME, SHARD_ID_DIMENSION_NAME };
802805
// Create two indices each with multiple shards
803806
int numShards = 3;
804807
Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numShards).build();
@@ -873,8 +876,8 @@ public void testClosingIndexWipesStats() throws Exception {
873876
ShardId shardId = indexService.getShard(i).shardId();
874877
List<String> dimensionValues = List.of(shardId.getIndexName(), shardId.toString());
875878
initialDimensionValues.add(dimensionValues);
876-
ImmutableCacheStatsHolder holder = cache.stats();
877-
ImmutableCacheStats snapshot = cache.stats().getStatsForDimensionValues(dimensionValues);
879+
ImmutableCacheStatsHolder holder = cache.stats(levels);
880+
ImmutableCacheStats snapshot = cache.stats(levels).getStatsForDimensionValues(dimensionValues);
878881
assertNotNull(snapshot);
879882
// check the values are not empty by confirming entries != 0, this should always be true since the missed value is loaded
880883
// into the cache
@@ -895,7 +898,7 @@ public void testClosingIndexWipesStats() throws Exception {
895898

896899
// Now stats for the closed index should be gone
897900
for (List<String> dimensionValues : initialDimensionValues) {
898-
ImmutableCacheStats snapshot = cache.stats().getStatsForDimensionValues(dimensionValues);
901+
ImmutableCacheStats snapshot = cache.stats(levels).getStatsForDimensionValues(dimensionValues);
899902
if (dimensionValues.get(0).equals(indexToCloseName)) {
900903
assertNull(snapshot);
901904
} else {

0 commit comments

Comments
 (0)