Skip to content

Append user agent for retry mode #2322

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

Merged
merged 2 commits into from
Mar 10, 2021
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
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-5b5a872.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "AWS SDK for Java v2",
"contributor": "",
"type": "feature",
"description": "Include the retry mode STANDARD or LEGACY in the user agents list."
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class ApplyUserAgentStage implements MutableRequestToRequestPipeline {

private static final String IO = "io";
private static final String HTTP = "http";
private static final String CONFIG = "cfg";
private static final String RETRY_MODE = "retry-mode";

private static final String AWS_EXECUTION_ENV_PREFIX = "exec-env/";

Expand Down Expand Up @@ -94,6 +96,15 @@ private StringBuilder getUserAgent(SdkClientConfiguration config, List<ApiName>
.append("/")
.append(SdkHttpUtils.urlEncode(clientName));

String retryMode = config.option(SdkClientOption.RETRY_POLICY).retryMode().toString();

userAgent.append(SPACE)
.append(CONFIG)
.append("/")
.append(RETRY_MODE)
.append("/")
.append(StringUtils.lowerCase(retryMode));

if (!requestApiNames.isEmpty()) {
String requestUserAgent = requestApiNames.stream()
.map(n -> n.name() + "/" + n.version())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import software.amazon.awssdk.core.retry.conditions.RetryCondition;
import software.amazon.awssdk.core.retry.conditions.TokenBucketRetryCondition;
import software.amazon.awssdk.utils.ToString;
import software.amazon.awssdk.utils.Validate;
import software.amazon.awssdk.utils.builder.CopyableBuilder;
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;

Expand Down Expand Up @@ -103,6 +104,9 @@ public static Builder builder() {
* Create a {@link RetryPolicy.Builder} populated with the defaults from the provided {@link RetryMode}.
*/
public static Builder builder(RetryMode retryMode) {
Validate.paramNotNull(retryMode, "The retry mode cannot be set as null. If you don't want to set the retry mode,"
+ " please use the other builder method without setting retry mode, and the default retry"
+ " mode will be used.");
return new BuilderImpl(retryMode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.internal.http.AmazonSyncHttpClient;
import software.amazon.awssdk.core.internal.http.timers.ClientExecutionAndRequestTimerTestUtils;
import software.amazon.awssdk.core.retry.RetryMode;
import software.amazon.awssdk.core.retry.RetryPolicy;
import software.amazon.awssdk.http.ExecutableHttpRequest;
import software.amazon.awssdk.http.HttpExecuteRequest;
import software.amazon.awssdk.http.HttpExecuteResponse;
Expand Down Expand Up @@ -183,6 +185,33 @@ public void testUserAgentContainsHttpClientInfo() {
Assert.assertTrue(userAgent.contains("http/UNKNOWN"));
}

@Test
public void testUserAgentContainsRetryModeInfo() {
HttpResponseHandler<?> handler = mock(HttpResponseHandler.class);

SdkClientConfiguration config = HttpTestUtils.testClientConfiguration().toBuilder()
.option(SdkClientOption.SYNC_HTTP_CLIENT, sdkHttpClient)
.option(SdkClientOption.CLIENT_TYPE, ClientType.SYNC)
.option(SdkClientOption.ENDPOINT, URI.create("http://example.com"))
.option(SdkClientOption.RETRY_POLICY, RetryPolicy.forRetryMode(RetryMode.STANDARD))
.build();
AmazonSyncHttpClient client = new AmazonSyncHttpClient(config);

client.requestExecutionBuilder()
.request(ValidSdkObjects.sdkHttpFullRequest().build())
.originalRequest(NoopTestRequest.builder().build())
.executionContext(ClientExecutionAndRequestTimerTestUtils.executionContext(null))
.execute(combinedSyncResponseHandler(handler, null));

ArgumentCaptor<HttpExecuteRequest> httpRequestCaptor = ArgumentCaptor.forClass(HttpExecuteRequest.class);
verify(sdkHttpClient).prepareRequest(httpRequestCaptor.capture());

final String userAgent = httpRequestCaptor.getValue().httpRequest().firstMatchingHeader("User-Agent")
.orElseThrow(() -> new AssertionError("User-Agent header was not found"));

Assert.assertTrue(userAgent.contains("cfg/retry-mode/standard"));
}

@Test
public void closeClient_shouldCloseDependencies() {
SdkClientConfiguration config = HttpTestUtils.testClientConfiguration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.awssdk.core.retry;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.verify;

import org.junit.Assert;
Expand Down Expand Up @@ -82,6 +83,19 @@ public void nonRetryPolicy_shouldUseNullCondition() {
assertThat(noneRetry.throttlingBackoffStrategy()).isEqualTo(BackoffStrategy.none());
}

@Test
public void nonRetryMode_shouldUseDefaultRetryMode() {
RetryPolicy policy = RetryPolicy.builder().build();
assertThat(policy.retryMode().toString()).isEqualTo("LEGACY");
}

@Test
public void nullRetryMode_shouldThrowNullPointerException() {
assertThatThrownBy(() -> RetryPolicy.builder(null).build())
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("retry mode cannot be set as null");
}

@Test
public void maxRetriesFromRetryModeIsCorrect() {
assertThat(RetryPolicy.forRetryMode(RetryMode.LEGACY).numRetries()).isEqualTo(3);
Expand Down