|
13 | 13 | import org.elasticsearch.cluster.health.ClusterHealthStatus;
|
14 | 14 | import org.elasticsearch.cluster.health.ClusterIndexHealth;
|
15 | 15 | import org.elasticsearch.cluster.health.ClusterStateHealth;
|
| 16 | +import org.elasticsearch.common.logging.DeprecationLogger; |
16 | 17 | import org.elasticsearch.xcontent.ParseField;
|
17 | 18 | import org.elasticsearch.common.Strings;
|
18 | 19 | import org.elasticsearch.common.io.stream.StreamInput;
|
|
24 | 25 | import org.elasticsearch.xcontent.XContentBuilder;
|
25 | 26 | import org.elasticsearch.xcontent.XContentParser;
|
26 | 27 | import org.elasticsearch.rest.RestStatus;
|
| 28 | +import org.elasticsearch.rest.action.search.RestSearchAction; |
27 | 29 |
|
28 | 30 | import java.io.IOException;
|
29 | 31 | import java.util.HashMap;
|
@@ -55,6 +57,7 @@ public class ClusterHealthResponse extends ActionResponse implements StatusToXCo
|
55 | 57 | private static final String INITIALIZING_SHARDS = "initializing_shards";
|
56 | 58 | private static final String UNASSIGNED_SHARDS = "unassigned_shards";
|
57 | 59 | private static final String INDICES = "indices";
|
| 60 | + private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestSearchAction.class); |
58 | 61 |
|
59 | 62 | private static final ConstructingObjectParser<ClusterHealthResponse, Void> PARSER =
|
60 | 63 | new ConstructingObjectParser<>("cluster_health_response", true,
|
@@ -97,7 +100,11 @@ public class ClusterHealthResponse extends ActionResponse implements StatusToXCo
|
97 | 100 | });
|
98 | 101 |
|
99 | 102 | private static final ObjectParser.NamedObjectParser<ClusterIndexHealth, Void> INDEX_PARSER =
|
100 |
| - (XContentParser parser, Void context, String index) -> ClusterIndexHealth.innerFromXContent(parser, index); |
| 103 | + (XContentParser parser, Void context, String index) -> ClusterIndexHealth.innerFromXContent(parser, index); |
| 104 | + private static final String ES_CLUSTER_HEALTH_REQUEST_TIMEOUT_200_KEY = "es.cluster_health.request_timeout_200"; |
| 105 | + static final String CLUSTER_HEALTH_REQUEST_TIMEOUT_DEPRECATION_MSG = "The HTTP status code for a cluster health timeout " + |
| 106 | + "will be changed from 408 to 200 in a future version. Set the [" + ES_CLUSTER_HEALTH_REQUEST_TIMEOUT_200_KEY + "] " + |
| 107 | + "system property to [true] to suppress this message and opt in to the future behaviour now."; |
101 | 108 |
|
102 | 109 | static {
|
103 | 110 | // ClusterStateHealth fields
|
@@ -130,8 +137,15 @@ public class ClusterHealthResponse extends ActionResponse implements StatusToXCo
|
130 | 137 | private boolean timedOut = false;
|
131 | 138 | private ClusterStateHealth clusterStateHealth;
|
132 | 139 | private ClusterHealthStatus clusterHealthStatus;
|
| 140 | + private boolean esClusterHealthRequestTimeout200 = readEsClusterHealthRequestTimeout200FromProperty(); |
133 | 141 |
|
134 |
| - public ClusterHealthResponse() {} |
| 142 | + public ClusterHealthResponse() { |
| 143 | + } |
| 144 | + |
| 145 | + /** For the testing of opting in for the 200 status code without setting a system property */ |
| 146 | + ClusterHealthResponse(boolean esClusterHealthRequestTimeout200) { |
| 147 | + this.esClusterHealthRequestTimeout200 = esClusterHealthRequestTimeout200; |
| 148 | + } |
135 | 149 |
|
136 | 150 | public ClusterHealthResponse(StreamInput in) throws IOException {
|
137 | 151 | super(in);
|
@@ -299,7 +313,15 @@ public String toString() {
|
299 | 313 |
|
300 | 314 | @Override
|
301 | 315 | public RestStatus status() {
|
302 |
| - return isTimedOut() ? RestStatus.REQUEST_TIMEOUT : RestStatus.OK; |
| 316 | + if (isTimedOut() == false) { |
| 317 | + return RestStatus.OK; |
| 318 | + } |
| 319 | + if (esClusterHealthRequestTimeout200) { |
| 320 | + return RestStatus.OK; |
| 321 | + } else { |
| 322 | + deprecationLogger.compatibleCritical("cluster_health_request_timeout", CLUSTER_HEALTH_REQUEST_TIMEOUT_DEPRECATION_MSG); |
| 323 | + return RestStatus.REQUEST_TIMEOUT; |
| 324 | + } |
303 | 325 | }
|
304 | 326 |
|
305 | 327 | @Override
|
@@ -359,4 +381,17 @@ public int hashCode() {
|
359 | 381 | return Objects.hash(clusterName, numberOfPendingTasks, numberOfInFlightFetch, delayedUnassignedShards, taskMaxWaitingTime,
|
360 | 382 | timedOut, clusterStateHealth, clusterHealthStatus);
|
361 | 383 | }
|
| 384 | + |
| 385 | + private static boolean readEsClusterHealthRequestTimeout200FromProperty() { |
| 386 | + String property = System.getProperty(ES_CLUSTER_HEALTH_REQUEST_TIMEOUT_200_KEY); |
| 387 | + if (property == null) { |
| 388 | + return false; |
| 389 | + } |
| 390 | + if (Boolean.parseBoolean(property)) { |
| 391 | + return true; |
| 392 | + } else { |
| 393 | + throw new IllegalArgumentException(ES_CLUSTER_HEALTH_REQUEST_TIMEOUT_200_KEY + " can only be unset or [true] but was [" |
| 394 | + + property + "]"); |
| 395 | + } |
| 396 | + } |
362 | 397 | }
|
0 commit comments