-
Notifications
You must be signed in to change notification settings - Fork 165
/
sentinel.go
500 lines (449 loc) · 12.1 KB
/
sentinel.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
package rueidis
import (
"container/list"
"context"
"errors"
"fmt"
"io"
"net"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/redis/rueidis/internal/cmds"
"github.com/redis/rueidis/internal/util"
)
func newSentinelClient(opt *ClientOption, connFn connFn, retryer retryHandler) (client *sentinelClient, err error) {
client = &sentinelClient{
cmd: cmds.NewBuilder(cmds.NoSlot),
mOpt: opt,
sOpt: newSentinelOpt(opt),
connFn: connFn,
sentinels: list.New(),
retry: !opt.DisableRetry,
retryHandler: retryer,
replica: opt.ReplicaOnly,
}
for _, sentinel := range opt.InitAddress {
client.sentinels.PushBack(sentinel)
}
if err = client.refresh(); err != nil {
client.Close()
return nil, err
}
return client, nil
}
type sentinelClient struct {
mConn atomic.Value
mAddr string
sAddr string
connFn connFn
mOpt *ClientOption
sOpt *ClientOption
sConn conn
sentinels *list.List
sc call
retryHandler retryHandler
cmd Builder
mu sync.Mutex
stop uint32
retry bool
replica bool
}
func (c *sentinelClient) B() Builder {
return c.cmd
}
func (c *sentinelClient) Do(ctx context.Context, cmd Completed) (resp RedisResult) {
attempts := 1
retry:
resp = c.mConn.Load().(conn).Do(ctx, cmd)
if c.retry && cmd.IsReadOnly() && c.isRetryable(resp.Error(), ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(
ctx, attempts, cmd, resp.Error(),
)
if shouldRetry {
attempts++
goto retry
}
}
if resp.NonRedisError() == nil { // not recycle cmds if error, since cmds may be used later in pipe. consider recycle them by pipe
cmds.PutCompleted(cmd)
}
return resp
}
func (c *sentinelClient) DoMulti(ctx context.Context, multi ...Completed) []RedisResult {
if len(multi) == 0 {
return nil
}
attempts := 1
retry:
resps := c.mConn.Load().(conn).DoMulti(ctx, multi...)
if c.retry && allReadOnly(multi) {
for i, resp := range resps.s {
if c.isRetryable(resp.Error(), ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(
ctx, attempts, multi[i], resp.Error(),
)
if shouldRetry {
resultsp.Put(resps)
attempts++
goto retry
}
}
}
}
for i, cmd := range multi {
if resps.s[i].NonRedisError() == nil {
cmds.PutCompleted(cmd)
}
}
return resps.s
}
func (c *sentinelClient) DoCache(ctx context.Context, cmd Cacheable, ttl time.Duration) (resp RedisResult) {
attempts := 1
retry:
resp = c.mConn.Load().(conn).DoCache(ctx, cmd, ttl)
if c.retry && c.isRetryable(resp.Error(), ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(ctx, attempts, Completed(cmd), resp.Error())
if shouldRetry {
attempts++
goto retry
}
}
if err := resp.NonRedisError(); err == nil || err == ErrDoCacheAborted {
cmds.PutCacheable(cmd)
}
return resp
}
func (c *sentinelClient) DoMultiCache(ctx context.Context, multi ...CacheableTTL) []RedisResult {
if len(multi) == 0 {
return nil
}
attempts := 1
retry:
resps := c.mConn.Load().(conn).DoMultiCache(ctx, multi...)
if c.retry {
for i, resp := range resps.s {
if c.isRetryable(resp.Error(), ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(
ctx, attempts, Completed(multi[i].Cmd), resp.Error(),
)
if shouldRetry {
resultsp.Put(resps)
attempts++
goto retry
}
}
}
}
for i, cmd := range multi {
if err := resps.s[i].NonRedisError(); err == nil || err == ErrDoCacheAborted {
cmds.PutCacheable(cmd.Cmd)
}
}
return resps.s
}
func (c *sentinelClient) Receive(ctx context.Context, subscribe Completed, fn func(msg PubSubMessage)) (err error) {
attempts := 1
retry:
err = c.mConn.Load().(conn).Receive(ctx, subscribe, fn)
if c.retry {
if _, ok := err.(*RedisError); !ok && c.isRetryable(err, ctx) {
shouldRetry := c.retryHandler.WaitOrSkipRetry(
ctx, attempts, subscribe, err,
)
if shouldRetry {
attempts++
goto retry
}
}
}
if err == nil {
cmds.PutCompleted(subscribe)
}
return err
}
func (c *sentinelClient) DoStream(ctx context.Context, cmd Completed) RedisResultStream {
resp := c.mConn.Load().(conn).DoStream(ctx, cmd)
cmds.PutCompleted(cmd)
return resp
}
func (c *sentinelClient) DoMultiStream(ctx context.Context, multi ...Completed) MultiRedisResultStream {
if len(multi) == 0 {
return RedisResultStream{e: io.EOF}
}
s := c.mConn.Load().(conn).DoMultiStream(ctx, multi...)
for _, cmd := range multi {
cmds.PutCompleted(cmd)
}
return s
}
func (c *sentinelClient) Dedicated(fn func(DedicatedClient) error) (err error) {
master := c.mConn.Load().(conn)
wire := master.Acquire()
dsc := &dedicatedSingleClient{cmd: c.cmd, conn: master, wire: wire, retry: c.retry, retryHandler: c.retryHandler}
err = fn(dsc)
dsc.release()
return err
}
func (c *sentinelClient) Dedicate() (DedicatedClient, func()) {
master := c.mConn.Load().(conn)
wire := master.Acquire()
dsc := &dedicatedSingleClient{cmd: c.cmd, conn: master, wire: wire, retry: c.retry, retryHandler: c.retryHandler}
return dsc, dsc.release
}
func (c *sentinelClient) Nodes() map[string]Client {
conn := c.mConn.Load().(conn)
disableCache := c.mOpt != nil && c.mOpt.DisableCache
return map[string]Client{conn.Addr(): newSingleClientWithConn(conn, c.cmd, c.retry, disableCache, c.retryHandler)}
}
func (c *sentinelClient) Close() {
atomic.StoreUint32(&c.stop, 1)
c.mu.Lock()
if c.sConn != nil {
c.sConn.Close()
}
if master := c.mConn.Load(); master != nil {
master.(conn).Close()
}
c.mu.Unlock()
}
func (c *sentinelClient) isRetryable(err error, ctx context.Context) (should bool) {
if err == nil || err == Nil || err == ErrDoCacheAborted || atomic.LoadUint32(&c.stop) != 0 || ctx.Err() != nil {
return false
}
if err, ok := err.(*RedisError); ok {
return err.IsLoading()
}
return true
}
func (c *sentinelClient) addSentinel(addr string) {
c.mu.Lock()
c._addSentinel(addr)
c.mu.Unlock()
}
func (c *sentinelClient) _addSentinel(addr string) {
for e := c.sentinels.Front(); e != nil; e = e.Next() {
if e.Value.(string) == addr {
return
}
}
c.sentinels.PushFront(addr)
}
func (c *sentinelClient) switchTargetRetry(addr string) {
c.mu.Lock()
err := c._switchTarget(addr)
c.mu.Unlock()
if err != nil {
go c.refreshRetry()
}
}
func (c *sentinelClient) _switchTarget(addr string) (err error) {
var target conn
if atomic.LoadUint32(&c.stop) == 1 {
return nil
}
if c.mAddr == addr {
target = c.mConn.Load().(conn)
if target.Error() != nil {
target = nil
}
}
if target == nil {
target = c.connFn(addr, c.mOpt)
if err = target.Dial(); err != nil {
return err
}
}
resp, err := target.Do(context.Background(), cmds.RoleCmd).ToArray()
if err != nil {
target.Close()
return err
}
if c.replica && resp[0].string != "slave" {
target.Close()
return errNotSlave
} else if !c.replica && resp[0].string != "master" {
target.Close()
return errNotMaster
}
c.mAddr = addr
if old := c.mConn.Swap(target); old != nil {
if prev := old.(conn); prev != target {
prev.Close()
}
}
return nil
}
func (c *sentinelClient) refreshRetry() {
retry:
if err := c.refresh(); err != nil {
goto retry
}
}
func (c *sentinelClient) refresh() (err error) {
return c.sc.Do(context.Background(), c._refresh)
}
func (c *sentinelClient) _refresh() (err error) {
var target string
var sentinels []string
c.mu.Lock()
head := c.sentinels.Front()
for e := head; e != nil; {
if atomic.LoadUint32(&c.stop) == 1 {
c.mu.Unlock()
return nil
}
addr := e.Value.(string)
if c.sAddr != addr || c.sConn == nil || c.sConn.Error() != nil {
if c.sConn != nil {
c.sConn.Close()
}
c.sAddr = addr
c.sConn = c.connFn(addr, c.sOpt)
err = c.sConn.Dial()
}
if err == nil {
// listWatch returns server address with sentinels.
// check if target is master or replica
if target, sentinels, err = c.listWatch(c.sConn); err == nil {
for _, sentinel := range sentinels {
c._addSentinel(sentinel)
}
// _switchTarget will switch the connection for master OR replica
if err = c._switchTarget(target); err == nil {
break
}
}
c.sConn.Close()
}
c.sentinels.MoveToBack(e)
if e = c.sentinels.Front(); e == head {
break
}
}
c.mu.Unlock()
if err == nil {
if master := c.mConn.Load(); master == nil {
err = ErrNoAddr
} else {
err = master.(conn).Error()
}
}
return err
}
// listWatch will use sentinel to list current master|replica address along with sentinels address
func (c *sentinelClient) listWatch(cc conn) (target string, sentinels []string, err error) {
ctx := context.Background()
sentinelsCMD := c.cmd.SentinelSentinels().Master(c.mOpt.Sentinel.MasterSet).Build()
getMasterCMD := c.cmd.SentinelGetMasterAddrByName().Master(c.mOpt.Sentinel.MasterSet).Build()
replicasCMD := c.cmd.SentinelReplicas().Master(c.mOpt.Sentinel.MasterSet).Build()
defer func() {
if err == nil { // not recycle cmds if error, since cmds may be used later in pipe. consider recycle them by pipe
cmds.PutCompleted(sentinelsCMD)
cmds.PutCompleted(getMasterCMD)
cmds.PutCompleted(replicasCMD)
}
}()
// unsubscribe in case there is any previous subscription
cc.Do(ctx, cmds.SentinelUnSubscribe)
go func(cc conn) {
if err := cc.Receive(ctx, cmds.SentinelSubscribe, func(event PubSubMessage) {
switch event.Channel {
case "+sentinel":
m := strings.SplitN(event.Message, " ", 4)
c.addSentinel(net.JoinHostPort(m[2], m[3]))
case "+switch-master":
m := strings.SplitN(event.Message, " ", 5)
if m[0] == c.sOpt.Sentinel.MasterSet {
c.switchTargetRetry(net.JoinHostPort(m[3], m[4]))
}
case "+reboot":
m := strings.SplitN(event.Message, " ", 7)
if m[0] == "master" && m[1] == c.sOpt.Sentinel.MasterSet {
c.switchTargetRetry(net.JoinHostPort(m[2], m[3]))
} else if c.replica && m[0] == "slave" && m[5] == c.sOpt.Sentinel.MasterSet {
c.refreshRetry()
}
// note that in case of failover, every slave in the setup
// will send +slave event individually.
case "+slave", "+sdown", "-sdown":
m := strings.SplitN(event.Message, " ", 7)
if c.replica && m[0] == "slave" && m[5] == c.sOpt.Sentinel.MasterSet {
// call refresh to randomly choose a new slave
c.refreshRetry()
}
}
}); err != nil && atomic.LoadUint32(&c.stop) == 0 {
c.refreshRetry()
}
}(cc)
var commands Commands
if c.replica {
commands = Commands{sentinelsCMD, replicasCMD}
} else {
commands = Commands{sentinelsCMD, getMasterCMD}
}
resp := cc.DoMulti(ctx, commands...)
defer resultsp.Put(resp)
others, err := resp.s[0].ToArray()
if err != nil {
return "", nil, err
}
for _, other := range others {
if m, err := other.AsStrMap(); err == nil {
sentinels = append(sentinels, net.JoinHostPort(m["ip"], m["port"]))
}
}
// we return random slave address instead of master
if c.replica {
addr, err := pickReplica(resp.s)
if err != nil {
return "", nil, err
}
return addr, sentinels, nil
}
// otherwise send master as address
m, err := resp.s[1].AsStrSlice()
if err != nil {
return "", nil, err
}
return net.JoinHostPort(m[0], m[1]), sentinels, nil
}
func pickReplica(resp []RedisResult) (string, error) {
replicas, err := resp[1].ToArray()
if err != nil {
return "", err
}
eligible := make([]map[string]string, 0, len(replicas))
// eliminate replicas with s_down condition
for i := range replicas {
replica, err := replicas[i].AsStrMap()
if err != nil {
continue
}
if _, ok := replica["s-down-time"]; !ok {
eligible = append(eligible, replica)
}
}
if len(eligible) == 0 {
return "", fmt.Errorf("not enough ready replicas")
}
// choose a replica randomly
m := eligible[util.FastRand(len(eligible))]
return net.JoinHostPort(m["ip"], m["port"]), nil
}
func newSentinelOpt(opt *ClientOption) *ClientOption {
o := *opt
o.Username = o.Sentinel.Username
o.Password = o.Sentinel.Password
o.ClientName = o.Sentinel.ClientName
o.Dialer = o.Sentinel.Dialer
o.TLSConfig = o.Sentinel.TLSConfig
o.SelectDB = 0 // https://github.com/redis/rueidis/issues/138
return &o
}
var (
errNotMaster = errors.New("the redis role is not master")
errNotSlave = errors.New("the redis role is not slave")
)