Search in sources :

Example 1 with HandshakeSuccessMessage

use of org.apache.flink.runtime.rpc.messages.HandshakeSuccessMessage in project flink by apache.

the class AkkaRpcService method connectInternal.

private <C extends RpcGateway> CompletableFuture<C> connectInternal(final String address, final Class<C> clazz, Function<ActorRef, InvocationHandler> invocationHandlerFactory) {
    checkState(!stopped, "RpcService is stopped");
    LOG.debug("Try to connect to remote RPC endpoint with address {}. Returning a {} gateway.", address, clazz.getName());
    final CompletableFuture<ActorRef> actorRefFuture = resolveActorAddress(address);
    final CompletableFuture<HandshakeSuccessMessage> handshakeFuture = actorRefFuture.thenCompose((ActorRef actorRef) -> AkkaFutureUtils.toJava(Patterns.ask(actorRef, new RemoteHandshakeMessage(clazz, getVersion()), configuration.getTimeout().toMilliseconds()).<HandshakeSuccessMessage>mapTo(ClassTag$.MODULE$.<HandshakeSuccessMessage>apply(HandshakeSuccessMessage.class))));
    final CompletableFuture<C> gatewayFuture = actorRefFuture.thenCombineAsync(handshakeFuture, (ActorRef actorRef, HandshakeSuccessMessage ignored) -> {
        InvocationHandler invocationHandler = invocationHandlerFactory.apply(actorRef);
        // Rather than using the System ClassLoader directly, we derive the
        // ClassLoader from this class.
        // That works better in cases where Flink runs embedded and
        // all Flink code is loaded dynamically
        // (for example from an OSGI bundle) through a custom ClassLoader
        ClassLoader classLoader = getClass().getClassLoader();
        @SuppressWarnings("unchecked") C proxy = (C) Proxy.newProxyInstance(classLoader, new Class<?>[] { clazz }, invocationHandler);
        return proxy;
    }, actorSystem.dispatcher());
    return guardCompletionWithContextClassLoader(gatewayFuture, flinkClassLoader);
}
Also used : ActorRef(akka.actor.ActorRef) ClassLoadingUtils.runWithContextClassLoader(org.apache.flink.runtime.concurrent.akka.ClassLoadingUtils.runWithContextClassLoader) ClassLoadingUtils.guardCompletionWithContextClassLoader(org.apache.flink.runtime.concurrent.akka.ClassLoadingUtils.guardCompletionWithContextClassLoader) ClassLoadingUtils.withContextClassLoader(org.apache.flink.runtime.concurrent.akka.ClassLoadingUtils.withContextClassLoader) InvocationHandler(java.lang.reflect.InvocationHandler) HandshakeSuccessMessage(org.apache.flink.runtime.rpc.messages.HandshakeSuccessMessage) RemoteHandshakeMessage(org.apache.flink.runtime.rpc.messages.RemoteHandshakeMessage)

Aggregations

ActorRef (akka.actor.ActorRef)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 ClassLoadingUtils.guardCompletionWithContextClassLoader (org.apache.flink.runtime.concurrent.akka.ClassLoadingUtils.guardCompletionWithContextClassLoader)1 ClassLoadingUtils.runWithContextClassLoader (org.apache.flink.runtime.concurrent.akka.ClassLoadingUtils.runWithContextClassLoader)1 ClassLoadingUtils.withContextClassLoader (org.apache.flink.runtime.concurrent.akka.ClassLoadingUtils.withContextClassLoader)1 HandshakeSuccessMessage (org.apache.flink.runtime.rpc.messages.HandshakeSuccessMessage)1 RemoteHandshakeMessage (org.apache.flink.runtime.rpc.messages.RemoteHandshakeMessage)1