Skip to content

Commit

Permalink
Implement the proposed code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
stIncMale committed May 8, 2024
1 parent 246353f commit 69b78ff
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

import java.util.Collections;
import java.util.List;
import java.util.function.BinaryOperator;

import static com.mongodb.assertions.Assertions.assertNotNull;
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
Expand All @@ -53,6 +52,8 @@
import static com.mongodb.internal.operation.CommandOperationHelper.initialRetryState;
import static com.mongodb.internal.operation.CommandOperationHelper.isRetryWritesEnabled;
import static com.mongodb.internal.operation.CommandOperationHelper.logRetryExecute;
import static com.mongodb.internal.operation.CommandOperationHelper.onRetryableReadAttemptFailure;
import static com.mongodb.internal.operation.CommandOperationHelper.onRetryableWriteAttemptFailure;
import static com.mongodb.internal.operation.CommandOperationHelper.transformWriteException;
import static com.mongodb.internal.operation.WriteConcernHelper.throwOnWriteConcernError;

Expand Down Expand Up @@ -286,11 +287,7 @@ static <D, T> void createReadCommandAndExecuteAsync(

static <R> AsyncCallbackSupplier<R> decorateReadWithRetriesAsync(final RetryState retryState, final OperationContext operationContext,
final AsyncCallbackSupplier<R> asyncReadFunction) {
BinaryOperator<Throwable> onAttemptFailure =
(@Nullable Throwable previouslyChosenException, Throwable mostRecentAttemptException) ->
CommandOperationHelper.onRetryableReadAttemptFailure(
operationContext, previouslyChosenException, mostRecentAttemptException);
return new RetryingAsyncCallbackSupplier<>(retryState, onAttemptFailure,
return new RetryingAsyncCallbackSupplier<>(retryState, onRetryableReadAttemptFailure(operationContext),
CommandOperationHelper::shouldAttemptToRetryRead, callback -> {
logRetryExecute(retryState, operationContext);
asyncReadFunction.get(callback);
Expand All @@ -299,11 +296,7 @@ static <R> AsyncCallbackSupplier<R> decorateReadWithRetriesAsync(final RetryStat

static <R> AsyncCallbackSupplier<R> decorateWriteWithRetriesAsync(final RetryState retryState, final OperationContext operationContext,
final AsyncCallbackSupplier<R> asyncWriteFunction) {
BinaryOperator<Throwable> onAttemptFailure =
(@Nullable Throwable previouslyChosenException, Throwable mostRecentAttemptException) ->
CommandOperationHelper.onRetryableWriteAttemptFailure(
operationContext, previouslyChosenException, mostRecentAttemptException);
return new RetryingAsyncCallbackSupplier<>(retryState, onAttemptFailure,
return new RetryingAsyncCallbackSupplier<>(retryState, onRetryableWriteAttemptFailure(operationContext),
CommandOperationHelper::shouldAttemptToRetryWrite, callback -> {
logRetryExecute(retryState, operationContext);
asyncWriteFunction.get(callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.bson.BsonDocument;

import java.util.List;
import java.util.function.BinaryOperator;
import java.util.function.Supplier;

import static com.mongodb.assertions.Assertions.assertFalse;
Expand All @@ -51,12 +52,11 @@ interface CommandCreator {
BsonDocument create(ServerDescription serverDescription, ConnectionDescription connectionDescription);
}

static Throwable onRetryableReadAttemptFailure(
final OperationContext operationContext,
@Nullable final Throwable previouslyChosenException,
final Throwable mostRecentAttemptException) {
operationContext.getServerDeprioritization().onAttemptFailure(mostRecentAttemptException);
return chooseRetryableReadException(previouslyChosenException, mostRecentAttemptException);
static BinaryOperator<Throwable> onRetryableReadAttemptFailure(final OperationContext operationContext) {
return (@Nullable Throwable previouslyChosenException, Throwable mostRecentAttemptException) -> {
operationContext.getServerDeprioritization().onAttemptFailure(mostRecentAttemptException);
return chooseRetryableReadException(previouslyChosenException, mostRecentAttemptException);
};
}

private static Throwable chooseRetryableReadException(
Expand All @@ -71,12 +71,11 @@ private static Throwable chooseRetryableReadException(
}
}

static Throwable onRetryableWriteAttemptFailure(
final OperationContext operationContext,
@Nullable final Throwable previouslyChosenException,
final Throwable mostRecentAttemptException) {
operationContext.getServerDeprioritization().onAttemptFailure(mostRecentAttemptException);
return chooseRetryableWriteException(previouslyChosenException, mostRecentAttemptException);
static BinaryOperator<Throwable> onRetryableWriteAttemptFailure(final OperationContext operationContext) {
return (@Nullable Throwable previouslyChosenException, Throwable mostRecentAttemptException) -> {
operationContext.getServerDeprioritization().onAttemptFailure(mostRecentAttemptException);
return chooseRetryableWriteException(previouslyChosenException, mostRecentAttemptException);
};
}

private static Throwable chooseRetryableWriteException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.BinaryOperator;
import java.util.function.Supplier;
import java.util.stream.Collectors;

Expand All @@ -63,6 +62,7 @@
import static com.mongodb.internal.operation.AsyncOperationHelper.withAsyncSourceAndConnection;
import static com.mongodb.internal.operation.CommandOperationHelper.addRetryableWriteErrorLabel;
import static com.mongodb.internal.operation.CommandOperationHelper.logRetryExecute;
import static com.mongodb.internal.operation.CommandOperationHelper.onRetryableWriteAttemptFailure;
import static com.mongodb.internal.operation.CommandOperationHelper.transformWriteException;
import static com.mongodb.internal.operation.OperationHelper.LOGGER;
import static com.mongodb.internal.operation.OperationHelper.isRetryableWrite;
Expand Down Expand Up @@ -141,11 +141,7 @@ public Boolean getRetryWrites() {

private <R> Supplier<R> decorateWriteWithRetries(final RetryState retryState, final OperationContext operationContext,
final Supplier<R> writeFunction) {
BinaryOperator<Throwable> onAttemptFailure =
(@Nullable Throwable previouslyChosenException, Throwable mostRecentAttemptException) ->
CommandOperationHelper.onRetryableWriteAttemptFailure(
operationContext, previouslyChosenException, mostRecentAttemptException);
return new RetryingSyncSupplier<>(retryState, onAttemptFailure,
return new RetryingSyncSupplier<>(retryState, onRetryableWriteAttemptFailure(operationContext),
this::shouldAttemptToRetryWrite, () -> {
logRetryExecute(retryState, operationContext);
return writeFunction.get();
Expand All @@ -154,11 +150,7 @@ private <R> Supplier<R> decorateWriteWithRetries(final RetryState retryState, fi

private <R> AsyncCallbackSupplier<R> decorateWriteWithRetries(final RetryState retryState, final OperationContext operationContext,
final AsyncCallbackSupplier<R> writeFunction) {
BinaryOperator<Throwable> onAttemptFailure =
(@Nullable Throwable previouslyChosenException, Throwable mostRecentAttemptException) ->
CommandOperationHelper.onRetryableWriteAttemptFailure(
operationContext, previouslyChosenException, mostRecentAttemptException);
return new RetryingAsyncCallbackSupplier<>(retryState, onAttemptFailure,
return new RetryingAsyncCallbackSupplier<>(retryState, onRetryableWriteAttemptFailure(operationContext),
this::shouldAttemptToRetryWrite, callback -> {
logRetryExecute(retryState, operationContext);
writeFunction.get(callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.bson.codecs.Decoder;

import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;

Expand All @@ -52,6 +51,8 @@
import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE;
import static com.mongodb.internal.operation.CommandOperationHelper.CommandCreator;
import static com.mongodb.internal.operation.CommandOperationHelper.logRetryExecute;
import static com.mongodb.internal.operation.CommandOperationHelper.onRetryableReadAttemptFailure;
import static com.mongodb.internal.operation.CommandOperationHelper.onRetryableWriteAttemptFailure;
import static com.mongodb.internal.operation.OperationHelper.ResourceSupplierInternalException;
import static com.mongodb.internal.operation.OperationHelper.canRetryRead;
import static com.mongodb.internal.operation.OperationHelper.canRetryWrite;
Expand Down Expand Up @@ -275,11 +276,7 @@ static <D, T> T createReadCommandAndExecute(

static <R> Supplier<R> decorateWriteWithRetries(final RetryState retryState,
final OperationContext operationContext, final Supplier<R> writeFunction) {
BinaryOperator<Throwable> onAttemptFailure =
(@Nullable Throwable previouslyChosenException, Throwable mostRecentAttemptException) ->
CommandOperationHelper.onRetryableWriteAttemptFailure(
operationContext, previouslyChosenException, mostRecentAttemptException);
return new RetryingSyncSupplier<>(retryState, onAttemptFailure,
return new RetryingSyncSupplier<>(retryState, onRetryableWriteAttemptFailure(operationContext),
CommandOperationHelper::shouldAttemptToRetryWrite, () -> {
logRetryExecute(retryState, operationContext);
return writeFunction.get();
Expand All @@ -288,11 +285,7 @@ static <R> Supplier<R> decorateWriteWithRetries(final RetryState retryState,

static <R> Supplier<R> decorateReadWithRetries(final RetryState retryState, final OperationContext operationContext,
final Supplier<R> readFunction) {
BinaryOperator<Throwable> onAttemptFailure =
(@Nullable Throwable previouslyChosenException, Throwable mostRecentAttemptException) ->
CommandOperationHelper.onRetryableReadAttemptFailure(
operationContext, previouslyChosenException, mostRecentAttemptException);
return new RetryingSyncSupplier<>(retryState, onAttemptFailure,
return new RetryingSyncSupplier<>(retryState, onRetryableReadAttemptFailure(operationContext),
CommandOperationHelper::shouldAttemptToRetryRead, () -> {
logRetryExecute(retryState, operationContext);
return readFunction.get();
Expand Down

0 comments on commit 69b78ff

Please sign in to comment.