Search in sources :

Example 1 with RemoteRpcInvocation

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

the class AkkaInvocationHandler method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    Class<?> declaringClass = method.getDeclaringClass();
    Object result;
    if (declaringClass.equals(AkkaGateway.class) || declaringClass.equals(MainThreadExecutable.class) || declaringClass.equals(Object.class) || declaringClass.equals(StartStoppable.class) || declaringClass.equals(RpcGateway.class) || declaringClass.equals(SelfGateway.class)) {
        result = method.invoke(this, args);
    } else {
        String methodName = method.getName();
        Class<?>[] parameterTypes = method.getParameterTypes();
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        Time futureTimeout = extractRpcTimeout(parameterAnnotations, args, timeout);
        Tuple2<Class<?>[], Object[]> filteredArguments = filterArguments(parameterTypes, parameterAnnotations, args);
        RpcInvocation rpcInvocation;
        if (isLocal) {
            rpcInvocation = new LocalRpcInvocation(methodName, filteredArguments.f0, filteredArguments.f1);
        } else {
            try {
                RemoteRpcInvocation remoteRpcInvocation = new RemoteRpcInvocation(methodName, filteredArguments.f0, filteredArguments.f1);
                if (remoteRpcInvocation.getSize() > maximumFramesize) {
                    throw new IOException("The rpc invocation size exceeds the maximum akka framesize.");
                } else {
                    rpcInvocation = remoteRpcInvocation;
                }
            } catch (IOException e) {
                LOG.warn("Could not create remote rpc invocation message. Failing rpc invocation because...", e);
                throw e;
            }
        }
        Class<?> returnType = method.getReturnType();
        if (returnType.equals(Void.TYPE)) {
            rpcEndpoint.tell(rpcInvocation, ActorRef.noSender());
            result = null;
        } else if (returnType.equals(Future.class)) {
            // execute an asynchronous call
            result = new FlinkFuture<>(Patterns.ask(rpcEndpoint, rpcInvocation, futureTimeout.toMilliseconds()));
        } else {
            // execute a synchronous call
            scala.concurrent.Future<?> scalaFuture = Patterns.ask(rpcEndpoint, rpcInvocation, futureTimeout.toMilliseconds());
            Future<?> futureResult = new FlinkFuture<>(scalaFuture);
            return futureResult.get(futureTimeout.getSize(), futureTimeout.getUnit());
        }
    }
    return result;
}
Also used : LocalRpcInvocation(org.apache.flink.runtime.rpc.akka.messages.LocalRpcInvocation) RemoteRpcInvocation(org.apache.flink.runtime.rpc.akka.messages.RemoteRpcInvocation) RpcInvocation(org.apache.flink.runtime.rpc.akka.messages.RpcInvocation) LocalRpcInvocation(org.apache.flink.runtime.rpc.akka.messages.LocalRpcInvocation) Time(org.apache.flink.api.common.time.Time) IOException(java.io.IOException) MainThreadExecutable(org.apache.flink.runtime.rpc.MainThreadExecutable) FlinkFuture(org.apache.flink.runtime.concurrent.impl.FlinkFuture) SelfGateway(org.apache.flink.runtime.rpc.SelfGateway) RemoteRpcInvocation(org.apache.flink.runtime.rpc.akka.messages.RemoteRpcInvocation) FlinkFuture(org.apache.flink.runtime.concurrent.impl.FlinkFuture) Future(org.apache.flink.runtime.concurrent.Future) StartStoppable(org.apache.flink.runtime.rpc.StartStoppable)

Aggregations

IOException (java.io.IOException)1 Time (org.apache.flink.api.common.time.Time)1 Future (org.apache.flink.runtime.concurrent.Future)1 FlinkFuture (org.apache.flink.runtime.concurrent.impl.FlinkFuture)1 MainThreadExecutable (org.apache.flink.runtime.rpc.MainThreadExecutable)1 SelfGateway (org.apache.flink.runtime.rpc.SelfGateway)1 StartStoppable (org.apache.flink.runtime.rpc.StartStoppable)1 LocalRpcInvocation (org.apache.flink.runtime.rpc.akka.messages.LocalRpcInvocation)1 RemoteRpcInvocation (org.apache.flink.runtime.rpc.akka.messages.RemoteRpcInvocation)1 RpcInvocation (org.apache.flink.runtime.rpc.akka.messages.RpcInvocation)1