2019-04-08 11:05:15 +02:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2020-09-27 23:09:46 +02:00
|
|
|
"context"
|
2019-04-08 11:05:15 +02:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-09-27 23:09:46 +02:00
|
|
|
"github.com/go-redis/redis/v7/internal"
|
|
|
|
"github.com/go-redis/redis/v7/internal/pool"
|
2019-04-08 11:05:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// FailoverOptions are used to configure a failover client and should
|
|
|
|
// be passed to NewFailoverClient.
|
|
|
|
type FailoverOptions struct {
|
|
|
|
// The master name.
|
|
|
|
MasterName string
|
|
|
|
// A seed list of host:port addresses of sentinel nodes.
|
2020-09-27 23:09:46 +02:00
|
|
|
SentinelAddrs []string
|
|
|
|
SentinelUsername string
|
|
|
|
SentinelPassword string
|
2019-04-08 11:05:15 +02:00
|
|
|
|
|
|
|
// Following options are copied from Options struct.
|
|
|
|
|
2020-09-27 23:09:46 +02:00
|
|
|
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
|
2019-04-08 11:05:15 +02:00
|
|
|
OnConnect func(*Conn) error
|
|
|
|
|
2020-09-27 23:09:46 +02:00
|
|
|
Username string
|
2019-04-08 11:05:15 +02:00
|
|
|
Password string
|
|
|
|
DB int
|
|
|
|
|
|
|
|
MaxRetries int
|
|
|
|
MinRetryBackoff time.Duration
|
|
|
|
MaxRetryBackoff time.Duration
|
|
|
|
|
|
|
|
DialTimeout time.Duration
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
WriteTimeout time.Duration
|
|
|
|
|
|
|
|
PoolSize int
|
|
|
|
MinIdleConns int
|
|
|
|
MaxConnAge time.Duration
|
|
|
|
PoolTimeout time.Duration
|
|
|
|
IdleTimeout time.Duration
|
|
|
|
IdleCheckFrequency time.Duration
|
|
|
|
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opt *FailoverOptions) options() *Options {
|
|
|
|
return &Options{
|
2020-09-27 23:09:46 +02:00
|
|
|
Addr: "FailoverClient",
|
|
|
|
Dialer: opt.Dialer,
|
2019-04-08 11:05:15 +02:00
|
|
|
OnConnect: opt.OnConnect,
|
|
|
|
|
|
|
|
DB: opt.DB,
|
2020-09-27 23:09:46 +02:00
|
|
|
Username: opt.Username,
|
2019-04-08 11:05:15 +02:00
|
|
|
Password: opt.Password,
|
|
|
|
|
2020-09-27 23:09:46 +02:00
|
|
|
MaxRetries: opt.MaxRetries,
|
|
|
|
MinRetryBackoff: opt.MinRetryBackoff,
|
|
|
|
MaxRetryBackoff: opt.MaxRetryBackoff,
|
2019-04-08 11:05:15 +02:00
|
|
|
|
|
|
|
DialTimeout: opt.DialTimeout,
|
|
|
|
ReadTimeout: opt.ReadTimeout,
|
|
|
|
WriteTimeout: opt.WriteTimeout,
|
|
|
|
|
|
|
|
PoolSize: opt.PoolSize,
|
|
|
|
PoolTimeout: opt.PoolTimeout,
|
|
|
|
IdleTimeout: opt.IdleTimeout,
|
|
|
|
IdleCheckFrequency: opt.IdleCheckFrequency,
|
2020-09-27 23:09:46 +02:00
|
|
|
MinIdleConns: opt.MinIdleConns,
|
|
|
|
MaxConnAge: opt.MaxConnAge,
|
2019-04-08 11:05:15 +02:00
|
|
|
|
|
|
|
TLSConfig: opt.TLSConfig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFailoverClient returns a Redis client that uses Redis Sentinel
|
|
|
|
// for automatic failover. It's safe for concurrent use by multiple
|
|
|
|
// goroutines.
|
|
|
|
func NewFailoverClient(failoverOpt *FailoverOptions) *Client {
|
|
|
|
opt := failoverOpt.options()
|
|
|
|
opt.init()
|
|
|
|
|
|
|
|
failover := &sentinelFailover{
|
|
|
|
masterName: failoverOpt.MasterName,
|
|
|
|
sentinelAddrs: failoverOpt.SentinelAddrs,
|
2020-09-27 23:09:46 +02:00
|
|
|
username: failoverOpt.SentinelUsername,
|
|
|
|
password: failoverOpt.SentinelPassword,
|
2019-04-08 11:05:15 +02:00
|
|
|
|
|
|
|
opt: opt,
|
|
|
|
}
|
|
|
|
|
|
|
|
c := Client{
|
2020-09-27 23:09:46 +02:00
|
|
|
baseClient: newBaseClient(opt, failover.Pool()),
|
|
|
|
ctx: context.Background(),
|
2019-04-08 11:05:15 +02:00
|
|
|
}
|
2020-09-27 23:09:46 +02:00
|
|
|
c.cmdable = c.Process
|
|
|
|
c.onClose = failover.Close
|
2019-04-08 11:05:15 +02:00
|
|
|
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type SentinelClient struct {
|
2020-09-27 23:09:46 +02:00
|
|
|
*baseClient
|
|
|
|
ctx context.Context
|
2019-04-08 11:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSentinelClient(opt *Options) *SentinelClient {
|
|
|
|
opt.init()
|
|
|
|
c := &SentinelClient{
|
2020-09-27 23:09:46 +02:00
|
|
|
baseClient: &baseClient{
|
2019-04-08 11:05:15 +02:00
|
|
|
opt: opt,
|
|
|
|
connPool: newConnPool(opt),
|
|
|
|
},
|
2020-09-27 23:09:46 +02:00
|
|
|
ctx: context.Background(),
|
2019-04-08 11:05:15 +02:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-09-27 23:09:46 +02:00
|
|
|
func (c *SentinelClient) Context() context.Context {
|
|
|
|
return c.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SentinelClient) WithContext(ctx context.Context) *SentinelClient {
|
|
|
|
if ctx == nil {
|
|
|
|
panic("nil context")
|
|
|
|
}
|
|
|
|
clone := *c
|
|
|
|
clone.ctx = ctx
|
|
|
|
return &clone
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SentinelClient) Process(cmd Cmder) error {
|
|
|
|
return c.ProcessContext(c.ctx, cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SentinelClient) ProcessContext(ctx context.Context, cmd Cmder) error {
|
|
|
|
return c.baseClient.process(ctx, cmd)
|
|
|
|
}
|
|
|
|
|
2019-04-08 11:05:15 +02:00
|
|
|
func (c *SentinelClient) pubSub() *PubSub {
|
|
|
|
pubsub := &PubSub{
|
|
|
|
opt: c.opt,
|
|
|
|
|
|
|
|
newConn: func(channels []string) (*pool.Conn, error) {
|
2020-09-27 23:09:46 +02:00
|
|
|
return c.newConn(context.TODO())
|
2019-04-08 11:05:15 +02:00
|
|
|
},
|
|
|
|
closeConn: c.connPool.CloseConn,
|
|
|
|
}
|
|
|
|
pubsub.init()
|
|
|
|
return pubsub
|
|
|
|
}
|
|
|
|
|
2020-09-27 23:09:46 +02:00
|
|
|
// Ping is used to test if a connection is still alive, or to
|
|
|
|
// measure latency.
|
|
|
|
func (c *SentinelClient) Ping() *StringCmd {
|
|
|
|
cmd := NewStringCmd("ping")
|
|
|
|
_ = c.Process(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-04-08 11:05:15 +02:00
|
|
|
// Subscribe subscribes the client to the specified channels.
|
|
|
|
// Channels can be omitted to create empty subscription.
|
|
|
|
func (c *SentinelClient) Subscribe(channels ...string) *PubSub {
|
|
|
|
pubsub := c.pubSub()
|
|
|
|
if len(channels) > 0 {
|
|
|
|
_ = pubsub.Subscribe(channels...)
|
|
|
|
}
|
|
|
|
return pubsub
|
|
|
|
}
|
|
|
|
|
|
|
|
// PSubscribe subscribes the client to the given patterns.
|
|
|
|
// Patterns can be omitted to create empty subscription.
|
|
|
|
func (c *SentinelClient) PSubscribe(channels ...string) *PubSub {
|
|
|
|
pubsub := c.pubSub()
|
|
|
|
if len(channels) > 0 {
|
|
|
|
_ = pubsub.PSubscribe(channels...)
|
|
|
|
}
|
|
|
|
return pubsub
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SentinelClient) GetMasterAddrByName(name string) *StringSliceCmd {
|
|
|
|
cmd := NewStringSliceCmd("sentinel", "get-master-addr-by-name", name)
|
2020-09-27 23:09:46 +02:00
|
|
|
_ = c.Process(cmd)
|
2019-04-08 11:05:15 +02:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SentinelClient) Sentinels(name string) *SliceCmd {
|
|
|
|
cmd := NewSliceCmd("sentinel", "sentinels", name)
|
2020-09-27 23:09:46 +02:00
|
|
|
_ = c.Process(cmd)
|
2019-04-08 11:05:15 +02:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failover forces a failover as if the master was not reachable, and without
|
|
|
|
// asking for agreement to other Sentinels.
|
|
|
|
func (c *SentinelClient) Failover(name string) *StatusCmd {
|
|
|
|
cmd := NewStatusCmd("sentinel", "failover", name)
|
2020-09-27 23:09:46 +02:00
|
|
|
_ = c.Process(cmd)
|
2019-04-08 11:05:15 +02:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets all the masters with matching name. The pattern argument is a
|
|
|
|
// glob-style pattern. The reset process clears any previous state in a master
|
|
|
|
// (including a failover in progress), and removes every slave and sentinel
|
|
|
|
// already discovered and associated with the master.
|
|
|
|
func (c *SentinelClient) Reset(pattern string) *IntCmd {
|
|
|
|
cmd := NewIntCmd("sentinel", "reset", pattern)
|
2020-09-27 23:09:46 +02:00
|
|
|
_ = c.Process(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlushConfig forces Sentinel to rewrite its configuration on disk, including
|
|
|
|
// the current Sentinel state.
|
|
|
|
func (c *SentinelClient) FlushConfig() *StatusCmd {
|
|
|
|
cmd := NewStatusCmd("sentinel", "flushconfig")
|
|
|
|
_ = c.Process(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Master shows the state and info of the specified master.
|
|
|
|
func (c *SentinelClient) Master(name string) *StringStringMapCmd {
|
|
|
|
cmd := NewStringStringMapCmd("sentinel", "master", name)
|
|
|
|
_ = c.Process(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Masters shows a list of monitored masters and their state.
|
|
|
|
func (c *SentinelClient) Masters() *SliceCmd {
|
|
|
|
cmd := NewSliceCmd("sentinel", "masters")
|
|
|
|
_ = c.Process(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slaves shows a list of slaves for the specified master and their state.
|
|
|
|
func (c *SentinelClient) Slaves(name string) *SliceCmd {
|
|
|
|
cmd := NewSliceCmd("sentinel", "slaves", name)
|
|
|
|
_ = c.Process(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// CkQuorum checks if the current Sentinel configuration is able to reach the
|
|
|
|
// quorum needed to failover a master, and the majority needed to authorize the
|
|
|
|
// failover. This command should be used in monitoring systems to check if a
|
|
|
|
// Sentinel deployment is ok.
|
|
|
|
func (c *SentinelClient) CkQuorum(name string) *StringCmd {
|
|
|
|
cmd := NewStringCmd("sentinel", "ckquorum", name)
|
|
|
|
_ = c.Process(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Monitor tells the Sentinel to start monitoring a new master with the specified
|
|
|
|
// name, ip, port, and quorum.
|
|
|
|
func (c *SentinelClient) Monitor(name, ip, port, quorum string) *StringCmd {
|
|
|
|
cmd := NewStringCmd("sentinel", "monitor", name, ip, port, quorum)
|
|
|
|
_ = c.Process(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set is used in order to change configuration parameters of a specific master.
|
|
|
|
func (c *SentinelClient) Set(name, option, value string) *StringCmd {
|
|
|
|
cmd := NewStringCmd("sentinel", "set", name, option, value)
|
|
|
|
_ = c.Process(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove is used in order to remove the specified master: the master will no
|
|
|
|
// longer be monitored, and will totally be removed from the internal state of
|
|
|
|
// the Sentinel.
|
|
|
|
func (c *SentinelClient) Remove(name string) *StringCmd {
|
|
|
|
cmd := NewStringCmd("sentinel", "remove", name)
|
|
|
|
_ = c.Process(cmd)
|
2019-04-08 11:05:15 +02:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
type sentinelFailover struct {
|
|
|
|
sentinelAddrs []string
|
|
|
|
|
2020-09-27 23:09:46 +02:00
|
|
|
opt *Options
|
|
|
|
username string
|
|
|
|
password string
|
2019-04-08 11:05:15 +02:00
|
|
|
|
|
|
|
pool *pool.ConnPool
|
|
|
|
poolOnce sync.Once
|
|
|
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
masterName string
|
|
|
|
_masterAddr string
|
|
|
|
sentinel *SentinelClient
|
|
|
|
pubsub *PubSub
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sentinelFailover) Close() error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
if c.sentinel != nil {
|
|
|
|
return c.closeSentinel()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-27 23:09:46 +02:00
|
|
|
func (c *sentinelFailover) closeSentinel() error {
|
|
|
|
firstErr := c.pubsub.Close()
|
|
|
|
c.pubsub = nil
|
|
|
|
|
|
|
|
err := c.sentinel.Close()
|
|
|
|
if err != nil && firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
c.sentinel = nil
|
|
|
|
|
|
|
|
return firstErr
|
|
|
|
}
|
|
|
|
|
2019-04-08 11:05:15 +02:00
|
|
|
func (c *sentinelFailover) Pool() *pool.ConnPool {
|
|
|
|
c.poolOnce.Do(func() {
|
2020-09-27 23:09:46 +02:00
|
|
|
opt := *c.opt
|
|
|
|
opt.Dialer = c.dial
|
|
|
|
c.pool = newConnPool(&opt)
|
2019-04-08 11:05:15 +02:00
|
|
|
})
|
|
|
|
return c.pool
|
|
|
|
}
|
|
|
|
|
2020-09-27 23:09:46 +02:00
|
|
|
func (c *sentinelFailover) dial(ctx context.Context, network, _ string) (net.Conn, error) {
|
2019-04-08 11:05:15 +02:00
|
|
|
addr, err := c.MasterAddr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-27 23:09:46 +02:00
|
|
|
if c.opt.Dialer != nil {
|
|
|
|
return c.opt.Dialer(ctx, network, addr)
|
|
|
|
}
|
2019-04-08 11:05:15 +02:00
|
|
|
return net.DialTimeout("tcp", addr, c.opt.DialTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sentinelFailover) MasterAddr() (string, error) {
|
|
|
|
addr, err := c.masterAddr()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
c.switchMaster(addr)
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sentinelFailover) masterAddr() (string, error) {
|
2020-09-27 23:09:46 +02:00
|
|
|
c.mu.RLock()
|
|
|
|
sentinel := c.sentinel
|
|
|
|
c.mu.RUnlock()
|
|
|
|
|
|
|
|
if sentinel != nil {
|
|
|
|
addr := c.getMasterAddr(sentinel)
|
|
|
|
if addr != "" {
|
|
|
|
return addr, nil
|
|
|
|
}
|
2019-04-08 11:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
2020-09-27 23:09:46 +02:00
|
|
|
if c.sentinel != nil {
|
|
|
|
addr := c.getMasterAddr(c.sentinel)
|
|
|
|
if addr != "" {
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
_ = c.closeSentinel()
|
|
|
|
}
|
|
|
|
|
2019-04-08 11:05:15 +02:00
|
|
|
for i, sentinelAddr := range c.sentinelAddrs {
|
|
|
|
sentinel := NewSentinelClient(&Options{
|
2020-09-27 23:09:46 +02:00
|
|
|
Addr: sentinelAddr,
|
|
|
|
Dialer: c.opt.Dialer,
|
|
|
|
|
|
|
|
Username: c.username,
|
|
|
|
Password: c.password,
|
2019-04-08 11:05:15 +02:00
|
|
|
|
|
|
|
MaxRetries: c.opt.MaxRetries,
|
|
|
|
|
|
|
|
DialTimeout: c.opt.DialTimeout,
|
|
|
|
ReadTimeout: c.opt.ReadTimeout,
|
|
|
|
WriteTimeout: c.opt.WriteTimeout,
|
|
|
|
|
|
|
|
PoolSize: c.opt.PoolSize,
|
|
|
|
PoolTimeout: c.opt.PoolTimeout,
|
|
|
|
IdleTimeout: c.opt.IdleTimeout,
|
|
|
|
IdleCheckFrequency: c.opt.IdleCheckFrequency,
|
|
|
|
|
|
|
|
TLSConfig: c.opt.TLSConfig,
|
|
|
|
})
|
|
|
|
|
|
|
|
masterAddr, err := sentinel.GetMasterAddrByName(c.masterName).Result()
|
|
|
|
if err != nil {
|
2020-09-27 23:09:46 +02:00
|
|
|
internal.Logger.Printf("sentinel: GetMasterAddrByName master=%q failed: %s",
|
2019-04-08 11:05:15 +02:00
|
|
|
c.masterName, err)
|
|
|
|
_ = sentinel.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push working sentinel to the top.
|
|
|
|
c.sentinelAddrs[0], c.sentinelAddrs[i] = c.sentinelAddrs[i], c.sentinelAddrs[0]
|
|
|
|
c.setSentinel(sentinel)
|
|
|
|
|
|
|
|
addr := net.JoinHostPort(masterAddr[0], masterAddr[1])
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("redis: all sentinels are unreachable")
|
|
|
|
}
|
|
|
|
|
2020-09-27 23:09:46 +02:00
|
|
|
func (c *sentinelFailover) getMasterAddr(sentinel *SentinelClient) string {
|
2019-04-08 11:05:15 +02:00
|
|
|
addr, err := sentinel.GetMasterAddrByName(c.masterName).Result()
|
|
|
|
if err != nil {
|
2020-09-27 23:09:46 +02:00
|
|
|
internal.Logger.Printf("sentinel: GetMasterAddrByName name=%q failed: %s",
|
2019-04-08 11:05:15 +02:00
|
|
|
c.masterName, err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return net.JoinHostPort(addr[0], addr[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sentinelFailover) switchMaster(addr string) {
|
|
|
|
c.mu.RLock()
|
|
|
|
masterAddr := c._masterAddr
|
|
|
|
c.mu.RUnlock()
|
|
|
|
if masterAddr == addr {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
2020-09-27 23:09:46 +02:00
|
|
|
if c._masterAddr == addr {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
internal.Logger.Printf("sentinel: new master=%q addr=%q",
|
2019-04-08 11:05:15 +02:00
|
|
|
c.masterName, addr)
|
|
|
|
_ = c.Pool().Filter(func(cn *pool.Conn) bool {
|
|
|
|
return cn.RemoteAddr().String() != addr
|
|
|
|
})
|
|
|
|
c._masterAddr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sentinelFailover) setSentinel(sentinel *SentinelClient) {
|
2020-09-27 23:09:46 +02:00
|
|
|
if c.sentinel != nil {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
2019-04-08 11:05:15 +02:00
|
|
|
c.sentinel = sentinel
|
2020-09-27 23:09:46 +02:00
|
|
|
c.discoverSentinels()
|
2019-04-08 11:05:15 +02:00
|
|
|
|
|
|
|
c.pubsub = sentinel.Subscribe("+switch-master")
|
|
|
|
go c.listen(c.pubsub)
|
|
|
|
}
|
|
|
|
|
2020-09-27 23:09:46 +02:00
|
|
|
func (c *sentinelFailover) discoverSentinels() {
|
|
|
|
sentinels, err := c.sentinel.Sentinels(c.masterName).Result()
|
2019-04-08 11:05:15 +02:00
|
|
|
if err != nil {
|
2020-09-27 23:09:46 +02:00
|
|
|
internal.Logger.Printf("sentinel: Sentinels master=%q failed: %s", c.masterName, err)
|
2019-04-08 11:05:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, sentinel := range sentinels {
|
|
|
|
vals := sentinel.([]interface{})
|
|
|
|
for i := 0; i < len(vals); i += 2 {
|
|
|
|
key := vals[i].(string)
|
|
|
|
if key == "name" {
|
|
|
|
sentinelAddr := vals[i+1].(string)
|
|
|
|
if !contains(c.sentinelAddrs, sentinelAddr) {
|
2020-09-27 23:09:46 +02:00
|
|
|
internal.Logger.Printf("sentinel: discovered new sentinel=%q for master=%q",
|
2019-04-08 11:05:15 +02:00
|
|
|
sentinelAddr, c.masterName)
|
|
|
|
c.sentinelAddrs = append(c.sentinelAddrs, sentinelAddr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sentinelFailover) listen(pubsub *PubSub) {
|
|
|
|
ch := pubsub.Channel()
|
|
|
|
for {
|
|
|
|
msg, ok := <-ch
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-09-27 23:09:46 +02:00
|
|
|
if msg.Channel == "+switch-master" {
|
2019-04-08 11:05:15 +02:00
|
|
|
parts := strings.Split(msg.Payload, " ")
|
|
|
|
if parts[0] != c.masterName {
|
2020-09-27 23:09:46 +02:00
|
|
|
internal.Logger.Printf("sentinel: ignore addr for master=%q", parts[0])
|
2019-04-08 11:05:15 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
addr := net.JoinHostPort(parts[3], parts[4])
|
|
|
|
c.switchMaster(addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func contains(slice []string, str string) bool {
|
|
|
|
for _, s := range slice {
|
|
|
|
if s == str {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|