private CompletableFuture<Void> createScope(final String scopeName) { return Futures.toVoid(Retry.indefinitelyWithExpBackoff(DELAY, MULTIPLIER, MAX_DELAY, e -> log.warn("Error creating event processor scope " + scopeName, e)) .runAsync(() -> controller.createScope(scopeName) .thenAccept(x -> log.info("Created controller scope {}", scopeName)), executor)); }
private CompletableFuture<Void> createStream(String scope, String streamName, final StreamConfiguration streamConfig) { return Futures.toVoid(Retry.indefinitelyWithExpBackoff(DELAY, MULTIPLIER, MAX_DELAY, e -> log.warn("Error creating event processor stream " + streamName, e)) .runAsync(() -> controller.createStream(scope, streamName, streamConfig) .thenAccept(x -> log.info("Created stream {}/{}", scope, streamName)), executor)); }
private CompletableFuture<Void> retryIndefinitelyThenComplete(Supplier<CompletableFuture<Void>> futureSupplier, CompletableFuture<Void> toComplete, Throwable e) { String failureMessage = String.format("Error writing event back into stream from processor %s", getProcessorName()); return Retry.indefinitelyWithExpBackoff(failureMessage) .runAsync(futureSupplier, executor) .thenRun(() -> { if (e != null) { toComplete.completeExceptionally(e); } else { toComplete.complete(null); } }); } }
retrySchedule.getMaxDelay(), t -> log.warn(writerId + " Failed to connect: ", t)) .runAsync(() -> { log.debug("Running reconnect for segment:{} writerID: {}", segmentName, writerId); if (state.isClosed() || state.isAlreadySealed()) {
log.debug("error while creating writer for requeststream {}", e); }) .runAsync(() -> { if (clientFactory.get() == null) { EventStreamClientFactory factory = null;
@Override public CompletableFuture<Void> processEvent(TestBase event) { receivedForProcessing.add(event); CompletableFuture<Void> result = new CompletableFuture<>(); Retry.withExpBackoff(100, 1, 5, 100) .retryWhen(RetryableException::isRetryable) .runAsync(() -> event.process(null), executor) .whenCompleteAsync((r, e) -> { if (e != null) { Throwable cause = Exceptions.unwrap(e); if (cause instanceof OperationDisallowedException) { Retry.indefinitelyWithExpBackoff("Error writing event back into requeststream") .runAsync(() -> writer.write(event), executor) .thenAccept(v -> result.completeExceptionally(cause)); } else { result.completeExceptionally(cause); } } else { result.complete(r); } }, executor); return result; } }
private CompletableFuture<Void> checkTxnStatus(Transaction<String> txn, int eventsWritten) { testState.committingTxn.add(txn.getTxnId()); return Retry.indefinitelyWithExpBackoff("Txn did not get committed").runAsync(() -> { Transaction.Status status = txn.checkStatus(); log.debug("Txn id {} status is {}", txn.getTxnId(), status); if (status.equals(Transaction.Status.COMMITTED)) { testState.incrementTotalWrittenEvents(eventsWritten); testState.committingTxn.remove(txn.getTxnId()); log.info("Event write count: {}", testState.getEventWrittenCount()); } else if (status.equals(Transaction.Status.ABORTED)) { log.debug("Transaction with id: {} aborted", txn.getTxnId()); testState.abortedTxn.add(txn.getTxnId()); } else { throw new TxnNotCompleteException(); } return CompletableFuture.completedFuture(null); }, executorService); }
void cleanUp(String scope, String stream, ReaderGroupManager readerGroupManager, String readerGroupName) throws InterruptedException, ExecutionException { CompletableFuture<Boolean> sealStreamStatus = Retry.indefinitelyWithExpBackoff("Failed to seal stream. retrying ...") .runAsync(() -> controller.sealStream(scope, stream), executorService); log.info("Sealing stream {}", stream); assertTrue(sealStreamStatus.get()); CompletableFuture<Boolean> deleteStreamStatus = controller.deleteStream(scope, stream); log.info("Deleting stream {}", stream); assertTrue(deleteStreamStatus.get()); log.info("Deleting readergroup {}", readerGroupName); readerGroupManager.deleteReaderGroup(readerGroupName); CompletableFuture<Boolean> deleteScopeStatus = controller.deleteScope(scope); log.info("Deleting scope {}", scope); assertTrue(deleteScopeStatus.get()); }
public static <U> CompletableFuture<U> indefiniteRetries(Supplier<CompletableFuture<U>> futureSupplier, ScheduledExecutorService executor) { return INDEFINITELY.runAsync(futureSupplier, executor); }
@Test public void retryIndefiniteTest() throws ExecutionException, InterruptedException { AtomicInteger i = new AtomicInteger(0); Retry.indefinitelyWithExpBackoff(10, 10, 10, e -> i.getAndIncrement()) .runAsync(() -> CompletableFuture.runAsync(() -> { if (i.get() < 10) { throw new RuntimeException("test"); } }), Executors.newSingleThreadScheduledExecutor()).get(); assert i.get() == 10; }
public static <U> CompletableFuture<U> withIndefiniteRetriesAsync(Supplier<CompletableFuture<U>> futureSupplier, Consumer<Throwable> exceptionConsumer, ScheduledExecutorService executor) { return Retry .indefinitelyWithExpBackoff(100, 2, 10000, exceptionConsumer) .runAsync(futureSupplier, executor); }