Skip to content

Commit

Permalink
raft: test TestCannotCommitWithoutNewTermEntry w/ & wo/ store liveness
Browse files Browse the repository at this point in the history
This commit changes TestCannotCommitWithoutNewTermEntry to allow testing
it when store liveness is both enabled and disabled.
  • Loading branch information
iskettaneh committed Oct 9, 2024
1 parent 8f2a996 commit 34ea5c0
Showing 1 changed file with 46 additions and 26 deletions.
72 changes: 46 additions & 26 deletions pkg/raft/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 34ea5c0

Please sign in to comment.