【Flink集群RPC通讯机制(三)】AkkaRpcActor设计与实现:接收RPC消息以及处理逻辑

文章目录

    • [1. 创建Receiver](#1. 创建Receiver)
    • [2. 进行消息处理](#2. 进行消息处理)

RPC请求发送后接收方的处理逻辑

在RpcEndpoint中创建的RemoteRpcInvocation消息,最终会通过Akka系统传递到被调用方。例如TaskExecutor向ResourceManager发送SlotReport请求的时候,会在TaskExecutor中将ResourceManagerGateway的方法名称和参数打包成RemoteRpcInvocation对象。然后经过网络发送到ResourceManager中的AkkaRpcActor,处理请求。

接下来深入了解AkkaRpcActor的设计与实现,了解在AkkaRpcActor中如何接收RemoteRpcInvocation消息并执行后续的操作。

1. 创建Receiver

如代码所示,首先在AkkaRpcActor中创建Receive对象,用于处理Akka系统接收的其他Actor发送过来的消息。

Receiver相关能力

在AkkaRpcActor中主要创建了RemoteHandshakeMessage、ControlMessages等消息对应的处理器,

  • 其中RemoteHandshakeMessage主要用于进行正式RPC通信之前的网络连接检测,保障RPC通信正常。
  • ControlMessages用于控制Akka系统,例如启动和停止Akka Actor等控制消息。这里我们重点关注第三种类型的消息,即在集群运行时中RPC组件通信使用的Message类型,此时会调用handleMessage()方法对这类消息进行处理。
java 复制代码
public Receive createReceive() {
    return ReceiveBuilder.create()
        .match(RemoteHandshakeMessage.class, this::handleHandshakeMessage)
        .match(ControlMessages.class, this::handleControlMessage)
        .matchAny(this::handleMessage)
        .build();
}

2. 进行消息处理

在AkkaRpcActor.handleMessage()方法中,最终会调用handleRpcMessage()方法继续对RPC消息进行处理。

如下代码:

java 复制代码
//根据RPC消息类型,进行不同方式处理
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 {
        // 省略部分代码
        sendErrorIfSender(
            new AkkaUnknownMessageException("Received unknown message " + message +  
               " of type " +  message.getClass().getSimpleName() + '.'));
    }
}

接着看AkkaRpcActor.handleRpcInvocation()方法逻辑:

  1. 判断当前RpcEndpoint是否实现了指定rpcMethod。

例如JobMaster调用ResourceManagerGateway.requestSlot()方法,会在lookupRpcMethod()方法中判断当前ResourceManager实现的Endpoint是否提供了该方法的实现。

  1. 当rpcMethod不为空时,rpcMethod.invoke(rpcEndpoint, rpcInvocation.getArgs())
  2. 调用sendAsyncResponse()、sendSyncResponse()方法通过Akka系统将RpcMethod返回值返回给调用方。
java 复制代码
private void handleRpcInvocation(RpcInvocation rpcInvocation) {
    Method rpcMethod = null;
    try {
        String methodName = rpcInvocation.getMethodName();
        Class<?>[] parameterTypes = rpcInvocation.getParameterTypes();
        rpcMethod = lookupRpcMethod(methodName, parameterTypes);
    } catch (ClassNotFoundException e) {
        // 省略部分代码
    }
    if (rpcMethod != null) {
        try {
            rpcMethod.setAccessible(true);
            if (rpcMethod.getReturnType().equals(Void.TYPE)) {
                // 没有返回值的情况
                rpcMethod.invoke(rpcEndpoint, rpcInvocation.getArgs());
            }
            else {
                  // 有返回值的情况
                final Object result;
                try {
                    result = rpcMethod.invoke(rpcEndpoint, rpcInvocation.getArgs());
                }
                catch (InvocationTargetException e) {
                    getSender()
                        .tell(new Status.Failure(e.getTargetException()), getSelf());
                    return;
                }
                final String methodName = rpcMethod.getName();
                if (result instanceof CompletableFuture) {
                    final CompletableFuture<?> responseFuture = 
                        (CompletableFuture<?>) result;
                    sendAsyncResponse(responseFuture, methodName);
                } else {
                    sendSyncResponse(result, methodName);
                }
            }
        } catch (Throwable e) {
            log.error("Error while executing remote procedure call {}.", 
                      rpcMethod, e);
            // 通知错误信息
            getSender().tell(new Status.Failure(e), getSelf());
        }
    }
}

接下来从更加宏观的角度了解各组件之间如何基于已经实现的RPC框架进行通信,进一步加深对Flink中RPC框架的了解。

参考:《Flink设计与实现:核心原理与源码解析》--张利兵

相关推荐
Hello.Reader8 分钟前
写给生产环境的 Flink 内存配置Process Memory、TaskManager 组件拆解与场景化调优
大数据·flink
m0_748229992 小时前
Laravel4.x核心更新全解析
开发语言·php
DolphinScheduler社区2 小时前
Linux 环境下,Apache DolphinScheduler 如何驱动 Flink 消费 Kafka 数据?
linux·flink·kafka·开源·apache·海豚调度·大数据工作流调度
Hello.Reader2 小时前
Flink 进程三种配置方式、JVM 参数映射与常见踩坑
大数据·jvm·flink
Hello.Reader3 小时前
Flink TaskManager 内存配置从“总量”到“组件”,把资源用在刀刃上
大数据·flink
h7ml3 小时前
电商返利系统中佣金计算的幂等性保障与对账补偿机制实现
服务器·前端·php
北辰当尹3 小时前
【小迪安全2023】day43 php应用&SQL注入&符号拼接&请求方法&HTTP头&JSON&编码类
sql·安全·php
csdn_aspnet3 小时前
PHP常用算法解析:从Web开发到性能优化的实战
php
LuminescenceJ3 小时前
RPC通信中的Context上下文如何跨进程传递消息,gRPC为例分析
开发语言·网络·后端·网络协议·rpc·golang
嵌入式×边缘AI:打怪升级日志4 小时前
Libmodbus 源码总体分析:框架、数据结构与核心函数详解
开发语言·数据结构·php