Search in sources :

Example 1 with ExecutionContext

use of scala.concurrent.ExecutionContext 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 2 with ExecutionContext

use of scala.concurrent.ExecutionContext in project flink by apache.

the class RobustActorSystem method create.

private static RobustActorSystem create(String name, ActorSystemSetup setup, Option<Thread.UncaughtExceptionHandler> uncaughtExceptionHandler) {
    final Optional<BootstrapSetup> bootstrapSettings = setup.get(BootstrapSetup.class);
    final ClassLoader classLoader = RobustActorSystem.class.getClassLoader();
    final Config appConfig = bootstrapSettings.map(BootstrapSetup::config).flatMap(RobustActorSystem::toJavaOptional).orElseGet(() -> ConfigFactory.load(classLoader));
    final Option<ExecutionContext> defaultEC = toScalaOption(bootstrapSettings.map(BootstrapSetup::defaultExecutionContext).flatMap(RobustActorSystem::toJavaOptional));
    final RobustActorSystem robustActorSystem = new RobustActorSystem(name, appConfig, classLoader, defaultEC, setup) {

        @Override
        public Thread.UncaughtExceptionHandler uncaughtExceptionHandler() {
            return uncaughtExceptionHandler.getOrElse(super::uncaughtExceptionHandler);
        }
    };
    robustActorSystem.start();
    return robustActorSystem;
}
Also used : ExecutionContext(scala.concurrent.ExecutionContext) Config(com.typesafe.config.Config) BootstrapSetup(akka.actor.BootstrapSetup)

Example 3 with ExecutionContext

use of scala.concurrent.ExecutionContext in project flink by apache.

the class FlinkFuture method thenComposeAsync.

@Override
public <R> Future<R> thenComposeAsync(final ApplyFunction<? super T, ? extends Future<R>> applyFunction, Executor executor) {
    Preconditions.checkNotNull(scalaFuture);
    Preconditions.checkNotNull(applyFunction);
    Preconditions.checkNotNull(executor);
    final ExecutionContext executionContext = createExecutionContext(executor);
    scala.concurrent.Future<R> flatMappedFuture = scalaFuture.flatMap(new Mapper<T, scala.concurrent.Future<R>>() {

        @Override
        public scala.concurrent.Future<R> apply(T value) {
            final Future<? extends R> future = applyFunction.apply(value);
            if (future instanceof FlinkFuture) {
                @SuppressWarnings("unchecked") FlinkFuture<R> flinkFuture = (FlinkFuture<R>) future;
                return flinkFuture.scalaFuture;
            } else {
                return Futures.future(new Callable<R>() {

                    @Override
                    public R call() throws Exception {
                        try {
                            return future.get();
                        } catch (ExecutionException e) {
                            // unwrap the execution exception if it's not a throwable
                            if (e.getCause() instanceof Exception) {
                                throw (Exception) e.getCause();
                            } else {
                                throw new FlinkFuture.ThrowableWrapperException(e.getCause());
                            }
                        }
                    }
                }, executionContext);
            }
        }
    }, executionContext);
    return new FlinkFuture<>(flatMappedFuture);
}
Also used : Callable(java.util.concurrent.Callable) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) ExecutionContext(scala.concurrent.ExecutionContext) CompletableFuture(org.apache.flink.runtime.concurrent.CompletableFuture) Future(org.apache.flink.runtime.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with ExecutionContext

use of scala.concurrent.ExecutionContext in project flink by apache.

the class FlinkFuture method handleAsync.

@Override
public <R> Future<R> handleAsync(final BiFunction<? super T, Throwable, ? extends R> biFunction, Executor executor) {
    Preconditions.checkNotNull(scalaFuture);
    Preconditions.checkNotNull(biFunction);
    Preconditions.checkNotNull(executor);
    final ExecutionContext executionContext = createExecutionContext(executor);
    final CompletableFuture<R> resultFuture = new FlinkCompletableFuture<>();
    scalaFuture.onComplete(new OnComplete<T>() {

        @Override
        public void onComplete(Throwable failure, T success) throws Throwable {
            final R result = biFunction.apply(success, failure);
            resultFuture.complete(result);
        }
    }, executionContext);
    return resultFuture;
}
Also used : ExecutionContext(scala.concurrent.ExecutionContext)

Example 5 with ExecutionContext

use of scala.concurrent.ExecutionContext in project controller by opendaylight.

the class ShardManager method onShutDown.

private void onShutDown() {
    List<Future<Boolean>> stopFutures = new ArrayList<>(localShards.size());
    for (ShardInformation info : localShards.values()) {
        if (info.getActor() != null) {
            LOG.debug("{}: Issuing gracefulStop to shard {}", persistenceId(), info.getShardId());
            FiniteDuration duration = info.getDatastoreContext().getShardRaftConfig().getElectionTimeOutInterval().$times(2);
            stopFutures.add(Patterns.gracefulStop(info.getActor(), duration, Shutdown.INSTANCE));
        }
    }
    LOG.info("Shutting down ShardManager {} - waiting on {} shards", persistenceId(), stopFutures.size());
    ExecutionContext dispatcher = new Dispatchers(context().system().dispatchers()).getDispatcher(Dispatchers.DispatcherType.Client);
    Future<Iterable<Boolean>> combinedFutures = Futures.sequence(stopFutures, dispatcher);
    combinedFutures.onComplete(new OnComplete<Iterable<Boolean>>() {

        @Override
        public void onComplete(final Throwable failure, final Iterable<Boolean> results) {
            LOG.debug("{}: All shards shutdown - sending PoisonPill to self", persistenceId());
            self().tell(PoisonPill.getInstance(), self());
            if (failure != null) {
                LOG.warn("{}: An error occurred attempting to shut down the shards", persistenceId(), failure);
            } else {
                int nfailed = 0;
                for (Boolean result : results) {
                    if (!result) {
                        nfailed++;
                    }
                }
                if (nfailed > 0) {
                    LOG.warn("{}: {} shards did not shut down gracefully", persistenceId(), nfailed);
                }
            }
        }
    }, dispatcher);
}
Also used : ArrayList(java.util.ArrayList) FiniteDuration(scala.concurrent.duration.FiniteDuration) ExecutionContext(scala.concurrent.ExecutionContext) Future(scala.concurrent.Future) Dispatchers(org.opendaylight.controller.cluster.common.actor.Dispatchers)

Aggregations

ExecutionContext (scala.concurrent.ExecutionContext)5 Callable (java.util.concurrent.Callable)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 BootstrapSetup (akka.actor.BootstrapSetup)1 Config (com.typesafe.config.Config)1 ArrayList (java.util.ArrayList)1 CompletableFuture (org.apache.flink.runtime.concurrent.CompletableFuture)1 Future (org.apache.flink.runtime.concurrent.Future)1 Dispatchers (org.opendaylight.controller.cluster.common.actor.Dispatchers)1 Tuple2 (scala.Tuple2)1 Future (scala.concurrent.Future)1 FiniteDuration (scala.concurrent.duration.FiniteDuration)1