360网站推广官网授权商,保定网站开发公司,自己能否建立公司网站,大连网站建设佳熙科技公司文章目录 1. 创建Receiver2. 进行消息处理 RPC请求发送后接收方的处理逻辑 在RpcEndpoint中创建的RemoteRpcInvocation消息#xff0c;最终会通过Akka系统传递到被调用方。例如TaskExecutor向ResourceManager发送SlotReport请求的时候#xff0c;会在TaskExecutor中将Resourc… 文章目录 1. 创建Receiver2. 进行消息处理 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()方法对这类消息进行处理。 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消息进行处理。
如下代码 //根据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()方法逻辑 判断当前RpcEndpoint是否实现了指定rpcMethod。 例如JobMaster调用ResourceManagerGateway.requestSlot()方法会在lookupRpcMethod()方法中判断当前ResourceManager实现的Endpoint是否提供了该方法的实现。 当rpcMethod不为空时rpcMethod.invoke(rpcEndpoint, rpcInvocation.getArgs())调用sendAsyncResponse()、sendSyncResponse()方法通过Akka系统将RpcMethod返回值返回给调用方。 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设计与实现核心原理与源码解析》–张利兵