Search in sources :

Example 46 with CompletionException

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);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) CyclicBarrier(java.util.concurrent.CyclicBarrier) CompletableFuture(java.util.concurrent.CompletableFuture) FileSystem(org.apache.flink.core.fs.FileSystem) CompletionException(java.util.concurrent.CompletionException) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File) Test(org.junit.Test)

Example 47 with CompletionException

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);
}
Also used : Path(java.nio.file.Path) CompletionException(java.util.concurrent.CompletionException) IOException(java.io.IOException) File(java.io.File) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting)

Example 48 with CompletionException

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));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletionException(java.util.concurrent.CompletionException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FlinkException(org.apache.flink.util.FlinkException) FlinkException(org.apache.flink.util.FlinkException) TimeoutException(java.util.concurrent.TimeoutException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) CompletionException(java.util.concurrent.CompletionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 49 with CompletionException

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);
    }
}
Also used : CompletionException(java.util.concurrent.CompletionException) DuplicateJobSubmissionException(org.apache.flink.runtime.client.DuplicateJobSubmissionException)

Example 50 with CompletionException

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));
    }
}
Also used : MasterState(org.apache.flink.runtime.checkpoint.MasterState) FlinkException(org.apache.flink.util.FlinkException) Logger(org.slf4j.Logger) Tuple2(org.apache.flink.api.java.tuple.Tuple2) Executor(java.util.concurrent.Executor) Collection(java.util.Collection) ExceptionUtils(org.apache.flink.util.ExceptionUtils) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Preconditions(org.apache.flink.util.Preconditions) ArrayList(java.util.ArrayList) MasterTriggerRestoreHook(org.apache.flink.runtime.checkpoint.MasterTriggerRestoreHook) LinkedHashMap(java.util.LinkedHashMap) LambdaUtil(org.apache.flink.util.LambdaUtil) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) SimpleVersionedSerializer(org.apache.flink.core.io.SimpleVersionedSerializer) Map(java.util.Map) Nullable(javax.annotation.Nullable) MasterState(org.apache.flink.runtime.checkpoint.MasterState) CompletionException(java.util.concurrent.CompletionException) FlinkException(org.apache.flink.util.FlinkException)

Aggregations

CompletionException (java.util.concurrent.CompletionException)199 Test (org.junit.Test)80 CompletableFuture (java.util.concurrent.CompletableFuture)62 List (java.util.List)52 ArrayList (java.util.ArrayList)51 IOException (java.io.IOException)45 Map (java.util.Map)39 Collection (java.util.Collection)31 ExecutionException (java.util.concurrent.ExecutionException)31 HashMap (java.util.HashMap)30 Collections (java.util.Collections)24 TimeUnit (java.util.concurrent.TimeUnit)22 Collectors (java.util.stream.Collectors)22 FlinkException (org.apache.flink.util.FlinkException)22 Before (org.junit.Before)21 Duration (java.time.Duration)19 Arrays (java.util.Arrays)19 BeforeClass (org.junit.BeforeClass)19 ExecutorService (java.util.concurrent.ExecutorService)18 Nonnull (javax.annotation.Nonnull)17