Skip to content

Conversation

@nhachicha
Copy link
Contributor

@nhachicha nhachicha commented Dec 9, 2025

Relevant specification changes:

JAVA-5950

@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.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ExponentialBackoffTest covers these unit tests already.

@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
@dariakp
Copy link

dariakp commented Dec 17, 2025

@nhachicha Please take note of mongodb/specifications#1868

Copy link
Member

@stIncMale stIncMale left a comment

Choose a reason for hiding this comment

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

*
* @return ExponentialBackoff configured for command retries
*/
public static ExponentialBackoff forCommandRetry() {
Copy link
Member

Choose a reason for hiding this comment

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

Let's not introduce unused code that we foresee to be needed in the future. In this PR, we only need backoffs for the convenient transaction API.

Comment on lines +76 to +82
public static ExponentialBackoff forTransactionRetry() {
return new ExponentialBackoff(
TRANSACTION_BASE_BACKOFF_MS,
TRANSACTION_MAX_BACKOFF_MS,
TRANSACTION_BACKOFF_GROWTH
);
}
Copy link
Member

Choose a reason for hiding this comment

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

Within this PR, I failed to find any benefits of expressing the backoff computation as behavior of an object (an instance of ExponentialBackoff), rather than just a static method; regardless of the approach, the ClientSessionImpl.withTransaction method has to maintain one new local variable: either the lazily initialized transactionBackoff, or the transactionAttempt (that's how it is named in the spec, but we are free to use a different name). Given this, I propose to go with the more straightforward approach of expressing the backoff computation as a static method1, rather than as an object behavior.

If in the future we observe that this is not enough and the "object" approach is needed, we'll be able to change the approach. But we will have a clear reason for that.

Also, storing all the constants in each instance of ExponentialBackoff as instance fields is unnecessary. If we have to use the "object" approach in the future, we better implement it in such a way that does not duplicate constants as instance fields in each object instance (an interface / abstract class with two implementations is one way to achieve that).

P.S. Some other review comments I left are written within the current "object" approach (as opposed to he proposed "method" approach). If the suggestion in this comment is applied, those comments should not be automatically discarded, but rather each should be examined and adopted, if applicable, to the "method" approach.


1 If in the future we need another static method for command retries, we will be able to move the computational logic in a private static method, and call that method from two public methods, passing the suitable constants as method arguments.

* limitations under the License.
*/

package com.mongodb.internal;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe the existing com.mongodb.internal.time package is a better place for ExponentialBackoff?

Comment on lines +33 to +34
* <li>{@link #forTransactionRetry()} - For withTransaction retries (5ms base, 500ms max, 1.5 growth)</li>
* <li>{@link #forCommandRetry()} - For command retries with overload (100ms base, 10000ms max, 2.0 growth)</li>
Copy link
Member

Choose a reason for hiding this comment

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

Let's not duplicate method-level documentation in the class-level documentation. Any duplication opens up opportunities for inconsistencies. We duplicate public API documentation for the sake of user convenience (though I has always been opposed to that, because it have lead to inconsistencies, and will lead to them again, which harm users), but internally, there seem to be no reason to duplicate documentation at all.


/**
* Creates a backoff instance configured for withTransaction retries.
* Uses: 5ms base, 500ms max, 1.5 growth factor.
Copy link
Member

Choose a reason for hiding this comment

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

If you insist that documenting this internally is valuable, which I doubt, let's do that in a way that prevents the documentation and the actual values from becoming inconsistent:

Suggested change
* Uses: 5ms base, 500ms max, 1.5 growth factor.
* Uses: {@value TRANSACTION_BASE_BACKOFF_MS} ms base, {@value TRANSACTION_MAX_BACKOFF_MS} ms max,
* {@value TRANSACTION_BACKOFF_GROWTH} growth factor.

P.S. Notice that I used a space between the numerical value and unit symbol. This is in accordance with the "5.4.3 Formatting the value of a quantity" section in The International System of Units (SI) Brochure released by The International Bureau of Weights and Measures (BIPM).

Comment on lines +226 to +230
session.withTransaction(() -> {
retryCount.incrementAndGet(); // Count the attempt before the operation that might fail
collection.insertOne(session, Document.parse("{ _id : 'backoff-test' }"));
return retryCount;
});
Copy link
Member

Choose a reason for hiding this comment

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

Let's simplify:

Suggested change
session.withTransaction(() -> {
retryCount.incrementAndGet(); // Count the attempt before the operation that might fail
collection.insertOne(session, Document.parse("{ _id : 'backoff-test' }"));
return retryCount;
});
session.withTransaction(() -> {
retryCount.incrementAndGet(); // Count the attempt before the operation that might fail
return collection.insertOne(session, Document.parse("{ _id : 'backoff-test' }"));
});

Comment on lines +271 to +273
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new MongoClientException("Transaction retry interrupted", e);
Copy link
Member

Choose a reason for hiding this comment

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

Let's use InterruptionUtil.interruptAndCreateMongoInterruptedException.

Comment on lines +266 to +274
long backoffMs = transactionBackoff.calculateDelayMs();
try {
if (backoffMs > 0) {
Thread.sleep(backoffMs);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new MongoClientException("Transaction retry interrupted", e);
}
Copy link
Member

Choose a reason for hiding this comment

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

Let's extract this code to a private static method. The withTransaction method is already too long, we should not make it longer without good reason.

try {
outer:
while (true) {
// Apply exponential backoff before retrying transaction
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this comment adds anything of value to a reader. Let's remove it.

@Override
public <T> T withTransaction(final TransactionBody<T> transactionBody, final TransactionOptions options) {
notNull("transactionBody", transactionBody);
long startTime = ClientSessionClock.INSTANCE.now();
Copy link
Member

Choose a reason for hiding this comment

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

[just a comment on a code that wasn't changed in this PR]

I have just noticed this ClientSessionClock - it uses non-monotonic clock. Horrendous.

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.

3 participants