|
17 | 17 |
|
18 | 18 | import com.mongodb.MongoOperationTimeoutException; |
19 | 19 | import com.mongodb.annotations.NotThreadSafe; |
20 | | -import com.mongodb.internal.TimeoutContext; |
21 | 20 | import com.mongodb.internal.async.SingleResultCallback; |
22 | 21 | import com.mongodb.internal.async.function.LoopState.AttachmentKey; |
23 | 22 | import com.mongodb.lang.NonNull; |
|
46 | 45 | */ |
47 | 46 | @NotThreadSafe |
48 | 47 | public final class RetryState { |
49 | | - public static final int RETRIES = 1; |
50 | | - public static final int INFINITE_ATTEMPTS = Integer.MAX_VALUE; |
| 48 | + public static final int MAX_RETRIES = 1; |
| 49 | + private static final int INFINITE_RETRIES = Integer.MAX_VALUE; |
51 | 50 |
|
52 | 51 | private final LoopState loopState; |
53 | 52 | private final int attempts; |
54 | | - private final boolean retryUntilTimeoutThrowsException; |
55 | 53 | @Nullable |
56 | 54 | private Throwable previouslyChosenException; |
57 | 55 |
|
58 | 56 | /** |
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. |
61 | | - * <p> |
62 | | - * If a timeout is not specified in the {@link TimeoutContext#hasTimeoutMS()}, the specified {@code retries} argument acts as a fallback |
63 | | - * bound. Otherwise, retries are unbounded until the timeout is reached. |
64 | | - * <p> |
65 | | - * 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 retries} and {@link TimeoutContext}. |
67 | | - * </p> |
68 | | - * |
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. |
72 | | - */ |
73 | | - public static RetryState withRetryableState(final int retries, final boolean retryUntilTimeoutThrowsException) { |
74 | | - assertTrue(retries > 0); |
75 | | - return new RetryState(retries, retryUntilTimeoutThrowsException); |
76 | | - } |
77 | | - |
78 | | - public static RetryState withNonRetryableState() { |
79 | | - return new RetryState(0, false); |
80 | | - } |
81 | | - |
82 | | - /** |
83 | | - * Creates a {@link RetryState} that does not limit the number of attempts. |
84 | | - * The number of attempts is limited iff {@link TimeoutContext#hasTimeoutMS()} is true and timeout has expired. |
85 | | - * <p> |
86 | | - * 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 {@link TimeoutContext}. |
88 | | - * </p> |
89 | | - * |
90 | | - * @param timeoutContext A timeout context that will be used to determine if the operation has timed out. |
| 57 | + * Creates a {@link RetryState} that does not explicitly limit the number of attempts. |
| 58 | + * Retrying still may be stopped because, for example, |
| 59 | + * the failed result from the most recent attempt is {@link MongoOperationTimeoutException}. |
91 | 60 | */ |
92 | | - public RetryState(final TimeoutContext timeoutContext) { |
93 | | - this(INFINITE_ATTEMPTS, timeoutContext.hasTimeoutMS()); |
| 61 | + public RetryState() { |
| 62 | + this(INFINITE_RETRIES); |
94 | 63 | } |
95 | 64 |
|
96 | 65 | /** |
97 | 66 | * @param retries A non-negative number of allowed retry attempts. |
98 | | - * {@link Integer#MAX_VALUE} is a special value interpreted as being unlimited. |
| 67 | + * {@value #INFINITE_RETRIES} is interpreted as {@linkplain #RetryState() absence of explicit limit}. |
99 | 68 | */ |
100 | | - private RetryState(final int retries, final boolean retryUntilTimeoutThrowsException) { |
| 69 | + public RetryState(final int retries) { |
101 | 70 | assertTrue(retries >= 0); |
102 | 71 | loopState = new LoopState(); |
103 | | - attempts = retries == INFINITE_ATTEMPTS ? INFINITE_ATTEMPTS : retries + 1; |
104 | | - this.retryUntilTimeoutThrowsException = retryUntilTimeoutThrowsException; |
| 72 | + attempts = retries == INFINITE_RETRIES ? INFINITE_RETRIES : retries + 1; |
105 | 73 | } |
106 | 74 |
|
107 | 75 | /** |
@@ -358,7 +326,7 @@ public boolean isFirstAttempt() { |
358 | 326 | * @see #attempt() |
359 | 327 | */ |
360 | 328 | private boolean isLastAttempt(final Throwable attemptException) { |
361 | | - boolean operationTimeout = retryUntilTimeoutThrowsException && attemptException instanceof MongoOperationTimeoutException; |
| 329 | + boolean operationTimeout = attemptException instanceof MongoOperationTimeoutException; |
362 | 330 | boolean attemptLimit = attempt() == attempts - 1; |
363 | 331 | return loopState.isLastIteration() || operationTimeout || attemptLimit; |
364 | 332 | } |
@@ -403,7 +371,7 @@ public <V> Optional<V> attachment(final AttachmentKey<V> key) { |
403 | 371 | public String toString() { |
404 | 372 | return "RetryState{" |
405 | 373 | + "loopState=" + loopState |
406 | | - + ", attempts=" + (attempts == INFINITE_ATTEMPTS ? "infinite" : attempts) |
| 374 | + + ", attempts=" + (attempts == INFINITE_RETRIES ? "infinite" : attempts) |
407 | 375 | + ", exception=" + previouslyChosenException |
408 | 376 | + '}'; |
409 | 377 | } |
|
0 commit comments