com.github.rholder.retry.RetryException Java Examples

The following examples show how to use com.github.rholder.retry.RetryException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HelloDemo.java    From retry with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    Callable<Boolean> callable = new Callable<Boolean>() {
        @Override
        public Boolean call() throws Exception {
            // do something useful here
            LOGGER.info("call...");
            throw new RuntimeException();
        }
    };

    Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
            .retryIfResult(Predicates.isNull())
            .retryIfExceptionOfType(IOException.class)
            .retryIfRuntimeException()
            .withStopStrategy(StopStrategies.stopAfterAttempt(3))
            .build();
    try {
        retryer.call(callable);
    } catch (RetryException | ExecutionException e) {
        e.printStackTrace();
    }

}
 
Example #2
Source File: GobblinMultiTaskAttempt.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/**
 * As the initialization of {@link Task} could have unstable external connection which could be healed through
 * retry, adding retry-wrapper here for the sake of fault-tolerance.
 */
@VisibleForTesting
Task createTaskWithRetry(WorkUnitState workUnitState, CountDownLatch countDownLatch) throws RetryException {
  Properties defaultRetryConfig = new Properties();
  defaultRetryConfig.setProperty(RETRY_TIME_OUT_MS, TimeUnit.MINUTES.toMillis(1L) + "");
  defaultRetryConfig.setProperty(RETRY_INTERVAL_MS, TimeUnit.SECONDS.toMillis(2L) + "");
  Config config = ConfigUtils.propertiesToConfig(this.jobState.getProperties())
      .withFallback(ConfigUtils.propertiesToConfig(defaultRetryConfig));
  Retryer<Task> retryer = RetryerFactory.newInstance(config);
  // An "effectively final" variable for counting how many retried has been done, mostly for logging purpose.
  final AtomicInteger counter = new AtomicInteger(0);

  try {
    return retryer.call(new Callable<Task>() {
      @Override
      public Task call()
          throws Exception {
        counter.incrementAndGet();
        log.info(String.format("Task creation attempt %s", counter.get()));
        return createTaskRunnable(workUnitState, countDownLatch);
      }
    });
  } catch (ExecutionException ee) {
    throw new RuntimeException("Failure in executing retryer due to, ", ee);
  }
}
 
Example #3
Source File: DuplicateHold.java    From java-samples with Apache License 2.0 6 votes vote down vote up
private void addAccountsToHold(String matterId, String holdId, List<HeldAccount> accounts)
    throws ExecutionException, RetryException {
  logger.log(
      Level.INFO,
      "There are more than 100 users on hold: " + holdId + " in matter: " + matterId + ".");
  for (HeldAccount account : accounts) {
    logger.log(Level.INFO, "Adding account: " + account.getAccountId() + " to hold: " + holdId);
    RetryableTemplate.callWithRetry(
        () ->
            vaultService
                .matters()
                .holds()
                .accounts()
                .create(matterId, holdId, account)
                .execute());
  }
}
 
Example #4
Source File: DigdagClient.java    From digdag with Apache License 2.0 6 votes vote down vote up
private Response invokeWithRetry(Invocation request)
{
    Retryer<Response> retryer = RetryerBuilder.<Response>newBuilder()
            .retryIfException(not(DigdagClient::isDeterministicError))
            .withWaitStrategy(exponentialWait())
            .withStopStrategy(stopAfterAttempt(10))
            .build();

    try {
        return retryer.call(() -> {
            Response res = request.invoke();
            if (res.getStatusInfo().getFamily() != SUCCESSFUL) {
                res.close();
                return handleErrorStatus(res);
            }
            return res;
        });
    }
    catch (ExecutionException | RetryException e) {
        Throwable cause = e.getCause() != null ? e.getCause() : e;
        throw Throwables.propagate(cause);
    }
}
 
Example #5
Source File: BigtableStorage.java    From styx with Apache License 2.0 6 votes vote down vote up
void writeEvent(SequenceEvent sequenceEvent) throws IOException {
  try {
    retryer.call(() -> {
      try (final Table eventsTable = connection.getTable(EVENTS_TABLE_NAME)) {
        final String workflowInstanceKey = sequenceEvent.event().workflowInstance().toKey();
        final String keyString = String.format("%s#%08d", workflowInstanceKey, sequenceEvent.counter());
        final byte[] key = Bytes.toBytes(keyString);
        final Put put = new Put(key, sequenceEvent.timestamp());

        final byte[] eventBytes = serialize(sequenceEvent.event()).toByteArray();
        put.addColumn(EVENT_CF, EVENT_QUALIFIER, eventBytes);
        eventsTable.put(put);
        return null;
      }
    });
  } catch (ExecutionException | RetryException e) {
    var cause = e.getCause();
    if (cause instanceof IOException) {
      throw (IOException) cause;
    } else {
      throw new RuntimeException(cause);
    }
  }
}
 
Example #6
Source File: ExponentialBackoff.java    From retry with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    Callable<Boolean> callable = new Callable<Boolean>() {
        @Override
        public Boolean call() throws Exception {
            // do something useful here
            LOGGER.info("call...");
            throw new RuntimeException();
        }
    };

    Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
            .retryIfResult(Predicates.isNull())
            .retryIfExceptionOfType(IOException.class)
            .retryIfRuntimeException()
            .withWaitStrategy(WaitStrategies.fixedWait(3, TimeUnit.SECONDS))
            .withStopStrategy(StopStrategies.stopAfterAttempt(3))
            .build();
    try {
        retryer.call(callable);
    } catch (RetryException | ExecutionException e) {
        e.printStackTrace();
    }
}
 
Example #7
Source File: KubernetesCleanupTest.java    From styx with Apache License 2.0 5 votes vote down vote up
private static <T> T retry(Callable<T> callable) {
  var retryer = RetryerBuilder.<T>newBuilder()
      .retryIfException()
      .withWaitStrategy(exponentialWait())
      .withStopStrategy(stopAfterDelay(30, SECONDS))
      .build();
  try {
    return retryer.call(callable);
  } catch (ExecutionException | RetryException e) {
    throw new RuntimeException(e);
  }
}
 
Example #8
Source File: ServiceAccountUsageAuthorizer.java    From styx with Apache License 2.0 5 votes vote down vote up
private <T> T retry(Callable<T> f) throws ExecutionException, RetryException {
  final Retryer<T> retryer = RetryerBuilder.<T>newBuilder()
      .retryIfException(Impl::isRetryableException)
      .withWaitStrategy(waitStrategy)
      .withStopStrategy(retryStopStrategy)
      .withRetryListener(Impl::onRequestAttempt)
      .build();

  return retryer.call(f);
}
 
Example #9
Source File: RetryableTemplate.java    From java-samples with Apache License 2.0 5 votes vote down vote up
public static <T> T callWithRetry(Callable<T> callable)
    throws ExecutionException, RetryException {
  Retryer<T> retryer =
      RetryerBuilder.<T>newBuilder()
          .retryIfException(
              input -> {
                if (input instanceof GoogleJsonResponseException) {
                  GoogleJsonResponseException jsonException = (GoogleJsonResponseException) input;
                  int responseCode = jsonException.getDetails().getCode();
                  if (DONT_RETRY.contains(responseCode)) {
                    logger.log(
                        Level.WARNING,
                        "Encountered Non Retryable Error: " + jsonException.getMessage());
                    return false;
                  } else {
                    logger.log(
                        Level.WARNING,
                        "Encountered retryable error: "
                            + jsonException.getMessage()
                            + ".Retrying...");
                    return true;
                  }
                } else {
                  logger.log(
                      Level.WARNING,
                      "Encountered error: " + input.getMessage() + ". Retrying...");
                  return true;
                }
              })
          .withWaitStrategy(WaitStrategies.fixedWait(40, TimeUnit.SECONDS))
          .withStopStrategy(StopStrategies.stopAfterAttempt(1000))
          .build();
  return retryer.call(callable);
}
 
Example #10
Source File: ShopifySdk.java    From shopify-sdk with Apache License 2.0 5 votes vote down vote up
private Response invokeResponseCallable(final Callable<Response> responseCallable) {
	final Retryer<Response> retryer = buildResponseRetyer();
	try {
		return retryer.call(responseCallable);
	} catch (ExecutionException | RetryException e) {
		throw new ShopifyClientException(RETRY_FAILED_MESSAGE, e);
	}
}
 
Example #11
Source File: RetryWriter.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private void callWithRetry(Callable<Void> callable) throws IOException {
  try {
    this.retryer.wrap(callable).call();
  } catch (ExecutionException | RetryException e) {
    throw new IOException(e);
  }
}
 
Example #12
Source File: GobblinHelixJobScheduler.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private void cancelJobIfRequired(DeleteJobConfigArrivalEvent deleteJobArrival) throws InterruptedException {
  Properties jobConfig = deleteJobArrival.getJobConfig();
  if (PropertiesUtils.getPropAsBoolean(jobConfig, GobblinClusterConfigurationKeys.CANCEL_RUNNING_JOB_ON_DELETE,
      GobblinClusterConfigurationKeys.DEFAULT_CANCEL_RUNNING_JOB_ON_DELETE)) {
    LOGGER.info("Cancelling workflow: {}", deleteJobArrival.getJobName());

    //Workaround for preventing indefinite hangs observed in TaskDriver.getWorkflows() call.
    Callable<Map<String, String>> workflowsCallable = () -> HelixUtils.getWorkflowIdsFromJobNames(this.jobHelixManager,
        Collections.singletonList(deleteJobArrival.getJobName()));
    Retryer<Map<String, String>> retryer = RetryerBuilder.<Map<String, String>>newBuilder()
        .retryIfException()
        .withStopStrategy(StopStrategies.stopAfterAttempt(5))
        .withAttemptTimeLimiter(AttemptTimeLimiters.fixedTimeLimit(this.helixWorkflowListingTimeoutMillis, TimeUnit.MILLISECONDS)).build();
    Map<String, String> jobNameToWorkflowIdMap;
    try {
      jobNameToWorkflowIdMap = retryer.call(workflowsCallable);
    } catch (ExecutionException | RetryException e) {
      LOGGER.error("Exception encountered when getting workflows from Helix; likely a Helix/Zk issue.", e);
      return;
    }

    if (jobNameToWorkflowIdMap.containsKey(deleteJobArrival.getJobName())) {
      String workflowId = jobNameToWorkflowIdMap.get(deleteJobArrival.getJobName());
      TaskDriver taskDriver = new TaskDriver(this.jobHelixManager);
      taskDriver.waitToStop(workflowId, this.helixJobStopTimeoutMillis);
      LOGGER.info("Stopped workflow: {}", deleteJobArrival.getJobName());
      //Wait until the cancelled job is complete.
      waitForJobCompletion(deleteJobArrival.getJobName());
    } else {
      LOGGER.warn("Could not find Helix Workflow Id for job: {}", deleteJobArrival.getJobName());
    }
  }
}
 
Example #13
Source File: RetryUtil.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
/**
 * Throws an appropriate exception when all attempts failed.
 *
 * @param e                 the retry exception
 * @param actionDescription a description of the action being performed
 * @throws InterruptedException if the action was interrupted
 * @throws E                    if the action timed out
 */
private static <E extends CcmException> void handleAllAttemptsFailed(
        RetryException e, String actionDescription, Class<E> exceptionClass, Supplier<E> exceptionSupplier, Logger logger) throws InterruptedException, E {
    logger.info("Failed to {}", actionDescription);
    Attempt<?> lastFailedAttempt = e.getLastFailedAttempt();
    if (lastFailedAttempt.hasException()) {
        Throwable cause = lastFailedAttempt.getExceptionCause();
        Throwables.throwIfInstanceOf(cause, exceptionClass);
        Throwables.throwIfInstanceOf(cause, InterruptedException.class);
        Throwables.throwIfUnchecked(cause);
        throw new IllegalStateException(cause);
    } else {
        throw exceptionSupplier.get();
    }
}
 
Example #14
Source File: ResyncListener.java    From Baragon with Apache License 2.0 5 votes vote down vote up
private void reapplyConfigsWithRetry() {
  Callable<Void> callable = new Callable<Void>() {
    public Void call() throws Exception {
      if (!agentLock.tryLock(agentLockTimeoutMs, TimeUnit.MILLISECONDS)) {
        LOG.warn("Failed to acquire lock for config reapply");
        throw new LockTimeoutException(String.format("Failed to acquire lock to reapply most current configs in %s ms", agentLockTimeoutMs), agentLock);
      }
      try {
        lifecycleHelper.applyCurrentConfigs();
        return null;
      } finally {
        agentLock.unlock();
      }
    }
  };

  Retryer<Void> retryer = RetryerBuilder.<Void>newBuilder()
    .retryIfException()
    .withStopStrategy(StopStrategies.stopAfterAttempt(configuration.getMaxReapplyConfigAttempts()))
    .withWaitStrategy(WaitStrategies.exponentialWait(1, TimeUnit.SECONDS))
    .build();

  try {
    retryer.call(callable);
  } catch (RetryException re) {
    LOG.error("Exception applying current configs", re.getLastFailedAttempt().getExceptionCause());
    lifecycleHelper.abort("Caught exception while trying to resync, aborting", re);
  } catch (ExecutionException ee) {
    LOG.error("Exception applying current configs", ee);
    lifecycleHelper.abort("Caught exception while trying to resync, aborting", ee);
  }
}
 
Example #15
Source File: GobblinMultiTaskAttempt.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
/**
 * Run a given list of {@link WorkUnit}s of a job.
 *
 * <p>
 *   This method assumes that the given list of {@link WorkUnit}s have already been flattened and
 *   each {@link WorkUnit} contains the task ID in the property {@link ConfigurationKeys#TASK_ID_KEY}.
 * </p>
 *
 * @param countDownLatch a {@link java.util.concurrent.CountDownLatch} waited on for job completion
 * @return a list of {@link Task}s from the {@link WorkUnit}s, as well as if there's a failure in task creation
 * which should be handled separately to avoid silently starving on certain workunit.
 */
private Pair<List<Task>, Boolean> runWorkUnits(CountUpAndDownLatch countDownLatch) {

  List<Task> tasks = Lists.newArrayList();
  boolean isTaskCreatedSuccessfully = true;
  while (this.workUnits.hasNext()) {
    WorkUnit workUnit = this.workUnits.next();
    String taskId = workUnit.getProp(ConfigurationKeys.TASK_ID_KEY);

    // skip tasks that executed successfully in a prior attempt
    if (taskSuccessfulInPriorAttempt(taskId)) {
      continue;
    }

    SubscopedBrokerBuilder<GobblinScopeTypes, ?> taskBrokerBuilder =
        this.jobBroker.newSubscopedBuilder(new TaskScopeInstance(taskId));
    WorkUnitState workUnitState = new WorkUnitState(workUnit, this.jobState, taskBrokerBuilder);
    workUnitState.setId(taskId);
    workUnitState.setProp(ConfigurationKeys.JOB_ID_KEY, this.jobId);
    workUnitState.setProp(ConfigurationKeys.TASK_ID_KEY, taskId);
    workUnitState.setProp(ConfigurationKeys.TASK_START_TIME_MILLIS_KEY, Long.toString(System.currentTimeMillis()));

    if (this.containerIdOptional.isPresent()) {
      workUnitState.setProp(ConfigurationKeys.TASK_ATTEMPT_ID_KEY, this.containerIdOptional.get());
    }

    // Create a new task from the work unit and submit the task to run.
    // If an exception occurs here then the count down latch is decremented
    // to avoid being stuck waiting for a task that was not created and submitted successfully.
    Task task = null;
    try {
      countDownLatch.countUp();
      task = createTaskWithRetry(workUnitState, countDownLatch);
      this.taskStateTracker.registerNewTask(task);
      task.setTaskFuture(this.taskExecutor.submit(task));
      tasks.add(task);
    } catch (Throwable e) {
      if (e instanceof OutOfMemoryError) {
        log.error("Encountering memory error in task creation/execution stage, please investigate memory usage:", e);
        printMemoryUsage();
      }

      if (task == null) {
        if (e instanceof RetryException) {
          // Indicating task being null due to failure in creation even after retrying.
          isTaskCreatedSuccessfully = false;
        }
        // task could not be created, so directly count down
        countDownLatch.countDown();
        log.error("Could not create task for workunit {}", workUnit, e);
      } else if (!task.hasTaskFuture()) {
        // Task was created and may have been registered, but not submitted, so call the
        // task state tracker task run completion directly since the task cancel does nothing if not submitted
        this.taskStateTracker.onTaskRunCompletion(task);
        log.error("Could not submit task for workunit {}", workUnit, e);
      } else {
        // task was created and submitted, but failed later, so cancel the task to decrement the CountDownLatch
        task.cancel();
        log.error("Failure after task submitted for workunit {}", workUnit, e);
      }
    }
  }

  EventSubmitter.Builder eventSubmitterBuilder = new EventSubmitter.Builder(JobMetrics.get(this.jobId, new JobMetrics.CreatorTag(this.attemptId)).getMetricContext(),
      "gobblin.runtime");
  eventSubmitterBuilder.addMetadata(this.taskEventMetadataGenerator.getMetadata(jobState, JobEvent.TASKS_SUBMITTED));
  eventSubmitterBuilder.build().submit(JobEvent.TASKS_SUBMITTED, "tasksCount", Long.toString(countDownLatch.getRegisteredParties()));

  return new Pair<>(tasks, isTaskCreatedSuccessfully);
}
 
Example #16
Source File: SingularityUploader.java    From Singularity with Apache License 2.0 4 votes vote down vote up
int uploadBatch(List<Path> toUpload) {
  final long start = System.currentTimeMillis();
  LOG.info("{} Uploading {} item(s)", logIdentifier, toUpload.size());

  int success = 0;

  for (int i = 0; i < toUpload.size(); i++) {
    final Context context = metrics.getUploadTimer().time();
    final Path file = toUpload.get(i);
    if (
      !configuration.isCheckForOpenFiles() ||
      uploadMetadata.isImmediate() ||
      !isFileOpen(file, configuration.isCheckOpenFilesViaFuser())
    ) {
      try {
        uploadSingle(i, file);
        metrics.upload();
        success++;
        Files.delete(file);
      } catch (RetryException re) {
        metrics.error();
        LOG.warn("{} Couldn't upload or delete {}", logIdentifier, file, re);
        exceptionNotifier.notify(
          String.format("%s exception during upload", re.getCause().getClass()),
          re.getCause(),
          ImmutableMap.of(
            "logIdentifier",
            logIdentifier,
            "file",
            file.toString(),
            "failedAttempts",
            Integer.toString(re.getNumberOfFailedAttempts())
          )
        );
      } catch (Exception e) {
        metrics.error();
        LOG.warn("{} Couldn't upload or delete {}", logIdentifier, file, e);
        exceptionNotifier.notify(
          String.format("Error during upload (%s)", e.getMessage()),
          e,
          ImmutableMap.of("logIdentifier", logIdentifier, "file", file.toString())
        );
      } finally {
        context.stop();
      }
    } else {
      LOG.debug("{} is in use by another process, will retry upload later", file);
    }
  }

  LOG.info(
    "{} Uploaded {} out of {} item(s) in {}",
    logIdentifier,
    success,
    toUpload.size(),
    JavaUtils.duration(start)
  );
  return toUpload.size();
}
 
Example #17
Source File: SingularityClient.java    From Singularity with Apache License 2.0 4 votes vote down vote up
private HttpResponse executeRequest(
  Function<String, String> hostToUri,
  Method method,
  Optional<?> body,
  Map<String, ?> queryParams
) {
  HttpRequest.Builder request = HttpRequest.newBuilder().setMethod(method);

  if (body.isPresent()) {
    request.setBody(body.get());
  }

  addQueryParams(request, queryParams);
  addCredentials(request);

  List<String> hosts = new ArrayList<>(hostsProvider.get());
  request.setRetryStrategy(RetryStrategy.NEVER_RETRY).setMaxRetries(1);

  try {
    return httpResponseRetryer.call(
      () -> {
        if (hosts.isEmpty()) {
          // We've tried everything we started with. Look again.
          hosts.addAll(hostsProvider.get());
        }

        int selection = random.nextInt(hosts.size());
        String host = hosts.get(selection);
        String url = hostToUri.apply(host);
        hosts.remove(selection);
        LOG.info("Making {} request to {}", method, url);
        request.setUrl(url);
        return httpClient.execute(request.build());
      }
    );
  } catch (ExecutionException | RetryException exn) {
    if (exn instanceof RetryException) {
      RetryException retryExn = (RetryException) exn;
      if (retryExn.getLastFailedAttempt().hasException()) {
        LOG.error(
          "Failed request to Singularity",
          retryExn.getLastFailedAttempt().getExceptionCause()
        );
      } else {
        LOG.error("Failed request to Singularity", exn);
      }
    } else {
      LOG.error("Failed request to Singularity", exn);
    }
    throw new SingularityClientException("Failed request to Singularity", exn);
  }
}