53
53
import static com .mongodb .operation .OperationHelper .CallableWithConnectionAndSource ;
54
54
import static com .mongodb .operation .OperationHelper .LOGGER ;
55
55
import static com .mongodb .operation .OperationHelper .canRetryWrite ;
56
+ import static com .mongodb .operation .OperationHelper .isRetryableWrite ;
56
57
import static com .mongodb .operation .OperationHelper .releasingCallback ;
57
58
import static com .mongodb .operation .OperationHelper .withConnection ;
58
59
import static com .mongodb .operation .OperationHelper .withReleasableConnection ;
60
+ import static java .lang .String .format ;
59
61
import static java .util .Arrays .asList ;
60
62
61
63
final class CommandOperationHelper {
@@ -454,6 +456,9 @@ public R call(final ConnectionSource source, final Connection connection) {
454
456
} catch (MongoException e ) {
455
457
exception = e ;
456
458
if (!shouldAttemptToRetry (command , e )) {
459
+ if (isRetryWritesEnabled (command )) {
460
+ logUnableToRetry (command .getFirstKey (), e );
461
+ }
457
462
throw exception ;
458
463
}
459
464
} finally {
@@ -469,6 +474,7 @@ public R call(final ConnectionSource source, final Connection connection) {
469
474
if (!canRetryWrite (source .getServerDescription (), connection .getDescription (), binding .getSessionContext ())) {
470
475
throw originalException ;
471
476
}
477
+ logRetryExecute (originalCommand .getFirstKey (), originalException );
472
478
return transformer .apply (connection .command (database , originalCommand , fieldNameValidator ,
473
479
readPreference , commandResultDecoder , binding .getSessionContext ()),
474
480
connection .getDescription ().getServerAddress ());
@@ -545,6 +551,9 @@ public void onResult(final T result, final Throwable originalError) {
545
551
546
552
private void checkRetryableException (final Throwable originalError , final SingleResultCallback <R > releasingCallback ) {
547
553
if (!shouldAttemptToRetry (command , originalError )) {
554
+ if (isRetryWritesEnabled (command )) {
555
+ logUnableToRetry (command .getFirstKey (), originalError );
556
+ }
548
557
releasingCallback .onResult (null , originalError );
549
558
} else {
550
559
oldConnection .release ();
@@ -554,6 +563,7 @@ private void checkRetryableException(final Throwable originalError, final Single
554
563
}
555
564
556
565
private void retryableCommand (final Throwable originalError ) {
566
+ logRetryExecute (command .getFirstKey (), originalError );
557
567
withConnection (binding , new AsyncCallableWithConnectionAndSource () {
558
568
@ Override
559
569
public void call (final AsyncConnectionSource source , final AsyncConnection connection , final Throwable t ) {
@@ -702,16 +712,30 @@ public void onResult(final D response, final Throwable t) {
702
712
}
703
713
704
714
private static boolean shouldAttemptToRetry (@ Nullable final BsonDocument command , final Throwable exception ) {
705
- return shouldAttemptToRetry (command != null
706
- && (command .containsKey ("txnNumber" )
707
- || command .getFirstKey ().equals ("commitTransaction" ) || command .getFirstKey ().equals ("abortTransaction" )),
708
- exception );
715
+ return isRetryWritesEnabled (command ) && isRetryableException (exception );
716
+ }
717
+
718
+ private static boolean isRetryWritesEnabled (@ Nullable final BsonDocument command ) {
719
+ return (command != null && (command .containsKey ("txnNumber" )
720
+ || command .getFirstKey ().equals ("commitTransaction" ) || command .getFirstKey ().equals ("abortTransaction" )));
709
721
}
710
722
711
723
static boolean shouldAttemptToRetry (final boolean retryWritesEnabled , final Throwable exception ) {
712
724
return retryWritesEnabled && isRetryableException (exception );
713
725
}
714
726
727
+ static void logRetryExecute (final String operation , final Throwable originalError ) {
728
+ if (LOGGER .isDebugEnabled ()) {
729
+ LOGGER .debug (format ("Retrying operation %s due to an error \" %s\" " , operation , originalError ));
730
+ }
731
+ }
732
+
733
+ static void logUnableToRetry (final String operation , final Throwable originalError ) {
734
+ if (LOGGER .isDebugEnabled ()) {
735
+ LOGGER .debug (format ("Unable to retry operation %s due to error \" %s\" " , operation , originalError ));
736
+ }
737
+ }
738
+
715
739
private CommandOperationHelper () {
716
740
}
717
741
}
0 commit comments