2014-05-11 18:11:55 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2014-07-31 17:01:54 +04:00
|
|
|
"log"
|
2014-05-11 18:11:55 +04:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
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.
|
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
Password string
|
2015-05-23 17:55:08 +03:00
|
|
|
DB int64
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
PoolSize int
|
2015-01-31 17:54:37 +03:00
|
|
|
PoolTimeout time.Duration
|
|
|
|
IdleTimeout time.Duration
|
2015-08-24 11:46:48 +03:00
|
|
|
|
|
|
|
MaxRetries int
|
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
|
|
|
|
|
|
|
DB: opt.DB,
|
|
|
|
Password: opt.Password,
|
|
|
|
|
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,
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
PoolSize: opt.PoolSize,
|
|
|
|
PoolTimeout: opt.PoolTimeout,
|
2014-05-11 18:11:55 +04:00
|
|
|
IdleTimeout: opt.IdleTimeout,
|
2015-08-24 11:46:48 +03:00
|
|
|
|
|
|
|
MaxRetries: opt.MaxRetries,
|
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()
|
|
|
|
failover := &sentinelFailover{
|
|
|
|
masterName: failoverOpt.MasterName,
|
|
|
|
sentinelAddrs: failoverOpt.SentinelAddrs,
|
|
|
|
|
|
|
|
opt: opt,
|
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
return newClient(opt, failover.Pool())
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type sentinelClient struct {
|
2015-01-24 15:12:48 +03:00
|
|
|
commandable
|
2014-05-11 18:11:55 +04:00
|
|
|
*baseClient
|
|
|
|
}
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
func newSentinel(opt *Options) *sentinelClient {
|
2015-01-24 15:12:48 +03:00
|
|
|
base := &baseClient{
|
|
|
|
opt: opt,
|
2015-05-23 17:55:08 +03:00
|
|
|
connPool: newConnPool(opt),
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2014-05-11 18:11:55 +04:00
|
|
|
return &sentinelClient{
|
2015-01-24 15:12:48 +03:00
|
|
|
baseClient: base,
|
|
|
|
commandable: commandable{process: base.process},
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sentinelClient) PubSub() *PubSub {
|
|
|
|
return &PubSub{
|
|
|
|
baseClient: &baseClient{
|
|
|
|
opt: c.opt,
|
2014-06-28 15:47:37 +04:00
|
|
|
connPool: newSingleConnPool(c.connPool, false),
|
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 {
|
|
|
|
masterName string
|
|
|
|
sentinelAddrs []string
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
opt *Options
|
2014-05-11 18:11:55 +04:00
|
|
|
|
|
|
|
pool pool
|
|
|
|
poolOnce sync.Once
|
|
|
|
|
|
|
|
lock sync.RWMutex
|
|
|
|
_sentinel *sentinelClient
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sentinelFailover) Pool() pool {
|
|
|
|
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) {
|
|
|
|
defer d.lock.Unlock()
|
|
|
|
d.lock.Lock()
|
|
|
|
|
|
|
|
// Try last working sentinel.
|
|
|
|
if d._sentinel != nil {
|
|
|
|
addr, err := d._sentinel.GetMasterAddrByName(d.masterName).Result()
|
|
|
|
if err != nil {
|
2014-07-31 17:01:54 +04:00
|
|
|
log.Printf("redis-sentinel: GetMasterAddrByName %q failed: %s", d.masterName, err)
|
2014-05-11 18:11:55 +04:00
|
|
|
d.resetSentinel()
|
|
|
|
} else {
|
|
|
|
addr := net.JoinHostPort(addr[0], addr[1])
|
2014-07-31 17:01:54 +04:00
|
|
|
log.Printf("redis-sentinel: %q addr is %s", d.masterName, addr)
|
2014-05-11 18:11:55 +04:00
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
})
|
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 {
|
2014-07-31 17:01:54 +04:00
|
|
|
log.Printf("redis-sentinel: GetMasterAddrByName %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)
|
|
|
|
addr := net.JoinHostPort(masterAddr[0], masterAddr[1])
|
|
|
|
log.Printf("redis-sentinel: %q addr is %s", d.masterName, addr)
|
|
|
|
return addr, nil
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("redis: all sentinels are unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sentinelFailover) setSentinel(sentinel *sentinelClient) {
|
|
|
|
d.discoverSentinels(sentinel)
|
|
|
|
d._sentinel = sentinel
|
|
|
|
go d.listen()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sentinelFailover) discoverSentinels(sentinel *sentinelClient) {
|
|
|
|
sentinels, err := sentinel.Sentinels(d.masterName).Result()
|
|
|
|
if err != nil {
|
2014-07-31 17:01:54 +04:00
|
|
|
log.Printf("redis-sentinel: Sentinels %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) {
|
2014-07-31 17:01:54 +04:00
|
|
|
log.Printf(
|
|
|
|
"redis-sentinel: discovered new %q sentinel: %s",
|
2014-05-11 18:11:55 +04:00
|
|
|
d.masterName, sentinelAddr,
|
|
|
|
)
|
|
|
|
d.sentinelAddrs = append(d.sentinelAddrs, sentinelAddr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:41:16 +03:00
|
|
|
// closeOldConns closes connections to the old master after failover switch.
|
|
|
|
func (d *sentinelFailover) closeOldConns(newMaster string) {
|
|
|
|
// Good connections that should be put back to the pool. They
|
|
|
|
// can't be put immediately, because pool.First will return them
|
|
|
|
// again on next iteration.
|
|
|
|
cnsToPut := make([]*conn, 0)
|
|
|
|
|
|
|
|
for {
|
|
|
|
cn := d.pool.First()
|
|
|
|
if cn == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if cn.RemoteAddr().String() != newMaster {
|
|
|
|
log.Printf(
|
|
|
|
"redis-sentinel: closing connection to the old master %s",
|
|
|
|
cn.RemoteAddr(),
|
|
|
|
)
|
|
|
|
d.pool.Remove(cn)
|
|
|
|
} else {
|
|
|
|
cnsToPut = append(cnsToPut, cn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cn := range cnsToPut {
|
|
|
|
d.pool.Put(cn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
func (d *sentinelFailover) listen() {
|
|
|
|
var pubsub *PubSub
|
|
|
|
for {
|
|
|
|
if pubsub == nil {
|
|
|
|
pubsub = d._sentinel.PubSub()
|
|
|
|
if err := pubsub.Subscribe("+switch-master"); err != nil {
|
2014-07-31 17:01:54 +04:00
|
|
|
log.Printf("redis-sentinel: Subscribe failed: %s", err)
|
2014-05-11 18:11:55 +04:00
|
|
|
d.lock.Lock()
|
|
|
|
d.resetSentinel()
|
|
|
|
d.lock.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msgIface, err := pubsub.Receive()
|
|
|
|
if err != nil {
|
2014-07-31 17:01:54 +04:00
|
|
|
log.Printf("redis-sentinel: Receive failed: %s", err)
|
2014-11-06 13:55:09 +03:00
|
|
|
pubsub.Close()
|
2014-05-11 18:11:55 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch msg := msgIface.(type) {
|
|
|
|
case *Message:
|
|
|
|
switch msg.Channel {
|
|
|
|
case "+switch-master":
|
|
|
|
parts := strings.Split(msg.Payload, " ")
|
|
|
|
if parts[0] != d.masterName {
|
2014-07-31 17:01:54 +04:00
|
|
|
log.Printf("redis-sentinel: ignore new %s addr", parts[0])
|
2014-05-11 18:11:55 +04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
addr := net.JoinHostPort(parts[3], parts[4])
|
2014-07-31 17:01:54 +04:00
|
|
|
log.Printf(
|
|
|
|
"redis-sentinel: new %q addr is %s",
|
2014-05-11 18:11:55 +04:00
|
|
|
d.masterName, addr,
|
|
|
|
)
|
2015-04-05 15:41:16 +03:00
|
|
|
|
|
|
|
d.closeOldConns(addr)
|
2014-05-11 18:11:55 +04:00
|
|
|
default:
|
2014-07-31 17:01:54 +04:00
|
|
|
log.Printf("redis-sentinel: unsupported message: %s", msg)
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
case *Subscription:
|
|
|
|
// Ignore.
|
|
|
|
default:
|
2014-07-31 17:01:54 +04:00
|
|
|
log.Printf("redis-sentinel: unsupported message: %s", msgIface)
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sentinelFailover) resetSentinel() {
|
|
|
|
d._sentinel.Close()
|
|
|
|
d._sentinel = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func contains(slice []string, str string) bool {
|
|
|
|
for _, s := range slice {
|
|
|
|
if s == str {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|