Skip to content

Commit 90ec4d5

Browse files
committed
Simplifying implementation by relying on CSOT to throw when timeout is exceeded (ex operationContext.getTimeoutContext().getReadTimeoutMS())
1 parent b051701 commit 90ec4d5

File tree

1 file changed

+5
-23
lines changed

1 file changed

+5
-23
lines changed

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import com.mongodb.WriteConcern;
2828
import com.mongodb.client.ClientSession;
2929
import com.mongodb.client.TransactionBody;
30-
import com.mongodb.internal.TimeoutContext;
3130
import com.mongodb.internal.ExponentialBackoff;
31+
import com.mongodb.internal.TimeoutContext;
32+
import com.mongodb.internal.observability.micrometer.TracingManager;
33+
import com.mongodb.internal.observability.micrometer.TransactionSpan;
3234
import com.mongodb.internal.operation.AbortTransactionOperation;
3335
import com.mongodb.internal.operation.CommitTransactionOperation;
3436
import com.mongodb.internal.operation.OperationHelper;
@@ -37,8 +39,6 @@
3739
import com.mongodb.internal.operation.WriteOperation;
3840
import com.mongodb.internal.session.BaseClientSessionImpl;
3941
import com.mongodb.internal.session.ServerSessionPool;
40-
import com.mongodb.internal.observability.micrometer.TracingManager;
41-
import com.mongodb.internal.observability.micrometer.TransactionSpan;
4242
import com.mongodb.lang.Nullable;
4343

4444
import static com.mongodb.MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL;
@@ -252,12 +252,8 @@ public <T> T withTransaction(final TransactionBody<T> transactionBody, final Tra
252252
notNull("transactionBody", transactionBody);
253253
long startTime = ClientSessionClock.INSTANCE.now();
254254
TimeoutContext withTransactionTimeoutContext = createTimeoutContext(options);
255-
// Use CSOT timeout if set, otherwise default to MAX_RETRY_TIME_LIMIT_MS
256-
Long timeoutMS = withTransactionTimeoutContext.getTimeoutSettings().getTimeoutMS();
257-
long maxRetryTimeMS = timeoutMS != null ? timeoutMS : MAX_RETRY_TIME_LIMIT_MS;
258255
ExponentialBackoff transactionBackoff = null;
259256
boolean isRetry = false;
260-
MongoException lastError = null;
261257

262258
try {
263259
outer:
@@ -267,19 +263,7 @@ public <T> T withTransaction(final TransactionBody<T> transactionBody, final Tra
267263
if (transactionBackoff == null) {
268264
transactionBackoff = ExponentialBackoff.forTransactionRetry();
269265
}
270-
// Calculate backoff delay and check if it would exceed timeout
271266
long backoffMs = transactionBackoff.calculateDelayMs();
272-
if (ClientSessionClock.INSTANCE.now() - startTime + backoffMs >= maxRetryTimeMS) {
273-
// If CSOT is enabled (timeoutMS is set), throw MongoOperationTimeoutException
274-
// Otherwise, throw the last error directly for backward compatibility
275-
if (timeoutMS != null) {
276-
throw new MongoOperationTimeoutException(
277-
"Transaction retry timeout exceeded after " + (ClientSessionClock.INSTANCE.now() - startTime) + "ms",
278-
lastError);
279-
} else {
280-
throw lastError;
281-
}
282-
}
283267
try {
284268
if (backoffMs > 0) {
285269
Thread.sleep(backoffMs);
@@ -304,8 +288,7 @@ public <T> T withTransaction(final TransactionBody<T> transactionBody, final Tra
304288
if (e instanceof MongoException && !(e instanceof MongoOperationTimeoutException)) {
305289
MongoException exceptionToHandle = OperationHelper.unwrap((MongoException) e);
306290
if (exceptionToHandle.hasErrorLabel(TRANSIENT_TRANSACTION_ERROR_LABEL)
307-
&& ClientSessionClock.INSTANCE.now() - startTime < maxRetryTimeMS) {
308-
lastError = exceptionToHandle; // Track the last error for timeout scenarios
291+
&& ClientSessionClock.INSTANCE.now() - startTime < MAX_RETRY_TIME_LIMIT_MS) {
309292
if (transactionSpan != null) {
310293
transactionSpan.spanFinalizing(false);
311294
}
@@ -320,10 +303,9 @@ public <T> T withTransaction(final TransactionBody<T> transactionBody, final Tra
320303
commitTransaction(false);
321304
break;
322305
} catch (MongoException e) {
323-
lastError = e; // Track the last error for timeout scenarios
324306
clearTransactionContextOnError(e);
325307
if (!(e instanceof MongoOperationTimeoutException)
326-
&& ClientSessionClock.INSTANCE.now() - startTime < maxRetryTimeMS) {
308+
&& ClientSessionClock.INSTANCE.now() - startTime < MAX_RETRY_TIME_LIMIT_MS) {
327309
applyMajorityWriteConcernToTransactionOptions();
328310

329311
if (!(e instanceof MongoExecutionTimeoutException)

0 commit comments

Comments
 (0)