分佈式快照算法: Chandy-Lamport 算法

  轉載https://zhuanlan.zhihu.com/p/53482103算法

       這哥們寫的好,順便轉過來吧,當作學習用。網絡

分佈式快照算法: Chandy-Lamport 算法

 

0. 引言

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

1. Overview

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

2. Global Snapshot

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

3. Chandy-Lamport 算法

那麼咱們基於上面假設的分佈式系統模型來看一下 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)

4. 例子

假設系統中包含兩個進程 P1 和 P2 ,P1 進程狀態包括三個變量 X1,Y1 和 Z1 , P2 進程包括三個變量 X2,Y2 和 Z2。初始狀態以下。

640?wx_fmt=jpeg

由 P1 發起全局 Snapshot 記錄,P1 先記錄自己的進程狀態,而後向 P2 發送 marker 信息。在 marker 信息到達 P2 以前,P2 向 P1 發送 message: M。

640?wx_fmt=jpeg

P2 收到 P1 發送過來的 marker 信息以後,記錄本身的狀態。而後 P1 收到 P2 以前發送過來的 message: M。對於 P1 來講,從 P2 channel 發送過來的信息至關因而 [M, marker],因爲 P1 已經作了 local snapshot,因此 P1 須要記錄 message M。

640?wx_fmt=jpeg

那麼全局 Snapshot 就至關於下圖中的藍色部分。

640?wx_fmt=jpeg

5. 總結

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。

Refer

  1. 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.

  2. Carbone P, Fóra G, Ewen S, et al. Lightweight asynchronous snapshots for distributed dataflows[J]. arXiv preprint arXiv:1506.08603, 2015.

  3. Time, Clocks and the Ordering of Events in a Distributed System

  4. Leslie Lamport Homepage

  5. tele.informatik.uni-freiburg.de

  6. people.cs.umass.edu/~ar

  7. cs.princeton.edu/course

  8. 簡單解釋: 分佈式快照(Chandy-Lamport算法)

相關文章
相關標籤/搜索