首页 > 其他分享 >Lab2C

Lab2C

时间:2023-02-09 22:25:20浏览次数:40  
标签:term log should rf Lab2C data its

2C的测试写好如下两个函数,然后在currentterm votedfor log这三个变量改变的时候调用persist就好,运行测试案例,有两个有两个案例fail,重点看下这两个测试案例.

func (rf *Raft) persist() {
	// Your code here (2C).
	// Example:
	w := new(bytes.Buffer)
	e := labgob.NewEncoder(w)
	e.Encode(rf.currentTerm)
	e.Encode(rf.votedFor)
	e.Encode(rf.log)
	data := w.Bytes()
	rf.persister.SaveRaftState(data)
}

func (rf *Raft) readPersist(data []byte) {
	if data == nil || len(data) < 1 { // bootstrap without any state?
		return
	}
	// Your code here (2C).
	// Example:
	r := bytes.NewBuffer(data)
	d := labgob.NewDecoder(r)
	d.Decode(&rf.currentTerm)
	d.Decode(&rf.votedFor)
	d.Decode(&rf.log)
}

img

TestFigure8Unreliable2C

TestReliableChurn2C

这两个案例卡了巨久,参考别人很多博客,有改心跳时间的,处理过期RPC都没用.
最后看student guide是日志同步慢造成的,AppendReply增加ConflictIndex和ConflictTerm参数后解决,逻辑如下

  • If a follower does not have prevLogIndex in its log, it should return with conflictIndex = len(log) and conflictTerm = None.

  • If a follower does have prevLogIndex in its log, but the term does not match, it should return conflictTerm = log[prevLogIndex].Term, and then search its log for the first index whose entry has term equal to conflictTerm.

  • Upon receiving a conflict response, the leader should first search its log for conflictTerm. If it finds an entry in its log with that term, it should set nextIndex to be the one beyond the index of the last entry in that term in its log.

  • If it does not find an entry with that term, it should set nextIndex = conflictIndex.

img

标签:term,log,should,rf,Lab2C,data,its
From: https://www.cnblogs.com/autumnnnn/p/17107345.html

相关文章