Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions client/rest/src/main/java/org/elasticsearch/client/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.ConnectionClosedException;
import org.apache.http.ContentTooLongException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
Expand Down Expand Up @@ -298,7 +299,7 @@ private Response performRequest(final NodeTuple<Iterator<Node>> tuple, final Int
onFailure(context.node);
Exception cause = extractAndWrapCause(e);
addSuppressedException(previousException, cause);
if (tuple.nodes.hasNext()) {
if (isRetryableException(e) && tuple.nodes.hasNext()) {
return performRequest(tuple, request, cause);
}
if (cause instanceof IOException) {
Expand Down Expand Up @@ -414,7 +415,7 @@ public void failed(Exception failure) {
try {
RequestLogger.logFailedRequest(logger, request.httpRequest, context.node, failure);
onFailure(context.node);
if (tuple.nodes.hasNext()) {
if (isRetryableException(failure) && tuple.nodes.hasNext()) {
listener.trackFailure(failure);
performRequestAsync(tuple, request, listener);
} else {
Expand Down Expand Up @@ -563,6 +564,19 @@ private static boolean isSuccessfulResponse(int statusCode) {
return statusCode < 300;
}

/**
* Should an exception cause retrying the request?
*/
private static boolean isRetryableException(Throwable e) {
if (e instanceof ExecutionException) {
e = e.getCause();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to say it'd be better to use the try in the caller, but seeing this thing, I get why you didn't.

if (e instanceof ContentTooLongException) {
return false;
}
return true;
}

private static boolean isRetryStatus(int statusCode) {
switch (statusCode) {
case 502:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@
import org.junit.Ignore;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static org.elasticsearch.client.RestClientTestUtil.getAllStatusCodes;
import static org.elasticsearch.client.RestClientTestUtil.randomErrorNoRetryStatusCode;
Expand Down Expand Up @@ -86,10 +89,17 @@ public static void startHttpServer() throws Exception {
}

private static RestClient buildRestClient(NodeSelector nodeSelector) {
return buildRestClient(nodeSelector, null);
}

private static RestClient buildRestClient(NodeSelector nodeSelector, RestClient.FailureListener failureListener) {
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);
if (pathPrefix.length() > 0) {
restClientBuilder.setPathPrefix((randomBoolean() ? "/" : "") + pathPrefixWithoutLeadingSlash);
}
if (failureListener != null) {
restClientBuilder.setFailureListener(failureListener);
}
restClientBuilder.setNodeSelector(nodeSelector);
return restClientBuilder.build();
}
Expand All @@ -101,6 +111,7 @@ private static HttpServer createHttpServer() throws Exception {
for (int statusCode : getAllStatusCodes()) {
httpServer.createContext(pathPrefix + "/" + statusCode, new ResponseHandler(statusCode));
}
httpServer.createContext(pathPrefix + "/20bytes", new ResponseHandlerWithContent());
httpServer.createContext(pathPrefix + "/wait", waitForCancelHandler);
return httpServer;
}
Expand Down Expand Up @@ -153,6 +164,18 @@ public void handle(HttpExchange httpExchange) throws IOException {
}
}

private static class ResponseHandlerWithContent implements HttpHandler {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
byte[] body = "01234567890123456789".getBytes(StandardCharsets.UTF_8);
httpExchange.sendResponseHeaders(200, body.length);
try (OutputStream out = httpExchange.getResponseBody()) {
out.write(body);
}
httpExchange.close();
}
}

@AfterClass
public static void stopHttpServers() throws IOException {
restClient.close();
Expand Down Expand Up @@ -303,6 +326,34 @@ public void testNodeSelector() throws Exception {
}
}

public void testNonRetryableException() throws Exception {
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.setHttpAsyncResponseConsumerFactory(
// Limit to very short responses to trigger a ContentTooLongException
() -> new HeapBufferedAsyncResponseConsumer(10)
);

AtomicInteger failureCount = new AtomicInteger();
RestClient client = buildRestClient(NodeSelector.ANY, new RestClient.FailureListener() {
@Override
public void onFailure(Node node) {
failureCount.incrementAndGet();
}
});

failureCount.set(0);
Request request = new Request("POST", "/20bytes");
request.setOptions(options);
try {
RestClientSingleHostTests.performRequestSyncOrAsync(client, request);
fail("Request should not succeed");
} catch (IOException e) {
assertEquals(stoppedFirstHost ? 2 : 1, failureCount.intValue());
}

client.close();
}

private static class TestResponse {
private final String method;
private final int statusCode;
Expand Down
6 changes: 6 additions & 0 deletions docs/changelog/87248.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 87248
summary: Do not retry client requests when failing with `ContentTooLargeException`
area: Java Low Level REST Client
type: bug
issues:
- 86041