Skip to content

Commit

Permalink
Add monotonic user restore test
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalmi committed Mar 10, 2023
1 parent a15a50e commit 795e781
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
46 changes: 41 additions & 5 deletions raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,11 +1024,16 @@ func TestRaft_SnapshotRestore_Monotonic(t *testing.T) {
// Make the cluster
conf := inmemConfig(t)
conf.TrailingLogs = 10
c := MakeCluster(1, t, conf)
opts := &MakeClusterOpts{
Peers: 1,
Bootstrap: true,
Conf: conf,
MonotonicLogs: true,
}
c := MakeClusterCustom(t, opts)
defer c.Close()

leader := c.Leader()
leader.logs = &MockMonotonicLogStore{s: leader.logs}

// Commit a lot of things
var future Future
Expand Down Expand Up @@ -1411,7 +1416,7 @@ func TestRaft_UserSnapshot(t *testing.T) {

// snapshotAndRestore does a snapshot and restore sequence and applies the given
// offset to the snapshot index, so we can try out different situations.
func snapshotAndRestore(t *testing.T, offset uint64) {
func snapshotAndRestore(t *testing.T, offset uint64, monotonicLogStore bool) {
// Make the cluster.
conf := inmemConfig(t)

Expand All @@ -1421,7 +1426,18 @@ func snapshotAndRestore(t *testing.T, offset uint64) {
conf.ElectionTimeout = 500 * time.Millisecond
conf.LeaderLeaseTimeout = 500 * time.Millisecond

c := MakeCluster(3, t, conf)
var c *cluster
if monotonicLogStore {
opts := &MakeClusterOpts{
Peers: 3,
Bootstrap: true,
Conf: conf,
MonotonicLogs: true,
}
c = MakeClusterCustom(t, opts)
} else {
c = MakeCluster(3, t, conf)
}
defer c.Close()

// Wait for things to get stable and commit some things.
Expand Down Expand Up @@ -1517,7 +1533,26 @@ func TestRaft_UserRestore(t *testing.T) {

for _, c := range cases {
t.Run(fmt.Sprintf("case %v", c), func(t *testing.T) {
snapshotAndRestore(t, c)
snapshotAndRestore(t, c, false)
})
}
}

func TestRaft_UserRestore_Monotonic(t *testing.T) {
cases := []uint64{
0,
1,
2,

// Snapshots from the future
100,
1000,
10000,
}

for _, c := range cases {
t.Run(fmt.Sprintf("case %v", c), func(t *testing.T) {
snapshotAndRestore(t, c, true)
})
}
}
Expand Down Expand Up @@ -2449,6 +2484,7 @@ func TestRaft_LeadershipTransferStopRightAway(t *testing.T) {
t.Errorf("leadership shouldn't have started, but instead it error with: %v", err)
}
}

func TestRaft_GetConfigurationNoBootstrap(t *testing.T) {
c := MakeCluster(2, t, nil)
defer c.Close()
Expand Down
8 changes: 7 additions & 1 deletion testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@ type MakeClusterOpts struct {
ConfigStoreFSM bool
MakeFSMFunc func() FSM
LongstopTimeout time.Duration
MonotonicLogs bool
}

// makeCluster will return a cluster with the given config and number of peers.
Expand Down Expand Up @@ -789,11 +790,16 @@ func makeCluster(t *testing.T, opts *MakeClusterOpts) *cluster {
// Create all the rafts
c.startTime = time.Now()
for i := 0; i < opts.Peers; i++ {
logs := c.stores[i]
var logs LogStore
logs = c.stores[i]
store := c.stores[i]
snap := c.snaps[i]
trans := c.trans[i]

if opts.MonotonicLogs {
logs = &MockMonotonicLogStore{s: logs}
}

peerConf := opts.Conf
peerConf.LocalID = configuration.Servers[i].ID
peerConf.Logger = newTestLoggerWithPrefix(t, string(configuration.Servers[i].ID))
Expand Down

0 comments on commit 795e781

Please sign in to comment.