Package org.broadinstitute.http.nio
Class RetryHandler
java.lang.Object
org.broadinstitute.http.nio.RetryHandler
Simple counter class to keep track of retry and reopen attempts when StorageExceptions are
encountered. Handles sleeping between retry/reopen attempts, as well as throwing an exception
when all retries/reopens are exhausted.
methods may be run and retried by running them through one of the
runWithRetries(IORunnable)
methods-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
invalid input: '{@linkplain /*missing*/}' equivalent which can throw IOExceptionstatic interface
Supplier equivalent which can throw IOException -
Field Summary
FieldsModifier and TypeFieldDescriptionthe default set of exception messages which are retried when encountereddefault set of exception types which will be retried when encountereddefault set of HTTP codes which will be retriedprivate static final org.slf4j.Logger
private final int
private final URI
-
Constructor Summary
ConstructorsConstructorDescriptionRetryHandler
(int maxRetries, Collection<Integer> retryableHttpCodes, Collection<Class<? extends Exception>> retryableExceptions, Collection<String> retryableMessages, Predicate<Throwable> retryPredicate, URI uri) Create a CloudStorageRetryHandler with the maximum retries and reopens set to different values.RetryHandler
(HttpFileSystemProviderSettings.RetrySettings settings, URI uri) -
Method Summary
Modifier and TypeMethodDescriptionint
boolean
isRetryable
(Exception exs) void
A function to run and potentially retry if an error occurs and meets the retry criteria Note that functions may be run repeatedly so any state which is changed during an unsuccessful attempt must not poison the class.<T> T
runWithRetries
(RetryHandler.IOSupplier<T> toRun) A function to run and potentially retry if an error occurs and meets the retry criteria Note that functions may be run repeatedly so any state which is changed during an unsuccessful attempt must not poison the class.private <T> T
runWithRetries
(RetryHandler.IOSupplier<T> toRun, IOException previousError) private static Duration
sleepBeforeNextAttempt
(int attempt) <T> T
tryOnceThenWithRetries
(RetryHandler.IOSupplier<T> runFirst, RetryHandler.IOSupplier<T> thenRunAndRetry) First attempt the runFirst function.
-
Field Details
-
DEFALT_RETRYABLE_MESSAGES
the default set of exception messages which are retried when encountered -
DEFAULT_RETRYABLE_EXCEPTIONS
default set of exception types which will be retried when encountered -
DEFAULT_RETRYABLE_HTTP_CODES
default set of HTTP codes which will be retried -
LOGGER
private static final org.slf4j.Logger LOGGER -
maxRetries
private final int maxRetries -
retryableHttpCodes
-
retryableMessages
-
retryableExceptions
-
customRetryPredicate
-
uri
-
-
Constructor Details
-
RetryHandler
- Parameters:
settings
- to configure the retry mechanismuri
- which URI is being queried, used in error messages
-
RetryHandler
public RetryHandler(int maxRetries, Collection<Integer> retryableHttpCodes, Collection<Class<? extends Exception>> retryableExceptions, Collection<String> retryableMessages, Predicate<Throwable> retryPredicate, URI uri) Create a CloudStorageRetryHandler with the maximum retries and reopens set to different values.- Parameters:
maxRetries
- maximum number of retries, 0 means nothing will be retriedretryableHttpCodes
- HTTP codes that are retryableretryableExceptions
- exception classes to retry when encounteredretryableMessages
- strings which will be matched against the exception messagesretryPredicate
- predicate to determine if an exception is retryableuri
- URI which is being retried, used in the error messages
-
-
Method Details
-
getMaxRetries
public int getMaxRetries()- Returns:
- the maximum number of retries before giving up
-
runWithRetries
A function to run and potentially retry if an error occurs and meets the retry criteria Note that functions may be run repeatedly so any state which is changed during an unsuccessful attempt must not poison the class. Functions must either clean up in a finally block or reset state entirely each time.- Parameters:
toRun
- the function to run- Throws:
IOException
- when the function throws an IOException and either it is unretryable or retries are exhausted in the case of a retryable error which is not retried this will be anOutOfRetriesException
-
runWithRetries
A function to run and potentially retry if an error occurs and meets the retry criteria Note that functions may be run repeatedly so any state which is changed during an unsuccessful attempt must not poison the class. Functions must either clean up in a finally block or reset state entirely each time.- Type Parameters:
T
- the type of the value returned by toRun- Parameters:
toRun
- the function to run- Returns:
- the value supplied by succesful completion of toRun
- Throws:
IOException
- when the function throws an IOException and either it is unretryable or retries are exhausted in the case of a retryable error which is not retried this will be anOutOfRetriesException
-
runWithRetries
private <T> T runWithRetries(RetryHandler.IOSupplier<T> toRun, IOException previousError) throws IOException - Throws:
IOException
-
tryOnceThenWithRetries
public <T> T tryOnceThenWithRetries(RetryHandler.IOSupplier<T> runFirst, RetryHandler.IOSupplier<T> thenRunAndRetry) throws IOException First attempt the runFirst function. If that fails and the error is retryable, retry using the thenRunAndRetry function. This is useful when there is a potentially efficient way to do something by taking advantage of the existing state of things, but in the case that that fails itt leaves a messy state and has to be cleaned up before trying again.- Type Parameters:
T
- value to be returned- Parameters:
runFirst
- first way of obtaining the valuethenRunAndRetry
- second method which must safely retriable- Returns:
- the value of the first non failing function call
- Throws:
IOException
- when an is thrown in the payload functions, and it's either not retryable or retries are exhausted.
-
sleepBeforeNextAttempt
- Parameters:
attempt
- attempt number, used to determine the wait time- Returns:
- the actual amount of time this slept for
-
isRetryable
- Parameters:
exs
- Exception to test- Returns:
- true if exs is a retryable error, otherwise false
-