Skip to content

Commit d40174d

Browse files
author
Colman Yau
committed
Update retry policy used in CodeGuruProfilerClient
Issue has been raised on aws/aws-sdk-java-v2#1684 that AWS Sdk client returns a non-retryable AbortedExceptions when an API call attempt timeout. This change added a new retry condition where the client will now retry when AbortedException is returned.
1 parent de03a04 commit d40174d

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

aws-codeguruprofiler-profilinggroup/src/main/java/software/amazon/codeguruprofiler/profilinggroup/CodeGuruProfilerClientBuilder.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
44
import software.amazon.awssdk.core.internal.retry.SdkDefaultRetrySetting;
5+
import software.amazon.awssdk.core.exception.AbortedException;
6+
import software.amazon.awssdk.core.exception.SdkClientException;
57
import software.amazon.awssdk.core.retry.RetryPolicy;
68
import software.amazon.awssdk.core.retry.backoff.BackoffStrategy;
79
import software.amazon.awssdk.core.retry.backoff.EqualJitterBackoffStrategy;
10+
import software.amazon.awssdk.core.retry.conditions.OrRetryCondition;
11+
import software.amazon.awssdk.core.retry.conditions.RetryCondition;
812
import software.amazon.awssdk.http.SdkHttpClient;
913
import software.amazon.awssdk.http.apache.ApacheHttpClient;
1014
import software.amazon.awssdk.services.codeguruprofiler.CodeGuruProfilerClient;
@@ -70,23 +74,41 @@ private static RetryPolicy getRetryPolicy() {
7074
.backoffStrategy(failureBackoffStrategy)
7175
.throttlingBackoffStrategy(throttlingBackoffStrategy)
7276
.numRetries(MAX_ERROR_RETRY) // We can be a bit slower in CloudFormation for the sake of not failing the deployment!
77+
.retryCondition(getRetryCondition())
7378
.build();
7479
}
7580

81+
private static RetryCondition getRetryCondition() {
82+
return OrRetryCondition.create(
83+
RetryCondition.defaultRetryCondition(), // Pull in SDK defaults
84+
retryAbortedExceptionCondition() // https://github.com/aws/aws-sdk-java-v2/issues/1684
85+
);
86+
}
87+
88+
private static RetryCondition retryAbortedExceptionCondition() {
89+
return c -> c.exception().getClass().equals(SdkClientException.class) &&
90+
c.exception().getCause() != null &&
91+
c.exception().getCause().getClass().equals(AbortedException.class);
92+
}
93+
7694
private static SdkHttpClient getHttpClient() {
7795
return ApacheHttpClient.builder()
7896
.connectionTimeout(CONNECTION_TIMEOUT)
7997
.socketTimeout(SOCKET_TIMEOUT)
8098
.build();
8199
}
82100

101+
public static ClientOverrideConfiguration getClientConfiguration() {
102+
return ClientOverrideConfiguration.builder()
103+
.retryPolicy(getRetryPolicy())
104+
.apiCallTimeout(OVERALL_TIMEOUT)
105+
.apiCallAttemptTimeout(ATTEMPT_TIMEOUT)
106+
.build();
107+
}
108+
83109
public static CodeGuruProfilerClient create() {
84110
return CodeGuruProfilerClient.builder()
85-
.overrideConfiguration(ClientOverrideConfiguration.builder()
86-
.retryPolicy(getRetryPolicy())
87-
.apiCallTimeout(OVERALL_TIMEOUT)
88-
.apiCallAttemptTimeout(ATTEMPT_TIMEOUT)
89-
.build())
111+
.overrideConfiguration(getClientConfiguration())
90112
.httpClient(getHttpClient())
91113
.build();
92114
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package software.amazon.codeguruprofiler.profilinggroup;
2+
3+
import org.junit.jupiter.api.Nested;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.ExtendWith;
6+
import org.mockito.junit.jupiter.MockitoExtension;
7+
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
8+
import software.amazon.awssdk.core.exception.AbortedException;
9+
import software.amazon.awssdk.core.exception.SdkClientException;
10+
import software.amazon.awssdk.core.exception.SdkException;
11+
import software.amazon.awssdk.core.retry.RetryPolicyContext;
12+
import software.amazon.awssdk.core.retry.conditions.RetryCondition;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
16+
@ExtendWith(MockitoExtension.class)
17+
class CodeGuruProfilerClientConfigurationTest {
18+
19+
@Nested
20+
class DescribeRetryCondition {
21+
@Test
22+
public void itShouldNotRetryClientErrorsByDefault() {
23+
assertThat(getRetryCondition().shouldRetry(createRetryContext(SdkClientException.create("test")))).isFalse();
24+
}
25+
26+
@Test
27+
public void itShouldRetryClientErrorsCausedByAbortedException() {
28+
// Until https://github.com/aws/aws-sdk-java-v2/issues/1684 is fixed
29+
SdkClientException clientException =
30+
SdkClientException.builder()
31+
.message("test")
32+
.cause(AbortedException.create("test aborted exception"))
33+
.build();
34+
35+
assertThat(getRetryCondition().shouldRetry(createRetryContext(clientException))).isTrue();
36+
}
37+
}
38+
39+
private RetryCondition getRetryCondition() {
40+
return getClientConfiguration()
41+
.retryPolicy().get()
42+
.retryCondition();
43+
}
44+
45+
private ClientOverrideConfiguration getClientConfiguration() {
46+
return CodeGuruProfilerClientBuilder.getClientConfiguration();
47+
}
48+
49+
private RetryPolicyContext createRetryContext(SdkException exception) {
50+
return RetryPolicyContext.builder()
51+
.exception(exception)
52+
.build();
53+
}
54+
}

0 commit comments

Comments
 (0)