flink源码分析_flink的checkpoint源码分析(二)

hery:flink的checkpoint源码分析​zhuanlan.zhihu.com

在上一篇文章中,分析到了TriggerCheckpoint消息发送的过程,现在继续分析TriggerCheckpoint消息接收的过程。

TriggerCheckpoint消息的发送是在JobMaster端,接收消息那么就是在TaskManager类中,我们查看源码发现TaskManager.scala文件中有个handleMessage的方法,这个方法就是用于接收消息的入口方法,这这个方法的内部是通过模式匹配的方式,来处理收到的各种消息。

override 

在上面的代码中,我们看到了关于AbstractCheckpointMessage 的抽象类,用于匹配关于checkpoint的消息,后面的handleCheckpointingMessage方法,就是执行关于checkpoint消息的方法

/**

从上面的方法中,我们可以看到,这个方法中主要处理两种类型的消息,一个是接收checkpoint的消息,一个是checkpoint完成通知的消息。解析处理checkpoint消息中,主要调用 task.triggerCheckpointBarrier(checkpointId, timestamp, checkpointOptions)方法

在Task类中triggerCheckpointBarrier 方法中,调用invokable的具体实现类,来执行checkpoint操作

public 

在上面的方法,会调用 invokable.triggerCheckpoint 来通过实现类来进步一步触发checkpoint操作,具体在StreamTask内部实现triggerCheckpoint方法。

public 

上面的方法中,封装了Checkpoint的度量信息,然后调用performCheckpoint方法

synchronized (lock) {
	if (isRunning) {
		// we can do a checkpoint

		// All of the following steps happen as an atomic step from the perspective of barriers and
		// records/watermarks/timers/callbacks.
		// We generally try to emit the checkpoint barrier as soon as possible to not affect downstream
		// checkpoint alignments

		// Step (1): Prepare the checkpoint, allow operators to do some pre-barrier work.
		//           The pre-barrier work should be nothing or minimal in the common case.
		// 第一步,准备checkpoint 允许所有的操作做一些barrier前的工作
		operatorChain.prepareSnapshotPreBarrier(checkpointMetaData.getCheckpointId());

		// Step (2): Send the checkpoint barrier downstream
		// 第二步,发送checkpoint barrier 到下游 以便下游operator尽快开始他们的checkpoint进程
		operatorChain.broadcastCheckpointBarrier(
				checkpointMetaData.getCheckpointId(),
				checkpointMetaData.getTimestamp(),
				checkpointOptions);

		// Step (3): Take the state snapshot. This should be largely asynchronous, to not
		//           impact progress of the streaming topology
		// 进行checkpoint state操作 获取Stete的快照,这在很大程度上应该是异步的,不影响流拓扑的进度
		checkpointState(checkpointMetaData, checkpointOptions, checkpointMetrics);
		return true;
	}
	..

上面这个方法时核心方法,核心功能点有三个

1、准备操作,准备checkpoint 允许所有的操作做一些barrier前的工作

2、发送checkpoint barrier 到下游 以便下游operator尽快开始他们的checkpoint进程

3、 进行checkpoint state操作 获取Stete的快照

在checkpointState 方法中,进行快照等操作

private 

继续调用executeCheckpointing方法

public 

在executeCheckpointing方法中,主要是两个操作,一个是循环变量op,然后调用checkpointStreamOperator方法,另一个是asyncCheckpointRunnable的操作。

private 

op.snapshotState方法,最终是由他的子类AbstractStreamOperator提供的一个final实现

public final OperatorSnapshotFutures snapshotState(long checkpointId, long timestamp, CheckpointOptions checkpointOptions,
			CheckpointStreamFactory factory) throws Exception {
...
// snapshotState(snapshotContext)方法在不同的最终operator中有自己的具体实现
	snapshotState(snapshotContext);
...

上面 snapshotState 方法,在不同的最终Operator中,都有自己的具体实现。

注意

上面描述的是如Source task触发checkpoint的过程,对应非Source的Operator,调用链路如下:

StreamInputProcessor/StreamTwoInputProcessor.processInput()

-> barrierHandler.getNextNonBlocked()

org.apache.flink.streaming.runtime.io.BarrierBuffer#getNextNonBlocked()

->processBarrier(...)

->notifyCheckpoint(receivedBarrier)

->triggerCheckpointOnBarrier

NotifyCheckpointComplete消息

发送消息

NotifyCheckpointComplete 消息的发送是从JobMaster到TaskManager,用来通知task他的检查点已经完成确定,task可以向第三方提交checkpoint。发送NotifyCheckpointComplete 消息的入口方法时在CheckpointCoordinator 类中receiveAcknowledgeMessage方法中

public 

在receiveAcknowledgeMessage方法中,主要调用completePendingCheckpoint 方法,其中核心内容如图:

d536e0cff5d3a31cc0a09b28c2c3650a.png

里面的具体调用过程类似于CheckPoint消息的发送过程。

接收消息

接收消息类似于接收Checkpoint消息,具体代码在TaskManager.scala 类中的handleMessage 方法中,具体实现在handleCheckpointingMessage 中NotifyCheckpointComplete 处理

// checkpoint完成通知的消息

主要是调用task的notifyCheckpointComplete 方法。

AcknowledgeCheckpoint消息

发送消息

AcknowledgeCheckpoint消息是taskManager发送到JobMaster的,用于确认Checkpoint的,告诉JobMaster,某task的checkpoint已经完成。具体的发送消息的代码是在RuntimeEnvironment类中acknowledgeCheckpoint方法。

public 

CheckpointResponder 是一个接口,他的实现类是在ActorGatewayCheckpointResponder

这个类实现了具体的方法

public 

接收消息

接收消息是在CheckpointCoordinator类中receiveAcknowledgeMessage方法

public 

DeclineCheckpoint消息

DeclineCheckpoint消息是有taskManager向JobMaster发送,用于告知CheckpointCoordinator,检查点的请求没能够被处理,可能的原因是某个task已经是running的状态,但是在内部可能还没有准备好志向检查点。说明见源代码注释

/**

发送消息

发送消息是在Task类中的triggerCheckpointBarrier方法中

public 

接收消息

接收消息类似于上面其他的接收消息方法,也是在CheckpointCoordinator类中的receiveDeclineMessage方法里

*

部分内容来源互联网,若有侵权联系删除

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值