Skip to content

Commit

Permalink
Add Node Level Validations
Browse files Browse the repository at this point in the history
Signed-off-by: Pranshu Shukla <[email protected]>
  • Loading branch information
Pranshu-S committed Jul 23, 2024
1 parent a0dff3f commit ff66431
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -431,15 +430,17 @@ 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",
AssertionError.class,
() -> 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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -500,6 +503,42 @@ public void testNodeIndicesStatsXContentWithAggregationOnNodes() {
});
}

public void testNodeIndicesStatsUnknownLevelThrowsException() {
List<MockStatsLevel> 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<String> 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<String, Object> xContentBuilderToMap(XContentBuilder xContentBuilder) {
return XContentHelper.convertToMap(BytesReference.bytes(xContentBuilder), true, xContentBuilder.contentType()).v2();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -171,7 +168,7 @@ private static StatsLevel getAcceptedLevel(String[] levels) {
Optional<StatsLevel> 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;
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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<Index, List<IndexShardStats>> entry : statsByShard.entrySet()) {
builder.startArray(entry.getKey().getName());
for (IndexShardStats indexShardStats : entry.getValue()) {
Expand Down

0 comments on commit ff66431

Please sign in to comment.