if (retry != null) { this.retryPolicy = new RetryPolicy() .withMaxRetries(retry.maxRetries()) .withMaxDuration(retry.maxDuration(), TimeUtil.chronoUnitToTimeUnit(retry.durationUnit())) .retryOn(retry.retryOn()); this.retryPolicy.abortOn(retry.abortOn()); factor = ((double) jitter) / delay; this.retryPolicy.withDelay(delay, TimeUnit.NANOSECONDS); this.retryPolicy.withJitter(factor); } else if (retry.delay() > 0) { this.retryPolicy.withDelay(retry.delay(), TimeUtil.chronoUnitToTimeUnit(retry.delayUnit())); this.retryPolicy = new RetryPolicy().withMaxRetries(0); // no retries
@Override public RetryPolicy getRetryPolicy() { RetryPolicy retryPolicy = new RetryPolicy().withMaxRetries(configuration.getRetries()); if (configuration.isExponentialBackoffEnabled()) { retryPolicy = retryPolicy.withBackoff(configuration.getExponentialBackoffDelay(), configuration.getExponentialBackoffMaxDelay(), TimeUnit.SECONDS); } return retryPolicy; } }
RetryPolicy failSafePolicy = new RetryPolicy(); failSafePolicy.withDelay(policy.getSleepTime().toMillis(), TimeUnit.MILLISECONDS); break; failSafePolicy.withDelay(policy.getRandomMin().toMillis(), policy.getRandomMax().toMillis(), TimeUnit.MILLISECONDS); break; failSafePolicy.withBackoff(policy.getSleepTime().toMillis(), policy.getExponentialMaxSleep().toMillis(), TimeUnit.MILLISECONDS, policy.getExponentialFactor()); break; failSafePolicy.withMaxDuration(policy.getMaxDuration().toMillis(), TimeUnit.MILLISECONDS); failSafePolicy.withMaxRetries(policy.getMaxAttempts()); failSafePolicy.withJitter(policy.getJitter().toMillis(), TimeUnit.MILLISECONDS); failSafePolicy.retryOn(e -> policy.getRetryPredicate().test(e));
public static RetryPolicy greedyRetryPolicy(long delayMs) { return new RetryPolicy() .retryOn(Arrays.asList( JedisException.class, DynoException.class, DynomiteClientDelegate.ClientDelegateException.class )) .withDelay(delayMs, TimeUnit.MILLISECONDS) .withMaxRetries(3); } }
/** Reset fileBackedOutputStream and retry if it fails. */ @SuppressWarnings("unchecked") private void reset() { RetryPolicy retryPolicy = new RetryPolicy() .retryOn(IOException.class) .withBackoff(INITIAL_RETRY_SLEEP, MAX_DELAY, INITIAL_RETRY_SLEEP_UNIT) .withMaxRetries(MAX_RETRY_ATTEMPTS); Failsafe.with(retryPolicy) .onFailedAttempt( throwable -> LOGGER.debug("failed to delete temporary file, will retry", throwable)) .onFailure(throwable -> LOGGER.debug("failed to delete temporary file", throwable)) .run(fileBackedOutputStream::reset); }
public SegmentMetaDataResponse checkDataSource(String engineName) { // @formatter:off RetryPolicy retryPolicy = new RetryPolicy() .retryOn(ResourceAccessException.class) .retryOn(Exception.class) .retryIf(result -> result == null) .withBackoff(delay, maxDelay, TimeUnit.SECONDS) .withMaxDuration(maxDuration, TimeUnit.SECONDS); // @formatter:on Callable<SegmentMetaDataResponse> callable = () -> queryService.segmentMetadata(engineName); // @formatter:off SegmentMetaDataResponse response = Failsafe.with(retryPolicy) .onRetriesExceeded((o, throwable) -> { throw new DataSourceIngestionException("Retries exceed for checking datasource : " + engineName); }) .onComplete((o, throwable, ctx) -> { if(ctx != null) { LOGGER.debug("Completed checking datasource({}). {} tries. Take time {} seconds.", engineName, ctx.getExecutions(), ctx.getElapsedTime().toSeconds()); } }) .get(callable); // @formatter:on return response; }
@Override public void stop(TestContext testContext, VirtualResource virtualResource, VirtualResourceInstance instance) { try { if (started.compareAndSet(true, false)) { testContext.<ContainerInfo>findCollection(DOCKER_CONTAINERS).stream().map( p -> p.id()) .forEachOrdered(containerId -> { LoggingUtil.INSTANCE.info( "Stopping and Removing Docker Container {}", containerId); RetryPolicy retryPolicy = new RetryPolicy() .retryOn(Throwable.class) .withBackoff(virtualResource.delay(), virtualResource .maxDelay(), virtualResource.unit()); stopContainer(containerId, retryPolicy); }); } } finally { if (client != null) { client.close(); } } }
public static RetryPolicy createRetryPolicy(final RiptideProperties.Retry config) { final RetryPolicy policy = new RetryPolicy(); Optional.ofNullable(config.getFixedDelay()) .ifPresent(delay -> delay.applyTo(policy::withDelay)); Optional.ofNullable(config.getBackoff()) .ifPresent(backoff -> { final TimeSpan delay = backoff.getDelay(); final TimeSpan maxDelay = backoff.getMaxDelay(); final TimeUnit unit = MILLISECONDS; @Nullable final Double delayFactor = backoff.getDelayFactor(); if (delayFactor == null) { policy.withBackoff(delay.to(unit), maxDelay.to(unit), unit); } else { policy.withBackoff(delay.to(unit), maxDelay.to(unit), unit, delayFactor); } }); Optional.ofNullable(config.getMaxRetries()) .ifPresent(policy::withMaxRetries); Optional.ofNullable(config.getMaxDuration()) .ifPresent(duration -> duration.applyTo(policy::withMaxDuration)); Optional.ofNullable(config.getJitterFactor()) .ifPresent(policy::withJitter); Optional.ofNullable(config.getJitter()) .ifPresent(jitter -> jitter.applyTo(policy::withJitter)); policy.retryOn(TransientFaultException.class); policy.retryOn(RetryException.class); policy.withDelay(new RetryAfterDelayFunction(systemUTC())); return policy; }
private RetryPolicy createRetryPolicy(int retries, Predicate<HermesResponse> retryCondition, long retrySleepInMillis, long maxRetrySleepInMillis) { RetryPolicy retryPolicy = new RetryPolicy().withMaxRetries(retries) .retryIf(retryCondition::test); if (retrySleepInMillis > 0) { retryPolicy.withBackoff(retrySleepInMillis, maxRetrySleepInMillis, TimeUnit.MILLISECONDS); } return retryPolicy; }
TopicMetadataLoader(BrokerMessageProducer brokerMessageProducer, int retryCount, long retryInterval, int threadPoolSize) { this.brokerMessageProducer = brokerMessageProducer; ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("topic-metadata-loader-%d").build(); this.scheduler = Executors.newScheduledThreadPool(threadPoolSize, threadFactory); this.retryPolicy = new RetryPolicy() .withMaxRetries(retryCount) .withDelay(retryInterval, TimeUnit.MILLISECONDS) .retryIf(MetadataLoadingResult::isFailure); }
TopicSchemaLoader(SchemaRepository schemaRepository, int retryCount, int threadPoolSize) { this.schemaRepository = schemaRepository; ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("topic-schema-loader-%d").build(); this.scheduler = Executors.newScheduledThreadPool(threadPoolSize, threadFactory); this.retryPolicy = new RetryPolicy() .withMaxRetries(retryCount) .retryIf(SchemaLoadingResult::isFailure); }
/** * Returns a copy of this RetryPolicy. */ public RetryPolicy<R> copy() { return new RetryPolicy<>(this); }
/** * Sets the {@code delayFunction} that computes the next delay before retrying. Delays will only occur for results * that equal the {@code result}. * * @param delayFunction the function to use to compute the delay before a next attempt * @param result the execution result that is expected in order to trigger the delay * @throws NullPointerException if {@code delayFunction} or {@code result} are null * @see DelayFunction */ public RetryPolicy<R> withDelayWhen(DelayFunction<R, ? extends Throwable> delayFunction, R result) { withDelay(delayFunction); Assert.notNull(result, "result"); this.delayResult = result; return this; }
/** * Sets the {@code delay} between retries, exponentially backing off to the {@code maxDelay} and multiplying * successive delays by a factor of 2. * * @throws NullPointerException if {@code chronoUnit} is null * @throws IllegalArgumentException if {@code delay} is <= 0 or {@code delay} is >= {@code maxDelay} * @throws IllegalStateException if {@code delay} is >= the {@link RetryPolicy#withMaxDuration(Duration) * maxDuration}, if delays have already been set, or if random delays have already been set */ public RetryPolicy<R> withBackoff(long delay, long maxDelay, ChronoUnit chronoUnit) { return withBackoff(delay, maxDelay, chronoUnit, 2); }
RetryPolicy failSafePolicy = new RetryPolicy(); failSafePolicy.withDelay(policy.getSleepTime().toMillis(), TimeUnit.MILLISECONDS); break; failSafePolicy.withDelay(policy.getRandomMin().toMillis(), policy.getRandomMax().toMillis(), TimeUnit.MILLISECONDS); break; failSafePolicy.withBackoff(policy.getSleepTime().toMillis(), policy.getExponentialMaxSleep().toMillis(), TimeUnit.MILLISECONDS, policy.getExponentialFactor()); break; failSafePolicy.withMaxDuration(policy.getMaxDuration().toMillis(), TimeUnit.MILLISECONDS); failSafePolicy.withMaxRetries(policy.getMaxAttempts()); failSafePolicy.withJitter(policy.getJitter().toMillis(), TimeUnit.MILLISECONDS); failSafePolicy.retryOn(e -> policy.getRetryPredicate().test(e));
protected RetryPolicy buildRetryPolicy() { RetryPolicy retryPolicy = new RetryPolicy().withMaxRetries(0); Properties userConfig = configurationGenerator.getUserConfig(); if (Boolean.parseBoolean((userConfig.getProperty(PARAM_RETRY_POLICY_ENABLED, "false")))) { int maxRetries = Integer.parseInt( userConfig.getProperty(PARAM_RETRY_POLICY_MAX_RETRIES, PARAM_RETRY_POLICY_DEFAULT_RETRIES)); int delay = Integer.parseInt( userConfig.getProperty(PARAM_RETRY_POLICY_DELAY_IN_MS, PARAM_POLICY_DEFAULT_DELAY_IN_MS)); retryPolicy = retryPolicy.retryOn(ConfigurationException.class).withMaxRetries(maxRetries).withDelay(delay, TimeUnit.MILLISECONDS); } return retryPolicy; }
public TreasureDataSender(Config config) { this.config = config; this.retryPolicy = new RetryPolicy(). retryOn(ex -> { if (ex == null) { // Success. Shouldn't retry. return false; } ErrorHandler errorHandler = config.getErrorHandler(); if (errorHandler != null) { errorHandler.handle(ex); } if (ex instanceof InterruptedException || ex instanceof NonRetryableException) { return false; } return true; }). withBackoff( getRetryInternalMs(), getMaxRetryInternalMs(), TimeUnit.MILLISECONDS, getRetryFactor()). withMaxRetries(getRetryMax()); this.client = buildClient(); }
public IngestionStatusResponse checkIngestion(String taskId) { RetryPolicy retryPolicy = new RetryPolicy() .retryOn(ResourceAccessException.class) .retryOn(Exception.class) .retryIf(result -> { if(result instanceof IngestionStatusResponse) { IngestionStatusResponse response = (IngestionStatusResponse) result; .withBackoff(delay, maxDelay, TimeUnit.SECONDS) .withMaxDuration(maxDuration, TimeUnit.SECONDS);
RetryPolicy retryPolicy = new RetryPolicy() .retryOn(IOException.class) .withBackoff(virtualResource.delay(), virtualResource.maxDelay(), virtualResource.unit());
/** * Sets the {@code delayFunction} that computes the next delay before retrying. Delays will only occur for failures * that are assignable from the {@code failure}. * * @param delayFunction the function to use to compute the delay before a next attempt * @param failure the execution failure that is expected in order to trigger the delay * @param <F> failure type * @throws NullPointerException if {@code delayFunction} or {@code failure} are null * @see DelayFunction */ public <F extends Throwable> RetryPolicy<R> withDelayOn(DelayFunction<R, F> delayFunction, Class<F> failure) { withDelay(delayFunction); Assert.notNull(failure, "failure"); this.delayFailure = failure; return this; }