.retryIfResult(Optional.ofNullable(resultRetryPredicate).orElse(result -> false)) .withWaitStrategy(WaitStrategies.join( WaitStrategies.exponentialWait(1000, 90, TimeUnit.SECONDS), WaitStrategies.randomWait(100, TimeUnit.MILLISECONDS, 500, TimeUnit.MILLISECONDS) ))
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(); }
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(); } }
/** * @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. }
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(); }
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<>(); }
.withStopStrategy(StopStrategies.stopAfterAttempt(retries)); if (backoffMax > 0) { retryerBuilder.withWaitStrategy(WaitStrategies.exponentialWait(backoffMax, TimeUnit.MILLISECONDS)); } else { retryerBuilder.withWaitStrategy(WaitStrategies.noWait());
.retryIfExceptionOfType(Exception.class) .retryIfRuntimeException() .withWaitStrategy(WaitStrategies.exponentialWait(5, 20, TimeUnit.SECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(3)) .build();
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); } }
@Override public RemoteConnection createRemoteConnection(String remoteAddress, OSFamily osFamily, LoginCredential loginCredential, int port) { Callable<RemoteConnection> callable = () -> remoteConnectionFactory .createRemoteConnection(remoteAddress, osFamily, loginCredential, port); Retryer<RemoteConnection> remoteConnectionRetryer = RetryerBuilder.<RemoteConnection>newBuilder().retryIfRuntimeException() .retryIfException(throwable -> throwable instanceof RemoteException) .withStopStrategy(StopStrategies.stopAfterAttempt(CONNECTION_RETRIES)) .withWaitStrategy(WaitStrategies .exponentialWait(INCREASE_TIMEOUT_FACTOR, MAXIMUM_TIMEOUT, TimeUnit.SECONDS)) .build(); try { return remoteConnectionRetryer.call(callable); } catch (ExecutionException | RetryException e) { throw new RuntimeException(e); } } }
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(); } }
private <T> T retry(Callable<T> f) throws ExecutionException, RetryException { final Retryer<T> retryer = RetryerBuilder.<T>newBuilder() .retryIfException(Impl::isRetryableException) .withWaitStrategy(WaitStrategies.join(exponentialWait(), randomWait(1, SECONDS))) .withStopStrategy(retryStopStrategy) .withRetryListener(Impl::onRequestAttempt) .build(); return retryer.call(f); }
&& ((GraknClientException) throwable).isRetriable()) .retryIfExceptionOfType(ConnectException.class) .withWaitStrategy(WaitStrategies.exponentialWait(10, 1, TimeUnit.MINUTES)) .withStopStrategy(StopStrategies.stopAfterAttempt(maxRetries + 1)) .withRetryListener(new RetryListener() {
/** * @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. }
&& ((GraknClientException) throwable).isRetriable()) .retryIfExceptionOfType(ConnectException.class) .withWaitStrategy(WaitStrategies.exponentialWait(10, 1, TimeUnit.MINUTES)) .withStopStrategy(StopStrategies.stopAfterAttempt(maxRetries + 1)) .withRetryListener(new RetryListener() {
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(); }
@Test public void testExponential() { WaitStrategy exponentialWait = WaitStrategies.exponentialWait(); assertTrue(exponentialWait.computeSleepTime(failedAttempt(1, 0)) == 2); assertTrue(exponentialWait.computeSleepTime(failedAttempt(2, 0)) == 4); assertTrue(exponentialWait.computeSleepTime(failedAttempt(3, 0)) == 8); assertTrue(exponentialWait.computeSleepTime(failedAttempt(4, 0)) == 16); assertTrue(exponentialWait.computeSleepTime(failedAttempt(5, 0)) == 32); assertTrue(exponentialWait.computeSleepTime(failedAttempt(6, 0)) == 64); }
@Test public void testExponentialWithMaximumWait() { WaitStrategy exponentialWait = WaitStrategies.exponentialWait(40, TimeUnit.MILLISECONDS); assertTrue(exponentialWait.computeSleepTime(failedAttempt(1, 0)) == 2); assertTrue(exponentialWait.computeSleepTime(failedAttempt(2, 0)) == 4); assertTrue(exponentialWait.computeSleepTime(failedAttempt(3, 0)) == 8); assertTrue(exponentialWait.computeSleepTime(failedAttempt(4, 0)) == 16); assertTrue(exponentialWait.computeSleepTime(failedAttempt(5, 0)) == 32); assertTrue(exponentialWait.computeSleepTime(failedAttempt(6, 0)) == 40); assertTrue(exponentialWait.computeSleepTime(failedAttempt(7, 0)) == 40); assertTrue(exponentialWait.computeSleepTime(failedAttempt(8, 0)) == 40); }
@Test public void testExponentialWithMultiplierAndMaximumWait() { WaitStrategy exponentialWait = WaitStrategies.exponentialWait(1000, 50000, TimeUnit.MILLISECONDS); assertTrue(exponentialWait.computeSleepTime(failedAttempt(1, 0)) == 2000); assertTrue(exponentialWait.computeSleepTime(failedAttempt(2, 0)) == 4000); assertTrue(exponentialWait.computeSleepTime(failedAttempt(3, 0)) == 8000); assertTrue(exponentialWait.computeSleepTime(failedAttempt(4, 0)) == 16000); assertTrue(exponentialWait.computeSleepTime(failedAttempt(5, 0)) == 32000); assertTrue(exponentialWait.computeSleepTime(failedAttempt(6, 0)) == 50000); assertTrue(exponentialWait.computeSleepTime(failedAttempt(7, 0)) == 50000); assertTrue(exponentialWait.computeSleepTime(failedAttempt(8, 0)) == 50000); }