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); } }
metrics.error(); LOG.warn("{} Couldn't upload or delete {}", logIdentifier, file, re); exceptionNotifier.notify(String.format("%s exception during upload", re.getCause().getClass()), re.getCause(), ImmutableMap.of("logIdentifier", logIdentifier, "file", file.toString(), "failedAttempts", Integer.toString(re.getNumberOfFailedAttempts()))); } catch (Exception e) { metrics.error();
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); } }
public void forPredicate(Elements elements, Duration timeout, Duration interval, Predicate<? super Elements> predicate) { Configuration configuration = elements.as(HasConfiguration.class).configure(); if (timeout == null) { timeout = configuration.defaultTimeout(); } if (interval == null) { interval = configuration.defaultInterval(); } Retryer<Elements> retrier = getRetryer(predicate, timeout, interval); try { retrier.call(Callables.returning(elements)); } catch (RetryException e) { // if interrupted, we need to propagate it with the thread marked as interrupted if (Thread.interrupted()) { Thread.currentThread().interrupt(); throw Throwables.propagate(e); } throw new TimeoutException(predicate, elements, e.getNumberOfFailedAttempts()); } catch (ExecutionException e) { throw Throwables.propagate(e.getCause()); } }
@Test public void testWithStopStrategy() throws Exception { Callable<Boolean> callable = notNullAfter5Attempts(); Retryer retryer = RetryerBuilder.newBuilder() .withStopStrategy(StopStrategies.stopAfterAttempt(3)) .retryIfResult(Objects::isNull) .build(); try { retryer.call(callable); fail("RetryException expected"); } catch (RetryException e) { assertEquals(3, e.getNumberOfFailedAttempts()); } }
@Test public void testRetryIfResult() throws Exception { Callable<Boolean> callable = notNullAfter5Attempts(); Retryer retryer = RetryerBuilder.newBuilder() .retryIfResult(Objects::isNull) .build(); assertTrue(retryer.call(callable)); callable = notNullAfter5Attempts(); retryer = RetryerBuilder.newBuilder() .retryIfResult(Objects::isNull) .withStopStrategy(StopStrategies.stopAfterAttempt(3)) .build(); try { retryer.call(callable); fail("Exception expected"); } catch (RetryException e) { assertEquals(3, e.getNumberOfFailedAttempts()); assertTrue(e.getLastFailedAttempt().hasResult()); assertNull(e.getLastFailedAttempt().getResult()); assertNull(e.getCause()); } }