Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry HTTP operation in case IOException too (exponential backoff) #3293

Merged
merged 1 commit into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#### Dependency Upgrade

#### New Features
* Fix #3291: Retrying the HTTP operation in case of IOException too

### 5.5.0 (2021-06-30)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,20 +578,28 @@ protected <T> T handleResponse(OkHttpClient client, Request.Builder requestBuild
}

protected Response retryWithExponentialBackoff(OkHttpClient client, Request request) throws InterruptedException, IOException {
Response response;
boolean doRetry;
int numRetries = 0;
do {
response = client.newCall(request).execute();
doRetry = numRetries < requestRetryBackoffLimit && response.code() >= 500;
if (doRetry) {
long retryInterval= retryIntervalCalculator.getInterval(numRetries);
LOG.debug("HTTP operation on url: {} should be retried as the response code was {}, retrying after {} millis", request.url(), response.code(), retryInterval);
Thread.sleep(retryInterval);
numRetries++;
long retryInterval;
while (true) {
try {
Response response = client.newCall(request).execute();
if (numRetries < requestRetryBackoffLimit && response.code() >= 500) {
retryInterval = retryIntervalCalculator.getInterval(numRetries);
LOG.debug("HTTP operation on url: {} should be retried as the response code was {}, retrying after {} millis", request.url(), response.code(), retryInterval);
} else {
return response;
}
} catch (IOException ie) {
if (numRetries < requestRetryBackoffLimit) {
retryInterval = retryIntervalCalculator.getInterval(numRetries);
LOG.debug(String.format("HTTP operation on url: %s should be retried after %d millis because of IOException", request.url(), retryInterval), ie);
} else {
throw ie;
}
}
} while(doRetry);
return response;
Thread.sleep(retryInterval);
numRetries++;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,13 @@ private OkHttpClient newHttpClientWithSomeFailures(final AtomicInteger httpExecu
when(mockCall.execute()).thenAnswer(i -> {
int count = httpExecutionCounter.getAndIncrement();
if (count < numFailures) {
return new Response.Builder().request(req).message("Internal Server Error").protocol(HTTP_1_1).code(500).build();
// Altering the type of the error for each call:
// even numbered calls (including the first call) fail with an IOException and odd numbered calls fail with HTTP response 500
if (count % 2 == 0) {
throw new IOException("For example java.net.ConnectException");
} else {
return new Response.Builder().request(req).message("Internal Server Error").protocol(HTTP_1_1).code(500).build();
}
} else {
Pod podNoLabels = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build();
ResponseBody body = ResponseBody.create(MediaType.get("application/json"), Serialization.asJson(podNoLabels));
Expand Down Expand Up @@ -269,7 +275,8 @@ void testNoHttpRetryWithDefaultConfig() throws MalformedURLException, IOExceptio
});

// Then
assertTrue(exception.getMessage().contains("Internal Server Error"));
assertTrue("As the first failure is an IOException the message of the causedBy expected to contain the given text: 'For example java.net.ConnectException'!",
exception.getCause().getMessage().contains("For example java.net.ConnectException"));
assertEquals(1, httpExecutionCounter.get());
}

Expand All @@ -290,7 +297,8 @@ void testHttpRetryWithMoreFailuresThanRetries() throws MalformedURLException, IO
});

// Then
assertTrue(exception.getMessage().contains("Internal Server Error"));
assertTrue("As the last failure, the 3rd one, is not an IOException the message expected to contain: 'Internal Server Error'!",
exception.getMessage().contains("Internal Server Error"));
assertEquals("Expected 4 calls: one normal try and 3 backoff retries!", 4, httpExecutionCounter.get());
}

Expand Down