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);
}
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();
}
}
}
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());
}
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();
}
}
}
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);
}
}
Aggregations