|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.retries; |
| 17 | + |
| 18 | +import java.util.function.Predicate; |
| 19 | +import software.amazon.awssdk.annotations.SdkPublicApi; |
| 20 | +import software.amazon.awssdk.annotations.ThreadSafe; |
| 21 | +import software.amazon.awssdk.retries.api.AcquireInitialTokenRequest; |
| 22 | +import software.amazon.awssdk.retries.api.BackoffStrategy; |
| 23 | +import software.amazon.awssdk.retries.api.RetryStrategy; |
| 24 | +import software.amazon.awssdk.retries.internal.AdaptiveRetryStrategyImpl; |
| 25 | +import software.amazon.awssdk.retries.internal.circuitbreaker.TokenBucketStore; |
| 26 | +import software.amazon.awssdk.retries.internal.ratelimiter.RateLimiterTokenBucketStore; |
| 27 | + |
| 28 | +/** |
| 29 | + * The adaptive retry strategy is a {@link RetryStrategy} when executing against a very resource-constrained set of resources. |
| 30 | + * <p> |
| 31 | + * Unlike {@link StandardRetryStrategy}, care should be taken when using this strategy. Specifically, it should be used: |
| 32 | + * <ol> |
| 33 | + * <li>When the availability of downstream resources are mostly affected by callers that are also using |
| 34 | + * the {@link AdaptiveRetryStrategy}. |
| 35 | + * <li>The scope (either the whole strategy or the {@link AcquireInitialTokenRequest#scope}) of the strategy is constrained |
| 36 | + * to target "resource", so that availability issues in one resource cannot delay other, unrelated resource's availability. |
| 37 | + * <p> |
| 38 | + * The adaptive retry strategy by default: |
| 39 | + * <ol> |
| 40 | + * <li>Retries on the conditions configured in the {@link Builder}. |
| 41 | + * <li>Retries 2 times (3 total attempts). Adjust with {@link Builder#maxAttempts} |
| 42 | + * <li>Uses a dynamic backoff delay based on load currently perceived against the downstream resource |
| 43 | + * <li>Circuit breaking (disabling retries) in the event of high downstream failures within an individual scope. |
| 44 | + * Circuit breaking may prevent a first attempt in outage scenarios to protect the downstream service. |
| 45 | + * </ol> |
| 46 | + * |
| 47 | + * @see StandardRetryStrategy |
| 48 | + */ |
| 49 | +@SdkPublicApi |
| 50 | +@ThreadSafe |
| 51 | +public interface AdaptiveRetryStrategy extends RetryStrategy<AdaptiveRetryStrategy.Builder, AdaptiveRetryStrategy> { |
| 52 | + |
| 53 | + /** |
| 54 | + * Create a new {@link AdaptiveRetryStrategy.Builder}. |
| 55 | + * |
| 56 | + * <p>Example Usage |
| 57 | + * <pre> |
| 58 | + * AdaptiveRetryStrategy retryStrategy = |
| 59 | + * AdaptiveRetryStrategy.builder() |
| 60 | + * .retryOnExceptionInstanceOf(IllegalArgumentException.class) |
| 61 | + * .retryOnExceptionInstanceOf(IllegalStateException.class) |
| 62 | + * .build(); |
| 63 | + * </pre> |
| 64 | + */ |
| 65 | + static AdaptiveRetryStrategy.Builder builder() { |
| 66 | + return AdaptiveRetryStrategyImpl |
| 67 | + .builder() |
| 68 | + .maxAttempts(DefaultRetryStrategy.Standard.MAX_ATTEMPTS) |
| 69 | + .tokenBucketStore(TokenBucketStore.builder() |
| 70 | + .tokenBucketMaxCapacity(DefaultRetryStrategy.Standard.TOKEN_BUCKET_SIZE) |
| 71 | + .build()) |
| 72 | + .tokenBucketExceptionCost(DefaultRetryStrategy.Standard.TOKEN_BUCKET_SIZE) |
| 73 | + .backoffStrategy(BackoffStrategy.exponentialDelay(DefaultRetryStrategy.Standard.BASE_DELAY, |
| 74 | + DefaultRetryStrategy.Standard.MAX_BACKOFF)) |
| 75 | + .rateLimiterTokenBucketStore(RateLimiterTokenBucketStore.builder().build()); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + Builder toBuilder(); |
| 80 | + |
| 81 | + interface Builder extends RetryStrategy.Builder<Builder, AdaptiveRetryStrategy> { |
| 82 | + /** |
| 83 | + * Configure the predicate to allow the strategy categorize a Throwable as throttling exception. |
| 84 | + */ |
| 85 | + Builder treatAsThrottling(Predicate<Throwable> treatAsThrottling); |
| 86 | + |
| 87 | + |
| 88 | + @Override |
| 89 | + AdaptiveRetryStrategy build(); |
| 90 | + } |
| 91 | +} |
0 commit comments