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
/
cluster_test.go
647 lines (519 loc) · 14.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"syscall"
"testing"
"time"
"github.com/BurntSushi/toml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const expectTimeout = 10 * time.Second
type testVersion string
const dbName = "db"
const (
start testVersion = "START"
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 *testZK
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
process *exec.Cmd
bufferedOutput bytes.Buffer
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 := createTestZk(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: 500 * time.Millisecond,
Transport: &http.Transport{},
}
return &testCluster{
T: t,
binary: binary,
source: source,
sequinses: make([]*testSequins, 0),
zk: zk,
testClient: testClient,
}
}
func (tc *testCluster) addSequins() *testSequins {
port := 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.ZK.Servers = []string{tc.zk.addr}
config.Test.AllowLocalCluster = true
// Slow everything down to an observable level.
config.ThrottleLoads = duration{5 * time.Millisecond}
config.Test.UpgradeDelay = duration{1 * time.Second}
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) {
for i := 0; i < n; i++ {
tc.addSequins()
}
}
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 (tc *testCluster) startTest() {
for _, ts := range tc.sequinses {
ts.startTest()
}
}
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\n%s", ts.name, ts.bufferedOutput.String())
}
tc.zk.close()
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/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() {
versions := make(chan testVersion)
go func() {
lastVersion := start
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 && lastVersion != start {
ts.T.Logf("%s (lastVersion: %s)", err, lastVersion)
}
version = down
}
}
if version != lastVersion {
versions <- 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))
}
}()
if ts.process == nil {
go func() {
// Wait for the process to register as down, then start it.
first := <-versions
require.Equal(ts.T, down, first, "setup: sequins process should start as down")
ts.start()
ts.progression <- first
for v := range versions {
ts.progression <- v
}
}()
}
}
func (ts *testSequins) start() {
ts.process = exec.Command(ts.binary, "--config", ts.configPath)
ts.process.Stdout = &ts.bufferedOutput
ts.process.Stderr = &ts.bufferedOutput
ts.process.Start()
}
func (ts *testSequins) hup() {
ts.process.Process.Signal(syscall.SIGHUP)
}
func (ts *testSequins) stop() {
ts.process.Process.Signal(syscall.SIGTERM)
ts.process.Process.Wait()
}
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(down, noVersion, v3)
tc.setup()
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(down, noVersion, v1, v2, v3)
tc.setup()
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)
tc.makeVersionAvailable(v3)
tc.expectProgression(down, noVersion, v3)
tc.setup()
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)
tc.makeVersionAvailable(v3)
tc.expectProgression(down, noVersion, v3)
tc.setup()
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)
tc.makeVersionAvailable(v1)
tc.expectProgression(down, noVersion, v1, v2, v3)
tc.setup()
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)
tc.expectProgression(down, noVersion, v1, v2)
tc.makeVersionAvailable(v1)
tc.setup()
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 available.
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)
tc.expectProgression(down, noVersion, v3)
tc.makeVersionAvailable(v3)
tc.setup()
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)
tc.expectProgression(down, noVersion, v3)
tc.makeVersionAvailable(v3)
tc.setup()
tc.startTest()
time.Sleep(expectTimeout)
s := tc.addSequins()
s.makeVersionAvailable(v3)
s.setup()
s.expectProgression(down, 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()
tc.addSequinses(3)
// 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(down, noVersion, v1, v3)
tc.sequinses[1].expectProgression(down, noVersion, v1, v2, v3)
tc.sequinses[2].expectProgression(down, noVersion, v1, v2, v3)
tc.makeVersionAvailable(v1)
tc.setup()
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)
tc.makeVersionAvailable(v3)
tc.expectProgression(down, noVersion, v3, down, v3)
tc.setup()
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)
tc.makeVersionAvailable(v2)
tc.sequinses[0].expectProgression(down, noVersion, v2, down, v3)
tc.sequinses[1].expectProgression(down, noVersion, v2, v3)
tc.sequinses[2].expectProgression(down, noVersion, v2, v3)
tc.setup()
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()
}