-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
621 lines (489 loc) · 17.3 KB
/
Copy pathserver_test.go
File metadata and controls
621 lines (489 loc) · 17.3 KB
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package raft
import (
"os"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/fortytw2/leaktest"
"github.com/navgeet/raft/internal/util"
)
// Set by environment variable. Indicates whether snapshotting
// is on or off. If auto snapshotting is on, all tests
// (excluding the manual snapshot tests) will be run with snapshotting
// enabled.
var snapshotting bool
// The size of snapshots if snapshotting is enabled.
var snapshotSize int
// TestMain sets up the Raft tests.
func TestMain(m *testing.M) {
snapshotting = os.Getenv("SNAPSHOTS") == "true"
snapshotSize, _ = strconv.Atoi(os.Getenv("SNAPSHOT_SIZE"))
exitCode := m.Run()
os.Exit(exitCode)
}
// TestSingleServerElection checks whether a cluster consisting of
// a single server can elect a leader.
func TestSingleServerElection(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 1, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
cluster.checkLeaders(false)
}
// TestBasicElection checks whether a cluster can elect a leader
// when there are no failures.
func TestBasicElection(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 3, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
cluster.checkLeaders(false)
}
// TestElectLeaderDisconnect checks whether a cluster can
// still elect a leader when a single server is Disconnected.
func TestElectLeaderDisconnect(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 3, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
// Disconnect the leader.
leader := cluster.checkLeaders(false)
cluster.disconnectServer(leader)
// See if the cluster can still elect a new leader.
cluster.checkLeaders(false)
}
// TestFailElectLeaderDisconnect checks whether a leader is
// elected when a majority of the servers are Disconnected.
func TestFailElectLeaderDisconnect(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 3, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
// Disconnect the leader and one other server, leaving
// only one server that is capable of communicating.
disconnectServer1 := cluster.checkLeaders(false)
disconnectServer2 := (disconnectServer1 + 1) % 3
cluster.disconnectServer(disconnectServer1)
cluster.disconnectServer(disconnectServer2)
// Check if the server can elect itself as the leader.
// This should not be successful.
cluster.checkLeaders(true)
}
// TestSingleServerSubmit checks whether a cluster consisting of
// a single server can commit a command.
func TestSingleServerSubmit(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 1, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
cluster.checkLeaders(false)
operations := makeOperations(1)
cluster.submit(operations[0], false, false, 1)
}
// TestSingleSubmit checks whether the cluster can successfully
// commit a single command when there are no failures.
func TestBasicSubmit(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 3, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
cluster.checkLeaders(false)
operations := makeOperations(1)
cluster.submit(operations[0], false, false, 3)
}
// TestMultipleSubmit checks whether a cluster can successfully
// commit multiple operations when there are no failures.
func TestMultipleSubmit(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 5, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
cluster.checkLeaders(false)
operations := makeOperations(200)
for _, command := range operations {
cluster.submit(command, false, false, 5)
}
}
// TestConcurrentSubmit test whether operations are correctly
// applied when there are multiple clients submitting operations
// at the same time.
func TestConcurrentSubmit(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 5, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
cluster.checkLeaders(false)
operations := makeOperations(200)
var wg sync.WaitGroup
// Simulates a client submitting operations.
client := func(operations []Operation, readyCh chan interface{}) {
defer wg.Done()
<-readyCh
for _, command := range operations {
cluster.submit(command, false, false, 5)
}
}
// The number of clients submitting operations concurrently.
numClients := 10
// The number of command each client will submit.
operationsPerClient := len(operations) / numClients
// Signals to the clients that they can start submitting operations.
readyCh := make(chan interface{})
// Spin up the clients with their respective operations.
for i := 0; i < numClients; i++ {
clientOperations := operations[i*operationsPerClient : (i+1)*operationsPerClient]
wg.Add(1)
go client(clientOperations, readyCh)
}
// Allow clients to start and wait until they are done.
close(readyCh)
wg.Wait()
if t.Failed() {
t.Fatal("concurrent apply operations failed")
}
}
// TestSubmitDisconnect checks that a cluster can still
// commit operations after the leader is disconnected.
func TestSubmitDisconnect(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 3, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
// Disconnect the leader and see if operations are still committed.
leader := cluster.checkLeaders(false)
cluster.disconnectServer(leader)
operations := makeOperations(20)
for _, command := range operations {
cluster.submit(command, true, false, 2)
}
}
// TestSubmitDisconnectRejoin checks that a cluster correctly
// handles leaders being disconnected and rejoining after operations
// are submitted.
func TestSubmitDisconnectRejoin(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 5, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
// Disconnect the first leader.
leader1 := cluster.checkLeaders(false)
// Submit some operations with this leader.
operations := makeOperations(80)
for i := 0; i < 20; i++ {
cluster.submit(operations[i], false, false, 5)
}
// Disconnect the leader.
cluster.disconnectServer(leader1)
// Submit some more operations. Note that we only expect
// 4 servers to apply the command.
for i := 20; i < 40; i++ {
cluster.submit(operations[i], true, false, 4)
}
// Disconnect the second leader.
leader2 := cluster.checkLeaders(false)
// Submit some more operations. Note that we only expect
// 3 servers to apply the command.
for i := 40; i < 60; i++ {
cluster.submit(operations[i], true, false, 3)
}
// Allow the old leaders to rejoin.
cluster.reconnectServer(leader1)
cluster.reconnectServer(leader2)
// Submit some more operations. All servers should apply the
// command now.
for i := 60; i < 80; i++ {
cluster.submit(operations[i], true, false, 5)
}
}
// TestSubmitDisconnectFail checks that a cluster is unable to
// commit operations when a majority of the servers are completely
// disconnected from the cluster but still online.
func TestSubmitDisconnectFail(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 5, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
// Disconnect the leader and two other servers, leaving
// only a minority of the server able to communicate.
leader := cluster.checkLeaders(false)
cluster.disconnectServer(leader)
cluster.disconnectServer((leader + 1) % 5)
cluster.disconnectServer((leader + 2) % 5)
// Try to submit some operations. This should be unsuccessful
// since only a minority of the cluster can communicate.
operations := makeOperations(20)
for _, command := range operations {
cluster.submit(command, false, true, 1)
}
}
// TestUnreliableNetwork tests whether a cluster can still make
// progress submitting multiple operations when multiple servers
// become disconnected from the rest of the cluster.
func TestUnreliableNetwork(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 5, snapshotting, snapshotSize)
done := int32(0)
wg := sync.WaitGroup{}
unreliableNetRoutine := func() {
defer wg.Done()
for atomic.LoadInt32(&done) == 0 {
// Allow the cluster to make some progress with no failures.
randomTime := util.RandomTimeout(700*time.Millisecond, 900*time.Millisecond)
time.Sleep(randomTime * time.Millisecond)
// Disconnect two random servers.
disconnect1 := util.RandomInt(0, 5)
disconnect2 := (disconnect1 + 1) % 5
cluster.disconnectServer(disconnect1)
cluster.disconnectServer(disconnect2)
// Allow the cluster to make progress while the servers are disconnected.
randomTime = util.RandomTimeout(700*time.Millisecond, 900*time.Millisecond)
time.Sleep(randomTime * time.Millisecond)
// Reconnect the servers.
cluster.reconnectAllServers()
}
}
cluster.startCluster()
defer cluster.stopCluster()
cluster.checkLeaders(false)
// Start disconnecting random servers.
wg.Add(1)
go unreliableNetRoutine()
// See if we can commit operations in the face of recurring partitions.
operations := makeOperations(300)
for _, command := range operations {
cluster.submit(command, true, false, 3)
}
atomic.StoreInt32(&done, 1)
wg.Wait()
}
// TestBasicPartition checks that a cluster can still make
// progress submitting multiple operations when there is a single
// partition.
func TestBasicPartition(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 5, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
// Wait for a leader.
cluster.checkLeaders(false)
// Partition the cluster.
cluster.createPartition()
// Wait for a leader.
cluster.checkLeaders(false)
operations := makeOperations(50)
for _, command := range operations {
cluster.submit(command, true, false, 3)
}
// Heal the partition.
cluster.reconnectAllServers()
}
// TestMultiPartition checks whether a cluster can still make
// progress submitting multiple operations in the presence of
// multiple and changing partitions.
func TestMultiPartition(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 5, snapshotting, snapshotSize)
// A go routine to crash random servers every so often.
done := int32(0)
wg := sync.WaitGroup{}
partitionRoutine := func() {
defer wg.Done()
for atomic.LoadInt32(&done) == 0 {
// Allow the cluster to make some progress with no failures.
randomTime := util.RandomTimeout(700*time.Millisecond, 900*time.Millisecond)
time.Sleep(randomTime * time.Millisecond)
// Make a new partition.
cluster.createPartition()
// Allow the cluster to make progress with the partition.
randomTime = util.RandomTimeout(700*time.Millisecond, 900*time.Millisecond)
time.Sleep(randomTime * time.Millisecond)
// Heal the partition.
cluster.reconnectAllServers()
}
}
cluster.startCluster()
defer cluster.stopCluster()
cluster.checkLeaders(false)
// Start partitioning
wg.Add(1)
go partitionRoutine()
// See if we can commit operations in the face of recurring partitions.
operations := makeOperations(300)
for _, command := range operations {
cluster.submit(command, true, false, 3)
}
atomic.StoreInt32(&done, 1)
wg.Wait()
}
// TestBasicCrash checks that a cluster can still make
// progress after a single server crashes.
func TestBasicCrash(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 5, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
// Wait for a leader and submit some operations.
leader := cluster.checkLeaders(false)
operations := makeOperations(200)
for i := 0; i < 25; i++ {
cluster.submit(operations[i], false, false, 5)
}
// Crash the leader and see if we can still make progress.
cluster.crashServer(leader)
for i := 25; i < len(operations); i++ {
cluster.submit(operations[i], true, false, 4)
}
}
// TestCrashRejoin checks that a cluster correctly
// handles a server crashing and coming back online
// after operations are submitted.
func TestCrashRejoin(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 5, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
// Wait for a leader and submit some operations.
leader := cluster.checkLeaders(false)
operations := makeOperations(200)
for i := 0; i < 25; i++ {
cluster.submit(operations[i], false, false, 5)
}
// Crash the leader and see if we can still make progress.
cluster.crashServer(leader)
for i := 25; i < 150; i++ {
cluster.submit(operations[i], true, false, 4)
}
// Allow the leader to rejoin and see if we can make progress
// committing operations.
cluster.restartServer(leader)
for i := 150; i < len(operations); i++ {
cluster.submit(operations[i], true, false, 5)
}
}
// TestMultiCrash checks if a cluster can still make
// progress committing operations in the face of multiple
// crashes.
func TestMultiCrash(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 5, snapshotting, snapshotSize)
// A go routine to crash random servers every so often.
done := int32(0)
wg := sync.WaitGroup{}
crashRoutine := func() {
defer wg.Done()
for atomic.LoadInt32(&done) == 0 {
// Allow the cluster to make some progress with no failures.
randomTime := util.RandomTimeout(700*time.Millisecond, 900*time.Millisecond)
time.Sleep(randomTime * time.Millisecond)
// Crash two random servers.
crash1 := util.RandomInt(0, 5)
crash2 := (crash1 + 1) % 5
cluster.crashServer(crash1)
cluster.crashServer(crash2)
// Allow the cluster to make progress while the servers are offline.
randomTime = util.RandomTimeout(700*time.Millisecond, 900*time.Millisecond)
time.Sleep(randomTime * time.Millisecond)
// Bring the servers back online.
cluster.restartServer(crash1)
cluster.restartServer(crash2)
}
}
cluster.startCluster()
defer cluster.stopCluster()
cluster.checkLeaders(false)
// Start crashing servers.
wg.Add(1)
go crashRoutine()
// See if we can commit operations in the face of multiple crashes.
operations := makeOperations(300)
for _, command := range operations {
cluster.submit(command, true, false, 3)
}
atomic.StoreInt32(&done, 1)
wg.Wait()
}
// TestDisconnectCrashPartition checks whether the cluster can still
// make progress when there are disconnections, crashes, and partitions.
func TestDisconnectCrashPartition(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 5, snapshotting, snapshotSize)
// A go routine to crash, disconnect, and partition random servers every so often.
done := int32(0)
wg := sync.WaitGroup{}
failureRoutine := func() {
defer wg.Done()
for atomic.LoadInt32(&done) == 0 {
// Allow the cluster to make some progress with no failures.
randomTime := util.RandomTimeout(700*time.Millisecond, 900*time.Millisecond)
time.Sleep(randomTime * time.Millisecond)
// Choose a random type of failure.
action := util.RandomInt(0, 3)
switch action {
// Crash a single server.
case 0:
crash := util.RandomInt(0, 5)
cluster.crashServer(crash)
randomTime = util.RandomTimeout(700*time.Millisecond, 900*time.Millisecond)
time.Sleep(randomTime * time.Millisecond)
cluster.restartServer(crash)
// Disconnect a single server.
case 1:
disconnect := util.RandomInt(0, 5)
cluster.disconnectServer(disconnect)
randomTime = util.RandomTimeout(200*time.Millisecond, 400*time.Millisecond)
time.Sleep(randomTime * time.Millisecond)
cluster.reconnectAllServers()
// Partition the servers into two separate groups.
case 2:
cluster.createPartition()
randomTime = util.RandomTimeout(700*time.Millisecond, 900*time.Millisecond)
time.Sleep(randomTime * time.Millisecond)
cluster.reconnectAllServers()
}
}
}
cluster.startCluster()
defer cluster.stopCluster()
cluster.checkLeaders(false)
// Start causing failures.
wg.Add(1)
go failureRoutine()
// See if we can commit operations in the face of random network and server failures.
// Submit enough operations to ensure that a variety of failures occur.
operations := makeOperations(500)
for _, command := range operations {
cluster.submit(command, true, false, 3)
}
atomic.StoreInt32(&done, 1)
wg.Wait()
}
// TestAllCrash checks that a cluster can still make
// progress committing operations after all the servers
// crash and come back online.
func TestAllCrash(t *testing.T) {
defer leaktest.CheckTimeout(t, 1*time.Second)
cluster := newCluster(t, 5, snapshotting, snapshotSize)
cluster.startCluster()
defer cluster.stopCluster()
// Wait for a leader and submit some operations.
cluster.checkLeaders(false)
operations := makeOperations(50)
for i := 0; i < 25; i++ {
cluster.submit(operations[i], false, false, 5)
}
// Crash all servers.
for i := 0; i < 5; i++ {
cluster.crashServer(i)
}
// Restart all the servers.
for i := 0; i < 5; i++ {
cluster.restartServer(i)
}
// Wait for another leader and submit more operations.
cluster.checkLeaders(false)
for i := 25; i < len(operations); i++ {
cluster.submit(operations[i], true, false, 5)
}
}