Skip to content

Commit eea10f1

Browse files
committed
_cluster/state should always return cluster_uuid (#30143)
Currently, the only way to get the REST response for the `/_cluster/state` call to return the `cluster_uuid` is to request the `metadata` metrics, which is one of the most expensive response structures. However, external monitoring agents will likely want the `cluster_uuid` to correlate the response with other API responses whether or not they want cluster metadata.
1 parent b19a204 commit eea10f1

File tree

6 files changed

+48
-1
lines changed

6 files changed

+48
-1
lines changed

docs/reference/cluster/state.asciidoc

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ of the cluster state (its size when serialized for transmission over
1515
the network), and the cluster state itself, which can be filtered to
1616
only retrieve the parts of interest, as described below.
1717

18+
The cluster's `cluster_uuid` is also returned as part of the top-level
19+
response, in addition to the `metadata` section. added[6.4.0]
20+
21+
NOTE: While the cluster is still forming, it is possible for the `cluster_uuid`
22+
to be `_na_` as well as the cluster state's version to be `-1`.
23+
1824
By default, the cluster state request is routed to the master node, to
1925
ensure that the latest cluster state is returned.
2026
For debugging purposes, you can retrieve the cluster state local to a

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"get cluster state":
33
- do:
44
cluster.state: {}
5-
5+
66
- is_true: master_node
77

88
---
@@ -18,3 +18,18 @@
1818
- is_true: master_node
1919
- gte: { compressed_size_in_bytes: 50 }
2020
- is_true: compressed_size
21+
22+
---
23+
"get cluster state returns cluster_uuid at the top level":
24+
- skip:
25+
version: " - 6.99.99"
26+
reason: "cluster state including cluster_uuid at the top level is new in v6.4.0 and higher"
27+
28+
- do:
29+
cluster.state:
30+
human: true
31+
32+
- is_true: cluster_uuid
33+
- is_true: master_node
34+
- gte: { compressed_size_in_bytes: 50 }
35+
- is_true: compressed_size

rest-api-spec/src/main/resources/rest-api-spec/test/cluster.state/20_filtering.yml

+16
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,19 @@ setup:
156156
- is_true: routing_table.indices.index1
157157
- is_true: metadata.indices.index2
158158
- is_true: routing_table.indices.index2
159+
160+
---
161+
"Filtering the cluster state returns cluster_uuid at the top level regardless of metric filters":
162+
- skip:
163+
version: " - 6.99.99"
164+
reason: "cluster state including cluster_uuid at the top level is new in v6.4.0 and higher"
165+
166+
- do:
167+
cluster.state:
168+
metric: [ master_node, version, metadata ]
169+
170+
- is_true: cluster_uuid
171+
- is_true: master_node
172+
- is_true: version
173+
- is_true: state_uuid
174+
- is_true: metadata

server/src/main/java/org/elasticsearch/cluster/ClusterState.java

+3
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ public String toString() {
326326
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
327327
EnumSet<Metric> metrics = Metric.parseString(params.param("metric", "_all"), true);
328328

329+
// always provide the cluster_uuid as part of the top-level response (also part of the metadata response)
330+
builder.field("cluster_uuid", metaData().clusterUUID());
331+
329332
if (metrics.contains(Metric.VERSION)) {
330333
builder.field("version", version);
331334
builder.field("state_uuid", stateUUID);

server/src/test/java/org/elasticsearch/action/admin/cluster/reroute/ClusterRerouteResponseTests.java

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public void testToXContent() throws IOException {
6969
assertEquals("{\n" +
7070
" \"acknowledged\" : true,\n" +
7171
" \"state\" : {\n" +
72+
" \"cluster_uuid\" : \"_na_\",\n" +
7273
" \"version\" : 0,\n" +
7374
" \"state_uuid\" : \"" + clusterState.stateUUID() + "\",\n" +
7475
" \"master_node\" : \"node0\",\n" +
@@ -136,6 +137,7 @@ public void testToXContent() throws IOException {
136137
assertEquals("{\n" +
137138
" \"acknowledged\" : true,\n" +
138139
" \"state\" : {\n" +
140+
" \"cluster_uuid\" : \"_na_\",\n" +
139141
" \"version\" : 0,\n" +
140142
" \"state_uuid\" : \"" + clusterState.stateUUID() + "\",\n" +
141143
" \"master_node\" : \"node0\"\n" +
@@ -168,6 +170,7 @@ public void testToXContent() throws IOException {
168170
assertEquals("{\n" +
169171
" \"acknowledged\" : true,\n" +
170172
" \"state\" : {\n" +
173+
" \"cluster_uuid\" : \"_na_\",\n" +
171174
" \"metadata\" : {\n" +
172175
" \"cluster_uuid\" : \"_na_\",\n" +
173176
" \"templates\" : { },\n" +

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

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.cluster.ClusterName;
1818
import org.elasticsearch.cluster.ClusterState;
1919
import org.elasticsearch.cluster.health.ClusterHealthStatus;
20+
import org.elasticsearch.cluster.metadata.MetaData;
2021
import org.elasticsearch.cluster.node.DiscoveryNode;
2122
import org.elasticsearch.cluster.node.DiscoveryNodes;
2223
import org.elasticsearch.cluster.routing.ShardRouting;
@@ -188,6 +189,7 @@ public void testNodesHash() {
188189

189190
@Override
190191
public void testToXContent() throws IOException {
192+
final String clusterUuid = "_cluster";
191193
final ClusterName clusterName = new ClusterName("_cluster_name");
192194
final TransportAddress transportAddress = new TransportAddress(TransportAddress.META_ADDRESS, 9300);
193195
final DiscoveryNode discoveryNode = new DiscoveryNode("_node_name",
@@ -201,6 +203,7 @@ public void testToXContent() throws IOException {
201203
Version.V_6_0_0_beta1);
202204

203205
final ClusterState clusterState = ClusterState.builder(clusterName)
206+
.metaData(MetaData.builder().clusterUUID(clusterUuid).build())
204207
.stateUUID("_state_uuid")
205208
.version(12L)
206209
.nodes(DiscoveryNodes.builder()
@@ -500,6 +503,7 @@ public void testToXContent() throws IOException {
500503
+ "\"cluster_state\":{"
501504
+ "\"nodes_hash\":1314980060,"
502505
+ "\"status\":\"green\","
506+
+ "\"cluster_uuid\":\"_cluster\","
503507
+ "\"version\":12,"
504508
+ "\"state_uuid\":\"_state_uuid\","
505509
+ "\"master_node\":\"_node\","

0 commit comments

Comments
 (0)