Raft figure 2

1.state

一.State

①Persistent state on all server(所有server的持久化的狀態(tài)):(Updated on stable storage before responding to RPCs)

currentTerm:自己已知的最新Term,初始化時是0,單調(diào)增加

votedFor:在currentTerm內(nèi),票投給了哪個candidateId

log[]:log entries;每一個entry 包含了針對狀態(tài)機的命令,還有這個entry是在哪個term的leader接到的.

②Volatile State on all server

commitIndex:已知的最后一個commited entry index(基0,單調(diào)遞增)

lastApplied:本server最后一個已經(jīng)applied到狀態(tài)機的entry index(基0,單調(diào)遞增)

③Volatile State on leader:在選舉后重新初始化

nextIndex[]:針對每個server的下一個待發(fā)送的entry index

matchIndex[]:已經(jīng)和leader對齊的index


2. AppendEntries RPC

二.AppendEntries RPC:invoked by leader to replicated log entries;also used as heartbeat.

①Arguments:

term: leader's term

leaderId:so follower can redirect clients 所以follower可以重定向客戶端

prevLogIndex:(index of previous log)index of log entry immediately preceding new ones 新entry的前一個index.

leaderCommit:leader's commitIndex

②Results:

term:currentTerm,用于讓leader檢查自己

success:當follower的匹配到了arguments里的prevLogIndex和term,為true

③Receiver implementation:

1.如果term < currentTerm 返回false(告訴leader,老鐵你過期了) §5.1

2.如果term和prevLogTerm匹配,但是log中不包含prevLogIndex,返回false. §5.3

3.如果存在entryIndex,但是這條entry的term與AppendEntries RPC中的term沖突.則把這條沖突的entry和以后的全部刪掉. §5.3

4.追加沒有出現(xiàn)在log中的新entry

5.if leaderCommit > commiteIndex,set commitIndex =min(leaderCommit, index of last new entry)


3. RequestVote RPC

三.RequestVote RPC :Invoked by candidates to gather votes (§5.2).

①Arguments:

term:candidate's term

candidateId:candidate requesting vote

lastLogIndex:index of candidate's last log entry. §5.4

lastLogTerm:term of candidate's last log entry. §5.4

②Results:

term:currentTerm, for candidate to update itself

voteGranted:true means candidate received vote

③Receiver implementation:

1.如果term < currentTerm 返回false? §5.1

2.votedFor is null or candidateId, and candidate’s log is at least as up-to-date as receiver’s log, grant vote (§5.2, §5.4)

voteFor是null或者就是這個candidateId,并且lastLogIndex至少比接收者的log新,投票


4.Rules for Servers

四.Rules for Servers

①All Servers:

1.If commitIndex > lastApplied: increment lastApplied, apply log[lastApplied] to state machine (§5.3) 先增加lastApplied,然后apply

2.If RPC request or response contains term T > currentTerm:set currentTerm = T, convert to follower (§5.1)(只要有別人告訴你,你的term過期了,就更新term并變成follower)

②Followers(§5.2):

1.回復candidate和leader的RPC

2.如果沒有收到任何leader的AppendEntries或者投票給某個candidate而選舉超時,轉變自己成為candidate.

③Candidates(§5.2):

1.在轉變成Candidate時,發(fā)起投票

-增加currentTerm

-Vote for self

-Reset election timer

-Sent RequestVote RPC to all other servers

2.If votes recieved majority of servers:become leader.

3.如果收到新leader的AppendEntries RPC,convert to Follower

4.如果選舉超時,重新再來一次選舉

④Leaders

1.當選時:發(fā)送初始化空的AppendEntries RPCs(heartbeat) 給每一個server;在空閑期間一直重復,以阻止election timer timeout

2.如果收到client的命令:追加entry到本地的log,在entry被應用到狀態(tài)機后回復.

3.If last log index ≥ nextIndex for a follower:從nextIndex開始發(fā)送帶著log entries 的AppendEntries RPC.

? -If successful: update nextIndex and matchIndex for follower (§5.3)

? -If AppendEntries fails because of log inconsistency:decrement nextIndex and retry (§5.3)? -- 上一任leader的entry還沒提交,leader就掛了

? -If there exists an N such that N > commitIndex, a majority of matchIndex[i] ≥ N, and log[N].term == currentTerm:set commitIndex = N (§5.3, §5.4).? 此條規(guī)則說明commitIndex如何根據(jù)matchIndex得到.

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子矛缨,更是在濱河造成了極大的恐慌暴构,老刑警劉巖压状,帶你破解...
    沈念sama閱讀 219,589評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件侥猩,死亡現(xiàn)場離奇詭異秫筏,居然都是意外死亡莫杈,警方通過查閱死者的電腦和手機互例,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評論 3 396
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來筝闹,“玉大人媳叨,你說我怎么就攤上這事腥光。” “怎么了糊秆?”我有些...
    開封第一講書人閱讀 165,933評論 0 356
  • 文/不壞的土叔 我叫張陵武福,是天一觀的道長。 經(jīng)常有香客問我痘番,道長捉片,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,976評論 1 295
  • 正文 為了忘掉前任汞舱,我火速辦了婚禮伍纫,結果婚禮上,老公的妹妹穿的比我還像新娘昂芜。我一直安慰自己莹规,他們只是感情好,可當我...
    茶點故事閱讀 67,999評論 6 393
  • 文/花漫 我一把揭開白布说铃。 她就那樣靜靜地躺著访惜,像睡著了一般。 火紅的嫁衣襯著肌膚如雪腻扇。 梳的紋絲不亂的頭發(fā)上债热,一...
    開封第一講書人閱讀 51,775評論 1 307
  • 那天,我揣著相機與錄音幼苛,去河邊找鬼窒篱。 笑死,一個胖子當著我的面吹牛舶沿,可吹牛的內(nèi)容都是我干的墙杯。 我是一名探鬼主播,決...
    沈念sama閱讀 40,474評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼括荡,長吁一口氣:“原來是場噩夢啊……” “哼高镐!你這毒婦竟也來了?” 一聲冷哼從身側響起畸冲,我...
    開封第一講書人閱讀 39,359評論 0 276
  • 序言:老撾萬榮一對情侶失蹤嫉髓,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后邑闲,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體算行,經(jīng)...
    沈念sama閱讀 45,854評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,007評論 3 338
  • 正文 我和宋清朗相戀三年苫耸,在試婚紗的時候發(fā)現(xiàn)自己被綠了州邢。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,146評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡褪子,死狀恐怖量淌,靈堂內(nèi)的尸體忽然破棺而出骗村,到底是詐尸還是另有隱情,我是刑警寧澤呀枢,帶...
    沈念sama閱讀 35,826評論 5 346
  • 正文 年R本政府宣布叙身,位于F島的核電站,受9級特大地震影響硫狞,放射性物質發(fā)生泄漏信轿。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,484評論 3 331
  • 文/蒙蒙 一残吩、第九天 我趴在偏房一處隱蔽的房頂上張望财忽。 院中可真熱鬧,春花似錦泣侮、人聲如沸即彪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽隶校。三九已至,卻和暖如春蛹锰,著一層夾襖步出監(jiān)牢的瞬間深胳,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評論 1 272
  • 我被黑心中介騙來泰國打工铜犬, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留舞终,地道東北人。 一個月前我還...
    沈念sama閱讀 48,420評論 3 373
  • 正文 我出身青樓癣猾,卻偏偏與公主長得像敛劝,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子纷宇,可洞房花燭夜當晚...
    茶點故事閱讀 45,107評論 2 356

推薦閱讀更多精彩內(nèi)容