Skip to content

Commit

Permalink
Simplified retry loop logic
Browse files Browse the repository at this point in the history
  • Loading branch information
criminosis committed Apr 24, 2024
1 parent 340b591 commit 13e65bf
Showing 1 changed file with 10 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -574,31 +574,24 @@ private Response performRequest(String method, String path, byte[] requestData)

private Response performRequestWithRetry(Request request) throws IOException {
int retryCount = 0;
ResponseException lastException;
do {
if (retryCount > 0) {
while (true) {
try {
return delegate.performRequest(request);
} catch (ResponseException e) {
if (!retryOnErrorCodes.contains(e.getResponse().getStatusLine().getStatusCode()) || retryCount >= retryAttemptLimit) {
throw e;
}
//Wait before trying again
long waitDurationMs = Math.min((long) (retryInitialWaitMs * Math.pow(10, retryCount - 1)), retryMaxWaitMs);
log.warn("Retrying Elasticsearch request in {} ms. Attempt {} of {}", waitDurationMs, retryCount, retryAttemptLimit);
try {
Thread.sleep(waitDurationMs);
} catch (InterruptedException e) {
throw new RuntimeException(String.format("Thread interrupted while waiting for retry attempt %d of %d", retryCount, retryAttemptLimit), e);
} catch (InterruptedException interruptedException) {
throw new RuntimeException(String.format("Thread interrupted while waiting for retry attempt %d of %d", retryCount, retryAttemptLimit), interruptedException);

Check warning on line 590 in janusgraph-es/src/main/java/org/janusgraph/diskstorage/es/rest/RestElasticSearchClient.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

janusgraph-es/src/main/java/org/janusgraph/diskstorage/es/rest/RestElasticSearchClient.java#L590

Avoid throwing raw exception types.
}
}
try {
return delegate.performRequest(request);
} catch (ResponseException e) {
if (!retryOnErrorCodes.contains(e.getResponse().getStatusLine().getStatusCode())) {
throw e;
}
lastException = e;
}
retryCount++;
} while (retryCount <= retryAttemptLimit);

//If we've exceeded our retry limit then throw the last exception that was captured from retrying
throw lastException;
}
}

private Response performRequest(Request request, byte[] requestData) throws IOException {
Expand Down

0 comments on commit 13e65bf

Please sign in to comment.