前言
在Flink的各个服务组件中,ResourceManager、JobMaster、TaskExecutor三者之间存在相互检测的心跳机制:ResourceManager会主动发送心跳请求探测JobMaster、TaskExecutor是否存活;JobMaster也会主动发送心跳请求探测TaskExecutor是否存活,以便进行任务重启或者失败处理。
核心组件
HeartbeatMonitor
接口,定义心跳状态信息相关的接口,管理需要心跳通信相关组件的心跳状态
HeartbeatTarget
作用
1.发起心跳的请求
2.接收心跳并进行响应
3.心跳信息中的负载可以添加额外的信息
4.维护ResourceId和HeartBeatMonitor的Map集合映射
UML
HeartbeatManager
接口,能够停止/启动 心跳的监听,并且能够在心跳超时进行上报
HeartbeatManagerImpl
HeartbeatManager的实现类,提供了组件监听的方法,心跳目标对象注册后会将监控对象信息方法Map中
HeartbeatManagerSenderImpl
继承了HeartbeatManagerImpl,实现了Runnable接口,能够周期性的调度进行心跳的检测
初始化
HeartbeatManagerSenderImpl(
long heartbeatPeriod,
long heartbeatTimeout,
int failedRpcRequestsUntilUnreachable,
ResourceID ownResourceID,
HeartbeatListener<I, O> heartbeatListener,
ScheduledExecutor mainThreadExecutor,
Logger log,
HeartbeatMonitor.Factory<O> heartbeatMonitorFactory) {
super(
heartbeatTimeout,
failedRpcRequestsUntilUnreachable,
ownResourceID,
heartbeatListener,
mainThreadExecutor,
log,
heartbeatMonitorFactory);
this.heartbeatPeriod = heartbeatPeriod;
mainThreadExecutor.schedule(this, 0L, TimeUnit.MILLISECONDS);
}
在初始化的时候会被线程池提交一个立刻执行的任务,从而进入到run方法
@Override
public void run() {
if (!stopped) {
log.debug("Trigger heartbeat request.");
for (HeartbeatMonitor<O> heartbeatMonitor : getHeartbeatTargets().values()) {
requestHeartbeat(heartbeatMonitor);
}
getMainThreadExecutor().schedule(this, heartbeatPeriod, TimeUnit.MILLISECONDS);
}
}
作用
1.进行状态判断
2.遍历心跳请求目标
3.发起心跳请求
4.以固定心跳周期的定时任务进行调度
HeartbeatServices
ClusterEntrypoint是集群启动的入口,启动的过程中initializeServices进行了服务的初始化,也包含心跳服务HeartbeatServices的初始化。
初始化HeartbeatServices调用方法栈如下:
ClusterEntrypoint#runCluster->
ClusterEntrypoint#initializeServices->
ClusterEntrypoint#createHaServices->
HeartbeatServices#fromConfiguration