Java Code Examples for com.github.rholder.retry.Retryer#call()

The following examples show how to use com.github.rholder.retry.Retryer#call() . 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: 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 2
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 3
Source File: HBaseRetryingUtils.java    From flinkDemo with Apache License 2.0 6 votes vote down vote up
/**
 * 重试发送数据到hbase
 *
 * @param table
 * @param puts      List<Put>
 * @throws Exception 连接异常
 */
public static void retrying(Table table, List<Put> puts) throws Exception {
    // 异常或者返回null都继续重试、每3秒重试一次、最多重试5次
    Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
            .retryIfException()
            .withWaitStrategy(WaitStrategies.fixedWait(500, TimeUnit.MILLISECONDS))
            .withStopStrategy(StopStrategies.stopAfterAttempt(6))
            .build();

    try {
        retryer.call(() -> HBaseUtils.batchPuts(table, puts));
    } catch (Exception e) {
        LOGGER.error("多次重试发送数据到hbase失败!", e);
        throw new Exception("多次重试发送数据到hbase失败!", e);
    }
}
 
Example 4
Source File: ServiceProvider.java    From ranger with Apache License 2.0 6 votes vote down vote up
private void createPath() throws Exception {
    Retryer<Void> retryer = RetryerBuilder.<Void>newBuilder()
            .retryIfExceptionOfType(KeeperException.NodeExistsException.class) //Ephemeral node still exists
            .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
            .withBlockStrategy(BlockStrategies.threadSleepStrategy())
            .withStopStrategy(StopStrategies.neverStop())
            .build();
    try {
        retryer.call(() -> {
            curatorFramework.create().withMode(CreateMode.EPHEMERAL).forPath(
                    String.format("/%s/%s", serviceName, serviceNode.representation()),
                    serializer.serialize(serviceNode));
            return null;
        });
    } catch (Exception e) {
        final String message = String.format("Could not create node for %s after 60 retries (1 min). " +
                        "This service will not be discoverable. Retry after some time.", serviceName);
        logger.error(message, e);
        throw new Exception(message, e);
    }

}
 
Example 5
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 6
Source File: LifecycleHelper.java    From Baragon with Apache License 2.0 6 votes vote down vote up
public void notifyService(String action) throws Exception {
  long start = System.currentTimeMillis();
  Retryer<AgentCheckInResponse> retryer = RetryerBuilder.<AgentCheckInResponse>newBuilder()
      .retryIfException()
      .withStopStrategy(StopStrategies.stopAfterAttempt(configuration.getMaxNotifyServiceAttempts()))
      .withWaitStrategy(WaitStrategies.exponentialWait(1, TimeUnit.SECONDS))
      .build();

  AgentCheckInResponse agentCheckInResponse = retryer.call(checkInCallable(action, false));
  while ((agentCheckInResponse.getState() != TrafficSourceState.DONE
      && System.currentTimeMillis() - start < configuration.getAgentCheckInTimeoutMs())) {
    try {
      Thread.sleep(agentCheckInResponse.getWaitTime());
    } catch (InterruptedException ie) {
      LOG.error("Interrupted waiting for check in with service, shutting down early");
      break;
    }
    agentCheckInResponse = retryer.call(checkInCallable(action, true));
  }
  LOG.info("Finished agent check in");
}
 
Example 7
Source File: MRCompactorJobRunner.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
private void moveTmpPathToOutputPath() throws IOException {
  Retryer<Void> retryer = RetryerFactory.newInstance(this.retrierConfig);

  LOG.info(String.format("Moving %s to %s", this.dataset.outputTmpPath(), this.dataset.outputPath()));

  this.fs.delete(this.dataset.outputPath(), true);

  if (this.isRetryEnabled) {
    try {
      retryer.call(() -> {
        if (fs.exists(this.dataset.outputPath())) {
          throw new IOException("Path " + this.dataset.outputPath() + " exists however it should not. Will wait more.");
        }
        return null;
      });
    } catch (Exception e) {
      throw new IOException(e);
    }
  }

  WriterUtils.mkdirsWithRecursivePermissionWithRetry(MRCompactorJobRunner.this.fs, this.dataset.outputPath().getParent(), this.perm, this.retrierConfig);

  Log.info("Moving from fs: ("+MRCompactorJobRunner.this.tmpFs.getUri()+") path: "+ this.dataset.outputTmpPath() + " to "+ "fs: ("+ FileSystem.get(this.dataset.outputPath().getParent().toUri(), this.fs.getConf()).getUri()+") output path: " + this.dataset.outputPath());
  HadoopUtils.movePath (MRCompactorJobRunner.this.tmpFs, this.dataset.outputTmpPath(), FileSystem.get(this.dataset.outputPath().getParent().toUri(), this.fs.getConf()), this.dataset.outputPath(), false, this.fs.getConf()) ;
}
 
Example 8
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 9
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 10
Source File: LifecycleHelper.java    From Baragon with Apache License 2.0 5 votes vote down vote up
private Collection<BaragonServiceState> getGlobalStateWithRetry() {
  Retryer<Collection<BaragonServiceState>> retryer = RetryerBuilder.<Collection<BaragonServiceState>>newBuilder()
      .retryIfException()
      .withStopStrategy(StopStrategies.stopAfterAttempt(configuration.getMaxGetGloablStateAttempts()))
      .withWaitStrategy(WaitStrategies.exponentialWait(1, TimeUnit.SECONDS))
      .build();

  try {
    return retryer.call(this::getGlobalState);
  } catch (Exception e) {
    LOG.error("Could not get global state from Baragon Service");
    throw Throwables.propagate(e);
  }
}
 
Example 11
Source File: SingularityUpstreamChecker.java    From Singularity with Apache License 2.0 5 votes vote down vote up
private void checkSyncUpstreamsState(
  LoadBalancerRequestId loadBalancerRequestId,
  String singularityRequestId
) {
  Retryer<SingularityLoadBalancerUpdate> syncingRetryer = RetryerBuilder
    .<SingularityLoadBalancerUpdate>newBuilder()
    .retryIfException()
    .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
    .retryIfResult(IS_WAITING_STATE)
    .build();
  try {
    LOG.info(
      "Checking load balancer request to sync upstreams for service {} using a retryer until the request state is no longer waiting.",
      singularityRequestId
    );
    SingularityLoadBalancerUpdate syncUpstreamsState = syncingRetryer.call(
      () -> lbClient.getState(loadBalancerRequestId)
    );
    if (syncUpstreamsState.getLoadBalancerState() == BaragonRequestState.SUCCESS) {
      LOG.debug(
        "Syncing upstreams for singularity request {} is {}.",
        singularityRequestId,
        syncUpstreamsState
      );
    } else {
      LOG.error(
        "Syncing upstreams for singularity request {} is {}.",
        singularityRequestId,
        syncUpstreamsState
      );
    }
  } catch (Exception e) {
    LOG.error(
      "Could not check sync upstream state for singularity request {}. ",
      singularityRequestId,
      e
    );
  }
}
 
Example 12
Source File: AWSInstanceNameLookupProcessor.java    From graylog-plugin-aws with Apache License 2.0 5 votes vote down vote up
private void waitForMigrationCompletion(ClusterConfigService clusterConfigService) throws java.util.concurrent.ExecutionException, com.github.rholder.retry.RetryException {
    final Retryer<Boolean> waitingForMigrationCompletion = RetryerBuilder.<Boolean>newBuilder()
            .retryIfResult((result) -> result == null || !result)
            .build();

    waitingForMigrationCompletion.call(() -> clusterConfigService.get(
            V20200505121200_EncryptAWSSecretKey.MigrationCompleted.class
    ) != null);
}
 
Example 13
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 14
Source File: WriterUtils.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
public static void mkdirsWithRecursivePermissionWithRetry(final FileSystem fs, final Path path, FsPermission perm, Config retrierConfig) throws IOException {

    if (fs.exists(path)) {
      return;
    }

    if (path.getParent() != null && !fs.exists(path.getParent())) {
      mkdirsWithRecursivePermissionWithRetry(fs, path.getParent(), perm, retrierConfig);
    }

    if (!fs.mkdirs(path, perm)) {
      throw new IOException(String.format("Unable to mkdir %s with permission %s", path, perm));
    }

    if (retrierConfig != NO_RETRY_CONFIG) {
      //Wait until file is not there as it can happen the file fail to exist right away on eventual consistent fs like Amazon S3
      Retryer<Void> retryer = RetryerFactory.newInstance(retrierConfig);

      try {
        retryer.call(() -> {
          if (!fs.exists(path)) {
            throw new IOException("Path " + path + " does not exist however it should. Will wait more.");
          }
          return null;
        });
      } catch (Exception e) {
        throw new IOException("Path " + path + "does not exist however it should. Giving up..."+ e);
      }
    }

    // Double check permission, since fs.mkdirs() may not guarantee to set the permission correctly
    if (!fs.getFileStatus(path).getPermission().equals(perm)) {
      fs.setPermission(path, perm);
    }
  }
 
Example 15
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 16
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 17
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 18
Source File: GobblinHelixTask.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
public GobblinHelixTask(TaskRunnerSuiteBase.Builder builder,
                        TaskCallbackContext taskCallbackContext,
                        TaskAttemptBuilder taskAttemptBuilder,
                        StateStores stateStores,
                        GobblinHelixTaskMetrics taskMetrics,
                        TaskDriver taskDriver)
{
  this.taskConfig = taskCallbackContext.getTaskConfig();
  this.helixJobId = taskCallbackContext.getJobConfig().getJobId();
  this.applicationName = builder.getApplicationName();
  this.instanceName = builder.getInstanceName();
  this.taskMetrics = taskMetrics;
  getInfoFromTaskConfig();

  Path jobStateFilePath = GobblinClusterUtils
      .getJobStateFilePath(stateStores.haveJobStateStore(),
                           builder.getAppWorkPath(),
                           this.jobId);

  Integer partitionNum = getPartitionForHelixTask(taskDriver);
  if (partitionNum == null) {
    throw new IllegalStateException(String.format("Task %s, job %s on instance %s has no partition assigned",
        this.helixTaskId, builder.getInstanceName(), this.helixJobId));
  }

  // Dynamic config is considered as part of JobState in SingleTask
  // Important to distinguish between dynamicConfig and Config
  final Config dynamicConfig = builder.getDynamicConfig()
      .withValue(GobblinClusterConfigurationKeys.TASK_RUNNER_HOST_NAME_KEY, ConfigValueFactory.fromAnyRef(builder.getHostName()))
      .withValue(GobblinClusterConfigurationKeys.CONTAINER_ID_KEY, ConfigValueFactory.fromAnyRef(builder.getContainerId()))
      .withValue(GobblinClusterConfigurationKeys.HELIX_INSTANCE_NAME_KEY, ConfigValueFactory.fromAnyRef(builder.getInstanceName()))
      .withValue(GobblinClusterConfigurationKeys.HELIX_JOB_ID_KEY, ConfigValueFactory.fromAnyRef(this.helixJobId))
      .withValue(GobblinClusterConfigurationKeys.HELIX_TASK_ID_KEY, ConfigValueFactory.fromAnyRef(this.helixTaskId))
      .withValue(GobblinClusterConfigurationKeys.HELIX_PARTITION_ID_KEY, ConfigValueFactory.fromAnyRef(partitionNum));

  Retryer<SingleTask> retryer = RetryerFactory.newInstance(builder.getConfig());

  try {
    eventBus = EventBusFactory.get(ContainerHealthCheckFailureEvent.CONTAINER_HEALTH_CHECK_EVENT_BUS_NAME,
        SharedResourcesBrokerFactory.getImplicitBroker());

    this.task = retryer.call(new Callable<SingleTask>() {
      @Override
      public SingleTask call() {
        return new SingleTask(jobId, workUnitFilePath, jobStateFilePath, builder.getFs(), taskAttemptBuilder,
            stateStores,
            dynamicConfig);
      }
    });
  } catch (Exception e) {
    log.error("Execution in creating a SingleTask-with-retry failed, will create a failing task", e);
    this.task = new SingleFailInCreationTask(jobId, workUnitFilePath, jobStateFilePath, builder.getFs(), taskAttemptBuilder,
        stateStores, dynamicConfig);
  }
}