[tinykv 2a] TestOldMessages

在tinykv 2a 接收appendmessage时,发现测试用例TestOldMessages2AB里的newMemoryStorageWithEnts 在比较的时候没有数据,我自己向raftlog的 entries 里面追加日志导致两个结果不一致,这个意思是我的数据不能够直接向entries 里append吗?

ilog := newLog(
		newMemoryStorageWithEnts([]pb.Entry{
			{}, {Data: nil, Term: 1, Index: 1},
			{Data: nil, Term: 2, Index: 2}, {Data: nil, Term: 3, Index: 3},
			{Data: []byte("somedata"), Term: 3, Index: 4},
		}))
	ilog.committed = 4
	base := ltoa(ilog)
	for i, p := range tt.peers {
		if sm, ok := p.(*Raft); ok {
			l := ltoa(sm.RaftLog)
			if g := diffu(base, l); g != "" {
				t.Errorf("#%d: diff:\
%s", i, g)
			}
		} else {
			t.Logf("#%d: empty log", i)
		}
	}

这个是比较的结果:

raft_test.go:516: #1: diff:
        --- /tmp/base208016859	2022-01-09 20:43:37.193077688 +0800
        +++ /tmp/other3085804305	2022-01-09 20:43:37.194077712 +0800
        @@ -1,2 +1,6 @@
         committed: 4
         applied:  0
        +#0: {EntryType:EntryNormal Term:1 Index:1 Data:[] XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0}
        +#1: {EntryType:EntryNormal Term:2 Index:2 Data:[] XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0}
        +#2: {EntryType:EntryNormal Term:3 Index:3 Data:[] XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0}
        +#3: {EntryType:EntryNormal Term:3 Index:4 Data:[115 111 109 101 100 97 116 97] XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0}
    raft_test.go:516: #2: diff:
        --- /tmp/base1566267759	2022-01-09 20:43:37.196077759 +0800
        +++ /tmp/other2235237263	2022-01-09 20:43:37.217078260 +0800
        @@ -1,2 +1,6 @@
         committed: 4
         applied:  0
        +#0: {EntryType:EntryNormal Term:1 Index:1 Data:[] XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0}
        +#1: {EntryType:EntryNormal Term:2 Index:2 Data:[] XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0}
        +#2: {EntryType:EntryNormal Term:3 Index:3 Data:[] XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0}
        +#3: {EntryType:EntryNormal Term:3 Index:4 Data:[115 111 109 101 100 97 116 97] XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0}
    raft_test.go:516: #3: diff:
        --- /tmp/base765527407	2022-01-09 20:43:37.220078332 +0800
        +++ /tmp/other2759677515	2022-01-09 20:43:37.226078475 +0800
        @@ -1,2 +1,6 @@
         committed: 4
         applied:  0
        +#0: {EntryType:EntryNormal Term:1 Index:1 Data:[] XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0}
        +#1: {EntryType:EntryNormal Term:2 Index:2 Data:[] XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0}
        +#2: {EntryType:EntryNormal Term:3 Index:3 Data:[] XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0}
        +#3: {EntryType:EntryNormal Term:3 Index:4 Data:[115 111 109 101 100 97 116 97] XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0}
1 个赞

不能啊,leader 来的消息不能直接 append 到内存里的,需要做冲突检查,如果冲突了是需要删除日志的。

1 个赞

看这个test的输出应该是你的RaftLog里面没有entry,这个test运行之后RaftLog里面应该有4条entry的,每个字段就是你现在test输出的那样。
leader添加日志之后其他节点当然也要向raftLog添加日志的,不知道你追加日志怎么导致结果不一致,每个entries就Term、Index、Data三个字段,你逐个检查一下哪个字段跟test的预期不符,或者有可能是你新leader当选之后没有添加一条data为空的日志?奥 对,还有楼上说的,检查是不是某条日志跟leader冲突了。
建议goland直接debug,2a的测试都可以打断点调试的。

1 个赞