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 @@ -225,6 +225,11 @@ public class CustomizationConfig {

private RetryMode defaultRetryMode;

/**
* Whether the client will use retry 2.1 behavior by default.
*/
private Boolean defaultNewRetries2026;

/**
* Whether to generate an abstract decorator class that delegates to the async service client
*/
Expand Down Expand Up @@ -714,6 +719,14 @@ public void setDefaultRetryMode(RetryMode defaultRetryMode) {
this.defaultRetryMode = defaultRetryMode;
}

public Boolean getDefaultNewRetries2026() {
return defaultNewRetries2026;
}

public void setDefaultNewRetries2026(Boolean defaultNewRetries2026) {
this.defaultNewRetries2026 = defaultNewRetries2026;
}

public ServiceConfig getServiceConfig() {
return serviceConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,10 @@ private void configureEnvironmentBearerToken(MethodSpec.Builder builder) {
private Optional<MethodSpec> mergeInternalDefaultsMethod() {
String userAgent = model.getCustomizationConfig().getUserAgent();
RetryMode defaultRetryMode = model.getCustomizationConfig().getDefaultRetryMode();
Boolean defaultNewRetries2026 = model.getCustomizationConfig().getDefaultNewRetries2026();

// If none of the options are customized, then we do not need to bother overriding the method
if (userAgent == null && defaultRetryMode == null) {
if (userAgent == null && defaultRetryMode == null && defaultNewRetries2026 == null) {
return Optional.empty();
}

Expand All @@ -348,6 +349,10 @@ private Optional<MethodSpec> mergeInternalDefaultsMethod() {
builder.addCode("c.option($T.DEFAULT_RETRY_MODE, $T.$L);\n",
SdkClientOption.class, RetryMode.class, defaultRetryMode.name());
}
if (defaultNewRetries2026 != null) {
builder.addCode("c.option($T.DEFAULT_NEW_RETRIES_2026, $L);\n",
SdkClientOption.class, defaultNewRetries2026);
}
builder.addCode("});\n");
return Optional.of(builder.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void baseClientBuilderClassWithNoAuthService_sra() {
}

@Test
void baseClientBuilderClassWithInternalUserAgent_sra() {
void baseClientBuilderClassWithInternalDefaults_sra() {
validateBaseClientBuilderClassGeneration(internalConfigModels(), "test-client-builder-internal-defaults-class.java");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ protected final SdkClientConfiguration mergeInternalDefaults(SdkClientConfigurat
return config.merge(c -> {
c.option(SdkClientOption.INTERNAL_USER_AGENT, "md/foobar");
c.option(SdkClientOption.DEFAULT_RETRY_MODE, RetryMode.STANDARD);
c.option(SdkClientOption.DEFAULT_NEW_RETRIES_2026, true);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"skip" : true
},
"userAgent": "md/foobar",
"defaultRetryMode": "STANDARD"
"defaultRetryMode": "STANDARD",
"defaultNewRetries2026": "true"
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
import software.amazon.awssdk.core.internal.SdkInternalTestAdvancedClientOption;
import software.amazon.awssdk.core.internal.retry.SdkDefaultRetryStrategy;
import software.amazon.awssdk.core.retry.NewRetries2026Resolver;
import software.amazon.awssdk.core.retry.RetryMode;
import software.amazon.awssdk.core.retry.RetryPolicy;
import software.amazon.awssdk.http.SdkHttpClient;
Expand Down Expand Up @@ -457,12 +458,18 @@ private void configureRetryStrategy(SdkClientConfiguration.Builder config) {
}

private RetryStrategy resolveAwsRetryStrategy(LazyValueSource config) {
Boolean defaultNewRetries2026 = config.get(SdkClientOption.DEFAULT_NEW_RETRIES_2026);

RetryMode retryMode = RetryMode.resolver()
.profileFile(config.get(SdkClientOption.PROFILE_FILE_SUPPLIER))
.profileName(config.get(SdkClientOption.PROFILE_NAME))
.defaultRetryMode(config.get(SdkClientOption.DEFAULT_RETRY_MODE))
.defaultNewRetries2026(defaultNewRetries2026)
.resolve();
return AwsRetryStrategy.forRetryMode(retryMode);

NewRetries2026Resolver newRetries2026Resolver = new NewRetries2026Resolver().defaultNewRetries2026(defaultNewRetries2026);

return AwsRetryStrategy.forRetryMode(retryMode, newRetries2026Resolver.resolve());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,29 @@ public static RetryStrategy defaultRetryStrategy() {
}

/**
* Retrieve the appropriate retry strategy for the retry mode with AWS-specific conditions added.
* Retrieve the appropriate retry strategy for the retry mode with AWS-specific conditions added. This is equivalent to
* {@code forRetryMode(mode, false)}.
*
* @param mode The retry mode for which we want to create a retry strategy.
* @return A retry strategy for the given retry mode.
*/
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 to create a retry strategy.
* @param newRetries2026Enabled Whether retries 2.1 behavior is enabled.
* @return A retry strategy for the given retry mode.
*/
public static RetryStrategy forRetryMode(RetryMode mode, boolean newRetries2026Enabled) {
Comment thread
dagnir marked this conversation as resolved.
switch (mode) {
case STANDARD:
return standardRetryStrategy();
return standardRetryStrategy(newRetries2026Enabled);
case ADAPTIVE_V2:
return adaptiveRetryStrategy();
return adaptiveRetryStrategy(newRetries2026Enabled);
case LEGACY:
return legacyRetryStrategy();
case ADAPTIVE:
Expand All @@ -83,6 +95,7 @@ public static RetryStrategy forRetryMode(RetryMode mode) {
}
}


/**
* Update the provided {@link RetryStrategy} to add AWS-specific conditions.
*
Expand All @@ -105,12 +118,23 @@ public static RetryStrategy doNotRetry() {
}

/**
* Returns a {@link StandardRetryStrategy} with AWS-specific conditions added.
* Returns a {@link StandardRetryStrategy} with AWS-specific conditions added. This is equivalent to {@code
* standardRetryStrategy(false)}.
*
* @return A {@link StandardRetryStrategy} with AWS-specific conditions added.
*/
public static StandardRetryStrategy standardRetryStrategy() {
StandardRetryStrategy.Builder builder = SdkDefaultRetryStrategy.standardRetryStrategyBuilder();
return standardRetryStrategy(false);
}

/**
* Returns a {@link StandardRetryStrategy} with AWS-specific conditions added.
*
* @param newRetries2026Enabled Whether retries 2.1 behavior is enabled.
* @return A {@link StandardRetryStrategy} with AWS-specific conditions added.
*/
public static StandardRetryStrategy standardRetryStrategy(boolean newRetries2026Enabled) {
StandardRetryStrategy.Builder builder = SdkDefaultRetryStrategy.standardRetryStrategyBuilder(newRetries2026Enabled);
return configure(builder).build();
}

Expand All @@ -126,12 +150,23 @@ public static LegacyRetryStrategy legacyRetryStrategy() {
}

/**
* Returns an {@link AdaptiveRetryStrategy} with AWS-specific conditions added.
* Returns an {@link AdaptiveRetryStrategy} with AWS-specific conditions added. This is equivalent to {@code
* adaptiveRetryStrategy(false)}.
*
* @return An {@link AdaptiveRetryStrategy} with AWS-specific conditions added.
*/
public static AdaptiveRetryStrategy adaptiveRetryStrategy() {
AdaptiveRetryStrategy.Builder builder = SdkDefaultRetryStrategy.adaptiveRetryStrategyBuilder();
return adaptiveRetryStrategy(false);
}

/**
* Returns an {@link AdaptiveRetryStrategy} with AWS-specific conditions added.
*
* @param newRetries2026Enabled Whether retries 2.1 behavior is enabled.
* @return An {@link AdaptiveRetryStrategy} with AWS-specific conditions added.
*/
public static AdaptiveRetryStrategy adaptiveRetryStrategy(boolean newRetries2026Enabled) {
AdaptiveRetryStrategy.Builder builder = SdkDefaultRetryStrategy.adaptiveRetryStrategyBuilder(newRetries2026Enabled);
return configure(builder)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* 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.client.builder;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static software.amazon.awssdk.core.client.config.SdkClientOption.RETRY_STRATEGY;

import java.util.stream.Stream;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import software.amazon.awssdk.core.SdkSystemSetting;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.retries.LegacyRetryStrategy;
import software.amazon.awssdk.retries.StandardRetryStrategy;
import software.amazon.awssdk.testutils.EnvironmentVariableHelper;

public class InternalDefaultsTest {

Check warning on line 38 in core/aws-core/src/test/java/software/amazon/awssdk/awscore/client/builder/InternalDefaultsTest.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=AZ22n5KBczfuCFFt3uJf&open=AZ22n5KBczfuCFFt3uJf&pullRequest=6887
private static String newRetries2026Save;

@BeforeAll
static void setup() {
newRetries2026Save = System.getProperty(SdkSystemSetting.AWS_NEW_RETRIES_2026.property());
}

@BeforeEach
void methodSetup() {
System.clearProperty(SdkSystemSetting.AWS_NEW_RETRIES_2026.property());
}

@AfterAll
static void teardown() {
if (newRetries2026Save != null) {
System.setProperty(SdkSystemSetting.AWS_NEW_RETRIES_2026.property(), newRetries2026Save);
} else {
System.clearProperty(SdkSystemSetting.AWS_NEW_RETRIES_2026.property());
}
}

@ParameterizedTest(name = "system prop = {0}, env var = {1}, default cfg = {2}, expected = {3}")
@MethodSource("newRetries2026Settings")
void buildClient_precedenceIsCorrect(String systemProperty, String environmentVariable, Boolean defaultConfig,
Class<?> retryStrategyClass) {
EnvironmentVariableHelper.run((env) -> {

Check warning on line 64 in core/aws-core/src/test/java/software/amazon/awssdk/awscore/client/builder/InternalDefaultsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the parentheses around the "env" parameter

See more on https://sonarcloud.io/project/issues?id=aws_aws-sdk-java-v2&issues=AZ22n5KBczfuCFFt3uJg&open=AZ22n5KBczfuCFFt3uJg&pullRequest=6887
if (environmentVariable != null) {
env.set(SdkSystemSetting.AWS_NEW_RETRIES_2026.environmentVariable(), environmentVariable);
}

if (systemProperty != null) {
System.setProperty(SdkSystemSetting.AWS_NEW_RETRIES_2026.property(), systemProperty);
}

TestClient sync = new TestClientBuilder(true)
.newRetries2026Default(defaultConfig)
.buildClient();

TestClient async = new TestClientBuilder(false)
.newRetries2026Default(defaultConfig)
.buildClient();

assertThat(sync.clientConfiguration.option(RETRY_STRATEGY)).isInstanceOf(retryStrategyClass);
assertThat(async.clientConfiguration.option(RETRY_STRATEGY)).isInstanceOf(retryStrategyClass);
});
}

// system property, environment variable, default config, expected retry strategy
static Stream<Arguments> newRetries2026Settings() {
return Stream.of(
Arguments.of(null, null, null, LegacyRetryStrategy.class),

Arguments.of("true", null, null, StandardRetryStrategy.class),
Arguments.of("false", null, null, LegacyRetryStrategy.class),
Arguments.of(null, "true", null, StandardRetryStrategy.class),
Arguments.of(null, "false", null, LegacyRetryStrategy.class),
Arguments.of(null, null, true, StandardRetryStrategy.class),
Arguments.of(null, null, false, LegacyRetryStrategy.class),

Arguments.of("true", null, false, StandardRetryStrategy.class),
Arguments.of(null, "true", false, StandardRetryStrategy.class)
);
}

private static class TestClient {
private final SdkClientConfiguration clientConfiguration;

public TestClient(SdkClientConfiguration clientConfiguration) {
this.clientConfiguration = clientConfiguration;
}
}

private static class TestClientBuilder extends AwsDefaultClientBuilder<TestClientBuilder, TestClient> {
private final boolean sync;
private Boolean newRetries2026Default;

protected TestClientBuilder(boolean sync) {
super(mock(SdkHttpClient.Builder.class), mock(SdkAsyncHttpClient.Builder.class), null);
this.sync = sync;
}

public TestClientBuilder newRetries2026Default(Boolean newRetries2026Default) {
this.newRetries2026Default = newRetries2026Default;
return this;
}

@Override
protected String serviceEndpointPrefix() {
return "test-client";
}

@Override
protected String signingName() {
return "test-client";
}

@Override
protected String serviceName() {
return "test-client";
}

@Override
protected final SdkClientConfiguration mergeInternalDefaults(SdkClientConfiguration config) {
return config.merge(c -> {
c.option(SdkClientOption.DEFAULT_NEW_RETRIES_2026, newRetries2026Default);
});
}

@Override
protected TestClient buildClient() {
SdkClientConfiguration config;
if (sync) {
config = syncClientConfiguration();
} else {
config = asyncClientConfiguration();
}

return new TestClient(config);
}
}
}
Loading
Loading