Skip to content

Commit 0038530

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 0038530

File tree

6 files changed

+373
-77
lines changed

6 files changed

+373
-77
lines changed

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

Lines changed: 90 additions & 28 deletions
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,57 @@ 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(
79+
Set<String> requestedMetrics,
80+
List<ClusterStatsNodeResponse> nodeResponses,
81+
MappingStats mappingStats,
82+
AnalysisStats analysisStats
83+
) {
7184
Map<String, ShardStats> countsPerIndex = new HashMap<>();
7285

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

80126
for (ClusterStatsNodeResponse r : nodeResponses) {
81127
// Aggregated response from the node
@@ -92,12 +138,12 @@ public ClusterStatsIndices(List<ClusterStatsNodeResponse> nodeResponses, Mapping
92138
}
93139
}
94140

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);
141+
docsStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.docs);
142+
storeStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.store);
143+
fieldDataConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.fieldData);
144+
queryCacheStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.queryCache);
145+
completionStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.completion);
146+
segmentsStatsConsumer.accept(r.getAggregatedNodeLevelStats().commonStats.segments);
101147
} else {
102148
// Default response from the node
103149
for (org.opensearch.action.admin.indices.stats.ShardStats shardStats : r.shardsStats()) {
@@ -113,21 +159,23 @@ public ClusterStatsIndices(List<ClusterStatsNodeResponse> nodeResponses, Mapping
113159

114160
if (shardStats.getShardRouting().primary()) {
115161
indexShardStats.primaries++;
116-
docs.add(shardCommonStats.docs);
162+
docsStatsConsumer.accept(shardCommonStats.docs);
117163
}
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);
164+
storeStatsConsumer.accept(shardCommonStats.store);
165+
fieldDataConsumer.accept(shardCommonStats.fieldData);
166+
queryCacheStatsConsumer.accept(shardCommonStats.queryCache);
167+
completionStatsConsumer.accept(shardCommonStats.completion);
168+
segmentsStatsConsumer.accept(shardCommonStats.segments);
123169
}
124170
}
125171
}
126172

127-
shards = new ShardStats();
128173
indexCount = countsPerIndex.size();
129-
for (final ShardStats indexCountsCursor : countsPerIndex.values()) {
130-
shards.addIndexShardCount(indexCountsCursor);
174+
if (Metric.SHARDS.containedIn(requestedMetrics)) {
175+
shards = new ShardStats();
176+
for (final ShardStats indexCountsCursor : countsPerIndex.values()) {
177+
shards.addIndexShardCount(indexCountsCursor);
178+
}
131179
}
132180

133181
this.mappings = mappingStats;
@@ -186,13 +234,27 @@ static final class Fields {
186234
@Override
187235
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
188236
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);
237+
if (shards != null) {
238+
shards.toXContent(builder, params);
239+
}
240+
if (docs != null) {
241+
docs.toXContent(builder, params);
242+
}
243+
if (store != null) {
244+
store.toXContent(builder, params);
245+
}
246+
if (fieldData != null) {
247+
fieldData.toXContent(builder, params);
248+
}
249+
if (queryCache != null) {
250+
queryCache.toXContent(builder, params);
251+
}
252+
if (completion != null) {
253+
completion.toXContent(builder, params);
254+
}
255+
if (segments != null) {
256+
segments.toXContent(builder, params);
257+
}
196258
if (mappings != null) {
197259
mappings.toXContent(builder, params);
198260
}

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

Lines changed: 67 additions & 38 deletions
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 (isFSInfoRequested && 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.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 (os != 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 (process != 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 (jvm != 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 (fs != 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 (plugins != 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)