Search in sources :

Example 1 with RetryListener

use of com.github.rholder.retry.RetryListener in project incubator-gobblin by apache.

the class RetryWriter method buildRetryer.

/**
 * 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();
}
Also used : Meter(com.codahale.metrics.Meter) RetryListener(com.github.rholder.retry.RetryListener)

Example 2 with RetryListener

use of com.github.rholder.retry.RetryListener in project graylog2-server by Graylog2.

the class MongoDBPreflightCheck method runCheck.

@Override
public void runCheck() throws PreflightCheckException {
    try {
        final Version mongoVersion = RetryerBuilder.<Version>newBuilder().retryIfResult(Objects::isNull).retryIfExceptionOfType(MongoTimeoutException.class).retryIfRuntimeException().withRetryListener(new RetryListener() {

            @Override
            public <V> void onRetry(Attempt<V> attempt) {
                if (attempt.hasResult()) {
                    return;
                }
                if (mongoVersionProbeAttempts == 0) {
                    LOG.info("MongoDB is not available. Retry #{}", attempt.getAttemptNumber());
                } else {
                    LOG.info("MongoDB is not available. Retry #{}/{}", attempt.getAttemptNumber(), mongoVersionProbeAttempts);
                }
            }
        }).withWaitStrategy(WaitStrategies.fixedWait(2, TimeUnit.SECONDS)).withStopStrategy(mongoVersionProbeAttempts == 0 ? StopStrategies.neverStop() : StopStrategies.stopAfterAttempt(mongoVersionProbeAttempts)).build().call(() -> {
            try (MongoClient mongoClient = (MongoClient) mongoConnection.connect()) {
                return MongoDBVersionCheck.getVersion(mongoClient);
            }
        });
        MongoDBVersionCheck.assertCompatibleVersion(mongoVersion);
        LOG.info("Connected to MongoDB version {}", mongoVersion);
    } catch (ExecutionException | RetryException e) {
        throw new PreflightCheckException("Failed to retrieve MongoDB version.", e);
    }
}
Also used : MongoClient(com.mongodb.MongoClient) Version(com.github.zafarkhaja.semver.Version) ExecutionException(java.util.concurrent.ExecutionException) RetryException(com.github.rholder.retry.RetryException) RetryListener(com.github.rholder.retry.RetryListener)

Aggregations

RetryListener (com.github.rholder.retry.RetryListener)2 Meter (com.codahale.metrics.Meter)1 RetryException (com.github.rholder.retry.RetryException)1 Version (com.github.zafarkhaja.semver.Version)1 MongoClient (com.mongodb.MongoClient)1 ExecutionException (java.util.concurrent.ExecutionException)1