关于raft论文中AppendEntries部分描述的疑惑

论文中提到了

  1. If leaderCommit > commitIndex, set commitIndex =
    min(leaderCommit, index of last new entry)

这个index of last new entry 各位怎么理解?最新?上一个?好像并不是raftlog在entries中的最后一个index,
如果是noop 或者 空message.entries 的时候又该怎么拿到

1 个赞

这一条是对于 follower 而言的。
即leader 通知follower, 我已经commit到 leaderCommit 这个index了, 你也可以commit到这里。
但如果follower的entries不够新,那follower就只能commit到,他自己的最后一个index。
最后,这个commit是不考虑内容的,raft层面的entries不用考虑意义,noop和普通entry一样处理就可以,要到后面的tinykv-server环节再考虑内容的意义。

index of last new entry 是指的 Append Entries RPC 中最后一条 Message 的 Index。至于最后一条消息是什么内容,无需考虑,那是应用层的事。

不可能是m.index吧

更新commitindex的时候更新成rpc里面最后一条entry的index,如果entries[]没有内容,那我们知道,数据是从prelogindex起始,长度为0,所以拿prelogindex代替entries最后一条的index

每个节点都会维护一个 commitIndex,表示在这个位置之前所有的日志项都已经提交,不会再次被更改。

Leader中的commitIndex是全局最新的,因为Leader的commitIndex是通过判断是否将一个日志项成功复制到了多数节点来判断的。当某个日志项成功复制到了多数节点,Leader会更新自己的commitIndex,并把这个变量放入到给Follower的消息中。

当 Follower 收到了来自Leader的消息,如果有日志项,就先处理日志项,清除与Leader不一致的日志项,写入新的日志项。

LederCommit 说明这个位置之前的日志项已经确定不会改变了。

如果 【日志项最后一个位置】 < LeaderCommit ,就说明自己还没有最新的日志,而自己能够确定的位置就是当前entries的最后一个位置,把commitIndex更新到这个位置就可以了。

如果【日志项最后一个位置】 >= LeaderCommit,就说明自己能够确定的位置和Leader是一样的,那么就更新到LeaderCommit这个位置就可以了。

1 个赞

十分感谢

你好,6.824助教写的学生指南里面,有这一段话:
The min in the final step (#5) of AppendEntries is necessary, and it needs to be computed with the index of the last new entry. It is not sufficient to simply have the function that applies things from your log between lastApplied and commitIndex stop when it reaches the end of your log. This is because you may have entries in your log that differ from the leader’s log after the entries that the leader sent you (which all match the ones in your log). Because #3 dictates that you only truncate your log if you have conflicting entries, those won’t be removed, and if leaderCommit is beyond the entries the leader sent you, you may apply incorrect entries.
这种情况会不会出现呢。

这种是会出现的吧,每次commit的时候都需要判断一下,如果commit index > lastIndex依然更新committed的话那应该是会导致错误的

此话题已在最后回复的 1 分钟后被自动关闭。不再允许新回复。