public class CompletableFutureRetryer extends Object
CompletableFuture
Constructor and Description |
---|
CompletableFutureRetryer(ThreadPoolExecutor executor,
Duration waitBetween,
org.apache.kafka.common.utils.Time time)
Util to help run retry
Currently, it only supports fixed-wait-time retry strategy
todo: need to enhance to support other retry strategy in the future.
|
Modifier and Type | Method and Description |
---|---|
<T> CompletableFuture<T> |
withRetries(Supplier<CompletableFuture<T>> attempter,
Predicate<Throwable> shouldRetry,
int attempts)
Compose a
CompletableFuture using the attempter to create the first
attempt and any retries permitted by the shouldRetry predicate. |
<T> CompletableFuture<T> |
withTimedRetries(Supplier<CompletableFuture<T>> attempter,
Predicate<Throwable> shouldRetry,
int attempts,
Consumer<Long> runTimeReporter,
Consumer<Long> numFailuresReporter)
Compose a
CompletableFuture using the attempter to create the first
attempt and any retries permitted by the shouldRetry predicate. |
public CompletableFutureRetryer(ThreadPoolExecutor executor, Duration waitBetween, org.apache.kafka.common.utils.Time time)
executor
- the executor to run the taskswaitBetween
- the duration of waiting between attemptstime
- Time object for tracking runtime of completable futures (counting retries)public <T> CompletableFuture<T> withRetries(Supplier<CompletableFuture<T>> attempter, Predicate<Throwable> shouldRetry, int attempts)
CompletableFuture
using the attempter
to create the first
attempt and any retries permitted by the shouldRetry
predicate. All retries wait
for the waitBetween
before going again, up to a maximum number of attemptsT
- the type of value the future will returnattempter
- produce an attempt as a CompletableFuture
shouldRetry
- determines whether a Throwable
is retryableattempts
- the number of attempts to make before allowing failureCompletableFuture
that runs until success or total failurepublic <T> CompletableFuture<T> withTimedRetries(Supplier<CompletableFuture<T>> attempter, Predicate<Throwable> shouldRetry, int attempts, Consumer<Long> runTimeReporter, Consumer<Long> numFailuresReporter)
CompletableFuture
using the attempter
to create the first
attempt and any retries permitted by the shouldRetry
predicate. All retries wait
or the waitBetween
before going again, up to a maximum number of attempts. Also reports total
run time until success (counting retries) using runTimeReporter
(does not report if ultimate result
is failure, or if runTimeReporter
is null).T
- the type of value the future will returnattempter
- produce an attempt as a CompletableFuture
shouldRetry
- determines whether a Throwable
is retryableattempts
- the number of attempts to make before allowing failurerunTimeReporter
- consumer function to report the total run time counting retries. Does not report if final
result is failurenumFailuresReporter
- consumer function to report the total failures.CompletableFuture
that runs until success or total failure