-
Notifications
You must be signed in to change notification settings - Fork 997
2.1 behavior in standard, adaptive strats #6871
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
dagnir
wants to merge
1
commit into
feature/master/2026-new-retries
Choose a base branch
from
dongie/retry-strategy-v2_1_option
base: feature/master/2026-new-retries
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 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
|
|
||
| package software.amazon.awssdk.retries; | ||
|
|
||
| import java.time.Duration; | ||
| import software.amazon.awssdk.annotations.SdkPublicApi; | ||
| import software.amazon.awssdk.annotations.ThreadSafe; | ||
| import software.amazon.awssdk.retries.api.BackoffStrategy; | ||
|
|
@@ -56,15 +57,33 @@ public interface StandardRetryStrategy extends RetryStrategy { | |
| * </pre> | ||
| */ | ||
| static Builder builder() { | ||
| return builder(false); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new {@link StandardRetryStrategy.Builder} with v2.0 or v2.1 retry constants. | ||
| * | ||
| * @param retries2026Enabled when {@code true}, uses v2.1 constants (50ms base delay, differentiated token costs); | ||
| * when {@code false}, uses v2.0 constants (100ms base delay, uniform token costs) | ||
| */ | ||
| static Builder builder(boolean retries2026Enabled) { | ||
| Duration baseDelay = retries2026Enabled ? DefaultRetryStrategy.Standard.BASE_DELAY_V21 | ||
| : DefaultRetryStrategy.Standard.BASE_DELAY_V20; | ||
| int exceptionCost = retries2026Enabled ? DefaultRetryStrategy.Standard.DEFAULT_EXCEPTION_TOKEN_COST_V21 | ||
| : DefaultRetryStrategy.Standard.DEFAULT_EXCEPTION_TOKEN_COST_V20; | ||
| // 2026 does not treat throttling exceptions differently from others | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: same here |
||
| int throttlingCost = retries2026Enabled ? DefaultRetryStrategy.Standard.THROTTLING_EXCEPTION_TOKEN_COST_V21 | ||
| : exceptionCost; | ||
| return DefaultStandardRetryStrategy | ||
| .builder() | ||
| .maxAttempts(DefaultRetryStrategy.Standard.MAX_ATTEMPTS) | ||
| .tokenBucketStore(TokenBucketStore | ||
| .builder() | ||
| .tokenBucketMaxCapacity(DefaultRetryStrategy.Standard.TOKEN_BUCKET_SIZE) | ||
| .build()) | ||
| .tokenBucketExceptionCost(DefaultRetryStrategy.Standard.DEFAULT_EXCEPTION_TOKEN_COST) | ||
| .backoffStrategy(BackoffStrategy.exponentialDelay(DefaultRetryStrategy.Standard.BASE_DELAY, | ||
| .tokenBucketExceptionCost(exceptionCost) | ||
| .throttlingTokenBucketExceptionCost(throttlingCost) | ||
| .backoffStrategy(BackoffStrategy.exponentialDelay(baseDelay, | ||
| DefaultRetryStrategy.Standard.MAX_BACKOFF)) | ||
| .throttlingBackoffStrategy(BackoffStrategy.exponentialDelay(DefaultRetryStrategy.Standard.THROTTLED_BASE_DELAY, | ||
| DefaultRetryStrategy.Standard.MAX_BACKOFF)); | ||
|
|
||
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
137 changes: 137 additions & 0 deletions
137
...s/src/test/java/software/amazon/awssdk/retries/AdaptiveRetryStrategyV21ConstantsTest.java
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,137 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.retries; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.time.Duration; | ||
| import org.junit.jupiter.api.Test; | ||
| import software.amazon.awssdk.retries.api.AcquireInitialTokenResponse; | ||
| import software.amazon.awssdk.retries.api.RefreshRetryTokenRequest; | ||
| import software.amazon.awssdk.retries.api.RefreshRetryTokenResponse; | ||
| import software.amazon.awssdk.retries.api.RetryStrategy; | ||
| import software.amazon.awssdk.retries.api.RetryToken; | ||
| import software.amazon.awssdk.retries.api.internal.AcquireInitialTokenRequestImpl; | ||
| import software.amazon.awssdk.retries.internal.DefaultRetryToken; | ||
|
|
||
| /** | ||
| * Tests that {@code AdaptiveRetryStrategy.builder(boolean retries2026Enabled)} selects the correct | ||
| * v2.0 or v2.1 constants for base delay, exception token cost, and throttling token cost. | ||
| */ | ||
| class AdaptiveRetryStrategyV21ConstantsTest { | ||
|
|
||
| private static final int BUCKET_CAPACITY = 500; | ||
|
|
||
| @Test | ||
| void v21Enabled_nonThrottlingRetry_deducts14Tokens() { | ||
| RetryStrategy strategy = AdaptiveRetryStrategy.builder(true) | ||
| .retryOnException(t -> true) | ||
| .treatAsThrottling(t -> false) | ||
| .build(); | ||
|
|
||
| DefaultRetryToken token = retryOnceBeforeSuccess(strategy, new RuntimeException("transient")); | ||
| assertThat(token.capacityRemaining()).isEqualTo(BUCKET_CAPACITY - 14); | ||
| } | ||
|
|
||
| @Test | ||
| void v21Enabled_throttlingRetry_deducts5Tokens() { | ||
| RetryStrategy strategy = AdaptiveRetryStrategy.builder(true) | ||
| .retryOnException(t -> true) | ||
| .treatAsThrottling(t -> true) | ||
| .build(); | ||
|
|
||
| DefaultRetryToken token = retryOnceBeforeSuccess(strategy, new RuntimeException("throttled")); | ||
| assertThat(token.capacityRemaining()).isEqualTo(BUCKET_CAPACITY - 5); | ||
| } | ||
|
|
||
| @Test | ||
| void v20_nonThrottlingRetry_deducts5Tokens() { | ||
| RetryStrategy strategy = AdaptiveRetryStrategy.builder(false) | ||
| .retryOnException(t -> true) | ||
| .treatAsThrottling(t -> false) | ||
| .build(); | ||
|
|
||
| DefaultRetryToken token = retryOnceBeforeSuccess(strategy, new RuntimeException("transient")); | ||
| assertThat(token.capacityRemaining()).isEqualTo(BUCKET_CAPACITY - 5); | ||
| } | ||
|
|
||
| @Test | ||
| void v20_throttlingRetry_deducts5Tokens() { | ||
| RetryStrategy strategy = AdaptiveRetryStrategy.builder(false) | ||
| .retryOnException(t -> true) | ||
| .treatAsThrottling(t -> true) | ||
| .build(); | ||
|
|
||
| DefaultRetryToken token = retryOnceBeforeSuccess(strategy, new RuntimeException("throttled")); | ||
| assertThat(token.capacityRemaining()).isEqualTo(BUCKET_CAPACITY - 5); | ||
| } | ||
|
|
||
| @Test | ||
| void v21Enabled_backoffUses50msBaseDelay() { | ||
| RetryStrategy strategy = AdaptiveRetryStrategy.builder(true) | ||
| .retryOnException(t -> true) | ||
| .build(); | ||
|
|
||
| RefreshRetryTokenResponse response = refreshToken(strategy, new RuntimeException("err")); | ||
| // First retry delay should include exponential backoff component in [0, 50ms] | ||
| assertThat(response.delay()).isBetween(Duration.ZERO, Duration.ofMillis(50)); | ||
| } | ||
|
|
||
| @Test | ||
| void v20_backoffUses100msBaseDelay() { | ||
| RetryStrategy strategy = AdaptiveRetryStrategy.builder(false) | ||
| .retryOnException(t -> true) | ||
| .build(); | ||
|
|
||
| RefreshRetryTokenResponse response = refreshToken(strategy, new RuntimeException("err")); | ||
| // First retry delay should include exponential backoff component in [0, 100ms] | ||
| assertThat(response.delay()).isBetween(Duration.ZERO, Duration.ofMillis(100)); | ||
| } | ||
|
|
||
| @Test | ||
| void noArgBuilder_usesV20Constants() { | ||
| RetryStrategy strategy = AdaptiveRetryStrategy.builder() | ||
| .retryOnException(t -> true) | ||
| .treatAsThrottling(t -> false) | ||
| .build(); | ||
|
|
||
| DefaultRetryToken token = retryOnceBeforeSuccess(strategy, new RuntimeException("transient")); | ||
| // v2.0: exception cost is 5 | ||
| assertThat(token.capacityRemaining()).isEqualTo(BUCKET_CAPACITY - 5); | ||
| } | ||
|
|
||
| /** | ||
| * Acquires an initial token, triggers one retry. Returns the token after the retry (before success). | ||
| */ | ||
| private DefaultRetryToken retryOnceBeforeSuccess(RetryStrategy strategy, Exception failure) { | ||
| AcquireInitialTokenResponse initial = strategy.acquireInitialToken(AcquireInitialTokenRequestImpl.create("test")); | ||
| RetryToken token = initial.token(); | ||
|
|
||
| RefreshRetryTokenResponse refreshResponse = strategy.refreshRetryToken( | ||
| RefreshRetryTokenRequest.builder().token(token).failure(failure).build()); | ||
|
|
||
| return (DefaultRetryToken) refreshResponse.token(); | ||
| } | ||
|
|
||
| /** | ||
| * Acquires an initial token and triggers one refresh to get the backoff delay. | ||
| */ | ||
| private RefreshRetryTokenResponse refreshToken(RetryStrategy strategy, Exception failure) { | ||
| AcquireInitialTokenResponse initial = strategy.acquireInitialToken(AcquireInitialTokenRequestImpl.create("test")); | ||
| return strategy.refreshRetryToken( | ||
| RefreshRetryTokenRequest.builder().token(initial.token()).failure(failure).build()); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: For the comment here, In v2.1 the values are different right? Looks like it's v2.0 that treats them the same.