raft初始的matchIndex和nextIndex是多少

如题。在编码的过程中,我的matchIndex初始值是0,nextIndex是1。newRaft函数里直接new了一条空日志,来保证日志的index是从1开始的。

但是在测试中,propMsg尚未被Step到raft实例中,他的检查点却是matchIndex=1。那么,这两个值的初始值到底应该是多少?

func TestProgressLeader2AB(t *testing.T) {
	r := newTestRaft(1, []uint64{1, 2}, 5, 1, NewMemoryStorage())
	r.becomeCandidate()
	r.becomeLeader()

	// Send proposals to r1. The first 5 entries should be appended to the log.
	propMsg := pb.Message{From: 1, To: 1, MsgType: pb.MessageType_MsgPropose, Entries: []*pb.Entry{{Data: []byte("foo")}}}
	for i := 0; i < 5; i++ {
		if pr := r.Prs[r.id]; pr.Match != uint64(i+1) || pr.Next != pr.Match+1 {
			t.Errorf("unexpected progress %v, expected: match(%v) == %v && next(%v) == match+1(%v)", pr, pr.Match, i+1, pr.Next, pr.Match+1)
		}
		if err := r.Step(propMsg); err != nil {
			t.Fatalf("proposal resulted in error: %v", err)
		}
	}
}
1 个赞

becomeLeader 有一步需要append noop,并通过sendappend 给follower
noop是一个空的entry,具体可以看doc.go 关于MsgPropose和append的描述。
然后通过responce修改match

1 个赞

next index初始化为last log index+1,match index初始化为0,在论文的figure 2有说明

1 个赞

初始的match是0,next是last Index + 1。

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