Skip to content

Commit

Permalink
Fix Failing Tests
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 12, 2024
1 parent 857e7e1 commit 9876c13
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -290,37 +297,38 @@ public void testNodeIndicesStatsDefaultResponse() {
builder.endObject();

Map<String, Object> 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);
Expand All @@ -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";
Expand All @@ -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<String, Object> 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) {
Expand Down
25 changes: 19 additions & 6 deletions server/src/main/java/org/opensearch/indices/NodeIndicesStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Index, List<IndexShardStats>> entry : statsByShard.entrySet()) {
builder.startArray(entry.getKey().getName());
Expand Down Expand Up @@ -372,5 +383,7 @@ public List<IndexShardStats> 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";

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 9876c13

Please sign in to comment.