software.amazon.awssdk.core.retry.conditions.RetryCondition Java Examples

The following examples show how to use software.amazon.awssdk.core.retry.conditions.RetryCondition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OrRetryConditionTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void conditionsAreEvaluatedInOrder() {
    int numConditions = 1000;
    int firstTrueCondition = 500;

    RetryCondition[] conditions = new RetryCondition[numConditions];
    for (int i = 0; i < numConditions; ++i) {
        RetryCondition mock = Mockito.mock(RetryCondition.class);
        when(mock.shouldRetry(RetryPolicyContexts.EMPTY)).thenReturn(i == firstTrueCondition);
        conditions[i] = mock;
    }

    assertTrue(OrRetryCondition.create(conditions).shouldRetry(RetryPolicyContexts.EMPTY));

    for (int i = 0; i < numConditions; ++i) {
        int timesExpected = i <= firstTrueCondition ? 1 : 0;
        Mockito.verify(conditions[i], times(timesExpected)).shouldRetry(RetryPolicyContexts.EMPTY);
    }
}
 
Example #2
Source File: AndRetryConditionTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void conditionsAreEvaluatedInOrder() {
    int numConditions = 1000;
    int firstFalseCondition = 500;

    RetryCondition[] conditions = new RetryCondition[numConditions];
    for (int i = 0; i < numConditions; ++i) {
        RetryCondition mock = Mockito.mock(RetryCondition.class);
        when(mock.shouldRetry(RetryPolicyContexts.EMPTY)).thenReturn(i != firstFalseCondition);
        conditions[i] = mock;
    }

    assertFalse(AndRetryCondition.create(conditions).shouldRetry(RetryPolicyContexts.EMPTY));

    for (int i = 0; i < numConditions; ++i) {
        int timesExpected = i <= firstFalseCondition ? 1 : 0;
        Mockito.verify(conditions[i], times(timesExpected)).shouldRetry(RetryPolicyContexts.EMPTY);
    }
}
 
Example #3
Source File: StsWebIdentityCredentialsProviderFactory.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private StsWebIdentityCredentialsProvider(WebIdentityTokenCredentialProperties credentialProperties) {
    String roleSessionName = credentialProperties.roleSessionName();
    String sessionName = roleSessionName != null ? roleSessionName : "aws-sdk-java-" + System.currentTimeMillis();

    OrRetryCondition retryCondition = OrRetryCondition.create(new StsRetryCondition(),
                                                              RetryCondition.defaultRetryCondition());

    this.stsClient = StsClient.builder()
                              .applyMutation(this::configureEndpoint)
                              .credentialsProvider(AnonymousCredentialsProvider.create())
                              .overrideConfiguration(o -> o.retryPolicy(r -> r.retryCondition(retryCondition)))
                              .build();

    AssumeRoleWithWebIdentityRequest request = AssumeRoleWithWebIdentityRequest.builder()
                                                                               .roleArn(credentialProperties.roleArn())
                                                                               .roleSessionName(sessionName)
                                                                               .build();

    AssumeRoleWithWebIdentityRequestSupplier supplier =
        new AssumeRoleWithWebIdentityRequestSupplier(request,
                                                     credentialProperties.webIdentityTokenFile());

    this.credentialsProvider =
        StsAssumeRoleWithWebIdentityCredentialsProvider.builder()
                                                       .stsClient(stsClient)
                                                       .refreshRequest(supplier)
                                                       .build();
}
 
Example #4
Source File: RetryPolicy.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Create a {@link RetryPolicy} that will NEVER retry.
 */
public static RetryPolicy none() {
    return RetryPolicy.builder()
                      .numRetries(0)
                      .backoffStrategy(BackoffStrategy.none())
                      .throttlingBackoffStrategy(BackoffStrategy.none())
                      .retryCondition(RetryCondition.none())
                      .additionalRetryConditionsAllowed(false)
                      .build();
}
 
Example #5
Source File: RetryPolicy.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private RetryCondition generateAggregateRetryCondition() {
    RetryCondition aggregate = AndRetryCondition.create(MaxNumberOfRetriesCondition.create(numRetries),
                                                        retryCondition);
    if (retryCapacityCondition != null) {
        return AndRetryCondition.create(aggregate, retryCapacityCondition);
    }
    return aggregate;
}
 
Example #6
Source File: RetryPolicy.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private BuilderImpl(RetryMode retryMode) {
    this.retryMode = retryMode;
    this.numRetries = SdkDefaultRetrySetting.maxAttempts(retryMode) - 1;
    this.additionalRetryConditionsAllowed = true;
    this.backoffStrategy = BackoffStrategy.defaultStrategy();
    this.throttlingBackoffStrategy = BackoffStrategy.defaultThrottlingStrategy();
    this.retryCondition = RetryCondition.defaultRetryCondition();
    this.retryCapacityCondition = TokenBucketRetryCondition.forRetryMode(retryMode);
}
 
Example #7
Source File: AwsRetryPolicy.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieve the {@link RetryCondition#defaultRetryCondition()} with AWS-specific conditions added.
 */
public static RetryCondition defaultRetryCondition() {
    return OrRetryCondition.create(RetryCondition.defaultRetryCondition(), awsRetryCondition());
}
 
Example #8
Source File: AsyncSqsClientFactory.java    From dynein with Apache License 2.0 4 votes vote down vote up
private RetryPolicy buildRetryPolicy(RetryPolicyConfiguration config) {
  RetryPolicy retryPolicy;

  if (config == null) {
    retryPolicy = RetryPolicy.none();
  } else {
    switch (config.getPolicy()) {
      case DEFAULT:
        retryPolicy = RetryPolicy.defaultRetryPolicy();
        break;
      case NONE:
        retryPolicy = RetryPolicy.none();
        break;
      default:
        RetryCondition condition;
        BackoffStrategy strategy;

        switch (config.getCondition()) {
          case DEFAULT:
            condition = RetryCondition.defaultRetryCondition();
            break;
          case MAX_NUM:
            condition = MaxNumberOfRetriesCondition.create(config.getNumRetries());
            break;
          default:
            condition = RetryCondition.none();
        }

        switch (config.getBackOff()) {
          case FULL_JITTER:
            strategy =
                FullJitterBackoffStrategy.builder()
                    .baseDelay(Duration.ofMillis(config.getBaseDelay()))
                    .maxBackoffTime(Duration.ofMillis(config.getMaximumBackoffTime()))
                    .build();
            break;
          case EQUAL_JITTER:
            strategy =
                EqualJitterBackoffStrategy.builder()
                    .baseDelay(Duration.ofMillis(config.getBaseDelay()))
                    .maxBackoffTime(Duration.ofMillis(config.getMaximumBackoffTime()))
                    .build();
            break;
          case FIXED_DELAY:
            strategy = FixedDelayBackoffStrategy.create(Duration.ofMillis(config.getBaseDelay()));
            break;
          case DEFAULT:
            strategy = BackoffStrategy.defaultStrategy();
            break;
          case DEFAULT_THROTTLE:
            strategy = BackoffStrategy.defaultThrottlingStrategy();
            break;
          default:
            strategy = BackoffStrategy.none();
        }

        retryPolicy =
            RetryPolicy.builder()
                .numRetries(config.getNumRetries())
                .retryCondition(condition)
                .backoffStrategy(strategy)
                .build();
    }
  }

  return retryPolicy;
}
 
Example #9
Source File: DefaultRetryConditionTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private boolean shouldRetry(Consumer<RetryPolicyContext.Builder> builder) {
    return RetryCondition.defaultRetryCondition().shouldRetry(RetryPolicyContext.builder()
                                                                .applyMutation(builder)
                                                                .build());
}
 
Example #10
Source File: RetryOnStatusCodeConditionTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void noStatusCodesInList_ReturnsFalse() {
    final RetryCondition noStatusCodes = RetryOnStatusCodeCondition.create(Collections.emptySet());
    assertFalse(noStatusCodes.shouldRetry(RetryPolicyContexts.withStatusCode(404)));
}
 
Example #11
Source File: RetryPolicy.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public RetryCondition retryCapacityCondition() {
    return this.retryCapacityCondition;
}
 
Example #12
Source File: RetryPolicy.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
public void setRetryCapacityCondition(RetryCondition retryCapacityCondition) {
    retryCapacityCondition(retryCapacityCondition);
}
 
Example #13
Source File: RetryPolicy.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public RetryCondition retryCondition() {
    return retryCondition;
}
 
Example #14
Source File: RetryPolicy.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
public void setRetryCondition(RetryCondition retryCondition) {
    retryCondition(retryCondition);
}
 
Example #15
Source File: RetryPolicy.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieve the {@link Builder#retryCondition(RetryCondition)} configured on the builder.
 */
public RetryCondition retryCondition() {
    return retryCondition;
}
 
Example #16
Source File: RetryPolicy.java    From aws-sdk-java-v2 with Apache License 2.0 2 votes vote down vote up
/**
 * @see #retryCapacityCondition(RetryCondition)
 */
RetryCondition retryCapacityCondition();
 
Example #17
Source File: RetryPolicy.java    From aws-sdk-java-v2 with Apache License 2.0 2 votes vote down vote up
/**
 * @see #retryCondition(RetryCondition)
 */
RetryCondition retryCondition();
 
Example #18
Source File: RetryPolicy.java    From aws-sdk-java-v2 with Apache License 2.0 2 votes vote down vote up
/**
 * Retrieve the retry condition that aggregates the {@link Builder#retryCondition(RetryCondition)},
 * {@link Builder#numRetries(Integer)} and {@link Builder#retryCapacityCondition(RetryCondition)} configured on the builder.
 */
public RetryCondition aggregateRetryCondition() {
    return aggregateRetryCondition;
}