use of java.util.concurrent.CompletionException in project flink by apache.
the class LocalFileSystemTest method testConcurrentMkdirs.
@Test
public void testConcurrentMkdirs() throws Exception {
final FileSystem fs = FileSystem.getLocalFileSystem();
final File root = temporaryFolder.getRoot();
final int directoryDepth = 10;
final int concurrentOperations = 10;
final Collection<File> targetDirectories = createTargetDirectories(root, directoryDepth, concurrentOperations);
final ExecutorService executor = Executors.newFixedThreadPool(concurrentOperations);
final CyclicBarrier cyclicBarrier = new CyclicBarrier(concurrentOperations);
try {
final Collection<CompletableFuture<Void>> mkdirsFutures = new ArrayList<>(concurrentOperations);
for (File targetDirectory : targetDirectories) {
final CompletableFuture<Void> mkdirsFuture = CompletableFuture.runAsync(() -> {
try {
cyclicBarrier.await();
assertThat(fs.mkdirs(Path.fromLocalFile(targetDirectory)), is(true));
} catch (Exception e) {
throw new CompletionException(e);
}
}, executor);
mkdirsFutures.add(mkdirsFuture);
}
final CompletableFuture<Void> allFutures = CompletableFuture.allOf(mkdirsFutures.toArray(new CompletableFuture[concurrentOperations]));
allFutures.get();
} finally {
final long timeout = 10000L;
ExecutorUtils.gracefulShutdown(timeout, TimeUnit.MILLISECONDS, executor);
}
}
use of java.util.concurrent.CompletionException in project flink by apache.
the class JarUploadHandler method handleRequest.
@Override
@VisibleForTesting
public CompletableFuture<JarUploadResponseBody> handleRequest(@Nonnull final HandlerRequest<EmptyRequestBody> request, @Nonnull final RestfulGateway gateway) throws RestHandlerException {
Collection<File> uploadedFiles = request.getUploadedFiles();
if (uploadedFiles.size() != 1) {
throw new RestHandlerException("Exactly 1 file must be sent, received " + uploadedFiles.size() + '.', HttpResponseStatus.BAD_REQUEST);
}
final Path fileUpload = uploadedFiles.iterator().next().toPath();
return CompletableFuture.supplyAsync(() -> {
if (!fileUpload.getFileName().toString().endsWith(".jar")) {
throw new CompletionException(new RestHandlerException("Only Jar files are allowed.", HttpResponseStatus.BAD_REQUEST));
} else {
final Path destination = jarDir.resolve(UUID.randomUUID() + "_" + fileUpload.getFileName());
try {
Files.move(fileUpload, destination);
} catch (IOException e) {
throw new CompletionException(new RestHandlerException(String.format("Could not move uploaded jar file [%s] to [%s].", fileUpload, destination), HttpResponseStatus.INTERNAL_SERVER_ERROR, e));
}
return new JarUploadResponseBody(destination.normalize().toString());
}
}, executor);
}
use of java.util.concurrent.CompletionException in project flink by apache.
the class FutureUtilsTest method testStopAtNonRetryableException.
/**
* Test that {@link FutureUtils#retry} should stop at non-retryable exception.
*/
@Test
public void testStopAtNonRetryableException() {
final int retries = 10;
final int notRetry = 3;
final AtomicInteger atomicInteger = new AtomicInteger(0);
final FlinkRuntimeException nonRetryableException = new FlinkRuntimeException("Non-retryable exception");
CompletableFuture<Boolean> retryFuture = FutureUtils.retry(() -> CompletableFuture.supplyAsync(() -> {
if (atomicInteger.incrementAndGet() == notRetry) {
// throw non-retryable exception
throw new CompletionException(nonRetryableException);
} else {
throw new CompletionException(new FlinkException("Test exception"));
}
}, TestingUtils.defaultExecutor()), retries, throwable -> ExceptionUtils.findThrowable(throwable, FlinkException.class).isPresent(), TestingUtils.defaultExecutor());
try {
retryFuture.get();
fail("Exception should be thrown.");
} catch (Exception ex) {
assertThat(ex, FlinkMatchers.containsCause(nonRetryableException));
}
assertThat(atomicInteger.get(), is(notRetry));
}
use of java.util.concurrent.CompletionException in project flink by apache.
the class SessionDispatcherLeaderProcess method filterOutDuplicateJobSubmissionException.
private Void filterOutDuplicateJobSubmissionException(Throwable throwable) {
final Throwable strippedException = ExceptionUtils.stripCompletionException(throwable);
if (strippedException instanceof DuplicateJobSubmissionException) {
final DuplicateJobSubmissionException duplicateJobSubmissionException = (DuplicateJobSubmissionException) strippedException;
log.debug("Ignore recovered job {} because the job is currently being executed.", duplicateJobSubmissionException.getJobID(), duplicateJobSubmissionException);
return null;
} else {
throw new CompletionException(throwable);
}
}
use of java.util.concurrent.CompletionException in project flink by apache.
the class MasterHooks method triggerHook.
// ------------------------------------------------------------------------
// checkpoint triggering
// ------------------------------------------------------------------------
/**
* Trigger master hook and return a completable future with state.
*
* @param hook The master hook given
* @param checkpointId The checkpoint ID of the triggering checkpoint
* @param timestamp The (informational) timestamp for the triggering checkpoint
* @param executor An executor that can be used for asynchronous I/O calls
* @param <T> The type of data produced by the hook
* @return the completable future with state
*/
public static <T> CompletableFuture<MasterState> triggerHook(MasterTriggerRestoreHook<T> hook, long checkpointId, long timestamp, Executor executor) {
final String id = hook.getIdentifier();
final SimpleVersionedSerializer<T> serializer = hook.createCheckpointDataSerializer();
try {
// call the hook!
final CompletableFuture<T> resultFuture = hook.triggerCheckpoint(checkpointId, timestamp, executor);
if (resultFuture == null) {
return CompletableFuture.completedFuture(null);
}
return resultFuture.thenApply(result -> {
// if the result of the future is not null, return it as state
if (result == null) {
return null;
} else if (serializer != null) {
try {
final int version = serializer.getVersion();
final byte[] bytes = serializer.serialize(result);
return new MasterState(id, bytes, version);
} catch (Throwable t) {
ExceptionUtils.rethrowIfFatalErrorOrOOM(t);
throw new CompletionException(new FlinkException("Failed to serialize state of master hook '" + id + '\'', t));
}
} else {
throw new CompletionException(new FlinkException("Checkpoint hook '" + id + " is stateful but creates no serializer"));
}
}).exceptionally((throwable) -> {
throw new CompletionException(new FlinkException("Checkpoint master hook '" + id + "' produced an exception", throwable.getCause()));
});
} catch (Throwable t) {
return FutureUtils.completedExceptionally(new FlinkException("Error while triggering checkpoint master hook '" + id + '\'', t));
}
}
Aggregations