2014-05-11 18:11:55 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
MaxRetries int
|
|
|
|
|
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
|
|
|
|
PoolTimeout time.Duration
|
|
|
|
IdleTimeout time.Duration
|
|
|
|
IdleCheckFrequency time.Duration
|
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,
|
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
|
|
|
|
2016-06-05 12:45:39 +03:00
|
|
|
client := Client{
|
|
|
|
baseClient: baseClient{
|
|
|
|
opt: opt,
|
|
|
|
connPool: failover.Pool(),
|
|
|
|
|
|
|
|
onClose: func() error {
|
|
|
|
return failover.Close()
|
|
|
|
},
|
2016-03-09 16:14:01 +03:00
|
|
|
},
|
|
|
|
}
|
2017-05-25 13:38:04 +03:00
|
|
|
client.setProcessor(client.Process)
|
2016-06-05 14:10:30 +03:00
|
|
|
|
2016-06-05 12:45:39 +03:00
|
|
|
return &client
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type sentinelClient struct {
|
2016-06-05 12:45:39 +03:00
|
|
|
cmdable
|
2016-03-09 16:14:01 +03:00
|
|
|
baseClient
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
func newSentinel(opt *Options) *sentinelClient {
|
2016-06-05 14:10:30 +03:00
|
|
|
opt.init()
|
2016-06-05 12:45:39 +03:00
|
|
|
client := sentinelClient{
|
|
|
|
baseClient: baseClient{
|
|
|
|
opt: opt,
|
|
|
|
connPool: newConnPool(opt),
|
|
|
|
},
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
2016-06-05 12:45:39 +03:00
|
|
|
client.cmdable = cmdable{client.Process}
|
|
|
|
return &client
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sentinelClient) PubSub() *PubSub {
|
|
|
|
return &PubSub{
|
2016-06-05 12:45:39 +03:00
|
|
|
base: baseClient{
|
2014-05-11 18:11:55 +04:00
|
|
|
opt: c.opt,
|
2017-04-17 15:43:58 +03:00
|
|
|
connPool: c.connPool,
|
2014-05-11 18:11:55 +04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sentinelClient) GetMasterAddrByName(name string) *StringSliceCmd {
|
|
|
|
cmd := NewStringSliceCmd("SENTINEL", "get-master-addr-by-name", name)
|
|
|
|
c.Process(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sentinelClient) Sentinels(name string) *SliceCmd {
|
|
|
|
cmd := NewSliceCmd("SENTINEL", "sentinels", name)
|
|
|
|
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
|
|
|
|
sentinel *sentinelClient
|
2016-03-09 16:14:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sentinelFailover) Close() error {
|
|
|
|
return d.resetSentinel()
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sentinelFailover) dial() (net.Conn, error) {
|
|
|
|
addr, err := d.MasterAddr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return net.DialTimeout("tcp", addr, d.opt.DialTimeout)
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func (d *sentinelFailover) Pool() *pool.ConnPool {
|
2014-05-11 18:11:55 +04:00
|
|
|
d.poolOnce.Do(func() {
|
2015-05-02 16:11:18 +03:00
|
|
|
d.opt.Dialer = d.dial
|
2015-05-23 17:55:08 +03:00
|
|
|
d.pool = newConnPool(d.opt)
|
2014-05-11 18:11:55 +04:00
|
|
|
})
|
|
|
|
return d.pool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sentinelFailover) MasterAddr() (string, error) {
|
2016-03-09 16:14:01 +03:00
|
|
|
d.mu.Lock()
|
2017-01-28 11:53:10 +03:00
|
|
|
defer d.mu.Unlock()
|
2014-05-11 18:11:55 +04:00
|
|
|
|
2017-06-29 16:53:49 +03:00
|
|
|
addr, err := d.masterAddr()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d._masterAddr != addr {
|
|
|
|
d.switchMaster(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sentinelFailover) masterAddr() (string, error) {
|
2014-05-11 18:11:55 +04:00
|
|
|
// Try last working sentinel.
|
2016-03-09 16:14:01 +03:00
|
|
|
if d.sentinel != nil {
|
|
|
|
addr, err := d.sentinel.GetMasterAddrByName(d.masterName).Result()
|
2017-06-29 16:53:49 +03:00
|
|
|
if err == nil {
|
2014-05-11 18:11:55 +04:00
|
|
|
addr := net.JoinHostPort(addr[0], addr[1])
|
2017-06-29 16:53:49 +03:00
|
|
|
internal.Logf("sentinel: master=%q addr=%q", d.masterName, addr)
|
2014-05-11 18:11:55 +04:00
|
|
|
return addr, nil
|
|
|
|
}
|
2017-06-29 16:53:49 +03:00
|
|
|
|
|
|
|
internal.Logf("sentinel: GetMasterAddrByName name=%q failed: %s", d.masterName, err)
|
|
|
|
d._resetSentinel()
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
2014-11-06 13:55:09 +03:00
|
|
|
for i, sentinelAddr := range d.sentinelAddrs {
|
2014-05-11 18:11:55 +04:00
|
|
|
sentinel := newSentinel(&Options{
|
2014-11-06 13:55:09 +03:00
|
|
|
Addr: sentinelAddr,
|
2014-05-11 18:11:55 +04:00
|
|
|
|
|
|
|
DialTimeout: d.opt.DialTimeout,
|
|
|
|
ReadTimeout: d.opt.ReadTimeout,
|
|
|
|
WriteTimeout: d.opt.WriteTimeout,
|
|
|
|
|
|
|
|
PoolSize: d.opt.PoolSize,
|
2015-01-31 16:20:37 +03:00
|
|
|
PoolTimeout: d.opt.PoolTimeout,
|
2014-05-11 18:11:55 +04:00
|
|
|
IdleTimeout: d.opt.IdleTimeout,
|
|
|
|
})
|
2017-06-29 16:53:49 +03:00
|
|
|
|
2014-11-06 13:55:09 +03:00
|
|
|
masterAddr, err := sentinel.GetMasterAddrByName(d.masterName).Result()
|
2014-05-11 18:11:55 +04:00
|
|
|
if err != nil {
|
2017-06-29 16:53:49 +03:00
|
|
|
internal.Logf("sentinel: GetMasterAddrByName master=%q failed: %s", d.masterName, err)
|
2014-11-06 13:55:09 +03:00
|
|
|
sentinel.Close()
|
|
|
|
continue
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
2014-11-06 13:55:09 +03:00
|
|
|
|
|
|
|
// Push working sentinel to the top.
|
|
|
|
d.sentinelAddrs[0], d.sentinelAddrs[i] = d.sentinelAddrs[i], d.sentinelAddrs[0]
|
|
|
|
d.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")
|
|
|
|
}
|
|
|
|
|
2017-06-29 16:53:49 +03:00
|
|
|
func (d *sentinelFailover) switchMaster(masterAddr string) {
|
|
|
|
internal.Logf(
|
|
|
|
"sentinel: new master=%q addr=%q",
|
|
|
|
d.masterName, masterAddr,
|
|
|
|
)
|
|
|
|
_ = d.Pool().Filter(func(cn *pool.Conn) bool {
|
|
|
|
return cn.RemoteAddr().String() != masterAddr
|
|
|
|
})
|
|
|
|
d._masterAddr = masterAddr
|
|
|
|
}
|
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
func (d *sentinelFailover) setSentinel(sentinel *sentinelClient) {
|
|
|
|
d.discoverSentinels(sentinel)
|
2016-03-09 16:14:01 +03:00
|
|
|
d.sentinel = sentinel
|
2016-03-16 17:57:24 +03:00
|
|
|
go d.listen(sentinel)
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
2016-03-09 16:14:01 +03:00
|
|
|
func (d *sentinelFailover) resetSentinel() error {
|
2017-06-29 16:53:49 +03:00
|
|
|
var err error
|
2016-03-09 16:14:01 +03:00
|
|
|
d.mu.Lock()
|
2017-06-29 16:53:49 +03:00
|
|
|
if d.sentinel != nil {
|
|
|
|
err = d._resetSentinel()
|
|
|
|
}
|
2016-03-09 16:14:01 +03:00
|
|
|
d.mu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sentinelFailover) _resetSentinel() error {
|
2017-06-29 16:53:49 +03:00
|
|
|
err := d.sentinel.Close()
|
|
|
|
d.sentinel = nil
|
2016-03-09 16:14:01 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
func (d *sentinelFailover) discoverSentinels(sentinel *sentinelClient) {
|
|
|
|
sentinels, err := sentinel.Sentinels(d.masterName).Result()
|
|
|
|
if err != nil {
|
2017-06-29 16:53:49 +03:00
|
|
|
internal.Logf("sentinel: Sentinels master=%q failed: %s", d.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)
|
|
|
|
if !contains(d.sentinelAddrs, sentinelAddr) {
|
2016-04-09 14:52:01 +03:00
|
|
|
internal.Logf(
|
2017-06-29 16:53:49 +03:00
|
|
|
"sentinel: discovered new sentinel=%q for master=%q",
|
|
|
|
sentinelAddr, d.masterName,
|
2014-05-11 18:11:55 +04:00
|
|
|
)
|
|
|
|
d.sentinelAddrs = append(d.sentinelAddrs, sentinelAddr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
func (d *sentinelFailover) listen(sentinel *sentinelClient) {
|
2014-05-11 18:11:55 +04:00
|
|
|
var pubsub *PubSub
|
|
|
|
for {
|
|
|
|
if pubsub == nil {
|
2016-03-16 17:57:24 +03:00
|
|
|
pubsub = sentinel.PubSub()
|
2017-02-08 12:24:09 +03:00
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
if err := pubsub.Subscribe("+switch-master"); err != nil {
|
2016-04-09 14:52:01 +03:00
|
|
|
internal.Logf("sentinel: Subscribe failed: %s", err)
|
2017-02-08 12:24:09 +03:00
|
|
|
pubsub.Close()
|
2014-05-11 18:11:55 +04:00
|
|
|
d.resetSentinel()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 16:14:01 +03:00
|
|
|
msg, err := pubsub.ReceiveMessage()
|
2014-05-11 18:11:55 +04:00
|
|
|
if err != nil {
|
2016-04-09 14:52:01 +03:00
|
|
|
internal.Logf("sentinel: ReceiveMessage failed: %s", err)
|
2014-11-06 13:55:09 +03:00
|
|
|
pubsub.Close()
|
2016-03-09 16:14:01 +03:00
|
|
|
d.resetSentinel()
|
2014-05-11 18:11:55 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-09 16:14:01 +03:00
|
|
|
switch msg.Channel {
|
|
|
|
case "+switch-master":
|
|
|
|
parts := strings.Split(msg.Payload, " ")
|
|
|
|
if parts[0] != d.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])
|
|
|
|
|
2017-06-29 16:53:49 +03:00
|
|
|
d.mu.Lock()
|
|
|
|
if d._masterAddr != addr {
|
|
|
|
d.switchMaster(addr)
|
|
|
|
}
|
|
|
|
d.mu.Unlock()
|
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
|
|
|
|
}
|