Skip to content

Commit e32f90d

Browse files
committed
PR feedback
1 parent 1c97dc7 commit e32f90d

3 files changed

Lines changed: 11 additions & 11 deletions

File tree

driver-core/src/main/com/mongodb/internal/time/ExponentialBackoff.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ private ExponentialBackoff() {
4242
/**
4343
* Calculate the backoff in milliseconds for transaction retries.
4444
*
45-
* @param attemptNumber The 0-based attempt number
45+
* @param attemptNumber The attempt number
4646
* @return The calculated backoff in milliseconds.
4747
*/
4848
public static long calculateTransactionBackoffMs(final int attemptNumber) {
4949
double jitter = testJitterSupplier != null
5050
? testJitterSupplier.getAsDouble()
5151
: ThreadLocalRandom.current().nextDouble();
5252
return Math.round(jitter * Math.min(
53-
TRANSACTION_BASE_MS * Math.pow(TRANSACTION_GROWTH, attemptNumber),
53+
TRANSACTION_BASE_MS * Math.pow(TRANSACTION_GROWTH, attemptNumber - 1),
5454
TRANSACTION_MAX_MS));
5555
}
5656

driver-core/src/test/unit/com/mongodb/internal/ExponentialBackoffTest.java renamed to driver-core/src/test/unit/com/mongodb/internal/time/ExponentialBackoffTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.mongodb.internal;
17+
package com.mongodb.internal.time;
1818

19-
import com.mongodb.internal.time.ExponentialBackoff;
2019
import org.junit.jupiter.api.Test;
2120

2221
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -31,17 +30,18 @@ void testTransactionRetryBackoff() {
3130
// With jitter, actual values will be between 0 and these maxima
3231
double[] expectedMaxValues = {5.0, 7.5, 11.25, 16.875, 25.3125, 37.96875, 56.953125, 85.4296875, 128.14453125, 192.21679688, 288.32519531, 432.48779297, 500.0};
3332

34-
for (int attemptNumber = 0; attemptNumber < expectedMaxValues.length; attemptNumber++) {
33+
for (int attemptNumber = 1; attemptNumber <= expectedMaxValues.length; attemptNumber++) {
3534
long backoff = ExponentialBackoff.calculateTransactionBackoffMs(attemptNumber);
36-
assertTrue(backoff >= 0 && backoff <= Math.round(expectedMaxValues[attemptNumber]),
37-
String.format("Attempt %d: backoff should be 0-%d ms, got: %d", attemptNumber, Math.round(expectedMaxValues[attemptNumber]), backoff));
35+
assertTrue(backoff >= 0 && backoff <= Math.round(expectedMaxValues[attemptNumber - 1]),
36+
String.format("Attempt %d: backoff should be 0-%d ms, got: %d", attemptNumber,
37+
Math.round(expectedMaxValues[attemptNumber - 1]), backoff));
3838
}
3939
}
4040

4141
@Test
4242
void testTransactionRetryBackoffRespectsMaximum() {
4343
// Even at high attempt numbers, backoff should never exceed 500ms
44-
for (int attemptNumber = 0; attemptNumber < 25; attemptNumber++) {
44+
for (int attemptNumber = 1; attemptNumber < 26; attemptNumber++) {
4545
long backoff = ExponentialBackoff.calculateTransactionBackoffMs(attemptNumber);
4646
assertTrue(backoff >= 0 && backoff <= 500,
4747
String.format("Attempt %d: backoff should be capped at 500 ms, got: %d ms", attemptNumber, backoff));
@@ -56,9 +56,9 @@ void testCustomJitter() {
5656
// Test with jitter = 1.0
5757
ExponentialBackoff.setTestJitterSupplier(() -> 1.0);
5858
try {
59-
for (int attemptNumber = 0; attemptNumber < expectedBackoffs.length; attemptNumber++) {
59+
for (int attemptNumber = 1; attemptNumber <= expectedBackoffs.length; attemptNumber++) {
6060
long backoff = ExponentialBackoff.calculateTransactionBackoffMs(attemptNumber);
61-
long expected = Math.round(expectedBackoffs[attemptNumber]);
61+
long expected = Math.round(expectedBackoffs[attemptNumber - 1]);
6262
assertEquals(expected, backoff,
6363
String.format("Attempt %d: with jitter=1.0, backoff should be %d ms", attemptNumber, expected));
6464
}

driver-sync/src/main/com/mongodb/client/internal/ClientSessionImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ private TimeoutContext createTimeoutContext(final TransactionOptions transaction
382382

383383
private static void backoff(final int transactionAttempt,
384384
final Timeout withTransactionTimeout, final MongoException lastError) {
385-
long backoffMs = ExponentialBackoff.calculateTransactionBackoffMs(transactionAttempt - 1);
385+
long backoffMs = ExponentialBackoff.calculateTransactionBackoffMs(transactionAttempt);
386386
withTransactionTimeout.shortenBy(backoffMs, TimeUnit.MILLISECONDS).onExpired(() -> {
387387
throw lastError;
388388
});

0 commit comments

Comments
 (0)