diff --git a/server/src/internalClusterTest/java/org/opensearch/nodestats/NodeStatsIT.java b/server/src/internalClusterTest/java/org/opensearch/nodestats/NodeStatsIT.java index 08f09a1421fd7..bbb5398c89aa7 100644 --- a/server/src/internalClusterTest/java/org/opensearch/nodestats/NodeStatsIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/nodestats/NodeStatsIT.java @@ -31,6 +31,7 @@ import org.opensearch.index.engine.DocumentMissingException; import org.opensearch.index.engine.VersionConflictEngineException; import org.opensearch.index.shard.IndexingStats.Stats.DocStatusStats; +import org.opensearch.indices.NodeIndicesStats; import org.opensearch.test.OpenSearchIntegTestCase; import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope; import org.opensearch.test.OpenSearchIntegTestCase.Scope; @@ -260,7 +261,13 @@ public void testNodeIndicesStatsDocStatusStatsCreateDeleteUpdate() { * returns ShardStats which is aggregated on the coordinator node when creating the XContent. */ public void testNodeIndicesStatsDefaultResponse() { - String testLevel = randomFrom("null", "node", "indices", "shards", "unknown"); + String testLevel = randomFrom( + "null", + NodeIndicesStats.Fields.NODE, + NodeIndicesStats.Fields.INDICES, + NodeIndicesStats.Fields.SHARDS, + "unknown" + ); internalCluster().startNode(); ensureGreen(); String indexName = "test1"; @@ -290,37 +297,38 @@ public void testNodeIndicesStatsDefaultResponse() { builder.endObject(); Map xContentMap = xContentBuilderToMap(builder); - LinkedHashMap indicesStatsMap = (LinkedHashMap) xContentMap.get("indices"); - assertFalse(indicesStatsMap.containsKey("indices")); - assertFalse(indicesStatsMap.containsKey("shards")); + LinkedHashMap indicesStatsMap = (LinkedHashMap) xContentMap.get(NodeIndicesStats.Fields.INDICES); + assertFalse(indicesStatsMap.containsKey(NodeIndicesStats.Fields.INDICES)); + assertFalse(indicesStatsMap.containsKey(NodeIndicesStats.Fields.SHARDS)); // With param containing level as 'indices', the indices stats are returned builder = XContentFactory.jsonBuilder(); builder.startObject(); builder = nodeStats.getIndices() - .toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("level", "indices"))); + .toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("level", NodeIndicesStats.Fields.INDICES))); builder.endObject(); xContentMap = xContentBuilderToMap(builder); - indicesStatsMap = (LinkedHashMap) xContentMap.get("indices"); - assertTrue(indicesStatsMap.containsKey("indices")); - assertFalse(indicesStatsMap.containsKey("shards")); + indicesStatsMap = (LinkedHashMap) xContentMap.get(NodeIndicesStats.Fields.INDICES); + assertTrue(indicesStatsMap.containsKey(NodeIndicesStats.Fields.INDICES)); + assertFalse(indicesStatsMap.containsKey(NodeIndicesStats.Fields.SHARDS)); - LinkedHashMap indexLevelStats = (LinkedHashMap) indicesStatsMap.get("indices"); + LinkedHashMap indexLevelStats = (LinkedHashMap) indicesStatsMap.get(NodeIndicesStats.Fields.INDICES); assertTrue(indexLevelStats.containsKey(indexName)); // With param containing level as 'shards', the shard stats are returned builder = XContentFactory.jsonBuilder(); builder.startObject(); - builder = nodeStats.getIndices().toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("level", "shards"))); + builder = nodeStats.getIndices() + .toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("level", NodeIndicesStats.Fields.SHARDS))); builder.endObject(); xContentMap = xContentBuilderToMap(builder); - indicesStatsMap = (LinkedHashMap) xContentMap.get("indices"); - assertFalse(indicesStatsMap.containsKey("indices")); - assertTrue(indicesStatsMap.containsKey("shards")); + indicesStatsMap = (LinkedHashMap) xContentMap.get(NodeIndicesStats.Fields.INDICES); + assertFalse(indicesStatsMap.containsKey(NodeIndicesStats.Fields.INDICES)); + assertTrue(indicesStatsMap.containsKey(NodeIndicesStats.Fields.SHARDS)); - LinkedHashMap shardLevelStats = (LinkedHashMap) indicesStatsMap.get("shards"); + LinkedHashMap shardLevelStats = (LinkedHashMap) indicesStatsMap.get(NodeIndicesStats.Fields.SHARDS); assertTrue(shardLevelStats.containsKey(indexName)); } catch (IOException e) { throw new RuntimeException(e); @@ -333,7 +341,18 @@ public void testNodeIndicesStatsDefaultResponse() { * individual data nodes instead and pre-compute information as required. */ public void testNodeIndicesStatsOptimizedResponse() { - String testLevel = randomFrom("null", "node", "indices", "shards", "unknown"); + String testLevel = randomFrom( + "null", + NodeIndicesStats.Fields.NODE, + NodeIndicesStats.Fields.INDICES, + NodeIndicesStats.Fields.SHARDS, + "unknown" + ); + String testToXContentLevel = randomFrom( + NodeIndicesStats.Fields.NODE, + NodeIndicesStats.Fields.INDICES, + NodeIndicesStats.Fields.SHARDS + ); internalCluster().startNode(); ensureGreen(); String indexName = "test1"; @@ -355,28 +374,28 @@ public void testNodeIndicesStatsOptimizedResponse() { try { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); - builder = nodeStats.getIndices().toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("level", "shards"))); + builder = nodeStats.getIndices() + .toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("level", testToXContentLevel))); builder.endObject(); Map xContentMap = xContentBuilderToMap(builder); - LinkedHashMap indicesStatsMap = (LinkedHashMap) xContentMap.get("indices"); - LinkedHashMap indicesStats = (LinkedHashMap) indicesStatsMap.get("indices"); - LinkedHashMap shardStats = (LinkedHashMap) indicesStatsMap.get("shards"); - + LinkedHashMap indicesStatsMap = (LinkedHashMap) xContentMap.get(NodeIndicesStats.Fields.INDICES); + LinkedHashMap indicesStats = (LinkedHashMap) indicesStatsMap.get(NodeIndicesStats.Fields.INDICES); + LinkedHashMap shardStats = (LinkedHashMap) indicesStatsMap.get(NodeIndicesStats.Fields.SHARDS); switch (testLevel) { - case "shards": - assertFalse(shardStats.isEmpty()); - assertFalse(indicesStats.isEmpty()); + case NodeIndicesStats.Fields.SHARDS: + assertFalse(shardStats == null || shardStats.isEmpty()); + assertFalse(indicesStats == null || indicesStats.isEmpty()); break; - case "indices": - assertTrue(shardStats.isEmpty()); - assertFalse(indicesStats.isEmpty()); + case NodeIndicesStats.Fields.INDICES: + assertTrue(shardStats == null || shardStats.isEmpty()); + assertFalse(indicesStats == null || indicesStats.isEmpty()); break; - case "node": + case NodeIndicesStats.Fields.NODE: case "null": case "unknown": - assertTrue(shardStats.isEmpty()); - assertTrue(indicesStats.isEmpty()); + assertTrue(shardStats == null || shardStats.isEmpty()); + assertTrue(indicesStats == null || indicesStats.isEmpty()); break; } } catch (IOException e) { diff --git a/server/src/main/java/org/opensearch/indices/NodeIndicesStats.java b/server/src/main/java/org/opensearch/indices/NodeIndicesStats.java index a21d988695ed4..cc285f882e606 100644 --- a/server/src/main/java/org/opensearch/indices/NodeIndicesStats.java +++ b/server/src/main/java/org/opensearch/indices/NodeIndicesStats.java @@ -292,12 +292,23 @@ private void writeStatsByShards(StreamOutput out) throws IOException { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - final String level = params.param("level", "node"); - final boolean isLevelValid = "indices".equalsIgnoreCase(level) - || "node".equalsIgnoreCase(level) - || "shards".equalsIgnoreCase(level); + final String level = params.param("level", Fields.NODE); + final boolean isLevelValid = Fields.NODE.equalsIgnoreCase(level) + || Fields.INDICES.equalsIgnoreCase(level) + || Fields.SHARDS.equalsIgnoreCase(level); if (!isLevelValid) { - throw new IllegalArgumentException("level parameter must be one of [indices] or [node] or [shards] but was [" + level + "]"); + throw new IllegalArgumentException( + "level parameter must be one of [" + + Fields.INDICES + + "] or " + + "[" + + Fields.NODE + + "] or [" + + Fields.SHARDS + + "] but was [" + + level + + "]" + ); } // "node" level @@ -318,7 +329,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } builder.endObject(); } else if (Fields.SHARDS.equals(level)) { - builder.startObject("shards"); + builder.startObject(Fields.SHARDS); if (statsByShard != null) { for (Map.Entry> entry : statsByShard.entrySet()) { builder.startArray(entry.getKey().getName()); @@ -372,5 +383,7 @@ public List getShardStats(Index index) { public static final class Fields { public static final String INDICES = "indices"; public static final String SHARDS = "shards"; + public static final String NODE = "node"; + } } diff --git a/server/src/test/java/org/opensearch/action/admin/cluster/node/stats/NodeStatsTests.java b/server/src/test/java/org/opensearch/action/admin/cluster/node/stats/NodeStatsTests.java index 88db8efff41b6..010f28f498c05 100644 --- a/server/src/test/java/org/opensearch/action/admin/cluster/node/stats/NodeStatsTests.java +++ b/server/src/test/java/org/opensearch/action/admin/cluster/node/stats/NodeStatsTests.java @@ -1159,7 +1159,7 @@ public void testNodeIndicesStatsSerializationWithOldESVersionNodes() throws IOEx public void testNodeIndicesStatsSerializationOnNewVersions() throws IOException { long numDocs = randomLongBetween(0, 10000); long numDeletedDocs = randomLongBetween(0, 100); - String levelParam = randomFrom("node", "indices", "shards"); + String levelParam = randomFrom(NodeIndicesStats.Fields.INDICES, "indices", "shards"); CommonStats commonStats = new CommonStats(CommonStatsFlags.NONE);