forked from CodisLabs/codis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentinel.go
790 lines (725 loc) · 18.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
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
// Copyright 2016 CodisLabs. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package redis
import (
"fmt"
"net"
"strconv"
"strings"
"time"
"golang.org/x/net/context"
"github.com/CodisLabs/codis/pkg/utils/errors"
"github.com/CodisLabs/codis/pkg/utils/sync2/atomic2"
redigo "github.com/garyburd/redigo/redis"
)
type Sentinel struct {
context.Context
Cancel context.CancelFunc
Product, Auth string
LogFunc func(format string, args ...interface{})
ErrFunc func(err error, format string, args ...interface{})
}
func NewSentinel(product, auth string) *Sentinel {
s := &Sentinel{Product: product, Auth: auth}
s.Context, s.Cancel = context.WithCancel(context.Background())
return s
}
func (s *Sentinel) IsCanceled() bool {
select {
case <-s.Context.Done():
return true
default:
return false
}
}
func (s *Sentinel) NodeName(gid int) string {
return fmt.Sprintf("%s-%d", s.Product, gid)
}
func (s *Sentinel) isSameProduct(name string) (gid int, _ bool) {
if !strings.HasPrefix(name, s.Product) {
return 0, false
}
var suffix = name[len(s.Product):]
if len(suffix) <= 1 || suffix[0] != '-' {
return 0, false
}
n, err := strconv.Atoi(suffix[1:])
if err != nil {
return 0, false
}
return n, true
}
func (s *Sentinel) printf(format string, args ...interface{}) {
if s.LogFunc != nil {
s.LogFunc(format, args...)
}
}
func (s *Sentinel) errorf(err error, format string, args ...interface{}) {
if s.ErrFunc != nil {
s.ErrFunc(err, format, args...)
}
}
func (s *Sentinel) do(sentinel string, timeout time.Duration,
fn func(client *Client) error) error {
c, err := NewClientNoAuth(sentinel, timeout)
if err != nil {
return err
}
defer c.Close()
return fn(c)
}
func (s *Sentinel) dispatch(ctx context.Context, sentinel string, timeout time.Duration,
fn func(client *Client) error) error {
c, err := NewClientNoAuth(sentinel, timeout)
if err != nil {
return err
}
defer c.Close()
var exit = make(chan error, 1)
go func() {
exit <- fn(c)
}()
select {
case <-ctx.Done():
return errors.Trace(ctx.Err())
case err := <-exit:
return err
}
}
func (s *Sentinel) subscribeCommand(client *Client, sentinel string,
onSubscribed func()) error {
defer func() {
client.Close()
}()
var channels = []interface{}{"+switch-master"}
go func() {
client.Send("SUBSCRIBE", channels...)
client.Flush()
}()
for _, sub := range channels {
values, err := redigo.Values(client.Receive())
if err != nil {
return errors.Trace(err)
} else if len(values) != 3 {
return errors.Errorf("invalid response = %v", values)
}
s, err := redigo.Strings(values[:2], nil)
if err != nil || s[0] != "subscribe" || s[1] != sub.(string) {
return errors.Errorf("invalid response = %v", values)
}
}
onSubscribed()
for {
values, err := redigo.Values(client.Receive())
if err != nil {
return errors.Trace(err)
} else if len(values) < 2 {
return errors.Errorf("invalid response = %v", values)
}
message, err := redigo.Strings(values, nil)
if err != nil || message[0] != "message" {
return errors.Errorf("invalid response = %v", values)
}
s.printf("sentinel-[%s] subscribe event %v", sentinel, message)
switch message[1] {
case "+switch-master":
if len(message) != 3 {
return errors.Errorf("invalid response = %v", values)
}
var params = strings.SplitN(message[2], " ", 2)
if len(params) != 2 {
return errors.Errorf("invalid response = %v", values)
}
_, yes := s.isSameProduct(params[0])
if yes {
return nil
}
}
}
}
func (s *Sentinel) subscribeDispatch(ctx context.Context, sentinel string, timeout time.Duration,
onSubscribed func()) (bool, error) {
var err = s.dispatch(ctx, sentinel, timeout, func(c *Client) error {
return s.subscribeCommand(c, sentinel, onSubscribed)
})
if err != nil {
switch errors.Cause(err) {
case context.Canceled, context.DeadlineExceeded:
return false, nil
default:
return false, err
}
}
return true, nil
}
func (s *Sentinel) Subscribe(sentinels []string, timeout time.Duration, onMajoritySubscribed func()) bool {
cntx, cancel := context.WithTimeout(s.Context, timeout)
defer cancel()
timeout += time.Second * 5
results := make(chan bool, len(sentinels))
var majority = 1 + len(sentinels)/2
var subscribed atomic2.Int64
for i := range sentinels {
go func(sentinel string) {
notified, err := s.subscribeDispatch(cntx, sentinel, timeout, func() {
if subscribed.Incr() == int64(majority) {
onMajoritySubscribed()
}
})
if err != nil {
s.errorf(err, "sentinel-[%s] subscribe failed", sentinel)
}
results <- notified
}(sentinels[i])
}
for alive := len(sentinels); ; alive-- {
if alive < majority {
if cntx.Err() == nil {
s.printf("sentinel subscribe lost majority (%d/%d)", alive, len(sentinels))
}
return false
}
select {
case <-cntx.Done():
if cntx.Err() != context.DeadlineExceeded {
s.printf("sentinel subscribe canceled (%v)", cntx.Err())
}
return false
case notified := <-results:
if notified {
s.printf("sentinel subscribe notified +switch-master")
return true
}
}
}
}
func (s *Sentinel) existsCommand(client *Client, names []string) (map[string]bool, error) {
defer func() {
if !client.isRecyclable() {
client.Close()
}
}()
go func() {
for _, name := range names {
client.Send("SENTINEL", "get-master-addr-by-name", name)
}
if len(names) != 0 {
client.Flush()
}
}()
exists := make(map[string]bool, len(names))
for _, name := range names {
r, err := client.Receive()
if err != nil {
return nil, errors.Trace(err)
}
exists[name] = (r != nil)
}
return exists, nil
}
func (s *Sentinel) slavesCommand(client *Client, names []string) (map[string][]map[string]string, error) {
defer func() {
if !client.isRecyclable() {
client.Close()
}
}()
exists, err := s.existsCommand(client, names)
if err != nil {
return nil, err
}
go func() {
var pending int
for _, name := range names {
if !exists[name] {
continue
}
pending++
client.Send("SENTINEL", "slaves", name)
}
if pending != 0 {
client.Flush()
}
}()
results := make(map[string][]map[string]string, len(names))
for _, name := range names {
if !exists[name] {
continue
}
values, err := redigo.Values(client.Receive())
if err != nil {
return nil, errors.Trace(err)
}
var slaves []map[string]string
for i := range values {
m, err := redigo.StringMap(values[i], nil)
if err != nil {
return nil, errors.Trace(err)
}
slaves = append(slaves, m)
}
results[name] = slaves
}
return results, nil
}
func (s *Sentinel) mastersCommand(client *Client) (map[int]map[string]string, error) {
defer func() {
if !client.isRecyclable() {
client.Close()
}
}()
values, err := redigo.Values(client.Do("SENTINEL", "masters"))
if err != nil {
return nil, errors.Trace(err)
}
var masters = make(map[int]map[string]string)
for i := range values {
p, err := redigo.StringMap(values[i], nil)
if err != nil {
return nil, errors.Trace(err)
}
gid, yes := s.isSameProduct(p["name"])
if yes {
masters[gid] = p
}
}
return masters, nil
}
func (s *Sentinel) mastersDispatch(ctx context.Context, sentinel string, timeout time.Duration) (map[int]*SentinelMaster, error) {
var masters = make(map[int]*SentinelMaster)
var err = s.dispatch(ctx, sentinel, timeout, func(c *Client) error {
p, err := s.mastersCommand(c)
if err != nil {
return err
}
for gid, master := range p {
epoch, err := strconv.ParseInt(master["config-epoch"], 10, 64)
if err != nil {
s.printf("sentinel-[%s] masters parse %s failed, config-epoch = '%s', %s",
sentinel, master["name"], master["config-epoch"], err)
continue
}
var ip, port = master["ip"], master["port"]
if ip == "" || port == "" {
s.printf("sentinel-[%s] masters parse %s failed, ip:port = '%s:%s'",
sentinel, master["name"], ip, port)
continue
}
masters[gid] = &SentinelMaster{
Addr: net.JoinHostPort(ip, port),
Info: master, Epoch: epoch,
}
}
return nil
})
if err != nil {
switch errors.Cause(err) {
case context.Canceled:
return nil, nil
default:
return nil, err
}
}
return masters, nil
}
type SentinelMaster struct {
Addr string
Info map[string]string
Epoch int64
}
func (s *Sentinel) Masters(sentinels []string, timeout time.Duration) (map[int]string, error) {
cntx, cancel := context.WithTimeout(s.Context, timeout)
defer cancel()
timeout += time.Second * 5
results := make(chan map[int]*SentinelMaster, len(sentinels))
var majority = 1 + len(sentinels)/2
for i := range sentinels {
go func(sentinel string) {
masters, err := s.mastersDispatch(cntx, sentinel, timeout)
if err != nil {
s.errorf(err, "sentinel-[%s] masters failed", sentinel)
}
results <- masters
}(sentinels[i])
}
masters := make(map[int]string)
current := make(map[int]*SentinelMaster)
var voted int
for alive := len(sentinels); ; alive-- {
if alive == 0 {
switch {
case cntx.Err() != context.DeadlineExceeded && cntx.Err() != nil:
s.printf("sentinel masters canceled (%v)", cntx.Err())
return nil, errors.Trace(cntx.Err())
case voted != len(sentinels):
s.printf("sentinel masters voted = (%d/%d) masters = %d (%v)", voted, len(sentinels), len(masters), cntx.Err())
}
if voted < majority {
return nil, errors.Errorf("lost majority (%d/%d)", voted, len(sentinels))
}
return masters, nil
}
select {
case <-cntx.Done():
switch {
case cntx.Err() != context.DeadlineExceeded:
s.printf("sentinel masters canceled (%v)", cntx.Err())
return nil, errors.Trace(cntx.Err())
default:
s.printf("sentinel masters voted = (%d/%d) masters = %d (%v)", voted, len(sentinels), len(masters), cntx.Err())
}
if voted < majority {
return nil, errors.Errorf("lost majority (%d/%d)", voted, len(sentinels))
}
return masters, nil
case m := <-results:
if m == nil {
continue
}
for gid, master := range m {
if current[gid] == nil || current[gid].Epoch < master.Epoch {
current[gid] = master
masters[gid] = master.Addr
}
}
voted += 1
}
}
}
type MonitorConfig struct {
Quorum int
ParallelSyncs int
DownAfter time.Duration
FailoverTimeout time.Duration
NotificationScript string
ClientReconfigScript string
}
func (s *Sentinel) monitorGroupsCommand(client *Client, sentniel string, config *MonitorConfig, groups map[int]*net.TCPAddr) error {
defer func() {
if !client.isRecyclable() {
client.Close()
}
}()
var names []string
for gid := range groups {
names = append(names, s.NodeName(gid))
}
if err := s.removeCommand(client, names); err != nil {
return err
}
go func() {
for gid, tcpAddr := range groups {
var ip, port = tcpAddr.IP.String(), tcpAddr.Port
client.Send("SENTINEL", "monitor", s.NodeName(gid), ip, port, config.Quorum)
}
if len(groups) != 0 {
client.Flush()
}
}()
for range groups {
_, err := client.Receive()
if err != nil {
return errors.Trace(err)
}
}
go func() {
for gid := range groups {
var args = []interface{}{"set", s.NodeName(gid)}
if config.ParallelSyncs != 0 {
args = append(args, "parallel-syncs", config.ParallelSyncs)
}
if config.DownAfter != 0 {
args = append(args, "down-after-milliseconds", int(config.DownAfter/time.Millisecond))
}
if config.FailoverTimeout != 0 {
args = append(args, "failover-timeout", int(config.FailoverTimeout/time.Millisecond))
}
if s.Auth != "" {
args = append(args, "auth-pass", s.Auth)
}
if config.NotificationScript != "" {
args = append(args, "notification-script", config.NotificationScript)
}
if config.ClientReconfigScript != "" {
args = append(args, "client-reconfig-script", config.ClientReconfigScript)
}
client.Send("SENTINEL", args...)
}
if len(groups) != 0 {
client.Flush()
}
}()
for range groups {
_, err := client.Receive()
if err != nil {
return errors.Trace(err)
}
}
return nil
}
func (s *Sentinel) monitorGroupsDispatch(ctx context.Context, sentinel string, timeout time.Duration,
config *MonitorConfig, groups map[int]*net.TCPAddr) error {
var err = s.dispatch(ctx, sentinel, timeout, func(c *Client) error {
return s.monitorGroupsCommand(c, sentinel, config, groups)
})
if err != nil {
switch errors.Cause(err) {
case context.Canceled:
return nil
default:
return err
}
}
return nil
}
func (s *Sentinel) MonitorGroups(sentinels []string, timeout time.Duration, config *MonitorConfig, groups map[int]string) error {
cntx, cancel := context.WithTimeout(s.Context, timeout)
defer cancel()
resolve := make(map[int]*net.TCPAddr)
var exit = make(chan error, 1)
go func() (err error) {
defer func() {
exit <- err
}()
for gid, addr := range groups {
if err := cntx.Err(); err != nil {
return errors.Trace(err)
}
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
s.printf("sentinel monitor resolve tcp address of %s failed, %s", addr, err)
return errors.Trace(err)
}
resolve[gid] = tcpAddr
}
return nil
}()
select {
case <-cntx.Done():
if cntx.Err() != context.DeadlineExceeded {
s.printf("sentinel monitor canceled (%v)", cntx.Err())
} else {
s.printf("sentinel montior resolve tcp address (%v)", cntx.Err())
}
return errors.Trace(cntx.Err())
case err := <-exit:
if err != nil {
return err
}
}
timeout += time.Second * 5
results := make(chan error, len(sentinels))
for i := range sentinels {
go func(sentinel string) {
err := s.monitorGroupsDispatch(cntx, sentinel, timeout, config, resolve)
if err != nil {
s.errorf(err, "sentinel-[%s] monitor failed", sentinel)
}
results <- err
}(sentinels[i])
}
var last error
for range sentinels {
select {
case <-cntx.Done():
if last != nil {
return last
}
return errors.Trace(cntx.Err())
case err := <-results:
if err != nil {
last = err
}
}
}
return last
}
func (s *Sentinel) removeCommand(client *Client, names []string) error {
defer func() {
if !client.isRecyclable() {
client.Close()
}
}()
exists, err := s.existsCommand(client, names)
if err != nil {
return err
}
go func() {
var pending int
for _, name := range names {
if !exists[name] {
continue
}
pending++
client.Send("SENTINEL", "remove", name)
}
if pending != 0 {
client.Flush()
}
}()
for _, name := range names {
if !exists[name] {
continue
}
_, err := client.Receive()
if err != nil {
return errors.Trace(err)
}
}
return nil
}
func (s *Sentinel) removeGroupsDispatch(ctx context.Context, sentinel string, timeout time.Duration,
groups map[int]bool) error {
var names []string
for gid := range groups {
names = append(names, s.NodeName(gid))
}
var err = s.dispatch(ctx, sentinel, timeout, func(c *Client) error {
return s.removeCommand(c, names)
})
if err != nil {
switch errors.Cause(err) {
case context.Canceled:
return nil
default:
return err
}
}
return nil
}
func (s *Sentinel) RemoveGroups(sentinels []string, timeout time.Duration, groups map[int]bool) error {
cntx, cancel := context.WithTimeout(s.Context, timeout)
defer cancel()
timeout += time.Second * 5
results := make(chan error, len(sentinels))
for i := range sentinels {
go func(sentinel string) {
err := s.removeGroupsDispatch(cntx, sentinel, timeout, groups)
if err != nil {
s.errorf(err, "sentinel-[%s] remove failed", sentinel)
}
results <- err
}(sentinels[i])
}
var last error
for range sentinels {
select {
case <-cntx.Done():
if last != nil {
return last
}
return errors.Trace(cntx.Err())
case err := <-results:
if err != nil {
last = err
}
}
}
return last
}
func (s *Sentinel) removeGroupsAllDispatch(ctx context.Context, sentinel string, timeout time.Duration) error {
var err = s.dispatch(ctx, sentinel, timeout, func(c *Client) error {
masters, err := s.mastersCommand(c)
if err != nil {
return err
}
var names []string
for gid := range masters {
names = append(names, s.NodeName(gid))
}
return s.removeCommand(c, names)
})
if err != nil {
switch errors.Cause(err) {
case context.Canceled:
return nil
default:
return err
}
}
return nil
}
func (s *Sentinel) RemoveGroupsAll(sentinels []string, timeout time.Duration) error {
cntx, cancel := context.WithTimeout(s.Context, timeout)
defer cancel()
timeout += time.Second * 5
results := make(chan error, len(sentinels))
for i := range sentinels {
go func(sentinel string) {
err := s.removeGroupsAllDispatch(cntx, sentinel, timeout)
if err != nil {
s.errorf(err, "sentinel-[%s] remove failed", sentinel)
}
results <- err
}(sentinels[i])
}
var last error
for range sentinels {
select {
case <-cntx.Done():
if last != nil {
return last
}
return errors.Trace(cntx.Err())
case err := <-results:
if err != nil {
last = err
}
}
}
return last
}
type SentinelGroup struct {
Master map[string]string `json:"master"`
Slaves []map[string]string `json:"slaves,omitempty"`
}
func (s *Sentinel) MastersAndSlavesClient(client *Client) (map[string]*SentinelGroup, error) {
defer func() {
if !client.isRecyclable() {
client.Close()
}
}()
masters, err := s.mastersCommand(client)
if err != nil {
return nil, err
}
var names []string
for gid := range masters {
names = append(names, s.NodeName(gid))
}
slaves, err := s.slavesCommand(client, names)
if err != nil {
return nil, err
}
results := make(map[string]*SentinelGroup, len(masters))
for gid, master := range masters {
var name = s.NodeName(gid)
results[name] = &SentinelGroup{
Master: master, Slaves: slaves[name],
}
}
return results, nil
}
func (s *Sentinel) MastersAndSlaves(sentinel string, timeout time.Duration) (map[string]*SentinelGroup, error) {
var results map[string]*SentinelGroup
var err = s.do(sentinel, timeout, func(c *Client) error {
m, err := s.MastersAndSlavesClient(c)
if err != nil {
return err
}
results = m
return nil
})
if err != nil {
return nil, err
}
return results, nil
}
func (s *Sentinel) FlushConfig(sentinel string, timeout time.Duration) error {
return s.do(sentinel, timeout, func(c *Client) error {
_, err := c.Do("SENTINEL", "flushconfig")
if err != nil {
return err
}
return nil
})
}