2014-05-11 18:11:55 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2019-05-31 16:36:57 +03:00
|
|
|
"context"
|
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
|
|
|
|
2020-03-11 17:29:16 +03:00
|
|
|
"github.com/go-redis/redis/v8/internal"
|
|
|
|
"github.com/go-redis/redis/v8/internal/pool"
|
2020-09-17 16:13:43 +03:00
|
|
|
"github.com/go-redis/redis/v8/internal/rand"
|
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.
|
2020-08-27 08:51:57 +03:00
|
|
|
SentinelAddrs []string
|
2021-09-18 09:39:26 +03:00
|
|
|
|
|
|
|
// If specified with SentinelPassword, enables ACL-based authentication (via
|
|
|
|
// AUTH <user> <pass>).
|
|
|
|
SentinelUsername string
|
|
|
|
// Sentinel password from "requirepass <password>" (if enabled) in Sentinel
|
|
|
|
// configuration, or, if SentinelUsername is also supplied, used for ACL-based
|
|
|
|
// authentication.
|
2019-06-04 14:05:29 +03:00
|
|
|
SentinelPassword string
|
2014-05-11 18:11:55 +04:00
|
|
|
|
2020-09-11 15:52:38 +03:00
|
|
|
// Allows routing read-only commands to the closest master or slave node.
|
|
|
|
// This option only works with NewFailoverClusterClient.
|
|
|
|
RouteByLatency bool
|
|
|
|
// Allows routing read-only commands to the random master or slave node.
|
|
|
|
// This option only works with NewFailoverClusterClient.
|
|
|
|
RouteRandomly bool
|
|
|
|
|
2020-09-09 16:20:58 +03:00
|
|
|
// Route all commands to slave read-only nodes.
|
|
|
|
SlaveOnly bool
|
2020-09-05 17:39:26 +03:00
|
|
|
|
2021-02-10 18:20:08 +03:00
|
|
|
// Use slaves disconnected with master when cannot get connected slaves
|
|
|
|
// Now, this option only works in RandomSlaveAddr function.
|
|
|
|
UseDisconnectedSlaves bool
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
// Following options are copied from Options struct.
|
|
|
|
|
2019-06-04 14:05:29 +03:00
|
|
|
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
|
2020-06-10 10:36:22 +03:00
|
|
|
OnConnect func(ctx context.Context, cn *Conn) error
|
2017-05-25 14:16:39 +03:00
|
|
|
|
2020-05-21 08:59:20 +03:00
|
|
|
Username string
|
2019-06-04 14:05:29 +03:00
|
|
|
Password string
|
|
|
|
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
|
|
|
|
2021-07-16 06:58:01 +03:00
|
|
|
// PoolFIFO uses FIFO mode for each node connection pool GET/PUT (default LIFO).
|
|
|
|
PoolFIFO bool
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-11 11:24:38 +03:00
|
|
|
func (opt *FailoverOptions) clientOptions() *Options {
|
|
|
|
return &Options{
|
2020-06-10 10:36:22 +03:00
|
|
|
Addr: "FailoverClient",
|
|
|
|
|
2019-05-18 14:00:07 +03:00
|
|
|
Dialer: opt.Dialer,
|
2017-05-25 14:16:39 +03:00
|
|
|
OnConnect: opt.OnConnect,
|
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
DB: opt.DB,
|
2020-05-21 08:59:20 +03:00
|
|
|
Username: opt.Username,
|
2014-05-11 18:11:55 +04:00
|
|
|
Password: opt.Password,
|
|
|
|
|
2019-10-21 13:21:23 +03:00
|
|
|
MaxRetries: opt.MaxRetries,
|
|
|
|
MinRetryBackoff: opt.MinRetryBackoff,
|
|
|
|
MaxRetryBackoff: opt.MaxRetryBackoff,
|
2016-03-17 19:00:47 +03:00
|
|
|
|
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,
|
|
|
|
|
2021-07-16 06:58:01 +03:00
|
|
|
PoolFIFO: opt.PoolFIFO,
|
2016-03-17 19:00:47 +03:00
|
|
|
PoolSize: opt.PoolSize,
|
|
|
|
PoolTimeout: opt.PoolTimeout,
|
|
|
|
IdleTimeout: opt.IdleTimeout,
|
|
|
|
IdleCheckFrequency: opt.IdleCheckFrequency,
|
2019-10-21 13:21:23 +03:00
|
|
|
MinIdleConns: opt.MinIdleConns,
|
|
|
|
MaxConnAge: opt.MaxConnAge,
|
2018-05-11 09:46:09 +03:00
|
|
|
|
|
|
|
TLSConfig: opt.TLSConfig,
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
2020-09-11 11:24:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (opt *FailoverOptions) sentinelOptions(addr string) *Options {
|
|
|
|
return &Options{
|
|
|
|
Addr: addr,
|
|
|
|
|
|
|
|
Dialer: opt.Dialer,
|
|
|
|
OnConnect: opt.OnConnect,
|
|
|
|
|
|
|
|
DB: 0,
|
2021-09-18 09:39:26 +03:00
|
|
|
Username: opt.SentinelUsername,
|
2020-09-11 11:24:38 +03:00
|
|
|
Password: opt.SentinelPassword,
|
|
|
|
|
|
|
|
MaxRetries: opt.MaxRetries,
|
|
|
|
MinRetryBackoff: opt.MinRetryBackoff,
|
|
|
|
MaxRetryBackoff: opt.MaxRetryBackoff,
|
|
|
|
|
|
|
|
DialTimeout: opt.DialTimeout,
|
|
|
|
ReadTimeout: opt.ReadTimeout,
|
|
|
|
WriteTimeout: opt.WriteTimeout,
|
|
|
|
|
2021-07-16 06:58:01 +03:00
|
|
|
PoolFIFO: opt.PoolFIFO,
|
2020-09-11 11:24:38 +03:00
|
|
|
PoolSize: opt.PoolSize,
|
|
|
|
PoolTimeout: opt.PoolTimeout,
|
|
|
|
IdleTimeout: opt.IdleTimeout,
|
|
|
|
IdleCheckFrequency: opt.IdleCheckFrequency,
|
|
|
|
MinIdleConns: opt.MinIdleConns,
|
|
|
|
MaxConnAge: opt.MaxConnAge,
|
|
|
|
|
|
|
|
TLSConfig: opt.TLSConfig,
|
|
|
|
}
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
2020-09-09 15:27:17 +03:00
|
|
|
func (opt *FailoverOptions) clusterOptions() *ClusterOptions {
|
2020-09-11 11:24:38 +03:00
|
|
|
return &ClusterOptions{
|
2020-09-09 15:27:17 +03:00
|
|
|
Dialer: opt.Dialer,
|
|
|
|
OnConnect: opt.OnConnect,
|
|
|
|
|
|
|
|
Username: opt.Username,
|
|
|
|
Password: opt.Password,
|
|
|
|
|
2020-09-11 15:52:38 +03:00
|
|
|
MaxRedirects: opt.MaxRetries,
|
|
|
|
|
|
|
|
RouteByLatency: opt.RouteByLatency,
|
|
|
|
RouteRandomly: opt.RouteRandomly,
|
|
|
|
|
2020-09-09 15:27:17 +03:00
|
|
|
MinRetryBackoff: opt.MinRetryBackoff,
|
|
|
|
MaxRetryBackoff: opt.MaxRetryBackoff,
|
|
|
|
|
|
|
|
DialTimeout: opt.DialTimeout,
|
|
|
|
ReadTimeout: opt.ReadTimeout,
|
|
|
|
WriteTimeout: opt.WriteTimeout,
|
|
|
|
|
2021-07-16 06:58:01 +03:00
|
|
|
PoolFIFO: opt.PoolFIFO,
|
2020-09-09 15:27:17 +03:00
|
|
|
PoolSize: opt.PoolSize,
|
|
|
|
PoolTimeout: opt.PoolTimeout,
|
|
|
|
IdleTimeout: opt.IdleTimeout,
|
|
|
|
IdleCheckFrequency: opt.IdleCheckFrequency,
|
|
|
|
MinIdleConns: opt.MinIdleConns,
|
|
|
|
MaxConnAge: opt.MaxConnAge,
|
|
|
|
|
|
|
|
TLSConfig: opt.TLSConfig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-09-11 15:52:38 +03:00
|
|
|
if failoverOpt.RouteByLatency {
|
|
|
|
panic("to route commands by latency, use NewFailoverClusterClient")
|
|
|
|
}
|
|
|
|
if failoverOpt.RouteRandomly {
|
|
|
|
panic("to route commands randomly, use NewFailoverClusterClient")
|
|
|
|
}
|
|
|
|
|
2020-09-11 11:24:38 +03:00
|
|
|
sentinelAddrs := make([]string, len(failoverOpt.SentinelAddrs))
|
|
|
|
copy(sentinelAddrs, failoverOpt.SentinelAddrs)
|
2014-05-11 18:11:55 +04:00
|
|
|
|
2021-02-10 18:25:09 +03:00
|
|
|
rand.Shuffle(len(sentinelAddrs), func(i, j int) {
|
|
|
|
sentinelAddrs[i], sentinelAddrs[j] = sentinelAddrs[j], sentinelAddrs[i]
|
|
|
|
})
|
|
|
|
|
2020-09-11 11:24:38 +03:00
|
|
|
failover := &sentinelFailover{
|
|
|
|
opt: failoverOpt,
|
|
|
|
sentinelAddrs: sentinelAddrs,
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
2016-06-05 14:10:30 +03:00
|
|
|
|
2020-09-11 11:24:38 +03:00
|
|
|
opt := failoverOpt.clientOptions()
|
|
|
|
opt.Dialer = masterSlaveDialer(failover)
|
|
|
|
opt.init()
|
2020-09-09 15:27:17 +03:00
|
|
|
|
|
|
|
connPool := newConnPool(opt)
|
2020-12-06 11:05:26 +03:00
|
|
|
|
2020-11-19 15:12:56 +03:00
|
|
|
failover.mu.Lock()
|
2020-09-09 15:27:17 +03:00
|
|
|
failover.onFailover = func(ctx context.Context, addr string) {
|
|
|
|
_ = connPool.Filter(func(cn *pool.Conn) bool {
|
|
|
|
return cn.RemoteAddr().String() != addr
|
|
|
|
})
|
|
|
|
}
|
2020-11-19 15:12:56 +03:00
|
|
|
failover.mu.Unlock()
|
2020-09-09 15:27:17 +03:00
|
|
|
|
2018-01-20 13:26:33 +03:00
|
|
|
c := Client{
|
2020-09-09 15:27:17 +03:00
|
|
|
baseClient: newBaseClient(opt, connPool),
|
2020-02-02 15:59:27 +03:00
|
|
|
ctx: context.Background(),
|
2016-03-09 16:14:01 +03:00
|
|
|
}
|
2019-08-24 12:22:52 +03:00
|
|
|
c.cmdable = c.Process
|
2020-02-02 15:59:27 +03:00
|
|
|
c.onClose = failover.Close
|
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
|
|
|
}
|
|
|
|
|
2020-09-09 16:20:58 +03:00
|
|
|
func masterSlaveDialer(
|
2020-09-11 11:24:38 +03:00
|
|
|
failover *sentinelFailover,
|
2020-09-09 16:20:58 +03:00
|
|
|
) func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
return func(ctx context.Context, network, _ string) (net.Conn, error) {
|
|
|
|
var addr string
|
|
|
|
var err error
|
|
|
|
|
2020-09-11 11:24:38 +03:00
|
|
|
if failover.opt.SlaveOnly {
|
2020-09-09 16:20:58 +03:00
|
|
|
addr, err = failover.RandomSlaveAddr(ctx)
|
|
|
|
} else {
|
|
|
|
addr, err = failover.MasterAddr(ctx)
|
|
|
|
if err == nil {
|
|
|
|
failover.trySwitchMaster(ctx, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if failover.opt.Dialer != nil {
|
|
|
|
return failover.opt.Dialer(ctx, network, addr)
|
|
|
|
}
|
2021-04-21 10:47:53 +03:00
|
|
|
|
|
|
|
netDialer := &net.Dialer{
|
|
|
|
Timeout: failover.opt.DialTimeout,
|
|
|
|
KeepAlive: 5 * time.Minute,
|
|
|
|
}
|
|
|
|
if failover.opt.TLSConfig == nil {
|
|
|
|
return netDialer.DialContext(ctx, network, addr)
|
|
|
|
}
|
|
|
|
return tls.DialWithDialer(netDialer, network, addr, failover.opt.TLSConfig)
|
2020-09-09 16:20:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2020-09-09 16:20:58 +03:00
|
|
|
// SentinelClient is a client for a Redis Sentinel.
|
2018-05-31 13:15:52 +03:00
|
|
|
type SentinelClient struct {
|
2019-05-31 17:37:34 +03:00
|
|
|
*baseClient
|
2020-09-17 12:27:16 +03:00
|
|
|
hooks
|
2019-05-31 16:36:57 +03:00
|
|
|
ctx context.Context
|
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{
|
2019-05-31 17:37:34 +03:00
|
|
|
baseClient: &baseClient{
|
2016-06-05 12:45:39 +03:00
|
|
|
opt: opt,
|
|
|
|
connPool: newConnPool(opt),
|
|
|
|
},
|
2019-07-04 11:18:06 +03:00
|
|
|
ctx: context.Background(),
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
2018-05-31 13:15:52 +03:00
|
|
|
return c
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
2019-05-31 16:36:57 +03:00
|
|
|
func (c *SentinelClient) Context() context.Context {
|
2019-07-04 11:18:06 +03:00
|
|
|
return c.ctx
|
2019-05-31 16:36:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SentinelClient) WithContext(ctx context.Context) *SentinelClient {
|
|
|
|
if ctx == nil {
|
|
|
|
panic("nil context")
|
|
|
|
}
|
|
|
|
clone := *c
|
2019-05-31 17:03:20 +03:00
|
|
|
clone.ctx = ctx
|
2019-05-31 16:36:57 +03:00
|
|
|
return &clone
|
|
|
|
}
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) Process(ctx context.Context, cmd Cmder) error {
|
2020-09-17 12:27:16 +03:00
|
|
|
return c.hooks.process(ctx, cmd, c.baseClient.process)
|
2019-05-31 16:36:57 +03: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,
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
newConn: func(ctx context.Context, channels []string) (*pool.Conn, error) {
|
|
|
|
return c.newConn(ctx)
|
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
|
|
|
}
|
|
|
|
|
2019-05-25 23:54:40 +03:00
|
|
|
// Ping is used to test if a connection is still alive, or to
|
|
|
|
// measure latency.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) Ping(ctx context.Context) *StringCmd {
|
|
|
|
cmd := NewStringCmd(ctx, "ping")
|
|
|
|
_ = c.Process(ctx, cmd)
|
2019-05-25 23:54:40 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
// Subscribe subscribes the client to the specified channels.
|
|
|
|
// Channels can be omitted to create empty subscription.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) Subscribe(ctx context.Context, channels ...string) *PubSub {
|
2018-10-01 11:38:21 +03:00
|
|
|
pubsub := c.pubSub()
|
|
|
|
if len(channels) > 0 {
|
2020-03-11 17:26:42 +03:00
|
|
|
_ = pubsub.Subscribe(ctx, channels...)
|
2018-10-01 11:38:21 +03:00
|
|
|
}
|
|
|
|
return pubsub
|
|
|
|
}
|
|
|
|
|
|
|
|
// PSubscribe subscribes the client to the given patterns.
|
|
|
|
// Patterns can be omitted to create empty subscription.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) PSubscribe(ctx context.Context, channels ...string) *PubSub {
|
2018-10-01 11:38:21 +03:00
|
|
|
pubsub := c.pubSub()
|
|
|
|
if len(channels) > 0 {
|
2020-03-11 17:26:42 +03:00
|
|
|
_ = pubsub.PSubscribe(ctx, channels...)
|
2018-10-01 11:38:21 +03:00
|
|
|
}
|
|
|
|
return pubsub
|
|
|
|
}
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) GetMasterAddrByName(ctx context.Context, name string) *StringSliceCmd {
|
|
|
|
cmd := NewStringSliceCmd(ctx, "sentinel", "get-master-addr-by-name", name)
|
|
|
|
_ = c.Process(ctx, cmd)
|
2014-05-11 18:11:55 +04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) Sentinels(ctx context.Context, name string) *SliceCmd {
|
|
|
|
cmd := NewSliceCmd(ctx, "sentinel", "sentinels", name)
|
|
|
|
_ = c.Process(ctx, cmd)
|
2014-05-11 18:11:55 +04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-02-20 13:39:33 +03:00
|
|
|
// Failover forces a failover as if the master was not reachable, and without
|
|
|
|
// asking for agreement to other Sentinels.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) Failover(ctx context.Context, name string) *StatusCmd {
|
|
|
|
cmd := NewStatusCmd(ctx, "sentinel", "failover", name)
|
|
|
|
_ = c.Process(ctx, cmd)
|
2019-02-20 13:39:33 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-02-20 18:19:42 +03:00
|
|
|
// 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.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) Reset(ctx context.Context, pattern string) *IntCmd {
|
|
|
|
cmd := NewIntCmd(ctx, "sentinel", "reset", pattern)
|
|
|
|
_ = c.Process(ctx, cmd)
|
2019-02-20 18:19:42 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-02-21 13:28:23 +03:00
|
|
|
// FlushConfig forces Sentinel to rewrite its configuration on disk, including
|
|
|
|
// the current Sentinel state.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) FlushConfig(ctx context.Context) *StatusCmd {
|
|
|
|
cmd := NewStatusCmd(ctx, "sentinel", "flushconfig")
|
|
|
|
_ = c.Process(ctx, cmd)
|
2019-02-21 13:28:23 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-02-21 20:13:04 +03:00
|
|
|
// Master shows the state and info of the specified master.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) Master(ctx context.Context, name string) *StringStringMapCmd {
|
|
|
|
cmd := NewStringStringMapCmd(ctx, "sentinel", "master", name)
|
|
|
|
_ = c.Process(ctx, cmd)
|
2019-02-21 20:13:04 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-05-25 23:31:06 +03:00
|
|
|
// Masters shows a list of monitored masters and their state.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) Masters(ctx context.Context) *SliceCmd {
|
|
|
|
cmd := NewSliceCmd(ctx, "sentinel", "masters")
|
|
|
|
_ = c.Process(ctx, cmd)
|
2019-05-25 23:31:06 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slaves shows a list of slaves for the specified master and their state.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) Slaves(ctx context.Context, name string) *SliceCmd {
|
|
|
|
cmd := NewSliceCmd(ctx, "sentinel", "slaves", name)
|
|
|
|
_ = c.Process(ctx, cmd)
|
2019-05-25 23:31:06 +03:00
|
|
|
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.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) CkQuorum(ctx context.Context, name string) *StringCmd {
|
|
|
|
cmd := NewStringCmd(ctx, "sentinel", "ckquorum", name)
|
|
|
|
_ = c.Process(ctx, cmd)
|
2019-05-25 23:31:06 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-05-25 23:54:40 +03:00
|
|
|
// Monitor tells the Sentinel to start monitoring a new master with the specified
|
2019-05-25 23:58:27 +03:00
|
|
|
// name, ip, port, and quorum.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) Monitor(ctx context.Context, name, ip, port, quorum string) *StringCmd {
|
|
|
|
cmd := NewStringCmd(ctx, "sentinel", "monitor", name, ip, port, quorum)
|
|
|
|
_ = c.Process(ctx, cmd)
|
2019-05-25 23:54:40 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set is used in order to change configuration parameters of a specific master.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) Set(ctx context.Context, name, option, value string) *StringCmd {
|
|
|
|
cmd := NewStringCmd(ctx, "sentinel", "set", name, option, value)
|
|
|
|
_ = c.Process(ctx, cmd)
|
2019-05-25 23:54:40 +03:00
|
|
|
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
|
2019-05-25 23:58:27 +03:00
|
|
|
// the Sentinel.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *SentinelClient) Remove(ctx context.Context, name string) *StringCmd {
|
|
|
|
cmd := NewStringCmd(ctx, "sentinel", "remove", name)
|
|
|
|
_ = c.Process(ctx, cmd)
|
2019-05-25 23:54:40 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-09-09 15:27:17 +03:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
type sentinelFailover struct {
|
2020-09-11 11:24:38 +03:00
|
|
|
opt *FailoverOptions
|
2014-05-11 18:11:55 +04:00
|
|
|
|
2020-09-11 11:24:38 +03:00
|
|
|
sentinelAddrs []string
|
2020-09-11 15:52:38 +03:00
|
|
|
|
|
|
|
onFailover func(ctx context.Context, addr string)
|
|
|
|
onUpdate func(ctx context.Context)
|
2014-05-11 18:11:55 +04:00
|
|
|
|
2017-06-29 16:53:49 +03:00
|
|
|
mu sync.RWMutex
|
|
|
|
_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
|
|
|
}
|
|
|
|
|
2019-10-08 12:43:00 +03: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
|
|
|
|
}
|
|
|
|
|
2020-09-04 12:54:06 +03:00
|
|
|
func (c *sentinelFailover) RandomSlaveAddr(ctx context.Context) (string, error) {
|
2021-02-10 18:20:08 +03:00
|
|
|
if c.opt == nil {
|
|
|
|
return "", errors.New("opt is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
addresses, err := c.slaveAddrs(ctx, false)
|
2020-09-04 12:54:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-02-10 18:20:08 +03:00
|
|
|
|
|
|
|
if len(addresses) == 0 && c.opt.UseDisconnectedSlaves {
|
|
|
|
addresses, err = c.slaveAddrs(ctx, true)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 16:20:58 +03:00
|
|
|
if len(addresses) == 0 {
|
2020-09-04 12:54:06 +03:00
|
|
|
return c.MasterAddr(ctx)
|
|
|
|
}
|
|
|
|
return addresses[rand.Intn(len(addresses))], nil
|
|
|
|
}
|
|
|
|
|
2020-09-09 15:27:17 +03:00
|
|
|
func (c *sentinelFailover) MasterAddr(ctx context.Context) (string, error) {
|
2019-10-08 12:43:00 +03:00
|
|
|
c.mu.RLock()
|
|
|
|
sentinel := c.sentinel
|
|
|
|
c.mu.RUnlock()
|
|
|
|
|
|
|
|
if sentinel != nil {
|
2020-03-11 17:26:42 +03:00
|
|
|
addr := c.getMasterAddr(ctx, sentinel)
|
2019-10-08 12:43:00 +03:00
|
|
|
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()
|
|
|
|
|
2019-10-08 12:43:00 +03:00
|
|
|
if c.sentinel != nil {
|
2020-03-11 17:26:42 +03:00
|
|
|
addr := c.getMasterAddr(ctx, c.sentinel)
|
2019-10-08 12:43:00 +03:00
|
|
|
if addr != "" {
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
_ = c.closeSentinel()
|
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
for i, sentinelAddr := range c.sentinelAddrs {
|
2020-09-11 11:24:38 +03:00
|
|
|
sentinel := NewSentinelClient(c.opt.sentinelOptions(sentinelAddr))
|
2014-05-11 18:11:55 +04:00
|
|
|
|
2020-09-11 11:24:38 +03:00
|
|
|
masterAddr, err := sentinel.GetMasterAddrByName(ctx, c.opt.MasterName).Result()
|
2014-05-11 18:11:55 +04:00
|
|
|
if err != nil {
|
2024-06-05 11:45:49 +03:00
|
|
|
internal.Logger.Printf("sentinel: GetMasterAddrByName master=%q failed: %s",
|
2020-09-11 11:24:38 +03:00
|
|
|
c.opt.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]
|
2020-03-11 17:26:42 +03:00
|
|
|
c.setSentinel(ctx, 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
|
|
|
}
|
|
|
|
|
2020-12-15 14:25:31 +03:00
|
|
|
return "", errors.New("redis: all sentinels specified in configuration are unreachable")
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
|
2021-02-10 18:20:08 +03:00
|
|
|
func (c *sentinelFailover) slaveAddrs(ctx context.Context, useDisconnected bool) ([]string, error) {
|
2020-09-04 12:54:06 +03:00
|
|
|
c.mu.RLock()
|
|
|
|
sentinel := c.sentinel
|
|
|
|
c.mu.RUnlock()
|
|
|
|
|
|
|
|
if sentinel != nil {
|
|
|
|
addrs := c.getSlaveAddrs(ctx, sentinel)
|
|
|
|
if len(addrs) > 0 {
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
if c.sentinel != nil {
|
|
|
|
addrs := c.getSlaveAddrs(ctx, c.sentinel)
|
|
|
|
if len(addrs) > 0 {
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
_ = c.closeSentinel()
|
|
|
|
}
|
2021-02-10 18:20:08 +03:00
|
|
|
|
|
|
|
var sentinelReachable bool
|
2020-09-04 12:54:06 +03:00
|
|
|
|
|
|
|
for i, sentinelAddr := range c.sentinelAddrs {
|
2020-09-11 11:24:38 +03:00
|
|
|
sentinel := NewSentinelClient(c.opt.sentinelOptions(sentinelAddr))
|
2020-09-04 12:54:06 +03:00
|
|
|
|
2020-09-11 11:24:38 +03:00
|
|
|
slaves, err := sentinel.Slaves(ctx, c.opt.MasterName).Result()
|
2020-09-04 12:54:06 +03:00
|
|
|
if err != nil {
|
2024-06-05 11:45:49 +03:00
|
|
|
internal.Logger.Printf("sentinel: Slaves master=%q failed: %s",
|
2020-09-11 11:24:38 +03:00
|
|
|
c.opt.MasterName, err)
|
2020-09-04 12:54:06 +03:00
|
|
|
_ = sentinel.Close()
|
|
|
|
continue
|
|
|
|
}
|
2021-02-10 18:20:08 +03:00
|
|
|
sentinelReachable = true
|
|
|
|
addrs := parseSlaveAddrs(slaves, useDisconnected)
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-04 12:54:06 +03:00
|
|
|
// Push working sentinel to the top.
|
|
|
|
c.sentinelAddrs[0], c.sentinelAddrs[i] = c.sentinelAddrs[i], c.sentinelAddrs[0]
|
|
|
|
c.setSentinel(ctx, sentinel)
|
|
|
|
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:20:08 +03:00
|
|
|
if sentinelReachable {
|
|
|
|
return []string{}, nil
|
|
|
|
}
|
2020-12-15 14:25:31 +03:00
|
|
|
return []string{}, errors.New("redis: all sentinels specified in configuration are unreachable")
|
2020-09-04 12:54:06 +03:00
|
|
|
}
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *sentinelFailover) getMasterAddr(ctx context.Context, sentinel *SentinelClient) string {
|
2020-09-11 11:24:38 +03:00
|
|
|
addr, err := sentinel.GetMasterAddrByName(ctx, c.opt.MasterName).Result()
|
2018-10-01 11:38:21 +03:00
|
|
|
if err != nil {
|
2024-06-05 11:45:49 +03:00
|
|
|
internal.Logger.Printf("sentinel: GetMasterAddrByName name=%q failed: %s",
|
2020-09-11 11:24:38 +03:00
|
|
|
c.opt.MasterName, err)
|
2018-10-01 11:38:21 +03:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return net.JoinHostPort(addr[0], addr[1])
|
2018-07-23 15:55:13 +03:00
|
|
|
}
|
|
|
|
|
2020-09-04 12:54:06 +03:00
|
|
|
func (c *sentinelFailover) getSlaveAddrs(ctx context.Context, sentinel *SentinelClient) []string {
|
2020-09-11 11:24:38 +03:00
|
|
|
addrs, err := sentinel.Slaves(ctx, c.opt.MasterName).Result()
|
2020-09-04 12:54:06 +03:00
|
|
|
if err != nil {
|
2024-06-05 11:45:49 +03:00
|
|
|
internal.Logger.Printf("sentinel: Slaves name=%q failed: %s",
|
2020-09-11 11:24:38 +03:00
|
|
|
c.opt.MasterName, err)
|
2020-09-04 12:54:06 +03:00
|
|
|
return []string{}
|
|
|
|
}
|
2021-02-10 18:20:08 +03:00
|
|
|
return parseSlaveAddrs(addrs, false)
|
2020-09-04 12:54:06 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 18:20:08 +03:00
|
|
|
func parseSlaveAddrs(addrs []interface{}, keepDisconnected bool) []string {
|
2020-09-11 15:52:38 +03:00
|
|
|
nodes := make([]string, 0, len(addrs))
|
2020-09-04 12:54:06 +03:00
|
|
|
for _, node := range addrs {
|
|
|
|
ip := ""
|
|
|
|
port := ""
|
|
|
|
flags := []string{}
|
|
|
|
lastkey := ""
|
|
|
|
isDown := false
|
|
|
|
|
|
|
|
for _, key := range node.([]interface{}) {
|
|
|
|
switch lastkey {
|
|
|
|
case "ip":
|
|
|
|
ip = key.(string)
|
|
|
|
case "port":
|
|
|
|
port = key.(string)
|
|
|
|
case "flags":
|
|
|
|
flags = strings.Split(key.(string), ",")
|
|
|
|
}
|
|
|
|
lastkey = key.(string)
|
|
|
|
}
|
2020-09-11 15:52:38 +03:00
|
|
|
|
2020-09-04 12:54:06 +03:00
|
|
|
for _, flag := range flags {
|
|
|
|
switch flag {
|
2021-02-10 18:20:08 +03:00
|
|
|
case "s_down", "o_down":
|
2020-09-04 12:54:06 +03:00
|
|
|
isDown = true
|
2021-02-10 18:20:08 +03:00
|
|
|
case "disconnected":
|
|
|
|
if !keepDisconnected {
|
|
|
|
isDown = true
|
|
|
|
}
|
2020-09-04 12:54:06 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-11 15:52:38 +03:00
|
|
|
|
2020-09-04 12:54:06 +03:00
|
|
|
if !isDown {
|
|
|
|
nodes = append(nodes, net.JoinHostPort(ip, port))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2020-09-09 15:27:17 +03:00
|
|
|
func (c *sentinelFailover) trySwitchMaster(ctx context.Context, addr string) {
|
2018-10-01 11:38:21 +03:00
|
|
|
c.mu.RLock()
|
2021-03-23 11:55:14 +03:00
|
|
|
currentAddr := c._masterAddr //nolint:ifshort
|
2018-10-01 11:38:21 +03:00
|
|
|
c.mu.RUnlock()
|
2020-09-09 15:27:17 +03:00
|
|
|
|
|
|
|
if addr == currentAddr {
|
2018-07-23 15:55:13 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-01 11:38:21 +03:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
2020-09-09 15:27:17 +03:00
|
|
|
if addr == c._masterAddr {
|
2019-10-08 12:43:00 +03:00
|
|
|
return
|
|
|
|
}
|
2020-09-09 15:27:17 +03:00
|
|
|
c._masterAddr = addr
|
2019-10-08 12:43:00 +03:00
|
|
|
|
2024-06-05 11:45:49 +03:00
|
|
|
internal.Logger.Printf("sentinel: new master=%q addr=%q",
|
2020-09-11 11:24:38 +03:00
|
|
|
c.opt.MasterName, addr)
|
2020-09-11 15:52:38 +03:00
|
|
|
if c.onFailover != nil {
|
|
|
|
c.onFailover(ctx, addr)
|
|
|
|
}
|
2017-06-29 16:53:49 +03:00
|
|
|
}
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *sentinelFailover) setSentinel(ctx context.Context, sentinel *SentinelClient) {
|
2019-10-08 12:43:00 +03:00
|
|
|
if c.sentinel != nil {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
2018-10-01 11:38:21 +03:00
|
|
|
c.sentinel = sentinel
|
2020-03-11 17:26:42 +03:00
|
|
|
c.discoverSentinels(ctx)
|
2018-11-11 13:13:00 +03:00
|
|
|
|
2020-09-11 15:52:38 +03:00
|
|
|
c.pubsub = sentinel.Subscribe(ctx, "+switch-master", "+slave-reconf-done")
|
2018-11-11 13:13:00 +03:00
|
|
|
go c.listen(c.pubsub)
|
2016-03-09 16:14:01 +03:00
|
|
|
}
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *sentinelFailover) discoverSentinels(ctx context.Context) {
|
2020-09-11 11:24:38 +03:00
|
|
|
sentinels, err := c.sentinel.Sentinels(ctx, c.opt.MasterName).Result()
|
2014-05-11 18:11:55 +04:00
|
|
|
if err != nil {
|
2024-06-05 11:45:49 +03:00
|
|
|
internal.Logger.Printf("sentinel: Sentinels master=%q failed: %s", c.opt.MasterName, err)
|
2014-05-11 18:11:55 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, sentinel := range sentinels {
|
|
|
|
vals := sentinel.([]interface{})
|
2021-03-12 18:20:58 +03:00
|
|
|
var ip, port string
|
2014-05-11 18:11:55 +04:00
|
|
|
for i := 0; i < len(vals); i += 2 {
|
|
|
|
key := vals[i].(string)
|
2021-03-12 18:20:58 +03:00
|
|
|
switch key {
|
|
|
|
case "ip":
|
|
|
|
ip = vals[i+1].(string)
|
|
|
|
case "port":
|
|
|
|
port = vals[i+1].(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ip != "" && port != "" {
|
|
|
|
sentinelAddr := net.JoinHostPort(ip, port)
|
|
|
|
if !contains(c.sentinelAddrs, sentinelAddr) {
|
2024-06-05 11:45:49 +03:00
|
|
|
internal.Logger.Printf("sentinel: discovered new sentinel=%q for master=%q",
|
2021-03-12 18:20:58 +03:00
|
|
|
sentinelAddr, c.opt.MasterName)
|
|
|
|
c.sentinelAddrs = append(c.sentinelAddrs, sentinelAddr)
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 13:13:00 +03:00
|
|
|
func (c *sentinelFailover) listen(pubsub *PubSub) {
|
2020-09-11 15:52:38 +03:00
|
|
|
ctx := context.TODO()
|
2020-12-06 11:05:26 +03:00
|
|
|
|
2020-09-11 15:52:38 +03:00
|
|
|
if c.onUpdate != nil {
|
|
|
|
c.onUpdate(ctx)
|
|
|
|
}
|
2014-05-11 18:11:55 +04:00
|
|
|
|
2020-09-11 15:52:38 +03:00
|
|
|
ch := pubsub.Channel()
|
|
|
|
for msg := range ch {
|
2019-04-17 16:14:30 +03:00
|
|
|
if msg.Channel == "+switch-master" {
|
2016-03-09 16:14:01 +03:00
|
|
|
parts := strings.Split(msg.Payload, " ")
|
2020-09-11 11:24:38 +03:00
|
|
|
if parts[0] != c.opt.MasterName {
|
2024-06-05 11:45:49 +03:00
|
|
|
internal.Logger.Printf("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])
|
2020-09-09 15:27:17 +03:00
|
|
|
c.trySwitchMaster(pubsub.getContext(), addr)
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
2020-09-11 15:52:38 +03:00
|
|
|
|
|
|
|
if c.onUpdate != nil {
|
|
|
|
c.onUpdate(ctx)
|
|
|
|
}
|
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
|
|
|
|
}
|
2020-09-09 15:27:17 +03:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2020-09-11 15:52:38 +03:00
|
|
|
// NewFailoverClusterClient returns a client that supports routing read-only commands
|
|
|
|
// to a slave node.
|
2020-09-09 15:27:17 +03:00
|
|
|
func NewFailoverClusterClient(failoverOpt *FailoverOptions) *ClusterClient {
|
2020-09-11 11:24:38 +03:00
|
|
|
sentinelAddrs := make([]string, len(failoverOpt.SentinelAddrs))
|
|
|
|
copy(sentinelAddrs, failoverOpt.SentinelAddrs)
|
2020-09-09 15:27:17 +03:00
|
|
|
|
2020-09-11 11:24:38 +03:00
|
|
|
failover := &sentinelFailover{
|
|
|
|
opt: failoverOpt,
|
|
|
|
sentinelAddrs: sentinelAddrs,
|
2020-09-09 15:27:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
opt := failoverOpt.clusterOptions()
|
|
|
|
opt.ClusterSlots = func(ctx context.Context) ([]ClusterSlot, error) {
|
|
|
|
masterAddr, err := failover.MasterAddr(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := []ClusterNode{{
|
|
|
|
Addr: masterAddr,
|
|
|
|
}}
|
|
|
|
|
2021-02-10 18:20:08 +03:00
|
|
|
slaveAddrs, err := failover.slaveAddrs(ctx, false)
|
2020-09-09 15:27:17 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, slaveAddr := range slaveAddrs {
|
|
|
|
nodes = append(nodes, ClusterNode{
|
|
|
|
Addr: slaveAddr,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
slots := []ClusterSlot{
|
|
|
|
{
|
|
|
|
Start: 0,
|
|
|
|
End: 16383,
|
|
|
|
Nodes: nodes,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return slots, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := NewClusterClient(opt)
|
2020-12-06 11:05:26 +03:00
|
|
|
|
|
|
|
failover.mu.Lock()
|
2020-09-11 15:52:38 +03:00
|
|
|
failover.onUpdate = func(ctx context.Context) {
|
|
|
|
c.ReloadState(ctx)
|
2020-09-09 15:27:17 +03:00
|
|
|
}
|
2020-12-06 11:05:26 +03:00
|
|
|
failover.mu.Unlock()
|
2020-09-09 15:27:17 +03:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|