Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为什么持久化了commitIndex和lastApplied? #9

Open
Vector6662 opened this issue May 28, 2022 · 2 comments
Open

为什么持久化了commitIndex和lastApplied? #9

Vector6662 opened this issue May 28, 2022 · 2 comments

Comments

@Vector6662
Copy link

请问,为什么您的代码中持久化了这两个变量呢?

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到磁盘里边?

@yzongyue
Copy link
Owner

重复 apply 是有问题的, 所以要持久化记录之前的点; apply term 和 记录 lastApplied 应该看成是原子的

@Vector6662
Copy link
Author

重复 apply 是有问题的, 所以要持久化记录之前的点; apply term 和 记录 lastApplied 应该看成是原子的

明白了,谢谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants