Skip to content

Commit 7bfbb7b

Browse files
committed
Fix _cluster/state to always return cluster_uuid
Since elastic#30143, the Cluster State API should always returns the current cluster_uuid in the response body, regardless of the metrics filters. This is not exactly true as it is returned only if metadata metrics and no specific indices are requested. This commit fixes the behavior to always return the cluster_uuid and add new test.
1 parent fa43aac commit 7bfbb7b

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,24 @@ setup:
163163
version: " - 6.3.99"
164164
reason: "cluster state including cluster_uuid at the top level is new in v6.4.0 and higher"
165165

166+
# Get the current cluster_uuid
167+
- do:
168+
cluster.state: {}
169+
- set: { metadata.cluster_uuid : cluster_uuid }
170+
166171
- do:
167172
cluster.state:
168-
metric: [ master_node, version, metadata ]
173+
metric: [ master_node, version ]
169174

170-
- is_true: cluster_uuid
175+
- match: { cluster_uuid: $cluster_uuid }
171176
- is_true: master_node
172177
- is_true: version
173178
- is_true: state_uuid
174-
- is_true: metadata
179+
180+
- do:
181+
cluster.state:
182+
metric: [ routing_table ]
183+
index: testidx
184+
185+
- match: { cluster_uuid: $cluster_uuid }
186+
- is_true: routing_table

server/src/main/java/org/elasticsearch/action/admin/cluster/state/TransportClusterStateAction.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,11 @@ protected void masterOperation(final ClusterStateRequest request, final ClusterS
9898
if (request.blocks()) {
9999
builder.blocks(currentState.blocks());
100100
}
101-
if (request.metaData()) {
102-
MetaData.Builder mdBuilder;
103-
if (request.indices().length == 0) {
104-
mdBuilder = MetaData.builder(currentState.metaData());
105-
} else {
106-
mdBuilder = MetaData.builder();
107-
}
108101

102+
MetaData.Builder mdBuilder = MetaData.builder();
103+
mdBuilder.clusterUUID(currentState.metaData().clusterUUID());
104+
105+
if (request.metaData()) {
109106
if (request.indices().length > 0) {
110107
String[] indices = indexNameExpressionResolver.concreteIndexNames(currentState, request);
111108
for (String filteredIndex : indices) {
@@ -114,17 +111,19 @@ protected void masterOperation(final ClusterStateRequest request, final ClusterS
114111
mdBuilder.put(indexMetaData, false);
115112
}
116113
}
114+
} else {
115+
mdBuilder = MetaData.builder(currentState.metaData());
117116
}
118117

119118
// Filter our metadata that shouldn't be returned by API
120-
for(ObjectObjectCursor<String, Custom> custom : currentState.metaData().customs()) {
119+
for(ObjectObjectCursor<String, Custom> custom : currentState.metaData().customs()) {
121120
if(!custom.value.context().contains(MetaData.XContentContext.API)) {
122121
mdBuilder.removeCustom(custom.key);
123122
}
124123
}
125-
126-
builder.metaData(mdBuilder);
127124
}
125+
builder.metaData(mdBuilder);
126+
128127
if (request.customs()) {
129128
for (ObjectObjectCursor<String, ClusterState.Custom> custom : currentState.customs()) {
130129
if (custom.value.isPrivate() == false) {

0 commit comments

Comments
 (0)