From 34ea5c09f68f46b88cb79b73645b2dceb9820343 Mon Sep 17 00:00:00 2001 From: Ibrahim Kettaneh Date: Wed, 9 Oct 2024 10:28:31 -0400 Subject: [PATCH] raft: test TestCannotCommitWithoutNewTermEntry w/ & wo/ store liveness This commit changes TestCannotCommitWithoutNewTermEntry to allow testing it when store liveness is both enabled and disabled. --- pkg/raft/raft_test.go | 72 +++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/pkg/raft/raft_test.go b/pkg/raft/raft_test.go index 0e9691c4effb..26e635d7a35c 100644 --- a/pkg/raft/raft_test.go +++ b/pkg/raft/raft_test.go @@ -676,39 +676,59 @@ func TestSingleNodeCommit(t *testing.T) { // when leader changes, no new proposal comes in and ChangeTerm proposal is // filtered. func TestCannotCommitWithoutNewTermEntry(t *testing.T) { - tt := newNetworkWithConfig(fortificationDisabledConfig, nil, nil, nil, nil, nil) - tt.send(pb.Message{From: 1, To: 1, Type: pb.MsgHup}) + var tt *network + testutils.RunTrueAndFalse(t, "store-liveness-enabled", + func(t *testing.T, storeLivenessEnabled bool) { - // 0 cannot reach 2,3,4 - tt.cut(1, 3) - tt.cut(1, 4) - tt.cut(1, 5) + if storeLivenessEnabled { + tt = newNetwork(nil, nil, nil, nil, nil) + } else { + tt = newNetworkWithConfig(fortificationDisabledConfig, nil, nil, nil, nil, nil) + } - tt.send(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("some data")}}}) - tt.send(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("some data")}}}) + tt.send(pb.Message{From: 1, To: 1, Type: pb.MsgHup}) - sm := tt.peers[1].(*raft) - assert.Equal(t, uint64(1), sm.raftLog.committed) + // 0 cannot reach 2,3,4 + tt.cut(1, 3) + tt.cut(1, 4) + tt.cut(1, 5) - // network recovery - tt.recover() - // avoid committing ChangeTerm proposal - tt.ignore(pb.MsgApp) + tt.send(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("some data")}}}) + tt.send(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("some data")}}}) - // elect 2 as the new leader with term 2 - tt.send(pb.Message{From: 2, To: 2, Type: pb.MsgHup}) + sm := tt.peers[1].(*raft) + assert.Equal(t, uint64(1), sm.raftLog.committed) - // no log entries from previous term should be committed - sm = tt.peers[2].(*raft) - assert.Equal(t, uint64(1), sm.raftLog.committed) + // network recovery + tt.recover() + // avoid committing ChangeTerm proposal + tt.ignore(pb.MsgApp) - tt.recover() - // send heartbeat; reset wait - tt.send(pb.Message{From: 2, To: 2, Type: pb.MsgBeat}) - // append an entry at current term - tt.send(pb.Message{From: 2, To: 2, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("some data")}}}) - // expect the committed to be advanced - assert.Equal(t, uint64(5), sm.raftLog.committed) + // elect 2 as the new leader with term 2 + if storeLivenessEnabled { + // We need peers to withdraw their support for the current leader so + // that a new leader can be elected. + tt.withdrawSupportAllPeers() + } + tt.send(pb.Message{From: 2, To: 2, Type: pb.MsgHup}) + + if storeLivenessEnabled { + // Now that a new leader is elected, we can grant support to all peers. + tt.grantSupportAllPeers() + } + + // no log entries from previous term should be committed + sm = tt.peers[2].(*raft) + assert.Equal(t, uint64(1), sm.raftLog.committed) + + tt.recover() + // send heartbeat; reset wait + tt.send(pb.Message{From: 2, To: 2, Type: pb.MsgBeat}) + // append an entry at current term + tt.send(pb.Message{From: 2, To: 2, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("some data")}}}) + // expect the committed to be advanced + assert.Equal(t, uint64(5), sm.raftLog.committed) + }) } // TestCommitWithoutNewTermEntry tests the entries could be committed