5353import static com .mongodb .operation .OperationHelper .CallableWithConnectionAndSource ;
5454import static com .mongodb .operation .OperationHelper .LOGGER ;
5555import static com .mongodb .operation .OperationHelper .canRetryWrite ;
56+ import static com .mongodb .operation .OperationHelper .isRetryableWrite ;
5657import static com .mongodb .operation .OperationHelper .releasingCallback ;
5758import static com .mongodb .operation .OperationHelper .withConnection ;
5859import static com .mongodb .operation .OperationHelper .withReleasableConnection ;
60+ import static java .lang .String .format ;
5961import static java .util .Arrays .asList ;
6062
6163final class CommandOperationHelper {
@@ -454,6 +456,9 @@ public R call(final ConnectionSource source, final Connection connection) {
454456 } catch (MongoException e ) {
455457 exception = e ;
456458 if (!shouldAttemptToRetry (command , e )) {
459+ if (isRetryWritesEnabled (command )) {
460+ logUnableToRetry (command .getFirstKey (), e );
461+ }
457462 throw exception ;
458463 }
459464 } finally {
@@ -469,6 +474,7 @@ public R call(final ConnectionSource source, final Connection connection) {
469474 if (!canRetryWrite (source .getServerDescription (), connection .getDescription (), binding .getSessionContext ())) {
470475 throw originalException ;
471476 }
477+ logRetryExecute (originalCommand .getFirstKey (), originalException );
472478 return transformer .apply (connection .command (database , originalCommand , fieldNameValidator ,
473479 readPreference , commandResultDecoder , binding .getSessionContext ()),
474480 connection .getDescription ().getServerAddress ());
@@ -545,6 +551,9 @@ public void onResult(final T result, final Throwable originalError) {
545551
546552 private void checkRetryableException (final Throwable originalError , final SingleResultCallback <R > releasingCallback ) {
547553 if (!shouldAttemptToRetry (command , originalError )) {
554+ if (isRetryWritesEnabled (command )) {
555+ logUnableToRetry (command .getFirstKey (), originalError );
556+ }
548557 releasingCallback .onResult (null , originalError );
549558 } else {
550559 oldConnection .release ();
@@ -554,6 +563,7 @@ private void checkRetryableException(final Throwable originalError, final Single
554563 }
555564
556565 private void retryableCommand (final Throwable originalError ) {
566+ logRetryExecute (command .getFirstKey (), originalError );
557567 withConnection (binding , new AsyncCallableWithConnectionAndSource () {
558568 @ Override
559569 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) {
702712 }
703713
704714 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" )));
709721 }
710722
711723 static boolean shouldAttemptToRetry (final boolean retryWritesEnabled , final Throwable exception ) {
712724 return retryWritesEnabled && isRetryableException (exception );
713725 }
714726
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+
715739 private CommandOperationHelper () {
716740 }
717741}
0 commit comments