Skip to content

Commit f55c09c

Browse files
author
Swetha Guptha
committed
URI path filtering support in cluster stats API
Signed-off-by: Swetha Guptha <[email protected]>
1 parent 12ff5ed commit f55c09c

File tree

9 files changed

+616
-77
lines changed

9 files changed

+616
-77
lines changed

server/src/internalClusterTest/java/org/opensearch/action/admin/cluster/stats/ClusterStatsIT.java

+107
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.opensearch.action.admin.cluster.node.stats.NodeStats;
3838
import org.opensearch.action.admin.cluster.node.stats.NodesStatsRequest;
3939
import org.opensearch.action.admin.cluster.node.stats.NodesStatsResponse;
40+
import org.opensearch.action.admin.cluster.stats.ClusterStatsRequest.SubMetrics;
4041
import org.opensearch.client.Client;
4142
import org.opensearch.client.Requests;
4243
import org.opensearch.cluster.health.ClusterHealthStatus;
@@ -505,6 +506,112 @@ public void testNodeRolesWithDataNodeLegacySettings() throws ExecutionException,
505506
assertEquals(expectedNodesRoles, Set.of(getNodeRoles(client, 0), getNodeRoles(client, 1)));
506507
}
507508

509+
public void testClusterStatsMetricFiltering() {
510+
internalCluster().startNode();
511+
ensureGreen();
512+
513+
client().admin().indices().prepareCreate("test1").setMapping("{\"properties\":{\"foo\":{\"type\": \"keyword\"}}}").get();
514+
515+
ClusterStatsResponse response = client().admin()
516+
.cluster()
517+
.prepareClusterStats()
518+
.useAggregatedNodeLevelResponses(randomBoolean())
519+
.get();
520+
assertNotNull(response.getIndicesStats());
521+
assertNotNull(response.getNodesStats());
522+
assertNotNull(response.getIndicesStats().getMappings());
523+
assertNotNull(response.getIndicesStats().getAnalysis());
524+
assertNotNull(response.getNodesStats().getJvm());
525+
assertNotNull(response.getNodesStats().getOs());
526+
527+
response = client().admin()
528+
.cluster()
529+
.prepareClusterStats()
530+
.useAggregatedNodeLevelResponses(randomBoolean())
531+
.requestMetrics(SubMetrics.allSubMetrics(ClusterStatsRequest.Metric.INDICES))
532+
.get();
533+
assertNotNull(response.getIndicesStats());
534+
assertNotNull(response.getIndicesStats().getShards());
535+
assertNotNull(response.getIndicesStats().getDocs());
536+
assertNotNull(response.getIndicesStats().getMappings());
537+
assertNotNull(response.getIndicesStats().getAnalysis());
538+
assertNull(response.getNodesStats());
539+
540+
response = client().admin()
541+
.cluster()
542+
.prepareClusterStats()
543+
.useAggregatedNodeLevelResponses(randomBoolean())
544+
.requestMetrics(SubMetrics.allSubMetrics(ClusterStatsRequest.Metric.NODES))
545+
.get();
546+
assertNotNull(response.getNodesStats());
547+
assertNotNull(response.getNodesStats().getJvm());
548+
assertNotNull(response.getNodesStats().getOs());
549+
assertNotNull(response.getNodesStats().getPlugins());
550+
assertNotNull(response.getNodesStats().getFs());
551+
assertNull(response.getIndicesStats());
552+
553+
response = client().admin()
554+
.cluster()
555+
.prepareClusterStats()
556+
.useAggregatedNodeLevelResponses(randomBoolean())
557+
.requestMetrics(Set.of(SubMetrics.MAPPINGS.metricName(), SubMetrics.ANALYSIS.metricName()))
558+
.get();
559+
assertNotNull(response.getIndicesStats());
560+
assertNotNull(response.getIndicesStats().getMappings());
561+
assertNotNull(response.getIndicesStats().getAnalysis());
562+
assertNull(response.getIndicesStats().getShards());
563+
assertNull(response.getIndicesStats().getDocs());
564+
assertNull(response.getNodesStats());
565+
566+
response = client().admin()
567+
.cluster()
568+
.prepareClusterStats()
569+
.useAggregatedNodeLevelResponses(randomBoolean())
570+
.requestMetrics(Set.of(SubMetrics.OS.metricName(), SubMetrics.PROCESS.metricName()))
571+
.get();
572+
assertNotNull(response.getNodesStats());
573+
assertNotNull(response.getNodesStats().getOs());
574+
assertNotNull(response.getNodesStats().getProcess());
575+
assertNull(response.getNodesStats().getPlugins());
576+
assertNull(response.getNodesStats().getFs());
577+
assertNull(response.getIndicesStats());
578+
579+
response = client().admin()
580+
.cluster()
581+
.prepareClusterStats()
582+
.useAggregatedNodeLevelResponses(randomBoolean())
583+
.requestMetrics(
584+
Set.of(
585+
SubMetrics.OS.metricName(),
586+
SubMetrics.PROCESS.metricName(),
587+
SubMetrics.MAPPINGS.metricName(),
588+
SubMetrics.ANALYSIS.metricName()
589+
)
590+
)
591+
.get();
592+
assertNotNull(response.getIndicesStats());
593+
assertNotNull(response.getIndicesStats().getMappings());
594+
assertNotNull(response.getIndicesStats().getAnalysis());
595+
assertNotNull(response.getNodesStats());
596+
assertNotNull(response.getNodesStats().getOs());
597+
assertNotNull(response.getNodesStats().getProcess());
598+
assertNull(response.getNodesStats().getPlugins());
599+
assertNull(response.getNodesStats().getFs());
600+
assertNull(response.getIndicesStats().getShards());
601+
assertNull(response.getIndicesStats().getDocs());
602+
603+
assertThrows(
604+
IllegalStateException.class,
605+
() -> client().admin()
606+
.cluster()
607+
.prepareClusterStats()
608+
.useAggregatedNodeLevelResponses(randomBoolean())
609+
.requestMetrics(Set.of("random_metric"))
610+
.get()
611+
);
612+
613+
}
614+
508615
private Map<String, Integer> getExpectedCounts(
509616
int dataRoleCount,
510617
int masterRoleCount,

server/src/main/java/org/opensearch/action/admin/cluster/stats/ClusterStatsIndices.java

+91-28
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
package org.opensearch.action.admin.cluster.stats;
3434

35+
import org.opensearch.action.admin.cluster.stats.ClusterStatsRequest.Metric;
36+
import org.opensearch.action.admin.cluster.stats.ClusterStatsRequest.SubMetrics;
3537
import org.opensearch.action.admin.indices.stats.CommonStats;
3638
import org.opensearch.common.annotation.PublicApi;
3739
import org.opensearch.core.xcontent.ToXContentFragment;
@@ -47,6 +49,8 @@
4749
import java.util.HashMap;
4850
import java.util.List;
4951
import java.util.Map;
52+
import java.util.Set;
53+
import java.util.function.Consumer;
5054

5155
/**
5256
* Cluster Stats per index
@@ -68,14 +72,57 @@ public class ClusterStatsIndices implements ToXContentFragment {
6872
private MappingStats mappings;
6973

7074
public ClusterStatsIndices(List<ClusterStatsNodeResponse> nodeResponses, MappingStats mappingStats, AnalysisStats analysisStats) {
75+
this(SubMetrics.allSubMetrics(Metric.INDICES), nodeResponses, mappingStats, analysisStats);
76+
77+
}
78+
79+
public ClusterStatsIndices(
80+
Set<String> requestedMetrics,
81+
List<ClusterStatsNodeResponse> nodeResponses,
82+
MappingStats mappingStats,
83+
AnalysisStats analysisStats
84+
) {
7185
Map<String, ShardStats> countsPerIndex = new HashMap<>();
7286

73-
this.docs = new DocsStats();
74-
this.store = new StoreStats();
75-
this.fieldData = new FieldDataStats();
76-
this.queryCache = new QueryCacheStats();
77-
this.completion = new CompletionStats();
78-
this.segments = new SegmentsStats();
87+
Consumer<DocsStats> docsStatsConsumer = (docs) -> {
88+
if (SubMetrics.DOCS.containedIn(requestedMetrics)) {
89+
if (this.docs == null) this.docs = new DocsStats();
90+
this.docs.add(docs);
91+
}
92+
};
93+
Consumer<StoreStats> storeStatsConsumer = (store) -> {
94+
if (SubMetrics.STORE.containedIn(requestedMetrics)) {
95+
if (this.store == null) this.store = new StoreStats();
96+
this.store.add(store);
97+
}
98+
};
99+
Consumer<FieldDataStats> fieldDataConsumer = (fieldDataStats) -> {
100+
if (SubMetrics.FIELDDATA.containedIn(requestedMetrics)) {
101+
if (this.fieldData == null) this.fieldData = new FieldDataStats();
102+
this.fieldData.add(fieldDataStats);
103+
}
104+
};
105+
106+
Consumer<QueryCacheStats> queryCacheStatsConsumer = (queryCacheStats) -> {
107+
if (SubMetrics.QUERY_CACHE.containedIn(requestedMetrics)) {
108+
if (this.queryCache == null) this.queryCache = new QueryCacheStats();
109+
this.queryCache.add(queryCacheStats);
110+
}
111+
};
112+
113+
Consumer<CompletionStats> completionStatsConsumer = (completionStats) -> {
114+
if (SubMetrics.COMPLETION.containedIn(requestedMetrics)) {
115+
if (this.completion == null) this.completion = new CompletionStats();
116+
this.completion.add(completionStats);
117+
}
118+
};
119+
120+
Consumer<SegmentsStats> segmentsStatsConsumer = (segmentsStats) -> {
121+
if (SubMetrics.SEGMENTS.containedIn(requestedMetrics)) {
122+
if (this.segments == null) this.segments = new SegmentsStats();
123+
this.segments.add(segmentsStats);
124+
}
125+
};
79126

80127
for (ClusterStatsNodeResponse r : nodeResponses) {
81128
// Aggregated response from the node
@@ -92,12 +139,12 @@ public ClusterStatsIndices(List<ClusterStatsNodeResponse> nodeResponses, Mapping
92139
}
93140
}
94141

95-
docs.add(r.getAggregatedNodeLevelStats().commonStats.docs);
96-
store.add(r.getAggregatedNodeLevelStats().commonStats.store);
97-
fieldData.add(r.getAggregatedNodeLevelStats().commonStats.fieldData);
98-
queryCache.add(r.getAggregatedNodeLevelStats().commonStats.queryCache);
99-
completion.add(r.getAggregatedNodeLevelStats().commonStats.completion);
100-
segments.add(r.getAggregatedNodeLevelStats().commonStats.segments);
142+
docsStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.docs);
143+
storeStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.store);
144+
fieldDataConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.fieldData);
145+
queryCacheStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.queryCache);
146+
completionStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.completion);
147+
segmentsStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.segments);
101148
} else {
102149
// Default response from the node
103150
for (org.opensearch.action.admin.indices.stats.ShardStats shardStats : r.shardsStats()) {
@@ -113,21 +160,23 @@ public ClusterStatsIndices(List<ClusterStatsNodeResponse> nodeResponses, Mapping
113160

114161
if (shardStats.getShardRouting().primary()) {
115162
indexShardStats.primaries++;
116-
docs.add(shardCommonStats.docs);
163+
docsStatsConsumer.accept(shardCommonStats.docs);
117164
}
118-
store.add(shardCommonStats.store);
119-
fieldData.add(shardCommonStats.fieldData);
120-
queryCache.add(shardCommonStats.queryCache);
121-
completion.add(shardCommonStats.completion);
122-
segments.add(shardCommonStats.segments);
165+
storeStatsConsumer.accept(shardCommonStats.store);
166+
fieldDataConsumer.accept(shardCommonStats.fieldData);
167+
queryCacheStatsConsumer.accept(shardCommonStats.queryCache);
168+
completionStatsConsumer.accept(shardCommonStats.completion);
169+
segmentsStatsConsumer.accept(shardCommonStats.segments);
123170
}
124171
}
125172
}
126173

127-
shards = new ShardStats();
128174
indexCount = countsPerIndex.size();
129-
for (final ShardStats indexCountsCursor : countsPerIndex.values()) {
130-
shards.addIndexShardCount(indexCountsCursor);
175+
if (SubMetrics.SHARDS.containedIn(requestedMetrics)) {
176+
shards = new ShardStats();
177+
for (final ShardStats indexCountsCursor : countsPerIndex.values()) {
178+
shards.addIndexShardCount(indexCountsCursor);
179+
}
131180
}
132181

133182
this.mappings = mappingStats;
@@ -186,13 +235,27 @@ static final class Fields {
186235
@Override
187236
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
188237
builder.field(Fields.COUNT, indexCount);
189-
shards.toXContent(builder, params);
190-
docs.toXContent(builder, params);
191-
store.toXContent(builder, params);
192-
fieldData.toXContent(builder, params);
193-
queryCache.toXContent(builder, params);
194-
completion.toXContent(builder, params);
195-
segments.toXContent(builder, params);
238+
if (shards != null) {
239+
shards.toXContent(builder, params);
240+
}
241+
if (docs != null) {
242+
docs.toXContent(builder, params);
243+
}
244+
if (store != null) {
245+
store.toXContent(builder, params);
246+
}
247+
if (fieldData != null) {
248+
fieldData.toXContent(builder, params);
249+
}
250+
if (queryCache != null) {
251+
queryCache.toXContent(builder, params);
252+
}
253+
if (completion != null) {
254+
completion.toXContent(builder, params);
255+
}
256+
if (segments != null) {
257+
segments.toXContent(builder, params);
258+
}
196259
if (mappings != null) {
197260
mappings.toXContent(builder, params);
198261
}

0 commit comments

Comments
 (0)