Class Retry

java.lang.Object
com.helixframework.retry.Retry

public final class Retry extends Object
Wrapper function that retries, with exponential backoff and jitter, in the event of an exception.
  • Field Details

    • DEFAULT_MAX_RETRIES

      public static final int DEFAULT_MAX_RETRIES
      Default number of retries attempted by this function before throwing RetriesExhaustedException.
      See Also:
    • DEFAULT_INITIAL_BACKOFF

      public static final Long DEFAULT_INITIAL_BACKOFF
      Default number of milliseconds to initially wait for the retry backoff.
  • Method Details

    • execute

      public static <T> T execute(RetryableFunction<T> fn) throws RetriesExhaustedException
      Executes the wrapped function with default maximum number of retries in the event of an exception.
      Type Parameters:
      T - return type of the wrapped function
      Parameters:
      fn - wrapped function to execute
      Returns:
      result of the wrapped function
      Throws:
      RetriesExhaustedException - if the maximum number of retries has been reached
    • execute

      public static <T> T execute(RetryableFunction<T> fn, int maxRetries) throws RetriesExhaustedException
      Executes the wrapped function with the specified maximum number of retries in the event of an exception.
      Type Parameters:
      T - return type of the wrapped function
      Parameters:
      fn - wrapped function to execute
      maxRetries - maximum number of times to retry the wrapped function in the event of an exception
      Returns:
      result of the wrapped function
      Throws:
      RetriesExhaustedException - if the maximum number of retries has been reached
    • execute

      public static <T> T execute(RetryableFunction<T> fn, int maxRetries, Long initialBackoff) throws RetriesExhaustedException
      Executes the wrapped function with the specified maximum number of retries, and an initial backoff, in the event of an exception.
      Type Parameters:
      T - return type of the wrapped function
      Parameters:
      fn - wrapped function to execute
      maxRetries - maximum number of times to retry the wrapped function in the event of an exception
      initialBackoff - number of milliseconds to initially wait for backoff
      Returns:
      result of the wrapped function
      Throws:
      RetriesExhaustedException - if the maximum number of retries has been reached
    • execute

      public static <T> T execute(RetryableFunction<T> fn, Collection<Class<? extends Throwable>> expectedErrors) throws RetriesExhaustedException
      Executes the wrapped function, terminating immediately if one of the expected errors is encountered.
      Type Parameters:
      T - return type of the wrapped function
      Parameters:
      fn - wrapped function to execute
      expectedErrors - a collection of errors that when encountered will cause the function to fail immediately without exhausting the maximum number of retries
      Returns:
      result of the wrapped function
      Throws:
      RetriesExhaustedException - if the maximum number of retries has been reached
    • execute

      public static <T> T execute(RetryableFunction<T> fn, int maxRetries, Collection<Class<? extends Throwable>> expectedErrors) throws RetriesExhaustedException
      Executes the wrapped function, terminating immediately if one of the expected errors is encountered, otherwise the function will be retried up to the specified maximum number of retries.
      Type Parameters:
      T - return type of the wrapped function
      Parameters:
      fn - wrapped function to execute
      maxRetries - maximum number of times to retry the wrapped function in the event of an exception not specified in the expectedErrors
      expectedErrors - a collection of errors that when encountered will cause the function to fail immediately without exhausting the maximum number of retries
      Returns:
      result of the wrapped function
      Throws:
      RetriesExhaustedException - if the maximum number of retries has been reached
    • execute

      public static <T> T execute(RetryableFunction<T> fn, int maxRetries, Long initialBackoff, Collection<Class<? extends Throwable>> expectedErrors) throws RetriesExhaustedException
      Executes the wrapped function, terminating immediately if one of the expected errors is encountered, otherwise the function will be retried up to the specified maximum number of retries.
      Type Parameters:
      T - return type of the wrapped function
      Parameters:
      fn - wrapped function to execute
      maxRetries - maximum number of times to retry the wrapped function in the event of an exception not specified in the expectedErrors
      initialBackoff - number of milliseconds to initially wait for backoff
      expectedErrors - a collection of errors that when encountered will cause the function to fail immediately without exhausting the maximum number of retries
      Returns:
      result of the wrapped function
      Throws:
      RetriesExhaustedException - if the maximum number of retries has been reached
      IllegalArgumentException - if maxRetries is negative or initialBackoff is negative
    • execute

      public static <T> T execute(RetryableFunction<T> fn, Supplier<T> fallback)
      Executes the wrapped function. In the event that all retries are exhausted, executes the fallback function.
      Type Parameters:
      T - return type of the wrapped function
      Parameters:
      fn - wrapped function to execute
      fallback - fallback Supplier to use if retries are exhausted
      Returns:
      result of the wrapped function
    • execute

      public static <T> T execute(RetryableFunction<T> fn, int maxRetries, Supplier<T> fallback)
      Executes the wrapped function. In the event that all retries are exhausted, executes the fallback function.
      Type Parameters:
      T - return type of the wrapped function
      Parameters:
      fn - wrapped function to execute
      maxRetries - the maximum number of times to retry the RetryableFunction in the event of an exception
      fallback - fallback Supplier to use if retries are exhausted
      Returns:
      result of the wrapped function
      Throws:
      IllegalArgumentException - if maxRetries is negative or fallback is null
    • execute

      public static <T> T execute(RetryableFunction<T> fn, int maxRetries, Long initialBackoff, Supplier<T> fallback)
      Executes the wrapped function. In the event that all retries are exhausted, executes the fallback function.
      Type Parameters:
      T - return type of the wrapped function
      Parameters:
      fn - wrapped function to execute
      maxRetries - the maximum number of times to retry the RetryableFunction in the event of an exception
      initialBackoff - number of milliseconds to initially wait for backoff
      fallback - fallback Supplier to use if retries are exhausted
      Returns:
      result of the wrapped function
      Throws:
      IllegalArgumentException - if maxRetries is negative, initialBackoff is negative, or fallback is null
    • execute

      public static <T> T execute(RetryableFunction<T> fn, int maxRetries, Long initialBackoff, Supplier<T> fallback, Collection<Class<? extends Throwable>> expectedErrors)
      Executes the provided RetryableFunction with the given parameters.
      Type Parameters:
      T - the return type of the wrapped function
      Parameters:
      fn - the RetryableFunction to execute
      maxRetries - the maximum number of times to retry the wrapped function in the event of an exception
      initialBackoff - number of milliseconds to initially wait for backoff
      fallback - the fallback Supplier to use if retries are exhausted
      expectedErrors - a collection of errors that will cause the function to fail immediately without exhausting the maximum number of retries
      Returns:
      the result of the wrapped function, or the result of the fallback Supplier if retries are exhausted
      Throws:
      IllegalArgumentException - if maxRetries is negative, initialBackoff is negative, or fallback is null