6.824-golabs-2020
6.824-golabs-2020 copied to clipboard
为什么持久化了commitIndex和lastApplied?
请问,为什么您的代码中持久化了这两个变量呢?
func (rf *Raft) getPersistData() []byte {
w := new(bytes.Buffer)
e := labgob.NewEncoder(w)
e.Encode(rf.term)
e.Encode(rf.voteFor)
e.Encode(rf.commitIndex)
e.Encode(rf.lastSnapshotIndex)
e.Encode(rf.lastSnapshotTerm)
e.Encode(rf.logEntries)
data := w.Bytes()
return data
}
其实我赞成您的这个做法,但在论文中并没有提到需要持久化这两个变量,尤其是lastApplied。 如果没有持久化lastApplied,节点在reboot之后,应该会根据commitIndex更新lastApplied,这样便会重复apply在reboot之前的entry。 难道是因为apply的状态本来就是不稳定的?也就是数据都是在内存中?🤣而不是apply到磁盘里边?
重复 apply 是有问题的, 所以要持久化记录之前的点; apply term 和 记录 lastApplied 应该看成是原子的
重复 apply 是有问题的, 所以要持久化记录之前的点; apply term 和 记录 lastApplied 应该看成是原子的
明白了,谢谢!