diff --git a/server/src/internalClusterTest/java/org/opensearch/nodestats/NodeStatsIT.java b/server/src/internalClusterTest/java/org/opensearch/nodestats/NodeStatsIT.java index cf40b73d5178d..b2c834a10a13d 100644 --- a/server/src/internalClusterTest/java/org/opensearch/nodestats/NodeStatsIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/nodestats/NodeStatsIT.java @@ -399,7 +399,6 @@ public void testNodeIndicesStatsXContentWithAggregationOnNodes() { testLevels.add(MockStatsLevel.NODE); testLevels.add(MockStatsLevel.INDICES); testLevels.add(MockStatsLevel.SHARDS); - testLevels.add(MockStatsLevel.UNKNOWN); internalCluster().startNode(); ensureGreen(); @@ -431,7 +430,7 @@ public void testNodeIndicesStatsXContentWithAggregationOnNodes() { builder.startObject(); - if (!testLevel.equals(MockStatsLevel.SHARDS)){ + if (!testLevel.equals(MockStatsLevel.SHARDS)) { final XContentBuilder failedBuilder = builder; assertThrows( "Expected shard stats in response for generating [SHARDS] field", @@ -439,7 +438,9 @@ public void testNodeIndicesStatsXContentWithAggregationOnNodes() { () -> nodeStats.getIndices() .toXContent( failedBuilder, - new ToXContent.MapParams(Collections.singletonMap("level", NodeIndicesStats.StatsLevel.SHARDS.getRestName())) + new ToXContent.MapParams( + Collections.singletonMap("level", NodeIndicesStats.StatsLevel.SHARDS.getRestName()) + ) ) ); } else { @@ -470,7 +471,9 @@ public void testNodeIndicesStatsXContentWithAggregationOnNodes() { () -> nodeStats.getIndices() .toXContent( failedBuilder, - new ToXContent.MapParams(Collections.singletonMap("level", NodeIndicesStats.StatsLevel.INDICES.getRestName())) + new ToXContent.MapParams( + Collections.singletonMap("level", NodeIndicesStats.StatsLevel.INDICES.getRestName()) + ) ) ); } else { @@ -500,6 +503,42 @@ public void testNodeIndicesStatsXContentWithAggregationOnNodes() { }); } + public void testNodeIndicesStatsUnknownLevelThrowsException() { + List testLevels = new ArrayList<>(); + + testLevels.add(MockStatsLevel.UNKNOWN); + internalCluster().startNode(); + ensureGreen(); + String indexName = "test1"; + assertAcked( + prepareCreate( + indexName, + clusterService().state().getNodes().getSize(), + Settings.builder().put("number_of_shards", 2).put("number_of_replicas", clusterService().state().getNodes().getSize() - 1) + ) + ); + ensureGreen(); + + testLevels.forEach(testLevel -> { + NodesStatsResponse response; + CommonStatsFlags commonStatsFlags = new CommonStatsFlags(); + commonStatsFlags.setIncludeIndicesStatsByLevel(true); + if (!testLevel.equals(MockStatsLevel.NULL)) { + ArrayList level_arg = new ArrayList<>(); + level_arg.add(testLevel.getRestName()); + + commonStatsFlags.setLevels(level_arg.toArray(new String[0])); + } + response = client().admin().cluster().prepareNodesStats().setIndices(commonStatsFlags).get(); + + assertTrue(response.hasFailures()); + assertEquals( + "Level provided is not supported by NodeIndicesStats", + response.failures().get(0).getCause().getCause().getMessage() + ); + }); + } + private Map xContentBuilderToMap(XContentBuilder xContentBuilder) { return XContentHelper.convertToMap(BytesReference.bytes(xContentBuilder), true, xContentBuilder.contentType()).v2(); } diff --git a/server/src/main/java/org/opensearch/indices/NodeIndicesStats.java b/server/src/main/java/org/opensearch/indices/NodeIndicesStats.java index ad786d236f3ce..4a5e216b514ae 100644 --- a/server/src/main/java/org/opensearch/indices/NodeIndicesStats.java +++ b/server/src/main/java/org/opensearch/indices/NodeIndicesStats.java @@ -61,17 +61,14 @@ import org.opensearch.index.translog.TranslogStats; import org.opensearch.index.warmer.WarmerStats; import org.opensearch.search.suggest.completion.CompletionStats; -import org.opensearch.transport.TransportException; import java.io.IOException; -import java.rmi.UnexpectedException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.stream.Collectors; /** * Global information on indices stats running on a specific node. @@ -171,7 +168,7 @@ private static StatsLevel getAcceptedLevel(String[] levels) { Optional level = Arrays.stream(StatsLevel.values()) .filter(field -> field.getRestName().equals(levels[0])) .findFirst(); - return level.orElse(null); + return level.orElseThrow(() -> new IllegalArgumentException("Level provided is not supported by NodeIndicesStats")); } return null; } @@ -344,7 +341,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws stats.toXContent(builder, params); if (StatsLevel.INDICES.getRestName().equals(level)) { - assert statsByIndex!=null || statsByShard!=null: "Expected shard stats or index stats in response for generating [" + StatsLevel.INDICES + "] field"; + assert statsByIndex != null || statsByShard != null : "Expected shard stats or index stats in response for generating [" + + StatsLevel.INDICES + + "] field"; if (statsByIndex == null) { statsByIndex = createStatsByIndex(statsByShard); } @@ -360,7 +359,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.endObject(); } else if (StatsLevel.SHARDS.getRestName().equals(level)) { builder.startObject(StatsLevel.SHARDS.getRestName()); - assert statsByShard != null: "Expected shard stats in response for generating [" + StatsLevel.SHARDS + "] field"; + assert statsByShard != null : "Expected shard stats in response for generating [" + StatsLevel.SHARDS + "] field"; for (Map.Entry> entry : statsByShard.entrySet()) { builder.startArray(entry.getKey().getName()); for (IndexShardStats indexShardStats : entry.getValue()) {