Posted Q鐨勫崥瀹?/a> Raft
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
鏈?/span>鏂囦负銆奟aft瀹炴垬銆嬬郴鍒楃4绡囷紝浠嬬粛Raft瀵归€変富鍜屾棩蹇楀鍒剁殑涓€浜涢澶栭檺鍒讹紝浠ヤ繚璇佸叾瀹夊叏鎬э紙璁烘枃鍘熸枃鏄?鈥淪afety鈥濓紝鍏跺疄姝ゅ缈昏瘧涓?span> 鈥滄纭€р€?/strong> 鏇村悎閫備竴浜涳紝鍥藉唴鈥滃畨鍏ㄦ€р€濅竴鑸搴?鈥淪ecurity鈥濓紝浣嗗嚭浜庡璁烘枃浣滆€呯殑鏁剰锛屾湰鏂囩户缁娇鐢ㄢ€滃畨鍏ㄦ€р€濓級銆?/span>
绗旇€呮湡鏈涘府鍔╄鑰呮繁鍏ョ悊瑙?Raft 鍗忚锛屽苟鑳戒粯璇镐簬宸ョ▼瀹炶返涓紝鍚屾椂瑙h涓嶆槗鐞嗚В鎴栨槗璇В鐨勫叧閿偣锛岀湅瀹屼笉鎳備綘鏉ユ媿鎴戰煒?銆傝绯诲垪浠?span>鍘熺悊銆佹簮鐮併€佸疄璺?/strong>涓変釜閮ㄥ垎璁茶В Raft 鍘熺悊鍙婄畻娉曪細
鍘熺悊閮ㄥ垎缁撳悎 Raft 璁烘枃璁茶В Raft 绠楁硶鍘熺悊锛屽垎绡囬伒寰?Raft 鐨勬ā鍧楀寲鎬濇兂锛屽垎鍒瑙?Leader election銆丩og replication銆丼afety銆丆luster membership change銆丩og compaction 绛夈€?/span>
婧愮爜閮ㄥ垎鍒嗘瀽 hashicorp/raft 鏉ュ涔犱竴涓伐涓氱晫鐨?Raft 瀹炵幇锛坔ashicorp/raft 鏄?Consul 鐨勫簳灞備緷璧栵級銆?/span>
瀹炶返閮ㄥ垎鍩轰簬 hashicorp/raft 瀹炵幇涓€涓畝鍗曠殑鍒嗗竷寮?kv 瀛樺偍锛屼綔涓烘敹灏俱€?/span>
绯诲垪鍘嗗彶鏂囩珷閾炬帴锛?/span>
------------
鍓嶉潰鐨勭珷鑺傛垜浠杩颁簡 Raft 绠楁硶鏄浣曢€変富鍜屽鍒舵棩蹇楃殑锛岀劧鑰屽埌鐩墠涓烘鎴戜滑鎻忚堪鐨?span>杩欏鏈哄埗杩樹笉鑳戒繚璇佹瘡涓妭鐐圭殑鐘舵€佹満浼氫弗鏍兼寜鐓х浉鍚岀殑椤哄簭 apply 鏃ュ織銆傛兂璞′竴涓満鏅細
Leader 灏嗕竴浜涙棩蹇楀鍒跺埌浜嗗ぇ澶氭暟鑺傜偣涓婏紝杩涜 commit 鍚庡彂鐢熶簡瀹曟満銆?/span>
鏌愪釜 follower 骞舵病鏈夎澶嶅埗鍒拌繖浜涙棩蹇楋紝浣嗗畠鍙備笌閫変妇骞跺綋閫変簡涓嬩竴浠?leader銆?/span>
鏂扮殑 leader 鍙堝悓姝ュ苟 commit 浜嗕竴浜涙棩蹇楋紝杩欎簺鏃ュ織瑕嗙洊鎺変簡鍏跺畠鑺傜偣涓婄殑涓婁竴浠?committed 鏃ュ織銆?/span>
鍚勪釜鑺傜偣鐨勭姸鎬佹満鍙兘 apply 浜嗕笉鍚岀殑鏃ュ織搴忓垪锛屽嚭鐜颁簡涓嶄竴鑷寸殑鎯呭喌銆?/span>
鍥犳鎴戜滑闇€瑕佸鈥滈€変富+鏃ュ織澶嶅埗鈥濊繖濂楁満鍒跺姞涓婁竴浜涢澶栫殑闄愬埗锛屾潵淇濊瘉鐘舵€佹満鐨勫畨鍏ㄦ€?/strong>锛屼篃灏辨槸 Raft 绠楁硶鐨勬纭€с€?/p>
1. 瀵归€変妇鐨勯檺鍒?/span> 鎴戜滑鍐嶆潵鍒嗘瀽涓嬪墠鏂囨墍杩扮殑 committed 鏃ュ織琚鐩栫殑鍦烘櫙锛屾牴鏈棶棰樺叾瀹炲彂鐢熷湪绗?姝ャ€侰andidate 蹇呴』鏈夎冻澶熺殑璧勬牸鎵嶈兘褰撻€夐泦缇?leader锛屽惁鍒欏畠灏变細缁欓泦缇ゅ甫鏉ヤ笉鍙鏂欑殑閿欒銆侰andidate 鏄惁鍏峰杩欎釜璧勬牸鍙互鍦ㄩ€変妇鏃舵坊鍔犱竴涓皬灏忕殑鏉′欢鏉ュ垽鏂紝鍗筹細 姣忎釜 candidate 蹇呴』鍦?RequestVote RPC 涓惡甯﹁嚜宸辨湰鍦版棩蹇楃殑鏈€鏂?(term, index)锛屽鏋?follower 鍙戠幇杩欎釜 candidate 鐨勬棩蹇楄繕娌℃湁鑷繁鐨勬柊锛屽垯鎷掔粷鎶曠エ缁欒 candidate銆?/strong> Candidate 鎯宠璧㈠緱閫変妇鎴愪负 leader锛屽繀椤诲緱鍒伴泦缇ゅぇ澶氭暟鑺傜偣鐨勬姇绁紝閭d箞瀹冪殑鏃ュ織灏变竴瀹氳嚦灏戜笉钀藉悗浜庡ぇ澶氭暟鑺傜偣銆傚張鍥犱负涓€鏉℃棩蹇楀彧鏈夊鍒跺埌浜嗗ぇ澶氭暟鑺傜偣鎵嶈兘琚?commit锛屽洜姝?span>鑳借耽寰楅€変妇鐨?candidate 涓€瀹氭嫢鏈夋墍鏈?committed 鏃ュ織銆?/p>
鍥犳鍓嶄竴绡囨枃绔犳垜浠墠浼氭柇瀹氬湴璇达細Follower 涓嶅彲鑳芥瘮 leader 澶氬嚭涓€浜?committed 鏃ュ織銆?/p>
姣旇緝涓や釜 (term, index) 鐨勯€昏緫闈炲父绠€鍗曪細濡傛灉 term 涓嶅悓 term 鏇村ぇ鐨勬棩蹇楁洿鏂帮紝鍚﹀垯 index 澶х殑鏃ュ織鏇存柊銆?/p>
2. 瀵规彁浜ょ殑闄愬埗 闄や簡瀵归€変妇澧炲姞涓€鐐归檺鍒跺锛屾垜浠繕闇€瀵?commit 琛屼负澧炲姞涓€鐐归檺鍒讹紝鏉ュ畬鎴愭垜浠?Raft 绠楁硶鏍稿績閮ㄥ垎鐨勬渶鍚庝竴鍧楁嫾鍥俱€?/p>
鍥炲繂涓嬩粈涔堟槸 commit锛?/p>
鎵€璋?commit 鍏跺疄灏辨槸瀵规棩蹇楃畝鍗曡繘琛屼竴涓爣璁帮紝琛ㄦ槑鍏跺彲浠ヨ apply 鍒扮姸鎬佹満锛屽苟閽堝鐩稿簲鐨勫鎴风璇锋眰杩涜鍝嶅簲銆?/p>
鐒惰€?leader 骞朵笉鑳藉湪浠讳綍鏃跺€欓兘闅忔剰 commit 鏃т换鏈熺暀涓嬬殑鏃ュ織锛屽嵆浣垮畠宸茬粡琚鍒跺埌浜嗗ぇ澶氭暟鑺傜偣銆俁aft 璁烘枃缁欏嚭浜嗕竴涓粡鍏稿満鏅細 鍥? 鍥?浠庡乏鍒板彸鎸夋椂闂撮『搴忔ā鎷熶簡闂鍦烘櫙銆?/p>
闃舵a锛歋1 鏄?leader锛屾敹鍒拌姹傚悗灏?(term2, index2) 鍙鍒剁粰浜?S2锛屽皻鏈鍒剁粰 S3 ~ S5銆?/p>
闃舵b锛歋1 瀹曟満锛孲5 褰撻€?term3 鐨?leader锛圫3銆丼4銆丼5 涓夌エ锛夛紝鏀跺埌璇锋眰鍚庝繚瀛樹簡 (term3, index2)锛屽皻鏈鍒剁粰浠讳綍鑺傜偣銆?/p>
闃舵c锛歋5 瀹曟満锛孲1 鎭㈠锛孲1 閲嶆柊褰撻€?term4 鐨?leader锛岀户缁皢 (term2, index2) 澶嶅埗缁欎簡 S3锛屽凡缁忔弧瓒冲ぇ澶氭暟鑺傜偣锛屽皢鍏?commit銆?/p>
闃舵d锛歋1 鍙堝畷鏈猴紝S5 鎭㈠锛孲5 閲嶆柊褰撻€?leader锛圫2銆丼3銆丼4 涓夌エ锛夛紝灏?(term3, inde2) 澶嶅埗缁欎簡鎵€鏈夎妭鐐瑰苟 commit銆傛敞鎰忥紝姝ゆ椂鍙戠敓浜嗚嚧鍛介敊璇紝宸茬粡 committed 鐨?(term2, index2) 琚?(term3, index2) 瑕嗙洊浜嗐€?/p>
涓轰簡閬垮厤杩欑閿欒锛屾垜浠渶瑕佹坊鍔犱竴涓澶栫殑闄愬埗锛?/p>
Leader 鍙厑璁?commit 鍖呭惈褰撳墠 term 鐨勬棩蹇椼€?/strong> 閽堝涓婅堪鍦烘櫙锛岄棶棰樺彂鐢熷湪闃舵c锛屽嵆浣夸綔涓?term4 leader 鐨?S1 灏?(term2, index2) 澶嶅埗缁欎簡澶у鏁拌妭鐐癸紝瀹冧篃涓嶈兘鐩存帴灏嗗叾 commit锛岃€屾槸蹇呴』绛夊緟 term4 鐨勬棩蹇楀埌鏉ュ苟鎴愬姛澶嶅埗鍚庯紝涓€骞惰繘琛?commit銆?/p>
闃舵e锛氬湪娣诲姞浜嗚繖涓檺鍒跺悗锛岃涔?(term2, index2) 濮嬬粓娌℃湁琚?commit锛岃繖鏍?S5 鍦ㄩ樁娈礵灏嗗叾瑕嗙洊灏辨槸瀹夊叏鐨勶紱瑕佷箞 (term2, index2) 鍚?(term4, index3) 涓€璧疯 commit锛岃繖鏍?S5 鏍规湰灏辨棤娉曞綋閫?leader锛屽洜涓哄ぇ澶氭暟鑺傜偣鐨勬棩蹇楅兘姣斿畠鏂帮紝涔熷氨涓嶅瓨鍦ㄥ墠杈圭殑闂浜嗐€?/p>
浠ヤ笂渚挎槸瀵圭畻娉曞鍔犵殑涓や釜灏忛檺鍒讹紝瀹冧滑瀵圭‘淇濈姸鎬佹満鐨勫畨鍏ㄦ€ц捣鍒颁簡鑷冲叧閲嶈鐨勪綔鐢ㄣ€?/p>
鑷虫鎴戜滑瀵?Raft 绠楁硶鐨勬牳蹇冮儴鍒嗭紝宸茬粡浠嬬粛瀹屾瘯锛屽鏋滄偍鍧氭寔闃呰鍒版湰鏂囷紝鐩镐俊宸茬粡鑳藉緢濂芥帉鎻?Raft 鍗忚鐨勬牳蹇冪畻娉曟満鍒躲€?/p>
涓嬩竴绡囨垜浠細浠嬬粛涓や釜鍚屾牱鎻忚堪浜庤鏂囧唴鐨勮緟鍔╂妧鏈細闆嗙兢鎴愬憳鍙樻洿鍜屾棩蹇楀帇缂╋紝瀹冧滑閮芥槸鍦?Raft 宸ョ▼瀹炶返涓繀涓嶅彲灏戠殑閮ㄥ垎锛屽崗鍔╄鑰呭湪婧愮爜鍒嗘瀽鍓嶅厖鍒嗘帉鎻℃牳蹇冨師鐞嗐€?/p>
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章