Skip to content

Commit

Permalink
Refactor AggregatedIndexStats to ClusterStatsNodeResponse
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 d543d54 commit e0f6178
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public ClusterStatsIndices(List<ClusterStatsNodeResponse> nodeResponses, Mapping
// Aggregated response from the node
if (r.getAggregatedNodeLevelStats() != null) {

for (Map.Entry<String, CommonStats.AggregatedIndexStats> entry : r.getAggregatedNodeLevelStats().indexStatsMap.entrySet()) {
for (Map.Entry<String, ClusterStatsNodeResponse.AggregatedIndexStats> entry : r.getAggregatedNodeLevelStats().indexStatsMap
.entrySet()) {
ShardStats indexShardStats = countsPerIndex.get(entry.getKey());
if (indexShardStats == null) {
indexShardStats = new ShardStats(entry.getValue());
Expand Down Expand Up @@ -208,6 +209,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
*/
@PublicApi(since = "1.0.0")
public static class ShardStats implements ToXContentFragment {

int indices;
int total;
int primaries;
Expand All @@ -223,8 +225,7 @@ public static class ShardStats implements ToXContentFragment {

public ShardStats() {}

public ShardStats(CommonStats.AggregatedIndexStats aggregatedIndexStats) {
this.indices = aggregatedIndexStats.indices;
public ShardStats(ClusterStatsNodeResponse.AggregatedIndexStats aggregatedIndexStats) {
this.total = aggregatedIndexStats.total;
this.primaries = aggregatedIndexStats.primaries;
}
Expand Down Expand Up @@ -356,9 +357,8 @@ public void addIndexShardCount(ShardStats indexShardCount) {
}
}

public void addStatsFrom(CommonStats.AggregatedIndexStats incomingStats) {
public void addStatsFrom(ClusterStatsNodeResponse.AggregatedIndexStats incomingStats) {
this.total += incomingStats.total;
this.indices += incomingStats.indices;
this.primaries += incomingStats.primaries;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
import org.opensearch.cluster.health.ClusterHealthStatus;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.common.Nullable;
import org.opensearch.common.annotation.PublicApi;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.index.cache.query.QueryCacheStats;
import org.opensearch.index.engine.SegmentsStats;
import org.opensearch.index.fielddata.FieldDataStats;
Expand Down Expand Up @@ -173,12 +175,12 @@ public void writeTo(StreamOutput out) throws IOException {
public class AggregatedNodeLevelStats extends BaseNodeResponse {

CommonStats commonStats;
Map<String, CommonStats.AggregatedIndexStats> indexStatsMap;
Map<String, AggregatedIndexStats> indexStatsMap;

protected AggregatedNodeLevelStats(StreamInput in) throws IOException {
super(in);
commonStats = in.readOptionalWriteable(CommonStats::new);
indexStatsMap = in.readMap(StreamInput::readString, CommonStats.AggregatedIndexStats::new);
indexStatsMap = in.readMap(StreamInput::readString, AggregatedIndexStats::new);
}

protected AggregatedNodeLevelStats(DiscoveryNode node, ShardStats[] indexShardsStats) {
Expand All @@ -194,9 +196,9 @@ protected AggregatedNodeLevelStats(DiscoveryNode node, ShardStats[] indexShardsS

// Index Level Stats
for (org.opensearch.action.admin.indices.stats.ShardStats shardStats : indexShardsStats) {
CommonStats.AggregatedIndexStats indexShardStats = this.indexStatsMap.get(shardStats.getShardRouting().getIndexName());
AggregatedIndexStats indexShardStats = this.indexStatsMap.get(shardStats.getShardRouting().getIndexName());
if (indexShardStats == null) {
indexShardStats = new CommonStats.AggregatedIndexStats();
indexShardStats = new AggregatedIndexStats();
this.indexStatsMap.put(shardStats.getShardRouting().getIndexName(), indexShardStats);
}

Expand All @@ -223,4 +225,26 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeMap(indexStatsMap, StreamOutput::writeString, (stream, stats) -> stats.writeTo(stream));
}
}

/**
* Node level statistics used for ClusterStatsIndices for _cluster/stats call.
*/
@PublicApi(since = "2.16.0")
public static class AggregatedIndexStats implements Writeable {
public int total = 0;
public int primaries = 0;

public AggregatedIndexStats(StreamInput in) throws IOException {
total = in.readVInt();
primaries = in.readVInt();
}

public AggregatedIndexStats() {}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(total);
out.writeVInt(primaries);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -544,29 +544,4 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}
return builder;
}

/**
* Node level statistics used for ClusterStatsIndices for _cluster/stats call.
*/
@PublicApi(since = "2.16.0")
public static class AggregatedIndexStats implements Writeable {
public int indices = 0;
public int total = 0;
public int primaries = 0;

public AggregatedIndexStats(StreamInput in) throws IOException {
indices = in.readVInt();
total = in.readVInt();
primaries = in.readVInt();
}

public AggregatedIndexStats() {}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(indices);
out.writeVInt(total);
out.writeVInt(primaries);
}
}
}

0 comments on commit e0f6178

Please sign in to comment.