鑱婅亰 Raft 绠楁硶

Posted SegmentFault

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鑱婅亰 Raft 绠楁硶相关的知识,希望对你有一定的参考价值。

鏈枃杞浇浜?SegmentFault 绀惧尯



 


涓€銆佹杩?/span>


鏈€杩戠粓浜庢湁鏃堕棿鍘荤湅鐪?raft 绠楁硶鐨勮鏂囷紝raft 浣滀负涓€涓槗浜庣悊瑙e拰瀹炵幇鐨勪竴鑷存€х畻娉?/span>锛堟瘯绔熸垜杩欐牱鐨勮彍馃惗閮借兘鐪嬫噦锛?/span>锛屽凡缁忓簲鐢ㄥ埌寰堝绯荤粺鐨勬瀯寤轰笂锛堝垎甯冨紡鏁版嵁搴?TIDB锛孯ocketMq 鐨?Dledger 绛夛級锛屾槸闈炲父鍊煎緱鍘绘繁鍏ョ悊瑙e拰鐮旂┒鐨勩€傛湰鏂囦富瑕佹槸瀛︿範 raft 璁烘枃鐨勬€荤粨浠ュ強鑷繁鐨勪竴浜涙兂娉曘€傚鏋滃ぇ瀹跺璁烘枃宸茬粡瓒冲浜嗚В銆傞偅灏变笉鐢ㄧ户缁線涓嬬湅浜嗐€傚厜鐪嬬湅鏂囨湯鐨勪釜浜烘兂娉?/span>锛堟剼瑙侊級锛屾杩庢潵鍜屾垜鎺㈣锛?/span>





浜屻€丷aft 绠楁硶姒傝堪


raft 绠楁硶涔嬫墍浠ュ鏄撶悊瑙o紝鍏朵竴鏄粬灏嗕竴鑷存€ч棶棰樺垝鍒嗘垚鍑犱釜瀛愰棶棰橈紝杩欏嚑涓瓙闂閮芥槸鐙珛銆佸彲鐞嗚В鍜岃В閲婄殑銆備粠浼犵粺鐨勬€濈淮鏉ヨ锛屽浜庝竴涓鏉傜殑绯荤粺鎴栬€呭伐绋嬶紝閮芥槸澶у寲灏忥紝鍒嗚В瀹炵幇锛岀劧鍚庡幓灏濊瘯铻嶅悎瑙e喅鏁翠綋閫昏緫銆傚寘鎷?CS 绯荤粺鐨勮璁′篃鏄姝ゃ€?/span>


涓€鑷存€х畻娉曠殑鐩爣


1. 瀹夊叏鎬э細鍦ㄩ潪鎷滃崰搴敊璇儏鍐典笅锛屽寘鎷綉缁滃欢杩熴€佸垎鍖恒€佷涪鍖呫€佸啑浣欏拰涔卞簭绛夐敊璇兘鍙互淇濊瘉姝g‘銆?br>2. 鍙敤鎬э細鍙闆嗙兢涓ぇ澶氭暟鑺傜偣澶勪簬 runing锛屽苟涓斾笉鍒嗗尯锛屽拰瀹㈡埛绔兘閫氫俊锛岄偅涔堟垜浠渶瑕佷繚璇佽繖涓泦缇ゅ彲鐢ㄣ€?br>3. 瀵逛簬鏁版嵁鍚屾锛屽皬閮ㄥ垎鎱㈣妭鐐圭殑涓嶄細褰卞搷绯荤粺鎬ц兘銆傚洜涓哄浜庢棩蹇楀鍒讹紝鎴戜滑濡傛灉绛夊緟鎵€鏈夎妭鐐瑰搷搴旓紝閭d箞绯荤粺鐨勬€ц兘浼氬瓨鍦ㄧ煭鏉挎晥搴斻€?/span>

璇寸櫧浜嗭紝灏辨槸濡傛灉涓€涓泦缇や腑锛屽鏋滃ぇ澶氭暟鑺傜偣鍙敤 锛堢綉缁溿€佹湇鍔★級 锛岄偅涔堥€氳繃 raft 绠楁硶锛屾垜浠氨鑳戒繚璇佹暣涓郴缁熷彲鐢?/span> 锛堝彲澶勭悊璇锋眰锛屾暟鎹竴鑷存€э級 銆傚悗闈㈡垜浠富瑕佺爺绌剁殑灏辨槸 raft 鏄浣曞仛鍒扮殑銆?/span>


棣栧厛鎴戜滑瑕佺煡閬擄紝Raft 绠楁硶灏嗗叾闂鍒掑垎涓?/span>


  • 棰嗗閫変妇
  • 鏃ュ織澶嶅埗
  • 瀹夊叏鎬?/span>


瀵逛簬涓€涓泦缇ゅ彧鏈変竴涓?/span> leader锛堥瀵硷級锛岄偅涔堟垜浠氨寰堝鏄撶悊瑙c€傚彧瑕侀瀵兼搷浣滃悓姝ュ埌瀵瑰簲鐨?/span> followers锛堣窡闅忚€咃級锛屾暟鎹繀鐒朵竴鑷淬€傚綋 leader 瀹曟満锛岄渶瑕佽繘琛岄瀵奸€変妇銆?/span>

鏃ュ織澶嶅埗鍏跺疄灏辨槸鍚屾鎿嶄綔鏁版嵁鐨勮繃绋嬨€俵eader 灏嗘搷浣滄棩蹇楀悓姝ュ埌鍏朵粬鑺傜偣銆?/span>


瀹夊叏鎬э細濡備綍瀹夊叏鐨勫悓姝ワ紝鍦ㄤ笉鍚岀殑鎯呭喌锛屾垜浠兘鑳戒繚璇佷竴鑷存€э紝杩欎篃灏辨槸瀹夊叏鎬ч渶瑕佽€冭檻鐨勯棶棰樸€?/span>

鍏跺疄灏辨槸濡傛锛宺aft 棣栧厛鍋囪浜嗛瀵奸€変妇銆傜劧鍚庡疄鐜颁簡鏃ュ織澶嶅埗锛屾渶鍚庡湪瀹夊叏闂涓婅В鍐充笂闈㈢殑婕忔礊闂銆?





涓夈€侀瀵奸€変妇


鐩殑锛氬綋闆嗙兢鍒濆鍖栨垨鑰呴瀵?gg 鐨勬椂鍊欓€夊嚭涓€涓柊鐨勯瀵笺€傛瘯绔熶竴涓泦缇や笉鑳芥病鏈夐瀵硷紝濡傛灉娌℃湁锛岄偅涔堣繖涓泦缇ゅ氨涓嶅彲鐢ㄤ簡銆?/span>


瑙﹀彂鏈哄埗锛氶€氳繃蹇冭烦銆?/span>

 


杩欏箙鍥惧睍鐜颁簡璺熼殢鑰呫€佸€欓€夎€呭拰棰嗗鑰呬箣闂寸殑鐘舵€佽浆鎹㈠叧绯伙紝涓嬮潰涓昏浠嬬粛浠栦滑鐨勮浆鎹㈡祦绋嬨€?/span>


璺熼殢鑰?/span>


濡傛灉浠栬兘鎸佺画浠庨瀵艰€呮垨鑰呭€欓€夎€呮帴鏀跺埌鏈夋晥鐨?/span> RPCs锛岄偅涔堜粬鐨勭姸鎬佸氨涓嶄細鍙樸€備竴鐩翠繚鎸佽窡闅忚€呰韩浠姐€備絾濡傛灉娌℃湁鎸佺画鎺ュ彈锛屼篃灏辨槸鍦ㄤ竴娈垫椂闂存病鏀跺埌鏈夋晥鐨?/span> RPCs锛岄偅灏遍€変妇瓒呮椂锛屼粬浼氬彉鎴愬€欓€夎€呫€?/span>


鍊欓€夎€?/span>


瑕佸彉涓哄€欓€夎€咃紝涔熷氨鎰忓懗鐫€浠栬寮€鍚竴杞柊鐨勯€変妇銆傞偅涔堣窡闅忚€呬細澧炲姞鑷繁鐨勪换鏈熷彿锛岃浆涓哄€欓€夎€呫€?/span>


鎴愪负鍊欓€夎€呭悗锛岃嚜宸变細骞惰鍙戦€佷竴涓负鑷繁鎶曠エ鐨?/span> RPCs 璇锋眰 缁欏叾浠栨湇鍔″櫒銆?/span>


鎴愪负鍊欓€夎€呯殑鏁翠釜杩囩▼涔熶細淇濇寔褰撳墠鐘舵€侊紝鐭ラ亾婊¤冻涓嬮潰涓変釜鏉′欢


  • 浠栬耽寰楅€変妇锛岃浆鍙樹负棰嗗鍒?/span>
  • 鍏朵粬鑺傜偣璧簡锛屼粬杞负璺熼殢鑰?/span>
  • 涓€娈垫椂闂存病鏈変换浣曚汉鑾疯儨銆傝鏄庨€夌エ琚摐鍒嗭紝閲嶅鎵ц銆?/span>


杩欓噷闇€瑕佹敞鎰忕殑鐐癸細


1. 瀵逛簬鍚屼竴浠绘湡鍙凤紝姣忎釜鑺傜偣涓€浼氭姇涓€寮犵エ銆傛瘮濡傛湇鍔″櫒 A 浣滀负鍊欓€夎€呯敓鎴愪簡缂栧彿涓?5 鐨勪换鏈熷彿锛岄偅涔堝鏋滄帴鏀跺埌鍏朵粬鑺傜偣鐨勭紪鍙蜂负 5 鐨勪换鏈熷彿鐨勬姇绁ㄨ姹傦紝浠栦細蹇界暐銆傝繖涓繃绋嬮伒寰殑鏄厛鏉ュ厛鎶曠殑鍘熷垯銆?br>2. 鍊欓€夎€呮帴鏀跺埌棰嗗鑰呯殑澹版槑銆備細鍒ゅ0鏄庝腑 RPC 鐨勪换鏈熷彿锛屽鏋滄瘮鑷繁鐨勮繕灏忥紝閭d箞浠栬繕浼氫繚鎸佸€欓€夎€呰韩浠斤紝鍚﹀垯杞负璺熼殢鑰呫€?br>3. 瀵逛簬涓婇潰绗笁鐐癸紝閲嶅鎵ц锛孯aft 閲囩敤闅忔満瓒呮椂閫変妇鏃堕棿杩涜浜嗕紭鍖栥€傞檷浣庨€夌エ鐡滃垎鐨勫彲鑳芥€с€?/span>





鍥涖€丷aft 鏃ュ織澶嶅埗


鐩存帴鍘荤悊瑙f棩蹇楀鍒讹紝鏄緢瀹规槗鐨勶紝瀹㈡埛绔殑涓€鏉℃寚浠ゅ埌杈撅紝棰嗗鑰呬細涓鸿繖鏉℃寚浠ゅ垱寤轰竴鏉℃棩蹇楁潯鐩紝骞朵笖骞惰鍙戦€佸埌鍏朵粬璺熼殢鑰呫€傚綋鏃ュ織琚畨鍏ㄥ鍒?/span>锛堟墍璋撳畨鍏ㄥ鍒跺悗闈細鏈夛級锛岄瀵间細灏嗘棩蹇楀簲鐢ㄥ埌鐘舵€佹満锛堟瘮濡傚鏋滄槸 mysql 鐨?insert锛岄偅涔堝氨鏄墽琛?insert 鎿嶄綔锛?/span>锛岀劧鍚庡搷搴斿鎴风銆?/span>


鑱婅亰 Raft 绠楁硶

濡備笂鍥撅紝姣忔潯鏃ュ織閮戒細鏈夊搴旂殑浠绘湡鍙凤紝鍜屾寚浠ゃ€?/span> 姣忎釜鏃ュ織閮戒細鏈夊搴旂殑绱㈠紩銆?/span>

raft 鏃ュ織鍖归厤鐗规€?/span>

1. 濡傛灉鍦ㄤ笉鍚岀殑鏃ュ織涓殑涓や釜鏉$洰鎷ユ湁鐩稿悓鐨勭储寮曞拰浠绘湡鍙凤紝閭d箞浠栦滑瀛樺偍浜嗙浉鍚岀殑鎸囦护銆?br>2. 濡傛灉鍦ㄤ笉鍚岀殑鏃ュ織涓殑涓や釜鏉$洰鎷ユ湁鐩稿悓鐨勭储寮曞拰浠绘湡鍙凤紝閭d箞浠栦滑涔嬪墠鐨勬墍鏈夋棩蹇楁潯鐩篃鍏ㄩ儴鐩稿悓銆?/span>

绗竴鐐癸細涓€涓换鏈熷彧鏈変竴涓瀵间汉锛屽苟涓旈瀵间汉鍦ㄤ竴涓换鏈熶腑瀵逛簬鍚屼竴绱㈠紩鏃ュ織锛屽彧浼氬垱寤轰竴鏉℃棩蹇楋紝鏄笉浼氭敼鍙樼殑锛屾槸纭畾鐨勩€傝繖灏变繚璇佺涓€鐐规垚绔嬨€?/span>

绗簩鐐癸細瑕佹兂鍏ㄩ儴鐩稿悓锛屽氨瑕佷繚璇佽窡闅忚€呭緱鍒扮殑鏃ュ織鏄瀵艰€呭彂閫佺殑椤哄簭闄勫姞涓婂幓鐨勩€傞瀵艰€呭湪鍙戦€佹柊鐨勬棩蹇楁椂锛屼細闄勫姞杩欐潯鏃ュ織涔嬪墠鏃ュ織鐨勭储寮曞拰浠绘湡鍙枫€傚鏋滆窡闅忚€呭彂鐜版暟鎹尮閰嶏紝鎵嶄細闄勫姞涓婂幓锛屽惁鍒欐嫆缁濄€傚氨鏄竴涓釜鐘舵€佷繚璇佷簡鏃ュ織鐨勫尮閰嶇壒鎬с€?/span>

瀵逛簬鏃ュ織涓嶄竴鑷寸殑鐜拌薄锛宺aft 鏄€氳繃璺熼殢鑰呭己鍒跺鍒堕瀵艰€呯殑鏃ュ織鏉ヤ繚璇佺殑銆?/span>


鑱婅亰 Raft 绠楁硶


濡備笂鍥撅紝瀵逛簬 a-f锛屾渶缁堥兘浼氬拰 leader 鍚屾锛屼篃灏辨槸璇达紝d 浼氫涪寮冩棩蹇椼€俧鐨勫搴旀棩蹇椾篃浼氳涓㈠純鍜岃鐩栥€?/span>
鍏跺疄灏辨槸閫氳繃鏃ュ織瑕嗙洊瑙e喅銆備絾鏄浜庢棩蹇楄鐩栵紝鎴戜滑灏变細鎯冲埌涓€涓棶棰橈紝浼氫笉浼氳鐩栧凡缁忔彁浜ょ殑鏃ュ織 锛堟棩蹇楀搴旀寚浠ゅ凡缁忚繑鍥炵粰瀹㈡埛绔級 銆傞偅褰撶劧涓嶄細锛屽鏋滅湡鏈夎繖鏍凤紝灏变細鏈変笉涓€鑷达紝鎴栬€呮寚浠や涪澶辩幇璞°€?/span>

閭d箞濡備綍鍘诲仛瑕嗙洊璺熼殢鑰呮棩蹇?/span>

鍏跺疄灏辨槸璺熼殢鑰呭湪 append 鏃ュ織鐨勬椂鍊欙紝浼氳繘琛岄敊璇牎楠屻€?/span>

鍦ㄥ€欓€夎€呮垚涓洪瀵艰€呯殑鏃跺€欙紝浼氫负姣忎釜璺熼殢鑰呭垵濮嬪寲涓€涓?/span> nextIndex 鏁扮粍锛屾暟缁勭殑鍊煎垵濮嬪寲涓鸿嚜宸辨渶鍚庢潯鏃ュ織 +1锛屽叾瀹炲氨鏄悊鎯冲寲鐘舵€侊紝榛樿璁や负鏃ュ織閮藉凡缁忓悓姝ユ垚鍔熴€備絾鏄悊鎯虫€讳細鍥犱负鍚勭鍘熷洜瀵艰嚧涓嶆纭紝灏辩敤涓婇潰閭e紶鍥俱€?/span> leader 浼氬垵濮嬪寲鎵€鏈? nextIndex 涓?11锛屼絾鏄湪鍚屾鏃ュ織鐨勮繃绋嬩腑銆俧 鑺傜偣浼氬嚭鐜版牎楠岄敊璇殑鍝嶅簲銆傚洜涓?f 鑺傜偣 10 绱㈠紩瀵瑰簲鐨勬棩蹇楀拰 leader10  绱㈠紩瀵瑰簲鐨勬棩蹇椾笉鐩稿悓 锛堜富瑕佹槸鏍规嵁浠绘湡鍙峰垽鏂級

杩欓噷鍐嶅己璋冧竴涓嬩负浠€涔堟牴鎹换鏈熷彿灏卞彲浠ュ垽鏂棩蹇楁槸鍚︿竴鑷达紝灏辨槸涓婇潰鎵€璇寸殑鏃ュ織鍖归厤鍘熷垯銆?/span>



 

浜斻€佸畨鍏ㄦ€?/span>


鎴戜滑鏄庣櫧浜嗗浣曢€変妇鍜屾棩蹇楀鍒讹紝浣嗘槸娌℃湁鑰冭檻瀹夊叏鎬ч棶棰樸€傚叾瀹炴垜涓婃參鎻愬埌锛屾瘮濡備竴涓畷鏈哄緢涔呯殑璺熼殢鐫€浼氳閫変负棰嗗鑰咃紝杩涜鏃ュ織瑕嗙洊鎿嶄綔浼氭湁涓㈠け闂銆?/span>
鍏跺疄瑙e喅杩欎釜鍔炴硶寰堢畝鍗曪紝灏辨槸鍦ㄩ瀵奸€変妇鐨勬椂鍊欙紝鍙兘璁╁畨鍏ㄧ殑鑺傜偣褰?leader锛屾墍璋撳畨鍏紝灏辨槸瀵瑰簲鑺傜偣鎷ユ湁褰撳墠棰嗗鑰呭凡缁忔彁浜ょ殑鎵€鏈夋棩蹇椼€俁aft 灏辨槸杩欎箞鍋氱殑銆?

Raft 涓妭鐐瑰湪鎶曠エ鐨勬椂鍊欙紝浼氬垽鏂鎶曠エ鐨勫€欓€夎€呭搴旂殑鏃ュ織鏄惁鑷冲皯鍜岃嚜宸变竴鏍锋柊銆傚鏋滀笉鏄紝鍒欎笉浼氱粰璇ュ€欓€夎€呮姇绁ㄣ€?/span>

鏃ュ織姣旇緝鐨勬柟娉曪細

1. 鏈€鍚庝竴鏉℃棩蹇楃殑浠绘湡鍙枫€傚鏋滃ぇ璇存槑鏂般€傚鏋滃皬锛岃鏄庝笉鏂般€傚鏋滅浉绛夈€傝烦鍒?
2. 鍒ゆ柇绱㈠紩闀垮害銆傚ぇ鐨勬洿鏂般€?/span>

杩樻湁涓€涓棶棰橈紝灏辨槸棰嗗浜轰笉鑳戒繚璇佷竴涓凡缁忓湪澶у鏁拌妭鐐瑰瓨鍦ㄧ殑鏃ュ織鏄惁宸茬粡鎻愪氦銆?/span>

鑱婅亰 Raft 绠楁硶


濡傚浘锛?/span> a銆乥銆乧銆乨銆乪 浠h〃涓嶅悓鐨勪换鏈熼樁娈?/span>

锛坅锛塖1 鏄?leader銆傚悓姝ヤ换鏈?2 鐨勬暟鎹粰 S2
锛坆锛塖1 瀹曟満锛孲5 褰撻€?/span> 锛圫3銆丼4銆丼5 鎶曠エ锛?/span> 锛屼骇鐢熶换鏈?3 鐨勬棩蹇?/span>
锛坈锛塖5 瀹曟満锛孲1 鎭㈠褰撻€?/span> 锛堝悓姝ヤ换鏈?2 鐨勬暟鎹粰 S3锛?/span> 銆?/span>
锛坉锛塖1 瀹曟満锛孲5 褰撻€?/span> 锛堝洜涓轰粬鐨勪换鏈熸棩蹇楁瘮鍏朵粬鐨勯兘鏂帮級 锛屽鍒朵簡浠绘湡 3 鐨勬墍鏈夋暟鎹€?/span>

鍋囧璇村湪 c 闃舵锛孲1 鎻愪氦浜嗕换鏈?2 鐨勬暟鎹紝閭d箞濡傛灉鍑虹幇 d锛屽垯浼氬鑷翠换鏈?2 鏁版嵁琚鐩栵紝涓㈠け銆備篃灏辨槸璇达紝S1 鍦ㄤ换鏈?4 鏃跺€欙紝涓嶈兘淇濊瘉宸茬粡鍦ㄥぇ澶氭暟鑺傜偣瀛樺湪鐨勬棩蹇?/span> 锛堜换鏈?2 鐨勬棩蹇楋級 鏄惁鎻愪氦銆?/span>
鎵€浠?raft 姘歌繙涓嶄細閫氳繃璁$畻鍓湰鏁扮洰鐨勬柟寮?/span> 锛堝ぇ澶氭暟瀛樺湪锛?/span> 鍘绘彁浜や竴涓箣鍓嶄换鏈熷唴 锛堜换鏈?2锛?/span> 鐨勬棩蹇楁潯鐩€傚彧鏈夐瀵间汉褰撳墠浠绘湡閲岀殑鏃ュ織鏉$洰閫氳繃璁$畻鍓湰鏁扮洰鍙互琚彁浜?/span> 锛坋 闃舵锛?/span> 銆傝繖鏍蜂箣鍓嶄换鏈熺殑鏁版嵁涔熶細琚彁浜ゃ€?/span>

閭h繖閲屾垜鐞嗚В涓€鐐瑰氨鏄紝鍔犲叆 S1 褰撻€変负 leader锛屽鍥?c 鐘舵€侊紝閭d箞锛屽鏋滀笉鍐嶆湁鏂扮殑鏃ュ織鍑虹幇锛屼换鏈?2 瀵瑰簲鐨勬棩蹇楀氨涓嶄細鎻愪氦銆傞偅涔堜細瀵艰嚧瀹㈡埛绔搴旂殑浠绘湡 2 璇锋眰澶辫触銆?/span>




鍏€佽窡闅忚€呭拰鍊欓€変汉瀹曟満


杩欎釜灏辨瘮杈冨鏄撶悊瑙d簡锛屽畷鏈虹殑璇? RPCs 灏变細澶辫触锛孯aft 閫氳繃鏃犻檺閲嶈瘯鍗磋В鍐宠繖涓棶棰樸€?/span>

鎵€浠ュ浜庢瘡涓?/span> RPCs 锛屽仛鍒板箓绛夊拰鏃犻檺閲嶈瘯锛屽湪鑺傜偣鎭㈠鍚庯紝灏辫繕鏄細淇濊瘉涓€鑷存€х姸鎬併€?/span>




涓冦€丷aft 闆嗙兢鎴愬憳鍙樺寲


瀵逛簬闆嗙兢鎴愬憳閰嶇疆鍙樺寲锛屽鏋滅洿鎺ユ洿鏂版瘡鍙版満鍣ㄩ厤缃紝閭d箞灏变細鏈夊畨鍏ㄦ€ч棶棰樸€備互涓哄浜庡悓涓€鏃跺埢锛屼笉鍚岃妭鐐逛娇鐢ㄧ殑涓嶅悓鐨勯厤缃幓鎵ц绠楁硶閫昏緫锛岃繖灏辨槸涓嶅畨鍏ㄧ殑銆?/span>

鑱婅亰 Raft 绠楁硶
濡傚浘锛岃摑鑹蹭唬琛ㄦ柊鐨勯厤缃€傜豢鑹蹭唬琛ㄨ€佺殑閰嶇疆銆侽ld 鐘舵€佹湁涓夊彴鏈哄櫒 Server1銆?銆?銆侼ew 鍔犲叆涓ゅ彴 server4銆?銆?

閭d箞闅忕潃閰嶇疆鏃堕棿搴旂敤鐨勪笉鍚岋紝鍙兘浼氬鑷撮€変妇鍑轰袱涓? leader 銆?/span>

姣斿
server1銆? 浣跨敤鑰侀厤缃紝閭d箞1鍜?閮芥湁鍙兘琚綋閫変负 leader 銆?/span> 3銆?銆? 浣跨敤蹇冨緱閰嶇疆锛屼粬浠箣涓殑涓€涓篃浼氳褰撻€変负 leader 銆?/span>

鍏跺疄杩欎釜闂鍘熷洜灏辨槸鑺傜偣浣跨敤浜嗕笉鍚岄厤缃墽琛岀畻娉曢€昏緫銆備负浜嗚В鍐宠繖涓棶棰橈紝raft 閲囩敤涓ら樁娈垫柟娉?/span> 锛堝叾瀹炲彧闇€瑕佷繚璇佷笉浼氳鏂版垨鑰呮棫閰嶇疆鍗曠嫭浣滃嚭鍐冲畾灏辫锛?/span>


raft 鎶婇厤缃綋浣滄櫘閫氭棩蹇楀舰寮忓幓鎻愪氦銆?/span>

涓轰簡瀹炵幇涓ら樁娈碉紝寮曞叆浜?C (old銆乶ew) 閰嶇疆銆?br>
杩樻湁涓€鐐瑰氨鏄紝涓€鐐逛竴涓柊鐨勯厤缃棩蹇楀鍔犲埌瀵瑰簲鐨勮妭鐐规棩蹇椾腑锛岄偅涔堣鑺傜偣灏变細绔嬪埢浣跨敤杩欐潯鏂扮殑鏃ュ織閰嶇疆銆?/span>
瀵逛簬 C锛坥ld銆乶ew锛夐厤缃紝鍏跺疄灏辨槸鍙湁鍚屾椂婊¤冻 old 鍜?new 閰嶇疆鐨勬椂鍊欐墠浼氱敓鏁堛€?


杩欐牱鐞嗘兂鐘舵€佷笅锛屽鏋滄嫢鏈?/span> C锛坥ld銆乶ew锛?/span> 閰嶇疆鐨勮妭鐐瑰綋閫変负 leader 銆傚苟涓旀彁浜や簡璇ラ厤缃紝閭d箞璇存槑 C锛坥ld銆乶ew锛?/span> 閰嶇疆宸茬粡鍦ㄥぇ澶氭暟鑺傜偣搴旂敤銆備笅娆¢€変妇鐨勪骇鐢熺殑 leader 鏃ュ織涓繀鐒朵細鏈夎閰嶇疆銆傝繖涓椂鍊欏湪鍒涘缓涓€鏉℃柊鐨?/span> C锛坣ew锛?/span> 閰嶇疆鎻愪氦锛屽嵆鍙€?/span>




鍏€佷釜浜烘兂娉曟€荤粨


鍏跺疄瀵逛簬 raft 绠楁硶锛岃鏂囨弿杩拌繕鏄潪甯告竻鏅板苟涓斿鏄撶悊瑙g殑銆傛牳蹇冮棶棰樼殑鍒嗚В銆傝鎴戜滑鐨勬€濊矾浼氬彉寰楅潪甯告竻鏅般€?/span>

瀵逛簬 raft 鐨勮璁★紝鍏朵腑浠绘湡杩欎釜姒傚康鏄潪甯哥粷濡欑殑銆傛棤璁烘槸鍦ㄩ瀵间汉閫変妇锛屾棩蹇楀鍒剁殑鏃跺€欙紝閮芥槸闈炲父渚濊禆浠绘湡鐨勩€?/span>

浠绘湡杩欎釜鍗曡皟澧為暱鐨勬蹇碉紝璁╁鑺傜偣澶勭悊浜嬫儏鍙樺緱闈炲父鏄撲簬鐞嗚В銆?/span>

瀵逛簬棰嗗閫変妇锛岃鏂囧仛鍒拌鎴戜滑鍙叧娉ㄩ€変妇闂銆傞€氳繃鍗曡皟澧為暱浠绘湡锛屼互鍙婃姇绁ㄨ繘琛岄€変妇锛屽苟涓斿湪閫昏緫涓婅璁′簡鑺傜偣鍦ㄥ悇涓鑹茬殑鑱岃兘鍜岃浆鎹€?/span>

瀵逛簬鏃ュ織澶嶅埗锛屼篃鏄€氳繃鏃ュ織鍖归厤瑙勫垯淇濊瘉浜嗘棩蹇楀鍒剁殑姝g‘鎵ц銆傚叾瀹炶繕鏄緷璧栦换鏈熴€傛棤璁烘槸鍦ㄦ棩蹇楁彁浜わ紝杩樻槸鍦ㄥ垽鏂槸鍚︽帴鍙楃殑鏃跺€欙紝閮芥槸渚濊禆浠绘湡锛屽繀瑕佹椂渚濊禆鏃ュ織绱㈠紩銆傚鏃ュ織鏁版嵁鏄棤鐘舵€佺殑銆?/span>

瀹屾垚涓婅堪涓や釜杩囩▼锛屽鏋滀笉鍘荤湅瀹夊叏鎬э紝鎴戜滑鍏跺疄鑷繁閮借兘鎯冲埌閫変妇杩囩▼涓鏋滄病鏈夐檺鍒舵潯浠剁殑婕忔礊銆傚湪澶嶅埗杩囩▼涓彲鑳戒細瀛樺湪鏁版嵁琚鐩栦涪澶辩殑闂銆?/span>

鐒跺悗 raft 閫氳繃鍊欓€変汉鐨勬渶鍚庢棩蹇楁潯鐩殑绱㈠紩鍊煎拰鍊欓€変汉鏈€鍚庢棩蹇楁潯鐩殑浠绘湡鍙锋潵鍐冲畾鏄惁鑳藉綋閫夈€?/span>




閫氳繃瀵圭畻娉曠殑瀛︿範锛岃鎴戜滑鏇村姞鏄撲簬鍘荤悊瑙e垎甯冨紡涓€鑷存€ч棶棰樸€傚苟涓旇浜哄拰鎯婂枩鐨勬槸锛屾牴鎹鏂囨弿杩帮紝鎴戜滑鑷繁鍘荤敤浠g爜瀹炵幇绠楁硶鏄潪甯稿鏄撶殑銆傛瘯绔熻鏂囧紑绡囦负鎴戜滑鍚ф暟鎹粨鏋勯兘瀹氫箟濂戒簡銆?/span>

鏂囩珷鍙傝€冿細

raft 璁烘枃锛?/span>In search of an Understandable Consensus Algorithm (Extended Version)
https://ramcloud.atlassian.net/wiki/download/attachments/6586375/raft.pdf



鐐瑰嚮宸︿笅瑙掗槄璇诲師鏂囷紝娆㈣繋鍒?nbsp; SegmentFault 鎬濆惁绀惧尯  鍜屾枃绔犱綔鑰呭睍寮€鏇村浜掑姩鍜屼氦娴併€?/span>

- END -

以上是关于鑱婅亰 Raft 绠楁硶的主要内容,如果未能解决你的问题,请参考以下文章

闈㈣瘯瀹橈細鑱婅亰 Java 鐨勫嚑鎶?JVM 绾ч攣鍚?/h1>

鍥剧殑绠楁硶---寮楁礇浼婂痉绠楁硶

绠楁硶鏁版嵁缁撴瀯01 /绠楁硶鏁版嵁缁撴瀯姒傝堪

鎷撳睍KMP绠楁硶 鍏ラ棬+妯℃澘

銆愬崄澶╄嚜鍒惰蒋娓叉煋鍣ㄣ€慏AY 02锛氱敾涓€鏉$洿绾匡紙DDA 绠楁硶 & Bresenham鈥檚 绠楁硶锛?/a>

鍐掓场绠楁硶