鍥捐В鍏辫瘑绠楁硶锛歊aft
Posted 娣卞叆鍘熺悊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍥捐В鍏辫瘑绠楁硶锛歊aft相关的知识,希望对你有一定的参考价值。
鐐瑰嚮馃憜钃濊壊鈥?nbsp;娣卞叆鍘熺悊鈥濓紝鍏虫敞骞垛€?/span>璁句负鏄熸爣鈥?/span>
鎶€鏈共璐э紝绗竴鏃堕棿鎺ㄩ€?/span>
銆屾嫓鍗犲涵灏嗗啗闂銆嶏細澶氫釜鎷滃崰搴皢鍐涜濡備綍鍦ㄥ彲鑳芥湁鍙涘緬銆佷俊浣垮彲鑳借绛栧弽鎴栬€呮殫鏉€鐨勬儏鍐典笅杈炬垚鏄惁瑕佽繘鏀荤殑涓€鑷存€у喅瀹氾紵杩欑瘒涓昏鏄粙缁嶇畝鍖栫増鎷滃崰搴皢鍐涢棶棰樼殑瑙e喅鏂规锛歊aft 鍏辫瘑绠楁硶銆?/span>
鎷滃崰搴皢鍐涢棶棰樻槸鍒嗗竷寮忛鍩熸渶澶嶆潅銆佹渶涓ユ牸鐨勫閿欐ā鍨嬨€備絾鍦ㄦ棩甯稿伐浣滀腑浣跨敤鐨勫垎甯冨紡绯荤粺闈㈠鐨勯棶棰樹笉浼氶偅涔堝鏉傦紝鏇村鐨勬槸璁$畻鏈烘晠闅滄寕鎺変簡锛屾垨鑰呯綉缁滈€氫俊闂鑰屾病娉曚紶閫掍俊鎭紝杩欑鎯呭喌涓嶈€冭檻璁$畻鏈轰箣闂翠簰鐩稿彂閫佹伓鎰忎俊鎭紝鏋佸ぇ绠€鍖栦簡绯荤粺瀵瑰閿欑殑瑕佹眰锛屾渶涓昏鐨勬槸杈惧埌涓€鑷存€с€?/p>
鎵€浠ュ皢鎷滃崰搴皢鍐涢棶棰樻牴鎹父瑙佺殑宸ヤ綔涓婄殑闂杩涜绠€鍖栵細鍋囪灏嗗啗涓病鏈夊彌鍐涳紝淇′娇鐨勪俊鎭彲闈犱絾鏈夊彲鑳借鏆楁潃鐨勬儏鍐典笅锛屽皢鍐涗滑濡備綍杈炬垚涓€鑷存€у喅瀹氾紵
瀵逛簬杩欎釜绠€鍖栧悗鐨勯棶棰橈紝鏈夎澶氳В鍐虫柟妗堬紝绗竴涓璇佹槑鐨勫叡璇嗙畻娉曟槸 Paxos锛岀敱鎷滃崰搴皢鍐涢棶棰樼殑浣滆€?Leslie Lamport 鍦?990骞存彁鍑猴紝鏈€鍒濅互璁烘枃闅炬噦鑰屽嚭鍚嶏紝鍚庢潵杩欏摜浠湪2001閲嶆柊鍙戜簡涓€绡囩畝鍗曠増鐨勮鏂?nbsp;Paxos Made Simple锛岀劧鑰岃繕鏄尯闅炬噦鐨勩€?/span>
鍥犱负 Paxos 闅炬噦锛岄毦瀹炵幇锛屾墍浠ユ柉鍧︾澶у鐨勬暀鎺堝湪2014骞村彂琛ㄤ簡鏂扮殑鍒嗗竷寮忓崗璁?Raft銆備笌 Paxos 鐩告瘮锛孯aft 鏈夌潃鍩烘湰鐩稿悓杩愯鏁堢巼锛屼絾鏄洿瀹规槗鐞嗚В锛屼篃鏇村鏄撹鐢ㄥ湪绯荤粺寮€鍙戜笂銆?/span>
閽堝绠€鍖栫増鎷滃崰搴皢鍐涢棶棰橈紝Raft 瑙e喅鏂规绫绘瘮
鎴戜滑杩樻槸鐢ㄦ嫓鍗犲涵灏嗗啗鐨勪緥瀛愭潵甯姪鐞嗚В Raft銆?/span>
鍋囪灏嗗啗涓病鏈夊彌鍐涳紝淇′娇鐨勪俊鎭彲闈犱絾鏈夊彲鑳借鏆楁潃鐨勬儏鍐典笅锛屽皢鍐涗滑濡備綍杈炬垚涓€鑷存€у喅瀹氾紵
Raft 鐨勮В鍐虫柟妗堝ぇ姒傚彲浠ョ悊瑙f垚 鍏堝湪鎵€鏈夊皢鍐涗腑閫夊嚭涓€涓ぇ灏嗗啗锛屾墍鏈夌殑鍐冲畾鐢卞ぇ灏嗗啗鏉ュ仛銆傞€変妇鐜妭锛氭瘮濡傝鐜板湪涓€鍏辨湁3涓皢鍐?A, B, C锛屾瘡涓皢鍐涢兘鏈変竴涓殢鏈烘椂闂寸殑鍊掕鏃跺櫒锛屽€掕鏃朵竴缁撴潫锛岃繖涓皢鍐涘氨浼氭妸鑷繁褰撴垚澶у皢鍐涘€欓€変汉锛岀劧鍚庢淳淇′娇鍘婚棶鍏朵粬鍑犱釜灏嗗啗锛岃兘涓嶈兘閫夋垜涓烘€诲皢鍐涳紵鍋囪鐜板湪灏嗗啗A鍊掕鏃剁粨鏉熶簡锛屼粬娲句俊浣夸紶閫掗€変妇鎶曠エ鐨勪俊鎭粰灏嗗啗B鍜孋锛屽鏋滃皢鍐汢鍜孋杩樻病鎶婅嚜宸卞綋鎴愬€欓€変汉锛堝€掕鏃惰繕娌℃湁缁撴潫锛夛紝骞朵笖娌℃湁鎶婇€変妇绁ㄦ姇缁欏叾浠栵紝浠栦滑鎶婄エ鎶曠粰灏嗗啗A锛屼俊浣垮湪鍥炲埌灏嗗啗A鏃讹紝灏嗗啗A鐭ラ亾鑷繁鏀跺埌浜嗚冻澶熺殑绁ㄦ暟锛屾垚涓轰簡澶у皢鍐涖€傚湪杩欎箣鍚庯紝鏄惁瑕佽繘鏀诲氨鐢卞ぇ灏嗗啗鍐冲畾锛岀劧鍚庢淳淇′娇鍘婚€氱煡鍙﹀涓や釜灏嗗啗锛屽鏋滃湪涓€娈垫椂闂村悗杩樻病鏈夋敹鍒板洖澶嶏紙鍙兘淇′娇琚殫鏉€锛夛紝閭e氨鍐嶉噸娲句竴涓俊浣匡紝鐩村埌鏀跺埌鍥炲銆?/span>
鏁呬簨鍏堣鍒拌繖閲岋紝甯屾湜涓嶅仛鎶€鏈柟闈㈢殑鏈嬪弸鍙互澶ф鑳界悊瑙?Raft 鐨勫師鐞嗭紝涓嬮潰浠庢瘮杈冩妧鏈殑瑙掑害璁茶 Raft 鐨勫師鐞嗐€?/span>
1. Raft 鑺傜偣鐘舵€?/h2>
浠庢嫓鍗犲涵灏嗗啗鐨勬晠浜嬫槧灏勫埌鍒嗗竷寮忕郴缁熶笂锛屾瘡涓皢鍐涚浉褰撲簬涓€涓垎甯冨紡缃戠粶鑺傜偣锛屾瘡涓妭鐐规湁涓夌鐘舵€侊細Follower锛孋andidate锛孡eader锛岀姸鎬佷箣闂存槸浜掔浉杞崲鐨勶紝鍙互鍙傝€冧笅鍥撅紝鍏蜂綋鐨勫悗闈㈣銆?/span>
姣忎釜鑺傜偣涓婇兘鏈変竴涓€掕鏃跺櫒 (Election Timeout)锛屾椂闂撮殢鏈哄湪 150ms 鍒?300ms 涔嬮棿銆傛湁鍑犵鎯呭喌浼氶噸璁?Timeout锛?/span>
鏀跺埌閫変妇鐨勮姹?/span>
鏀跺埌 Leader 鐨?Heartbeat (鍚庨潰浼氳鍒?
鍦?Raft 杩愯杩囩▼涓紝鏈€涓昏杩涜涓や釜娲诲姩锛?/span>
閫変富 Leader Election
澶嶅埗鏃ュ織 Log Replication
2. 閫変富 Leader Election
2.1 姝e父鎯呭喌涓嬮€変富
鍋囪鐜板湪鏈夊鍥?涓妭鐐癸紝5涓妭鐐逛竴寮€濮嬬殑鐘舵€侀兘鏄?Follower銆?/span>
鍦ㄤ竴涓妭鐐瑰€掕鏃剁粨鏉?(Timeout) 鍚庯紝杩欎釜鑺傜偣鐨勭姸鎬佸彉鎴?Candidate 寮€濮嬮€変妇锛屽畠缁欏叾浠栧嚑涓妭鐐瑰彂閫侀€変妇璇锋眰 (RequestVote)
鍏朵粬鍥涗釜鑺傜偣閮借繑鍥炴垚鍔燂紝杩欎釜鑺傜偣鐨勭姸鎬佺敱 Candidate 鍙樻垚浜?Leader锛屽苟鍦ㄦ瘡涓竴灏忔鏃堕棿鍚庯紝灏辩粰鎵€鏈夌殑 Follower 鍙戦€佷竴涓?Heartbeat 浠ヤ繚鎸佹墍鏈夎妭鐐圭殑鐘舵€侊紝Follower 鏀跺埌 Leader 鐨?Heartbeat 鍚庨噸璁?Timeout銆?/span>
杩欐槸鏈€绠€鍗曠殑閫変富鎯呭喌锛屽彧瑕佹湁瓒呰繃涓€鍗婄殑鑺傜偣鎶曟敮鎸佺エ浜嗭紝Candidate 鎵嶄細琚€変妇涓?Leader锛?涓妭鐐圭殑鎯呭喌涓嬶紝3涓妭鐐?(鍖呮嫭 Candidate 鏈韩) 鎶曚簡鏀寔灏辫銆?/span>
2.2 Leader 鍑烘晠闅滄儏鍐典笅鐨勯€変富
涓€寮€濮嬪凡缁忔湁涓€涓?Leader锛屾墍鏈夎妭鐐规甯歌繍琛屻€?/span>
Leader 鍑烘晠闅滄寕鎺変簡锛屽叾浠栧洓涓?Follower 灏嗚繘琛岄噸鏂伴€変富銆?/span>
4涓妭鐐圭殑閫変富杩囩▼鍜?涓妭鐐圭殑绫讳技锛屽湪閫夊嚭涓€涓柊鐨?Leader 鍚庯紝鍘熸潵鐨?Leader 鎭㈠浜嗗張閲嶆柊鍔犲叆浜嗭紝杩欎釜鏃跺€欐€庝箞澶勭悊锛熷湪 Raft 閲岋紝绗嚑杞€変妇鏄湁璁板綍鐨勶紝閲嶆柊鍔犲叆鐨?Leader 鏄涓€杞€変妇 (Term 1) 閫夊嚭鏉ョ殑锛岃€岀幇鍦ㄧ殑 Leader 鍒欐槸 Term 2锛屾墍鏈夊師鏉ョ殑 Leader 浼氳嚜瑙夐檷绾т负 Follower
2.3 澶氫釜 Candidate 鎯呭喌涓嬬殑閫変富
鍋囪涓€寮€濮嬫湁4涓妭鐐癸紝閮借繕鏄?Follower銆?/span>
鏈変袱涓?Follower 鍚屾椂 Timeout锛岄兘鍙樻垚浜?Candidate 寮€濮嬮€変妇锛屽垎鍒粰涓€涓?Follower 鍙戦€佷簡鎶曠エ璇锋眰銆?/span>
涓や釜 Follower 鍒嗗埆杩斿洖浜唎k锛岃繖鏃朵袱涓?Candidate 閮藉彧鏈?绁紝瑕?绁ㄦ墠鑳借閫夋垚 Leader銆?/span>
涓や釜 Candidate 浼氬垎鍒粰鍙﹀涓€涓繕娌℃湁缁欒嚜宸辨姇绁ㄧ殑 Follower 鍙戦€佹姇绁ㄨ姹傘€?/span>
浣嗘槸鍥犱负 Follower 鍦ㄨ繖涓€杞€変妇涓紝閮藉凡缁忔姇瀹岀エ浜嗭紝鎵€浠ラ兘鎷掔粷浜嗕粬浠殑璇锋眰銆傛墍浠ュ湪 Term 2 娌℃湁 Leader 琚€夊嚭鏉ャ€?/span>
杩欐椂锛屼袱涓妭鐐圭殑鐘舵€佹槸 Candidate锛屼袱涓槸 Follower锛屼絾鏄粬浠殑鍊掕鏃跺櫒浠嶇劧鍦ㄨ繍琛岋紝鏈€鍏?Timeout 鐨勯偅涓妭鐐逛細杩涜鍙戣捣鏂颁竴杞?Term 3 鐨勬姇绁ㄣ€?/span>
涓や釜 Follower 鍦?Term 3 杩樻病鎶曡繃绁紝鎵€浠ヨ繑鍥?OK锛岃繖鏃?Candidate 涓€鍏辨湁涓夌エ锛岃閫変负浜?Leader銆?/span>
濡傛灉 Leader Heartbeat 鐨勬椂闂存櫄浜庡彟澶栦竴涓?Candidate timeout 鐨勬椂闂达紝鍙﹀涓€涓?Candidate 浠嶇劧浼氬彂閫侀€変妇璇锋眰銆?/span>
涓や釜 Follower 宸茬粡鎶曞畬绁ㄤ簡锛屾嫆缁濅簡杩欎釜 Candidate 鐨勬姇绁ㄨ姹傘€?/span>
Leader 杩涜 Heartbeat锛?Candidate 鏀跺埌鍚庣姸鎬佽嚜鍔ㄨ浆涓?Follower锛屽畬鎴愰€変富銆?/span>
浠ヤ笂鏄?Raft 鏈€閲嶈娲诲姩涔嬩竴閫変富鐨勪粙缁嶏紝浠ュ強鍦ㄤ笉鍚屾儏鍐典笅濡備綍杩涜閫変富銆?/span>
3. 澶嶅埗鏃ュ織 Log Replication
3.1 姝e父鎯呭喌涓嬪鍒舵棩蹇?/h3>
Raft 鍦ㄥ疄闄呭簲鐢ㄥ満鏅腑鐨勪竴鑷存€ф洿澶氱殑鏄綋鐜板湪涓嶅悓鑺傜偣涔嬮棿鐨勬暟鎹竴鑷存€э紝瀹㈡埛绔彂閫佽姹傚埌浠讳綍涓€涓妭鐐归兘鑳芥敹鍒颁竴鑷寸殑杩斿洖锛屽綋涓€涓妭鐐瑰嚭鏁呴殰鍚庯紝鍏朵粬鑺傜偣浠嶇劧鑳戒互宸叉湁鐨勬暟鎹甯歌繘琛屻€傚湪閫変富涔嬪悗鐨勫鍒舵棩蹇楀氨鏄负浜嗚揪鍒拌繖涓洰鐨勩€?/span>
涓€寮€濮嬶紝Leader 鍜?涓や釜 Follower 閮芥病鏈変换浣曟暟鎹€?/span>
瀹㈡埛绔彂閫佽姹傜粰 Leader锛屽偍瀛樻暟鎹?鈥渟ally鈥濓紝Leader 鍏堝皢鏁版嵁鍐欏湪鏈湴鏃ュ織锛岃繖鏃跺€欐暟鎹繕鏄?nbsp;Uncommitted (杩樻病鏈€缁堢‘璁わ紝绾㈣壊琛ㄧず)
Leader 缁欎袱涓?Follower 鍙戦€?AppendEntries 璇锋眰锛屾暟鎹湪 Follower 涓婃病鏈夊啿绐侊紝鍒欏皢鏁版嵁鏆傛椂鍐欏湪鏈湴鏃ュ織锛孎ollower 鐨勬暟鎹篃杩樻槸 Uncommitted銆?/span>
Follower 灏嗘暟鎹啓鍒版湰鍦板悗锛岃繑鍥?OK銆侺eader 鏀跺埌鍚庢垚鍔熻繑鍥烇紝鍙鏀跺埌鐨勬垚鍔熺殑杩斿洖鏁伴噺瓒呰繃鍗婃暟 (鍖呭惈Leader)锛孡eader 灏嗘暟鎹?鈥渟ally鈥?鐨勭姸鎬佹敼鎴?Committed銆? 杩欎釜鏃跺€?Leader 灏卞彲浠ヨ繑鍥炵粰瀹㈡埛绔簡)
Leader 鍐嶆缁?Follower 鍙戦€?AppendEntries 璇锋眰锛屾敹鍒拌姹傚悗锛孎ollower 灏嗘湰鍦版棩蹇楅噷 Uncommitted 鏁版嵁鏀规垚 Committed銆傝繖鏍峰氨瀹屾垚浜嗕竴鏁翠釜澶嶅埗鏃ュ織鐨勮繃绋嬶紝涓変釜鑺傜偣鐨勬暟鎹槸涓€鑷寸殑锛?/span>
3.2 Network Partition 鎯呭喌涓嬭繘琛屽鍒舵棩蹇?/h3>
鍦?Network Partition 鐨勬儏鍐典笅锛岄儴鍒嗚妭鐐逛箣闂存病鍔炴硶浜掔浉閫氫俊锛孯aft 涔熻兘淇濊瘉鍦ㄨ繖绉嶆儏鍐典笅鏁版嵁鐨勪竴鑷存€с€?/span>
涓€寮€濮嬫湁 5 涓妭鐐瑰浜庡悓涓€缃戠粶鐘舵€佷笅銆?/span>
Network Partition 灏嗚妭鐐瑰垎鎴愪袱杈癸紝涓€杈规湁涓や釜鑺傜偣锛屼竴杈逛笁涓妭鐐广€?/span>
涓や釜鑺傜偣杩欒竟宸茬粡鏈?Leader 浜嗭紝鏉ヨ嚜瀹㈡埛绔殑鏁版嵁 鈥渂ob鈥?閫氳繃 Leader 鍚屾鍒?Follower銆?/span>
鍥犱负鍙湁涓や釜鑺傜偣锛屽皯浜?涓妭鐐癸紝鎵€浠?鈥渂ob鈥?鐨勭姸鎬佷粛鏄?Uncommitted銆傛墍浠ュ湪杩欓噷锛屾湇鍔″櫒浼氳繑鍥為敊璇粰瀹㈡埛绔?/span>
鍙﹀涓€涓?Partition 鏈変笁涓妭鐐癸紝杩涜閲嶆柊閫変富銆傚鎴风鏁版嵁 鈥渢om鈥?鍙戝埌鏂扮殑 Leader锛岄€氳繃鍜屼笂鑺傜綉缁滅姸鎬佷笅鐩镐技鐨勮繃绋嬶紝鍚屾鍒板彟澶栦袱涓?Follower銆?/span>
鍥犱负杩欎釜 Partition 鏈?涓妭鐐癸紝瓒呰繃鍗婃暟锛屾墍浠ユ暟鎹?鈥渢om鈥?閮?Commit 浜嗐€?/span>
缃戠粶鐘舵€佹仮澶嶏紝5涓妭鐐瑰啀娆″浜庡悓涓€涓綉缁滅姸鎬佷笅銆備絾鏄繖閲屽嚭鐜颁簡鏁版嵁鍐茬獊 鈥渂ob" 鍜?鈥渢om"
涓変釜鑺傜偣鐨?Leader 骞挎挱 AppendEntries
涓や釜鑺傜偣 Partition 鐨?Leader 鑷姩闄嶇骇涓?Follower锛屽洜涓鸿繖涓?Partition 鐨勬暟鎹?鈥渂ob鈥?娌℃湁 Commit锛岃繑鍥炵粰瀹㈡埛绔殑鏄敊璇紝瀹㈡埛绔煡閬撹姹傛病鏈夋垚鍔燂紝鎵€浠?Follower 鍦ㄦ敹鍒?AppendEntries 璇锋眰鏃讹紝鍙互鎶?鈥渂ob鈥?鍒犻櫎锛岀劧鍚庡悓姝?鈥漷om鈥濓紝閫氳繃杩欎箞涓€涓繃绋嬶紝灏卞畬鎴愪簡鍦?Network Partition 鎯呭喌涓嬬殑澶嶅埗鏃ュ織锛屼繚璇佷簡鏁版嵁鐨勪竴鑷存€с€?/span>
灏忔€荤粨
Raft 鏄兘澶熷疄鐜板垎甯冨紡绯荤粺寮轰竴鑷存€х殑绠楁硶锛屾瘡涓郴缁熻妭鐐规湁涓夌鐘舵€?Follower锛孋andidate锛孡eader銆傚疄鐜?Raft 绠楁硶涓や釜鏈€閲嶈鐨勪簨鏄細閫変富鍜屽鍒舵棩蹇?/span>
鍙傝€冮摼鎺ワ細
Raft 瀹樼綉锛歨ttps://raft.github.io/
Raft 鍘熺悊鍔ㄧ敾 (鎺ㄨ崘鐪嬬湅)锛歨ttp://thesecretlivesofdata.com/raft/
Raft 绠楁硶瑙f瀽鍥剧墖鏉ユ簮锛歨ttp://www.infoq.com/cn/articles/coreos-analyse-etcd
https://www.cnblogs.com/lishiqi-blog/p/11898822.html
-娣卞叆鍘熺悊-
鐭ュ叾鐒跺苟鐭ュ叾鎵€浠ョ劧
以上是关于鍥捐В鍏辫瘑绠楁硶锛歊aft的主要内容,如果未能解决你的问题,请参考以下文章
銆愬崄澶╄嚜鍒惰蒋娓叉煋鍣ㄣ€慏AY 03锛氱敾涓€涓笁瑙掑舰锛堝悜閲忓弶涔樼畻娉?& 閲嶅績鍧愭爣绠楁硶锛?/a>