4444import com .mongodb .internal .binding .WriteBinding ;
4545import com .mongodb .internal .connection .AsyncConnection ;
4646import com .mongodb .internal .connection .Connection ;
47+ import com .mongodb .internal .connection .OperationContext ;
4748import com .mongodb .internal .operation .OperationHelper .ResourceSupplierInternalException ;
4849import com .mongodb .internal .operation .retry .AttachmentKeys ;
4950import com .mongodb .internal .validator .NoOpFieldNameValidator ;
@@ -183,19 +184,20 @@ static RetryState initialRetryState(final boolean retry) {
183184 return new RetryState (retry ? RetryState .RETRIES : 0 );
184185 }
185186
186- static <R > Supplier <R > decorateReadWithRetries (final RetryState retryState , final Supplier <R > readFunction ) {
187+ static <R > Supplier <R > decorateReadWithRetries (final RetryState retryState , final OperationContext operationContext ,
188+ final Supplier <R > readFunction ) {
187189 return new RetryingSyncSupplier <>(retryState , CommandOperationHelper ::chooseRetryableReadException ,
188190 CommandOperationHelper ::shouldAttemptToRetryRead , () -> {
189- logRetryExecute (retryState );
191+ logRetryExecute (retryState , operationContext );
190192 return readFunction .get ();
191193 });
192194 }
193195
194- static <R > AsyncCallbackSupplier <R > decorateReadWithRetries (final RetryState retryState ,
196+ static <R > AsyncCallbackSupplier <R > decorateReadWithRetries (final RetryState retryState , final OperationContext operationContext ,
195197 final AsyncCallbackSupplier <R > asyncReadFunction ) {
196198 return new RetryingAsyncCallbackSupplier <>(retryState , CommandOperationHelper ::chooseRetryableReadException ,
197199 CommandOperationHelper ::shouldAttemptToRetryRead , callback -> {
198- logRetryExecute (retryState );
200+ logRetryExecute (retryState , operationContext );
199201 asyncReadFunction .get (callback );
200202 });
201203 }
@@ -219,7 +221,7 @@ static <D, T> T executeRetryableRead(
219221 final CommandReadTransformer <D , T > transformer ,
220222 final boolean retryReads ) {
221223 RetryState retryState = initialRetryState (retryReads );
222- Supplier <T > read = decorateReadWithRetries (retryState , () ->
224+ Supplier <T > read = decorateReadWithRetries (retryState , binding . getOperationContext (), () ->
223225 withSourceAndConnection (readConnectionSourceSupplier , false , (source , connection ) -> {
224226 retryState .breakAndThrowIfRetryAnd (() -> !canRetryRead (source .getServerDescription (), binding .getSessionContext ()));
225227 return createReadCommandAndExecute (retryState , binding , source , database , commandCreator , decoder , transformer , connection );
@@ -288,7 +290,8 @@ static <D, T> void executeRetryableReadAsync(
288290 final SingleResultCallback <T > callback ) {
289291 RetryState retryState = initialRetryState (retryReads );
290292 binding .retain ();
291- AsyncCallbackSupplier <T > asyncRead = CommandOperationHelper .<T >decorateReadWithRetries (retryState , funcCallback ->
293+ AsyncCallbackSupplier <T > asyncRead = CommandOperationHelper .<T >decorateReadWithRetries (retryState , binding .getOperationContext (),
294+ funcCallback ->
292295 withAsyncSourceAndConnection (sourceAsyncSupplier , false , funcCallback ,
293296 (source , connection , releasingCallback ) -> {
294297 if (retryState .breakAndCompleteIfRetryAnd (() -> !canRetryRead (source .getServerDescription (),
@@ -375,19 +378,20 @@ static <T> void executeCommandAsync(final AsyncWriteBinding binding,
375378 binding , transformingWriteCallback (transformer , connection , addingRetryableLabelCallback ));
376379 }
377380
378- static <R > Supplier <R > decorateWriteWithRetries (final RetryState retryState , final Supplier <R > writeFunction ) {
381+ static <R > Supplier <R > decorateWriteWithRetries (final RetryState retryState ,
382+ final OperationContext operationContext , final Supplier <R > writeFunction ) {
379383 return new RetryingSyncSupplier <>(retryState , CommandOperationHelper ::chooseRetryableWriteException ,
380384 CommandOperationHelper ::shouldAttemptToRetryWrite , () -> {
381- logRetryExecute (retryState );
385+ logRetryExecute (retryState , operationContext );
382386 return writeFunction .get ();
383387 });
384388 }
385389
386- static <R > AsyncCallbackSupplier <R > decorateWriteWithRetries (final RetryState retryState ,
390+ static <R > AsyncCallbackSupplier <R > decorateWriteWithRetries (final RetryState retryState , final OperationContext operationContext ,
387391 final AsyncCallbackSupplier <R > asyncWriteFunction ) {
388392 return new RetryingAsyncCallbackSupplier <>(retryState , CommandOperationHelper ::chooseRetryableWriteException ,
389393 CommandOperationHelper ::shouldAttemptToRetryWrite , callback -> {
390- logRetryExecute (retryState );
394+ logRetryExecute (retryState , operationContext );
391395 asyncWriteFunction .get (callback );
392396 });
393397 }
@@ -402,7 +406,7 @@ static <T, R> R executeRetryableWrite(
402406 final CommandWriteTransformer <T , R > transformer ,
403407 final Function <BsonDocument , BsonDocument > retryCommandModifier ) {
404408 RetryState retryState = initialRetryState (true );
405- Supplier <R > retryingWrite = decorateWriteWithRetries (retryState , () -> {
409+ Supplier <R > retryingWrite = decorateWriteWithRetries (retryState , binding . getOperationContext (), () -> {
406410 boolean firstAttempt = retryState .isFirstAttempt ();
407411 if (!firstAttempt && binding .getSessionContext ().hasActiveTransaction ()) {
408412 binding .getSessionContext ().clearTransactionContext ();
@@ -451,7 +455,8 @@ static <T, R> void executeRetryableWriteAsync(
451455 final SingleResultCallback <R > callback ) {
452456 RetryState retryState = initialRetryState (true );
453457 binding .retain ();
454- AsyncCallbackSupplier <R > asyncWrite = CommandOperationHelper .<R >decorateWriteWithRetries (retryState , funcCallback -> {
458+ AsyncCallbackSupplier <R > asyncWrite = CommandOperationHelper .<R >decorateWriteWithRetries (retryState ,
459+ binding .getOperationContext (), funcCallback -> {
455460 boolean firstAttempt = retryState .isFirstAttempt ();
456461 if (!firstAttempt && binding .getSessionContext ().hasActiveTransaction ()) {
457462 binding .getSessionContext ().clearTransactionContext ();
@@ -601,15 +606,17 @@ static void addRetryableWriteErrorLabel(final MongoException exception, final in
601606 }
602607 }
603608
604- static void logRetryExecute (final RetryState retryState ) {
609+ static void logRetryExecute (final RetryState retryState , final OperationContext operationContext ) {
605610 if (LOGGER .isDebugEnabled () && !retryState .isFirstAttempt ()) {
606611 String commandDescription = retryState .attachment (AttachmentKeys .commandDescriptionSupplier ()).map (Supplier ::get ).orElse (null );
607612 Throwable exception = retryState .exception ().orElseThrow (Assertions ::fail );
608613 int oneBasedAttempt = retryState .attempt () + 1 ;
614+ long operationId = operationContext .getId ();
609615 LOGGER .debug (commandDescription == null
610- ? format ("Retrying the operation due to the error \" %s\" ; attempt #%d" , exception , oneBasedAttempt )
611- : format ("Retrying the operation '%s' due to the error \" %s\" ; attempt #%d" ,
612- commandDescription , exception , oneBasedAttempt ));
616+ ? format ("Retrying the operation with operation ID %s due to the error \" %s\" . Attempt number: #%d" ,
617+ operationId , exception , oneBasedAttempt )
618+ : format ("Retrying the operation '%s' with operation ID %s due to the error \" %s\" . Attempt number: #%d" ,
619+ commandDescription , operationId , exception , oneBasedAttempt ));
613620 }
614621 }
615622
0 commit comments