-
Notifications
You must be signed in to change notification settings - Fork 272
/
Retryer.java
321 lines (286 loc) · 13.1 KB
/
Retryer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright 2012-2015 Ray Holder
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.rholder.retry;
import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/**
* A retryer, which executes a call, and retries it until it succeeds, or
* a stop strategy decides to stop retrying. A wait strategy is used to sleep
* between attempts. The strategy to decide if the call succeeds or not is
* also configurable.
* <p></p>
* A retryer can also wrap the callable into a RetryerCallable, which can be submitted to an executor.
* <p></p>
* Retryer instances are better constructed with a {@link RetryerBuilder}. A retryer
* is thread-safe, provided the arguments passed to its constructor are thread-safe.
*
* @param <V> the type of the call return value
* @author JB
* @author Jason Dunkelberger (dirkraft)
*/
public final class Retryer<V> {
private final StopStrategy stopStrategy;
private final WaitStrategy waitStrategy;
private final BlockStrategy blockStrategy;
private final AttemptTimeLimiter<V> attemptTimeLimiter;
private final Predicate<Attempt<V>> rejectionPredicate;
private final Collection<RetryListener> listeners;
/**
* Constructor
*
* @param stopStrategy the strategy used to decide when the retryer must stop retrying
* @param waitStrategy the strategy used to decide how much time to sleep between attempts
* @param rejectionPredicate the predicate used to decide if the attempt must be rejected
* or not. If an attempt is rejected, the retryer will retry the call, unless the stop
* strategy indicates otherwise or the thread is interrupted.
*/
public Retryer(@Nonnull StopStrategy stopStrategy,
@Nonnull WaitStrategy waitStrategy,
@Nonnull Predicate<Attempt<V>> rejectionPredicate) {
this(AttemptTimeLimiters.<V>noTimeLimit(), stopStrategy, waitStrategy, BlockStrategies.threadSleepStrategy(), rejectionPredicate);
}
/**
* Constructor
*
* @param attemptTimeLimiter to prevent from any single attempt from spinning infinitely
* @param stopStrategy the strategy used to decide when the retryer must stop retrying
* @param waitStrategy the strategy used to decide how much time to sleep between attempts
* @param rejectionPredicate the predicate used to decide if the attempt must be rejected
* or not. If an attempt is rejected, the retryer will retry the call, unless the stop
* strategy indicates otherwise or the thread is interrupted.
*/
public Retryer(@Nonnull AttemptTimeLimiter<V> attemptTimeLimiter,
@Nonnull StopStrategy stopStrategy,
@Nonnull WaitStrategy waitStrategy,
@Nonnull Predicate<Attempt<V>> rejectionPredicate) {
this(attemptTimeLimiter, stopStrategy, waitStrategy, BlockStrategies.threadSleepStrategy(), rejectionPredicate);
}
/**
* Constructor
*
* @param attemptTimeLimiter to prevent from any single attempt from spinning infinitely
* @param stopStrategy the strategy used to decide when the retryer must stop retrying
* @param waitStrategy the strategy used to decide how much time to sleep between attempts
* @param blockStrategy the strategy used to decide how to block between retry attempts; eg, Thread#sleep(), latches, etc.
* @param rejectionPredicate the predicate used to decide if the attempt must be rejected
* or not. If an attempt is rejected, the retryer will retry the call, unless the stop
* strategy indicates otherwise or the thread is interrupted.
*/
public Retryer(@Nonnull AttemptTimeLimiter<V> attemptTimeLimiter,
@Nonnull StopStrategy stopStrategy,
@Nonnull WaitStrategy waitStrategy,
@Nonnull BlockStrategy blockStrategy,
@Nonnull Predicate<Attempt<V>> rejectionPredicate) {
this(attemptTimeLimiter, stopStrategy, waitStrategy, blockStrategy, rejectionPredicate, new ArrayList<RetryListener>());
}
/**
* Constructor
*
* @param attemptTimeLimiter to prevent from any single attempt from spinning infinitely
* @param stopStrategy the strategy used to decide when the retryer must stop retrying
* @param waitStrategy the strategy used to decide how much time to sleep between attempts
* @param blockStrategy the strategy used to decide how to block between retry attempts; eg, Thread#sleep(), latches, etc.
* @param rejectionPredicate the predicate used to decide if the attempt must be rejected
* or not. If an attempt is rejected, the retryer will retry the call, unless the stop
* strategy indicates otherwise or the thread is interrupted.
* @param listeners collection of retry listeners
*/
@Beta
public Retryer(@Nonnull AttemptTimeLimiter<V> attemptTimeLimiter,
@Nonnull StopStrategy stopStrategy,
@Nonnull WaitStrategy waitStrategy,
@Nonnull BlockStrategy blockStrategy,
@Nonnull Predicate<Attempt<V>> rejectionPredicate,
@Nonnull Collection<RetryListener> listeners) {
Preconditions.checkNotNull(attemptTimeLimiter, "timeLimiter may not be null");
Preconditions.checkNotNull(stopStrategy, "stopStrategy may not be null");
Preconditions.checkNotNull(waitStrategy, "waitStrategy may not be null");
Preconditions.checkNotNull(blockStrategy, "blockStrategy may not be null");
Preconditions.checkNotNull(rejectionPredicate, "rejectionPredicate may not be null");
Preconditions.checkNotNull(listeners, "listeners may not null");
this.attemptTimeLimiter = attemptTimeLimiter;
this.stopStrategy = stopStrategy;
this.waitStrategy = waitStrategy;
this.blockStrategy = blockStrategy;
this.rejectionPredicate = rejectionPredicate;
this.listeners = listeners;
}
/**
* Executes the given callable. If the rejection predicate
* accepts the attempt, the stop strategy is used to decide if a new attempt
* must be made. Then the wait strategy is used to decide how much time to sleep
* and a new attempt is made.
*
* @param callable the callable task to be executed
* @return the computed result of the given callable
* @throws ExecutionException if the given callable throws an exception, and the
* rejection predicate considers the attempt as successful. The original exception
* is wrapped into an ExecutionException.
* @throws RetryException if all the attempts failed before the stop strategy decided
* to abort, or the thread was interrupted. Note that if the thread is interrupted,
* this exception is thrown and the thread's interrupt status is set.
*/
public V call(Callable<V> callable) throws ExecutionException, RetryException {
long startTime = System.nanoTime();
for (int attemptNumber = 1; ; attemptNumber++) {
Attempt<V> attempt;
try {
V result = attemptTimeLimiter.call(callable);
attempt = new ResultAttempt<V>(result, attemptNumber, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
} catch (Throwable t) {
attempt = new ExceptionAttempt<V>(t, attemptNumber, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
}
for (RetryListener listener : listeners) {
listener.onRetry(attempt);
}
if (!rejectionPredicate.apply(attempt)) {
return attempt.get();
}
if (stopStrategy.shouldStop(attempt)) {
throw new RetryException(attemptNumber, attempt);
} else {
long sleepTime = waitStrategy.computeSleepTime(attempt);
try {
blockStrategy.block(sleepTime);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RetryException(attemptNumber, attempt);
}
}
}
}
/**
* Wraps the given {@link Callable} in a {@link RetryerCallable}, which can
* be submitted to an executor. The returned {@link RetryerCallable} uses
* this {@link Retryer} instance to call the given {@link Callable}.
*
* @param callable the callable to wrap
* @return a {@link RetryerCallable} that behaves like the given {@link Callable} with retry behavior defined by this {@link Retryer}
*/
public RetryerCallable<V> wrap(Callable<V> callable) {
return new RetryerCallable<V>(this, callable);
}
@Immutable
static final class ResultAttempt<R> implements Attempt<R> {
private final R result;
private final long attemptNumber;
private final long delaySinceFirstAttempt;
public ResultAttempt(R result, long attemptNumber, long delaySinceFirstAttempt) {
this.result = result;
this.attemptNumber = attemptNumber;
this.delaySinceFirstAttempt = delaySinceFirstAttempt;
}
@Override
public R get() throws ExecutionException {
return result;
}
@Override
public boolean hasResult() {
return true;
}
@Override
public boolean hasException() {
return false;
}
@Override
public R getResult() throws IllegalStateException {
return result;
}
@Override
public Throwable getExceptionCause() throws IllegalStateException {
throw new IllegalStateException("The attempt resulted in a result, not in an exception");
}
@Override
public long getAttemptNumber() {
return attemptNumber;
}
@Override
public long getDelaySinceFirstAttempt() {
return delaySinceFirstAttempt;
}
}
@Immutable
static final class ExceptionAttempt<R> implements Attempt<R> {
private final ExecutionException e;
private final long attemptNumber;
private final long delaySinceFirstAttempt;
public ExceptionAttempt(Throwable cause, long attemptNumber, long delaySinceFirstAttempt) {
this.e = new ExecutionException(cause);
this.attemptNumber = attemptNumber;
this.delaySinceFirstAttempt = delaySinceFirstAttempt;
}
@Override
public R get() throws ExecutionException {
throw e;
}
@Override
public boolean hasResult() {
return false;
}
@Override
public boolean hasException() {
return true;
}
@Override
public R getResult() throws IllegalStateException {
throw new IllegalStateException("The attempt resulted in an exception, not in a result");
}
@Override
public Throwable getExceptionCause() throws IllegalStateException {
return e.getCause();
}
@Override
public long getAttemptNumber() {
return attemptNumber;
}
@Override
public long getDelaySinceFirstAttempt() {
return delaySinceFirstAttempt;
}
}
/**
* A {@link Callable} which wraps another {@link Callable} in order to add
* retrying behavior from a given {@link Retryer} instance.
*
* @author JB
*/
public static class RetryerCallable<X> implements Callable<X> {
private Retryer<X> retryer;
private Callable<X> callable;
private RetryerCallable(Retryer<X> retryer,
Callable<X> callable) {
this.retryer = retryer;
this.callable = callable;
}
/**
* Makes the enclosing retryer call the wrapped callable.
*
* @see Retryer#call(Callable)
*/
@Override
public X call() throws ExecutionException, RetryException {
return retryer.call(callable);
}
}
}