转载https://zhuanlan.zhihu.com/p/53482103算法
这哥们写的好,顺便转过来吧,当作学习用。网络
Spark 的 Structured Streaming 的 Continuous Processing Mode 的容错处理使用了分布式快照(Distributed Snapshot)算法 Chandy-Lamport 算法,那么分布式快照算法能够用来解决什么问题呢?架构
A snapshot algorithm is used to create a consistent snapshot of the global state of adistributed system. Due to the lack of globally shared memory and a global clock, this isn't trivially possible.
简单来讲就是用来在缺少相似全局时钟或者全局时钟不可靠的分布式系统中来肯定一种全局状态。app
那么分布式快照算法应用到流式系统中就是肯定一个 Global 的 Snapshot,错误处理的时候各个节点根据上一次的 Global Snapshot 来恢复。下面就介绍一下在流式系统中普遍使用分布式快照算法:Chandy-Lamport 算法。Flink 使用的是 Chandy-Lamport 的改进算法。async
Chandy-Lamport 算法以两个做者的名字命名,没错,其中 Lamport 就是分布式系统领域无人不晓的 Leslie Lamport,著名的一致性算法 Paxos 的做者。算法的论文于 1985 年发表,Distributed Snapshots: Determining Global States of a Distributed System,提到这篇论文,不得不提一下这篇论文的由来,洗个澡的时间想出来的。分布式
The distributed snapshot algorithm described here came about when I visited Chandy, who was then at the University of Texas in Austin. He posed the problem to me over dinner, but we had both had too much wine to think about it right then. The next morning, in the shower, I came up with the solution. When I arrived at Chandy's office, he was waiting for me with the same solution. I consider the algorithm to be a straightforward application of the basic ideas from Time, Clocks and the Ordering of Events in a Distributed System.
正如 Lamport 所述,算法的思想很是的 straightforward,在描述算法以前须要先介绍一下 Global Snapshot。ide
Global Snapshot 咱们也能够理解为 Global State,中文能够叫作全局状态,在系统作 Failure Recovery 的时候很是有用,也是普遍应用在分布式系统,更可能是分布式计算系统中的一种容错处理理论基础。学习
在 Chandy-Lamport 算法中,为了定义分布式系统的全局状态,咱们先将分布式系统简化成有限个进程和进程之间的 channel 组成,也就是一个有向图:节点是进程,边是 channel。由于是分布式系统,也就是说,这些进程是运行在不一样的物理机器上的。那么一个分布式系统的全局状态就是有进程的状态和 channel 中的 message 组成,这个也是分布式快照算法须要记录的。this
由于是有向图,因此每一个进程对应着两类 channel: input channel, output channel。同时假设 Channel 是一个容量无限大的 FIFO 队列,收到的 message 都是有序且无重复的。Chandy-Lamport 分布式快照算法经过记录每一个进程的 local state 和它的 input channel 中有序的 message,咱们能够认为这是一个局部快照。那么全局快照就能够经过将全部的进程的局部快照合并起来获得。idea
那么咱们基于上面假设的分布式系统模型来看一下 Chandy-Lamport 算法具体的工做流程是什么样的。主要包括下面三个部分:
Initiating a snapshot: 也就是开始建立 snapshot,能够由系统中的任意一个进程发起
Propagating a snapshot: 系统中其余进程开始逐个建立 snapshot 的过程
Terminating a snapshot: 算法结束条件
Initiating a snapshot
进程 Pi 发起: 记录本身的进程状态,同时生产一个标识信息 marker,marker 和进程通讯的 message 不一样
将 marker 信息经过 ouput channel 发送给系统里面的其余进程
开始记录全部 input channel 接收到的 message
Propagating a snapshot
对于进程 Pj 从 input channel Ckj 接收到 marker 信息:
若是 Pj 尚未记录本身的进程状态,则
Pj 记录本身的进程状态,同时将 channel Ckj 置为空
向 output channel 发送 marker 信息
不然
记录其余 channel 在收到 marker 以前的 channel 中收到全部 message
因此这里的 marker 实际上是充当一个分隔符,分隔进程作 local snapshot (记录进程状态)的 message。好比 Pj 作完 local snapshot 以后 Ckj 中发送过来的 message 为 [a,b,c,marker,x,y,z] 那么 a, b, c 就是进程 Pk 作 local snapshot 前的数据,Pj 对于这部分数据须要记录下来,好比记录在 log 里面。而 marker 后面 message 正常处理掉就能够了。
Terminating a snapshot
全部的进程都收到 marker 信息而且记录下本身的状态和 channel 的状态(包含的 message)
假设系统中包含两个进程 P1 和 P2 ,P1 进程状态包括三个变量 X1,Y1 和 Z1 , P2 进程包括三个变量 X2,Y2 和 Z2。初始状态以下。
由 P1 发起全局 Snapshot 记录,P1 先记录自己的进程状态,而后向 P2 发送 marker 信息。在 marker 信息到达 P2 以前,P2 向 P1 发送 message: M。
P2 收到 P1 发送过来的 marker 信息以后,记录本身的状态。而后 P1 收到 P2 以前发送过来的 message: M。对于 P1 来讲,从 P2 channel 发送过来的信息至关因而 [M, marker],因为 P1 已经作了 local snapshot,因此 P1 须要记录 message M。
那么全局 Snapshot 就至关于下图中的蓝色部分。
Chandy-Lamport 算法经过抽象分布式系统模型描述了一种简单直接可是很是有效的分布式快照算法。讨论 Chandy-Lamport 算法必定要注意算法的几个前提:网络可靠、消息有序。
Spark 的 Structured Streaming 虽然在官方博客中披露使用的 Chandy-Lamport 算法来作 Failover 处理,可是并无更细节的披露。相比之下 Flink 在 2015 发布了一篇论文 Lightweight asynchronous snapshots for distributed dataflows 更适合在工程上实现,并且已经应用在了 Flink 项目中。核心思想是在 input source 端插入 barrier 来替代 Chandy-Lamport 算法中的 Marker,经过控制 barrier 的同步来实现 snapshot 的备份和 exactly-once 语义。若是看过 Spark Streaming 那篇论文,对于这个地方很明显的一个疑问就是如何处理 straggler(分布式系统中运行明显慢于其余节点的节点),答案是没法处理。
有时候不得不认可,在大多数状况下,所谓系统架构都是在作 trade-off。
Chandy K M, Lamport L. Distributed snapshots: Determining global states of distributed systems[J]. ACM Transactions on Computer Systems (TOCS), 1985, 3(1): 63-75.
Carbone P, Fóra G, Ewen S, et al. Lightweight asynchronous snapshots for distributed dataflows[J]. arXiv preprint arXiv:1506.08603, 2015.
Time, Clocks and the Ordering of Events in a Distributed System
Leslie Lamport Homepage
tele.informatik.uni-freiburg.de
people.cs.umass.edu/~ar
cs.princeton.edu/course
简单解释: 分布式快照(Chandy-Lamport算法)