-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add default backoff strategies (#3906)
* Add default backoff strategies * Moved the backoff strategires to the SPI package * Use AssertJ instead of Hamcrest
- Loading branch information
Showing
12 changed files
with
794 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
.../java/software/amazon/awssdk/retries/api/internal/backoff/BackoffStrategiesConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.retries.api.internal.backoff; | ||
|
||
import java.time.Duration; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
|
||
/** | ||
* Constants and utility functions shared by the BackoffStrategy implementations. | ||
*/ | ||
@SdkInternalApi | ||
class BackoffStrategiesConstants { | ||
static final Duration BASE_DELAY_CEILING = Duration.ofMillis(Integer.MAX_VALUE); // Around ~24.8 days | ||
static final Duration MAX_BACKOFF_CEILING = Duration.ofMillis(Integer.MAX_VALUE); // Around ~24.8 days | ||
/** | ||
* Max permitted retry times. To prevent exponentialDelay from overflow, there must be 2 ^ retriesAttempted <= 2 ^ 31 - 1, | ||
* which means retriesAttempted <= 30, so that is the ceil for retriesAttempted. | ||
*/ | ||
static final int RETRIES_ATTEMPTED_CEILING = (int) Math.floor(Math.log(Integer.MAX_VALUE) / Math.log(2)); | ||
|
||
private BackoffStrategiesConstants() { | ||
} | ||
|
||
/** | ||
* Returns the computed exponential delay in milliseconds given the retries attempted, the base delay and the max backoff | ||
* time. | ||
* | ||
* <p>Specifically it returns {@code min(maxDelay, baseDelay * (1 << (attempt - 2)))}. To prevent overflowing the attempts | ||
* get capped to 30. | ||
*/ | ||
static int calculateExponentialDelay(int retriesAttempted, Duration baseDelay, Duration maxBackoffTime) { | ||
int cappedRetries = Math.min(retriesAttempted, BackoffStrategiesConstants.RETRIES_ATTEMPTED_CEILING); | ||
return (int) Math.min(baseDelay.multipliedBy(1L << (cappedRetries - 2)).toMillis(), maxBackoffTime.toMillis()); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
.../java/software/amazon/awssdk/retries/api/internal/backoff/ExponentialDelayWithJitter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.retries.api.internal.backoff; | ||
|
||
import static software.amazon.awssdk.retries.api.internal.backoff.BackoffStrategiesConstants.calculateExponentialDelay; | ||
|
||
import java.time.Duration; | ||
import java.util.Random; | ||
import java.util.function.Supplier; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.retries.api.BackoffStrategy; | ||
import software.amazon.awssdk.utils.NumericUtils; | ||
import software.amazon.awssdk.utils.ToString; | ||
import software.amazon.awssdk.utils.Validate; | ||
|
||
/** | ||
* Strategy that waits for a random period of time between 0ms and an exponentially increasing amount of time between each | ||
* subsequent attempt of the same call. | ||
* | ||
* <p>Specifically, the first attempt waits 0ms, and each subsequent attempt waits between | ||
* 0ms and {@code min(maxDelay, baseDelay * (1 << (attempt - 2)))}. | ||
*/ | ||
@SdkInternalApi | ||
public final class ExponentialDelayWithJitter implements BackoffStrategy { | ||
private final Supplier<Random> randomSupplier; | ||
private final Duration baseDelay; | ||
private final Duration maxDelay; | ||
|
||
public ExponentialDelayWithJitter(Supplier<Random> randomSupplier, Duration baseDelay, Duration maxDelay) { | ||
this.randomSupplier = Validate.paramNotNull(randomSupplier, "random"); | ||
this.baseDelay = NumericUtils.min(Validate.isPositive(baseDelay, "baseDelay"), | ||
BackoffStrategiesConstants.BASE_DELAY_CEILING); | ||
this.maxDelay = NumericUtils.min(Validate.isPositive(maxDelay, "maxDelay"), | ||
BackoffStrategiesConstants.MAX_BACKOFF_CEILING); | ||
} | ||
|
||
@Override | ||
public Duration computeDelay(int attempt) { | ||
Validate.isPositive(attempt, "attempt"); | ||
if (attempt == 1) { | ||
return Duration.ZERO; | ||
} | ||
int delay = calculateExponentialDelay(attempt, baseDelay, maxDelay); | ||
int randInt = randomSupplier.get().nextInt(delay); | ||
return Duration.ofMillis(randInt); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ToString.builder("ExponentialDelayWithJitter") | ||
.add("baseDelay", baseDelay) | ||
.add("maxDelay", maxDelay) | ||
.build(); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...va/software/amazon/awssdk/retries/api/internal/backoff/ExponentialDelayWithoutJitter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.retries.api.internal.backoff; | ||
|
||
import static software.amazon.awssdk.retries.api.internal.backoff.BackoffStrategiesConstants.calculateExponentialDelay; | ||
|
||
import java.time.Duration; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.retries.api.BackoffStrategy; | ||
import software.amazon.awssdk.utils.NumericUtils; | ||
import software.amazon.awssdk.utils.ToString; | ||
import software.amazon.awssdk.utils.Validate; | ||
|
||
/** | ||
* Strategy that waits for an exponentially increasing amount of time between each subsequent attempt of the same call. | ||
* | ||
* <p>Specifically, the first attempt waits 0ms, and each subsequent attempt waits for | ||
* {@code min(maxDelay, baseDelay * (1 << (attempt - 2)))}. | ||
*/ | ||
@SdkInternalApi | ||
public final class ExponentialDelayWithoutJitter implements BackoffStrategy { | ||
private final Duration baseDelay; | ||
private final Duration maxDelay; | ||
|
||
public ExponentialDelayWithoutJitter(Duration baseDelay, Duration maxDelay) { | ||
this.baseDelay = NumericUtils.min(Validate.isPositive(baseDelay, "baseDelay"), | ||
BackoffStrategiesConstants.BASE_DELAY_CEILING); | ||
this.maxDelay = NumericUtils.min(Validate.isPositive(maxDelay, "maxDelay"), | ||
BackoffStrategiesConstants.MAX_BACKOFF_CEILING); | ||
} | ||
|
||
@Override | ||
public Duration computeDelay(int attempt) { | ||
Validate.isPositive(attempt, "attempt"); | ||
if (attempt == 1) { | ||
return Duration.ZERO; | ||
} | ||
int delay = calculateExponentialDelay(attempt, baseDelay, maxDelay); | ||
return Duration.ofMillis(delay); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ToString.builder("ExponentialDelayWithoutJitter") | ||
.add("baseDelay", baseDelay) | ||
.add("maxDelay", maxDelay) | ||
.build(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...c/main/java/software/amazon/awssdk/retries/api/internal/backoff/FixedDelayWithJitter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.retries.api.internal.backoff; | ||
|
||
import java.time.Duration; | ||
import java.util.Random; | ||
import java.util.function.Supplier; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.retries.api.BackoffStrategy; | ||
import software.amazon.awssdk.utils.NumericUtils; | ||
import software.amazon.awssdk.utils.ToString; | ||
import software.amazon.awssdk.utils.Validate; | ||
|
||
/** | ||
* Strategy that waits for a random period of time between 0ms and the provided delay. | ||
*/ | ||
@SdkInternalApi | ||
public final class FixedDelayWithJitter implements BackoffStrategy { | ||
private final Supplier<Random> randomSupplier; | ||
private final Duration delay; | ||
|
||
public FixedDelayWithJitter(Supplier<Random> randomSupplier, Duration delay) { | ||
this.randomSupplier = Validate.paramNotNull(randomSupplier, "random"); | ||
this.delay = NumericUtils.min(Validate.isPositive(delay, "delay"), BackoffStrategiesConstants.BASE_DELAY_CEILING); | ||
} | ||
|
||
@Override | ||
public Duration computeDelay(int attempt) { | ||
Validate.isPositive(attempt, "attempt"); | ||
return Duration.ofMillis(randomSupplier.get().nextInt((int) delay.toMillis())); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ToString.builder("FixedDelayWithJitter") | ||
.add("delay", delay) | ||
.build(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...ain/java/software/amazon/awssdk/retries/api/internal/backoff/FixedDelayWithoutJitter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.retries.api.internal.backoff; | ||
|
||
import java.time.Duration; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.retries.api.BackoffStrategy; | ||
import software.amazon.awssdk.utils.ToString; | ||
import software.amazon.awssdk.utils.Validate; | ||
|
||
/** | ||
* Strategy that waits for a period of time equal to the provided delay. | ||
*/ | ||
@SdkInternalApi | ||
public final class FixedDelayWithoutJitter implements BackoffStrategy { | ||
private final Duration delay; | ||
|
||
public FixedDelayWithoutJitter(Duration delay) { | ||
this.delay = Validate.isPositive(delay, "delay"); | ||
} | ||
|
||
@Override | ||
public Duration computeDelay(int attempt) { | ||
Validate.isPositive(attempt, "attempt"); | ||
return delay; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ToString.builder("FixedDelayWithoutJitter") | ||
.add("delay", delay) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.