Skip to content

Commit 27f8e6e

Browse files
authored
Merge pull request #7 from aws-cloudformation/update-retry-policy
Update retry policy used in CodeGuruProfilerClient
2 parents de03a04 + 537677b commit 27f8e6e

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed

aws-codeguruprofiler-profilinggroup/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ automatically overwritten.
1414
After modifying the JSON schema or changing the handlers implementation make sure you do the following before sending a pull-request:
1515

1616
```
17-
pre-commit run --all-files && AWS_REGION=us-east-1 mvn clean package
17+
pre-commit run --all-files && AWS_REGION=us-east-1 mvn clean verify package
1818
```
1919

2020
**References**

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;
56
import software.amazon.awssdk.core.retry.RetryPolicy;
7+
import software.amazon.awssdk.core.retry.RetryPolicyContext;
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+
shouldRetryAbortedException() // https://github.com/aws/aws-sdk-java-v2/issues/1684
85+
);
86+
}
87+
88+
private static RetryCondition shouldRetryAbortedException() {
89+
return (RetryPolicyContext c) ->
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+
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,58 @@
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+
import software.amazon.awssdk.services.codeguruprofiler.model.ConflictException;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
17+
@ExtendWith(MockitoExtension.class)
18+
public class CodeGuruProfilerClientConfigurationTest {
19+
20+
@Nested
21+
class DescribeRetryCondition {
22+
@Test
23+
public void itShouldNotRetryClientErrorsByDefault() {
24+
assertThat(getRetryCondition().shouldRetry(createRetryContext(SdkClientException.create("test")))).isFalse();
25+
}
26+
27+
@Test
28+
public void itShouldNotRetryErrorsWhenClientErrorsNotCausedByAbortedException() {
29+
SdkClientException clientException =
30+
SdkClientException.create("test", ConflictException.builder().build());
31+
assertThat(getRetryCondition().shouldRetry(createRetryContext(clientException))).isFalse();
32+
}
33+
34+
@Test
35+
public void itShouldRetryClientErrorsCausedByAbortedException() {
36+
// Until https://github.com/aws/aws-sdk-java-v2/issues/1684 is fixed
37+
SdkClientException clientException =
38+
SdkClientException.create("test", AbortedException.create("test aborted exception"));
39+
assertThat(getRetryCondition().shouldRetry(createRetryContext(clientException))).isTrue();
40+
}
41+
}
42+
43+
private RetryCondition getRetryCondition() {
44+
return getClientConfiguration()
45+
.retryPolicy().get()
46+
.retryCondition();
47+
}
48+
49+
private ClientOverrideConfiguration getClientConfiguration() {
50+
return CodeGuruProfilerClientBuilder.getClientConfiguration();
51+
}
52+
53+
private RetryPolicyContext createRetryContext(SdkException exception) {
54+
return RetryPolicyContext.builder()
55+
.exception(exception)
56+
.build();
57+
}
58+
}

0 commit comments

Comments
 (0)