1 raft要達(dá)成一致,是需要半數(shù)以上的節(jié)點(diǎn)同意的,但是這個(gè)邏輯在jraft中非常的隱蔽本砰。
這個(gè)邏輯是在Ballot類(lèi),在init方法中我們看到
this.quorum = this.peers.size() / 2 + 1;
這個(gè)就是核心點(diǎn)了蓝谨。
Ballot類(lèi)的grant的核心邏輯就是灌具,如果命中了初始化的節(jié)點(diǎn)之一,就
this.quorum--;
Ballot類(lèi)的最后判斷也就很簡(jiǎn)單了譬巫,就是判斷quorum是不是小于0
public boolean isGranted() {
return this.quorum <= 0 && oldQuorum <= 0;
}
2 Ballot的isGranted方法在使用上也有點(diǎn)容易困惑
image.png
這個(gè)代碼咋一看咖楣,以為waiter.onCommitted肯定是會(huì)被執(zhí)行到的,但其實(shí)中途有個(gè)return芦昔。
條件是lastCommittedIndex == 0诱贿,只有沒(méi)超過(guò)半數(shù)lastCommittedIndex == 0才成立,只要超過(guò)半數(shù)lastCommittedIndex = logIndex咕缎。
所以這樣才是把邏輯串起來(lái)珠十,只是初次看不容易理解