forked from hashicorp/raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integ_test.go
266 lines (230 loc) · 5.76 KB
/
integ_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package raft
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"testing"
"time"
)
// CheckInteg will skip a test if integration testing is not enabled.
func CheckInteg(t *testing.T) {
if !IsInteg() {
t.SkipNow()
}
}
// IsInteg returns a boolean telling you if we're in integ testing mode.
func IsInteg() bool {
return os.Getenv("INTEG_TESTS") != ""
}
type RaftEnv struct {
dir string
conf *Config
fsm *MockFSM
store *InmemStore
snapshot *FileSnapshotStore
peers *JSONPeers
trans *NetworkTransport
raft *Raft
}
func (r *RaftEnv) Release() {
log.Printf("[WARN] Release node at %v", r.raft.localAddr)
f := r.raft.Shutdown()
if err := f.Error(); err != nil {
panic(err)
}
r.trans.Close()
os.RemoveAll(r.dir)
}
func MakeRaft(t *testing.T, conf *Config) *RaftEnv {
env := &RaftEnv{}
// Set the config
if conf == nil {
conf = inmemConfig(t)
}
env.conf = conf
dir, err := ioutil.TempDir("", "raft")
if err != nil {
t.Fatalf("err: %v ", err)
}
env.dir = dir
stable := NewInmemStore()
env.store = stable
snap, err := NewFileSnapshotStore(dir, 3, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
env.snapshot = snap
env.fsm = &MockFSM{}
trans, err := NewTCPTransport("127.0.0.1:0", nil, 2, time.Second, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
env.trans = trans
env.peers = NewJSONPeers(dir, trans)
log.Printf("[INFO] Starting node at %v", trans.LocalAddr())
raft, err := NewRaft(conf, env.fsm, stable, stable, snap, env.peers, trans)
if err != nil {
t.Fatalf("err: %v", err)
}
env.raft = raft
return env
}
func WaitFor(env *RaftEnv, state RaftState) error {
limit := time.Now().Add(200 * time.Millisecond)
for env.raft.State() != state {
if time.Now().Before(limit) {
time.Sleep(10 * time.Millisecond)
} else {
return fmt.Errorf("failed to transition to state %v", state)
}
}
return nil
}
func WaitForAny(state RaftState, envs []*RaftEnv) (*RaftEnv, error) {
limit := time.Now().Add(200 * time.Millisecond)
CHECK:
for _, env := range envs {
if env.raft.State() == state {
return env, nil
}
}
if time.Now().Before(limit) {
goto WAIT
}
return nil, fmt.Errorf("failed to find node in %v state", state)
WAIT:
time.Sleep(10 * time.Millisecond)
goto CHECK
}
func WaitFuture(f Future, t *testing.T) error {
timer := time.AfterFunc(200*time.Millisecond, func() {
panic(fmt.Errorf("timeout waiting for future %v", f))
})
defer timer.Stop()
return f.Error()
}
func NoErr(err error, t *testing.T) {
if err != nil {
t.Fatalf("err: %v", err)
}
}
func CheckConsistent(envs []*RaftEnv, t *testing.T) {
limit := time.Now().Add(400 * time.Millisecond)
first := envs[0]
var err error
CHECK:
l1 := len(first.fsm.logs)
for i := 1; i < len(envs); i++ {
env := envs[i]
l2 := len(env.fsm.logs)
if l1 != l2 {
err = fmt.Errorf("log length mismatch %d %d", l1, l2)
goto ERR
}
for idx, log := range first.fsm.logs {
other := env.fsm.logs[idx]
if bytes.Compare(log, other) != 0 {
err = fmt.Errorf("log %d mismatch %v %v", idx, log, other)
goto ERR
}
}
}
return
ERR:
if time.Now().After(limit) {
t.Fatalf("%v", err)
}
time.Sleep(20 * time.Millisecond)
goto CHECK
}
// Tests Raft by creating a cluster, growing it to 5 nodes while
// causing various stressful conditions
func TestRaft_Integ(t *testing.T) {
CheckInteg(t)
conf := DefaultConfig()
conf.HeartbeatTimeout = 50 * time.Millisecond
conf.ElectionTimeout = 50 * time.Millisecond
conf.LeaderLeaseTimeout = 50 * time.Millisecond
conf.CommitTimeout = 5 * time.Millisecond
conf.SnapshotThreshold = 100
conf.TrailingLogs = 10
conf.EnableSingleNode = true
// Create a single node
env1 := MakeRaft(t, conf)
NoErr(WaitFor(env1, Leader), t)
// Do some commits
var futures []Future
for i := 0; i < 100; i++ {
futures = append(futures, env1.raft.Apply([]byte(fmt.Sprintf("test%d", i)), 0))
}
for _, f := range futures {
NoErr(WaitFuture(f, t), t)
log.Printf("[DEBUG] Applied %v", f)
}
// Do a snapshot
NoErr(WaitFuture(env1.raft.Snapshot(), t), t)
// Join a few nodes!
var envs []*RaftEnv
for i := 0; i < 4; i++ {
env := MakeRaft(t, conf)
addr := env.trans.LocalAddr()
NoErr(WaitFuture(env1.raft.AddPeer(addr), t), t)
envs = append(envs, env)
}
// Wait for a leader
leader, err := WaitForAny(Leader, append([]*RaftEnv{env1}, envs...))
NoErr(err, t)
// Do some more commits
futures = nil
for i := 0; i < 100; i++ {
futures = append(futures, leader.raft.Apply([]byte(fmt.Sprintf("test%d", i)), 0))
}
for _, f := range futures {
NoErr(WaitFuture(f, t), t)
log.Printf("[DEBUG] Applied %v", f)
}
// Shoot two nodes in the head!
rm1, rm2 := envs[0], envs[1]
rm1.Release()
rm2.Release()
envs = envs[2:]
time.Sleep(10 * time.Millisecond)
// Wait for a leader
leader, err = WaitForAny(Leader, append([]*RaftEnv{env1}, envs...))
NoErr(err, t)
// Do some more commits
futures = nil
for i := 0; i < 100; i++ {
futures = append(futures, leader.raft.Apply([]byte(fmt.Sprintf("test%d", i)), 0))
}
for _, f := range futures {
NoErr(WaitFuture(f, t), t)
log.Printf("[DEBUG] Applied %v", f)
}
// Join a few new nodes!
for i := 0; i < 2; i++ {
env := MakeRaft(t, conf)
addr := env.trans.LocalAddr()
NoErr(WaitFuture(leader.raft.AddPeer(addr), t), t)
envs = append(envs, env)
}
// Remove the old nodes
NoErr(WaitFuture(leader.raft.RemovePeer(rm1.raft.localAddr), t), t)
NoErr(WaitFuture(leader.raft.RemovePeer(rm2.raft.localAddr), t), t)
// Shoot the leader
env1.Release()
time.Sleep(3 * conf.HeartbeatTimeout)
// Wait for a leader
leader, err = WaitForAny(Leader, envs)
NoErr(err, t)
allEnvs := append([]*RaftEnv{env1}, envs...)
CheckConsistent(allEnvs, t)
if len(env1.fsm.logs) != 300 {
t.Fatalf("should apply 300 logs! %d", len(env1.fsm.logs))
}
for _, e := range envs {
e.Release()
}
}