-
Notifications
You must be signed in to change notification settings - Fork 1
/
integration_test.go
410 lines (358 loc) · 10.1 KB
/
integration_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
package sentinel
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/garyburd/redigo/redis"
redisv8 "github.com/go-redis/redis/v8"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"go.uber.org/zap/zaptest/observer"
)
func TestIntegration_SimpleCheck(t *testing.T) {
s := &failoverTestSuite{}
defer func() {
s.TearDownTest(t)
}()
s.SetUpSuite(t)
s.SetUpTest(t)
s.TestSimpleCheck(t)
}
func TestIntegration_Failover(t *testing.T) {
s := &failoverTestSuite{}
defer func() {
s.TearDownTest(t)
}()
s.SetUpSuite(t)
s.SetUpTest(t)
s.CheckFailover(t)
}
type failoverTestSuite struct {
}
var testPort = []int{16379, 16380, 16381}
func (s *failoverTestSuite) SetUpSuite(t *testing.T) {
_, err := exec.LookPath("redis-server")
assert.NoError(t, err)
}
func (s *failoverTestSuite) TearDownSuite(testing.T) {
}
func (s *failoverTestSuite) SetUpTest(t *testing.T) {
for _, port := range testPort {
s.stopRedis(t, port)
s.startRedis(t, port)
s.doCommand(t, port, "SLAVEOF", "NO", "ONE")
s.doCommand(t, port, "FLUSHALL")
}
}
func (s *failoverTestSuite) TearDownTest(t *testing.T) {
for _, port := range testPort {
fmt.Println("stopping redis")
s.stopRedis(t, port)
}
}
type redisChecker struct {
sync.Mutex
ok bool
buf bytes.Buffer
}
func (r *redisChecker) Write(data []byte) (int, error) {
r.Lock()
defer r.Unlock()
r.buf.Write(data)
if strings.Contains(r.buf.String(), "to accept connections") {
r.ok = true
}
fmt.Println(string(data))
return len(data), nil
}
func (s *failoverTestSuite) startRedis(t *testing.T, port int) {
checker := &redisChecker{ok: false}
// start redis and use memory only
cmd := exec.Command("redis-server", "--port", fmt.Sprintf("%d", port), "--save", "")
cmd.Stdout = checker
cmd.Stderr = checker
err := cmd.Start()
assert.NoError(t, err)
for i := 0; i < 20; i++ {
var ok bool
checker.Lock()
ok = checker.ok
checker.Unlock()
if ok {
return
}
time.Sleep(500 * time.Millisecond)
}
assert.FailNow(t, "redis-server can not start ok after 10s")
}
func (s *failoverTestSuite) stopRedis(c *testing.T, port int) {
cmd := exec.Command("redis-cli", "-p", fmt.Sprintf("%d", port), "shutdown", "nosave")
cmd.Run()
}
func (s *failoverTestSuite) doCommand(t *testing.T, port int, cmd string, cmdArgs ...interface{}) interface{} {
conn, err := redis.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
assert.NoError(t, err)
v, err := conn.Do(cmd, cmdArgs...)
assert.NoError(t, err)
return v
}
func (s *failoverTestSuite) TestSimpleCheck(t *testing.T) {
masterPort := testPort[0]
setupIntegration(t, 3, func(c *Config) {
c.Masters = []MasterMonitor{
{
Addr: fmt.Sprintf("127.0.0.1:%d", masterPort),
DownAfter: time.Second,
},
}
})
}
// after this return, numInstances of sentinels have already recognized master
func setupIntegration(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
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) {
client, err := newInternalClient(addr)
return client, err
}
// 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,
mu: testLock,
conf: conf,
history: history{
termsVote: map[int][]voteInfo{}, // vote history of each sentinel instance in terms
termsVoteRequesters: map[int][]string{}, // candidates who request for vote in terms
termsLeader: map[int]string{}, // candidates who became leader in terms
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,
logObservers: logObservers,
t: t,
}
for idx := range suite.logObservers {
go suite.consumeLogs(idx, suite.logObservers[idx])
}
return suite
}
func (s *failoverTestSuite) getRunID(masterAddr string) (string, error) {
str, err := redisv8.NewClient(&redisv8.Options{Addr: masterAddr}).Info(context.Background(), "server").Result()
if err != nil {
return "", err
}
sc := bufio.NewScanner(strings.NewReader(str))
sc.Split(bufio.ScanLines)
for sc.Scan() {
err := sc.Err()
if err != nil {
if errors.Is(err, io.EOF) {
return "", fmt.Errorf("cannot find runid")
}
return "", err
}
item := sc.Text()
if strings.HasPrefix(item, "run_id:") {
return item[7:], nil
}
}
return "", fmt.Errorf("cannot find runid")
}
func (s *failoverTestSuite) CheckFailover(t *testing.T) {
var (
masterAddr string
masterID string
)
masterPort := testPort[0]
s.buildReplTopo(t, testPort[0], testPort[1:])
suite := setupIntegration(t, 3, func(c *Config) {
masterAddr = fmt.Sprintf("127.0.0.1:%d", masterPort)
apiMasterID, err := s.getRunID(masterAddr)
assert.NoError(t, err)
masterID = apiMasterID
c.Masters = []MasterMonitor{
{
Name: "test",
RunID: masterID,
Addr: masterAddr,
DownAfter: time.Second,
Quorum: 2,
FailoverTimeout: time.Second * 10,
ReconfigSlaveTimeout: time.Second * 10,
ParallelSync: 1,
},
}
})
defer suite.CleanUp()
//slave instances connected
ok := eventually(t, func() bool {
suite.mu.Lock()
defer suite.mu.Unlock()
for runID := range suite.mapRunIDtoIdx {
masterSlavesMap, exist := suite.instancesMasterSlaveMap[runID]
if !exist {
return false
}
slaves, exist := masterSlavesMap[masterID]
if !exist {
return false
}
// may check more detail here
if len(slaves) != len(testPort[1:]) {
return false
}
}
return true
}, time.Second*3, "slave instances monitoring routines are not intitialized")
if !ok {
return
}
// setup slave for master
// kill master
s.stopRedis(t, masterPort)
// 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)
}
suite.mu.Unlock()
assert.ElementsMatch(t, totalInstances, instanceIDs, "mismatch sentinels instances")
correctMasterInfo := suite.checkTermMasterInfo(1)
masterAddrs := [][]string{}
for _, s := range suite.instances {
port := s.conf.Port
sentinelClient := redisv8.NewSentinelClient(&redisv8.Options{
Addr: fmt.Sprintf("localhost:%s", port),
})
cmd := sentinelClient.GetMasterAddrByName(context.Background(), "test")
assert.NoError(t, cmd.Err())
newMasterAddr, err := cmd.Result()
assert.NoError(t, err)
masterAddrs = append(masterAddrs, newMasterAddr)
}
// all sentinel report the same new master addr
for _, item := range masterAddrs[1:] {
assert.Equal(t, masterAddrs[0], item)
}
hasAddr := fmt.Sprintf("%s:%s", masterAddrs[0][0], masterAddrs[0][1])
assert.Equal(t, correctMasterInfo.addr, hasAddr)
// master address has changed
assert.NotEqual(t, masterAddr, masterAddr[0])
}
func (s *failoverTestSuite) buildReplTopo(t *testing.T, masterPort int, slavePorts []int) {
// port := testPort[0]
for _, p := range slavePorts {
s.doCommand(t, p, "SLAVEOF", "127.0.0.1", masterPort)
}
s.doCommand(t, masterPort, "SET", "a", 10)
s.doCommand(t, masterPort, "SET", "b", 20)
// wait data sync
for _, p := range slavePorts {
s.waitReplConnected(t, p, 10)
}
s.waitSync(t, masterPort, 10)
time.Sleep(2 * time.Second)
// slaves are fully sync
for _, p := range slavePorts {
n, err := redis.Int(s.doCommand(t, p, "GET", "a"), nil)
assert.NoError(t, err)
if err != nil {
fmt.Printf("here : %s\n", err.Error())
}
assert.Equal(t, 10, n)
}
}
func (s *failoverTestSuite) waitReplConnected(t *testing.T, port int, timeout int) {
for i := 0; i < timeout*2; i++ {
v, _ := redis.Values(s.doCommand(t, port, "ROLE"), nil)
tp, _ := redis.String(v[0], nil)
if tp == "slave" {
state, _ := redis.String(v[3], nil)
if state == ConnectedState || state == SyncState {
return
}
}
time.Sleep(500 * time.Millisecond)
}
logger.Fatalf("wait %ds, but 127.0.0.1:%d can not connect to master", timeout, port)
}
func (s *failoverTestSuite) waitSync(t *testing.T, port int, timeout int) {
g := newGroup(fmt.Sprintf("127.0.0.1:%d", port))
for i := 0; i < timeout*2; i++ {
err := g.doRole()
assert.NoError(t, err)
same := true
offset := g.Master.Offset
if offset > 0 {
for _, slave := range g.Slaves {
if slave.Offset != offset {
same = false
}
}
}
if same {
return
}
time.Sleep(500 * time.Millisecond)
}
logger.Fatalf("wait %ds, but all slaves can not sync the same with master %v", timeout, g)
}