002.flink源码分析之-基于akka的RPC在flink中的使用逻辑

flink使用akka作为底层RPC框架,实际上flink只是在组件之间的通信使用了akka,而task之间的数据传输使用的是netty框架。

Akka简介

在这里插入图片描述
akka通信框架有如下特点:

1、ActorSystem 是管理 Actor 生命周期的组件, Actor 是负责进行通信的组件
2、每个 Actor 都有一个MailBox,别的 Actor 发送给它的消息都首先储存在 MailBox 中,通过这种 方式可以实现异步通信。
3、每个 Actor是单线程的处理方式,不断的从 MailBox 拉取消息执行处理,所以对于 Actor 的消息处理,不适合调用会阻塞的处理方法。
4、Actor 可以改变他自身的状态,可以接收消息,也可以发送消息,还可以生成新的 Actor
5、每一个ActorSystem 和Actor都在启动的时候会给定一个 name,如果要从ActorSystem中,获取一个 Actor,则通过以下的方式来进行Actor的获取: akka.tcp://asname@host:9527/user/actorname
6、如果一个 Actor要和另外一个 Actor进行通信,则必须先获取对方 Actor 的 ActorRef 对象,然 后通过该对象发送消息即可。
7、通过tell 发送异步消息,不接收响应,通过 ask 发送异步消息,得到 Future 返回,通过异步回到 返回处理结果。

Flink 中的RPC

Flink RPC 框架主要抽象了RpcService,RpcEndpoint,RpcGateway,RpcServer这几个接口。

RpcService

/**
 * Interface for rpc services. An rpc service is used to start and connect to a {@link RpcEndpoint}.
 * Connecting to a rpc server will return a {@link RpcGateway} which can be used to call remote
 * procedures.
 */
public interface RpcService {
   

定义了RPC服务端(调用和被调用端)的一系列行为,远程调用需要指定地址和端口号。包含服务的启停,和连接其他远程的RpcService。注意的接口方法:
在这里插入图片描述
主要方法:

  • connect:连接到一个RpcEndpoint,返回一个RpcGateway,然后调用者可以使用此gateway进行远程方法调用。
  • fenceRpcServer:获取新的RpcServer,可用于重新选主后,更新fencingToken。
  • startServer:启动一个RpcEndpoint,返回一个RpcServer。
  • stopServer: 停止某个RpcEndpoint。
  • scheduleRunnable:延迟调度执行某任务。
  • execute:异步执行某任务。

RpcEndpoint

所有提供远程调用的组件都会继承此抽象类并实现组件自身提供的业务方法,并且保证同一个RpcEndpoint上的所有远程调用都在同一个线程中执行。

/**
 * Base class for RPC endpoints. Distributed components which offer remote procedure calls have to
 * extend the RPC endpoint base class. An RPC endpoint is backed by an {@link RpcService}.
 *
 * <h1>Endpoint and Gateway</h1>
 *
 * To be done...
 *
 * <h1>Single Threaded Endpoint Execution </h1>
 *
 * <p>All RPC calls on the same endpoint are called by the same thread (referred to as the
 * endpoint's <i>main thread</i>). Thus, by executing all state changing operations within the main
 * thread, we don't have to reason about concurrent accesses, in the same way in the Actor Model of
 * Erlang or Akka.
 *
 * <p>The RPC endpoint provides {@link #runAsync(Runnable)}, {@link #callAsync(Callable, Time)} and
 * the {@link #getMainThreadExecutor()} to execute code in the RPC endpoint's main thread.
 *
 * <h1>Lifecycle</h1>
 *
 * <p>The RPC endpoint has the following stages:
 *
 * <ul>
 *   <li>The RPC endpoint is created in a non-running state and does not serve any RPC requests.
 *   <li>Calling the {@link #start()} method triggers the start of the RPC endpoint and schedules
 *       overridable {@link #onStart()} method call to the main thread.
 *   <li>When the start operation ends the RPC endpoint is moved to the running state and starts to
 *       serve and complete RPC requests.
 *   <li>Calling the {@link #closeAsync()} method triggers the termination of the RPC endpoint and
 *       schedules overridable {@link #onStop()} method call to the main thread.
 *   <li>When {@link #onStop()} method is called, it triggers an asynchronous stop operation. The
 *       RPC endpoint is not in the running state anymore but it continues to serve RPC requests.
 *   <li>When the asynchronous stop operation ends, the RPC endpoint terminates completely and does
 *       not serve RPC requests anymore.
 * </ul>
 *
 * The running state can be queried in a RPC method handler or in the main thread by calling {@link
 * #isRunning()} method.
 */
public abstract class RpcEndpoint implements RpcGateway, AutoCloseableAsync {
   

RpcEndpoint的生命周期:
created -> start -> running -> termination -> asynchronous stop -> terminates completely
另外,start方法的调用会在调用前触发onStart方法的调用。closeAsync()方法的调用会先触发onStop()方法的调用。

RpcGateway

用于远程调用RpcEndpoint的某些方法。可以理解为客服端代理。

RpcServer

RpcService的startServer返回的对象,相当于RpcEndpoint自身的代理对象(self gateway)。通过RpcEndpoint的getSelfGateway方法获取其自身的gateway对象然后调用该endpoint的方法。

其中最重要的是RPCEndpoint,RpcEndpoint 下面有四个比较重要的子类:

1、TaskExecutor 集群中从节点中最重要的角色,负责资源管理
2、Dispatcher 主节点中的一个工作角色,负责 job调度执行
3、JobMaster 应用程序中的 主控程序,类似于 Spark 中的Driver的作用
4、ResourceManager集群中的主节点 JobManager 中的负责资源管理的角色,和 TaskExecutor 一 起构成资源管理的主从架构

当在任意地方发现要创建这四个组件的任何一个组件的实例对象的时候,创建成功了之后,都会要去执行他的 onStart() ,在集群启动的源码分析中,其实这些组件的很多的工作流程,都被放在 onStart() 里面。
来看RpcEndpoint的定义

public abstract class RpcEndpoint 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值