Search in sources :

Example 1 with RpcServer

use of org.apache.flink.runtime.rpc.RpcServer in project flink by apache.

the class AkkaRpcService method startServer.

@Override
public <C extends RpcEndpoint & RpcGateway> RpcServer startServer(C rpcEndpoint) {
    checkNotNull(rpcEndpoint, "rpc endpoint");
    final SupervisorActor.ActorRegistration actorRegistration = registerAkkaRpcActor(rpcEndpoint);
    final ActorRef actorRef = actorRegistration.getActorRef();
    final CompletableFuture<Void> actorTerminationFuture = actorRegistration.getTerminationFuture();
    LOG.info("Starting RPC endpoint for {} at {} .", rpcEndpoint.getClass().getName(), actorRef.path());
    final String akkaAddress = AkkaUtils.getAkkaURL(actorSystem, actorRef);
    final String hostname;
    Option<String> host = actorRef.path().address().host();
    if (host.isEmpty()) {
        hostname = "localhost";
    } else {
        hostname = host.get();
    }
    Set<Class<?>> implementedRpcGateways = new HashSet<>(RpcUtils.extractImplementedRpcGateways(rpcEndpoint.getClass()));
    implementedRpcGateways.add(RpcServer.class);
    implementedRpcGateways.add(AkkaBasedEndpoint.class);
    final InvocationHandler akkaInvocationHandler;
    if (rpcEndpoint instanceof FencedRpcEndpoint) {
        // a FencedRpcEndpoint needs a FencedAkkaInvocationHandler
        akkaInvocationHandler = new FencedAkkaInvocationHandler<>(akkaAddress, hostname, actorRef, configuration.getTimeout(), configuration.getMaximumFramesize(), actorTerminationFuture, ((FencedRpcEndpoint<?>) rpcEndpoint)::getFencingToken, captureAskCallstacks, flinkClassLoader);
        implementedRpcGateways.add(FencedMainThreadExecutable.class);
    } else {
        akkaInvocationHandler = new AkkaInvocationHandler(akkaAddress, hostname, actorRef, configuration.getTimeout(), configuration.getMaximumFramesize(), actorTerminationFuture, captureAskCallstacks, flinkClassLoader);
    }
    // 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") RpcServer server = (RpcServer) Proxy.newProxyInstance(classLoader, implementedRpcGateways.toArray(new Class<?>[implementedRpcGateways.size()]), akkaInvocationHandler);
    return server;
}
Also used : ActorRef(akka.actor.ActorRef) InvocationHandler(java.lang.reflect.InvocationHandler) RpcServer(org.apache.flink.runtime.rpc.RpcServer) 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) FencedRpcEndpoint(org.apache.flink.runtime.rpc.FencedRpcEndpoint) HashSet(java.util.HashSet)

Example 2 with RpcServer

use of org.apache.flink.runtime.rpc.RpcServer in project flink by apache.

the class AkkaRpcService method fenceRpcServer.

@Override
public <F extends Serializable> RpcServer fenceRpcServer(RpcServer rpcServer, F fencingToken) {
    if (rpcServer instanceof AkkaBasedEndpoint) {
        InvocationHandler fencedInvocationHandler = new FencedAkkaInvocationHandler<>(rpcServer.getAddress(), rpcServer.getHostname(), ((AkkaBasedEndpoint) rpcServer).getActorRef(), configuration.getTimeout(), configuration.getMaximumFramesize(), null, () -> fencingToken, captureAskCallstacks, flinkClassLoader);
        // 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();
        return (RpcServer) Proxy.newProxyInstance(classLoader, new Class<?>[] { RpcServer.class, AkkaBasedEndpoint.class }, fencedInvocationHandler);
    } else {
        throw new RuntimeException("The given RpcServer must implement the AkkaGateway in order to fence it.");
    }
}
Also used : AkkaRpcRuntimeException(org.apache.flink.runtime.rpc.akka.exceptions.AkkaRpcRuntimeException) RpcServer(org.apache.flink.runtime.rpc.RpcServer) 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)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)2 ClassLoadingUtils.guardCompletionWithContextClassLoader (org.apache.flink.runtime.concurrent.akka.ClassLoadingUtils.guardCompletionWithContextClassLoader)2 ClassLoadingUtils.runWithContextClassLoader (org.apache.flink.runtime.concurrent.akka.ClassLoadingUtils.runWithContextClassLoader)2 ClassLoadingUtils.withContextClassLoader (org.apache.flink.runtime.concurrent.akka.ClassLoadingUtils.withContextClassLoader)2 RpcServer (org.apache.flink.runtime.rpc.RpcServer)2 ActorRef (akka.actor.ActorRef)1 HashSet (java.util.HashSet)1 FencedRpcEndpoint (org.apache.flink.runtime.rpc.FencedRpcEndpoint)1 AkkaRpcRuntimeException (org.apache.flink.runtime.rpc.akka.exceptions.AkkaRpcRuntimeException)1