This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcluster_test.go
1149 lines (935 loc) · 27.7 KB
/
cluster_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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"sync"
"syscall"
"testing"
"time"
"github.com/BurntSushi/toml"
"github.com/samuel/go-zookeeper/zk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stripe/sequins/zk/zktest"
)
const expectTimeout = 10 * time.Second
type testVersion string
const dbName = "db"
const (
noVersion testVersion = "NONE"
v1 testVersion = "v1"
v2 testVersion = "v2"
v3 testVersion = "v3"
down testVersion = "DOWN"
timeout testVersion = "TIMEOUT"
)
type testCluster struct {
*testing.T
binary string
source string
sequinses []*testSequins
zk *zk.TestCluster
testClient *http.Client
}
type testSequins struct {
*testing.T
name string
binary string
storePath string
configPath string
backendPath string
config sequinsConfig
testClient *http.Client
expectedProgression []testVersion
cmdError chan error
process *exec.Cmd
log *os.File
progression chan testVersion
}
func newTestCluster(t *testing.T) *testCluster {
binary, _ := filepath.Abs("sequins")
if _, err := os.Stat(binary); err != nil {
t.Skip("Skipping functional cluster tests because no binary is available. Please run the tests with 'make test'.")
}
source, err := ioutil.TempDir("", "sequins-cluster-")
require.NoError(t, err)
zk := zktest.New(t)
// Give the zookeeper cluster a chance to start up.
time.Sleep(1 * time.Second)
// We have a specific transport to the client, so it doesn't try to reuse
// connections between tests
var testClient = &http.Client{
Timeout: 5000 * time.Millisecond,
Transport: &http.Transport{},
}
return &testCluster{
T: t,
binary: binary,
source: source,
sequinses: make([]*testSequins, 0),
zk: zk,
testClient: testClient,
}
}
type configOption func(*sequinsConfig)
func repl(r int) configOption {
return func(config *sequinsConfig) {
config.Sharding.Replication = r
}
}
func minRepl(r int) configOption {
return func(config *sequinsConfig) {
config.Sharding.MinReplication = r
}
}
func maxRepl(r int) configOption {
return func(config *sequinsConfig) {
config.Sharding.MaxReplication = r
}
}
func noShardID() configOption {
return func(config *sequinsConfig) {
config.Sharding.ShardID = ""
}
}
func shardID(id string) configOption {
return func(config *sequinsConfig) {
config.Sharding.ShardID = id
}
}
func featureFlags(file *os.File) configOption {
return func(config *sequinsConfig) {
config.GoforitFlagJsonPath = file.Name()
config.Sharding.ClusterName = ""
}
}
func (tc *testCluster) addSequins(opts ...configOption) *testSequins {
port := zktest.RandomPort()
path := filepath.Join(tc.source, fmt.Sprintf("node-%d", port))
storePath := filepath.Join(path, "store")
err := os.MkdirAll(storePath, 0755|os.ModeDir)
require.NoError(tc.T, err, "setup: create store path")
backendPath := filepath.Join(path, "backend")
err = os.MkdirAll(backendPath, 0755|os.ModeDir)
require.NoError(tc.T, err, "setup: create backend path")
configPath := filepath.Join(path, "sequins.conf")
config := defaultConfig()
name := fmt.Sprintf("localhost:%d", port)
config.Bind = name
config.Source = backendPath
config.LocalStore = path
config.RequireSuccessFile = true
config.Sharding.Enabled = true
config.Sharding.TimeToConverge = duration{100 * time.Millisecond}
config.Sharding.ProxyTimeout = duration{600 * time.Millisecond}
config.Sharding.AdvertisedHostname = "localhost"
config.Sharding.ShardID = strconv.Itoa(len(tc.sequinses) + 1)
config.ZK.Servers = []string{fmt.Sprintf("localhost:%d", tc.zk.Servers[0].Port)}
config.Test.AllowLocalCluster = true
// Slow everything down to an observable level.
config.ThrottleLoads = duration{5 * time.Millisecond}
config.Test.UpgradeDelay = duration{1 * time.Second}
for _, opt := range opts {
opt(&config)
}
s := &testSequins{
T: tc.T,
name: name,
binary: tc.binary,
backendPath: backendPath,
configPath: configPath,
config: config,
testClient: tc.testClient,
progression: make(chan testVersion, 1024),
}
tc.sequinses = append(tc.sequinses, s)
return s
}
func (tc *testCluster) addSequinses(n int, opts ...configOption) {
for i := 0; i < n; i++ {
tc.addSequins(opts...)
}
}
func (tc *testCluster) expectProgression(versions ...testVersion) {
for _, ts := range tc.sequinses {
ts.expectProgression(versions...)
}
}
func (tc *testCluster) setup() {
for _, ts := range tc.sequinses {
ts.setup()
}
}
func (tc *testCluster) makeVersionAvailable(version testVersion) {
for _, ts := range tc.sequinses {
ts.makeVersionAvailable(version)
}
}
func (tc *testCluster) removeAvailableVersion(version testVersion) {
for _, ts := range tc.sequinses {
ts.removeAvailableVersion(version)
}
}
func (ts *testSequins) getPartitions(version testVersion) []int {
url := fmt.Sprintf("http://%s/healthz", ts.name)
res, err := ts.testClient.Get(url)
require.NoError(ts, err)
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
require.NoError(ts, err)
var dbs map[string]map[testVersion]nodeVersionStatus
err = json.Unmarshal(body, &dbs)
require.NoError(ts, err)
partitions := make([]int, 0)
if status, ok := dbs[dbName][version]; ok {
partitions = status.Partitions
sort.Ints(partitions)
}
return partitions
}
func (tc *testCluster) startTest() {
var wg sync.WaitGroup
wg.Add(len(tc.sequinses))
for _, ts := range tc.sequinses {
go func(ts *testSequins) {
defer wg.Done()
ts.startTest()
}(ts)
}
wg.Wait()
}
func (tc *testCluster) assertProgression() {
for _, ts := range tc.sequinses {
ts.assertProgression()
}
}
func (tc *testCluster) hup() {
for _, ts := range tc.sequinses {
ts.hup()
}
}
func (tc *testCluster) tearDown() {
for _, ts := range tc.sequinses {
ts.process.Process.Kill()
tc.T.Logf("Output for %s at %s", ts.name, ts.log.Name())
}
tc.zk.Stop()
os.RemoveAll(tc.source)
}
func (ts *testSequins) expectProgression(versions ...testVersion) {
ts.expectedProgression = versions
}
func (ts *testSequins) setup() {
f, err := os.Create(ts.configPath)
require.NoError(ts.T, err, "setup: create config")
err = toml.NewEncoder(f).Encode(ts.config)
require.NoError(ts.T, err, "setup: create config")
f.Close()
}
func (ts *testSequins) makeVersionAvailable(version testVersion) {
path := filepath.Join(ts.backendPath, dbName, string(version))
err := directoryCopy(ts.T, path, "test_databases/healthy/baby-names/1")
require.NoError(ts.T, err, "setup: make version available: %s", version)
f, err := os.Create(filepath.Join(path, "_SUCCESS"))
require.NoError(ts.T, err, "setup: make version available: %s", version)
f.Close()
}
func (ts *testSequins) removeAvailableVersion(version testVersion) {
path := filepath.Join(ts.backendPath, dbName, string(version))
os.RemoveAll(path)
}
func (ts *testSequins) startTest() {
ts.start()
go func() {
lastVersion := noVersion
bootingUp := true
for {
now := time.Now()
key := babyNames[rand.Intn(len(babyNames))].key
url := fmt.Sprintf("http://%s/%s/%s", ts.name, dbName, key)
var version testVersion
resp, err := ts.testClient.Get(url)
if err == nil {
v := resp.Header.Get("X-Sequins-Version")
resp.Body.Close()
if resp.StatusCode > 404 {
version = down
} else if v == "" {
version = noVersion
} else {
version = testVersion(v)
}
} else {
// A number of timeouts are ok - this isn't the friendliest environment,
// after all. We want to fail fast and frequently so that we don't
// miss changes to the available version.
netErr, ok := err.(net.Error)
if ok && netErr.Timeout() {
version = lastVersion
} else {
if lastVersion != down {
ts.T.Logf("%s (lastVersion: %s)", err, lastVersion)
}
version = down
}
}
if bootingUp && version == down {
version = lastVersion
} else {
bootingUp = false
}
if version != lastVersion {
ts.progression <- version
lastVersion = version
}
// Sleep for 250 milliseconds less the time we took to make the last
// request, such that we make a request roughly every 250 milliseconds.
time.Sleep((250 * time.Millisecond) - time.Now().Sub(now))
}
}()
}
func (ts *testSequins) start() {
for i := 0; i < 3; i++ {
ts.setup()
log, err := ioutil.TempFile("", "sequins-test-cluster-")
require.NoError(ts.T, err, "setup: creating log")
ts.log = log
ts.process = exec.Command(ts.binary, "--config", ts.configPath)
ts.process.Stdout = log
ts.process.Stderr = log
ts.process.Start()
ts.cmdError = make(chan error, 1)
go func(cmdError chan error) {
cmdError <- ts.process.Wait()
}(ts.cmdError)
select {
case err = <-ts.cmdError:
ts.config.Bind = fmt.Sprintf("localhost:%d", zktest.RandomPort())
ts.name = ts.config.Bind
case <-time.After(time.Second * 1):
return
}
}
ts.T.Fatal("Sequins did not start up after 3 tries")
}
func (ts *testSequins) hup() {
ts.process.Process.Signal(syscall.SIGHUP)
}
func (ts *testSequins) stop() {
ts.process.Process.Signal(syscall.SIGTERM)
<-ts.cmdError
}
func (ts *testSequins) assertProgression() {
var actualProgression []testVersion
Progression:
for range ts.expectedProgression {
t := time.NewTimer(expectTimeout)
select {
case v := <-ts.progression:
actualProgression = append(actualProgression, v)
case <-t.C:
actualProgression = append(actualProgression, timeout)
break Progression
}
}
expected := ""
for _, v := range ts.expectedProgression {
if expected != "" {
expected += " -> "
}
expected += string(v)
}
actual := ""
for _, v := range actualProgression {
if actual != "" {
actual += " -> "
}
actual += string(v)
}
assert.Equal(ts.T, expected, actual, "unexpected progression for %s", ts.name)
}
// TestClusterEmptySingleNode tests that a node with no preexisting state can start up
// and serve requests.
func TestClusterEmptySingleNode(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
tc.addSequinses(1)
tc.makeVersionAvailable(v3)
tc.expectProgression(v3)
tc.startTest()
tc.assertProgression()
}
// TestClusterUpgradingSingleNode tests that a node can upgrade to one version, and
// then upgrade a second and third time.
func TestClusterUpgradingSingleNode(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
tc.addSequinses(1)
tc.makeVersionAvailable(v1)
tc.expectProgression(v1, v2, v3)
tc.startTest()
time.Sleep(expectTimeout)
tc.makeVersionAvailable(v2)
tc.hup()
time.Sleep(expectTimeout)
tc.makeVersionAvailable(v3)
tc.hup()
tc.assertProgression()
}
// TestClusterEmpty tests that a cluster with no preexisting state can start up
// and serve requests.
func TestClusterEmpty(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
tc.addSequinses(3, minRepl(2))
tc.makeVersionAvailable(v3)
tc.expectProgression(v3)
tc.startTest()
tc.assertProgression()
}
// TestClusterEmpty tests that a cluster with many nodes and no preexisting
// state can start up and serve requests.
func TestLargeClusterEmpty(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
tc.addSequinses(30, minRepl(2))
tc.makeVersionAvailable(v3)
tc.expectProgression(v3)
tc.startTest()
tc.assertProgression()
}
// TestClusterUpgrading tests that a cluster can upgrade to one version, and
// then upgrade a second and third time.
func TestClusterUpgrading(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
tc.addSequinses(3, minRepl(2))
tc.makeVersionAvailable(v1)
tc.expectProgression(v1, v2, v3)
tc.startTest()
time.Sleep(expectTimeout)
tc.makeVersionAvailable(v2)
tc.hup()
time.Sleep(expectTimeout)
tc.makeVersionAvailable(v3)
tc.hup()
tc.assertProgression()
}
// TestClusterDelayedUpgrade tests that one node can upgrade several seconds earlier
// that the rest of the cluster without losing any reads.
func TestClusterDelayedUpgrade(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
tc.addSequinses(3, minRepl(2))
tc.expectProgression(v1, v2)
tc.makeVersionAvailable(v1)
tc.startTest()
time.Sleep(expectTimeout)
tc.makeVersionAvailable(v2)
tc.sequinses[0].hup()
time.Sleep(expectTimeout)
tc.hup()
tc.assertProgression()
}
// TestClusterNoDowngrade tests that a cluster will never downgrade to an older
// version, even if the newer one is unavailable.
func TestClusterNoDowngrade(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
tc.addSequinses(3, minRepl(2))
tc.expectProgression(v3)
tc.makeVersionAvailable(v3)
tc.startTest()
time.Sleep(expectTimeout)
tc.makeVersionAvailable(v2)
tc.removeAvailableVersion(v3)
tc.hup()
tc.assertProgression()
}
// TestClusterLateJoin tests if a node can join an existing cluster and
// immediately start serving the version that the rest of the cluster has.
func TestClusterLateJoin(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
tc.addSequinses(3, minRepl(2))
tc.expectProgression(v3)
tc.makeVersionAvailable(v3)
tc.startTest()
time.Sleep(expectTimeout)
s := tc.addSequins(minRepl(2))
s.makeVersionAvailable(v3)
s.expectProgression(v3)
s.startTest()
tc.assertProgression()
}
// TestClusterNodeWithoutData tests if a node can safely stay behind while
// the rest of the cluster upgrades.
func TestClusterNodeWithoutData(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
// Needs repl(3) so two nodes can cover two of each partition.
tc.addSequinses(3, repl(3), minRepl(2))
// By default this is 10 minutes; we're reducing it to confirm that
// nodes are not removing versions that their peers still have.
tc.sequinses[0].config.Test.VersionRemoveTimeout = duration{5 * time.Second}
tc.sequinses[1].config.Test.VersionRemoveTimeout = duration{5 * time.Second}
tc.sequinses[2].config.Test.VersionRemoveTimeout = duration{5 * time.Second}
// Because it's behind, it's expected that the first node will flap when it
// proxies requests and gets v2 from peers.
// tc.sequinses[0].expectProgression(v1, v3)
tc.sequinses[1].expectProgression(v1, v2, v3)
tc.sequinses[2].expectProgression(v1, v2, v3)
tc.makeVersionAvailable(v1)
tc.startTest()
time.Sleep(expectTimeout)
tc.sequinses[1].makeVersionAvailable(v2)
tc.sequinses[2].makeVersionAvailable(v2)
tc.hup()
time.Sleep(expectTimeout)
tc.makeVersionAvailable(v3)
tc.hup()
tc.assertProgression()
}
// TestClusterRollingRestart tests that a cluster can be restarted a node at a
// time and stay on the same version continually.
func TestClusterRollingRestart(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
tc.addSequinses(3, minRepl(2))
tc.makeVersionAvailable(v3)
tc.expectProgression(v3, down, v3)
tc.startTest()
time.Sleep(expectTimeout)
for _, s := range tc.sequinses {
s.stop()
time.Sleep(expectTimeout)
s.start()
time.Sleep(expectTimeout)
}
tc.assertProgression()
}
// TestClusterNodeVacation tests that if a node is down while the rest of a
// cluster upgrades without it, it can rejoin without issue.
func TestClusterNodeVacation(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
tc.addSequinses(3, minRepl(2))
tc.makeVersionAvailable(v2)
tc.sequinses[0].expectProgression(v2, down, v3)
tc.sequinses[1].expectProgression(v2, v3)
tc.sequinses[2].expectProgression(v2, v3)
tc.startTest()
time.Sleep(expectTimeout)
tc.sequinses[0].stop()
time.Sleep(expectTimeout)
tc.makeVersionAvailable(v3)
tc.sequinses[1].hup()
tc.sequinses[2].hup()
time.Sleep(expectTimeout)
tc.sequinses[0].start()
tc.assertProgression()
}
type crashTest struct {
MinReplication int
VersionAfterCrash string
MissingData bool
}
func testCrash(t *testing.T, testCase crashTest) {
tc := newTestCluster(t)
defer tc.tearDown()
tc.addSequinses(3, repl(2), minRepl(testCase.MinReplication))
// Keep one sequins permanently hanging
tc.sequinses[1].config.Test.Hang = hangAfterRead{
Version: "v2",
File: "part-00003",
}
tc.makeVersionAvailable(v1)
tc.expectProgression(v1)
tc.startTest()
tc.assertProgression()
tc.makeVersionAvailable(v2)
tc.hup()
time.Sleep(expectTimeout)
// Simulate a crash.
tc.sequinses[2].stop()
// Ensure node 2 is down.
url := fmt.Sprintf("http://%s/%s/%s", tc.sequinses[2].name, dbName, babyNames[0].key)
_, err := tc.testClient.Get(url)
assert.Error(t, err)
testCrashStatus(t, tc, testCase.VersionAfterCrash, testCase.MissingData)
// When nodes come back up, we should be fine.
tc.sequinses[1].stop()
tc.sequinses[1].config.Test.Hang = hangAfterRead{}
tc.sequinses[1].start()
tc.sequinses[2].start()
time.Sleep(expectTimeout)
testCrashStatus(t, tc, "v2", false)
}
func testCrashStatus(t *testing.T, tc *testCluster, versionAfterCrash string, missingData bool) {
// Check for missing data.
numErrors := 0
numSuccess := 0
for _, name := range babyNames {
url := fmt.Sprintf("http://%s/%s/%s", tc.sequinses[0].name, dbName, name.key)
resp, err := tc.testClient.Get(url)
require.NoError(t, err)
resp.Body.Close()
if resp.StatusCode >= 500 && resp.StatusCode < 600 {
numErrors++
} else {
require.Equal(t, 200, resp.StatusCode)
numSuccess++
v := resp.Header.Get("X-Sequins-Version")
assert.Equal(t, versionAfterCrash, v)
}
}
assert.NotZero(t, numSuccess)
// If data should be missing, node 0 returns error for at least some key.
if missingData {
assert.NotZero(t, numErrors)
} else {
assert.Zero(t, numErrors)
}
}
// TestClusterCrashMin tests that min_replication allows robustness in case
// of a crash just after a version is complete.
func TestClusterCrashMin(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
testCases := []crashTest{
// With min_replication = 1, we prematurely go to v2, and then data is missing
// after a crash.
{1, "v2", true},
// With min_replication = 2, we hold off on v2, and still can serve v1.
{2, "v1", false},
}
for _, testCase := range testCases {
t.Run(fmt.Sprintf("MinRepl-%d", testCase.MinReplication), func(t *testing.T) {
t.Parallel()
testCrash(t, testCase)
})
}
}
func testMinReplication(t *testing.T, mrepl int, progression testVersion) {
tc := newTestCluster(t)
defer tc.tearDown()
tc.addSequinses(4, repl(3), minRepl(mrepl))
tc.makeVersionAvailable(v1)
tc.expectProgression(v1)
tc.startTest()
tc.assertProgression()
tc.sequinses[3].stop()
tc.makeVersionAvailable(v2)
tc.hup()
tc.sequinses[3].expectProgression(down)
for i := 0; i <= 2; i++ {
tc.sequinses[i].expectProgression(progression)
}
tc.assertProgression()
}
// TestClusterMin tests that 1 < min_replication < replication < count(shard_id)
// allows upgrades even if a node goes down.
func TestClusterMin(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
testCases := []struct {
MinReplication int
Progression testVersion
}{
{3, timeout},
{2, v2},
}
for _, testCase := range testCases {
t.Run(fmt.Sprintf("MinRepl-%d", testCase.MinReplication), func(t *testing.T) {
t.Parallel()
testMinReplication(t, testCase.MinReplication, testCase.Progression)
})
}
}
// TestMaxReplication tests that nodes within the cluster will respect
// partition replication limits.
func TestMaxReplication(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
// Create a partially full cluster. Both nodes should grab all the partitions.
s := tc.addSequins(repl(2), maxRepl(2), shardID("1"))
s.makeVersionAvailable(v1)
s.expectProgression(v1)
s.startTest()
time.Sleep(expectTimeout)
s = tc.addSequins(repl(2), maxRepl(2), shardID("2"))
s.makeVersionAvailable(v1)
s.expectProgression(v1)
s.startTest()
time.Sleep(expectTimeout)
expectedAssignments := [][]int{{0, 1, 2, 3, 4}, {0, 1, 2, 3, 4}}
for i, host := range tc.sequinses {
id := host.getShardID(t)
require.Equal(t, expectedAssignments[i], host.getPartitions(v1), "partition assignment for node (%s) should be correct", id)
}
// Add a node that will respect the maximum replication and thus fetch
// no nodes.
s = tc.addSequins(repl(2), maxRepl(2), shardID("3"))
s.makeVersionAvailable(v1)
s.expectProgression(v1)
s.startTest()
time.Sleep(expectTimeout)
expectedAssignments = [][]int{{0, 1, 2, 3, 4}, {0, 1, 2, 3, 4}, {}}
for i, host := range tc.sequinses {
id := host.getShardID(t)
require.Equal(t, expectedAssignments[i], host.getPartitions(v1), "partition assignment for node (%s) should be correct", id)
}
// Add a node that will have the maximum replication limit disabled and
// thus fetch partitions as normal.
s = tc.addSequins(repl(2), maxRepl(0), shardID("4"))
s.makeVersionAvailable(v1)
s.expectProgression(v1)
s.startTest()
time.Sleep(expectTimeout)
expectedAssignments = [][]int{{0, 1, 2, 3, 4}, {0, 1, 2, 3, 4}, {}, {1, 3}}
for i, host := range tc.sequinses {
id := host.getShardID(t)
require.Equal(t, expectedAssignments[i], host.getPartitions(v1), "partition assignment for node (%s) should be correct", id)
}
}
func (ts *testSequins) getShardID(t *testing.T) string {
url := fmt.Sprintf("http://%s/healthz", ts.name)
resp, err := ts.testClient.Get(url)
assert.NoError(t, err)
defer resp.Body.Close()
return resp.Header.Get("X-Sequins-Shard-ID")
}
func TestClusterAutoAssignSingleID(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
s1 := tc.addSequins(noShardID())
tc.makeVersionAvailable(v3)
tc.startTest()
time.Sleep(expectTimeout)
shardID := s1.getShardID(t)
assert.Equal(t, "1", shardID)
}
// Test shardID auto-assignment when spinning up nodes one at a time.
func TestClusterAutoAssignMultipleIDs(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
for i := 1; i <= 3; i++ {
s := tc.addSequins(noShardID())
s.makeVersionAvailable(v3)
s.startTest()
time.Sleep(expectTimeout)
}
for i, s := range tc.sequinses {
shardID := fmt.Sprintf("%d", i+1)
assert.Equal(t, shardID, s.getShardID(t))
}
}
// Test shardID auto-assignment when the nodes start at the same time.
func TestClusterAutoAssignParallelIDs(t *testing.T) {
if testing.Short() {
t.Skip("skipping cluster test in short mode.")
}
t.Parallel()
tc := newTestCluster(t)
defer tc.tearDown()
tc.addSequinses(3, noShardID())
tc.makeVersionAvailable(v3)
tc.startTest()
time.Sleep(expectTimeout * 3)
for i := 1; i <= 3; i++ {
shardID := fmt.Sprintf("%d", i)
found := false
for _, s := range tc.sequinses {
if s.getShardID(t) == shardID {
found = true
break
}