Skip to content

Update retry policy used in CodeGuruProfilerClient #7

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 4 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -2,9 +2,13 @@

import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.internal.retry.SdkDefaultRetrySetting;
import software.amazon.awssdk.core.exception.AbortedException;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.retry.RetryPolicy;
import software.amazon.awssdk.core.retry.backoff.BackoffStrategy;
import software.amazon.awssdk.core.retry.backoff.EqualJitterBackoffStrategy;
import software.amazon.awssdk.core.retry.conditions.OrRetryCondition;
import software.amazon.awssdk.core.retry.conditions.RetryCondition;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.services.codeguruprofiler.CodeGuruProfilerClient;
Expand Down Expand Up @@ -70,23 +74,41 @@ private static RetryPolicy getRetryPolicy() {
.backoffStrategy(failureBackoffStrategy)
.throttlingBackoffStrategy(throttlingBackoffStrategy)
.numRetries(MAX_ERROR_RETRY) // We can be a bit slower in CloudFormation for the sake of not failing the deployment!
.retryCondition(getRetryCondition())
.build();
}

private static RetryCondition getRetryCondition() {
return OrRetryCondition.create(
RetryCondition.defaultRetryCondition(), // Pull in SDK defaults
retryAbortedExceptionCondition() // https://github.com/aws/aws-sdk-java-v2/issues/1684
);
}

private static RetryCondition retryAbortedExceptionCondition() {
return c -> c.exception().getClass().equals(SdkClientException.class) &&
c.exception().getCause() != null &&
c.exception().getCause().getClass().equals(AbortedException.class);
}

private static SdkHttpClient getHttpClient() {
return ApacheHttpClient.builder()
.connectionTimeout(CONNECTION_TIMEOUT)
.socketTimeout(SOCKET_TIMEOUT)
.build();
}

public static ClientOverrideConfiguration getClientConfiguration() {
return ClientOverrideConfiguration.builder()
.retryPolicy(getRetryPolicy())
.apiCallTimeout(OVERALL_TIMEOUT)
.apiCallAttemptTimeout(ATTEMPT_TIMEOUT)
.build();
}

public static CodeGuruProfilerClient create() {
return CodeGuruProfilerClient.builder()
.overrideConfiguration(ClientOverrideConfiguration.builder()
.retryPolicy(getRetryPolicy())
.apiCallTimeout(OVERALL_TIMEOUT)
.apiCallAttemptTimeout(ATTEMPT_TIMEOUT)
.build())
.overrideConfiguration(getClientConfiguration())
.httpClient(getHttpClient())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package software.amazon.codeguruprofiler.profilinggroup;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.exception.AbortedException;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.core.retry.RetryPolicyContext;
import software.amazon.awssdk.core.retry.conditions.RetryCondition;

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

@ExtendWith(MockitoExtension.class)
class CodeGuruProfilerClientConfigurationTest {

@Nested
class DescribeRetryCondition {
@Test
public void itShouldNotRetryClientErrorsByDefault() {
assertThat(getRetryCondition().shouldRetry(createRetryContext(SdkClientException.create("test")))).isFalse();
}

@Test
public void itShouldRetryClientErrorsCausedByAbortedException() {
// Until https://github.com/aws/aws-sdk-java-v2/issues/1684 is fixed
SdkClientException clientException =
SdkClientException.builder()
.message("test")
.cause(AbortedException.create("test aborted exception"))
.build();

assertThat(getRetryCondition().shouldRetry(createRetryContext(clientException))).isTrue();
}
}

private RetryCondition getRetryCondition() {
return getClientConfiguration()
.retryPolicy().get()
.retryCondition();
}

private ClientOverrideConfiguration getClientConfiguration() {
return CodeGuruProfilerClientBuilder.getClientConfiguration();
}

private RetryPolicyContext createRetryContext(SdkException exception) {
return RetryPolicyContext.builder()
.exception(exception)
.build();
}
}