.retryIfExceptionOfType(DuplicateKeyException.class) .withWaitStrategy(WaitStrategies.randomWait(5, TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(10)) .build(); try {
WaitStrategies.randomWait(100, TimeUnit.MILLISECONDS, 500, TimeUnit.MILLISECONDS) )) .withStopStrategy(StopStrategies.stopAfterAttempt(retryCount)) .withBlockStrategy(BlockStrategies.threadSleepStrategy()) .withRetryListener(new RetryListener() {
public BaseJdbcBufferedInserter(State state, Connection conn) { this.conn = conn; this.batchSize = state.getPropAsInt(WRITER_JDBC_INSERT_BATCH_SIZE, DEFAULT_WRITER_JDBC_INSERT_BATCH_SIZE); if (this.batchSize < 1) { throw new IllegalArgumentException(WRITER_JDBC_INSERT_BATCH_SIZE + " should be a positive number"); } int maxWait = state.getPropAsInt(WRITER_JDBC_INSERT_RETRY_TIMEOUT, DEFAULT_WRITER_JDBC_INSERT_RETRY_TIMEOUT); int maxAttempts = state.getPropAsInt(WRITER_JDBC_INSERT_RETRY_MAX_ATTEMPT, DEFAULT_WRITER_JDBC_INSERT_RETRY_MAX_ATTEMPT); //retry after 2, 4, 8, 16... sec, allow at most maxWait sec delay this.retryer = RetryerBuilder.<Boolean> newBuilder().retryIfException() .withWaitStrategy(WaitStrategies.exponentialWait(1000, maxWait, TimeUnit.SECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(maxAttempts)).build(); }
/** * Retry to execute callable command. * * @param callable callable command * @param <T> return type * @return execute result */ public <T> Optional<T> execute(final Callable<T> callable) { Retryer<T> retryer = RetryerBuilder.<T>newBuilder() .retryIfExceptionOfType(TimeoutException.class) .retryIfExceptionOfType(ExecutionException.class) .retryIfExceptionOfType(InterruptedException.class) .withWaitStrategy(WaitStrategies.fixedWait(config.getRetryIntervalMilliseconds(), TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(config.getMaxRetries())) .build(); try { return Optional.fromNullable(retryer.call(callable)); } catch (final ExecutionException | RetryException ex) { throw new RegistryCenterException(ex); } } }
/** * @return RetryerBuilder that retries on all exceptions except NonTransientException with exponential back off */ public static RetryerBuilder<Void> createRetryBuilder(State state) { Predicate<Throwable> transients = new Predicate<Throwable>() { @Override public boolean apply(Throwable t) { return !(t instanceof NonTransientException); } }; long multiplier = state.getPropAsLong(RETRY_MULTIPLIER, 500L); long maxWaitMsPerInterval = state.getPropAsLong(RETRY_MAX_WAIT_MS_PER_INTERVAL, 10000); int maxAttempts = state.getPropAsInt(RETRY_MAX_ATTEMPTS, 5); return RetryerBuilder.<Void> newBuilder() .retryIfException(transients) .withWaitStrategy(WaitStrategies.exponentialWait(multiplier, maxWaitMsPerInterval, TimeUnit.MILLISECONDS)) //1, 2, 4, 8, 16 seconds delay .withStopStrategy(StopStrategies.stopAfterAttempt(maxAttempts)); //Total 5 attempts and fail. }
private <T> T callWithRetriesAndTimeout(Callable<T> callable, Optional<Integer> retryCount) throws Exception { RetryerBuilder<T> retryerBuilder = RetryerBuilder.<T>newBuilder() .withAttemptTimeLimiter(AttemptTimeLimiters.<T>fixedTimeLimit(configuration.getDockerClientTimeLimitSeconds(), TimeUnit.SECONDS, executor)); if (retryCount.isPresent()) { retryerBuilder.withStopStrategy(StopStrategies.stopAfterAttempt(retryCount.get())); } return retryerBuilder.build().call(callable); } }
public SingularityClient(String contextPath, HttpClient httpClient, Provider<List<String>> hostsProvider, Optional<SingularityClientCredentials> credentials, boolean ssl, int retryAttempts, Predicate<HttpResponse> retryStrategy) { this.httpClient = httpClient; this.contextPath = contextPath; this.hostsProvider = hostsProvider; this.random = new Random(); this.credentials = credentials; this.ssl = ssl; this.httpResponseRetryer = RetryerBuilder.<HttpResponse>newBuilder() .withStopStrategy(StopStrategies.stopAfterAttempt(retryAttempts)) .withWaitStrategy(WaitStrategies.exponentialWait()) .retryIfResult(retryStrategy::test) .retryIfException() .build(); }
.retryIfRuntimeException() .withWaitStrategy(WaitStrategies.fixedWait(configuration.getRetryWaitMs(), TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(configuration.getRetryCount())) .build();
private void runHealthcheck() { Optional<HealthcheckOptions> maybeOptions = task.getTaskDefinition().getHealthcheckOptions(); Optional<String> expectedHealthcheckResultFilePath = task.getTaskDefinition().getHealthcheckResultFilePath(); if (maybeOptions.isPresent() && expectedHealthcheckResultFilePath.isPresent()) { LOG.debug("Checking for healthcheck file {}", expectedHealthcheckResultFilePath.get()); String taskAppDirectory = task.getTaskDefinition().getTaskAppDirectory(); File fullHealthcheckPath = Paths.get(taskAppDirectory, expectedHealthcheckResultFilePath.get()).toFile(); try { Integer healthcheckMaxRetries = maybeOptions.get().getMaxRetries().or(configuration.getDefaultHealthcheckMaxRetries()); Integer retryInterval = maybeOptions.get().getIntervalSeconds().or(5); Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder() .retryIfResult(bool -> !bool) .withWaitStrategy(WaitStrategies.fixedWait(retryInterval, TimeUnit.SECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(healthcheckMaxRetries)) .build(); retryer.call(() -> fullHealthcheckPath.exists()); executorUtils.sendStatusUpdate(task.getDriver(), task.getTaskInfo().getTaskId(), Protos.TaskState.TASK_RUNNING, String.format("Task running process %s (health check file found successfully).", getCurrentProcessToString()), task.getLog()); } catch (ExecutionException | RetryException e) { executorUtils.sendStatusUpdate(task.getDriver(), task.getTaskInfo().getTaskId(), TaskState.TASK_FAILED, String.format("Task timed out on health checks (health check file not found)."), task.getLog()); } } else { executorUtils.sendStatusUpdate(task.getDriver(), task.getTaskInfo().getTaskId(), Protos.TaskState.TASK_RUNNING, String.format("Task running process %s", getCurrentProcessToString()), task.getLog()); } } }
.retryIfRuntimeException() .withWaitStrategy(WaitStrategies.fixedWait(configuration.getRetryWaitMs(), TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(configuration.getRetryCount())) .build();
public MessageToken(Message message, Connection connection, IotHttp2Client client) { this.message = message; this.connection = connection; this.client = client; this.stopStrategy = StopStrategies.stopAfterAttempt(32); this.waitStrategy = WaitStrategies.exponentialWait(10, TimeUnit.MINUTES); this.attempt = new MessageAttempt(); this.publishFuture = new CompletableFuture<>(); }
public BaseJdbcBufferedInserter(State state, Connection conn) { this.conn = conn; this.batchSize = state.getPropAsInt(WRITER_JDBC_INSERT_BATCH_SIZE, DEFAULT_WRITER_JDBC_INSERT_BATCH_SIZE); if (this.batchSize < 1) { throw new IllegalArgumentException(WRITER_JDBC_INSERT_BATCH_SIZE + " should be a positive number"); } int maxWait = state.getPropAsInt(WRITER_JDBC_INSERT_RETRY_TIMEOUT, DEFAULT_WRITER_JDBC_INSERT_RETRY_TIMEOUT); int maxAttempts = state.getPropAsInt(WRITER_JDBC_INSERT_RETRY_MAX_ATTEMPT, DEFAULT_WRITER_JDBC_INSERT_RETRY_MAX_ATTEMPT); //retry after 2, 4, 8, 16... sec, allow at most maxWait sec delay this.retryer = RetryerBuilder.<Boolean> newBuilder().retryIfException() .withWaitStrategy(WaitStrategies.exponentialWait(1000, maxWait, TimeUnit.SECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(maxAttempts)).build(); }
@Override public void restore() { // Failing here means restarting the styx scheduler and replaying all events again. This is // quite time consuming and distressing when deploying, so try hard. final Retryer<Object> retryer = RetryerBuilder.newBuilder() .retryIfException() .withWaitStrategy(WaitStrategies.exponentialWait(10, TimeUnit.SECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(10)) .withRetryListener(this::onRestorePollPodsAttempt) .build(); try { retryer.call(Executors.callable(this::tryPollPods)); } catch (ExecutionException | RetryException e) { throw new RuntimeException(e); } }
@Bean public Retryer retryer(RatesAppConfigHolder ratesAppConfig) { return RetryerBuilder.newBuilder() .retryIfExceptionOfType(IOException.class) .retryIfRuntimeException() .withWaitStrategy(randomWait(ratesAppConfig.getMinTimeWait(), TimeUnit.MILLISECONDS, ratesAppConfig.getMaxTimeWait(), TimeUnit.MILLISECONDS)) .withStopStrategy(stopAfterAttempt(ratesAppConfig.getMaxAttempts())) .build(); }
@Bean public Retryer retryer(KycConfigHolder kycConfigHolder) { return RetryerBuilder.newBuilder() .retryIfExceptionOfType(Exception.class) .retryIfRuntimeException() .withWaitStrategy(randomWait(kycConfigHolder.getMinTimeWait(), TimeUnit.MILLISECONDS, kycConfigHolder.getMaxTimeWait(), TimeUnit.MILLISECONDS)) .withStopStrategy(stopAfterAttempt(kycConfigHolder.getMaxAttempts())) .build(); }
@Test public void testStopAfterAttempt() { assertFalse(StopStrategies.stopAfterAttempt(3).shouldStop(failedAttempt(2, 6546L))); assertTrue(StopStrategies.stopAfterAttempt(3).shouldStop(failedAttempt(3, 6546L))); assertTrue(StopStrategies.stopAfterAttempt(3).shouldStop(failedAttempt(4, 6546L))); }
public JSONFetcher(int timeout, int retries, int minBackoff, int maxBackoff) { Preconditions.checkArgument(retries >= 0, "retries argument must be a non-negative integer"); this.timeout = timeout; RetryerBuilder<byte[]> retryerBuilder = RetryerBuilder .<byte[]>newBuilder() .retryIfExceptionOfType(IOException.class) .withStopStrategy(StopStrategies.stopAfterAttempt(retries + 1)); if ((minBackoff > 0) && (maxBackoff > 0)) { retryerBuilder.withWaitStrategy(WaitStrategies.exponentialWait( minBackoff, maxBackoff, TimeUnit.MILLISECONDS)); } else { retryerBuilder.withWaitStrategy(WaitStrategies.noWait()); } retryer = retryerBuilder.build(); }
@Bean public Retryer retryer(KycConfigHolder kycConfigHolder) { return RetryerBuilder.newBuilder() .retryIfExceptionOfType(Exception.class) .retryIfRuntimeException() .withWaitStrategy(randomWait(kycConfigHolder.getMinTimeWait(), TimeUnit.MILLISECONDS, kycConfigHolder.getMaxTimeWait(), TimeUnit.MILLISECONDS)) .withStopStrategy(stopAfterAttempt(kycConfigHolder.getMaxAttempts())) .build(); }
@ParameterizedTest @MethodSource("unchecked") void testRunThrowsWhenRetriesAreStopped(Class<? extends Throwable> throwable) throws Exception { Retryer retryer = RetryerBuilder.newBuilder() .retryIfExceptionOfType(throwable) .withStopStrategy(StopStrategies.stopAfterAttempt(3)) .build(); Thrower thrower = new Thrower(throwable, 5); try { retryer.run(thrower); fail("Should have thrown"); } catch (RetryException e) { assertSame(e.getCause().getClass(), throwable); } assertEquals(3, thrower.invocations); }
@Test public void testWithStopStrategy() throws Exception { Callable<Boolean> callable = notNullAfter5Attempts(); Retryer retryer = RetryerBuilder.newBuilder() .withStopStrategy(StopStrategies.stopAfterAttempt(3)) .retryIfResult(Objects::isNull) .build(); try { retryer.call(callable); fail("RetryException expected"); } catch (RetryException e) { assertEquals(3, e.getNumberOfFailedAttempts()); } }