【Flink原理和應用】:分佈式快照算法—— Chandy-Lamport 算法

引言

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.

簡單來說就是用來在缺乏類似全局時鐘或者全局時鐘不可靠的分佈式系統中來確定一種全局狀態。

那麼分佈式快照算法應用到流式系統中就是確定一個 Global 的 Snapshot,錯誤處理的時候各個節點根據上一次的 Global Snapshot 來恢復。下面就介紹一下在流式系統中廣泛使用分佈式快照算法:Chandy-Lamport 算法。Flink 使用的是 Chandy-Lamport 的改進算法。

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。

2. Global Snapshot

Global Snapshot 我們也可以理解爲 Global State,中文可以叫做全局狀態,在系統做 Failure Recovery 的時候非常有用,也是廣泛應用在分佈式系統,更多是分佈式計算系統中的一種容錯處理理論基礎。

在 Chandy-Lamport 算法中,爲了定義分佈式系統的全局狀態,我們先將分佈式系統簡化成有限個進程和進程之間的 channel 組成,也就是一個有向圖:節點是進程,邊是 channel。因爲是分佈式系統,也就是說,這些進程是運行在不同的物理機器上的。那麼一個分佈式系統的全局狀態就是有進程的狀態和 channel 中的 message 組成,這個也是分佈式快照算法需要記錄的。

因爲是有向圖,所以每個進程對應着兩類 channel: input channel, output channel。同時假設 Channel 是一個容量無限大的 FIFO 隊列,收到的 message 都是有序且無重複的。Chandy-Lamport 分佈式快照算法通過記錄每個進程的 local state 和它的 input channel 中有序的 message,我們可以認爲這是一個局部快照。那麼全局快照就可以通過將所有的進程的局部快照合併起來得到。

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。初始狀態如下。
在這裏插入圖片描述

由 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 就相當於下圖中的藍色部分。
在這裏插入圖片描述

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算法)

原文出處:https://blog.csdn.net/hxcaifly/article/details/87872150

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章