Search in sources :

Example 1 with WaitStrategy

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

the class MessagesRetryWaitTest method secondsBasedRetryWaitsForSecondsStartingWith1.

@Test
void secondsBasedRetryWaitsForSecondsStartingWith1() {
    WaitStrategy waitStrategy = Messages.exponentialWaitSeconds;
    assertAll(() -> assertThat(waitStrategy.computeSleepTime(new IndexBlockRetryAttempt(1))).isEqualTo(1000), () -> assertThat(waitStrategy.computeSleepTime(new IndexBlockRetryAttempt(2))).isEqualTo(2000), () -> assertThat(waitStrategy.computeSleepTime(new IndexBlockRetryAttempt(3))).isEqualTo(4000), () -> assertThat(waitStrategy.computeSleepTime(new IndexBlockRetryAttempt(4))).isEqualTo(8000), () -> assertThat(waitStrategy.computeSleepTime(new IndexBlockRetryAttempt(5))).isEqualTo(16000));
}
Also used : WaitStrategy(com.github.rholder.retry.WaitStrategy) Test(org.junit.jupiter.api.Test)

Example 2 with WaitStrategy

use of com.github.rholder.retry.WaitStrategy in project cloudbreak by hortonworks.

the class RetryUtil method buildRetryer.

/**
 * Builds a retryer.
 *
 * @param actionDescription a description of the action being performed
 * @param waitUntilTime     the latest time to wait to perform the action
 * @param sleepTimeMs       the sleep time in millisecond for retries
 * @param exceptionClass    the class of exception to throw
 * @param logger            the logger
 * @param <T>               the type of object returned
 * @return the retryer
 */
private static <T> Retryer<T> buildRetryer(String actionDescription, ZonedDateTime waitUntilTime, long sleepTimeMs, Class<? extends CcmException> exceptionClass, Logger logger) {
    StopStrategy stop;
    if (waitUntilTime != null) {
        // Given the time at which waiting should stop,
        // get the available number of millis from this instant
        stop = StopStrategyFactory.waitUntilDateTime(waitUntilTime);
        logger.info("Trying until {} to {}", waitUntilTime, actionDescription);
    } else {
        stop = StopStrategies.neverStop();
        logger.warn("Unbounded wait to {}", actionDescription);
    }
    WaitStrategy wait = WaitStrategies.fixedWait(sleepTimeMs, TimeUnit.MILLISECONDS);
    logger.info("Checking every {} milliseconds", sleepTimeMs);
    return RetryerBuilder.<T>newBuilder().retryIfException(t -> exceptionClass.isInstance(t) && ((CcmException) t).isRetryable()).retryIfResult(Objects::isNull).withStopStrategy(stop).withWaitStrategy(wait).build();
}
Also used : CcmException(com.sequenceiq.cloudbreak.ccm.exception.CcmException) StopStrategy(com.github.rholder.retry.StopStrategy) WaitStrategy(com.github.rholder.retry.WaitStrategy)

Example 3 with WaitStrategy

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

the class MessagesRetryWaitTest method millisecondsBasedRetryWaitsForMillisecondsStartingWith2.

// This test was added to document how the retry strategy actually behaves since this is hard to deduct from the code
@Test
void millisecondsBasedRetryWaitsForMillisecondsStartingWith2() {
    WaitStrategy waitStrategy = Messages.exponentialWaitMilliseconds;
    assertAll(() -> assertThat(waitStrategy.computeSleepTime(new IndexBlockRetryAttempt(1))).isEqualTo(2), () -> assertThat(waitStrategy.computeSleepTime(new IndexBlockRetryAttempt(2))).isEqualTo(4), () -> assertThat(waitStrategy.computeSleepTime(new IndexBlockRetryAttempt(3))).isEqualTo(8), () -> assertThat(waitStrategy.computeSleepTime(new IndexBlockRetryAttempt(4))).isEqualTo(16), () -> assertThat(waitStrategy.computeSleepTime(new IndexBlockRetryAttempt(5))).isEqualTo(32));
}
Also used : WaitStrategy(com.github.rholder.retry.WaitStrategy) Test(org.junit.jupiter.api.Test)

Aggregations

WaitStrategy (com.github.rholder.retry.WaitStrategy)3 Test (org.junit.jupiter.api.Test)2 StopStrategy (com.github.rholder.retry.StopStrategy)1 CcmException (com.sequenceiq.cloudbreak.ccm.exception.CcmException)1