Skip to content

Commit 8b1804d

Browse files
committed
Make RetryState.isLastAttempt private, and simplify the code
JAVA-5956, JAVA-6117, JAVA-6113, JAVA-6119, JAVA-6141
1 parent c0f9627 commit 8b1804d

3 files changed

Lines changed: 137 additions & 148 deletions

File tree

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

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ private RetryState(final int retries, final boolean retryUntilTimeoutThrowsExcep
132132
* per attempt and only if all the following is true:
133133
* <ul>
134134
* <li>{@code onAttemptFailureOperator} completed normally;</li>
135-
* <li>the most recent attempt is not the {@linkplain #isLastAttempt() last} one.</li>
135+
* <li>the most recent attempt is not known to be the {@linkplain #isLastAttempt(Throwable) last} one.</li>
136136
* </ul>
137137
* The {@code retryPredicate} accepts this {@link RetryState} and the exception from the most recent attempt,
138138
* and may mutate the exception. The {@linkplain RetryState} advances to represent the state of a new attempt
139139
* after (in the happens-before order) testing the {@code retryPredicate}, and only if the predicate completes normally.
140140
* @throws RuntimeException Iff any of the following is true:
141141
* <ul>
142142
* <li>the {@code onAttemptFailureOperator} completed abruptly;</li>
143-
* <li>the most recent attempt is the {@linkplain #isLastAttempt() last} one;</li>
143+
* <li>the most recent attempt is known to be the {@linkplain #isLastAttempt(Throwable) last} one;</li>
144144
* <li>the {@code retryPredicate} completed abruptly;</li>
145145
* <li>the {@code retryPredicate} is {@code false}.</li>
146146
* </ul>
@@ -187,24 +187,10 @@ private void doAdvanceOrThrow(final Throwable attemptException,
187187
}
188188
assertTrue(!isFirstAttempt() || previouslyChosenException == null);
189189
Throwable newlyChosenException = callOnAttemptFailureOperator(previouslyChosenException, attemptException, onlyRuntimeExceptions, onAttemptFailureOperator);
190-
191-
/*
192-
* A MongoOperationTimeoutException indicates that the operation timed out, either during command execution or server selection.
193-
* The timeout for server selection is determined by the computedServerSelectionMS = min(serverSelectionTimeoutMS, timeoutMS).
194-
*
195-
* It is important to check if the exception is an instance of MongoOperationTimeoutException to detect a timeout.
196-
*/
197-
if (isLastAttempt() || attemptException instanceof MongoOperationTimeoutException) {
190+
if (isLastAttempt(attemptException)) {
198191
previouslyChosenException = newlyChosenException;
199-
/*
200-
* The function of isLastIteration() is to indicate if retrying has
201-
* been explicitly halted. Such a stop is not interpreted as
202-
* a timeout exception but as a deliberate cessation of retry attempts.
203-
*/
204-
if (retryUntilTimeoutThrowsException && !loopState.isLastIteration()) {
205-
previouslyChosenException = createMongoTimeoutException(
206-
"Retry attempt exceeded the timeout limit.",
207-
previouslyChosenException);
192+
if (attemptException instanceof MongoOperationTimeoutException) {
193+
previouslyChosenException = createMongoTimeoutException("Retry attempt exceeded the timeout limit.", previouslyChosenException);
208194
}
209195
throw previouslyChosenException;
210196
} else {
@@ -365,27 +351,23 @@ public boolean isFirstAttempt() {
365351
* An attempt is known to be the last one iff any of the following applies:
366352
* <ul>
367353
* <li>{@link #breakAndThrowIfRetryAnd(Supplier)} / {@link #breakAndCompleteIfRetryAnd(Supplier, SingleResultCallback)} / {@link #markAsLastAttempt()} was called.</li>
368-
* <li>A timeout is set and has been reached.</li>
354+
* <li>A timeout is set and has been reached, as indicated by {@code attemptException}.</li>
369355
* <li>No timeout is set, and the number of attempts is limited, and the current attempt is the last one.</li>
370356
* </ul>
371357
*
372358
* @see #attempt()
373359
*/
374-
public boolean isLastAttempt() {
375-
if (loopState.isLastIteration()) {
376-
return true;
377-
}
378-
if (retryUntilTimeoutThrowsException) {
379-
return false;
380-
}
381-
return attempt() == attempts - 1;
360+
private boolean isLastAttempt(final Throwable attemptException) {
361+
boolean lastIteration = loopState.isLastIteration();
362+
boolean operationTimeout = retryUntilTimeoutThrowsException && attemptException instanceof MongoOperationTimeoutException;
363+
assertFalse(lastIteration && operationTimeout);
364+
return lastIteration || operationTimeout || attempt() == attempts - 1;
382365
}
383366

384367
/**
385368
* A 0-based attempt number.
386369
*
387370
* @see #isFirstAttempt()
388-
* @see #isLastAttempt()
389371
*/
390372
public int attempt() {
391373
return loopState.iteration();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public final class RetryingAsyncCallbackSupplier<R> implements AsyncCallbackSupp
6969
* per attempt and only if all the following is true:
7070
* <ul>
7171
* <li>{@code onAttemptFailureOperator} completed normally;</li>
72-
* <li>the most recent attempt is not the {@linkplain RetryState#isLastAttempt() last} one.</li>
72+
* <li>the most recent attempt is not known to be the last one.</li>
7373
* </ul>
7474
* The {@code retryPredicate} accepts this {@link RetryState} and the exception from the most recent attempt,
7575
* and may mutate the exception. The {@linkplain RetryState} advances to represent the state of a new attempt

0 commit comments

Comments
 (0)