@Override public V allocate(final Slot slot) throws Exception { return retryer.call(new Callable<V>() { @Override public V call() throws Exception { return allocator.allocate(slot); } }); }
/** * @param allocator Allocator * @param waitInterval Wait time between allocation attempts (ms) * @param maxWaitInterval Max time to wait for an allocation (ms) */ public RetryingAllocator(Allocator<V> allocator, long waitInterval, long maxWaitInterval) { this.allocator = allocator; this.retryer = RetryerBuilder.<V>newBuilder() .retryIfException() .withWaitStrategy(WaitStrategies.fixedWait(waitInterval, TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterDelay(maxWaitInterval, TimeUnit.MILLISECONDS)) .withRetryListener(new RetryListener() { @Override public <V> void onRetry(Attempt<V> attempt) { if (attempt.hasException()) { log.error("Error allocating slot", attempt.getExceptionCause()); } } }) .build(); }
@Override public <V> void onRetry(Attempt<V> attempt) { if (attempt.hasException()) { LOG.error("Caught exception during bulk indexing: {}, retrying (attempt #{}).", attempt.getExceptionCause(), attempt.getAttemptNumber()); } else if (attempt.getAttemptNumber() > 1) { LOG.info("Bulk indexing finally successful (attempt #{}).", attempt.getAttemptNumber()); } } })
String shortDescription, String operationName) throws RuntimeException { Retryer<T> retryer = RetryerBuilder.<T>newBuilder() .retryIfException(Optional.ofNullable(throwablePredicate).orElse(exception -> true)) .retryIfResult(Optional.ofNullable(resultRetryPredicate).orElse(result -> false)) .withWaitStrategy(WaitStrategies.join( WaitStrategies.exponentialWait(1000, 90, TimeUnit.SECONDS), WaitStrategies.randomWait(100, TimeUnit.MILLISECONDS, 500, TimeUnit.MILLISECONDS) )) .withStopStrategy(StopStrategies.stopAfterAttempt(retryCount)) .withBlockStrategy(BlockStrategies.threadSleepStrategy()) .withRetryListener(new RetryListener() { @Override public <V> void onRetry(Attempt<V> attempt) { .build(); return retryer.call(supplierCommand::get); } catch (ExecutionException executionException) { String errorMessage = String.format("Operation '%s:%s' failed for the %d time in RetryUtil", operationName, String errorMessage = String.format("Operation '%s:%s' failed after retrying %d times, retry limit %d", operationName, shortDescription, internalNumberOfRetries.get(), 3); logger.debug(errorMessage, retryException.getLastFailedAttempt().getExceptionCause()); throw new RuntimeException(errorMessage, retryException.getLastFailedAttempt().getExceptionCause());
/** * 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); } } }
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(); }
final Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder() .retryIfResult(Predicates.not(Predicates.equalTo(Boolean.TRUE))) .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS)) .withStopStrategy(StopStrategies.stopAfterDelay(maxWait, timeUnit)) .build(); retryer.call(checkForEmptyBuffers); } catch (RetryException e) { LOG.info("Buffers not empty after {} {}. Giving up.", maxWait, timeUnit.name().toLowerCase(Locale.ENGLISH));
final Retryer<Object> retryer = RetryerBuilder.newBuilder() .retryIfExceptionOfType(DuplicateKeyException.class) .withWaitStrategy(WaitStrategies.randomWait(5, TimeUnit.MILLISECONDS)) .withStopStrategy(StopStrategies.stopAfterAttempt(10)) .build(); try { retryer.call(() -> mongoDBSessionService.saveWithoutValidation(dbSession)); } catch (ExecutionException e) { LOG.warn("Unexpected exception when saving session to MongoDB. Failed to update session.", e); } catch (RetryException e) { LOG.warn("Tried to update session 10 times, but still failed. This is likely because of https://jira.mongodb.org/browse/SERVER-14322", e); throw new RuntimeException(e.getCause());
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(); } }
@Inject public IngestExecutorService(@Named(PERMIT_COUNT) int permitCount) { m_permitCount = permitCount; //m_congestionTimer = new CongestionTimer(m_permitCount); m_semaphore = new CongestionSemaphore(m_permitCount); m_threadGroup = new ThreadGroup("KairosDynamic"); m_internalExecutor = Executors.newCachedThreadPool(new ThreadFactory() { private int m_count = 0; @Override public Thread newThread(Runnable r) { Thread t = new Thread(m_threadGroup, r,"Ingest worker-"+m_count++); //t.setDaemon(true); return t; } }); m_retryer = RetryerBuilder.<Integer>newBuilder() .retryIfExceptionOfType(NoHostAvailableException.class) .retryIfExceptionOfType(UnavailableException.class) .withWaitStrategy(WaitStrategies.fibonacciWait(1, TimeUnit.MINUTES)) .build(); }
private BulkResult runBulkRequest(final Bulk request, int count) { try { return BULK_REQUEST_RETRYER.call(() -> client.execute(request)); } catch (ExecutionException | RetryException e) { if (e instanceof RetryException) { LOG.error("Could not bulk index {} messages. Giving up after {} attempts.", count, ((RetryException) e).getNumberOfFailedAttempts()); } else { LOG.error("Couldn't bulk index " + count + " messages.", e); } throw new RuntimeException(e); } }
@Override public <V> void onRetry(Attempt<V> attempt) { if (attempt.hasException()) { log.error("Error allocating slot", attempt.getExceptionCause()); } } })
@Override public <V> void onRetry(Attempt<V> attempt) { logger.debug("Attempt # {}, {} millis since first attempt. Operation: {}, description:{}", attempt.getAttemptNumber(), attempt.getDelaySinceFirstAttempt(), operationName, shortDescription); internalNumberOfRetries.incrementAndGet(); } })
/** Calls to submit will block until a permit is available to process the request @param callable */ public void submit(Callable<Integer> callable) { try { //System.out.println("Execute called"); m_semaphore.acquire(); //System.out.println("Submitting"); m_internalExecutor.submit( new IngestFutureTask(m_retryer.wrap(callable))); //System.out.println("Done submitting"); } //Potentially thrown by acquire catch (InterruptedException e) { e.printStackTrace(); } }
/** * @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 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(); }
JOURNAL_WRITE_RETRYER.call(new Callable<Void>() { @Override public Void call() throws Exception {
@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++; } } });
/** * Submits the user defined {@link #insertBatch(PreparedStatement)} call to the {@link Retryer} which takes care * of resubmitting the records according to {@link #WRITER_JDBC_INSERT_RETRY_TIMEOUT} and {@link #WRITER_JDBC_INSERT_RETRY_MAX_ATTEMPT} * when failure happens. * * @param pstmt PreparedStatement object */ protected void executeBatchInsert(final PreparedStatement pstmt) { try { // Need a Callable interface to be wrapped by Retryer. this.retryer.wrap(new Callable<Boolean>() { @Override public Boolean call() throws Exception { return insertBatch(pstmt); } }).call(); } catch (Exception e) { throw new RuntimeException("Failed to insert.", e); } resetBatch(); }