Skip to content

Commit cc087b0

Browse files
authored
Refactor MixedBulkWriteOperation to simplify retry logic there (#1989)
JAVA-6218
1 parent c707ffe commit cc087b0

18 files changed

Lines changed: 693 additions & 598 deletions

File tree

driver-core/src/main/com/mongodb/internal/async/AsyncRunnable.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@
111111
* <li>Is every c.complete followed by a return, to end execution?</li>
112112
* <li>Have all sync method calls been converted to async, where needed?</li>
113113
* </ol>
114-
*
115-
* <p>This class is not part of the public API and may be removed or changed
116-
* at any time
114+
* <p>
115+
* If, when writing a lambda expression, you need to have an effectively {@code final} variable
116+
* whose value may be mutated, use {@link MutableValue}.
117+
* <p>
118+
* This class is not part of the public API and may be removed or changed at any time.
117119
*/
118120
@FunctionalInterface
119121
public interface AsyncRunnable extends AsyncSupplier<Void>, AsyncConsumer<Void> {

driver-core/src/main/com/mongodb/internal/async/function/RetryState.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ private static Throwable callOnAttemptFailureOperator(
208208
* @param readOnlyRetryState Must not be mutated by this method.
209209
* @param onlyRuntimeExceptions See {@link #doAdvanceOrThrow(Throwable, BinaryOperator, BiPredicate, boolean)}.
210210
*/
211-
private boolean shouldRetry(final RetryState readOnlyRetryState, final Throwable attemptException, final Throwable newlyChosenException,
211+
private static boolean shouldRetry(final RetryState readOnlyRetryState, final Throwable attemptException,
212+
final Throwable newlyChosenException,
212213
final boolean onlyRuntimeExceptions, final BiPredicate<RetryState, Throwable> retryPredicate) {
213214
try {
214215
return retryPredicate.test(readOnlyRetryState, attemptException);
@@ -295,16 +296,6 @@ public boolean breakAndCompleteIfRetryAnd(final Supplier<Boolean> predicate, fin
295296
}
296297
}
297298

298-
/**
299-
* This method is similar to
300-
* {@link RetryState#breakAndThrowIfRetryAnd(Supplier)} / {@link RetryState#breakAndCompleteIfRetryAnd(Supplier, SingleResultCallback)}.
301-
* The difference is that it allows the current attempt to continue, yet no more attempts will happen. Also, unlike the aforementioned
302-
* methods, this method has effect even if called during the {@linkplain #isFirstAttempt() first attempt}.
303-
*/
304-
public void markAsLastAttempt() {
305-
loopState.markAsLastIteration();
306-
}
307-
308299
/**
309300
* Returns {@code true} iff the current attempt is the first one, i.e., no retry attempts have been made.
310301
*
@@ -318,7 +309,7 @@ public boolean isFirstAttempt() {
318309
* Returns {@code true} iff the current attempt is known to be the last one, i.e., it is known that no more attempts will be made.
319310
* An attempt is known to be the last one iff any of the following applies:
320311
* <ul>
321-
* <li>{@link #breakAndThrowIfRetryAnd(Supplier)} / {@link #breakAndCompleteIfRetryAnd(Supplier, SingleResultCallback)} / {@link #markAsLastAttempt()} was called.</li>
312+
* <li>{@link #breakAndThrowIfRetryAnd(Supplier)} / {@link #breakAndCompleteIfRetryAnd(Supplier, SingleResultCallback)} was called.</li>
322313
* <li>{@code attemptException} is a {@link MongoOperationTimeoutException}.</li>
323314
* <li>The number of attempts is limited, and the current attempt is the last one.</li>
324315
* </ul>

driver-core/src/main/com/mongodb/internal/operation/AsyncOperationHelper.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ static <R> void withAsyncSourceAndConnection(
110110
final boolean wrapConnectionSourceException,
111111
final OperationContext operationContext,
112112
final SingleResultCallback<R> callback,
113-
final AsyncCallbackTriFunction<AsyncConnectionSource, AsyncConnection, OperationContext, R> asyncFunction)
114-
throws OperationHelper.ResourceSupplierInternalException {
113+
final AsyncCallbackTriFunction<AsyncConnectionSource, AsyncConnection, OperationContext, R> asyncFunction) {
115114
SingleResultCallback<R> errorHandlingCallback = errorHandlingCallback(callback, OperationHelper.LOGGER);
116115

117116
OperationContext serverSelectionOperationContext =
@@ -140,8 +139,7 @@ static <R, T extends ReferenceCounted> void withAsyncSuppliedResource(final Asyn
140139
final boolean wrapSourceConnectionException,
141140
final OperationContext operationContext,
142141
final SingleResultCallback<R> callback,
143-
final AsyncCallbackFunction<T, R> function)
144-
throws OperationHelper.ResourceSupplierInternalException {
142+
final AsyncCallbackFunction<T, R> function) {
145143
SingleResultCallback<R> errorHandlingCallback = errorHandlingCallback(callback, OperationHelper.LOGGER);
146144
resourceSupplier.apply(operationContext, (resource, supplierException) -> {
147145
if (supplierException != null) {
@@ -331,7 +329,7 @@ static <D, T> void createReadCommandAndExecuteAsync(
331329

332330
static <R> AsyncCallbackSupplier<R> decorateReadWithRetriesAsync(final RetryState retryState, final OperationContext operationContext,
333331
final AsyncCallbackSupplier<R> asyncReadFunction) {
334-
return new RetryingAsyncCallbackSupplier<>(retryState, onRetryableReadAttemptFailure(operationContext),
332+
return new RetryingAsyncCallbackSupplier<>(retryState, onRetryableReadAttemptFailure(operationContext.getServerDeprioritization()),
335333
CommandOperationHelper::loggingShouldAttemptToRetryRead, callback -> {
336334
logRetryCommand(retryState, operationContext);
337335
asyncReadFunction.get(callback);
@@ -340,7 +338,7 @@ static <R> AsyncCallbackSupplier<R> decorateReadWithRetriesAsync(final RetryStat
340338

341339
static <R> AsyncCallbackSupplier<R> decorateWriteWithRetriesAsync(final RetryState retryState, final OperationContext operationContext,
342340
final AsyncCallbackSupplier<R> asyncWriteFunction) {
343-
return new RetryingAsyncCallbackSupplier<>(retryState, onRetryableWriteAttemptFailure(operationContext),
341+
return new RetryingAsyncCallbackSupplier<>(retryState, onRetryableWriteAttemptFailure(operationContext.getServerDeprioritization()),
344342
CommandOperationHelper::loggingShouldAttemptToRetryWriteAndAddRetryableLabel, callback -> {
345343
logRetryCommand(retryState, operationContext);
346344
asyncWriteFunction.get(callback);

0 commit comments

Comments
 (0)