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