use of java.util.concurrent.CompletionException in project incubator-ratis by apache.
the class FileStoreClient method sendAsync.
static CompletableFuture<ByteString> sendAsync(ByteString request, Function<Message, CompletableFuture<RaftClientReply>> sendFunction) {
return sendFunction.apply(() -> request).thenApply(reply -> {
final StateMachineException sme = reply.getStateMachineException();
if (sme != null) {
throw new CompletionException("Failed to send request " + request, sme);
}
Preconditions.assertTrue(reply.isSuccess(), () -> "Failed " + request + ", reply=" + reply);
return reply.getMessage().getContent();
});
}
use of java.util.concurrent.CompletionException in project pravega by pravega.
the class RetryTests method retryFutureTests.
@Test
public void retryFutureTests() {
ScheduledExecutorService executorService = ExecutorServiceHelpers.newScheduledThreadPool(5, "testpool");
// 1. series of retryable exceptions followed by a failure
begin = Instant.now();
CompletableFuture<Integer> result = retryFuture(uniformDelay, 1, maxLoops, maxDelay, true, executorService);
assertEquals(result.join().intValue(), expectedResult);
end = Instant.now();
duration = end.toEpochMilli() - begin.toEpochMilli();
log.debug("Expected duration = {}", expectedDurationUniform);
log.debug("Actual duration = {}", duration);
assertTrue(duration >= expectedDurationUniform);
// 2, series of retryable exceptions followed by a non-retryable failure
begin = Instant.now();
result = retryFuture(uniformDelay, 1, maxLoops, maxDelay, false, executorService);
try {
result.join();
} catch (CompletionException ce) {
end = Instant.now();
duration = end.toEpochMilli() - begin.toEpochMilli();
log.debug("Expected duration = {}", expectedDurationUniform);
log.debug("Actual duration = {}", duration);
assertTrue(duration >= expectedDurationUniform);
assertTrue(ce.getCause() instanceof NonretryableException);
assertEquals(accumulator.get(), expectedResult);
}
// 3. exponential backoff
begin = Instant.now();
result = retryFuture(exponentialInitialDelay, multiplier, maxLoops, maxDelay, true, executorService);
assertEquals(result.join().intValue(), expectedResult);
end = Instant.now();
duration = end.toEpochMilli() - begin.toEpochMilli();
log.debug("Expected duration = {}", expectedDurationExponential);
log.debug("Actual duration = {}", duration);
assertTrue(duration >= expectedDurationExponential);
// 4. Exhaust retries
begin = Instant.now();
result = retryFuture(uniformDelay, 1, maxLoops - 1, maxDelay, true, executorService);
try {
result.join();
} catch (Exception e) {
end = Instant.now();
duration = end.toEpochMilli() - begin.toEpochMilli();
log.debug("Expected duration = {}", expectedDurationUniform - uniformDelay);
log.debug("Actual duration = {}", duration);
assertTrue(duration >= expectedDurationUniform - uniformDelay);
assertTrue(e instanceof CompletionException);
assertTrue(e.getCause() instanceof RetriesExhaustedException);
assertTrue(e.getCause().getCause() instanceof CompletionException);
assertTrue(e.getCause().getCause().getCause() instanceof RetryableException);
}
}
use of java.util.concurrent.CompletionException in project pravega by pravega.
the class ControllerEventProcessors method handleOrphanedReaders.
private CompletableFuture<Void> handleOrphanedReaders(final EventProcessorGroup<? extends ControllerEvent> group, final Supplier<Set<String>> processes) {
return withRetriesAsync(() -> CompletableFuture.supplyAsync(() -> {
try {
return group.getProcesses();
} catch (CheckpointStoreException e) {
if (e.getType().equals(CheckpointStoreException.Type.NoNode)) {
return Collections.<String>emptySet();
}
throw new CompletionException(e);
}
}, executor), RETRYABLE_PREDICATE, Integer.MAX_VALUE, executor).thenComposeAsync(groupProcesses -> withRetriesAsync(() -> CompletableFuture.supplyAsync(() -> {
try {
return new ImmutablePair<>(processes.get(), groupProcesses);
} catch (Exception e) {
log.error(String.format("Error fetching current processes%s", group.toString()), e);
throw new CompletionException(e);
}
}, executor), RETRYABLE_PREDICATE, Integer.MAX_VALUE, executor)).thenComposeAsync(pair -> {
Set<String> activeProcesses = pair.getLeft();
Set<String> registeredProcesses = pair.getRight();
if (registeredProcesses == null || registeredProcesses.isEmpty()) {
return CompletableFuture.completedFuture(null);
}
if (activeProcesses != null) {
registeredProcesses.removeAll(activeProcesses);
}
List<CompletableFuture<Void>> futureList = new ArrayList<>();
for (String process : registeredProcesses) {
futureList.add(withRetriesAsync(() -> CompletableFuture.runAsync(() -> {
try {
group.notifyProcessFailure(process);
} catch (CheckpointStoreException e) {
log.error(String.format("Error notifying failure of process=%s in event processor group %s", process, group.toString()), e);
throw new CompletionException(e);
}
}, executor), RETRYABLE_PREDICATE, Integer.MAX_VALUE, executor));
}
return Futures.allOf(futureList);
});
}
use of java.util.concurrent.CompletionException in project pravega by pravega.
the class DeleteStreamTask method execute.
@Override
public CompletableFuture<Void> execute(final DeleteStreamEvent request) {
final OperationContext context = streamMetadataStore.createContext(request.getScope(), request.getStream());
String scope = request.getScope();
String stream = request.getStream();
return streamMetadataStore.isSealed(scope, stream, context, executor).thenComposeAsync(sealed -> {
if (!sealed) {
log.warn("{}/{} stream not sealed", scope, stream);
return Futures.failedFuture(new RuntimeException("Stream not sealed"));
}
return notifyAndDelete(context, scope, stream);
}, executor).exceptionally(e -> {
if (e instanceof StoreException.DataNotFoundException) {
return null;
}
log.error("{}/{} stream delete workflow threw exception.", scope, stream, e);
throw new CompletionException(e);
});
}
use of java.util.concurrent.CompletionException in project pravega by pravega.
the class StreamSegmentMapperTests method testCreateTransactionAlreadyExists.
/**
* Tests the ability of the StreamSegmentMapper to create a new Transaction if the Transaction already exists (partially
* or fully).
*/
@Test
public void testCreateTransactionAlreadyExists() {
final String parentSegmentName = "NewSegment";
final UUID txnId = UUID.randomUUID();
final String txnName = StreamSegmentNameUtils.getTransactionNameFromId(parentSegmentName, txnId);
testCreateAlreadyExists(txnName, (mapper, attributes) -> mapper.createNewStreamSegment(parentSegmentName, null, TIMEOUT).exceptionally(ex -> {
if (Exceptions.unwrap(ex) instanceof StreamSegmentExistsException) {
return null;
}
throw new CompletionException(ex);
}).thenCompose(v -> mapper.createNewTransactionStreamSegment(parentSegmentName, txnId, attributes, TIMEOUT)));
}
Aggregations