Raft 算法相关,第1张

Raft 算法相关,第2张

抛出原论文:

Raft uses the voting process to prevent a candidate from winning an election unless its log contains all committed entries. A candidate must contact a majority of the cluster in order to be elected, which means that every committed entry must be present in at least one of those servers. If the candidate ’ s log is at least as up-to-date as any other log in that majority (where “ up-to-date ” is defined precisely below), then it will hold all the committed entries... 这是说,leader 必须包含所有已提交的日志,那么选择时候选节点必须是 up-to-date 的。

Raft determines which of two logs is more up-to-date by comparing the index and term of the last entries in the logs. If the logs have last entries with different terms, then the log with the later term is more up-to-date. If the logs end with the same term, then whichever log is longer is more up-to-date. 这是说,判断 up-to-date 的标准是,term 号越大越新; term 号一致则日志越长越新。

我想问下,满足这个 up-to-date 标准,为什么该候选节点就包含了所有的已提交日志了?

----------------------- 以下是精选回复-----------------------

答:term 越高,说明这个节点与 leader 进行数据同步的时间越近,自然其 commitIndex 越准确;这是前提,因为 commit 只能由 leader 进行
如果两个节点的 term 一致,说明他们都有同一个 leader,同一个 leader 的 commitIndex 是一致的,所以选择 commitIndex 较大的那个
答:因为假设某节点会 win the election 但是有一些 committed 的 log 它没有,那么根据 committed 的标准,一定会有等于一半总节点数的活着的节点含有了这个 committed 的 log。而因为这些节点有该节点没有的而且 committed 的 log, 那么这些其他节点就要么比该节点 term 大,要么与该节点同 term 但比该节点 log 长,因此则会比该节点更 up-to-date,从而导致有大于等于一半总节点数(有 committed log 的那些节点和失联的 leader 节点)的节点不给他 vote, 使其不能 win the election. 最后会是那些有更多 committed 的 log 的节点 win the election. 因此,win the election 的节点一定是含有所有 committed 的 log 的节点。不知道说的对不对,欢迎讨论。如果这回答写在知乎上,一定会有人说怎么又中英文混写了,不过会说这话的人估计也不会点进来这个问题,23333
答:我之前说的不清楚,我丢失了一个“最后一个 entry ”,如果我重新说一遍这句话,我会这么说:`其他的包含当前节点没有的 committed 的 log, 那么这些其他节点就要么<最后一个 entry>的 term 比该节点<最后一个 entry>的 term 大,要么与该节点<最后一个 entry>同 term 但<log>比该节点 log 长`。原文:"Raft determines which of two logs is more up-to-date by comparing the index and term of the last entries in the logs. If the logs have last entries with different terms, then the log with the later term is more up-to-date. If the logs end with the same term, then whichever log is longer is more up to date"。你看现在你懂没懂?还没明白的话我再继续解释

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » Raft 算法相关

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情