Skip to content

Commit 7e78762

Browse files
committed
Check RestApi compatibility version
1 parent d025da1 commit 7e78762

File tree

9 files changed

+70
-38
lines changed

9 files changed

+70
-38
lines changed

rest-api-spec/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ tasks.named("yamlRestTestV7CompatTransform").configure { task ->
8181
task.skipTest("search.aggregation/20_terms/string profiler via global ordinals native implementation", "The profiler results aren't backwards compatible.")
8282
task.skipTest("search.aggregation/20_terms/string profiler via map", "The profiler results aren't backwards compatible.")
8383
task.skipTest("search.aggregation/20_terms/numeric profiler", "The profiler results aren't backwards compatible.")
84-
task.skipTest("cluster.health/20_request_timeout/cluster health request timeout on waiting for nodes", "Status code changed to 200 OK in 8.0.0")
85-
task.skipTest("cluster.health/20_request_timeout/cluster health request timeout waiting for active shards", "Status code changed to 200 OK in 8.0.0")
8684

8785
task.replaceValueInMatch("_type", "_doc")
8886
task.addAllowedWarningRegex("\\[types removal\\].*")

server/src/main/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthRequest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.common.Priority;
1919
import org.elasticsearch.common.io.stream.StreamInput;
2020
import org.elasticsearch.common.io.stream.StreamOutput;
21+
import org.elasticsearch.core.RestApiVersion;
2122
import org.elasticsearch.core.TimeValue;
2223

2324
import java.io.IOException;
@@ -40,6 +41,7 @@ public class ClusterHealthRequest extends MasterNodeReadRequest<ClusterHealthReq
4041
* The default value is 'cluster'.
4142
*/
4243
private Level level = Level.CLUSTER;
44+
private RestApiVersion restApiVersion = RestApiVersion.current();
4345

4446
public ClusterHealthRequest() {
4547
}
@@ -67,6 +69,9 @@ public ClusterHealthRequest(StreamInput in) throws IOException {
6769
} else {
6870
indicesOptions = IndicesOptions.lenientExpandOpen();
6971
}
72+
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
73+
restApiVersion = RestApiVersion.valueOf(in.readString());
74+
}
7075
}
7176

7277
@Override
@@ -97,6 +102,9 @@ public void writeTo(StreamOutput out) throws IOException {
97102
if (out.getVersion().onOrAfter(Version.V_7_2_0)) {
98103
indicesOptions.writeIndicesOptions(out);
99104
}
105+
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
106+
out.writeString(restApiVersion.name());
107+
}
100108
}
101109

102110
@Override
@@ -261,6 +269,14 @@ public ActionRequestValidationException validate() {
261269
return null;
262270
}
263271

272+
public RestApiVersion restApiVersion() {
273+
return restApiVersion;
274+
}
275+
276+
public void setRestApiVersion(RestApiVersion restApiVersion) {
277+
this.restApiVersion = restApiVersion;
278+
}
279+
264280
public enum Level {
265281
CLUSTER, INDICES, SHARDS
266282
}

server/src/main/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthResponse.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@
88

99
package org.elasticsearch.action.admin.cluster.health;
1010

11+
import org.elasticsearch.Version;
1112
import org.elasticsearch.action.ActionResponse;
1213
import org.elasticsearch.cluster.ClusterState;
1314
import org.elasticsearch.cluster.health.ClusterHealthStatus;
1415
import org.elasticsearch.cluster.health.ClusterIndexHealth;
1516
import org.elasticsearch.cluster.health.ClusterStateHealth;
16-
import org.elasticsearch.xcontent.ParseField;
1717
import org.elasticsearch.common.Strings;
1818
import org.elasticsearch.common.io.stream.StreamInput;
1919
import org.elasticsearch.common.io.stream.StreamOutput;
20+
import org.elasticsearch.common.xcontent.StatusToXContentObject;
21+
import org.elasticsearch.core.RestApiVersion;
2022
import org.elasticsearch.core.TimeValue;
23+
import org.elasticsearch.rest.RestStatus;
2124
import org.elasticsearch.xcontent.ConstructingObjectParser;
2225
import org.elasticsearch.xcontent.ObjectParser;
23-
import org.elasticsearch.common.xcontent.StatusToXContentObject;
26+
import org.elasticsearch.xcontent.ParseField;
2427
import org.elasticsearch.xcontent.XContentBuilder;
2528
import org.elasticsearch.xcontent.XContentParser;
26-
import org.elasticsearch.rest.RestStatus;
2729

2830
import java.io.IOException;
2931
import java.util.HashMap;
@@ -98,10 +100,6 @@ public class ClusterHealthResponse extends ActionResponse implements StatusToXCo
98100

99101
private static final ObjectParser.NamedObjectParser<ClusterIndexHealth, Void> INDEX_PARSER =
100102
(XContentParser parser, Void context, String index) -> ClusterIndexHealth.innerFromXContent(parser, index);
101-
private static final String ES_CLUSTER_HEALTH_REQUEST_TIMEOUT_200_KEY = "es.cluster_health.request_timeout_200";
102-
static final String CLUSTER_HEALTH_REQUEST_TIMEOUT_DEPRECATION_MSG = "The HTTP status code for a cluster health timeout " +
103-
"will be changed from 408 to 200 in a future version. Set the [" + ES_CLUSTER_HEALTH_REQUEST_TIMEOUT_200_KEY + "] " +
104-
"system property to [true] to suppress this message and opt in to the future behaviour now.";
105103

106104
static {
107105
// ClusterStateHealth fields
@@ -134,6 +132,7 @@ public class ClusterHealthResponse extends ActionResponse implements StatusToXCo
134132
private boolean timedOut = false;
135133
private ClusterStateHealth clusterStateHealth;
136134
private ClusterHealthStatus clusterHealthStatus;
135+
private RestApiVersion restApiVersion = RestApiVersion.current();
137136

138137
public ClusterHealthResponse() {
139138
}
@@ -148,22 +147,28 @@ public ClusterHealthResponse(StreamInput in) throws IOException {
148147
numberOfInFlightFetch = in.readInt();
149148
delayedUnassignedShards= in.readInt();
150149
taskMaxWaitingTime = in.readTimeValue();
150+
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
151+
restApiVersion = RestApiVersion.valueOf(in.readString());
152+
}
151153
}
152154

153155
/** needed for plugins BWC */
154156
public ClusterHealthResponse(String clusterName, String[] concreteIndices, ClusterState clusterState) {
155-
this(clusterName, concreteIndices, clusterState, -1, -1, -1, TimeValue.timeValueHours(0));
157+
this(clusterName, concreteIndices, clusterState, -1, -1, -1,
158+
TimeValue.timeValueHours(0), RestApiVersion.current());
156159
}
157160

158161
public ClusterHealthResponse(String clusterName, String[] concreteIndices, ClusterState clusterState, int numberOfPendingTasks,
159-
int numberOfInFlightFetch, int delayedUnassignedShards, TimeValue taskMaxWaitingTime) {
162+
int numberOfInFlightFetch, int delayedUnassignedShards, TimeValue taskMaxWaitingTime,
163+
RestApiVersion restApiVersion) {
160164
this.clusterName = clusterName;
161165
this.numberOfPendingTasks = numberOfPendingTasks;
162166
this.numberOfInFlightFetch = numberOfInFlightFetch;
163167
this.delayedUnassignedShards = delayedUnassignedShards;
164168
this.taskMaxWaitingTime = taskMaxWaitingTime;
165169
this.clusterStateHealth = new ClusterStateHealth(clusterState, concreteIndices);
166170
this.clusterHealthStatus = clusterStateHealth.getStatus();
171+
this.restApiVersion = restApiVersion;
167172
}
168173

169174
/**
@@ -281,6 +286,10 @@ public double getActiveShardsPercent() {
281286
return clusterStateHealth.getActiveShardsPercent();
282287
}
283288

289+
public void setRestApiVersion(RestApiVersion restApiVersion) {
290+
this.restApiVersion = restApiVersion;
291+
}
292+
284293
public static ClusterHealthResponse readResponseFrom(StreamInput in) throws IOException {
285294
return new ClusterHealthResponse(in);
286295
}
@@ -295,6 +304,9 @@ public void writeTo(StreamOutput out) throws IOException {
295304
out.writeInt(numberOfInFlightFetch);
296305
out.writeInt(delayedUnassignedShards);
297306
out.writeTimeValue(taskMaxWaitingTime);
307+
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
308+
out.writeString(restApiVersion.name());
309+
}
298310
}
299311

300312
@Override
@@ -304,8 +316,8 @@ public String toString() {
304316

305317
@Override
306318
public RestStatus status() {
307-
if (isTimedOut() == false) {
308-
return RestStatus.OK;
319+
if (isTimedOut() && restApiVersion.equals(RestApiVersion.V_7)) {
320+
return RestStatus.REQUEST_TIMEOUT;
309321
}
310322
return RestStatus.OK;
311323
}
@@ -359,25 +371,13 @@ public boolean equals(Object o) {
359371
Objects.equals(taskMaxWaitingTime, that.taskMaxWaitingTime) &&
360372
timedOut == that.timedOut &&
361373
Objects.equals(clusterStateHealth, that.clusterStateHealth) &&
362-
clusterHealthStatus == that.clusterHealthStatus;
374+
clusterHealthStatus == that.clusterHealthStatus &&
375+
restApiVersion == that.restApiVersion;
363376
}
364377

365378
@Override
366379
public int hashCode() {
367380
return Objects.hash(clusterName, numberOfPendingTasks, numberOfInFlightFetch, delayedUnassignedShards, taskMaxWaitingTime,
368-
timedOut, clusterStateHealth, clusterHealthStatus);
369-
}
370-
371-
private static boolean readEsClusterHealthRequestTimeout200FromProperty() {
372-
String property = System.getProperty(ES_CLUSTER_HEALTH_REQUEST_TIMEOUT_200_KEY);
373-
if (property == null) {
374-
return false;
375-
}
376-
if (Boolean.parseBoolean(property)) {
377-
return true;
378-
} else {
379-
throw new IllegalArgumentException(ES_CLUSTER_HEALTH_REQUEST_TIMEOUT_200_KEY + " can only be unset or [true] but was ["
380-
+ property + "]");
381-
}
381+
timedOut, clusterStateHealth, clusterHealthStatus, restApiVersion);
382382
}
383383
}

server/src/main/java/org/elasticsearch/action/admin/cluster/health/TransportClusterHealthAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,13 @@ private ClusterHealthResponse clusterHealth(ClusterHealthRequest request, Cluste
337337
// one of the specified indices is not there - treat it as RED.
338338
ClusterHealthResponse response = new ClusterHealthResponse(clusterState.getClusterName().value(), Strings.EMPTY_ARRAY,
339339
clusterState, numberOfPendingTasks, numberOfInFlightFetch, UnassignedInfo.getNumberOfDelayedUnassigned(clusterState),
340-
pendingTaskTimeInQueue);
340+
pendingTaskTimeInQueue, request.restApiVersion());
341341
response.setStatus(ClusterHealthStatus.RED);
342342
return response;
343343
}
344344

345345
return new ClusterHealthResponse(clusterState.getClusterName().value(), concreteIndices, clusterState, numberOfPendingTasks,
346-
numberOfInFlightFetch, UnassignedInfo.getNumberOfDelayedUnassigned(clusterState), pendingTaskTimeInQueue);
346+
numberOfInFlightFetch, UnassignedInfo.getNumberOfDelayedUnassigned(clusterState), pendingTaskTimeInQueue,
347+
request.restApiVersion());
347348
}
348349
}

server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterHealthAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public static ClusterHealthRequest fromRequest(final RestRequest request) {
8181
if (request.param("wait_for_events") != null) {
8282
clusterHealthRequest.waitForEvents(Priority.valueOf(request.param("wait_for_events").toUpperCase(Locale.ROOT)));
8383
}
84+
clusterHealthRequest.setRestApiVersion(request.getRestApiVersion());
8485
return clusterHealthRequest;
8586
}
8687

server/src/main/java/org/elasticsearch/rest/action/cat/RestHealthAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ protected void documentation(StringBuilder sb) {
4747
@Override
4848
public RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
4949
ClusterHealthRequest clusterHealthRequest = new ClusterHealthRequest();
50+
clusterHealthRequest.setRestApiVersion(request.getRestApiVersion());
5051

5152
return channel -> client.admin().cluster().health(clusterHealthRequest, new RestResponseListener<ClusterHealthResponse>(channel) {
5253
@Override

server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.elasticsearch.common.settings.Settings;
3232
import org.elasticsearch.common.time.DateFormatter;
3333
import org.elasticsearch.common.unit.ByteSizeValue;
34+
import org.elasticsearch.core.RestApiVersion;
3435
import org.elasticsearch.core.TimeValue;
3536
import org.elasticsearch.index.IndexSettings;
3637
import org.elasticsearch.rest.RestRequest;
@@ -122,7 +123,7 @@ public void onResponse(final GetSettingsResponse getSettingsResponse) {
122123
sendClusterStateRequest(indices, subRequestIndicesOptions, masterNodeTimeout, client,
123124
ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure));
124125
sendClusterHealthRequest(indices, subRequestIndicesOptions, masterNodeTimeout, client,
125-
ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure));
126+
ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure), request.getRestApiVersion());
126127
}
127128
});
128129
};
@@ -168,12 +169,14 @@ private void sendClusterHealthRequest(final String[] indices,
168169
final IndicesOptions indicesOptions,
169170
final TimeValue masterNodeTimeout,
170171
final NodeClient client,
171-
final ActionListener<ClusterHealthResponse> listener) {
172+
final ActionListener<ClusterHealthResponse> listener,
173+
RestApiVersion restApiVersion) {
172174

173175
final ClusterHealthRequest request = new ClusterHealthRequest();
174176
request.indices(indices);
175177
request.indicesOptions(indicesOptions);
176178
request.masterNodeTimeout(masterNodeTimeout);
179+
request.setRestApiVersion(restApiVersion);
177180

178181
client.admin().cluster().health(request, listener);
179182
}

server/src/test/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthResponsesTests.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
import org.elasticsearch.common.io.stream.StreamInput;
2020
import org.elasticsearch.common.io.stream.Writeable;
2121
import org.elasticsearch.common.settings.Settings;
22+
import org.elasticsearch.core.RestApiVersion;
2223
import org.elasticsearch.core.TimeValue;
23-
import org.elasticsearch.xcontent.ToXContent;
24-
import org.elasticsearch.xcontent.XContentParser;
2524
import org.elasticsearch.rest.RestStatus;
2625
import org.elasticsearch.test.AbstractSerializingTestCase;
26+
import org.elasticsearch.xcontent.ToXContent;
27+
import org.elasticsearch.xcontent.XContentParser;
2728
import org.hamcrest.Matchers;
2829

2930
import java.io.IOException;
@@ -42,7 +43,20 @@
4243
public class ClusterHealthResponsesTests extends AbstractSerializingTestCase<ClusterHealthResponse> {
4344
private final ClusterHealthRequest.Level level = randomFrom(ClusterHealthRequest.Level.values());
4445

45-
public void testTimeoutReturns200() {
46+
public void testTimeoutReturns408WhenCompatibleWithV7() {
47+
ClusterHealthResponse res = new ClusterHealthResponse();
48+
res.setRestApiVersion(RestApiVersion.V_7);
49+
for (int i = 0; i < 5; i++) {
50+
res.setTimedOut(randomBoolean());
51+
if (res.isTimedOut()) {
52+
assertEquals(RestStatus.REQUEST_TIMEOUT, res.status());
53+
} else {
54+
assertEquals(RestStatus.OK, res.status());
55+
}
56+
}
57+
}
58+
59+
public void testAlwaysReturns200RegardlessTimeout() {
4660
ClusterHealthResponse res = new ClusterHealthResponse();
4761
for (int i = 0; i < 5; i++) {
4862
res.setTimedOut(randomBoolean());
@@ -57,7 +71,7 @@ public void testClusterHealth() throws IOException {
5771
int delayedUnassigned = randomIntBetween(0, 200);
5872
TimeValue pendingTaskInQueueTime = TimeValue.timeValueMillis(randomIntBetween(1000, 100000));
5973
ClusterHealthResponse clusterHealth = new ClusterHealthResponse("bla", new String[] {Metadata.ALL},
60-
clusterState, pendingTasks, inFlight, delayedUnassigned, pendingTaskInQueueTime);
74+
clusterState, pendingTasks, inFlight, delayedUnassigned, pendingTaskInQueueTime, RestApiVersion.current());
6175
clusterHealth = maybeSerialize(clusterHealth);
6276
assertClusterHealth(clusterHealth);
6377
assertThat(clusterHealth.getNumberOfPendingTasks(), Matchers.equalTo(pendingTasks));

x-pack/plugin/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ tasks.named("yamlRestTestV7CompatTransform").configure{ task ->
9898
task.skipTest("rollup/put_job/Test basic put_job", "rollup was an experimental feature, also see #41227")
9999
task.skipTest("rollup/start_job/Test start job twice", "rollup was an experimental feature, also see #41227")
100100
task.skipTest("ml/trained_model_cat_apis/Test cat trained models", "A type field was added to cat.ml_trained_models #73660, this is a backwards compatible change. Still this is a cat api, and we don't support them with rest api compatibility. (the test would be very hard to transform too)")
101-
task.skipTest("rollup/security_tests/Attribute-based access", "Status code changed to 200 OK in 8.0.0")
102-
task.skipTest("rollup/security_tests/Index-based access", "Status code changed to 200 OK in 8.0.0")
103101

104102
task.replaceKeyInDo("license.delete", "xpack-license.delete")
105103
task.replaceKeyInDo("license.get", "xpack-license.get")

0 commit comments

Comments
 (0)