Skip to content

Commit 6d36235

Browse files
authored
FullJitterBackoffStrategy zero check (#4809)
* Fix IllegalArgumentException in FullJitterBackoffStrategy when base delay and max backoff time are zero.
1 parent db355d3 commit 6d36235

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "",
5+
"description": "Fix IllegalArgumentException in FullJitterBackoffStrategy when base delay and max backoff time are zero."
6+
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/retry/backoff/FullJitterBackoffStrategy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ private FullJitterBackoffStrategy(BuilderImpl builder) {
6161
@Override
6262
public Duration computeDelayBeforeNextRetry(RetryPolicyContext context) {
6363
int ceil = calculateExponentialDelay(context.retriesAttempted(), baseDelay, maxBackoffTime);
64-
// Minimum of 1 ms (consistent with BackoffStrategy.none()'s behavior)
65-
return Duration.ofMillis(random.nextInt(ceil) + 1L);
64+
return ceil == 0 ? Duration.ofMillis(0L) : Duration.ofMillis(random.nextInt(ceil) + 1L);
6665
}
6766

6867
@Override

core/sdk-core/src/test/java/software/amazon/awssdk/core/retry/RetryPolicyTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,16 @@ public void fastFailRateLimitingConfigured_retryModeAdaptive_doesNotThrow() {
185185
public void hashCodeDoesNotThrow() {
186186
RetryPolicy.defaultRetryPolicy().hashCode();
187187
}
188+
189+
@Test
190+
public void fullJitter_zeroBackoffTimeZeroDelayZeroRetries_delayShouldBeZero() {
191+
RetryPolicyContext context = RetryPolicyContext.builder()
192+
.retriesAttempted(0)
193+
.build();
194+
FullJitterBackoffStrategy strategy = FullJitterBackoffStrategy.builder()
195+
.baseDelay(Duration.ZERO)
196+
.maxBackoffTime(Duration.ZERO)
197+
.build();
198+
assertThat(strategy.computeDelayBeforeNextRetry(context)).isEqualTo(Duration.ofMillis(0L));
199+
}
188200
}

utils/src/main/java/software/amazon/awssdk/utils/Validate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,11 +757,11 @@ public static Long isPositiveOrNull(Long num, String fieldName) {
757757
}
758758

759759
/**
760-
* Asserts that the given duration is positive (non-negative and non-zero).
760+
* Asserts that the given duration is positive, including zero.
761761
*
762762
* @param duration Number to validate
763763
* @param fieldName Field name to display in exception message if not positive.
764-
* @return Duration if positive.
764+
* @return Duration if positive or zero.
765765
*/
766766
public static Duration isNotNegative(Duration duration, String fieldName) {
767767
if (duration == null) {

0 commit comments

Comments
 (0)