2018-10-16

1. 背景

为理解raft, 参照2018年的 https://pdos.csail.mit.edu/6.824/labs/lab-raft.html 进行其中的lab 2A 和 2B.

2. 代码实现/协议细节

2.1 文章摘录

https://thesquareplanet.com/blog/students-guide-to-raft/

  • you might reasonably reset a peer’s election timer whenever you receive an AppendEntries or RequestVote RPC, as both indicate that some other peer either thinks it’s the leader, or is trying to become the leader. Intuitively, this means that we shouldn’t be interfering. However, if you read Figure 2 carefully, it says:

    If election timeout elapses without receiving AppendEntries RPC from current leader or granting vote to candidate: convert to candidate. ​

  • many would simply reset their election timer when they received a heartbeat, and then return success, without performing any of the checks specified in Figure 2. This is extremely dangerous. By accepting the RPC, the follower is implicitly telling the leader that their log matches the leader’s log up to and including the prevLogIndex included in the AppendEntries arguments. Upon receiving the reply, the leader might then decide (incorrectly) that some entry has been replicated to a majority of servers, and start committing it. 接收到心跳, 不能简单地返回成功, 而应该 对比leader发过来的prevLogIndex 和 自己(follower)已有的log. 这样, 可以应对如下情况: 当某个节点网络断开, 重新加入集群时, 就能够及时地更新自身数据到最新状态.

https://thesquareplanet.com/blog/raft-qa/

  • applied 和 committed 的关系:

    Any log entry that you have applied to the application state machine is “applied”. An entry should never be applied unless it has already been committed. An entry can be committed, but not yet applied. You will likely apply committed entries very soon after they become committed. 先commit, 后apply.

当 Leader 发现 entry 已经被大多数节点 Append到日志,就认为这个 entry 已经是 Committed 的了, 之后就是操作状态机了.

2.2 apply的实现

因为follower要能够从参数中看出leader的apply变动, 故
方案1:

  1. leader 往[commitIndex] 写入logA
  2. args.LeaderCommit=commitIndex
  3. 发送网络包, follower添加log
  4. leader根据收到的包, 若过半数完成, 执行apply, commitIndex++
  5. follower 接收的心跳中LeaderCommit 变动了, 执行apply动作

方案2:
AppendEntriesArgs 中添加新的字段 LeaderLastApply 来实现

2.3 代码

https://github.com/awakening-fong/mit_6.824

3 其他无关紧要的

3.1 会发生split vote吗?

发生split vote的一个场景是 以5个节点(A~E)的情况为例,

   A       B

    O      O
   / \     |
  /   \    |
 O     O   O

 C     D   E

A获得了2票(来自C和D), B得到了1票(来自E), 都没超过半数(3票), A和B都无法当选.

4. 参考资料

https://pdos.csail.mit.edu/6.824/papers/raft-extended.pdf
https://thesquareplanet.com/blog/raft-qa/
https://thesquareplanet.com/blog/students-guide-to-raft/

本文地址: https://awakening-fong.github.io/posts/distributed_system/raft_mit_6-824

转载请注明出处: https://awakening-fong.github.io


若无法评论, 请打开JavaScript, 并通过proxy.


blog comments powered by Disqus