Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.<Header>emptyList(), HeapBufferedResponseConsumerFactory.DEFAULT).build();
Collections.<Header>emptyList(), HeapBufferedResponseConsumerFactory.DEFAULT, null).build();

private final List<Header> 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);
}

/**
Expand All @@ -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();
Expand Down Expand Up @@ -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<Header> headers;
private HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory;
private Boolean strictDeprecationMode;

private Builder(List<Header> headers, HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory) {
private Builder(List<Header> headers, HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory,
Boolean strictDeprecationMode) {
this.headers = new ArrayList<>(headers);
this.httpAsyncResponseConsumerFactory = httpAsyncResponseConsumerFactory;
this.strictDeprecationMode = strictDeprecationMode;
}

/**
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Iterator<Node>> nodeTuple, final HttpRequestBase request,
final Set<Integer> ignoreErrorCodes,
final boolean thisRequestStrictDeprecationMode,
final HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory,
final FailureTrackingResponseListener listener) {
final Node node = nodeTuple.nodes.next();
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,17 @@ private void assertDeprecationWarnings(List<String> warningHeaderTexts, List<Str
for (String warningHeaderText : warningHeaderTexts) {
options.addHeader("Warning", warningHeaderText);
}
boolean thisRequestStrictDeprecationMode;
if (randomBoolean()) {
thisRequestStrictDeprecationMode = randomBoolean();
options.setStrictDeprecationMode(thisRequestStrictDeprecationMode);
} else {
thisRequestStrictDeprecationMode = strictDeprecationMode;
}
request.setOptions(options);

Response response;
if (strictDeprecationMode) {
if (thisRequestStrictDeprecationMode) {
try {
restClient.performRequest(request);
fail("expected ResponseException because strict deprecation mode is enabled");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksAction;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
Expand Down Expand Up @@ -618,7 +619,11 @@ protected RestClient buildClient(Settings settings, HttpHost[] hosts) throws IOE
/**
* Whether the used REST client should return any response containing at
* least one warning header as a failure.
* @deprecated always run in strict mode and use
* {@link RequestOptions.Builder#setStrictDeprecationMode} to enable
* permissive mode for individual requests
*/
@Deprecated
protected boolean getStrictDeprecationMode() {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ protected static void setOptions(Request request, Map<String, String> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,4 @@ protected final RestClientBuilder getClientBuilderWithSniffedHosts() throws IOEx
configureClient(builder, restClientSettings());
return builder;
}

@Override
protected boolean getStrictDeprecationMode() {
return false;
}
}