2014-05-11 18:11:55 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2018-05-11 09:46:09 +03:00
|
|
|
"crypto/tls"
|
2014-05-11 18:11:55 +04:00
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2017-02-18 17:42:34 +03:00
|
|
|
"github.com/go-redis/redis/internal"
|
|
|
|
"github.com/go-redis/redis/internal/pool"
|
2014-05-11 18:11:55 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
// FailoverOptions are used to configure a failover client and should
|
|
|
|
// be passed to NewFailoverClient.
|
2014-05-11 18:11:55 +04:00
|
|
|
type FailoverOptions struct {
|
2015-01-31 17:54:37 +03:00
|
|
|
// The master name.
|
|
|
|
MasterName string
|
2015-05-23 17:55:08 +03:00
|
|
|
// A seed list of host:port addresses of sentinel nodes.
|
2014-05-11 18:11:55 +04:00
|
|
|
SentinelAddrs []string
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
// Following options are copied from Options struct.
|
|
|
|
|
2017-05-25 14:16:39 +03:00
|
|
|
OnConnect func(*Conn) error
|
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
Password string
|
2016-06-05 12:45:39 +03:00
|
|
|
DB int
|
2015-05-23 17:55:08 +03:00
|
|
|
|
2018-08-14 16:16:27 +03:00
|
|
|
MaxRetries int
|
|
|
|
MinRetryBackoff time.Duration
|
|
|
|
MaxRetryBackoff time.Duration
|
2016-03-17 19:00:47 +03:00
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
DialTimeout time.Duration
|
|
|
|
ReadTimeout time.Duration
|
2015-01-31 17:54:37 +03:00
|
|
|
WriteTimeout time.Duration
|
2014-05-11 18:11:55 +04:00
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
PoolSize int
|
2018-08-14 16:16:27 +03:00
|
|
|
MinIdleConns int
|
|
|
|
MaxConnAge time.Duration
|
2016-03-17 19:00:47 +03:00
|
|
|
PoolTimeout time.Duration
|
|
|
|
IdleTimeout time.Duration
|
|
|
|
IdleCheckFrequency time.Duration
|
2018-05-11 09:46:09 +03:00
|
|
|
|
|
|
|
TLSConfig *tls.Config
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
func (opt *FailoverOptions) options() *Options {
|
|
|
|
return &Options{
|
|
|
|
Addr: "FailoverClient",
|
2014-05-11 18:11:55 +04:00
|
|
|
|
2017-05-25 14:16:39 +03:00
|
|
|
OnConnect: opt.OnConnect,
|
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
DB: opt.DB,
|
|
|
|
Password: opt.Password,
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
MaxRetries: opt.MaxRetries,
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
DialTimeout: opt.DialTimeout,
|
2014-05-11 18:11:55 +04:00
|
|
|
ReadTimeout: opt.ReadTimeout,
|
|
|
|
WriteTimeout: opt.WriteTimeout,
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
PoolSize: opt.PoolSize,
|
|
|
|
PoolTimeout: opt.PoolTimeout,
|
|
|
|
IdleTimeout: opt.IdleTimeout,
|
|
|
|
IdleCheckFrequency: opt.IdleCheckFrequency,
|
2018-05-11 09:46:09 +03:00
|
|
|
|
|
|
|
TLSConfig: opt.TLSConfig,
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-12 09:36:03 +03:00
|
|
|
// NewFailoverClient returns a Redis client that uses Redis Sentinel
|
|
|
|
// for automatic failover. It's safe for concurrent use by multiple
|
|
|
|
// goroutines.
|
2014-05-11 18:11:55 +04:00
|
|
|
func NewFailoverClient(failoverOpt *FailoverOptions) *Client {
|
|
|
|
opt := failoverOpt.options()
|
2016-06-05 14:10:30 +03:00
|
|
|
opt.init()
|
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
failover := &sentinelFailover{
|
|
|
|
masterName: failoverOpt.MasterName,
|
|
|
|
sentinelAddrs: failoverOpt.SentinelAddrs,
|
|
|
|
|
|
|
|
opt: opt,
|
|
|
|
}
|
2016-06-05 14:10:30 +03:00
|
|
|
|
2018-01-20 13:26:33 +03:00
|
|
|
c := Client{
|
2016-06-05 12:45:39 +03:00
|
|
|
baseClient: baseClient{
|
|
|
|
opt: opt,
|
|
|
|
connPool: failover.Pool(),
|
|
|
|
|
|
|
|
onClose: func() error {
|
|
|
|
return failover.Close()
|
|
|
|
},
|
2016-03-09 16:14:01 +03:00
|
|
|
},
|
|
|
|
}
|
2018-01-20 13:26:33 +03:00
|
|
|
c.baseClient.init()
|
2018-08-12 11:11:01 +03:00
|
|
|
c.cmdable.setProcessor(c.Process)
|
2016-06-05 14:10:30 +03:00
|
|
|
|
2018-01-20 13:26:33 +03:00
|
|
|
return &c
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2018-05-31 13:15:52 +03:00
|
|
|
type SentinelClient struct {
|
2016-03-09 16:14:01 +03:00
|
|
|
baseClient
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
2018-05-31 13:15:52 +03:00
|
|
|
func NewSentinelClient(opt *Options) *SentinelClient {
|
2016-06-05 14:10:30 +03:00
|
|
|
opt.init()
|
2018-05-31 13:15:52 +03:00
|
|
|
c := &SentinelClient{
|
2016-06-05 12:45:39 +03:00
|
|
|
baseClient: baseClient{
|
|
|
|
opt: opt,
|
|
|
|
connPool: newConnPool(opt),
|
|
|
|
},
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
2018-01-20 13:26:33 +03:00
|
|
|
c.baseClient.init()
|
2018-05-31 13:15:52 +03:00
|
|
|
return c
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
func (c *SentinelClient) pubSub() *PubSub {
|
2018-07-23 15:55:13 +03:00
|
|
|
pubsub := &PubSub{
|
2017-07-09 10:07:20 +03:00
|
|
|
opt: c.opt,
|
|
|
|
|
|
|
|
newConn: func(channels []string) (*pool.Conn, error) {
|
|
|
|
return c.newConn()
|
2014-05-11 18:11:55 +04:00
|
|
|
},
|
2017-07-09 10:07:20 +03:00
|
|
|
closeConn: c.connPool.CloseConn,
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
2018-07-23 15:55:13 +03:00
|
|
|
pubsub.init()
|
|
|
|
return pubsub
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03: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
|
|
|
|
}
|
|
|
|
|
2018-05-31 13:15:52 +03:00
|
|
|
func (c *SentinelClient) GetMasterAddrByName(name string) *StringSliceCmd {
|
2018-10-01 11:38:21 +03:00
|
|
|
cmd := NewStringSliceCmd("sentinel", "get-master-addr-by-name", name)
|
2014-05-11 18:11:55 +04:00
|
|
|
c.Process(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2018-05-31 13:15:52 +03:00
|
|
|
func (c *SentinelClient) Sentinels(name string) *SliceCmd {
|
2018-10-01 11:38:21 +03:00
|
|
|
cmd := NewSliceCmd("sentinel", "sentinels", name)
|
2014-05-11 18:11:55 +04:00
|
|
|
c.Process(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
type sentinelFailover struct {
|
|
|
|
sentinelAddrs []string
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
opt *Options
|
2014-05-11 18:11:55 +04:00
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
pool *pool.ConnPool
|
2014-05-11 18:11:55 +04:00
|
|
|
poolOnce sync.Once
|
|
|
|
|
2017-06-29 16:53:49 +03:00
|
|
|
mu sync.RWMutex
|
|
|
|
masterName string
|
|
|
|
_masterAddr string
|
2018-05-31 13:15:52 +03:00
|
|
|
sentinel *SentinelClient
|
2018-11-09 17:32:22 +03:00
|
|
|
pubsub *PubSub
|
2016-03-09 16:14:01 +03:00
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
func (c *sentinelFailover) Close() error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2018-10-25 09:42:56 +03:00
|
|
|
if c.sentinel != nil {
|
|
|
|
return c.closeSentinel()
|
|
|
|
}
|
|
|
|
return nil
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
func (c *sentinelFailover) Pool() *pool.ConnPool {
|
|
|
|
c.poolOnce.Do(func() {
|
|
|
|
c.opt.Dialer = c.dial
|
|
|
|
c.pool = newConnPool(c.opt)
|
2014-05-11 18:11:55 +04:00
|
|
|
})
|
2018-10-01 11:38:21 +03:00
|
|
|
return c.pool
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
func (c *sentinelFailover) dial() (net.Conn, error) {
|
|
|
|
addr, err := c.MasterAddr()
|
2017-07-09 10:07:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-01 11:38:21 +03:00
|
|
|
return net.DialTimeout("tcp", addr, c.opt.DialTimeout)
|
2017-07-09 10:07:20 +03:00
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
func (c *sentinelFailover) MasterAddr() (string, error) {
|
|
|
|
addr, err := c.masterAddr()
|
2017-06-29 16:53:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-10-01 11:38:21 +03:00
|
|
|
c.switchMaster(addr)
|
2017-06-29 16:53:49 +03:00
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
func (c *sentinelFailover) masterAddr() (string, error) {
|
|
|
|
addr := c.getMasterAddr()
|
|
|
|
if addr != "" {
|
|
|
|
return addr, nil
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
for i, sentinelAddr := range c.sentinelAddrs {
|
2018-05-31 13:15:52 +03:00
|
|
|
sentinel := NewSentinelClient(&Options{
|
2014-11-06 13:55:09 +03:00
|
|
|
Addr: sentinelAddr,
|
2014-05-11 18:11:55 +04:00
|
|
|
|
2018-10-10 11:51:34 +03:00
|
|
|
MaxRetries: c.opt.MaxRetries,
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
DialTimeout: c.opt.DialTimeout,
|
|
|
|
ReadTimeout: c.opt.ReadTimeout,
|
|
|
|
WriteTimeout: c.opt.WriteTimeout,
|
2014-05-11 18:11:55 +04:00
|
|
|
|
2018-10-10 11:51:34 +03:00
|
|
|
PoolSize: c.opt.PoolSize,
|
|
|
|
PoolTimeout: c.opt.PoolTimeout,
|
|
|
|
IdleTimeout: c.opt.IdleTimeout,
|
|
|
|
IdleCheckFrequency: c.opt.IdleCheckFrequency,
|
|
|
|
|
|
|
|
TLSConfig: c.opt.TLSConfig,
|
2014-05-11 18:11:55 +04:00
|
|
|
})
|
2017-06-29 16:53:49 +03:00
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
masterAddr, err := sentinel.GetMasterAddrByName(c.masterName).Result()
|
2014-05-11 18:11:55 +04:00
|
|
|
if err != nil {
|
2018-05-31 13:15:52 +03:00
|
|
|
internal.Logf("sentinel: GetMasterAddrByName master=%q failed: %s",
|
2018-10-01 11:38:21 +03:00
|
|
|
c.masterName, err)
|
2018-10-25 09:42:56 +03:00
|
|
|
_ = sentinel.Close()
|
2014-11-06 13:55:09 +03:00
|
|
|
continue
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
2014-11-06 13:55:09 +03:00
|
|
|
|
|
|
|
// Push working sentinel to the top.
|
2018-10-01 11:38:21 +03:00
|
|
|
c.sentinelAddrs[0], c.sentinelAddrs[i] = c.sentinelAddrs[i], c.sentinelAddrs[0]
|
|
|
|
c.setSentinel(sentinel)
|
2017-06-29 16:53:49 +03:00
|
|
|
|
2014-11-06 13:55:09 +03:00
|
|
|
addr := net.JoinHostPort(masterAddr[0], masterAddr[1])
|
|
|
|
return addr, nil
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("redis: all sentinels are unreachable")
|
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
func (c *sentinelFailover) getMasterAddr() string {
|
|
|
|
c.mu.RLock()
|
|
|
|
sentinel := c.sentinel
|
|
|
|
c.mu.RUnlock()
|
|
|
|
|
|
|
|
if sentinel == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := sentinel.GetMasterAddrByName(c.masterName).Result()
|
|
|
|
if err != nil {
|
|
|
|
internal.Logf("sentinel: GetMasterAddrByName name=%q failed: %s",
|
|
|
|
c.masterName, err)
|
|
|
|
c.mu.Lock()
|
|
|
|
if c.sentinel == sentinel {
|
|
|
|
c.closeSentinel()
|
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return net.JoinHostPort(addr[0], addr[1])
|
2018-07-23 15:55:13 +03:00
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
func (c *sentinelFailover) switchMaster(addr string) {
|
|
|
|
c.mu.RLock()
|
|
|
|
masterAddr := c._masterAddr
|
|
|
|
c.mu.RUnlock()
|
|
|
|
if masterAddr == addr {
|
2018-07-23 15:55:13 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
2018-07-23 15:55:13 +03:00
|
|
|
internal.Logf("sentinel: new master=%q addr=%q",
|
|
|
|
c.masterName, addr)
|
|
|
|
_ = c.Pool().Filter(func(cn *pool.Conn) bool {
|
|
|
|
return cn.RemoteAddr().String() != addr
|
2017-06-29 16:53:49 +03:00
|
|
|
})
|
2018-07-23 15:55:13 +03:00
|
|
|
c._masterAddr = addr
|
2017-06-29 16:53:49 +03:00
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
func (c *sentinelFailover) setSentinel(sentinel *SentinelClient) {
|
|
|
|
c.discoverSentinels(sentinel)
|
|
|
|
c.sentinel = sentinel
|
|
|
|
go c.listen(sentinel)
|
2016-03-09 16:14:01 +03:00
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
func (c *sentinelFailover) closeSentinel() error {
|
2018-11-09 17:32:22 +03:00
|
|
|
if err := c.pubsub.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-01 11:38:21 +03:00
|
|
|
err := c.sentinel.Close()
|
|
|
|
c.sentinel = nil
|
2016-03-09 16:14:01 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
func (c *sentinelFailover) discoverSentinels(sentinel *SentinelClient) {
|
|
|
|
sentinels, err := sentinel.Sentinels(c.masterName).Result()
|
2014-05-11 18:11:55 +04:00
|
|
|
if err != nil {
|
2018-10-01 11:38:21 +03:00
|
|
|
internal.Logf("sentinel: Sentinels master=%q failed: %s", c.masterName, err)
|
2014-05-11 18:11:55 +04: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)
|
2018-10-01 11:38:21 +03:00
|
|
|
if !contains(c.sentinelAddrs, sentinelAddr) {
|
|
|
|
internal.Logf("sentinel: discovered new sentinel=%q for master=%q",
|
|
|
|
sentinelAddr, c.masterName)
|
|
|
|
c.sentinelAddrs = append(c.sentinelAddrs, sentinelAddr)
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
func (c *sentinelFailover) listen(sentinel *SentinelClient) {
|
2018-11-09 17:32:22 +03:00
|
|
|
c.pubsub = sentinel.Subscribe("+switch-master")
|
|
|
|
ch := c.pubsub.Channel()
|
2018-07-23 15:55:13 +03:00
|
|
|
for {
|
2018-10-01 11:38:21 +03:00
|
|
|
msg, ok := <-ch
|
|
|
|
if !ok {
|
|
|
|
break
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
2016-03-09 16:14:01 +03:00
|
|
|
switch msg.Channel {
|
|
|
|
case "+switch-master":
|
|
|
|
parts := strings.Split(msg.Payload, " ")
|
2018-10-01 11:38:21 +03:00
|
|
|
if parts[0] != c.masterName {
|
2017-06-29 16:53:49 +03:00
|
|
|
internal.Logf("sentinel: ignore addr for master=%q", parts[0])
|
2016-03-09 16:14:01 +03:00
|
|
|
continue
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
2016-03-09 16:14:01 +03:00
|
|
|
addr := net.JoinHostPort(parts[3], parts[4])
|
2018-10-01 11:38:21 +03:00
|
|
|
c.switchMaster(addr)
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func contains(slice []string, str string) bool {
|
|
|
|
for _, s := range slice {
|
|
|
|
if s == str {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|