diff --git a/client/rest/src/main/java/org/elasticsearch/client/RequestOptions.java b/client/rest/src/main/java/org/elasticsearch/client/RequestOptions.java index cf6bd3d49f59e..2bae41b80e9c2 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/RequestOptions.java +++ b/client/rest/src/main/java/org/elasticsearch/client/RequestOptions.java @@ -36,19 +36,27 @@ * manipulated without changing Elasticsearch's behavior. */ public final class RequestOptions { + /** + * Default request options. + */ public static final RequestOptions DEFAULT = new Builder( - Collections.
emptyList(), HeapBufferedResponseConsumerFactory.DEFAULT).build(); + Collections.
emptyList(), HeapBufferedResponseConsumerFactory.DEFAULT, null).build(); private final List
headers; private final HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory; + private final Boolean strictDeprecationMode; private RequestOptions(Builder builder) { this.headers = Collections.unmodifiableList(new ArrayList<>(builder.headers)); this.httpAsyncResponseConsumerFactory = builder.httpAsyncResponseConsumerFactory; + this.strictDeprecationMode = builder.strictDeprecationMode; } + /** + * Create a builder that contains these options but can be modified. + */ public Builder toBuilder() { - return new Builder(headers, httpAsyncResponseConsumerFactory); + return new Builder(headers, httpAsyncResponseConsumerFactory, strictDeprecationMode); } /** @@ -68,6 +76,16 @@ public HttpAsyncResponseConsumerFactory getHttpAsyncResponseConsumerFactory() { return httpAsyncResponseConsumerFactory; } + /** + * Override the client's default for + * {@link RestClientBuilder#setStrictDeprecationMode strict deprecation} + * . Null means accept the client's default, true means throw an + * exception if there are warnings, and false means don't throw. + */ + public Boolean getStrictDeprecationMode() { + return strictDeprecationMode; + } + @Override public String toString() { StringBuilder b = new StringBuilder(); @@ -106,13 +124,21 @@ public int hashCode() { return Objects.hash(headers, httpAsyncResponseConsumerFactory); } + /** + * Builds {@link RequestOptions}. Get one by calling + * {@link RequestOptions#toBuilder} on {@link RequestOptions#DEFAULT} or + * any other {@linkplain RequestOptions}. + */ public static class Builder { private final List
headers; private HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory; + private Boolean strictDeprecationMode; - private Builder(List
headers, HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory) { + private Builder(List
headers, HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory, + Boolean strictDeprecationMode) { this.headers = new ArrayList<>(headers); this.httpAsyncResponseConsumerFactory = httpAsyncResponseConsumerFactory; + this.strictDeprecationMode = strictDeprecationMode; } /** @@ -141,6 +167,16 @@ public void setHttpAsyncResponseConsumerFactory(HttpAsyncResponseConsumerFactory this.httpAsyncResponseConsumerFactory = Objects.requireNonNull(httpAsyncResponseConsumerFactory, "httpAsyncResponseConsumerFactory cannot be null"); } + + /** + * Override the client's default for + * {@link RestClientBuilder#setStrictDeprecationMode strict deprecation} + * . Null means accept the client's default, true means throw an + * exception if there are warnings, and false means don't throw. + */ + public void setStrictDeprecationMode(Boolean strictDeprecationMode) { + this.strictDeprecationMode = strictDeprecationMode; + } } /** diff --git a/client/rest/src/main/java/org/elasticsearch/client/RestClient.java b/client/rest/src/main/java/org/elasticsearch/client/RestClient.java index d68e371f31836..a91bf1fcf8b26 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/RestClient.java +++ b/client/rest/src/main/java/org/elasticsearch/client/RestClient.java @@ -274,12 +274,15 @@ void performRequestAsyncNoCatch(Request request, ResponseListener listener) thro setHeaders(httpRequest, request.getOptions().getHeaders()); FailureTrackingResponseListener failureTrackingResponseListener = new FailureTrackingResponseListener(listener); long startTime = System.nanoTime(); - performRequestAsync(startTime, nextNode(), httpRequest, ignoreErrorCodes, + boolean thisRequestStrictDeprecationMode = request.getOptions().getStrictDeprecationMode() == null ? + strictDeprecationMode : request.getOptions().getStrictDeprecationMode(); + performRequestAsync(startTime, nextNode(), httpRequest, ignoreErrorCodes, thisRequestStrictDeprecationMode, request.getOptions().getHttpAsyncResponseConsumerFactory(), failureTrackingResponseListener); } private void performRequestAsync(final long startTime, final NodeTuple> nodeTuple, final HttpRequestBase request, final Set ignoreErrorCodes, + final boolean thisRequestStrictDeprecationMode, final HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory, final FailureTrackingResponseListener listener) { final Node node = nodeTuple.nodes.next(); @@ -298,7 +301,7 @@ public void completed(HttpResponse httpResponse) { Response response = new Response(request.getRequestLine(), node.getHost(), httpResponse); if (isSuccessfulResponse(statusCode) || ignoreErrorCodes.contains(response.getStatusLine().getStatusCode())) { onResponse(node); - if (strictDeprecationMode && response.hasWarnings()) { + if (thisRequestStrictDeprecationMode && response.hasWarnings()) { listener.onDefinitiveFailure(new ResponseException(response)); } else { listener.onSuccess(response); @@ -343,7 +346,8 @@ private void retryIfPossible(Exception exception) { } else { listener.trackFailure(exception); request.reset(); - performRequestAsync(startTime, nodeTuple, request, ignoreErrorCodes, httpAsyncResponseConsumerFactory, listener); + performRequestAsync(startTime, nodeTuple, request, ignoreErrorCodes, + thisRequestStrictDeprecationMode, httpAsyncResponseConsumerFactory, listener); } } else { listener.onDefinitiveFailure(exception); diff --git a/client/rest/src/test/java/org/elasticsearch/client/RequestOptionsTests.java b/client/rest/src/test/java/org/elasticsearch/client/RequestOptionsTests.java index 19106792228d9..9ab1f934135b8 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RequestOptionsTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RequestOptionsTests.java @@ -118,6 +118,8 @@ static RequestOptions.Builder randomBuilder() { builder.setHttpAsyncResponseConsumerFactory(new HeapBufferedResponseConsumerFactory(1)); } + // NOCOMMIT randomize override strict mode + return builder; } diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostTests.java index 65eb92c0e8d45..3a428551706bc 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostTests.java @@ -357,10 +357,17 @@ private void assertDeprecationWarnings(List warningHeaderTexts, List headers) { logger.debug("Adding header {} with value {}", header.getKey(), header.getValue()); options.addHeader(header.getKey(), header.getValue()); } + // We check the warnings ourselves so we don't need the client to do it for us + options.setStrictDeprecationMode(false); request.setOptions(options); } diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ESClientYamlSuiteTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ESClientYamlSuiteTestCase.java index 14a843d086916..f04bead4fbfa1 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ESClientYamlSuiteTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ESClientYamlSuiteTestCase.java @@ -432,9 +432,4 @@ protected final RestClientBuilder getClientBuilderWithSniffedHosts() throws IOEx configureClient(builder, restClientSettings()); return builder; } - - @Override - protected boolean getStrictDeprecationMode() { - return false; - } }