Skip to content

Commit 57032c0

Browse files
authored
Add packaging to cluster stats response (#41048)
This commit adds a packaging_types field to the cluster stats response that outlines the build flavors and types present in a cluster.
1 parent 43ba344 commit 57032c0

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

docs/reference/cluster/stats.asciidoc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,28 @@ Will return, for example:
195195
},
196196
"discovery_types": {
197197
...
198-
}
198+
},
199+
"packaging_types": [
200+
{
201+
...
202+
}
203+
]
199204
}
200205
}
201206
--------------------------------------------------
202207
// TESTRESPONSE[s/"plugins": \[[^\]]*\]/"plugins": $body.$_path/]
203208
// TESTRESPONSE[s/"network_types": \{[^\}]*\}/"network_types": $body.$_path/]
204209
// TESTRESPONSE[s/"discovery_types": \{[^\}]*\}/"discovery_types": $body.$_path/]
210+
// TESTRESPONSE[s/"packaging_types": \[[^\]]*\]/"packaging_types": $body.$_path/]
205211
// TESTRESPONSE[s/: true|false/: $body.$_path/]
206212
// TESTRESPONSE[s/: (\-)?[0-9]+/: $body.$_path/]
207213
// TESTRESPONSE[s/: "[^"]*"/: $body.$_path/]
208214
// These replacements do a few things:
209215
// 1. Ignore the contents of the `plugins` object because we don't know all of
210216
// the plugins that will be in it. And because we figure folks don't need to
211217
// see an exhaustive list anyway.
212-
// 2. Similarly, ignore the contents of `network_types` and `discovery_types`.
218+
// 2. Similarly, ignore the contents of `network_types`, `discovery_types`, and
219+
// `packaging_types`.
213220
// 3. All of the numbers and strings on the right hand side of *every* field in
214221
// the response are ignored. So we're really only asserting things about the
215222
// the shape of this response, not the values in it.

rest-api-spec/src/main/resources/rest-api-spec/test/cluster.stats/10_basic.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,15 @@
7171
cluster.stats: {}
7272

7373
- is_true: nodes.discovery_types
74+
75+
---
76+
"get cluster stats returns packaging types":
77+
78+
- skip:
79+
version: " - 7.99.99"
80+
reason: "packaging types are added for v8.0.0"
81+
82+
- do:
83+
cluster.stats: {}
84+
85+
- is_true: nodes.packaging_types

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
2828
import org.elasticsearch.cluster.node.DiscoveryNode;
2929
import org.elasticsearch.common.Strings;
30+
import org.elasticsearch.common.collect.Tuple;
3031
import org.elasticsearch.common.network.NetworkModule;
3132
import org.elasticsearch.common.settings.Settings;
3233
import org.elasticsearch.common.transport.TransportAddress;
@@ -61,6 +62,7 @@ public class ClusterStatsNodes implements ToXContentFragment {
6162
private final Set<PluginInfo> plugins;
6263
private final NetworkTypes networkTypes;
6364
private final DiscoveryTypes discoveryTypes;
65+
private final PackagingTypes packagingTypes;
6466

6567
ClusterStatsNodes(List<ClusterStatsNodeResponse> nodeResponses) {
6668
this.versions = new HashSet<>();
@@ -93,6 +95,7 @@ public class ClusterStatsNodes implements ToXContentFragment {
9395
this.jvm = new JvmStats(nodeInfos, nodeStats);
9496
this.networkTypes = new NetworkTypes(nodeInfos);
9597
this.discoveryTypes = new DiscoveryTypes(nodeInfos);
98+
this.packagingTypes = new PackagingTypes(nodeInfos);
9699
}
97100

98101
public Counts getCounts() {
@@ -172,6 +175,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
172175
builder.endObject();
173176

174177
discoveryTypes.toXContent(builder, params);
178+
179+
packagingTypes.toXContent(builder, params);
175180
return builder;
176181
}
177182

@@ -650,4 +655,38 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
650655
}
651656
}
652657

658+
static class PackagingTypes implements ToXContentFragment {
659+
660+
private final Map<Tuple<String, String>, AtomicInteger> packagingTypes;
661+
662+
PackagingTypes(final List<NodeInfo> nodeInfos) {
663+
final var packagingTypes = new HashMap<Tuple<String, String>, AtomicInteger>();
664+
for (final var nodeInfo : nodeInfos) {
665+
final var flavor = nodeInfo.getBuild().flavor().displayName();
666+
final var type = nodeInfo.getBuild().type().displayName();
667+
packagingTypes.computeIfAbsent(Tuple.tuple(flavor, type), k -> new AtomicInteger()).incrementAndGet();
668+
}
669+
this.packagingTypes = Collections.unmodifiableMap(packagingTypes);
670+
}
671+
672+
@Override
673+
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
674+
builder.startArray("packaging_types");
675+
{
676+
for (final var entry : packagingTypes.entrySet()) {
677+
builder.startObject();
678+
{
679+
builder.field("flavor", entry.getKey().v1());
680+
builder.field("type", entry.getKey().v2());
681+
builder.field("count", entry.getValue().get());
682+
}
683+
builder.endObject();
684+
}
685+
}
686+
builder.endArray();
687+
return builder;
688+
}
689+
690+
}
691+
653692
}

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.monitoring.collector.cluster;
77

8+
import org.elasticsearch.Build;
89
import org.elasticsearch.Version;
910
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
1011
import org.elasticsearch.action.admin.cluster.node.info.PluginsAndModules;
@@ -268,6 +269,11 @@ public void testToXContent() throws IOException {
268269
when(mockJvmInfo.getBundledJdk()).thenReturn(true);
269270
when(mockJvmInfo.getUsingBundledJdk()).thenReturn(true);
270271

272+
final Build mockBuild = mock(Build.class);
273+
when(mockBuild.flavor()).thenReturn(Build.Flavor.DEFAULT);
274+
when(mockBuild.type()).thenReturn(Build.Type.DOCKER);
275+
when(mockNodeInfo.getBuild()).thenReturn(mockBuild);
276+
271277
final NodeStats mockNodeStats = mock(NodeStats.class);
272278
when(mockNodeStats.getTimestamp()).thenReturn(0L);
273279

@@ -521,7 +527,14 @@ public void testToXContent() throws IOException {
521527
+ "},"
522528
+ "\"discovery_types\":{"
523529
+ "\"_disco\":1"
524-
+ "}"
530+
+ "},"
531+
+ "\"packaging_types\":["
532+
+ "{"
533+
+ "\"flavor\":\"default\","
534+
+ "\"type\":\"docker\","
535+
+ "\"count\":1"
536+
+ "}"
537+
+ "]"
525538
+ "}"
526539
+ "},"
527540
+ "\"cluster_state\":{"

0 commit comments

Comments
 (0)