-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsentinel_test.go
663 lines (603 loc) · 19.4 KB
/
sentinel_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
package sentinel
import (
"fmt"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
"golang.org/x/sync/errgroup"
)
var (
stdLogger *zap.SugaredLogger
)
func init() {
stdLogger = newLogger()
}
func customLogObserver() (*zap.SugaredLogger, *observer.ObservedLogs) {
observedCore, recordedLogs := observer.New(zap.DebugLevel)
normalCore := stdLogger.Desugar().Core()
teeCore := zapcore.NewTee(observedCore, normalCore)
built := zap.New(teeCore, zap.WithCaller(true)).Sugar()
return built, recordedLogs
}
var (
defaultMasterAddr = "localhost:6767"
)
type testSuite struct {
mu *sync.Mutex
mapRunIDtoIdx map[string]int // code use runID as identifier, test suite use integer index, use this to remap
mapIdxtoRunID map[int]string // code use runID as identifier, test suite use integer index, use this to remap
instances []*Sentinel
// masterLinks []*toyClient // each represent a connection from a sentinel to master
conf Config
cluster *InstancesManager
// master *ToyKeva
slavesMap map[string]*ToyKeva
history
logObservers []*observer.ObservedLogs
t *testing.T
}
type history struct {
currentLeader string
currentTerm int
termsVote map[int][]voteInfo // key by term seq, val is array of each sentinels' term info
termsVoteRequesters map[int][]string
termsLeader map[int]string
termsSelectedSlave map[int]string
termsPromotedSlave map[int]string
termsMasterInfo map[int]masterInfo
// masterSlaveMap map[string]map[string]string
instancesMasterSlaveMap map[string]instanceMasterSlaveMap
termsMasterInstanceCreation map[int][]string // check which sentinel has initialize master instance
failOverStates map[int]failOverState // capture current failoverstate of each instance
}
type instanceMasterSlaveMap = map[string][]string
type masterInfo struct {
runID string
addr string
}
type voteInfo struct {
selfVote string
neighborVotes map[string]string // info about what a sentinel sees other sentinel voted
}
func (t *testSuite) CleanUp() {
for _, instance := range t.instances {
instance.Shutdown()
}
}
// this function also pre spawn some goroutines to continuously update testSuite state according
// to event logs recorded from sentinels instance, test functions only need to assert those states
func setupWithCustomConfig(t *testing.T, numInstances int, customConf func(*Config)) *testSuite {
file := filepath.Join("test", "config", "sentinel.yaml")
viper.SetConfigType("yaml")
viper.SetConfigFile(file)
err := viper.ReadInConfig()
assert.NoError(t, err)
var conf Config
err = viper.Unmarshal(&conf)
assert.NoError(t, err)
if customConf != nil {
customConf(&conf)
}
masterAddr := conf.Masters[0].Addr
parts := strings.Split(masterAddr, ":")
host, port := parts[0], parts[1]
// fake cluster of instances
toyCluster := &InstancesManager{
addrMap: map[string]*ToyKeva{},
mu: &sync.Mutex{},
simulateConnections: map[string]*toyClient{},
}
cluster := toyCluster.NewMasterToyKeva(host, port)
slaveMap := cluster.SpawnSlaves(3)
// make master remember slaves
sentinels := []*Sentinel{}
logObservers := make([]*observer.ObservedLogs, numInstances)
testLock := &sync.Mutex{}
basePort := 2000
mapRunIDToIdx := map[string]int{}
mapIdxToRunID := map[int]string{}
for i := 0; i < numInstances; i++ {
localIdx := i
// start new sentinel instance
s, err := NewFromConfig(conf)
assert.NoError(t, err)
s.conf.Port = strconv.Itoa(basePort + localIdx)
mapRunIDToIdx[s.runID] = localIdx
mapIdxToRunID[localIdx] = s.runID
// a function to create fake client from sentinel to other instance
s.clientFactory = func(addr string) (InternalClient, error) {
curMaster := cluster.getMaster()
instance := cluster.getInstanceByAddr(addr)
client := NewToyKevaClient(instance)
if curMaster.getAddr() == addr {
cluster.mu.Lock()
defer cluster.mu.Unlock()
cluster.simulateConnections[s.runID] = client
}
return client, nil
}
// s.slaveFactory = toySlaveFactory
customLogger, observer := customLogObserver()
logObservers[localIdx] = observer
s.logger = customLogger
err = s.Start()
assert.NoError(t, err)
sentinels = append(sentinels, s)
}
// sleep for 2 second to ensure all sentinels have pubsub and recognized each other
time.Sleep(3 * time.Second)
for _, s := range sentinels {
s.mu.Lock()
masterI, ok := s.masterInstances[masterAddr]
assert.True(t, ok)
s.mu.Unlock()
masterI.mu.Lock()
assert.Equal(t, numInstances-1, len(masterI.sentinels))
masterI.mu.Unlock()
}
suite := &testSuite{
instances: sentinels,
// masterLinks: masterLinks,
mu: testLock,
conf: conf,
cluster: toyCluster,
// master: master,
slavesMap: slaveMap,
history: history{
termsVote: map[int][]voteInfo{},
termsVoteRequesters: map[int][]string{},
termsLeader: map[int]string{},
failOverStates: map[int]failOverState{},
termsSelectedSlave: map[int]string{},
termsPromotedSlave: map[int]string{},
termsMasterInfo: map[int]masterInfo{},
termsMasterInstanceCreation: map[int][]string{},
instancesMasterSlaveMap: map[string]instanceMasterSlaveMap{},
},
mapRunIDtoIdx: mapRunIDToIdx,
mapIdxtoRunID: mapIdxToRunID,
logObservers: logObservers,
t: t,
}
for idx := range suite.logObservers {
go suite.consumeLogs(idx, suite.logObservers[idx])
}
return suite
}
func setup(t *testing.T, numInstances int) *testSuite {
return setupWithCustomConfig(t, numInstances, nil)
}
func TestInit(t *testing.T) {
t.Run("3 instances", func(t *testing.T) {
s := setup(t, 3)
s.CleanUp()
})
t.Run("5 instances", func(t *testing.T) {
s := setup(t, 5)
s.CleanUp()
})
}
func TestSDown(t *testing.T) {
checkSdown := func(t *testing.T, numSdown int, numInstances int) {
suite := setupWithCustomConfig(t, numInstances, func(c *Config) {
//important: adjust quorum to the strictest level = numInstances
c.Masters[0].Quorum = numInstances
})
defer suite.CleanUp()
for _, s := range suite.instances {
s.mu.Lock()
masterI, ok := s.masterInstances[defaultMasterAddr]
assert.True(t, ok)
s.mu.Unlock()
masterI.mu.Lock()
assert.Equal(t, numInstances-1, len(masterI.sentinels))
masterI.mu.Unlock()
}
suite.cluster.mu.Lock()
counter := 0
downInstances := map[string]struct{}{}
for runID := range suite.cluster.simulateConnections {
s := suite.cluster.simulateConnections[runID]
downInstances[runID] = struct{}{}
s.disconnect()
counter++
if counter == numSdown {
break
}
}
suite.cluster.mu.Unlock()
// links[disconnectedIdx].disconnect()
time.Sleep(suite.conf.Masters[0].DownAfter)
// 1 more second for sure
time.Sleep(1 * time.Second)
// check if a given sentinel is in sdown state, and holds for a long time
for _, sentinel := range suite.instances {
_, isDown := downInstances[sentinel.selfID()]
if isDown {
// should be subjdown
checkMasterState(t, defaultMasterAddr, sentinel, masterStateSubjDown)
} else {
// should be normal
checkMasterState(t, defaultMasterAddr, sentinel, masterStateUp)
}
}
}
t.Run("1 out of 3 subjectively down", func(t *testing.T) {
checkSdown(t, 1, 3)
})
t.Run("2 out of 4 subjectively down", func(t *testing.T) {
checkSdown(t, 2, 3)
})
}
func TestODown(t *testing.T) {
testOdown := func(t *testing.T, numInstances int) {
suite := setup(t, numInstances)
defer suite.CleanUp()
for _, s := range suite.instances {
s.mu.Lock()
masterI, ok := s.masterInstances[defaultMasterAddr]
assert.True(t, ok)
s.mu.Unlock()
masterI.mu.Lock()
assert.Equal(t, numInstances-1, len(masterI.sentinels))
masterI.mu.Unlock()
}
suite.cluster.killCurrentMaster()
time.Sleep(suite.conf.Masters[0].DownAfter)
// check if a given sentinel is in sdown state, and holds for a long time
// others still see master is up
gr := errgroup.Group{}
for idx := range suite.instances {
localSentinel := suite.instances[idx]
gr.Go(func() error {
met := eventually(t, func() bool {
return masterStateIs(defaultMasterAddr, localSentinel, masterStateObjDown)
}, 5*time.Second, "sentinel %s did not recognize master as o down", localSentinel.listener.Addr())
if !met {
return fmt.Errorf("sentinel %s did not recognize master as o down", localSentinel.listener.Addr())
}
return nil
})
}
assert.NoError(t, gr.Wait())
}
t.Run("3 instances o down", func(t *testing.T) {
testOdown(t, 3)
})
t.Run("5 instances o down", func(t *testing.T) {
testOdown(t, 5)
})
}
func (suite *testSuite) checkTermVoteOfSentinel(t *testing.T, sentinelIdx int, term int) {
currentSentinel := suite.instances[sentinelIdx]
reached := eventually(t, func() bool {
suite.mu.Lock()
defer suite.mu.Unlock()
termInfo := suite.termsVote[term][sentinelIdx]
return termInfo.selfVote != ""
}, 10*time.Second, "sentinel %s never votes for any instance in term %d", currentSentinel.runID, term)
if !reached {
assert.FailNowf(suite.t, "sentinel did not vote for any instance", "")
}
}
func (suite *testSuite) checkTermVoteOfSentinelNeighbor(t *testing.T, instanceIdx int, neighborID string, term int) {
currentSentinel := suite.instances[instanceIdx]
eventually(t, func() bool {
suite.mu.Lock()
defer suite.mu.Unlock()
termInfo := suite.termsVote[term][instanceIdx]
if termInfo.neighborVotes == nil {
return false
}
vote := termInfo.neighborVotes[neighborID]
return vote != ""
}, 10*time.Second, "sentinel %s cannot get its neighbor's leader in term %d", currentSentinel.runID, term)
}
// - create a stream of logs, observe from stream and change status of test suite
// - assert function wait for the change of status only
func TestLeaderVoteNotConflict(t *testing.T) {
assertion := func(t *testing.T, numInstances int) {
suite := setupWithCustomConfig(t, numInstances, func(c *Config) {
c.Masters[0].Quorum = numInstances/2 + 1 // force normal quorum
})
defer suite.CleanUp()
suite.cluster.killCurrentMaster()
time.Sleep(suite.conf.Masters[0].DownAfter)
// check if a given sentinel is in sdown state, and holds for a long time
// others still see master is up
gr := errgroup.Group{}
suite.mu.Lock()
suite.termsVote[1] = make([]voteInfo, len(suite.instances))
suite.mu.Unlock()
for idx := range suite.instances {
instanceIdx := idx
gr.Go(func() error {
suite.checkTermVoteOfSentinel(t, instanceIdx, 1)
return nil
})
}
gr.Wait()
gr2 := errgroup.Group{}
for idx := range suite.instances {
localSentinel := suite.instances[idx]
m := getSentinelMaster(defaultMasterAddr, localSentinel)
m.mu.Lock()
for sentinelIdx := range m.sentinels {
si := m.sentinels[sentinelIdx]
si.mu.Lock()
neighborID := si.runID
si.mu.Unlock()
instanceIdx := idx
gr2.Go(func() error {
suite.checkTermVoteOfSentinelNeighbor(t, instanceIdx, neighborID, 1)
return nil
})
}
m.mu.Unlock()
}
gr2.Wait()
suite.mu.Lock()
defer suite.mu.Unlock()
for idx := range suite.instances {
thisInstanceHistory := suite.termsVote[1][idx]
thisInstanceVote := thisInstanceHistory.selfVote
thisInstanceID := suite.instances[idx].runID
for idx2 := range suite.instances {
if idx2 == idx {
continue
}
neiborInstanceVote := suite.termsVote[1][idx2]
if neiborInstanceVote.neighborVotes[thisInstanceID] != thisInstanceVote {
assert.Failf(t, "conflict vote between instances",
"instance %s records that instance %s voted for %s, but %s says it voted for %s",
suite.instances[idx2].runID,
thisInstanceID,
neiborInstanceVote.neighborVotes[thisInstanceID],
thisInstanceID,
thisInstanceVote,
)
}
}
}
// 1.for each instance, compare its vote with how other instances records its vote
// 2.record each instance leader, find real leader of that term
// 3.find that real leader and check if its failover state is something in selecting slave
}
t.Run("3 instances do not conflict", func(t *testing.T) {
assertion(t, 3)
})
}
func TestLeaderElection(t *testing.T) {
assertion := func(t *testing.T, numInstances int) {
suite := setupWithCustomConfig(t, numInstances, func(c *Config) {
c.Masters[0].Quorum = numInstances/2 + 1 // force normal quorum
})
defer suite.CleanUp()
suite.cluster.killCurrentMaster()
time.Sleep(suite.conf.Masters[0].DownAfter)
//TODO: check more info of this recognized leader
voters := suite.checkVotersOfTerm(1, numInstances)
expect := []string{}
for _, item := range suite.instances {
expect = append(expect, item.runID)
}
assert.ElementsMatch(t, expect, voters)
suite.checkClusterHasLeader()
}
t.Run("3 instances vote leader success", func(t *testing.T) {
assertion(t, 3)
})
}
func TestPromoteSlave(t *testing.T) {
// slaveCustomizer let test customize initial slave config (offset, priority) to check if sentinel choose
// correct instance for failover
assertion := func(t *testing.T, numInstances int, slaveCustomizer func(map[string]*ToyKeva) *ToyKeva) {
suite := setupWithCustomConfig(t, numInstances, func(c *Config) {
c.Masters[0].Quorum = numInstances/2 + 1 // force normal quorum
})
defer suite.CleanUp()
expectedChosenSlave := slaveCustomizer(suite.slavesMap)
suite.cluster.killCurrentMaster()
time.Sleep(suite.conf.Masters[0].DownAfter)
//TODO: check more info of this recognized leader
suite.checkClusterHasLeader()
promotedSlave := suite.checkTermPromotedSlave(1)
if promotedSlave == "" {
assert.FailNowf(t, "empty slave promoted", "term %d has no promoted slave", 1)
} else {
suite.mu.Lock()
selectedSlave := suite.termsSelectedSlave[1]
suite.mu.Unlock()
assert.Equal(t, selectedSlave, promotedSlave,
"different promoted and selected slave", "promoted (%s) slave is different from selected (%s) slave", promotedSlave, selectedSlave)
assert.Equal(t, expectedChosenSlave.id, promotedSlave, "wrong slave promoted", "want %s, but have %s", expectedChosenSlave.id,
promotedSlave,
)
}
}
t.Run("select slave by highest offset", func(t *testing.T) {
assertion(t, 3, func(slaveMap map[string]*ToyKeva) *ToyKeva {
for idx := range slaveMap {
slave := slaveMap[idx]
slave.mu.Lock()
slave.offset = 10
slave.mu.Unlock()
return slave
}
return nil
})
})
t.Run("select slave by highest priority", func(t *testing.T) {
assertion(t, 3, func(slaveMap map[string]*ToyKeva) *ToyKeva {
for idx := range slaveMap {
slave := slaveMap[idx]
slave.mu.Lock()
slave.priority = 10
slave.mu.Unlock()
return slave
}
return nil
})
})
}
func TestResetMasterInstance(t *testing.T) {
// slaveCustomizer let test customize initial slave config (offset, priority) to check if sentinel choose
// correct instance for failover
assertion := func(t *testing.T, numInstances int, slaveCustomizer func(map[string]*ToyKeva) *ToyKeva) {
suite := setupWithCustomConfig(t, numInstances, func(c *Config) {
c.Masters[0].Quorum = numInstances/2 + 1 // force normal quorum
})
defer suite.CleanUp()
expectedNewMaster := slaveCustomizer(suite.slavesMap)
expectedNewMaster.mu.Lock()
expectedID := expectedNewMaster.id
expectedNewMaster.mu.Unlock()
suite.cluster.killCurrentMaster()
time.Sleep(suite.conf.Masters[0].DownAfter)
//TODO: check more info of this recognized leader
promotedSlave := suite.checkTermPromotedSlave(1)
suite.checkClusterHasLeader()
masterInfo := suite.checkTermMasterInfo(1)
assert.Equal(t, promotedSlave, masterInfo.runID)
if masterInfo.runID != "" {
assert.Equal(t, masterInfo.runID, expectedID, "failover to wrong master", "term %d wants master %s instead of %s",
expectedID, masterInfo.runID)
}
// check if all instance create new master instance
instanceIDs := suite.checkTermMasterCreation(1, 3)
suite.mu.Lock()
totalInstances := []string{}
for _, s := range suite.instances {
totalInstances = append(totalInstances, s.runID)
}
assert.ElementsMatch(t, totalInstances, instanceIDs, "mismatch sentinels instances")
suite.mu.Unlock()
}
t.Run("select slave by highest offset", func(t *testing.T) {
assertion(t, 3, func(slaveMap map[string]*ToyKeva) *ToyKeva {
for idx := range slaveMap {
slave := slaveMap[idx]
slave.mu.Lock()
slave.offset = 10
slave.mu.Unlock()
return slave
}
return nil
})
})
t.Run("select slave by highest priority", func(t *testing.T) {
assertion(t, 3, func(slaveMap map[string]*ToyKeva) *ToyKeva {
for idx := range slaveMap {
slave := slaveMap[idx]
slave.mu.Lock()
slave.priority = 10
slave.mu.Unlock()
return slave
}
return nil
})
})
}
func (s *testSuite) checkTermMasterCreation(term int, length int) []string {
var ret []string
eventually(s.t, func() bool {
s.mu.Lock()
defer s.mu.Unlock()
sentinelIds, exist := s.termsMasterInstanceCreation[term]
if exist && len(sentinelIds) == length {
ret = sentinelIds
return true
}
return false
}, 7*time.Second, "term %d has not enough %d master creation event", term, length)
return ret
}
func (s *testSuite) checkTermMasterInfo(term int) masterInfo {
var ret masterInfo
eventually(s.t, func() bool {
s.mu.Lock()
defer s.mu.Unlock()
masterInfo, exist := s.termsMasterInfo[term]
if exist && masterInfo.runID != "" {
ret = masterInfo
return true
}
return false
}, 5*time.Second, "term %d has no master created", term)
return ret
}
func (s *testSuite) checkTermPromotedSlave(term int) string {
var ret string
eventually(s.t, func() bool {
s.mu.Lock()
defer s.mu.Unlock()
promoted, exist := s.termsPromotedSlave[term]
if exist && promoted != "" {
ret = promoted
return true
}
return false
}, 5*time.Second, "term %d has no slave promoted", term)
return ret
}
func (s *testSuite) checkVotersOfTerm(term int, expectVoters int) []string {
var ret []string
var votesNum int
eventually(s.t, func() bool {
var found bool
locked(s.mu, func() {
voteHistory := s.termsVote[term]
if voteHistory == nil {
return
}
var count int
var currentVoters []string
for idx, item := range voteHistory {
if item.selfVote != "" {
count++
currentVoters = append(currentVoters, s.mapIdxtoRunID[idx])
}
}
votesNum = count
found = (count == expectVoters)
if found {
ret = currentVoters
}
})
return found
}, 10*time.Second, "term %d have %d instead of %d voters", term, votesNum, expectVoters)
return ret
}
func (s *testSuite) checkClusterHasLeader() {
eventually(s.t, func() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.currentLeader != ""
}, 10*time.Second, "current leader is empty")
}
func getSentinelMaster(masterAddr string, s *Sentinel) *masterInstance {
s.mu.Lock()
m := s.masterInstances[masterAddr]
s.mu.Unlock()
return m
}
func eventually(t *testing.T, f func() bool, duration time.Duration, msgAndArgs ...interface{}) bool {
return assert.Eventually(t, f, duration, 50*time.Millisecond, msgAndArgs...)
}
func checkMasterState(t *testing.T, masterAddr string, s *Sentinel, state masterInstanceState) {
assert.Equal(t, state, getSentinelMaster(masterAddr, s).getState())
}
func masterStateIs(masterAddr string, s *Sentinel, state masterInstanceState) bool {
s.mu.Lock()
m := s.masterInstances[masterAddr]
s.mu.Unlock()
return state == m.getState()
}