-
Notifications
You must be signed in to change notification settings - Fork 523
feat: add automatic S3 request retry with exponential backoff #1701
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
Open
allanrogerr
wants to merge
18
commits into
minio:master
Choose a base branch
from
allanrogerr:feature/retry-mechanism
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9524c1f
feat: add automatic S3 request retry with exponential backoff
allanrogerr a79aa11
fix: address Copilot review on retry mechanism
allanrogerr beead6e
fix: strengthen exception assertions in RetryTest and guard regionCac…
allanrogerr 53da859
fix: replace instanceof assertions with typed catch to satisfy SpotBu…
allanrogerr 60aeaed
fix: use ThreadLocalRandom for backoff jitter, disable OkHttp retry, …
allanrogerr d1c52e0
style: fix Spotless formatting violations in BaseS3Client
allanrogerr 0d687ec
fix: propagate runAsync dispatch failure into retryFuture
allanrogerr 34abd64
refactor: move retry to OkHttp interceptor, drop S3-code retry
allanrogerr 1997b48
fix: narrow throws clauses in RetryTest to satisfy SpotBugs
allanrogerr b2cad44
Merge branch 'master' into feature/retry-mechanism
allanrogerr ba65312
refactor: strip retry to balamurugana's interceptor proposal scope
allanrogerr 6d39a12
feat: restore full retry capability on top of OkHttp interceptor
allanrogerr 756e294
docs(retry): clarify RetryInterceptor as supported API; add terminal-…
allanrogerr 2f2fb4f
fix(retry): cancellation awareness, broader docs, tighter scope, more…
allanrogerr 7fe6e87
fix(retry): drop public Javadoc links to package-private Retry; strip…
allanrogerr 9a5c8e4
fix(retry): address bala review on PR #1701 review 4248622939
allanrogerr 93e4f47
fix(retry): address bala review batch on PR #1701 review 4250022733
allanrogerr 0ae488b
fix(retry): drop Retry.java; revert createBody bracket per r3206778779
allanrogerr 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /* | ||
|
allanrogerr marked this conversation as resolved.
Outdated
|
||
| * MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2026 MinIO, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.minio; | ||
|
|
||
| import com.google.common.collect.ImmutableSet; | ||
| import io.minio.errors.ErrorResponseException; | ||
| import io.minio.errors.InvalidResponseException; | ||
| import io.minio.errors.ServerException; | ||
| import java.io.IOException; | ||
| import java.util.Random; | ||
| import java.util.Set; | ||
| import java.util.concurrent.CompletionException; | ||
| import javax.net.ssl.SSLHandshakeException; | ||
|
|
||
| /** Retry configuration and helpers for S3 request execution. */ | ||
| class Retry { | ||
| /** Default maximum number of retry attempts per request. */ | ||
| static final int MAX_RETRY = 10; | ||
|
|
||
| /** Base sleep unit for exponential backoff (milliseconds). */ | ||
| static final long RETRY_BASE_MS = 200L; | ||
|
|
||
| /** Maximum sleep cap for exponential backoff (milliseconds). */ | ||
| static final long RETRY_CAP_MS = 1_000L; | ||
|
|
||
| /** | ||
| * S3 error codes that should trigger a retry. Matches the retryableS3Codes set from minio-go | ||
| * retry.go. | ||
| */ | ||
| private static final Set<String> RETRYABLE_S3_CODES = | ||
| ImmutableSet.of( | ||
| "RequestError", | ||
| "RequestTimeout", | ||
| "Throttling", | ||
| "ThrottlingException", | ||
| "RequestLimitExceeded", | ||
| "RequestThrottled", | ||
| "InternalError", | ||
| "ExpiredToken", | ||
| "ExpiredTokenException", | ||
| "SlowDown", | ||
| "SlowDownWrite", | ||
| "SlowDownRead"); | ||
|
|
||
| /** | ||
| * HTTP status codes that should trigger a retry. Matches retryableHTTPStatusCodes from minio-go | ||
| * retry.go. | ||
| */ | ||
| private static final Set<Integer> RETRYABLE_HTTP_CODES = | ||
| ImmutableSet.of( | ||
| 408, // Request Timeout | ||
| 429, // Too Many Requests | ||
| 499, // Client Closed Request (nginx) | ||
| 500, // Internal Server Error | ||
| 502, // Bad Gateway | ||
| 503, // Service Unavailable | ||
| 504, // Gateway Timeout | ||
| 520 // Cloudflare unknown error | ||
| ); | ||
|
allanrogerr marked this conversation as resolved.
Outdated
|
||
|
|
||
| static boolean isRetryableS3Code(String code) { | ||
| return code != null && RETRYABLE_S3_CODES.contains(code); | ||
| } | ||
|
|
||
| static boolean isRetryableHttpCode(int code) { | ||
| return RETRYABLE_HTTP_CODES.contains(code); | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the IOException is retryable. Non-retryable: TLS handshake failures, HTTP/HTTPS | ||
| * protocol mismatch. Everything else (connection reset, EOF, server closed idle connection) is | ||
| * retried. | ||
| */ | ||
| static boolean isRetryableIOException(IOException e) { | ||
| // TLS certificate / handshake failures are not retryable. | ||
| if (e instanceof SSLHandshakeException) return false; | ||
| String msg = e.getMessage(); | ||
| // Protocol mismatch is not retryable. | ||
| if (msg != null && msg.contains("server gave HTTP response to HTTPS client")) return false; | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the throwable represents a retryable failure. Handles IOException, | ||
| * ErrorResponseException, ServerException, and InvalidResponseException. | ||
| */ | ||
| static boolean isRetryable(Throwable t) { | ||
| if (t instanceof CompletionException) t = t.getCause(); | ||
| if (t == null) return false; | ||
|
|
||
| if (t instanceof IOException) { | ||
| return isRetryableIOException((IOException) t); | ||
| } | ||
|
|
||
| if (t instanceof ErrorResponseException) { | ||
| ErrorResponseException e = (ErrorResponseException) t; | ||
| String code = e.errorResponse().code(); | ||
| // "RetryHead" is handled separately by executeHeadAsync — must not be swallowed here. | ||
| if ("RetryHead".equals(code)) return false; | ||
| if (isRetryableS3Code(code)) return true; | ||
| if (e.response() != null && isRetryableHttpCode(e.response().code())) return true; | ||
| return false; | ||
| } | ||
|
|
||
| if (t instanceof ServerException) { | ||
| return isRetryableHttpCode(((ServerException) t).statusCode()); | ||
| } | ||
|
|
||
| if (t instanceof InvalidResponseException) { | ||
| return isRetryableHttpCode(((InvalidResponseException) t).responseCode()); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Computes the full-jitter exponential backoff delay for retry {@code attempt} (1-indexed: 1 = | ||
| * first retry). Matches minio-go's {@code exponentialBackoffWait(i)}: | ||
| * | ||
| * <pre> | ||
| * attempt=1 → [0, 200 ms] | ||
| * attempt=2 → [0, 400 ms] | ||
| * attempt=3 → [0, 800 ms] | ||
| * attempt=4+→ [0, 1000 ms] (capped) | ||
| * </pre> | ||
| * | ||
| * Pass {@code attempt <= 0} to get 0 (no delay). | ||
| */ | ||
| static long computeBackoffMs(int attempt, Random random) { | ||
| if (attempt <= 0) return 0L; | ||
| // exp = attempt-1 so that attempt=1 maps to base*2^0=200ms cap | ||
| int exp = Math.min(attempt - 1, 30); | ||
| long cap = Math.min(RETRY_CAP_MS, RETRY_BASE_MS * (1L << exp)); | ||
| return (long) (random.nextDouble() * cap); | ||
| } | ||
|
|
||
| private Retry() {} | ||
| } | ||
|
allanrogerr marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.