Skip to content

Commit 1a7b37b

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 1a7b37b

File tree

7 files changed

+366
-78
lines changed

7 files changed

+366
-78
lines changed

Diff for: .idea/inspectionProfiles/Project_Default.xml

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+86-28
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

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

35+
import org.opensearch.action.admin.cluster.stats.ClusterStatsRequest.Metric;
3536
import org.opensearch.action.admin.indices.stats.CommonStats;
3637
import org.opensearch.common.annotation.PublicApi;
3738
import org.opensearch.core.xcontent.ToXContentFragment;
@@ -47,6 +48,8 @@
4748
import java.util.HashMap;
4849
import java.util.List;
4950
import java.util.Map;
51+
import java.util.Set;
52+
import java.util.function.Consumer;
5053

5154
/**
5255
* Cluster Stats per index
@@ -68,14 +71,52 @@ public class ClusterStatsIndices implements ToXContentFragment {
6871
private MappingStats mappings;
6972

7073
public ClusterStatsIndices(List<ClusterStatsNodeResponse> nodeResponses, MappingStats mappingStats, AnalysisStats analysisStats) {
74+
this(Metric.allIndicesMetrics(), nodeResponses, mappingStats, analysisStats);
75+
76+
}
77+
78+
public ClusterStatsIndices(Set<String> requestedMetrics, List<ClusterStatsNodeResponse> nodeResponses, MappingStats mappingStats, AnalysisStats analysisStats) {
7179
Map<String, ShardStats> countsPerIndex = new HashMap<>();
7280

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();
81+
Consumer<DocsStats> docsStatsConsumer = (docs) -> {
82+
if (Metric.DOCS.containedIn(requestedMetrics)) {
83+
if (this.docs == null) this.docs = new DocsStats();
84+
this.docs.add(docs);
85+
}
86+
};
87+
Consumer<StoreStats> storeStatsConsumer = (store) -> {
88+
if (Metric.STORE.containedIn(requestedMetrics)) {
89+
if (this.store == null) this.store = new StoreStats();
90+
this.store.add(store);
91+
}
92+
};
93+
Consumer<FieldDataStats> fieldDataConsumer = (fieldDataStats) -> {
94+
if (Metric.FIELD_DATA.containedIn(requestedMetrics)) {
95+
if (this.fieldData == null) this.fieldData = new FieldDataStats();
96+
this.fieldData.add(fieldDataStats);
97+
}
98+
};
99+
100+
Consumer<QueryCacheStats> queryCacheStatsConsumer = (queryCacheStats) -> {
101+
if (Metric.QUERY_CACHE.containedIn(requestedMetrics)) {
102+
if (this.queryCache == null) this.queryCache = new QueryCacheStats();
103+
this.queryCache.add(queryCacheStats);
104+
}
105+
};
106+
107+
Consumer<CompletionStats> completionStatsConsumer = (completionStats) -> {
108+
if (Metric.COMPLETION.containedIn(requestedMetrics)) {
109+
if (this.completion == null) this.completion = new CompletionStats();
110+
this.completion.add(completionStats);
111+
}
112+
};
113+
114+
Consumer<SegmentsStats> segmentsStatsConsumer = (segmentsStats) -> {
115+
if (Metric.SEGMENTS.containedIn(requestedMetrics)) {
116+
if (this.segments == null) this.segments = new SegmentsStats();
117+
this.segments.add(segmentsStats);
118+
}
119+
};
79120

80121
for (ClusterStatsNodeResponse r : nodeResponses) {
81122
// Aggregated response from the node
@@ -92,12 +133,12 @@ public ClusterStatsIndices(List<ClusterStatsNodeResponse> nodeResponses, Mapping
92133
}
93134
}
94135

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);
136+
docsStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.docs);
137+
storeStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.store);
138+
fieldDataConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.fieldData);
139+
queryCacheStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.queryCache);
140+
completionStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.completion);
141+
segmentsStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.segments);
101142
} else {
102143
// Default response from the node
103144
for (org.opensearch.action.admin.indices.stats.ShardStats shardStats : r.shardsStats()) {
@@ -113,21 +154,24 @@ public ClusterStatsIndices(List<ClusterStatsNodeResponse> nodeResponses, Mapping
113154

114155
if (shardStats.getShardRouting().primary()) {
115156
indexShardStats.primaries++;
116-
docs.add(shardCommonStats.docs);
157+
docsStatsConsumer.accept(shardCommonStats.docs);
117158
}
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);
159+
storeStatsConsumer.accept(shardCommonStats.store);
160+
fieldDataConsumer.accept(shardCommonStats.fieldData);
161+
queryCacheStatsConsumer.accept(shardCommonStats.queryCache);
162+
completionStatsConsumer.accept(shardCommonStats.completion);
163+
segmentsStatsConsumer.accept(shardCommonStats.segments);
123164
}
124165
}
125166
}
126167

127-
shards = new ShardStats();
168+
128169
indexCount = countsPerIndex.size();
129-
for (final ShardStats indexCountsCursor : countsPerIndex.values()) {
130-
shards.addIndexShardCount(indexCountsCursor);
170+
if (Metric.SHARDS.containedIn(requestedMetrics)) {
171+
shards = new ShardStats();
172+
for (final ShardStats indexCountsCursor : countsPerIndex.values()) {
173+
shards.addIndexShardCount(indexCountsCursor);
174+
}
131175
}
132176

133177
this.mappings = mappingStats;
@@ -186,13 +230,27 @@ static final class Fields {
186230
@Override
187231
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
188232
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);
233+
if (shards != null) {
234+
shards.toXContent(builder, params);
235+
}
236+
if (docs != null) {
237+
docs.toXContent(builder, params);
238+
}
239+
if (store != null) {
240+
store.toXContent(builder, params);
241+
}
242+
if (fieldData != null) {
243+
fieldData.toXContent(builder, params);
244+
}
245+
if (queryCache != null) {
246+
queryCache.toXContent(builder, params);
247+
}
248+
if (completion != null) {
249+
completion.toXContent(builder, params);
250+
}
251+
if (segments != null) {
252+
segments.toXContent(builder, params);
253+
}
196254
if (mappings != null) {
197255
mappings.toXContent(builder, params);
198256
}

Diff for: server/src/main/java/org/opensearch/action/admin/cluster/stats/ClusterStatsNodes.java

+67-38
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.opensearch.action.admin.cluster.node.info.NodeInfo;
3737
import org.opensearch.action.admin.cluster.node.info.PluginsAndModules;
3838
import org.opensearch.action.admin.cluster.node.stats.NodeStats;
39+
import org.opensearch.action.admin.cluster.stats.ClusterStatsRequest.Metric;
3940
import org.opensearch.cluster.node.DiscoveryNode;
4041
import org.opensearch.cluster.node.DiscoveryNodeRole;
4142
import org.opensearch.common.annotation.PublicApi;
@@ -90,37 +91,47 @@ public class ClusterStatsNodes implements ToXContentFragment {
9091
private final IngestStats ingestStats;
9192

9293
ClusterStatsNodes(List<ClusterStatsNodeResponse> nodeResponses) {
94+
this(Metric.allNodesMetrics(), nodeResponses);
95+
}
96+
97+
ClusterStatsNodes(Set<String> requestedMetrics, List<ClusterStatsNodeResponse> nodeResponses) {
9398
this.versions = new HashSet<>();
94-
this.fs = new FsInfo.Path();
95-
this.plugins = new HashSet<>();
99+
boolean isFSInfoRequested = Metric.FS.containedIn(requestedMetrics);
100+
boolean isPluginsInfoRequested = Metric.PLUGINS.containedIn(requestedMetrics);
101+
this.fs = isFSInfoRequested ? new FsInfo.Path() : null;
102+
this.plugins = isPluginsInfoRequested ? new HashSet<>() : null;
96103

97104
Set<InetAddress> seenAddresses = new HashSet<>(nodeResponses.size());
98105
List<NodeInfo> nodeInfos = new ArrayList<>(nodeResponses.size());
99-
List<NodeStats> nodeStats = new ArrayList<>(nodeResponses.size());
106+
List<NodeStats> nodesStats = new ArrayList<>(nodeResponses.size());
100107
for (ClusterStatsNodeResponse nodeResponse : nodeResponses) {
101-
nodeInfos.add(nodeResponse.nodeInfo());
102-
nodeStats.add(nodeResponse.nodeStats());
103-
this.versions.add(nodeResponse.nodeInfo().getVersion());
104-
this.plugins.addAll(nodeResponse.nodeInfo().getInfo(PluginsAndModules.class).getPluginInfos());
108+
NodeInfo nodeInfo = nodeResponse.nodeInfo();
109+
NodeStats nodeStats = nodeResponse.nodeStats();
110+
nodeInfos.add(nodeInfo);
111+
nodesStats.add(nodeStats);
112+
this.versions.add(nodeInfo.getVersion());
113+
if (isPluginsInfoRequested) {
114+
this.plugins.addAll(nodeInfo.getInfo(PluginsAndModules.class).getPluginInfos());
115+
}
105116

106117
// now do the stats that should be deduped by hardware (implemented by ip deduping)
107-
TransportAddress publishAddress = nodeResponse.nodeInfo().getInfo(TransportInfo.class).address().publishAddress();
118+
TransportAddress publishAddress = nodeInfo.getInfo(TransportInfo.class).address().publishAddress();
108119
final InetAddress inetAddress = publishAddress.address().getAddress();
109120
if (!seenAddresses.add(inetAddress)) {
110121
continue;
111122
}
112-
if (nodeResponse.nodeStats().getFs() != null) {
113-
this.fs.add(nodeResponse.nodeStats().getFs().getTotal());
123+
if (nodeStats.getFs() != null) {
124+
this.fs.add(nodeStats.getFs().getTotal());
114125
}
115126
}
116127
this.counts = new Counts(nodeInfos);
117-
this.os = new OsStats(nodeInfos, nodeStats);
118-
this.process = new ProcessStats(nodeStats);
119-
this.jvm = new JvmStats(nodeInfos, nodeStats);
120-
this.networkTypes = new NetworkTypes(nodeInfos);
121-
this.discoveryTypes = new DiscoveryTypes(nodeInfos);
122-
this.packagingTypes = new PackagingTypes(nodeInfos);
123-
this.ingestStats = new IngestStats(nodeStats);
128+
this.os = Metric.OS.containedIn(requestedMetrics) ? new OsStats(nodeInfos, nodesStats) : null;
129+
this.process = Metric.PROCESS.containedIn(requestedMetrics) ? new ProcessStats(nodesStats) : null;
130+
this.jvm = Metric.JVM.containedIn(requestedMetrics) ? new JvmStats(nodeInfos, nodesStats) : null;
131+
this.networkTypes = Metric.NETWORK_TYPES.containedIn(requestedMetrics) ? new NetworkTypes(nodeInfos) : null;
132+
this.discoveryTypes = Metric.DISCOVERY_TYPES.containedIn(requestedMetrics) ? new DiscoveryTypes(nodeInfos) : null;
133+
this.packagingTypes = Metric.PACKAGING_TYPES.containedIn(requestedMetrics) ? new PackagingTypes(nodeInfos) : null;
134+
this.ingestStats = Metric.INGEST_STATS.containedIn(requestedMetrics) ? new IngestStats(nodesStats) : null;
124135
}
125136

126137
public Counts getCounts() {
@@ -179,36 +190,54 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
179190
}
180191
builder.endArray();
181192

182-
builder.startObject(Fields.OS);
183-
os.toXContent(builder, params);
184-
builder.endObject();
193+
if (getOs() != null) {
194+
builder.startObject(Fields.OS);
195+
os.toXContent(builder, params);
196+
builder.endObject();
197+
}
185198

186-
builder.startObject(Fields.PROCESS);
187-
process.toXContent(builder, params);
188-
builder.endObject();
199+
if (getProcess() != null) {
200+
builder.startObject(Fields.PROCESS);
201+
process.toXContent(builder, params);
202+
builder.endObject();
203+
}
189204

190-
builder.startObject(Fields.JVM);
191-
jvm.toXContent(builder, params);
192-
builder.endObject();
205+
if (getJvm() != null) {
206+
builder.startObject(Fields.JVM);
207+
jvm.toXContent(builder, params);
208+
builder.endObject();
209+
}
193210

194-
builder.field(Fields.FS);
195-
fs.toXContent(builder, params);
211+
if (getFs() != null) {
212+
builder.field(Fields.FS);
213+
fs.toXContent(builder, params);
214+
}
196215

197-
builder.startArray(Fields.PLUGINS);
198-
for (PluginInfo pluginInfo : plugins) {
199-
pluginInfo.toXContent(builder, params);
216+
if (getPlugins() != null) {
217+
builder.startArray(Fields.PLUGINS);
218+
for (PluginInfo pluginInfo : plugins) {
219+
pluginInfo.toXContent(builder, params);
220+
}
221+
builder.endArray();
200222
}
201-
builder.endArray();
202223

203-
builder.startObject(Fields.NETWORK_TYPES);
204-
networkTypes.toXContent(builder, params);
205-
builder.endObject();
224+
if (networkTypes != null) {
225+
builder.startObject(Fields.NETWORK_TYPES);
226+
networkTypes.toXContent(builder, params);
227+
builder.endObject();
228+
}
206229

207-
discoveryTypes.toXContent(builder, params);
230+
if (discoveryTypes != null) {
231+
discoveryTypes.toXContent(builder, params);
232+
}
208233

209-
packagingTypes.toXContent(builder, params);
234+
if (packagingTypes != null) {
235+
packagingTypes.toXContent(builder, params);
236+
}
210237

211-
ingestStats.toXContent(builder, params);
238+
if (ingestStats != null) {
239+
ingestStats.toXContent(builder, params);
240+
}
212241

213242
return builder;
214243
}

0 commit comments

Comments
 (0)