Skip to content

Conversation

@nhachicha
Copy link
Contributor

@nhachicha nhachicha requested a review from Copilot December 9, 2025 14:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements exponential backoff with jitter for transaction retries in MongoDB's withTransaction convenience API. The implementation adds a configurable backoff mechanism that applies delays between retry attempts when transient transaction errors occur, following the MongoDB specification with a growth factor of 1.5 for transactions.

Key Changes

  • Introduces ExponentialBackoff utility class with factory methods for transaction retries (5ms base, 500ms max, 1.5x growth) and command retries (100ms base, 10s max, 2.0x growth)
  • Integrates backoff logic into ClientSessionImpl.withTransaction() to delay between retry attempts
  • Adjusts test configuration to verify backoff behavior with multiple retry attempts

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
driver-core/src/main/com/mongodb/internal/ExponentialBackoff.java New utility class implementing exponential backoff with jitter using ThreadLocalRandom
driver-sync/src/main/com/mongodb/client/internal/ClientSessionImpl.java Adds backoff delay before transaction retries and uses CSOT timeout when available
driver-core/src/test/unit/com/mongodb/internal/ExponentialBackoffTest.java Comprehensive unit tests validating backoff calculations, growth factors, and maximum caps
driver-sync/src/test/functional/com/mongodb/client/WithTransactionProseTest.java New functional test verifying exponential backoff behavior and adjusted existing test configuration

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

AtomicInteger retryCount = new AtomicInteger(0);

session.withTransaction(() -> {
retryCount.incrementAndGet(); // Count the attempt before the operation that might fail
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test verifies the retry count but does not validate that exponential backoff delays are actually applied. Consider measuring elapsed time and asserting minimum expected delays to ensure backoff is functioning correctly. For example, with 3 retries at delays of ~5ms, ~7.5ms, and ~11.25ms, the total elapsed time should be at least the sum of minimum expected delays.

Copilot uses AI. Check for mistakes.
@nhachicha nhachicha marked this pull request as ready for review December 9, 2025 15:24
@nhachicha nhachicha requested a review from a team as a code owner December 9, 2025 15:24
@nhachicha nhachicha requested review from stIncMale and strogiyotec and removed request for strogiyotec December 9, 2025 15:24
nhachicha and others added 2 commits December 9, 2025 15:32
…Impl.java

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@vbabanin vbabanin self-requested a review December 9, 2025 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant