Skip to content

Commit c726922

Browse files
committed
Refactoring: remove the unused RetryState.attempts method, update docs
JAVA-5956, JAVA-6117, JAVA-6113, JAVA-6119, JAVA-6141
1 parent 9b788cf commit c726922

2 files changed

Lines changed: 25 additions & 46 deletions

File tree

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

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ public final class RetryState {
5656
private Throwable previouslyChosenException;
5757

5858
/**
59-
* Creates a {@code RetryState} with a positive number of allowed retries. {@link Integer#MAX_VALUE} is a special value interpreted as
60-
* being unlimited.
59+
* Creates a {@code RetryState} with a positive number of allowed retry attempts.
60+
* {@link Integer#MAX_VALUE} is a special value interpreted as being unlimited.
6161
* <p>
62-
* If a timeout is not specified in the {@link TimeoutContext#hasTimeoutMS()}, the specified {@code retries} param acts as a fallback
62+
* If a timeout is not specified in the {@link TimeoutContext#hasTimeoutMS()}, the specified {@code retries} argument acts as a fallback
6363
* bound. Otherwise, retries are unbounded until the timeout is reached.
6464
* <p>
6565
* It is possible to provide an additional {@code retryPredicate} in the {@link #doAdvanceOrThrow} method,
66-
* which can be used to stop retrying based on a custom condition additionally to {@code retires} and {@link TimeoutContext}.
66+
* which can be used to stop retrying based on a custom condition additionally to {@code retries} and {@link TimeoutContext}.
6767
* </p>
6868
*
69-
* @param retries A positive number of allowed retries. {@link Integer#MAX_VALUE} is a special value interpreted as being unlimited.
70-
* @param retryUntilTimeoutThrowsException If {@code true}, then if a {@link MongoOperationTimeoutException} is throws then retrying stops.
71-
* @see #attempts()
69+
* @param retries A positive number of allowed retry attempts.
70+
* {@link Integer#MAX_VALUE} is a special value interpreted as being unlimited.
71+
* @param retryUntilTimeoutThrowsException If {@code true}, then if a {@link MongoOperationTimeoutException} is thrown then retrying stops.
7272
*/
7373
public static RetryState withRetryableState(final int retries, final boolean retryUntilTimeoutThrowsException) {
7474
assertTrue(retries > 0);
@@ -80,23 +80,22 @@ public static RetryState withNonRetryableState() {
8080
}
8181

8282
/**
83-
* Creates a {@link RetryState} that does not limit the number of retries.
83+
* Creates a {@link RetryState} that does not limit the number of attempts.
8484
* The number of attempts is limited iff {@link TimeoutContext#hasTimeoutMS()} is true and timeout has expired.
8585
* <p>
8686
* It is possible to provide an additional {@code retryPredicate} in the {@link #doAdvanceOrThrow} method,
87-
* which can be used to stop retrying based on a custom condition additionally to {@code retires} and {@link TimeoutContext}.
87+
* which can be used to stop retrying based on a custom condition additionally to {@link TimeoutContext}.
8888
* </p>
8989
*
9090
* @param timeoutContext A timeout context that will be used to determine if the operation has timed out.
91-
* @see #attempts()
9291
*/
9392
public RetryState(final TimeoutContext timeoutContext) {
9493
this(INFINITE_ATTEMPTS, timeoutContext.hasTimeoutMS());
9594
}
9695

9796
/**
98-
* @param retries A non-negative number of allowed retries. {@link Integer#MAX_VALUE} is a special value interpreted as being unlimited.
99-
* @see #attempts()
97+
* @param retries A non-negative number of allowed retry attempts.
98+
* {@link Integer#MAX_VALUE} is a special value interpreted as being unlimited.
10099
*/
101100
private RetryState(final int retries, final boolean retryUntilTimeoutThrowsException) {
102101
assertTrue(retries >= 0);
@@ -107,7 +106,7 @@ private RetryState(final int retries, final boolean retryUntilTimeoutThrowsExcep
107106

108107
/**
109108
* Advances this {@link RetryState} such that it represents the state of a new attempt.
110-
* If there is at least one more {@linkplain #attempts() attempt} left, it is consumed by this method.
109+
* If there is at least one more attempt left, it is consumed by this method.
111110
* Must not be called before the {@linkplain #isFirstAttempt() first attempt}, must be called before each subsequent attempt.
112111
* <p>
113112
* This method is intended to be used by code that generally does not handle {@link Error}s explicitly,
@@ -353,58 +352,43 @@ public void markAsLastAttempt() {
353352
}
354353

355354
/**
356-
* Returns {@code true} iff the current attempt is the first one, i.e., no retries have been made.
355+
* Returns {@code true} iff the current attempt is the first one, i.e., no retry attempts have been made.
357356
*
358-
* @see #attempts()
357+
* @see #attempt()
359358
*/
360359
public boolean isFirstAttempt() {
361360
return loopState.isFirstIteration();
362361
}
363362

364363
/**
365-
* Returns {@code true} iff the current attempt is known to be the last one, i.e., it is known that no more retries will be made.
364+
* 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.
366365
* An attempt is known to be the last one iff any of the following applies:
367366
* <ul>
368367
* <li>{@link #breakAndThrowIfRetryAnd(Supplier)} / {@link #breakAndCompleteIfRetryAnd(Supplier, SingleResultCallback)} / {@link #markAsLastAttempt()} was called.</li>
369368
* <li>A timeout is set and has been reached.</li>
370-
* <li>No timeout is set, and the number of {@linkplain #attempts() attempts} is limited, and the current attempt is the last one.</li>
369+
* <li>No timeout is set, and the number of attempts is limited, and the current attempt is the last one.</li>
371370
* </ul>
372371
*
373-
* @see #attempts()
372+
* @see #attempt()
374373
*/
375374
public boolean isLastAttempt() {
376-
if (loopState.isLastIteration()){
375+
if (loopState.isLastIteration()) {
377376
return true;
378377
}
379378
if (retryUntilTimeoutThrowsException) {
380-
return false;
379+
return false;
381380
}
382381
return attempt() == attempts - 1;
383382
}
384383

385384
/**
386385
* A 0-based attempt number.
387386
*
388-
* @see #attempts()
389-
*/
390-
public int attempt() {
391-
return loopState.iteration();
392-
}
393-
394-
/**
395-
* Returns a positive maximum number of attempts:
396-
* <ul>
397-
* <li>0 if the number of retries is {@linkplain #RetryState(TimeoutContext) unlimited};</li>
398-
* <li>1 if no retries are allowed;</li>
399-
* <li>{@link #RetryState(int, boolean) retries} + 1 otherwise.</li>
400-
* </ul>
401-
*
402-
* @see #attempt()
403387
* @see #isFirstAttempt()
404388
* @see #isLastAttempt()
405389
*/
406-
public int attempts() {
407-
return attempts == INFINITE_ATTEMPTS ? 0 : attempts;
390+
public int attempt() {
391+
return loopState.iteration();
408392
}
409393

410394
/**

driver-core/src/test/unit/com/mongodb/internal/async/function/RetryStateTest.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,19 @@ void unlimitedAttemptsAndAdvance(final TimeoutContext timeoutContext) {
7777
assertAll(
7878
() -> assertTrue(retryState.isFirstAttempt()),
7979
() -> assertEquals(0, retryState.attempt()),
80-
() -> assertFalse(retryState.isLastAttempt()),
81-
() -> assertEquals(0, retryState.attempts())
80+
() -> assertFalse(retryState.isLastAttempt())
8281
);
8382
advance(retryState);
8483
assertAll(
8584
() -> assertFalse(retryState.isFirstAttempt()),
8685
() -> assertEquals(1, retryState.attempt()),
87-
() -> assertFalse(retryState.isLastAttempt()),
88-
() -> assertEquals(0, retryState.attempts())
86+
() -> assertFalse(retryState.isLastAttempt())
8987
);
9088
retryState.markAsLastAttempt();
9189
assertAll(
9290
() -> assertFalse(retryState.isFirstAttempt()),
9391
() -> assertEquals(1, retryState.attempt()),
94-
() -> assertTrue(retryState.isLastAttempt()),
95-
() -> assertEquals(0, retryState.attempts())
92+
() -> assertTrue(retryState.isLastAttempt())
9693
);
9794
}
9895

@@ -105,14 +102,12 @@ void limitedAttemptsAndAdvance() {
105102
() -> assertTrue(retryState.isFirstAttempt()),
106103
() -> assertEquals(0, retryState.attempt()),
107104
() -> assertTrue(retryState.isLastAttempt()),
108-
() -> assertEquals(1, retryState.attempts()),
109105
() -> assertThrows(attemptException.getClass(), () ->
110106
retryState.advanceOrThrow(attemptException, (e1, e2) -> e2, (rs, e) -> true)),
111107
// when there is only one attempt, it is both the first and the last one
112108
() -> assertTrue(retryState.isFirstAttempt()),
113109
() -> assertEquals(0, retryState.attempt()),
114-
() -> assertTrue(retryState.isLastAttempt()),
115-
() -> assertEquals(1, retryState.attempts())
110+
() -> assertTrue(retryState.isLastAttempt())
116111
);
117112
}
118113

0 commit comments

Comments
 (0)