Search in sources :

Example 11 with ExecutionException

use of java.util.concurrent.ExecutionException in project flink by apache.

the class FlinkFuture method thenCombineAsync.

@Override
public <U, R> Future<R> thenCombineAsync(final Future<U> other, final BiFunction<? super T, ? super U, ? extends R> biFunction, final Executor executor) {
    Preconditions.checkNotNull(other);
    Preconditions.checkNotNull(biFunction);
    Preconditions.checkNotNull(executor);
    final ExecutionContext executionContext = createExecutionContext(executor);
    final scala.concurrent.Future<U> thatScalaFuture;
    if (other instanceof FlinkFuture) {
        thatScalaFuture = ((FlinkFuture<U>) other).scalaFuture;
    } else {
        thatScalaFuture = Futures.future(new Callable<U>() {

            @Override
            public U call() throws Exception {
                try {
                    return other.get();
                } catch (ExecutionException e) {
                    // unwrap the execution exception if the cause is an Exception
                    if (e.getCause() instanceof Exception) {
                        throw (Exception) e.getCause();
                    } else {
                        // it's an error or a throwable which we have to wrap for the moment
                        throw new FlinkFuture.ThrowableWrapperException(e.getCause());
                    }
                }
            }
        }, executionContext);
    }
    scala.concurrent.Future<R> result = scalaFuture.zip(thatScalaFuture).map(new Mapper<Tuple2<T, U>, R>() {

        @Override
        public R apply(Tuple2<T, U> tuple2) {
            return biFunction.apply(tuple2._1, tuple2._2);
        }
    }, executionContext);
    return new FlinkFuture<>(result);
}
Also used : Callable(java.util.concurrent.Callable) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) ExecutionContext(scala.concurrent.ExecutionContext) Tuple2(scala.Tuple2) ExecutionException(java.util.concurrent.ExecutionException)

Example 12 with ExecutionException

use of java.util.concurrent.ExecutionException in project flink by apache.

the class RpcConnectionTest method testConnectFailure.

@Test
public void testConnectFailure() {
    ActorSystem actorSystem = null;
    RpcService rpcService = null;
    try {
        actorSystem = AkkaUtils.createActorSystem(new Configuration(), Option.apply(new Tuple2<String, Object>("localhost", 0)));
        // we start the RPC service with a very long timeout to ensure that the test
        // can only pass if the connection problem is not recognized merely via a timeout
        rpcService = new AkkaRpcService(actorSystem, Time.of(10000000, TimeUnit.SECONDS));
        Future<TaskExecutorGateway> future = rpcService.connect("foo.bar.com.test.invalid", TaskExecutorGateway.class);
        future.get(10000000, TimeUnit.SECONDS);
        fail("should never complete normally");
    } catch (TimeoutException e) {
        fail("should not fail with a generic timeout exception");
    } catch (ExecutionException e) {
        // that is what we want
        assertTrue(e.getCause() instanceof RpcConnectionException);
        assertTrue("wrong error message", e.getCause().getMessage().contains("foo.bar.com.test.invalid"));
    } catch (Throwable t) {
        fail("wrong exception: " + t);
    } finally {
        if (rpcService != null) {
            rpcService.stopService();
        }
        if (actorSystem != null) {
            actorSystem.shutdown();
        }
    }
}
Also used : ActorSystem(akka.actor.ActorSystem) Configuration(org.apache.flink.configuration.Configuration) TaskExecutorGateway(org.apache.flink.runtime.taskexecutor.TaskExecutorGateway) RpcConnectionException(org.apache.flink.runtime.rpc.exceptions.RpcConnectionException) AkkaRpcService(org.apache.flink.runtime.rpc.akka.AkkaRpcService) AkkaRpcService(org.apache.flink.runtime.rpc.akka.AkkaRpcService) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 13 with ExecutionException

use of java.util.concurrent.ExecutionException in project flink by apache.

the class AkkaRpcServiceTest method testExecuteCallable.

/**
	 * Tests that the {@link AkkaRpcService} can execute callables and returns their result as
	 * a {@link Future}.
	 */
@Test
public void testExecuteCallable() throws InterruptedException, ExecutionException, TimeoutException {
    final OneShotLatch latch = new OneShotLatch();
    final int expected = 42;
    Future<Integer> result = akkaRpcService.execute(new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            latch.trigger();
            return expected;
        }
    });
    int actual = result.get(30L, TimeUnit.SECONDS);
    assertEquals(expected, actual);
    assertTrue(latch.isTriggered());
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 14 with ExecutionException

use of java.util.concurrent.ExecutionException in project flink by apache.

the class StateBackendTestBase method testAsyncSnapshotCancellation.

@Test
public void testAsyncSnapshotCancellation() throws Exception {
    OneShotLatch blocker = new OneShotLatch();
    OneShotLatch waiter = new OneShotLatch();
    BlockerCheckpointStreamFactory streamFactory = new BlockerCheckpointStreamFactory(1024 * 1024);
    streamFactory.setWaiterLatch(waiter);
    streamFactory.setBlockerLatch(blocker);
    streamFactory.setAfterNumberInvocations(100);
    AbstractKeyedStateBackend<Integer> backend = null;
    try {
        backend = createKeyedBackend(IntSerializer.INSTANCE);
        if (!backend.supportsAsynchronousSnapshots()) {
            return;
        }
        InternalValueState<VoidNamespace, Integer> valueState = backend.createValueState(VoidNamespaceSerializer.INSTANCE, new ValueStateDescriptor<>("test", IntSerializer.INSTANCE));
        valueState.setCurrentNamespace(VoidNamespace.INSTANCE);
        for (int i = 0; i < 10; ++i) {
            backend.setCurrentKey(i);
            valueState.update(i);
        }
        RunnableFuture<KeyGroupsStateHandle> snapshot = backend.snapshot(0L, 0L, streamFactory, CheckpointOptions.forFullCheckpoint());
        Thread runner = new Thread(snapshot);
        runner.start();
        // wait until the code reached some stream read
        waiter.await();
        // close the backend to see if the close is propagated to the stream
        backend.close();
        //unblock the stream so that it can run into the IOException
        blocker.trigger();
        //dispose the backend
        backend.dispose();
        runner.join();
        try {
            snapshot.get();
            fail("Close was not propagated.");
        } catch (ExecutionException ex) {
        //ignore
        }
    } finally {
        if (null != backend) {
            IOUtils.closeQuietly(backend);
            backend.dispose();
        }
    }
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) BlockerCheckpointStreamFactory(org.apache.flink.runtime.util.BlockerCheckpointStreamFactory) ExecutionException(java.util.concurrent.ExecutionException) CheckedThread(org.apache.flink.core.testutils.CheckedThread) Test(org.junit.Test)

Example 15 with ExecutionException

use of java.util.concurrent.ExecutionException in project flink by apache.

the class DirectExecutorService method invokeAny.

@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
    long end = System.currentTimeMillis() + unit.toMillis(timeout);
    Exception exception = null;
    Iterator<? extends Callable<T>> iterator = tasks.iterator();
    while (end > System.currentTimeMillis() && iterator.hasNext()) {
        Callable<T> callable = iterator.next();
        try {
            return callable.call();
        } catch (Exception e) {
            // ignore exception and try next
            exception = e;
        }
    }
    if (iterator.hasNext()) {
        throw new TimeoutException("Could not finish execution of tasks within time.");
    } else {
        throw new ExecutionException("No tasks finished successfully.", exception);
    }
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ExecutionException (java.util.concurrent.ExecutionException)1341 IOException (java.io.IOException)367 Test (org.junit.Test)335 TimeoutException (java.util.concurrent.TimeoutException)258 ArrayList (java.util.ArrayList)237 Future (java.util.concurrent.Future)218 ExecutorService (java.util.concurrent.ExecutorService)152 CountDownLatch (java.util.concurrent.CountDownLatch)103 List (java.util.List)98 CancellationException (java.util.concurrent.CancellationException)98 Callable (java.util.concurrent.Callable)97 Test (org.testng.annotations.Test)78 HashMap (java.util.HashMap)69 Map (java.util.Map)65 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)64 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)63 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)56 ParallelTest (com.hazelcast.test.annotation.ParallelTest)47 QuickTest (com.hazelcast.test.annotation.QuickTest)47 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)46