Class RetryHandler

java.lang.Object
org.broadinstitute.http.nio.RetryHandler

public class RetryHandler extends Object
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
  • Field Details

    • DEFALT_RETRYABLE_MESSAGES

      public static final Set<String> DEFALT_RETRYABLE_MESSAGES
      the default set of exception messages which are retried when encountered
    • DEFAULT_RETRYABLE_EXCEPTIONS

      public static final Set<Class<? extends Exception>> DEFAULT_RETRYABLE_EXCEPTIONS
      default set of exception types which will be retried when encountered
    • DEFAULT_RETRYABLE_HTTP_CODES

      public static final Set<Integer> 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

      private final Set<Integer> retryableHttpCodes
    • retryableMessages

      private final Set<String> retryableMessages
    • retryableExceptions

      private final Set<Class<? extends Exception>> retryableExceptions
    • customRetryPredicate

      private final Predicate<Throwable> customRetryPredicate
    • uri

      private final URI uri
  • Constructor Details

    • RetryHandler

      public RetryHandler(HttpFileSystemProviderSettings.RetrySettings settings, URI uri)
      Parameters:
      settings - to configure the retry mechanism
      uri - 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 retried
      retryableHttpCodes - HTTP codes that are retryable
      retryableExceptions - exception classes to retry when encountered
      retryableMessages - strings which will be matched against the exception messages
      retryPredicate - predicate to determine if an exception is retryable
      uri - 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

      public void runWithRetries(RetryHandler.IORunnable toRun) throws IOException
      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 an OutOfRetriesException
    • runWithRetries

      public <T> T runWithRetries(RetryHandler.IOSupplier<T> toRun) throws IOException
      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 an OutOfRetriesException
    • 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 value
      thenRunAndRetry - 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

      private static Duration sleepBeforeNextAttempt(int attempt)
      Parameters:
      attempt - attempt number, used to determine the wait time
      Returns:
      the actual amount of time this slept for
    • isRetryable

      public boolean isRetryable(Exception exs)
      Parameters:
      exs - Exception to test
      Returns:
      true if exs is a retryable error, otherwise false