Search in sources :

Example 1 with RpcInvocation

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

the class AkkaInvocationHandler method invokeRpc.

// ------------------------------------------------------------------------
// Private methods
// ------------------------------------------------------------------------
/**
 * Invokes a RPC method by sending the RPC invocation details to the rpc endpoint.
 *
 * @param method to call
 * @param args of the method call
 * @return result of the RPC; the result future is completed with a {@link TimeoutException} if
 *     the requests times out; if the recipient is not reachable, then the result future is
 *     completed with a {@link RecipientUnreachableException}.
 * @throws Exception if the RPC invocation fails
 */
private Object invokeRpc(Method method, Object[] args) throws Exception {
    String methodName = method.getName();
    Class<?>[] parameterTypes = method.getParameterTypes();
    Annotation[][] parameterAnnotations = method.getParameterAnnotations();
    Time futureTimeout = extractRpcTimeout(parameterAnnotations, args, timeout);
    final RpcInvocation rpcInvocation = createRpcInvocationMessage(method.getDeclaringClass().getSimpleName(), methodName, parameterTypes, args);
    Class<?> returnType = method.getReturnType();
    final Object result;
    if (Objects.equals(returnType, Void.TYPE)) {
        tell(rpcInvocation);
        result = null;
    } else {
        // Capture the call stack. It is significantly faster to do that via an exception than
        // via Thread.getStackTrace(), because exceptions lazily initialize the stack trace,
        // initially only
        // capture a lightweight native pointer, and convert that into the stack trace lazily
        // when needed.
        final Throwable callStackCapture = captureAskCallStack ? new Throwable() : null;
        // execute an asynchronous call
        final CompletableFuture<?> resultFuture = ask(rpcInvocation, futureTimeout).thenApply(resultValue -> deserializeValueIfNeeded(resultValue, method, flinkClassLoader));
        final CompletableFuture<Object> completableFuture = new CompletableFuture<>();
        resultFuture.whenComplete((resultValue, failure) -> {
            if (failure != null) {
                completableFuture.completeExceptionally(resolveTimeoutException(ExceptionUtils.stripCompletionException(failure), callStackCapture, address, rpcInvocation));
            } else {
                completableFuture.complete(resultValue);
            }
        });
        if (Objects.equals(returnType, CompletableFuture.class)) {
            result = completableFuture;
        } else {
            try {
                result = completableFuture.get(futureTimeout.getSize(), futureTimeout.getUnit());
            } catch (ExecutionException ee) {
                throw new RpcException("Failure while obtaining synchronous RPC result.", ExceptionUtils.stripExecutionException(ee));
            }
        }
    }
    return result;
}
Also used : LocalRpcInvocation(org.apache.flink.runtime.rpc.messages.LocalRpcInvocation) RemoteRpcInvocation(org.apache.flink.runtime.rpc.messages.RemoteRpcInvocation) RpcInvocation(org.apache.flink.runtime.rpc.messages.RpcInvocation) Time(org.apache.flink.api.common.time.Time) CompletableFuture(java.util.concurrent.CompletableFuture) RpcException(org.apache.flink.runtime.rpc.exceptions.RpcException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with RpcInvocation

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

the class AkkaRpcActor method handleRpcMessage.

protected void handleRpcMessage(Object message) {
    if (message instanceof RunAsync) {
        handleRunAsync((RunAsync) message);
    } else if (message instanceof CallAsync) {
        handleCallAsync((CallAsync) message);
    } else if (message instanceof RpcInvocation) {
        handleRpcInvocation((RpcInvocation) message);
    } else {
        log.warn("Received message of unknown type {} with value {}. Dropping this message!", message.getClass().getName(), message);
        sendErrorIfSender(new AkkaUnknownMessageException("Received unknown message " + message + " of type " + message.getClass().getSimpleName() + '.'));
    }
}
Also used : LocalRpcInvocation(org.apache.flink.runtime.rpc.messages.LocalRpcInvocation) RpcInvocation(org.apache.flink.runtime.rpc.messages.RpcInvocation) AkkaUnknownMessageException(org.apache.flink.runtime.rpc.akka.exceptions.AkkaUnknownMessageException) RunAsync(org.apache.flink.runtime.rpc.messages.RunAsync) CallAsync(org.apache.flink.runtime.rpc.messages.CallAsync)

Aggregations

LocalRpcInvocation (org.apache.flink.runtime.rpc.messages.LocalRpcInvocation)2 RpcInvocation (org.apache.flink.runtime.rpc.messages.RpcInvocation)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 Time (org.apache.flink.api.common.time.Time)1 AkkaUnknownMessageException (org.apache.flink.runtime.rpc.akka.exceptions.AkkaUnknownMessageException)1 RpcException (org.apache.flink.runtime.rpc.exceptions.RpcException)1 CallAsync (org.apache.flink.runtime.rpc.messages.CallAsync)1 RemoteRpcInvocation (org.apache.flink.runtime.rpc.messages.RemoteRpcInvocation)1 RunAsync (org.apache.flink.runtime.rpc.messages.RunAsync)1