-
Notifications
You must be signed in to change notification settings - Fork 113
/
sentinel_test.go
460 lines (400 loc) · 11.2 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
package radix
import (
"fmt"
"net"
"strings"
"sync"
. "testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type sentinelStub struct {
sync.Mutex
// The addresses of the actual instances this stub returns. We ignore the
// primary name for the tests
primAddr string
secAddrs []string
// addresses of all "sentinels" in the cluster
sentAddrs []string
// stubChs which have been created for stubs and want to know about
// switch-master messages
stubChs map[chan<- PubSubMessage]bool
}
func newSentinelStub(primAddr string, secAddrs, sentAddrs []string) sentinelStub {
return sentinelStub{
primAddr: primAddr,
secAddrs: secAddrs,
sentAddrs: sentAddrs,
stubChs: map[chan<- PubSubMessage]bool{},
}
}
func addrToM(addr string) map[string]string {
thisM := map[string]string{}
thisM["ip"], thisM["port"], _ = net.SplitHostPort(addr)
return thisM
}
type sentinelStubConn struct {
*sentinelStub
Conn
stubCh chan<- PubSubMessage
}
func (ssc *sentinelStubConn) Close() error {
ssc.sentinelStub.Lock()
defer ssc.sentinelStub.Unlock()
delete(ssc.sentinelStub.stubChs, ssc.stubCh)
return ssc.Conn.Close()
}
// addr must be one of sentAddrs.
func (s *sentinelStub) newConn(network, addr string) (Conn, error) {
s.Lock()
defer s.Unlock()
var found bool
for _, sentAddr := range s.sentAddrs {
if sentAddr == addr {
found = true
break
}
}
if !found {
return nil, fmt.Errorf("%q not in sentinel cluster", addr)
}
conn, stubCh := PubSubStub(network, addr, func(args []string) interface{} {
s.Lock()
defer s.Unlock()
if args[0] != "SENTINEL" {
return fmt.Errorf("command %q not supported by stub", args[0])
}
switch args[1] {
case "MASTER":
return addrToM(s.primAddr)
case "SLAVES":
mm := make([]map[string]string, len(s.secAddrs))
for i := range s.secAddrs {
mm[i] = addrToM(s.secAddrs[i])
}
return mm
case "SENTINELS":
ret := []map[string]string{}
for _, otherAddr := range s.sentAddrs {
if otherAddr == addr {
continue
}
ret = append(ret, addrToM(otherAddr))
}
return ret
default:
return fmt.Errorf("subcommand %q not supported by stub", args[1])
}
})
s.stubChs[stubCh] = true
return &sentinelStubConn{
sentinelStub: s,
Conn: conn,
stubCh: stubCh,
}, nil
}
func (s *sentinelStub) switchPrimary(newPrimAddr string, newSecAddrs ...string) {
s.Lock()
defer s.Unlock()
oldSplit := strings.Split(s.primAddr, ":")
newSplit := strings.Split(newPrimAddr, ":")
msg := PubSubMessage{
Channel: "switch-master",
Message: []byte(fmt.Sprintf("stub %s %s %s %s", oldSplit[0], oldSplit[1], newSplit[0], newSplit[1])),
}
s.primAddr = newPrimAddr
s.secAddrs = newSecAddrs
for stubCh := range s.stubChs {
stubCh <- msg
}
}
func TestSentinel(t *T) {
stub := newSentinelStub(
"127.0.0.1:6379", // primAddr
[]string{"127.0.0.2:6379", "127.0.0.3:6379"}, // secAddrs
[]string{"127.0.0.1:26379", "127.0.0.2:26379", "[0:0:0:0:0:ffff:7f00:3]:26379"}, // sentAddrs
)
// our fake poolFn will always _actually_ connect to 127.0.0.1, we just
// don't tell anyone
poolFn := func(string, string) (Client, error) {
return NewPool("tcp", "127.0.0.1:6379", 10)
}
scc, err := NewSentinel(
"stub", stub.sentAddrs,
SentinelConnFunc(stub.newConn), SentinelPoolFunc(poolFn),
)
require.Nil(t, err)
assertState := func(primAddr string, secAddrs, sentAddrs []string) {
gotPrimAddr, gotSecAddrs := scc.Addrs()
assert.Equal(t, primAddr, gotPrimAddr)
assert.Len(t, gotSecAddrs, len(secAddrs))
for i := range secAddrs {
assert.Contains(t, gotSecAddrs, secAddrs[i])
}
gotSentAddrs := scc.SentinelAddrs()
assert.Len(t, gotSentAddrs, len(sentAddrs))
for i := range sentAddrs {
assert.Contains(t, gotSentAddrs, sentAddrs[i])
}
scc.l.RLock()
assert.Len(t, scc.sentinelAddrs, len(sentAddrs))
for i := range sentAddrs {
assert.Contains(t, scc.sentinelAddrs, sentAddrs[i])
}
scc.l.RUnlock()
}
assertPoolWorks := func() {
c := 10
wg := new(sync.WaitGroup)
wg.Add(c)
for i := 0; i < c; i++ {
go func() {
key, val := randStr(), randStr()
require.Nil(t, scc.Do(Cmd(nil, "SET", key, val)))
var out string
require.Nil(t, scc.Do(Cmd(&out, "GET", key)))
assert.Equal(t, val, out)
wg.Done()
}()
}
wg.Wait()
}
assertState(
"127.0.0.1:6379",
[]string{"127.0.0.2:6379", "127.0.0.3:6379"},
[]string{"127.0.0.1:26379", "127.0.0.2:26379", "[0:0:0:0:0:ffff:7f00:3]:26379"},
)
assertPoolWorks()
stub.switchPrimary("127.0.0.2:6379", "127.0.0.3:6379")
go assertPoolWorks()
assert.Equal(t, "switch-master completed", <-scc.testEventCh)
assertState(
"127.0.0.2:6379",
[]string{"127.0.0.3:6379"},
[]string{"127.0.0.1:26379", "127.0.0.2:26379", "[0:0:0:0:0:ffff:7f00:3]:26379"},
)
assertPoolWorks()
// Check that closing the instance doesn't deadlock while updating the state.
// This is racy but should be good enough for now.
scc.l.Lock()
scc.forceMasterSwitch(100 * time.Millisecond) // delay so that Close has a chance to acquire scc.l
scc.l.Unlock()
require.NoError(t, scc.Close())
}
type stubSentinelPool struct {
Client // to inherit, but not use
addr string
closed bool
}
func (ssp *stubSentinelPool) Close() error {
ssp.closed = true
return nil
}
// this also tests that Clients get carried over during failover.
func TestSentinelClientsAddrs(t *T) {
type testState struct {
primAddr string
nilSecAddrs, secAddrs []string
}
secAddrsM := func(ts testState) map[string]bool {
m := map[string]bool{}
for _, addr := range ts.nilSecAddrs {
m[addr] = true
}
for _, addr := range ts.secAddrs {
m[addr] = true
}
return m
}
secAddrs := func(ts testState) []string {
m := secAddrsM(ts)
l := make([]string, 0, len(m))
for addr := range m {
l = append(l, addr)
}
return l
}
assertAddrs := func(ts testState, sc *Sentinel) {
gotPrimAddr, gotSecAddrs := sc.Addrs()
assert.Equal(t, ts.primAddr, gotPrimAddr)
expSecAddrs := secAddrsM(ts)
assert.Len(t, gotSecAddrs, len(expSecAddrs))
for addr := range expSecAddrs {
assert.Contains(t, gotSecAddrs, addr)
}
}
type testCase struct {
start, end testState
closed []string
}
poolFn := func(network, addr string) (Client, error) {
return &stubSentinelPool{addr: addr}, nil
}
cases := []testCase{
{
start: testState{primAddr: "A:0"},
end: testState{primAddr: "B:0"},
closed: []string{"A:0"},
},
{
start: testState{primAddr: "A:0"},
end: testState{primAddr: "B:0", secAddrs: []string{"A:0"}},
},
{
start: testState{
primAddr: "A:0",
nilSecAddrs: []string{"B:0"},
},
end: testState{primAddr: "B:0", secAddrs: []string{"A:0"}},
},
{
start: testState{
primAddr: "A:0",
nilSecAddrs: []string{"B:0", "C:0"},
},
end: testState{
primAddr: "B:0",
nilSecAddrs: []string{"C:0"},
},
closed: []string{"A:0"},
},
{
start: testState{
primAddr: "A:0",
nilSecAddrs: []string{"B:0"},
secAddrs: []string{"C:0"},
},
end: testState{
primAddr: "B:0",
secAddrs: []string{"C:0"},
},
closed: []string{"A:0"},
},
{
start: testState{
primAddr: "A:0",
nilSecAddrs: []string{"B:0"},
secAddrs: []string{"C:0"},
},
end: testState{
primAddr: "A:0",
nilSecAddrs: []string{"B:0"},
},
closed: []string{"C:0"},
},
{
start: testState{
primAddr: "A:0",
nilSecAddrs: []string{"B:0"},
secAddrs: []string{"C:0"},
},
end: testState{
primAddr: "A:0",
secAddrs: []string{"C:0"},
},
},
}
for _, tc := range cases {
stub := newSentinelStub(tc.start.primAddr, secAddrs(tc.start), []string{"127.0.0.1:26379"})
sc, err := NewSentinel(
"stub", stub.sentAddrs,
SentinelConnFunc(stub.newConn), SentinelPoolFunc(poolFn),
)
require.Nil(t, err)
// call Client on all secAddrs so Clients get created for them, double
// check that the clients were indeed created in clients map
for _, addr := range tc.start.secAddrs {
client, err := sc.Client(addr)
assert.Nil(t, err)
assert.NotNil(t, client)
assert.Equal(t, client, sc.clients[addr])
}
// collect all non-nil clients to check against return from Clients
// later
prevClients := map[string]Client{}
for addr, client := range sc.clients {
if client != nil {
prevClients[addr] = client
}
}
// collect all clients which are expected to be closed, so we can check
// their closed fields later
willClose := map[string]*stubSentinelPool{}
for _, addr := range tc.closed {
client := sc.clients[addr]
require.NotNil(t, client)
willClose[addr] = client.(*stubSentinelPool)
}
assertAddrs(tc.start, sc)
stub.switchPrimary(tc.end.primAddr, secAddrs(tc.end)...)
assert.Equal(t, "switch-master completed", <-sc.testEventCh)
assertAddrs(tc.end, sc)
for addr, ssp := range willClose {
assert.True(t, ssp.closed, "addr:%q not closed", addr)
}
// check returns from Client. If the addr was in prevClients the Client
// should stay the same from there.
assertClient := func(addr string) {
assert.Contains(t, sc.clients, addr)
client, err := sc.Client(addr)
assert.Nil(t, err)
if prevClient := prevClients[addr]; prevClient != nil {
assert.Equal(t, prevClient, client)
}
}
assertClient(tc.end.primAddr)
for _, secAddr := range tc.end.secAddrs {
assertClient(secAddr)
}
// test that, for nilSecAddrs, they are in the clients map but don't
// have a Client value. Then test that if Client is called with that
// addr a new Client is created in the clients map, and that same client
// is returned the next time Client is called.
for _, nilSecAddr := range tc.end.nilSecAddrs {
assert.Contains(t, sc.clients, nilSecAddr)
assert.Nil(t, sc.clients[nilSecAddr])
client, err := sc.Client(nilSecAddr)
assert.Nil(t, err)
assert.NotNil(t, client)
assert.Equal(t, client, sc.clients[nilSecAddr])
client2, err := sc.Client(nilSecAddr)
assert.Nil(t, err)
assert.Equal(t, client, client2)
}
}
}
func TestSentinelSecondaryRead(t *T) {
stub := newSentinelStub(
"127.0.0.1:9736", // primAddr
[]string{"127.0.0.2:9736", "127.0.0.3:9736"}, // secAddrs
[]string{"127.0.0.1:29736", "127.0.0.2:9736", "127.0.0.3:9736"}, // sentAddrs
)
// our fake poolFn will always _actually_ connect to 127.0.0.1, we just
// don't tell anyone
poolFn := func(network, addr string) (Client, error) {
return Stub(network, addr, func(args []string) interface{} {
return addr
}), nil
}
scc, err := NewSentinel(
"stub",
stub.sentAddrs,
SentinelConnFunc(stub.newConn),
SentinelPoolFunc(poolFn),
)
require.Nil(t, err)
runTest := func(n int) {
primAddr, secAddrs := scc.Addrs()
for i := 0; i < n; i++ {
var addr string
require.NoError(t, scc.DoSecondary(Cmd(&addr, "GIMME", "YOUR", "ADDRESS")))
assert.NotEqualf(t, scc.primAddr, addr, "command was sent to master at %s", primAddr)
assert.Containsf(t, secAddrs, addr, "returned address if not a secondary. expected one of %v, got %v", secAddrs, addr)
}
}
runTest(32)
stub.switchPrimary("127.0.0.2:9736", "127.0.0.3:9736")
assert.Equal(t, "switch-master completed", <-scc.testEventCh)
runTest(32)
}