-
Notifications
You must be signed in to change notification settings - Fork 1k
/
integ_test.go
588 lines (514 loc) · 16.4 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package raft
import (
"bytes"
"context"
"fmt"
"os"
"sync/atomic"
"testing"
"time"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/require"
)
// 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
trans *NetworkTransport
raft *Raft
logger hclog.Logger
}
// Release shuts down and cleans up any stored data, its not restartable after this
func (r *RaftEnv) Release() {
r.Shutdown()
os.RemoveAll(r.dir)
}
// Shutdown shuts down raft & transport, but keeps track of its data, its restartable
// after a Shutdown() by calling Start()
func (r *RaftEnv) Shutdown() {
r.logger.Warn(fmt.Sprintf("Shutdown node at %v", r.raft.localAddr))
f := r.raft.Shutdown()
if err := f.Error(); err != nil {
panic(err)
}
r.trans.Close()
}
// Restart will start a raft node that was previously Shutdown()
func (r *RaftEnv) Restart(t *testing.T) {
trans, err := NewTCPTransport(string(r.raft.localAddr), nil, 2, time.Second, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
r.trans = trans
r.logger.Info("starting node", "addr", trans.LocalAddr())
raft, err := NewRaft(r.conf, r.fsm, r.store, r.store, r.snapshot, r.trans)
if err != nil {
t.Fatalf("err: %v", err)
}
r.raft = raft
}
func MakeRaft(tb testing.TB, conf *Config, bootstrap bool) *RaftEnv {
// Set the config
if conf == nil {
conf = inmemConfig(tb)
}
dir, err := os.MkdirTemp("", "raft")
if err != nil {
tb.Fatalf("err: %v ", err)
}
stable := NewInmemStore()
snap, err := NewFileSnapshotStore(dir, 3, nil)
if err != nil {
tb.Fatalf("err: %v", err)
}
env := &RaftEnv{
conf: conf,
dir: dir,
store: stable,
snapshot: snap,
fsm: &MockFSM{},
}
trans, err := NewTCPTransport("localhost:0", nil, 2, time.Second, nil)
if err != nil {
tb.Fatalf("err: %v", err)
}
env.logger = hclog.New(&hclog.LoggerOptions{
Name: string(trans.LocalAddr()) + " :",
})
env.trans = trans
if bootstrap {
var configuration Configuration
configuration.Servers = append(configuration.Servers, Server{
Suffrage: Voter,
ID: conf.LocalID,
Address: trans.LocalAddr(),
})
err = BootstrapCluster(conf, stable, stable, snap, trans, configuration)
if err != nil {
tb.Fatalf("err: %v", err)
}
}
env.logger.Info("starting node", "addr", trans.LocalAddr())
conf.Logger = env.logger
raft, err := NewRaft(conf, env.fsm, stable, stable, snap, trans)
if err != nil {
tb.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) error {
timer := time.AfterFunc(1000*time.Millisecond, func() {
panic(fmt.Errorf("timeout waiting for future %v", f))
})
defer timer.Stop()
return f.Error()
}
func NoErr(err error, tb testing.TB) {
tb.Helper()
if err != nil {
tb.Fatalf("err: %v", err)
}
}
func CheckConsistent(envs []*RaftEnv, t *testing.T) {
limit := time.Now().Add(400 * time.Millisecond)
first := envs[0]
first.fsm.Lock()
defer first.fsm.Unlock()
var err error
CHECK:
l1 := len(first.fsm.logs)
for i := 1; i < len(envs); i++ {
env := envs[i]
env.fsm.Lock()
l2 := len(env.fsm.logs)
if l1 != l2 {
err = fmt.Errorf("log length mismatch %d %d", l1, l2)
env.fsm.Unlock()
goto ERR
}
for idx, log := range first.fsm.logs {
other := env.fsm.logs[idx]
if bytes.Compare(log, other) != 0 {
err = fmt.Errorf("log entry %d mismatch between %s/%s : '%s' / '%s'", idx, first.raft.localAddr, env.raft.localAddr, log, other)
env.fsm.Unlock()
goto ERR
}
}
env.fsm.Unlock()
}
return
ERR:
if time.Now().After(limit) {
t.Fatalf("%v", err)
}
first.fsm.Unlock()
time.Sleep(20 * time.Millisecond)
first.fsm.Lock()
goto CHECK
}
// return a log entry that's at least sz long that has the prefix 'test i '
func logBytes(i, sz int) []byte {
var logBuffer bytes.Buffer
fmt.Fprintf(&logBuffer, "test %d ", i)
for logBuffer.Len() < sz {
logBuffer.WriteByte('x')
}
return logBuffer.Bytes()
}
// 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.LocalID = ServerID("first")
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
// Create a single node
env1 := MakeRaft(t, conf, true)
NoErr(WaitFor(env1, Leader), t)
totalApplied := 0
applyAndWait := func(leader *RaftEnv, n, sz int) {
// Do some commits
var futures []ApplyFuture
for i := 0; i < n; i++ {
futures = append(futures, leader.raft.Apply(logBytes(i, sz), 0))
}
for _, f := range futures {
NoErr(WaitFuture(f), t)
leader.logger.Debug("applied", "index", f.Index(), "size", sz)
}
totalApplied += n
}
// Do some commits
applyAndWait(env1, 100, 10)
// Do a snapshot
NoErr(WaitFuture(env1.raft.Snapshot()), t)
// Join a few nodes!
var envs []*RaftEnv
for i := 0; i < 4; i++ {
conf.LocalID = ServerID(fmt.Sprintf("next-batch-%d", i))
env := MakeRaft(t, conf, false)
addr := env.trans.LocalAddr()
NoErr(WaitFuture(env1.raft.AddVoter(conf.LocalID, addr, 0, 0)), t)
envs = append(envs, env)
}
// Wait for a leader
leader, err := WaitForAny(Leader, append([]*RaftEnv{env1}, envs...))
NoErr(err, t)
// Do some more commits
applyAndWait(leader, 100, 10)
// Snapshot the leader
NoErr(WaitFuture(leader.raft.Snapshot()), t)
CheckConsistent(append([]*RaftEnv{env1}, envs...), t)
// shutdown a follower
disconnected := envs[len(envs)-1]
disconnected.Shutdown()
// Do some more commits [make sure the resulting snapshot will be a reasonable size]
applyAndWait(leader, 100, 10000)
// snapshot the leader [leaders log should be compacted past the disconnected follower log now]
NoErr(WaitFuture(leader.raft.Snapshot()), t)
// Unfortunately we need to wait for the leader to start backing off RPCs to the down follower
// such that when the follower comes back up it'll run an election before it gets an rpc from
// the leader
time.Sleep(time.Second * 5)
// start the now out of date follower back up
disconnected.Restart(t)
// wait for it to get caught up
timeout := time.Now().Add(time.Second * 10)
for disconnected.raft.getLastApplied() < leader.raft.getLastApplied() {
time.Sleep(time.Millisecond)
if time.Now().After(timeout) {
t.Fatalf("Gave up waiting for follower to get caught up to leader")
}
}
CheckConsistent(append([]*RaftEnv{env1}, envs...), t)
// 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
applyAndWait(leader, 100, 10)
// Join a few new nodes!
for i := 0; i < 2; i++ {
conf.LocalID = ServerID(fmt.Sprintf("final-batch-%d", i))
env := MakeRaft(t, conf, false)
addr := env.trans.LocalAddr()
NoErr(WaitFuture(leader.raft.AddVoter(conf.LocalID, addr, 0, 0)), t)
envs = append(envs, env)
leader, err = WaitForAny(Leader, append([]*RaftEnv{env1}, envs...))
NoErr(err, t)
}
// Wait for a leader
leader, err = WaitForAny(Leader, append([]*RaftEnv{env1}, envs...))
NoErr(err, t)
// Remove the old nodes
NoErr(WaitFuture(leader.raft.RemoveServer(rm1.raft.localID, 0, 0)), t)
NoErr(WaitFuture(leader.raft.RemoveServer(rm2.raft.localID, 0, 0)), 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) != totalApplied {
t.Fatalf("should apply %d logs! %d", totalApplied, len(env1.fsm.logs))
}
for _, e := range envs {
e.Release()
}
}
func TestRaft_RestartFollower_LongInitialHeartbeat(t *testing.T) {
CheckInteg(t)
tests := []struct {
name string
restartInitialTimeouts time.Duration
expectNewLeader bool
}{
{"Default", 0, true},
{"InitialHigher", time.Second, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
conf := DefaultConfig()
conf.LocalID = ServerID("first")
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
// Create a single node
env1 := MakeRaft(t, conf, true)
NoErr(WaitFor(env1, Leader), t)
// Join a few nodes!
var envs []*RaftEnv
for i := 0; i < 2; i++ {
conf.LocalID = ServerID(fmt.Sprintf("next-batch-%d", i))
env := MakeRaft(t, conf, false)
addr := env.trans.LocalAddr()
NoErr(WaitFuture(env1.raft.AddVoter(conf.LocalID, addr, 0, 0)), t)
envs = append(envs, env)
}
allEnvs := append([]*RaftEnv{env1}, envs...)
// Wait for a leader
_, err := WaitForAny(Leader, append([]*RaftEnv{env1}, envs...))
NoErr(err, t)
CheckConsistent(append([]*RaftEnv{env1}, envs...), t)
// TODO without this sleep, the restarted follower doesn't have any stored config
// and aborts the election because it doesn't know of any peers. Shouldn't
// CheckConsistent prevent that?
time.Sleep(time.Second)
// shutdown a follower
disconnected := envs[len(envs)-1]
disconnected.logger.Info("stopping follower")
disconnected.Shutdown()
seeNewLeader := func(o *Observation) bool { _, ok := o.Data.(LeaderObservation); return ok }
leaderCh := make(chan Observation)
// TODO Closing this channel results in panics, even though we're calling Release.
// defer close(leaderCh)
leaderChanges := new(uint32)
go func() {
for range leaderCh {
atomic.AddUint32(leaderChanges, 1)
}
}()
requestVoteCh := make(chan Observation)
seeRequestVote := func(o *Observation) bool { _, ok := o.Data.(RequestVoteRequest); return ok }
requestVotes := new(uint32)
go func() {
for range requestVoteCh {
atomic.AddUint32(requestVotes, 1)
}
}()
for _, env := range allEnvs {
env.raft.RegisterObserver(NewObserver(leaderCh, false, seeNewLeader))
}
// Unfortunately we need to wait for the leader to start backing off RPCs to the down follower
// such that when the follower comes back up it'll run an election before it gets an rpc from
// the leader
time.Sleep(time.Second * 5)
if tt.restartInitialTimeouts != 0 {
disconnected.conf.HeartbeatTimeout = tt.restartInitialTimeouts
disconnected.conf.ElectionTimeout = tt.restartInitialTimeouts
}
disconnected.logger.Info("restarting follower")
disconnected.Restart(t)
time.Sleep(time.Second * 2)
if tt.expectNewLeader {
require.NotEqual(t, 0, atomic.LoadUint32(leaderChanges))
} else {
require.Equal(t, uint32(0), atomic.LoadUint32(leaderChanges))
}
if tt.restartInitialTimeouts != 0 {
for _, env := range envs {
env.raft.RegisterObserver(NewObserver(requestVoteCh, false, seeRequestVote))
NoErr(env.raft.ReloadConfig(ReloadableConfig{
TrailingLogs: conf.TrailingLogs,
SnapshotInterval: conf.SnapshotInterval,
SnapshotThreshold: conf.SnapshotThreshold,
HeartbeatTimeout: 250 * time.Millisecond,
ElectionTimeout: 250 * time.Millisecond,
}), t)
}
// Make sure that reload by itself doesn't trigger a vote
time.Sleep(300 * time.Millisecond)
require.Equal(t, uint32(0), atomic.LoadUint32(requestVotes))
// Stop the leader, ensure that we don't see a request vote within the first 50ms
// (original config of the non-restarted follower), but that we do see one within
// the 250ms both followers should now be using for heartbeat timeout. Well, not
// quite: we wait for two heartbeat intervals (plus a fudge factor), because the
// first time around, last contact will have been recent enough that no vote will
// be triggered.
env1.logger.Info("stopping leader")
env1.Shutdown()
time.Sleep(50 * time.Millisecond)
require.Equal(t, uint32(0), atomic.LoadUint32(requestVotes))
time.Sleep(600 * time.Millisecond)
require.NotEqual(t, uint32(0), atomic.LoadUint32(requestVotes))
}
for _, e := range allEnvs {
e.Release()
}
})
}
}
// TestRaft_PreVote_LeaderSpam test that when a leader spam the followers
// with pre-vote requests they can still transition to candidate.
// The reason this test need to live in here is that we need the transport heartbeat fast-path
// to use as a trick to avoid heartbeat keeping the cluster stable.
// That fast-path only exists in the net transport.
func TestRaft_PreVote_LeaderSpam(t *testing.T) {
CheckInteg(t)
conf := DefaultConfig()
conf.LocalID = ServerID("first")
conf.HeartbeatTimeout = 50 * time.Millisecond
conf.ElectionTimeout = 50 * time.Millisecond
conf.LeaderLeaseTimeout = 50 * time.Millisecond
conf.CommitTimeout = 5 * time.Second
conf.SnapshotThreshold = 100
conf.TrailingLogs = 10
// Create a single node
leader := MakeRaft(t, conf, true)
NoErr(WaitFor(leader, Leader), t)
// Join a few nodes!
var followers []*RaftEnv
for i := 0; i < 2; i++ {
conf.LocalID = ServerID(fmt.Sprintf("next-batch-%d", i))
env := MakeRaft(t, conf, false)
addr := env.trans.LocalAddr()
NoErr(WaitFuture(leader.raft.AddVoter(conf.LocalID, addr, 0, 0)), t)
followers = append(followers, env)
}
// Wait for a leader
_, err := WaitForAny(Leader, append([]*RaftEnv{leader}, followers...))
NoErr(err, t)
CheckConsistent(append([]*RaftEnv{leader}, followers...), t)
leaderT := leader.raft.trans
// spam all the followers with pre-vote requests from the leader
// those requests should be granted as long as the leader haven't changed.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
for {
ticker := time.NewTicker(conf.HeartbeatTimeout / 2)
for _, f := range followers {
rsp := RequestPreVoteResponse{}
reqPreVote := RequestPreVoteRequest{
RPCHeader: leader.raft.getRPCHeader(),
Term: leader.raft.getCurrentTerm() + 1,
LastLogIndex: leader.raft.getLastIndex(),
LastLogTerm: leader.raft.getCurrentTerm(),
}
// We don't need to check the error here because when leader change
// it will start failing with "rejecting pre-vote request since we have a leader"
_ = leaderT.(WithPreVote).RequestPreVote(f.raft.localID, f.raft.localAddr, &reqPreVote, &rsp)
}
select {
case <-ticker.C:
case <-ctx.Done():
return
}
}
}()
time.Sleep(time.Second)
// for all followers ignore heartbeat from current leader, so we can transition to candidate state.
// the purpose of this test is to verify that spamming nodes with pre-votes don't cause them to never
// transition to Candidates.
for _, f := range followers {
//copy f to avoid data race
f1 := f
f1.trans.SetHeartbeatHandler(func(rpc RPC) {
if a, ok := rpc.Command.(*AppendEntriesRequest); ok {
if ServerID(a.GetRPCHeader().ID) == leader.raft.localID {
resp := &AppendEntriesResponse{
RPCHeader: f1.raft.getRPCHeader(),
Term: f1.raft.getCurrentTerm(),
LastLog: f1.raft.getLastIndex(),
Success: false,
NoRetryBackoff: false,
}
rpc.Respond(resp, nil)
} else {
f.raft.processHeartbeat(rpc)
}
}
})
}
time.Sleep(1 * time.Second)
// New leader should be one of the former followers.
_, err = WaitForAny(Leader, followers)
NoErr(err, t)
}