-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathleadershiptransfer_test.go
76 lines (58 loc) · 2.08 KB
/
leadershiptransfer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package fuzzy
import (
"math/rand"
"testing"
"time"
"github.com/hashicorp/raft"
)
// 5 node cluster
func TestRaft_FuzzyLeadershipTransfer(t *testing.T) {
cluster := newRaftCluster(t, testLogWriter, "lt", 5, nil)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
s := newApplySource("LeadershipTransfer")
data := cluster.generateNApplies(s, uint(r.Intn(10000)))
futures := cluster.sendNApplies(time.Minute, data)
cluster.leadershipTransfer(time.Minute)
data = cluster.generateNApplies(s, uint(r.Intn(10000)))
futures = append(futures, cluster.sendNApplies(time.Minute, data)...)
cluster.leadershipTransfer(time.Minute)
data = cluster.generateNApplies(s, uint(r.Intn(10000)))
futures = append(futures, cluster.sendNApplies(time.Minute, data)...)
cluster.leadershipTransfer(time.Minute)
data = cluster.generateNApplies(s, uint(r.Intn(10000)))
futures = append(futures, cluster.sendNApplies(time.Minute, data)...)
ac := cluster.checkApplyFutures(futures)
cluster.Stop(t, time.Minute)
cluster.VerifyLog(t, ac)
cluster.VerifyFSM(t)
}
type LeadershipTransferMode int
type LeadershipTransfer struct {
verifier appendEntriesVerifier
slowNodes map[string]bool
delayMin time.Duration
delayMax time.Duration
mode LeadershipTransferMode
}
func (lt *LeadershipTransfer) Report(t *testing.T) {
lt.verifier.Report(t)
}
func (lt *LeadershipTransfer) PreRPC(s, t string, r *raft.RPC) error {
return nil
}
func (lt *LeadershipTransfer) nap() {
d := lt.delayMin + time.Duration(rand.Int63n((lt.delayMax - lt.delayMin).Nanoseconds()))
time.Sleep(d)
}
func (lt *LeadershipTransfer) PostRPC(src, target string, r *raft.RPC, res *raft.RPCResponse) error {
return nil
}
func (lt *LeadershipTransfer) PreRequestVote(src, target string, v *raft.RequestVoteRequest) (*raft.RequestVoteResponse, error) {
return nil, nil
}
func (lt *LeadershipTransfer) PreAppendEntries(src, target string, v *raft.AppendEntriesRequest) (*raft.AppendEntriesResponse, error) {
lt.verifier.PreAppendEntries(src, target, v)
return nil, nil
}