Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.awscore.internal.AwsErrorCode;
import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.core.internal.retry.RetryPolicyAdapter;
import software.amazon.awssdk.core.internal.retry.SdkDefaultRetryStrategy;
import software.amazon.awssdk.core.retry.RetryMode;
import software.amazon.awssdk.core.retry.RetryUtils;
import software.amazon.awssdk.retries.AdaptiveRetryStrategy;
import software.amazon.awssdk.retries.DefaultRetryStrategy;
import software.amazon.awssdk.retries.LegacyRetryStrategy;
Expand Down Expand Up @@ -135,7 +137,7 @@ public static StandardRetryStrategy standardRetryStrategy() {
*/
public static StandardRetryStrategy standardRetryStrategy(boolean newRetries2026Enabled) {
StandardRetryStrategy.Builder builder = SdkDefaultRetryStrategy.standardRetryStrategyBuilder(newRetries2026Enabled);
return configure(builder).build();
return configure(builder, newRetries2026Enabled).build();
}

/**
Expand Down Expand Up @@ -167,7 +169,7 @@ public static AdaptiveRetryStrategy adaptiveRetryStrategy() {
*/
public static AdaptiveRetryStrategy adaptiveRetryStrategy(boolean newRetries2026Enabled) {
AdaptiveRetryStrategy.Builder builder = SdkDefaultRetryStrategy.adaptiveRetryStrategyBuilder(newRetries2026Enabled);
return configure(builder)
return configure(builder, newRetries2026Enabled)
.build();
}

Expand All @@ -179,7 +181,22 @@ public static AdaptiveRetryStrategy adaptiveRetryStrategy(boolean newRetries2026
* @return The given builder
*/
public static <T extends RetryStrategy.Builder<T, ?>> T configure(T builder) {
return configure(builder, false);
}

/**
* Configures a retry strategy using its builder to add AWS-specific retry exceptions.
*
* @param builder The builder to add the AWS-specific retry exceptions
* @param <T> The type of the builder extending {@link RetryStrategy.Builder}
* @return The given builder
*/
private static <T extends RetryStrategy.Builder<T, ?>> T configure(T builder, boolean newRetries2026Enabled) {
builder.retryOnException(AwsRetryStrategy::retryOnAwsRetryableErrors);
if (newRetries2026Enabled) {
builder.retryOnException(AwsRetryStrategy::isLimitExceededErrorCode);
builder.treatAsThrottling(AwsRetryStrategy::treatAsThrottlingV21);
}
markDefaultsAdded(builder);
return builder;
}
Expand All @@ -205,6 +222,25 @@ private static boolean retryOnAwsRetryableErrors(Throwable ex) {
return false;
}

/**
* Additionally, check for LimitExceededException as it was not previously treated as a throttling exception.
*/
private static boolean treatAsThrottlingV21(Throwable ex) {
if (!(ex instanceof SdkException)) {
return false;
}

SdkException sdkException = (SdkException) ex;

return RetryUtils.isThrottlingException(sdkException)
|| isLimitExceededErrorCode(sdkException);
}

private static boolean isLimitExceededErrorCode(Throwable ex) {
return ex instanceof AwsServiceException
&& "LimitExceededException".equals(((AwsServiceException) ex).awsErrorDetails().errorCode());
}

/**
* Returns a {@link RetryStrategy} that implements the legacy {@link RetryMode#ADAPTIVE} mode.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.awscore.retry;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.google.common.base.Supplier;
import java.time.Duration;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import software.amazon.awssdk.awscore.exception.AwsErrorDetails;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.retries.StandardRetryStrategy;
import software.amazon.awssdk.retries.api.AcquireInitialTokenRequest;
import software.amazon.awssdk.retries.api.RefreshRetryTokenRequest;
import software.amazon.awssdk.retries.api.RetryToken;
import software.amazon.awssdk.retries.api.TokenAcquisitionFailedException;
import software.amazon.awssdk.retries.internal.DefaultRetryToken;

public class AwsRetryStrategyTest {

Check warning on line 34 in core/aws-core/src/test/java/software/amazon/awssdk/awscore/retry/AwsRetryStrategyTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this 'public' modifier.

See more on https://sonarcloud.io/project/issues?id=aws_aws-sdk-java-v2&issues=AZ3a_-bmJh43CeE7aLPb&open=AZ3a_-bmJh43CeE7aLPb&pullRequest=6920

@ParameterizedTest
@CsvSource({"true", "false"})
void standardRetryStrategy_limitExceededException_retryBehaviorCorrect(boolean newRetries2026Enabled) {
StandardRetryStrategy strategy = AwsRetryStrategy.standardRetryStrategy(newRetries2026Enabled);

RetryToken token = strategy.acquireInitialToken(AcquireInitialTokenRequest.create("test")).token();
RefreshRetryTokenRequest refresh = RefreshRetryTokenRequest.builder()
.failure(createTestException("LimitExceededException"))
.token(token)
.build();

if (newRetries2026Enabled) {
assertThat(strategy.refreshRetryToken(refresh).delay()).isGreaterThanOrEqualTo(Duration.ZERO);
} else {
assertThatThrownBy(() -> strategy.refreshRetryToken(refresh))
.isInstanceOf(TokenAcquisitionFailedException.class)
.matches(e -> {
TokenAcquisitionFailedException acquireException = (TokenAcquisitionFailedException) e;
DefaultRetryToken exceptionToken = (DefaultRetryToken) acquireException.token();
return exceptionToken.state() == DefaultRetryToken.TokenState.NON_RETRYABLE_EXCEPTION;
});
}
}

@ParameterizedTest
@CsvSource({"Throttling",
"ThrottlingException",
"ThrottledException",
"RequestThrottledException",
"TooManyRequestsException",
"ProvisionedThroughputExceededException",
"TransactionInProgressException",
"RequestLimitExceeded",
"BandwidthLimitExceeded",
"LimitExceededException",
"RequestThrottled",
"SlowDown",
"PriorRequestNotComplete",
"EC2ThrottledException"})
void standardRetryStrategy_retry21_throttlingBehaviorCorrect(String errorCode) {
AwsServiceException exception = createTestException(errorCode);

for (int i = 0; i < 128; ++i) {
StandardRetryStrategy strategy = AwsRetryStrategy.standardRetryStrategy(true);

RetryToken token = strategy.acquireInitialToken(AcquireInitialTokenRequest.create("test")).token();
RefreshRetryTokenRequest refresh = RefreshRetryTokenRequest.builder()
.token(token)
.failure(exception)
.build();
Duration delay = strategy.refreshRetryToken(refresh).delay();

assertThat(delay).isBetween(Duration.ZERO, Duration.ofMillis(1000));
}
}

private static AwsServiceException createTestException(String errorCode) {
AwsErrorDetails details = AwsErrorDetails.builder()
.errorCode(errorCode)
.build();
return AwsServiceException.builder().awsErrorDetails(details).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public final class ProfileProperty {
*/
public static final String RETRY_MODE = "retry_mode";

/**
* How many HTTP requests an SDK should make for a single SDK operation invocation before giving up. See the JavaDocs for
* {@code SdkSystemSetting.AWS_MAX_ATTEMPTS} and {@code RetryStrategy.maxAttempts()} for more information.
*/
public static final String MAX_ATTEMPTS = "max_attempts";

/**
* The "defaults mode" to be used for clients created using the currently-configured profile. Defaults mode determins how SDK
* default configuration should be resolved. See the {@code DefaultsMode} class JavaDoc for more
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.core.internal.retry;

import java.util.Optional;
import java.util.function.Supplier;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.SdkSystemSetting;
import software.amazon.awssdk.profiles.ProfileFile;
import software.amazon.awssdk.profiles.ProfileFileSystemSetting;
import software.amazon.awssdk.profiles.ProfileProperty;
import software.amazon.awssdk.utils.OptionalUtils;

/**
* Resolves the retry max attempts from {@link SdkSystemSetting#AWS_MAX_ATTEMPTS} and {@link ProfileProperty#MAX_ATTEMPTS}.
*/
@SdkInternalApi
public class MaxAttemptsResolver {
private Supplier<ProfileFile> profileFile;
private String profileName;

/**
* Configure the profile file that should be used when determining the max attempts. The supplier is only consulted
* if a higher-priority determinant (e.g. environment variables) does not find the setting.
*/
public MaxAttemptsResolver profileFile(Supplier<ProfileFile> profileFile) {
this.profileFile = profileFile;
return this;
}

/**
* Configure the profile file name should be used when determining the max attempts.
*/
public MaxAttemptsResolver profileName(String profileName) {
this.profileName = profileName;
return this;
}

/**
* Resolve the max attempts based on the configured values. If not configured, returns {@code null}.
*/
public Integer resolve() {
return OptionalUtils.firstPresent(fromSystemSettings(), () -> fromProfileFile(profileFile, profileName))
.orElse(null);
}


private static Optional<Integer> fromSystemSettings() {
return SdkSystemSetting.AWS_MAX_ATTEMPTS.getIntegerValue();
}

private static Optional<Integer> fromProfileFile(Supplier<ProfileFile> profileFile, String profileName) {
profileFile = profileFile != null ? profileFile : ProfileFile::defaultProfileFile;
profileName = profileName != null ? profileName : ProfileFileSystemSetting.AWS_PROFILE.getStringValueOrThrow();
return profileFile.get()
.profile(profileName)
.flatMap(p -> p.property(ProfileProperty.MAX_ATTEMPTS))
.map(Integer::parseInt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,19 @@ public static RetryStrategy defaultRetryStrategy() {
* @return the appropriate retry strategy for the retry mode with AWS-specific conditions added.
*/
public static RetryStrategy forRetryMode(RetryMode mode) {
return forRetryMode(mode, false);
}

/**
* Retrieve the appropriate retry strategy for the retry mode with AWS-specific conditions added.
*
* @param mode The retry mode for which we want the retry strategy
* @return the appropriate retry strategy for the retry mode with AWS-specific conditions added.
*/
public static RetryStrategy forRetryMode(RetryMode mode, boolean newRetries2026Enabled) {
switch (mode) {
case STANDARD:
return standardRetryStrategy();
return standardRetryStrategy(newRetries2026Enabled);
case ADAPTIVE:
return legacyAdaptiveRetryStrategy();
case ADAPTIVE_V2:
Expand Down Expand Up @@ -115,6 +125,10 @@ public static StandardRetryStrategy standardRetryStrategy() {
return standardRetryStrategyBuilder().build();
}

public static StandardRetryStrategy standardRetryStrategy(boolean newRetries2026Enabled) {
return standardRetryStrategyBuilder(newRetries2026Enabled).build();
}

/**
* Returns a {@link LegacyRetryStrategy} with generic SDK retry conditions.
*
Expand Down Expand Up @@ -149,7 +163,7 @@ public static StandardRetryStrategy.Builder standardRetryStrategyBuilder() {
*/
public static StandardRetryStrategy.Builder standardRetryStrategyBuilder(boolean newRetries2026Enabled) {
StandardRetryStrategy.Builder builder = DefaultRetryStrategy.standardStrategyBuilder(newRetries2026Enabled);
return configure(builder);
return configure(builder, newRetries2026Enabled);
}


Expand Down Expand Up @@ -179,7 +193,7 @@ public static AdaptiveRetryStrategy.Builder adaptiveRetryStrategyBuilder() {
*/
public static AdaptiveRetryStrategy.Builder adaptiveRetryStrategyBuilder(boolean newRetries2026Enabled) {
AdaptiveRetryStrategy.Builder builder = DefaultRetryStrategy.adaptiveStrategyBuilder(newRetries2026Enabled);
return configure(builder);
return configure(builder, newRetries2026Enabled);
}

/**
Expand All @@ -190,13 +204,17 @@ public static AdaptiveRetryStrategy.Builder adaptiveRetryStrategyBuilder(boolean
* @return The given builder
*/
public static <T extends RetryStrategy.Builder<T, ?>> T configure(T builder) {
return configure(builder, false);
}

private static <T extends RetryStrategy.Builder<T, ?>> T configure(T builder, boolean newRetries2026Enabled) {
builder.retryOnException(SdkDefaultRetryStrategy::retryOnRetryableException)
.retryOnException(SdkDefaultRetryStrategy::retryOnStatusCodes)
.retryOnException(SdkDefaultRetryStrategy::retryOnClockSkewException)
.retryOnException(SdkDefaultRetryStrategy::retryOnThrottlingCondition);
SdkDefaultRetrySetting.RETRYABLE_EXCEPTIONS.forEach(builder::retryOnExceptionOrCauseInstanceOf);
builder.treatAsThrottling(SdkDefaultRetryStrategy::treatAsThrottling);
Integer maxAttempts = SdkSystemSetting.AWS_MAX_ATTEMPTS.getIntegerValue().orElse(null);
Integer maxAttempts = resolveMaxAttempts(newRetries2026Enabled);
if (maxAttempts != null) {
builder.maxAttempts(maxAttempts);
}
Expand Down Expand Up @@ -281,5 +299,13 @@ private static void markDefaultsAdded(RetryStrategy.Builder<?, ?> builder) {
}
}

static Integer resolveMaxAttempts(boolean newRetries2026Enabled) {
if (newRetries2026Enabled) {
return new MaxAttemptsResolver().resolve();
}

// pre 2.1 changes, we never looked at the profile file
return SdkSystemSetting.AWS_MAX_ATTEMPTS.getIntegerValue().orElse(null);
Comment thread
alextwoods marked this conversation as resolved.
}
}

Loading
Loading