com.github.rholder.retry.Retryer Java Examples

The following examples show how to use com.github.rholder.retry.Retryer. 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: 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 #2
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 #3
Source File: RetryUtil.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
/**
 * Builds a retryer.
 *
 * @param actionDescription a description of the action being performed
 * @param waitUntilTime     the latest time to wait to perform the action
 * @param sleepTimeMs       the sleep time in millisecond for retries
 * @param exceptionClass    the class of exception to throw
 * @param logger            the logger
 * @param <T>               the type of object returned
 * @return the retryer
 */
private static <T> Retryer<T> buildRetryer(String actionDescription, ZonedDateTime waitUntilTime, long sleepTimeMs,
        Class<? extends CcmException> exceptionClass, Logger logger) {
    StopStrategy stop;
    if (waitUntilTime != null) {
        // Given the time at which waiting should stop,
        // get the available number of millis from this instant
        stop = StopStrategyFactory.waitUntilDateTime(waitUntilTime);
        logger.info("Trying until {} to {}", waitUntilTime, actionDescription);
    } else {
        stop = StopStrategies.neverStop();
        logger.warn("Unbounded wait to {}", actionDescription);
    }

    WaitStrategy wait = WaitStrategies.fixedWait(sleepTimeMs, TimeUnit.MILLISECONDS);
    logger.info("Checking every {} milliseconds", sleepTimeMs);

    return RetryerBuilder.<T>newBuilder()
            .retryIfException(t -> exceptionClass.isInstance(t) && ((CcmException) t).isRetryable())
            .retryIfResult(Objects::isNull)
            .withStopStrategy(stop)
            .withWaitStrategy(wait)
            .build();
}
 
Example #4
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 #5
Source File: RetryWriter.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/**
 * Build Retryer.
 * - If Writer implements Retriable, it will use the RetryerBuilder from the writer.
 * - Otherwise, it will use DEFAULT writer builder.
 *
 * - If Gobblin metrics is enabled, it will emit all failure count in to metrics.
 *
 * @param state
 * @return
 */
private Retryer<Void> buildRetryer(State state) {
  RetryerBuilder<Void> builder = null;
  if (writer instanceof Retriable) {
    builder = ((Retriable) writer).getRetryerBuilder();
  } else {
    builder = createRetryBuilder(state);
  }

  if (GobblinMetrics.isEnabled(state)) {
    final Optional<Meter> retryMeter = Optional.of(Instrumented.getMetricContext(state, getClass()).meter(FAILED_RETRY_WRITES_METER));

    builder.withRetryListener(new RetryListener() {
      @Override
      public <V> void onRetry(Attempt<V> attempt) {
        if (attempt.hasException()) {
          LOG.warn("Caught exception. This may be retried.", attempt.getExceptionCause());
          Instrumented.markMeter(retryMeter);
          failedWrites++;
        }
      }
    });
  }
  return builder.build();
}
 
Example #6
Source File: RetryerFactory.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/**
 * Creates new instance of retryer based on the config.
 * Accepted config keys are defined in RetryerFactory as static member variable.
 * You can use State along with ConfigBuilder and config prefix to build config.
 *
 * @param config
 * @return
 */
public static <T> Retryer<T> newInstance(Config config) {
  config = config.withFallback(DEFAULTS);
  RetryType type = RetryType.valueOf(config.getString(RETRY_TYPE).toUpperCase());

  switch (type) {
    case EXPONENTIAL:
      return newExponentialRetryer(config);
    case FIXED:
      return newFixedRetryer(config);
    case FIXED_ATTEMPT:
      return newFixedAttemptBoundRetryer(config);
    default:
      throw new IllegalArgumentException(type + " is not supported");
  }
}
 
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: 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 #9
Source File: DefaultPowerBiConnection.java    From powerbi-rest-java with MIT License 6 votes vote down vote up
@Override
public <T> Future<T> execute(final PowerBiOperation<T> val) {
    // use a retryer to keep attempting to send data to powerBI if we receive a rate limit exception.
    // use exponential backoff to create a window of time for the request to come through.

    // TODO : the time to wait is actually in the response header, come back and add that value.
    final Retryer<T> retryer = RetryerBuilder.<T>newBuilder()
            // we are retrying on these exceptions because they are able to be handled by just retrying this callable
            // again (assuming that the maximum retries value is greater than 1 and this isn't the last retry attempt
            // before giving up.
            .retryIfExceptionOfType(RateLimitExceededException.class)
            .retryIfExceptionOfType(RequestAuthenticationException.class)
            .withAttemptTimeLimiter(AttemptTimeLimiters.<T>fixedTimeLimit(maximumWaitTime, TimeUnit.MILLISECONDS))
            .withStopStrategy(StopStrategies.stopAfterAttempt(maximumRetries))
            .build();

    return executor.submit(new Callable<T>() {
        @Override
        public T call() throws Exception {
            return retryer.call(new PowerBiCallable<>(val, clientBuilder));
        }
    });
}
 
Example #10
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 #11
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 #12
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 #13
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 #14
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 #15
Source File: CommonConfig.java    From txle with Apache License 2.0 5 votes vote down vote up
@Bean
public Retryer<Boolean> retryer() {
    return RetryerBuilder.<Boolean>newBuilder()
            .retryIfException()
            .retryIfResult(Predicates.equalTo(false))
            .withWaitStrategy(WaitStrategies.fixedWait(interval, TimeUnit.SECONDS))
            .withStopStrategy(StopStrategies.stopAfterAttempt(times))
            .build();
}
 
Example #16
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 #17
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 #18
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 #19
Source File: RetryerFactory.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private static <T> Retryer<T> newFixedAttemptBoundRetryer(Config config) {
  return RetryerBuilder.<T> newBuilder()
      .retryIfException(RETRY_EXCEPTION_PREDICATE)
      .withWaitStrategy(WaitStrategies.fixedWait(config.getLong(RETRY_INTERVAL_MS), TimeUnit.MILLISECONDS))
      .withStopStrategy(StopStrategies.stopAfterAttempt(config.getInt(RETRY_TIMES)))
      .build();
}
 
Example #20
Source File: RetryerFactory.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private static <T> Retryer<T> newExponentialRetryer(Config config) {
  return RetryerBuilder.<T> newBuilder()
      .retryIfException(RETRY_EXCEPTION_PREDICATE)
      .withWaitStrategy(WaitStrategies.exponentialWait(config.getLong(RETRY_MULTIPLIER),
                                                       config.getLong(RETRY_INTERVAL_MS),
                                                       TimeUnit.MILLISECONDS))
      .withStopStrategy(StopStrategies.stopAfterDelay(config.getLong(RETRY_TIME_OUT_MS), TimeUnit.MILLISECONDS))
      .build();
}
 
Example #21
Source File: RetryerFactory.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private static <T> Retryer<T> newFixedRetryer(Config config) {
  return RetryerBuilder.<T> newBuilder()
      .retryIfException(RETRY_EXCEPTION_PREDICATE)
      .withWaitStrategy(WaitStrategies.fixedWait(config.getLong(RETRY_INTERVAL_MS), TimeUnit.MILLISECONDS))
      .withStopStrategy(StopStrategies.stopAfterDelay(config.getLong(RETRY_TIME_OUT_MS), TimeUnit.MILLISECONDS))
      .build();
}
 
Example #22
Source File: ShopifySdk.java    From shopify-sdk with Apache License 2.0 5 votes vote down vote up
private Retryer<Response> buildResponseRetyer() {
	return RetryerBuilder.<Response>newBuilder().retryIfResult(ShopifySdk::shouldRetryResponse).retryIfException()
			.withWaitStrategy(WaitStrategies.randomWait(minimumRequestRetryRandomDelayMilliseconds,
					TimeUnit.MILLISECONDS, maximumRequestRetryRandomDelayMilliseconds, TimeUnit.MILLISECONDS))
			.withStopStrategy(
					StopStrategies.stopAfterDelay(maximumRequestRetryTimeoutMilliseconds, TimeUnit.MILLISECONDS))
			.withRetryListener(new ShopifySdkRetryListener()).build();
}
 
Example #23
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 #24
Source File: MysqlSchemaUtil.java    From SpinalTap with Apache License 2.0 5 votes vote down vote up
private <T> Retryer<T> createRetryer() {
  return RetryerBuilder.<T>newBuilder()
      .retryIfRuntimeException()
      .withWaitStrategy(WaitStrategies.exponentialWait(2, 30, TimeUnit.SECONDS))
      .withStopStrategy(StopStrategies.stopAfterDelay(3 * DateTimeConstants.MILLIS_PER_MINUTE))
      .build();
}
 
Example #25
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 #26
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 #27
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 #28
Source File: EventIngressRetryingCallbackStream.java    From tasmo with Apache License 2.0 4 votes vote down vote up
public EventIngressRetryingCallbackStream(CallbackStream<List<WrittenEvent>> delegate, Retryer<Boolean> retryer) {
    this.delegate = delegate;
    this.retryer = retryer;
}
 
Example #29
Source File: EventIngressRetryingCallbackStream.java    From tasmo with Apache License 2.0 4 votes vote down vote up
public EventIngressRetryingCallbackStream(CallbackStream<List<WrittenEvent>> delegate, Retryer<Boolean> retryer) {
    this.delegate = delegate;
    this.retryer = retryer;
}
 
Example #30
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);
  }
}