Skip to content

Commit 137491c

Browse files
authored
MINOR: Support ExponentialBackoff without jitter (apache#10455)
It is useful to allow ExponentialBackoff to be configured to work without jitter, in order to make unit tests more repeatable. Reviewers: David Arthur <[email protected]>
1 parent 2f36001 commit 137491c

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

clients/src/main/java/org/apache/kafka/common/utils/ExponentialBackoff.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public long backoff(long attempts) {
4747
}
4848
double exp = Math.min(attempts, this.expMax);
4949
double term = initialInterval * Math.pow(multiplier, exp);
50-
double randomFactor = ThreadLocalRandom.current().nextDouble(1 - jitter, 1 + jitter);
50+
double randomFactor = jitter < Double.MIN_NORMAL ? 1.0 :
51+
ThreadLocalRandom.current().nextDouble(1 - jitter, 1 + jitter);
5152
return (long) (randomFactor * term);
5253
}
5354
}

clients/src/test/java/org/apache/kafka/common/utils/ExponentialBackoffTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,13 @@ public void testExponentialBackoff() {
4545
}
4646
}
4747
}
48+
49+
@Test
50+
public void testExponentialBackoffWithoutJitter() {
51+
ExponentialBackoff exponentialBackoff = new ExponentialBackoff(100, 2, 400, 0.0);
52+
assertEquals(100, exponentialBackoff.backoff(0));
53+
assertEquals(200, exponentialBackoff.backoff(1));
54+
assertEquals(400, exponentialBackoff.backoff(2));
55+
assertEquals(400, exponentialBackoff.backoff(3));
56+
}
4857
}

0 commit comments

Comments
 (0)