2015-01-24 15:12:48 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2018-03-07 16:39:56 +03:00
|
|
|
"context"
|
2018-05-11 09:46:09 +03:00
|
|
|
"crypto/tls"
|
2016-12-13 18:28:39 +03:00
|
|
|
"fmt"
|
2018-02-12 17:15:40 +03:00
|
|
|
"math"
|
2015-01-24 15:12:48 +03:00
|
|
|
"math/rand"
|
2017-06-30 00:43:19 +03:00
|
|
|
"net"
|
2018-08-04 12:19:19 +03:00
|
|
|
"runtime"
|
2018-07-22 10:50:26 +03:00
|
|
|
"sort"
|
2015-01-24 15:12:48 +03:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2015-12-30 16:53:45 +03:00
|
|
|
|
2017-02-18 17:42:34 +03:00
|
|
|
"github.com/go-redis/redis/internal"
|
|
|
|
"github.com/go-redis/redis/internal/hashtag"
|
|
|
|
"github.com/go-redis/redis/internal/pool"
|
|
|
|
"github.com/go-redis/redis/internal/proto"
|
2015-01-24 15:12:48 +03:00
|
|
|
)
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
var errClusterNoNodes = fmt.Errorf("redis: cluster has no nodes")
|
2016-10-09 14:12:32 +03:00
|
|
|
|
2016-10-09 13:30:45 +03:00
|
|
|
// ClusterOptions are used to configure a cluster client and should be
|
|
|
|
// passed to NewClusterClient.
|
|
|
|
type ClusterOptions struct {
|
|
|
|
// A seed list of host:port addresses of cluster nodes.
|
|
|
|
Addrs []string
|
|
|
|
|
|
|
|
// The maximum number of retries before giving up. Command is retried
|
|
|
|
// on network errors and MOVED/ASK redirects.
|
2018-06-29 10:45:05 +03:00
|
|
|
// Default is 8 retries.
|
2016-10-09 13:30:45 +03:00
|
|
|
MaxRedirects int
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
// Enables read-only commands on slave nodes.
|
2016-10-09 13:30:45 +03:00
|
|
|
ReadOnly bool
|
2017-07-09 13:10:07 +03:00
|
|
|
// Allows routing read-only commands to the closest master or slave node.
|
2018-05-17 16:09:56 +03:00
|
|
|
// It automatically enables ReadOnly.
|
2016-10-09 13:30:45 +03:00
|
|
|
RouteByLatency bool
|
2018-03-07 13:23:38 +03:00
|
|
|
// Allows routing read-only commands to the random master or slave node.
|
2018-06-29 10:45:05 +03:00
|
|
|
// It automatically enables ReadOnly.
|
2018-03-07 13:23:38 +03:00
|
|
|
RouteRandomly bool
|
2016-10-09 13:30:45 +03:00
|
|
|
|
2018-07-18 15:28:51 +03:00
|
|
|
// Optional function that returns cluster slots information.
|
2018-06-29 10:45:05 +03:00
|
|
|
// It is useful to manually create cluster of standalone Redis servers
|
2018-07-18 15:28:51 +03:00
|
|
|
// and load-balance read/write operations between master and slaves.
|
|
|
|
// It can use service like ZooKeeper to maintain configuration information
|
|
|
|
// and Cluster.ReloadState to manually trigger state reloading.
|
2018-06-29 10:45:05 +03:00
|
|
|
ClusterSlots func() ([]ClusterSlot, error)
|
|
|
|
|
2018-10-14 10:53:48 +03:00
|
|
|
// Optional hook that is called when a new node is created.
|
|
|
|
OnNewNode func(*Client)
|
|
|
|
|
2016-10-09 13:30:45 +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)
|
2019-05-18 14:00:07 +03:00
|
|
|
|
2017-05-25 14:16:39 +03:00
|
|
|
OnConnect func(*Conn) error
|
|
|
|
|
2018-08-14 16:16:27 +03:00
|
|
|
Password string
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
MaxRetries int
|
|
|
|
MinRetryBackoff time.Duration
|
|
|
|
MaxRetryBackoff time.Duration
|
2016-10-09 13:30:45 +03:00
|
|
|
|
|
|
|
DialTimeout time.Duration
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
WriteTimeout time.Duration
|
|
|
|
|
|
|
|
// PoolSize applies per cluster node and not for the whole cluster.
|
|
|
|
PoolSize int
|
2018-08-12 10:08:21 +03:00
|
|
|
MinIdleConns int
|
|
|
|
MaxConnAge time.Duration
|
2016-10-09 13:30:45 +03:00
|
|
|
PoolTimeout time.Duration
|
|
|
|
IdleTimeout time.Duration
|
|
|
|
IdleCheckFrequency time.Duration
|
2018-05-11 09:46:09 +03:00
|
|
|
|
|
|
|
TLSConfig *tls.Config
|
2016-10-09 13:30:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (opt *ClusterOptions) init() {
|
|
|
|
if opt.MaxRedirects == -1 {
|
|
|
|
opt.MaxRedirects = 0
|
|
|
|
} else if opt.MaxRedirects == 0 {
|
2018-02-27 16:01:10 +03:00
|
|
|
opt.MaxRedirects = 8
|
2016-10-09 13:30:45 +03:00
|
|
|
}
|
|
|
|
|
2018-09-20 12:49:43 +03:00
|
|
|
if (opt.RouteByLatency || opt.RouteRandomly) && opt.ClusterSlots == nil {
|
2016-10-09 13:30:45 +03:00
|
|
|
opt.ReadOnly = true
|
|
|
|
}
|
2017-07-09 13:10:07 +03:00
|
|
|
|
2018-08-04 12:19:19 +03:00
|
|
|
if opt.PoolSize == 0 {
|
|
|
|
opt.PoolSize = 5 * runtime.NumCPU()
|
|
|
|
}
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
switch opt.ReadTimeout {
|
|
|
|
case -1:
|
|
|
|
opt.ReadTimeout = 0
|
|
|
|
case 0:
|
|
|
|
opt.ReadTimeout = 3 * time.Second
|
|
|
|
}
|
|
|
|
switch opt.WriteTimeout {
|
|
|
|
case -1:
|
|
|
|
opt.WriteTimeout = 0
|
|
|
|
case 0:
|
|
|
|
opt.WriteTimeout = opt.ReadTimeout
|
|
|
|
}
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
switch opt.MinRetryBackoff {
|
|
|
|
case -1:
|
|
|
|
opt.MinRetryBackoff = 0
|
|
|
|
case 0:
|
|
|
|
opt.MinRetryBackoff = 8 * time.Millisecond
|
|
|
|
}
|
|
|
|
switch opt.MaxRetryBackoff {
|
|
|
|
case -1:
|
|
|
|
opt.MaxRetryBackoff = 0
|
|
|
|
case 0:
|
|
|
|
opt.MaxRetryBackoff = 512 * time.Millisecond
|
|
|
|
}
|
2016-10-09 13:30:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (opt *ClusterOptions) clientOptions() *Options {
|
|
|
|
const disableIdleCheck = -1
|
|
|
|
|
|
|
|
return &Options{
|
2019-05-18 14:00:07 +03:00
|
|
|
Dialer: opt.Dialer,
|
2017-05-25 14:16:39 +03:00
|
|
|
OnConnect: opt.OnConnect,
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
MaxRetries: opt.MaxRetries,
|
|
|
|
MinRetryBackoff: opt.MinRetryBackoff,
|
|
|
|
MaxRetryBackoff: opt.MaxRetryBackoff,
|
|
|
|
Password: opt.Password,
|
2017-07-25 04:35:41 +03:00
|
|
|
readOnly: opt.ReadOnly,
|
2016-10-09 13:30:45 +03:00
|
|
|
|
|
|
|
DialTimeout: opt.DialTimeout,
|
|
|
|
ReadTimeout: opt.ReadTimeout,
|
|
|
|
WriteTimeout: opt.WriteTimeout,
|
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
PoolSize: opt.PoolSize,
|
|
|
|
MinIdleConns: opt.MinIdleConns,
|
|
|
|
MaxConnAge: opt.MaxConnAge,
|
|
|
|
PoolTimeout: opt.PoolTimeout,
|
|
|
|
IdleTimeout: opt.IdleTimeout,
|
2016-10-09 13:30:45 +03:00
|
|
|
IdleCheckFrequency: disableIdleCheck,
|
2018-05-11 09:46:09 +03:00
|
|
|
|
|
|
|
TLSConfig: opt.TLSConfig,
|
2016-10-09 13:30:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-05-06 21:12:31 +03:00
|
|
|
type clusterNode struct {
|
2018-02-12 17:15:40 +03:00
|
|
|
Client *Client
|
2017-07-09 13:10:07 +03:00
|
|
|
|
2018-02-12 17:15:40 +03:00
|
|
|
latency uint32 // atomic
|
2018-02-15 14:00:54 +03:00
|
|
|
generation uint32 // atomic
|
2019-06-16 13:58:15 +03:00
|
|
|
failing uint32 // atomic
|
2016-10-09 11:18:57 +03:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func newClusterNode(clOpt *ClusterOptions, addr string) *clusterNode {
|
|
|
|
opt := clOpt.clientOptions()
|
|
|
|
opt.Addr = addr
|
|
|
|
node := clusterNode{
|
|
|
|
Client: NewClient(opt),
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:15:40 +03:00
|
|
|
node.latency = math.MaxUint32
|
2016-11-08 12:46:44 +03:00
|
|
|
if clOpt.RouteByLatency {
|
2018-02-12 17:15:40 +03:00
|
|
|
go node.updateLatency()
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
|
|
|
|
2018-10-14 10:53:48 +03:00
|
|
|
if clOpt.OnNewNode != nil {
|
|
|
|
clOpt.OnNewNode(node.Client)
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
return &node
|
|
|
|
}
|
|
|
|
|
2018-05-17 16:09:56 +03:00
|
|
|
func (n *clusterNode) String() string {
|
|
|
|
return n.Client.String()
|
|
|
|
}
|
|
|
|
|
2018-02-15 14:00:54 +03:00
|
|
|
func (n *clusterNode) Close() error {
|
|
|
|
return n.Client.Close()
|
|
|
|
}
|
|
|
|
|
2016-12-16 17:26:48 +03:00
|
|
|
func (n *clusterNode) updateLatency() {
|
|
|
|
const probes = 10
|
2018-02-12 17:15:40 +03:00
|
|
|
|
|
|
|
var latency uint32
|
2016-12-16 17:26:48 +03:00
|
|
|
for i := 0; i < probes; i++ {
|
|
|
|
start := time.Now()
|
|
|
|
n.Client.Ping()
|
2018-02-12 17:15:40 +03:00
|
|
|
probe := uint32(time.Since(start) / time.Microsecond)
|
|
|
|
latency = (latency + probe) / 2
|
2016-12-16 17:26:48 +03:00
|
|
|
}
|
2018-02-12 17:15:40 +03:00
|
|
|
atomic.StoreUint32(&n.latency, latency)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *clusterNode) Latency() time.Duration {
|
|
|
|
latency := atomic.LoadUint32(&n.latency)
|
|
|
|
return time.Duration(latency) * time.Microsecond
|
|
|
|
}
|
|
|
|
|
2019-06-16 13:58:15 +03:00
|
|
|
func (n *clusterNode) MarkAsFailing() {
|
|
|
|
atomic.StoreUint32(&n.failing, uint32(time.Now().Unix()))
|
2016-12-16 17:26:48 +03:00
|
|
|
}
|
|
|
|
|
2019-06-16 13:58:15 +03:00
|
|
|
func (n *clusterNode) Failing() bool {
|
|
|
|
const timeout = 15 // 15 seconds
|
2018-02-12 17:15:40 +03:00
|
|
|
|
2019-06-16 13:58:15 +03:00
|
|
|
failing := atomic.LoadUint32(&n.failing)
|
|
|
|
if failing == 0 {
|
2018-02-12 17:15:40 +03:00
|
|
|
return false
|
|
|
|
}
|
2019-06-16 13:58:15 +03:00
|
|
|
if time.Now().Unix()-int64(failing) < timeout {
|
2018-02-12 17:15:40 +03:00
|
|
|
return true
|
|
|
|
}
|
2019-06-16 13:58:15 +03:00
|
|
|
atomic.StoreUint32(&n.failing, 0)
|
2018-02-12 17:15:40 +03:00
|
|
|
return false
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
func (n *clusterNode) Generation() uint32 {
|
2018-02-15 14:00:54 +03:00
|
|
|
return atomic.LoadUint32(&n.generation)
|
2017-07-09 13:10:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *clusterNode) SetGeneration(gen uint32) {
|
2018-02-15 14:00:54 +03:00
|
|
|
for {
|
|
|
|
v := atomic.LoadUint32(&n.generation)
|
|
|
|
if gen < v || atomic.CompareAndSwapUint32(&n.generation, v, gen) {
|
|
|
|
break
|
|
|
|
}
|
2017-07-09 13:10:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
//------------------------------------------------------------------------------
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
type clusterNodes struct {
|
2016-03-17 19:00:47 +03:00
|
|
|
opt *ClusterOptions
|
|
|
|
|
2018-02-27 15:35:03 +03:00
|
|
|
mu sync.RWMutex
|
|
|
|
allAddrs []string
|
|
|
|
allNodes map[string]*clusterNode
|
|
|
|
clusterAddrs []string
|
|
|
|
closed bool
|
2018-02-15 14:00:54 +03:00
|
|
|
|
2018-05-17 14:36:51 +03:00
|
|
|
_generation uint32 // atomic
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func newClusterNodes(opt *ClusterOptions) *clusterNodes {
|
|
|
|
return &clusterNodes{
|
2018-02-15 14:00:54 +03:00
|
|
|
opt: opt,
|
|
|
|
|
|
|
|
allAddrs: opt.Addrs,
|
2018-02-27 15:35:03 +03:00
|
|
|
allNodes: make(map[string]*clusterNode),
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
2016-05-06 21:12:31 +03:00
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func (c *clusterNodes) Close() error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2016-10-02 15:44:01 +03:00
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
if c.closed {
|
|
|
|
return nil
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
2016-11-08 12:46:44 +03:00
|
|
|
c.closed = true
|
2016-05-06 21:12:31 +03:00
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
var firstErr error
|
2018-02-27 15:35:03 +03:00
|
|
|
for _, node := range c.allNodes {
|
2016-11-08 12:46:44 +03:00
|
|
|
if err := node.Client.Close(); err != nil && firstErr == nil {
|
|
|
|
firstErr = err
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
2016-10-09 13:30:45 +03:00
|
|
|
}
|
2018-02-15 14:00:54 +03:00
|
|
|
|
2018-02-27 15:35:03 +03:00
|
|
|
c.allNodes = nil
|
|
|
|
c.clusterAddrs = nil
|
2016-11-08 12:46:44 +03:00
|
|
|
|
|
|
|
return firstErr
|
2016-03-17 19:00:47 +03:00
|
|
|
}
|
|
|
|
|
2017-09-11 08:58:56 +03:00
|
|
|
func (c *clusterNodes) Addrs() ([]string, error) {
|
2018-02-15 14:00:54 +03:00
|
|
|
var addrs []string
|
2017-08-31 15:22:47 +03:00
|
|
|
c.mu.RLock()
|
2017-09-11 08:58:56 +03:00
|
|
|
closed := c.closed
|
2018-02-15 14:00:54 +03:00
|
|
|
if !closed {
|
2018-02-27 15:35:03 +03:00
|
|
|
if len(c.clusterAddrs) > 0 {
|
|
|
|
addrs = c.clusterAddrs
|
2018-02-15 14:00:54 +03:00
|
|
|
} else {
|
|
|
|
addrs = c.allAddrs
|
|
|
|
}
|
|
|
|
}
|
2017-09-11 08:58:56 +03:00
|
|
|
c.mu.RUnlock()
|
2017-08-31 15:22:47 +03:00
|
|
|
|
2017-09-11 08:58:56 +03:00
|
|
|
if closed {
|
|
|
|
return nil, pool.ErrClosed
|
2017-08-31 15:22:47 +03:00
|
|
|
}
|
2017-09-11 08:58:56 +03:00
|
|
|
if len(addrs) == 0 {
|
|
|
|
return nil, errClusterNoNodes
|
2017-08-31 15:22:47 +03:00
|
|
|
}
|
2017-09-11 08:58:56 +03:00
|
|
|
return addrs, nil
|
2017-08-31 15:22:47 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
func (c *clusterNodes) NextGeneration() uint32 {
|
2018-05-17 14:36:51 +03:00
|
|
|
return atomic.AddUint32(&c._generation, 1)
|
2017-07-09 13:10:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GC removes unused nodes.
|
2018-01-17 13:55:20 +03:00
|
|
|
func (c *clusterNodes) GC(generation uint32) {
|
2017-07-09 13:10:07 +03:00
|
|
|
var collected []*clusterNode
|
|
|
|
c.mu.Lock()
|
2018-02-27 15:35:03 +03:00
|
|
|
for addr, node := range c.allNodes {
|
2017-07-09 13:10:07 +03:00
|
|
|
if node.Generation() >= generation {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-02-27 15:35:03 +03:00
|
|
|
c.clusterAddrs = remove(c.clusterAddrs, addr)
|
|
|
|
delete(c.allNodes, addr)
|
2017-07-09 13:10:07 +03:00
|
|
|
collected = append(collected, node)
|
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
2018-02-27 15:35:03 +03:00
|
|
|
for _, node := range collected {
|
|
|
|
_ = node.Client.Close()
|
|
|
|
}
|
2017-07-09 13:10:07 +03:00
|
|
|
}
|
|
|
|
|
2018-05-17 15:21:51 +03:00
|
|
|
func (c *clusterNodes) Get(addr string) (*clusterNode, error) {
|
2016-11-08 12:46:44 +03:00
|
|
|
var node *clusterNode
|
2018-02-27 15:35:03 +03:00
|
|
|
var err error
|
2016-05-06 21:12:31 +03:00
|
|
|
c.mu.RLock()
|
2018-02-27 15:35:03 +03:00
|
|
|
if c.closed {
|
|
|
|
err = pool.ErrClosed
|
|
|
|
} else {
|
|
|
|
node = c.allNodes[addr]
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
2016-05-06 21:12:31 +03:00
|
|
|
c.mu.RUnlock()
|
2018-05-17 15:21:51 +03:00
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clusterNodes) GetOrCreate(addr string) (*clusterNode, error) {
|
|
|
|
node, err := c.Get(addr)
|
2018-02-27 15:35:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if node != nil {
|
2016-06-05 14:30:56 +03:00
|
|
|
return node, nil
|
2016-03-17 19:00:47 +03:00
|
|
|
}
|
|
|
|
|
2016-05-06 21:12:31 +03:00
|
|
|
c.mu.Lock()
|
2016-11-08 12:46:44 +03:00
|
|
|
defer c.mu.Unlock()
|
2016-06-05 14:30:56 +03:00
|
|
|
|
|
|
|
if c.closed {
|
|
|
|
return nil, pool.ErrClosed
|
|
|
|
}
|
|
|
|
|
2018-02-27 15:35:03 +03:00
|
|
|
node, ok := c.allNodes[addr]
|
2016-11-08 12:46:44 +03:00
|
|
|
if ok {
|
2018-02-27 15:35:03 +03:00
|
|
|
return node, err
|
2015-03-18 13:41:24 +03:00
|
|
|
}
|
2019-01-08 10:25:41 +03:00
|
|
|
|
|
|
|
node = newClusterNode(c.opt, addr)
|
2015-03-18 13:41:24 +03:00
|
|
|
|
2018-02-15 14:00:54 +03:00
|
|
|
c.allAddrs = appendIfNotExists(c.allAddrs, addr)
|
2019-01-08 12:15:59 +03:00
|
|
|
c.clusterAddrs = append(c.clusterAddrs, addr)
|
2018-02-27 15:35:03 +03:00
|
|
|
c.allNodes[addr] = node
|
2018-02-15 14:00:54 +03:00
|
|
|
|
2018-02-27 15:35:03 +03:00
|
|
|
return node, err
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2018-03-06 18:10:01 +03:00
|
|
|
func (c *clusterNodes) All() ([]*clusterNode, error) {
|
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
|
|
|
|
if c.closed {
|
|
|
|
return nil, pool.ErrClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
cp := make([]*clusterNode, 0, len(c.allNodes))
|
|
|
|
for _, node := range c.allNodes {
|
|
|
|
cp = append(cp, node)
|
|
|
|
}
|
|
|
|
return cp, nil
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func (c *clusterNodes) Random() (*clusterNode, error) {
|
2017-09-11 08:58:56 +03:00
|
|
|
addrs, err := c.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
|
|
|
|
2018-02-15 14:00:54 +03:00
|
|
|
n := rand.Intn(len(addrs))
|
|
|
|
return c.GetOrCreate(addrs[n])
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2018-07-22 10:50:26 +03:00
|
|
|
type clusterSlot struct {
|
|
|
|
start, end int
|
|
|
|
nodes []*clusterNode
|
|
|
|
}
|
|
|
|
|
|
|
|
type clusterSlotSlice []*clusterSlot
|
|
|
|
|
|
|
|
func (p clusterSlotSlice) Len() int {
|
|
|
|
return len(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p clusterSlotSlice) Less(i, j int) bool {
|
|
|
|
return p[i].start < p[j].start
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p clusterSlotSlice) Swap(i, j int) {
|
|
|
|
p[i], p[j] = p[j], p[i]
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
type clusterState struct {
|
2017-07-09 13:10:07 +03:00
|
|
|
nodes *clusterNodes
|
2018-05-17 16:09:56 +03:00
|
|
|
Masters []*clusterNode
|
|
|
|
Slaves []*clusterNode
|
2017-07-09 13:10:07 +03:00
|
|
|
|
2018-07-22 10:50:26 +03:00
|
|
|
slots []*clusterSlot
|
2017-07-09 13:10:07 +03:00
|
|
|
|
|
|
|
generation uint32
|
2018-05-21 16:33:41 +03:00
|
|
|
createdAt time.Time
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
|
|
|
|
2018-05-17 16:09:56 +03:00
|
|
|
func newClusterState(
|
|
|
|
nodes *clusterNodes, slots []ClusterSlot, origin string,
|
|
|
|
) (*clusterState, error) {
|
2016-11-08 12:46:44 +03:00
|
|
|
c := clusterState{
|
2018-05-21 16:33:41 +03:00
|
|
|
nodes: nodes,
|
2017-07-09 13:10:07 +03:00
|
|
|
|
2018-07-22 10:50:26 +03:00
|
|
|
slots: make([]*clusterSlot, 0, len(slots)),
|
2018-05-21 16:33:41 +03:00
|
|
|
|
|
|
|
generation: nodes.NextGeneration(),
|
|
|
|
createdAt: time.Now(),
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 15:56:43 +03:00
|
|
|
originHost, _, _ := net.SplitHostPort(origin)
|
|
|
|
isLoopbackOrigin := isLoopback(originHost)
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
for _, slot := range slots {
|
|
|
|
var nodes []*clusterNode
|
2017-07-09 13:10:07 +03:00
|
|
|
for i, slotNode := range slot.Nodes {
|
2017-06-30 00:43:19 +03:00
|
|
|
addr := slotNode.Addr
|
2018-08-14 15:56:43 +03:00
|
|
|
if !isLoopbackOrigin {
|
|
|
|
addr = replaceLoopbackHost(addr, originHost)
|
2017-06-30 00:43:19 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
node, err := c.nodes.GetOrCreate(addr)
|
2016-11-08 12:46:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-09 13:10:07 +03:00
|
|
|
|
|
|
|
node.SetGeneration(c.generation)
|
2016-11-08 12:46:44 +03:00
|
|
|
nodes = append(nodes, node)
|
2017-07-09 13:10:07 +03:00
|
|
|
|
|
|
|
if i == 0 {
|
2018-05-17 16:09:56 +03:00
|
|
|
c.Masters = appendUniqueNode(c.Masters, node)
|
2017-07-09 13:10:07 +03:00
|
|
|
} else {
|
2018-05-17 16:09:56 +03:00
|
|
|
c.Slaves = appendUniqueNode(c.Slaves, node)
|
2017-07-09 13:10:07 +03:00
|
|
|
}
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
|
|
|
|
2018-07-22 10:50:26 +03:00
|
|
|
c.slots = append(c.slots, &clusterSlot{
|
|
|
|
start: slot.Start,
|
|
|
|
end: slot.End,
|
|
|
|
nodes: nodes,
|
|
|
|
})
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
|
|
|
|
2018-07-22 10:50:26 +03:00
|
|
|
sort.Sort(clusterSlotSlice(c.slots))
|
|
|
|
|
2018-03-06 18:10:01 +03:00
|
|
|
time.AfterFunc(time.Minute, func() {
|
|
|
|
nodes.GC(c.generation)
|
|
|
|
})
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
return &c, nil
|
|
|
|
}
|
|
|
|
|
2018-08-14 15:56:43 +03:00
|
|
|
func replaceLoopbackHost(nodeAddr, originHost string) string {
|
|
|
|
nodeHost, nodePort, err := net.SplitHostPort(nodeAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nodeAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeIP := net.ParseIP(nodeHost)
|
|
|
|
if nodeIP == nil {
|
|
|
|
return nodeAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
if !nodeIP.IsLoopback() {
|
|
|
|
return nodeAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use origin host which is not loopback and node port.
|
|
|
|
return net.JoinHostPort(originHost, nodePort)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLoopback(host string) bool {
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
if ip == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return ip.IsLoopback()
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func (c *clusterState) slotMasterNode(slot int) (*clusterNode, error) {
|
2016-05-06 21:12:31 +03:00
|
|
|
nodes := c.slotNodes(slot)
|
2016-12-16 15:05:21 +03:00
|
|
|
if len(nodes) > 0 {
|
|
|
|
return nodes[0], nil
|
2015-05-05 12:48:49 +03:00
|
|
|
}
|
2016-12-16 15:05:21 +03:00
|
|
|
return c.nodes.Random()
|
2015-05-05 12:48:49 +03:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func (c *clusterState) slotSlaveNode(slot int) (*clusterNode, error) {
|
2016-05-06 21:12:31 +03:00
|
|
|
nodes := c.slotNodes(slot)
|
|
|
|
switch len(nodes) {
|
|
|
|
case 0:
|
2016-11-08 12:46:44 +03:00
|
|
|
return c.nodes.Random()
|
2016-05-06 21:12:31 +03:00
|
|
|
case 1:
|
2016-06-05 14:30:56 +03:00
|
|
|
return nodes[0], nil
|
2016-05-06 21:12:31 +03:00
|
|
|
case 2:
|
2019-06-16 13:58:15 +03:00
|
|
|
if slave := nodes[1]; !slave.Failing() {
|
2016-10-09 11:18:57 +03:00
|
|
|
return slave, nil
|
|
|
|
}
|
|
|
|
return nodes[0], nil
|
2016-05-06 21:12:31 +03:00
|
|
|
default:
|
2016-10-09 11:18:57 +03:00
|
|
|
var slave *clusterNode
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
n := rand.Intn(len(nodes)-1) + 1
|
|
|
|
slave = nodes[n]
|
2019-06-16 13:58:15 +03:00
|
|
|
if !slave.Failing() {
|
2018-11-21 11:49:30 +03:00
|
|
|
return slave, nil
|
2016-10-09 11:18:57 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-21 11:49:30 +03:00
|
|
|
|
|
|
|
// All slaves are loading - use master.
|
|
|
|
return nodes[0], nil
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func (c *clusterState) slotClosestNode(slot int) (*clusterNode, error) {
|
2016-12-16 17:26:48 +03:00
|
|
|
const threshold = time.Millisecond
|
|
|
|
|
2016-05-06 21:12:31 +03:00
|
|
|
nodes := c.slotNodes(slot)
|
2016-06-05 14:30:56 +03:00
|
|
|
if len(nodes) == 0 {
|
2016-11-08 12:46:44 +03:00
|
|
|
return c.nodes.Random()
|
2016-06-05 14:30:56 +03:00
|
|
|
}
|
|
|
|
|
2016-05-06 21:12:31 +03:00
|
|
|
var node *clusterNode
|
|
|
|
for _, n := range nodes {
|
2019-06-16 13:58:15 +03:00
|
|
|
if n.Failing() {
|
2016-12-16 17:26:48 +03:00
|
|
|
continue
|
|
|
|
}
|
2018-02-12 17:15:40 +03:00
|
|
|
if node == nil || node.Latency()-n.Latency() > threshold {
|
2016-05-06 21:12:31 +03:00
|
|
|
node = n
|
2015-04-04 16:46:57 +03:00
|
|
|
}
|
|
|
|
}
|
2016-06-05 14:30:56 +03:00
|
|
|
return node, nil
|
2015-04-04 16:46:57 +03:00
|
|
|
}
|
|
|
|
|
2019-06-16 13:58:15 +03:00
|
|
|
func (c *clusterState) slotRandomNode(slot int) (*clusterNode, error) {
|
2018-03-07 13:23:38 +03:00
|
|
|
nodes := c.slotNodes(slot)
|
2019-06-16 13:58:15 +03:00
|
|
|
if len(nodes) == 0 {
|
|
|
|
return c.nodes.Random()
|
|
|
|
}
|
2018-03-07 13:23:38 +03:00
|
|
|
n := rand.Intn(len(nodes))
|
2019-06-16 13:58:15 +03:00
|
|
|
return nodes[n], nil
|
2018-03-07 13:23:38 +03:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func (c *clusterState) slotNodes(slot int) []*clusterNode {
|
2018-07-22 10:50:26 +03:00
|
|
|
i := sort.Search(len(c.slots), func(i int) bool {
|
|
|
|
return c.slots[i].end >= slot
|
|
|
|
})
|
|
|
|
if i >= len(c.slots) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
x := c.slots[i]
|
|
|
|
if slot >= x.start && slot <= x.end {
|
|
|
|
return x.nodes
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2018-03-06 18:10:01 +03:00
|
|
|
type clusterStateHolder struct {
|
2018-03-07 15:38:18 +03:00
|
|
|
load func() (*clusterState, error)
|
2018-03-06 18:10:01 +03:00
|
|
|
|
2018-12-13 13:26:02 +03:00
|
|
|
state atomic.Value
|
2018-03-07 15:38:18 +03:00
|
|
|
reloading uint32 // atomic
|
2018-03-06 18:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newClusterStateHolder(fn func() (*clusterState, error)) *clusterStateHolder {
|
|
|
|
return &clusterStateHolder{
|
|
|
|
load: fn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 16:09:56 +03:00
|
|
|
func (c *clusterStateHolder) Reload() (*clusterState, error) {
|
2018-03-06 18:10:01 +03:00
|
|
|
state, err := c.load()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.state.Store(state)
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clusterStateHolder) LazyReload() {
|
|
|
|
if !atomic.CompareAndSwapUint32(&c.reloading, 0, 1) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
defer atomic.StoreUint32(&c.reloading, 0)
|
|
|
|
|
2018-12-13 13:26:02 +03:00
|
|
|
_, err := c.Reload()
|
2018-11-24 14:16:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return
|
2018-03-06 18:10:01 +03:00
|
|
|
}
|
2018-11-24 14:16:21 +03:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2018-03-06 18:10:01 +03:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clusterStateHolder) Get() (*clusterState, error) {
|
|
|
|
v := c.state.Load()
|
|
|
|
if v != nil {
|
2018-05-21 16:33:41 +03:00
|
|
|
state := v.(*clusterState)
|
|
|
|
if time.Since(state.createdAt) > time.Minute {
|
|
|
|
c.LazyReload()
|
|
|
|
}
|
|
|
|
return state, nil
|
2018-03-06 18:10:01 +03:00
|
|
|
}
|
2018-12-13 13:26:02 +03:00
|
|
|
return c.Reload()
|
2018-03-06 18:10:01 +03:00
|
|
|
}
|
|
|
|
|
2018-06-29 10:45:05 +03:00
|
|
|
func (c *clusterStateHolder) ReloadOrGet() (*clusterState, error) {
|
|
|
|
state, err := c.Reload()
|
|
|
|
if err == nil {
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
return c.Get()
|
|
|
|
}
|
|
|
|
|
2018-03-06 18:10:01 +03:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2019-05-31 17:03:20 +03:00
|
|
|
type clusterClient struct {
|
2016-11-08 12:46:44 +03:00
|
|
|
cmdable
|
2019-05-31 17:03:20 +03:00
|
|
|
hooks
|
2016-11-08 12:46:44 +03:00
|
|
|
|
2018-03-06 15:50:48 +03:00
|
|
|
opt *ClusterOptions
|
|
|
|
nodes *clusterNodes
|
2018-03-06 18:10:01 +03:00
|
|
|
state *clusterStateHolder
|
2018-03-06 15:50:48 +03:00
|
|
|
cmdsInfoCache *cmdsInfoCache
|
2019-05-31 17:03:20 +03:00
|
|
|
}
|
2018-02-15 14:00:54 +03:00
|
|
|
|
2019-05-31 17:03:20 +03:00
|
|
|
// ClusterClient is a Redis Cluster client representing a pool of zero
|
|
|
|
// or more underlying connections. It's safe for concurrent use by
|
|
|
|
// multiple goroutines.
|
|
|
|
type ClusterClient struct {
|
|
|
|
*clusterClient
|
2019-05-31 16:36:57 +03:00
|
|
|
ctx context.Context
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClusterClient returns a Redis Cluster client as described in
|
|
|
|
// http://redis.io/topics/cluster-spec.
|
|
|
|
func NewClusterClient(opt *ClusterOptions) *ClusterClient {
|
|
|
|
opt.init()
|
|
|
|
|
|
|
|
c := &ClusterClient{
|
2019-05-31 17:03:20 +03:00
|
|
|
clusterClient: &clusterClient{
|
|
|
|
opt: opt,
|
|
|
|
nodes: newClusterNodes(opt),
|
|
|
|
},
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
2018-03-06 18:10:01 +03:00
|
|
|
c.state = newClusterStateHolder(c.loadState)
|
2018-05-17 15:21:51 +03:00
|
|
|
c.cmdsInfoCache = newCmdsInfoCache(c.cmdsInfo)
|
2018-01-20 13:26:33 +03:00
|
|
|
|
2018-05-24 03:30:50 +03:00
|
|
|
c.init()
|
2016-11-08 12:46:44 +03:00
|
|
|
if opt.IdleCheckFrequency > 0 {
|
|
|
|
go c.reaper(opt.IdleCheckFrequency)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-12-13 13:26:02 +03:00
|
|
|
func (c *ClusterClient) init() {
|
2019-05-31 17:03:20 +03:00
|
|
|
c.cmdable = c.Process
|
2018-12-13 13:26:02 +03:00
|
|
|
}
|
|
|
|
|
2018-03-07 16:39:56 +03:00
|
|
|
func (c *ClusterClient) Context() context.Context {
|
|
|
|
if c.ctx != nil {
|
|
|
|
return c.ctx
|
|
|
|
}
|
|
|
|
return context.Background()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) WithContext(ctx context.Context) *ClusterClient {
|
|
|
|
if ctx == nil {
|
|
|
|
panic("nil context")
|
|
|
|
}
|
2019-05-31 16:36:57 +03:00
|
|
|
clone := *c
|
2019-05-31 17:03:20 +03:00
|
|
|
clone.ctx = ctx
|
2019-05-31 16:36:57 +03:00
|
|
|
return &clone
|
2018-03-07 14:50:14 +03:00
|
|
|
}
|
|
|
|
|
2017-03-20 13:15:21 +03:00
|
|
|
// Options returns read-only Options that were used to create the client.
|
|
|
|
func (c *ClusterClient) Options() *ClusterOptions {
|
|
|
|
return c.opt
|
|
|
|
}
|
|
|
|
|
2019-05-31 16:36:57 +03:00
|
|
|
// ReloadState reloads cluster state. If available it calls ClusterSlots func
|
|
|
|
// to get cluster slots information.
|
|
|
|
func (c *ClusterClient) ReloadState() error {
|
|
|
|
_, err := c.state.Reload()
|
2017-08-31 15:22:47 +03:00
|
|
|
return err
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the cluster client, releasing any open resources.
|
|
|
|
//
|
|
|
|
// It is rare to Close a ClusterClient, as the ClusterClient is meant
|
|
|
|
// to be long-lived and shared between many goroutines.
|
|
|
|
func (c *ClusterClient) Close() error {
|
|
|
|
return c.nodes.Close()
|
|
|
|
}
|
|
|
|
|
2018-08-12 11:11:01 +03:00
|
|
|
// Do creates a Cmd from the args and processes the cmd.
|
|
|
|
func (c *ClusterClient) Do(args ...interface{}) *Cmd {
|
2019-06-04 13:30:47 +03:00
|
|
|
return c.DoContext(c.ctx, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) DoContext(ctx context.Context, args ...interface{}) *Cmd {
|
2018-08-12 11:11:01 +03:00
|
|
|
cmd := NewCmd(args...)
|
2019-06-04 13:30:47 +03:00
|
|
|
c.ProcessContext(ctx, cmd)
|
2018-08-12 11:11:01 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-06-17 15:09:38 +03:00
|
|
|
func (c *ClusterClient) Process(cmd Cmder) error {
|
2019-06-04 13:30:47 +03:00
|
|
|
return c.ProcessContext(c.ctx, cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) ProcessContext(ctx context.Context, cmd Cmder) error {
|
|
|
|
return c.hooks.process(ctx, cmd, c.process)
|
2018-01-20 13:26:33 +03:00
|
|
|
}
|
|
|
|
|
2019-06-04 13:30:47 +03:00
|
|
|
func (c *ClusterClient) process(ctx context.Context, cmd Cmder) error {
|
2019-06-16 13:58:15 +03:00
|
|
|
cmdInfo := c.cmdInfo(cmd.Name())
|
|
|
|
slot := c.cmdSlot(cmd)
|
|
|
|
|
2018-02-27 15:50:08 +03:00
|
|
|
var node *clusterNode
|
2016-06-17 15:09:38 +03:00
|
|
|
var ask bool
|
2016-06-05 14:10:30 +03:00
|
|
|
for attempt := 0; attempt <= c.opt.MaxRedirects; attempt++ {
|
2019-06-16 13:58:15 +03:00
|
|
|
var err error
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
if attempt > 0 {
|
2017-08-31 15:22:47 +03:00
|
|
|
time.Sleep(c.retryBackoff(attempt))
|
2017-07-09 13:10:07 +03:00
|
|
|
}
|
|
|
|
|
2018-02-27 15:50:08 +03:00
|
|
|
if node == nil {
|
2019-06-16 13:58:15 +03:00
|
|
|
node, err = c.cmdNode(cmdInfo, slot)
|
2018-02-27 15:50:08 +03:00
|
|
|
if err != nil {
|
|
|
|
cmd.setErr(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-24 15:12:48 +03:00
|
|
|
if ask {
|
2016-05-06 21:12:31 +03:00
|
|
|
pipe := node.Client.Pipeline()
|
2018-01-17 13:55:20 +03:00
|
|
|
_ = pipe.Process(NewCmd("ASKING"))
|
|
|
|
_ = pipe.Process(cmd)
|
2019-06-04 13:30:47 +03:00
|
|
|
_, err = pipe.ExecContext(ctx)
|
2018-01-17 13:55:20 +03:00
|
|
|
_ = pipe.Close()
|
2015-01-24 15:12:48 +03:00
|
|
|
ask = false
|
|
|
|
} else {
|
2019-06-04 13:30:47 +03:00
|
|
|
err = node.Client.ProcessContext(ctx, cmd)
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
// If there is no error - we are done.
|
2016-03-19 17:33:14 +03:00
|
|
|
if err == nil {
|
2017-08-31 15:22:47 +03:00
|
|
|
break
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2019-01-08 17:37:22 +03:00
|
|
|
if err != Nil {
|
|
|
|
c.state.LazyReload()
|
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2018-11-21 11:49:30 +03:00
|
|
|
// If slave is loading - pick another node.
|
2016-10-09 14:38:31 +03:00
|
|
|
if c.opt.ReadOnly && internal.IsLoadingError(err) {
|
2019-06-16 13:58:15 +03:00
|
|
|
node.MarkAsFailing()
|
2018-11-21 11:49:30 +03:00
|
|
|
node = nil
|
2016-10-09 11:18:57 +03:00
|
|
|
continue
|
2016-10-09 06:30:01 +03:00
|
|
|
}
|
|
|
|
|
2015-03-18 13:41:24 +03:00
|
|
|
var moved bool
|
|
|
|
var addr string
|
2016-10-09 14:38:31 +03:00
|
|
|
moved, ask, addr = internal.IsMovedError(err)
|
2015-03-18 13:41:24 +03:00
|
|
|
if moved || ask {
|
2018-02-27 15:50:08 +03:00
|
|
|
node, err = c.nodes.GetOrCreate(addr)
|
|
|
|
if err != nil {
|
2017-08-31 15:22:47 +03:00
|
|
|
break
|
2016-06-17 15:09:38 +03:00
|
|
|
}
|
2015-03-18 13:41:24 +03:00
|
|
|
continue
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2019-02-27 13:53:44 +03:00
|
|
|
if err == pool.ErrClosed || internal.IsReadOnlyError(err) {
|
2018-02-27 15:50:08 +03:00
|
|
|
node = nil
|
2018-02-15 14:00:54 +03:00
|
|
|
continue
|
2018-01-17 13:55:20 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:37:22 +03:00
|
|
|
if internal.IsRetryableError(err, true) {
|
|
|
|
// First retry the same node.
|
|
|
|
if attempt == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-06-16 13:58:15 +03:00
|
|
|
// Second try another node.
|
|
|
|
node.MarkAsFailing()
|
|
|
|
node = nil
|
2019-01-08 17:37:22 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-03-18 13:41:24 +03:00
|
|
|
break
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2016-06-17 15:09:38 +03:00
|
|
|
|
|
|
|
return cmd.Err()
|
|
|
|
}
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
// ForEachMaster concurrently calls the fn on each master node in the cluster.
|
2016-12-12 18:30:08 +03:00
|
|
|
// It returns the first error if any.
|
2017-07-09 13:10:07 +03:00
|
|
|
func (c *ClusterClient) ForEachMaster(fn func(client *Client) error) error {
|
2018-06-29 10:45:05 +03:00
|
|
|
state, err := c.state.ReloadOrGet()
|
2017-08-31 15:22:47 +03:00
|
|
|
if err != nil {
|
2018-06-29 10:45:05 +03:00
|
|
|
return err
|
2016-12-12 18:30:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
errCh := make(chan error, 1)
|
2018-05-17 16:09:56 +03:00
|
|
|
for _, master := range state.Masters {
|
2016-12-12 18:30:08 +03:00
|
|
|
wg.Add(1)
|
|
|
|
go func(node *clusterNode) {
|
|
|
|
defer wg.Done()
|
|
|
|
err := fn(node.Client)
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case errCh <- err:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2017-07-09 13:10:07 +03:00
|
|
|
}(master)
|
2016-12-12 18:30:08 +03:00
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
// ForEachSlave concurrently calls the fn on each slave node in the cluster.
|
2016-06-17 15:09:38 +03:00
|
|
|
// It returns the first error if any.
|
2017-07-09 13:10:07 +03:00
|
|
|
func (c *ClusterClient) ForEachSlave(fn func(client *Client) error) error {
|
2018-06-29 10:45:05 +03:00
|
|
|
state, err := c.state.ReloadOrGet()
|
2017-08-31 15:22:47 +03:00
|
|
|
if err != nil {
|
2018-06-29 10:45:05 +03:00
|
|
|
return err
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
2016-06-17 15:09:38 +03:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
2016-10-02 15:44:01 +03:00
|
|
|
errCh := make(chan error, 1)
|
2018-05-17 16:09:56 +03:00
|
|
|
for _, slave := range state.Slaves {
|
2016-06-17 15:09:38 +03:00
|
|
|
wg.Add(1)
|
|
|
|
go func(node *clusterNode) {
|
2016-10-02 15:44:01 +03:00
|
|
|
defer wg.Done()
|
2016-06-17 15:09:38 +03:00
|
|
|
err := fn(node.Client)
|
|
|
|
if err != nil {
|
2016-10-02 15:44:01 +03:00
|
|
|
select {
|
|
|
|
case errCh <- err:
|
|
|
|
default:
|
2016-06-17 15:09:38 +03:00
|
|
|
}
|
|
|
|
}
|
2017-07-09 13:10:07 +03:00
|
|
|
}(slave)
|
2016-06-17 15:09:38 +03:00
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
// ForEachNode concurrently calls the fn on each known node in the cluster.
|
|
|
|
// It returns the first error if any.
|
|
|
|
func (c *ClusterClient) ForEachNode(fn func(client *Client) error) error {
|
2018-06-29 10:45:05 +03:00
|
|
|
state, err := c.state.ReloadOrGet()
|
2017-08-31 15:22:47 +03:00
|
|
|
if err != nil {
|
2018-06-29 10:45:05 +03:00
|
|
|
return err
|
2017-07-09 13:10:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
errCh := make(chan error, 1)
|
|
|
|
worker := func(node *clusterNode) {
|
|
|
|
defer wg.Done()
|
|
|
|
err := fn(node.Client)
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case errCh <- err:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 16:09:56 +03:00
|
|
|
for _, node := range state.Masters {
|
2017-07-09 13:10:07 +03:00
|
|
|
wg.Add(1)
|
|
|
|
go worker(node)
|
|
|
|
}
|
2018-05-17 16:09:56 +03:00
|
|
|
for _, node := range state.Slaves {
|
2017-07-09 13:10:07 +03:00
|
|
|
wg.Add(1)
|
|
|
|
go worker(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
// PoolStats returns accumulated connection pool stats.
|
|
|
|
func (c *ClusterClient) PoolStats() *PoolStats {
|
2017-03-04 14:04:27 +03:00
|
|
|
var acc PoolStats
|
|
|
|
|
2018-03-06 18:10:01 +03:00
|
|
|
state, _ := c.state.Get()
|
2017-07-09 13:10:07 +03:00
|
|
|
if state == nil {
|
2017-03-04 14:04:27 +03:00
|
|
|
return &acc
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
2016-04-09 12:52:43 +03:00
|
|
|
|
2018-05-17 16:09:56 +03:00
|
|
|
for _, node := range state.Masters {
|
2017-07-09 13:10:07 +03:00
|
|
|
s := node.Client.connPool.Stats()
|
|
|
|
acc.Hits += s.Hits
|
2017-09-22 12:23:46 +03:00
|
|
|
acc.Misses += s.Misses
|
2017-07-09 13:10:07 +03:00
|
|
|
acc.Timeouts += s.Timeouts
|
2017-09-11 09:10:17 +03:00
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
acc.TotalConns += s.TotalConns
|
2018-08-12 10:08:21 +03:00
|
|
|
acc.IdleConns += s.IdleConns
|
2017-09-11 09:10:17 +03:00
|
|
|
acc.StaleConns += s.StaleConns
|
2017-07-09 13:10:07 +03:00
|
|
|
}
|
|
|
|
|
2018-05-17 16:09:56 +03:00
|
|
|
for _, node := range state.Slaves {
|
2016-11-08 12:46:44 +03:00
|
|
|
s := node.Client.connPool.Stats()
|
|
|
|
acc.Hits += s.Hits
|
2017-09-22 12:23:46 +03:00
|
|
|
acc.Misses += s.Misses
|
2016-11-08 12:46:44 +03:00
|
|
|
acc.Timeouts += s.Timeouts
|
2017-09-11 09:10:17 +03:00
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
acc.TotalConns += s.TotalConns
|
2018-08-12 10:08:21 +03:00
|
|
|
acc.IdleConns += s.IdleConns
|
2017-09-11 09:10:17 +03:00
|
|
|
acc.StaleConns += s.StaleConns
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2017-07-09 13:10:07 +03:00
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
return &acc
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2018-02-15 14:00:54 +03:00
|
|
|
func (c *ClusterClient) loadState() (*clusterState, error) {
|
2018-06-29 10:45:05 +03:00
|
|
|
if c.opt.ClusterSlots != nil {
|
|
|
|
slots, err := c.opt.ClusterSlots()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newClusterState(c.nodes, slots, "")
|
|
|
|
}
|
|
|
|
|
2018-03-06 18:10:01 +03:00
|
|
|
addrs, err := c.nodes.Addrs()
|
2017-03-04 14:04:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-06 21:12:31 +03:00
|
|
|
|
2018-03-06 18:10:01 +03:00
|
|
|
var firstErr error
|
|
|
|
for _, addr := range addrs {
|
|
|
|
node, err := c.nodes.GetOrCreate(addr)
|
|
|
|
if err != nil {
|
|
|
|
if firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2017-03-04 14:04:27 +03:00
|
|
|
|
2018-03-06 18:10:01 +03:00
|
|
|
slots, err := node.Client.ClusterSlots().Result()
|
|
|
|
if err != nil {
|
|
|
|
if firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2018-03-06 18:10:01 +03:00
|
|
|
return newClusterState(c.nodes, slots, node.Client.opt.Addr)
|
2018-02-15 14:00:54 +03:00
|
|
|
}
|
|
|
|
|
2018-03-06 18:10:01 +03:00
|
|
|
return nil, firstErr
|
2018-02-15 14:00:54 +03:00
|
|
|
}
|
|
|
|
|
2015-04-05 15:41:16 +03:00
|
|
|
// reaper closes idle connections to the cluster.
|
2016-10-02 15:44:01 +03:00
|
|
|
func (c *ClusterClient) reaper(idleCheckFrequency time.Duration) {
|
|
|
|
ticker := time.NewTicker(idleCheckFrequency)
|
2015-05-01 11:01:01 +03:00
|
|
|
defer ticker.Stop()
|
2015-05-01 09:33:47 +03:00
|
|
|
|
2017-04-02 17:10:47 +03:00
|
|
|
for range ticker.C {
|
2016-11-08 12:46:44 +03:00
|
|
|
nodes, err := c.nodes.All()
|
|
|
|
if err != nil {
|
2015-05-01 11:01:01 +03:00
|
|
|
break
|
2015-05-01 09:33:47 +03:00
|
|
|
}
|
|
|
|
|
2016-05-06 21:12:31 +03:00
|
|
|
for _, node := range nodes {
|
2017-09-11 09:10:17 +03:00
|
|
|
_, err := node.Client.connPool.(*pool.ConnPool).ReapStaleConns()
|
2016-03-17 19:00:47 +03:00
|
|
|
if err != nil {
|
2016-04-09 14:52:01 +03:00
|
|
|
internal.Logf("ReapStaleConns failed: %s", err)
|
2015-04-05 15:41:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
func (c *ClusterClient) Pipeline() Pipeliner {
|
2016-06-05 12:45:39 +03:00
|
|
|
pipe := Pipeline{
|
2018-01-20 13:26:33 +03:00
|
|
|
exec: c.processPipeline,
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
2019-05-31 17:03:20 +03:00
|
|
|
pipe.init()
|
2016-06-05 12:45:39 +03:00
|
|
|
return &pipe
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
func (c *ClusterClient) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
|
2017-09-25 11:48:44 +03:00
|
|
|
return c.Pipeline().Pipelined(fn)
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
|
|
|
|
2019-06-04 13:30:47 +03:00
|
|
|
func (c *ClusterClient) processPipeline(ctx context.Context, cmds []Cmder) error {
|
2019-05-31 16:36:57 +03:00
|
|
|
return c.hooks.processPipeline(c.ctx, cmds, c._processPipeline)
|
2018-01-20 13:26:33 +03:00
|
|
|
}
|
|
|
|
|
2019-06-04 13:30:47 +03:00
|
|
|
func (c *ClusterClient) _processPipeline(ctx context.Context, cmds []Cmder) error {
|
2018-09-11 13:09:16 +03:00
|
|
|
cmdsMap := newCmdsMap()
|
|
|
|
err := c.mapCmdsByNode(cmds, cmdsMap)
|
2016-12-13 18:28:39 +03:00
|
|
|
if err != nil {
|
2017-08-31 15:22:47 +03:00
|
|
|
setCmdsErr(cmds, err)
|
2016-12-13 18:28:39 +03:00
|
|
|
return err
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
for attempt := 0; attempt <= c.opt.MaxRedirects; attempt++ {
|
|
|
|
if attempt > 0 {
|
|
|
|
time.Sleep(c.retryBackoff(attempt))
|
|
|
|
}
|
|
|
|
|
2018-09-11 13:09:16 +03:00
|
|
|
failedCmds := newCmdsMap()
|
2018-09-11 12:37:57 +03:00
|
|
|
var wg sync.WaitGroup
|
2018-09-11 13:09:16 +03:00
|
|
|
|
|
|
|
for node, cmds := range cmdsMap.m {
|
2018-09-11 12:37:57 +03:00
|
|
|
wg.Add(1)
|
|
|
|
go func(node *clusterNode, cmds []Cmder) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2019-06-04 14:05:29 +03:00
|
|
|
cn, err := node.Client.getConn(ctx)
|
2018-09-11 12:37:57 +03:00
|
|
|
if err != nil {
|
|
|
|
if err == pool.ErrClosed {
|
2018-09-11 13:09:16 +03:00
|
|
|
c.mapCmdsByNode(cmds, failedCmds)
|
2018-09-11 12:37:57 +03:00
|
|
|
} else {
|
|
|
|
setCmdsErr(cmds, err)
|
|
|
|
}
|
2018-09-11 13:09:16 +03:00
|
|
|
return
|
2018-01-17 13:55:20 +03:00
|
|
|
}
|
2016-04-09 10:47:15 +03:00
|
|
|
|
2019-06-08 15:02:51 +03:00
|
|
|
err = c.pipelineProcessCmds(ctx, node, cn, cmds, failedCmds)
|
2018-10-14 11:27:34 +03:00
|
|
|
node.Client.releaseConnStrict(cn, err)
|
2018-09-11 12:37:57 +03:00
|
|
|
}(node, cmds)
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
|
|
|
|
2018-09-11 13:09:16 +03:00
|
|
|
wg.Wait()
|
|
|
|
if len(failedCmds.m) == 0 {
|
2016-12-13 18:28:39 +03:00
|
|
|
break
|
|
|
|
}
|
2016-04-09 10:47:15 +03:00
|
|
|
cmdsMap = failedCmds
|
|
|
|
}
|
|
|
|
|
2018-08-12 11:11:01 +03:00
|
|
|
return cmdsFirstErr(cmds)
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
|
|
|
|
2018-09-11 13:09:16 +03:00
|
|
|
type cmdsMap struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
m map[*clusterNode][]Cmder
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCmdsMap() *cmdsMap {
|
|
|
|
return &cmdsMap{
|
|
|
|
m: make(map[*clusterNode][]Cmder),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) mapCmdsByNode(cmds []Cmder, cmdsMap *cmdsMap) error {
|
2018-03-06 18:10:01 +03:00
|
|
|
state, err := c.state.Get()
|
2017-08-31 15:22:47 +03:00
|
|
|
if err != nil {
|
|
|
|
setCmdsErr(cmds, err)
|
2018-09-11 13:09:16 +03:00
|
|
|
return err
|
2017-08-31 15:22:47 +03:00
|
|
|
}
|
|
|
|
|
2019-05-25 17:42:01 +03:00
|
|
|
cmdsAreReadOnly := c.opt.ReadOnly && c.cmdsAreReadOnly(cmds)
|
2016-12-13 18:28:39 +03:00
|
|
|
for _, cmd := range cmds {
|
2019-06-16 13:58:15 +03:00
|
|
|
slot := c.cmdSlot(cmd)
|
|
|
|
|
2018-05-31 21:40:56 +03:00
|
|
|
var node *clusterNode
|
|
|
|
var err error
|
|
|
|
if cmdsAreReadOnly {
|
2019-06-16 13:58:15 +03:00
|
|
|
cmdInfo := c.cmdInfo(cmd.Name())
|
|
|
|
node, err = c.cmdNode(cmdInfo, slot)
|
2018-05-31 21:40:56 +03:00
|
|
|
} else {
|
|
|
|
node, err = state.slotMasterNode(slot)
|
|
|
|
}
|
2016-12-13 18:28:39 +03:00
|
|
|
if err != nil {
|
2018-09-11 13:09:16 +03:00
|
|
|
return err
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
2019-06-16 13:58:15 +03:00
|
|
|
|
2018-09-11 13:09:16 +03:00
|
|
|
cmdsMap.mu.Lock()
|
|
|
|
cmdsMap.m[node] = append(cmdsMap.m[node], cmd)
|
|
|
|
cmdsMap.mu.Unlock()
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
2018-09-11 13:09:16 +03:00
|
|
|
return nil
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:40:56 +03:00
|
|
|
func (c *ClusterClient) cmdsAreReadOnly(cmds []Cmder) bool {
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
cmdInfo := c.cmdInfo(cmd.Name())
|
|
|
|
if cmdInfo == nil || !cmdInfo.ReadOnly {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
func (c *ClusterClient) pipelineProcessCmds(
|
2019-06-08 15:02:51 +03:00
|
|
|
ctx context.Context, node *clusterNode, cn *pool.Conn, cmds []Cmder, failedCmds *cmdsMap,
|
2016-12-13 18:28:39 +03:00
|
|
|
) error {
|
2019-06-08 15:02:51 +03:00
|
|
|
err := cn.WithWriter(ctx, c.opt.WriteTimeout, func(wr *proto.Writer) error {
|
2018-08-17 13:56:37 +03:00
|
|
|
return writeCmd(wr, cmds...)
|
2018-08-15 11:53:15 +03:00
|
|
|
})
|
2018-02-15 14:00:54 +03:00
|
|
|
if err != nil {
|
2016-04-09 10:47:15 +03:00
|
|
|
setCmdsErr(cmds, err)
|
2018-09-11 13:09:16 +03:00
|
|
|
failedCmds.mu.Lock()
|
|
|
|
failedCmds.m[node] = cmds
|
|
|
|
failedCmds.mu.Unlock()
|
2016-12-13 18:28:39 +03:00
|
|
|
return err
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
|
|
|
|
2019-06-08 15:02:51 +03:00
|
|
|
return cn.WithReader(ctx, c.opt.ReadTimeout, func(rd *proto.Reader) error {
|
2018-12-13 14:27:41 +03:00
|
|
|
return c.pipelineReadCmds(node, rd, cmds, failedCmds)
|
2018-08-15 11:53:15 +03:00
|
|
|
})
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) pipelineReadCmds(
|
2018-12-13 14:27:41 +03:00
|
|
|
node *clusterNode, rd *proto.Reader, cmds []Cmder, failedCmds *cmdsMap,
|
2016-12-13 18:28:39 +03:00
|
|
|
) error {
|
2018-12-13 14:27:41 +03:00
|
|
|
var firstErr error
|
2016-12-13 18:28:39 +03:00
|
|
|
for _, cmd := range cmds {
|
2018-08-15 09:38:58 +03:00
|
|
|
err := cmd.readReply(rd)
|
2016-12-13 18:28:39 +03:00
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
if c.checkMovedErr(cmd, err, failedCmds) {
|
|
|
|
continue
|
2016-06-05 14:30:56 +03:00
|
|
|
}
|
2016-12-13 18:28:39 +03:00
|
|
|
|
2019-05-25 17:42:01 +03:00
|
|
|
if c.opt.ReadOnly && internal.IsLoadingError(err) {
|
2019-06-16 13:58:15 +03:00
|
|
|
node.MarkAsFailing()
|
2019-05-25 17:42:01 +03:00
|
|
|
} else if internal.IsRedisError(err) {
|
2017-08-31 15:22:47 +03:00
|
|
|
continue
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
2017-08-31 15:22:47 +03:00
|
|
|
|
2018-12-13 14:27:41 +03:00
|
|
|
failedCmds.mu.Lock()
|
|
|
|
failedCmds.m[node] = append(failedCmds.m[node], cmd)
|
|
|
|
failedCmds.mu.Unlock()
|
|
|
|
if firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
2016-06-05 14:30:56 +03:00
|
|
|
}
|
2018-12-13 14:27:41 +03:00
|
|
|
return firstErr
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
2016-06-05 14:30:56 +03:00
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
func (c *ClusterClient) checkMovedErr(
|
2018-09-11 13:09:16 +03:00
|
|
|
cmd Cmder, err error, failedCmds *cmdsMap,
|
2017-08-31 15:22:47 +03:00
|
|
|
) bool {
|
|
|
|
moved, ask, addr := internal.IsMovedError(err)
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
if moved {
|
2018-03-06 18:10:01 +03:00
|
|
|
c.state.LazyReload()
|
2016-12-03 18:30:13 +03:00
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
node, err := c.nodes.GetOrCreate(addr)
|
2016-12-13 18:28:39 +03:00
|
|
|
if err != nil {
|
2017-08-31 15:22:47 +03:00
|
|
|
return false
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
2018-09-11 13:09:16 +03:00
|
|
|
failedCmds.mu.Lock()
|
|
|
|
failedCmds.m[node] = append(failedCmds.m[node], cmd)
|
|
|
|
failedCmds.mu.Unlock()
|
2017-08-31 15:22:47 +03:00
|
|
|
return true
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
2017-08-31 15:22:47 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
if ask {
|
2017-07-09 13:10:07 +03:00
|
|
|
node, err := c.nodes.GetOrCreate(addr)
|
2016-12-13 18:28:39 +03:00
|
|
|
if err != nil {
|
2017-08-31 15:22:47 +03:00
|
|
|
return false
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
2018-09-11 13:09:16 +03:00
|
|
|
failedCmds.mu.Lock()
|
|
|
|
failedCmds.m[node] = append(failedCmds.m[node], NewCmd("ASKING"), cmd)
|
|
|
|
failedCmds.mu.Unlock()
|
2017-08-31 15:22:47 +03:00
|
|
|
return true
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
2017-08-31 15:22:47 +03:00
|
|
|
|
|
|
|
return false
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
2016-12-16 15:19:53 +03:00
|
|
|
// TxPipeline acts like Pipeline, but wraps queued commands with MULTI/EXEC.
|
2017-05-02 18:00:53 +03:00
|
|
|
func (c *ClusterClient) TxPipeline() Pipeliner {
|
2016-12-13 18:28:39 +03:00
|
|
|
pipe := Pipeline{
|
2018-01-20 13:26:33 +03:00
|
|
|
exec: c.processTxPipeline,
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
2019-05-31 17:03:20 +03:00
|
|
|
pipe.init()
|
2016-12-13 18:28:39 +03:00
|
|
|
return &pipe
|
|
|
|
}
|
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
func (c *ClusterClient) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) {
|
2017-09-25 11:48:44 +03:00
|
|
|
return c.TxPipeline().Pipelined(fn)
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
2019-06-04 13:30:47 +03:00
|
|
|
func (c *ClusterClient) processTxPipeline(ctx context.Context, cmds []Cmder) error {
|
|
|
|
return c.hooks.processPipeline(ctx, cmds, c._processTxPipeline)
|
2019-05-31 16:36:57 +03:00
|
|
|
}
|
|
|
|
|
2019-06-04 13:30:47 +03:00
|
|
|
func (c *ClusterClient) _processTxPipeline(ctx context.Context, cmds []Cmder) error {
|
2018-03-06 18:10:01 +03:00
|
|
|
state, err := c.state.Get()
|
2016-12-13 18:28:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
cmdsMap := c.mapCmdsBySlot(cmds)
|
2016-12-13 18:28:39 +03:00
|
|
|
for slot, cmds := range cmdsMap {
|
2017-03-04 14:04:27 +03:00
|
|
|
node, err := state.slotMasterNode(slot)
|
2016-12-13 18:28:39 +03:00
|
|
|
if err != nil {
|
|
|
|
setCmdsErr(cmds, err)
|
2016-04-09 10:47:15 +03:00
|
|
|
continue
|
|
|
|
}
|
2016-12-13 18:28:39 +03:00
|
|
|
cmdsMap := map[*clusterNode][]Cmder{node: cmds}
|
2017-08-31 15:22:47 +03:00
|
|
|
|
|
|
|
for attempt := 0; attempt <= c.opt.MaxRedirects; attempt++ {
|
|
|
|
if attempt > 0 {
|
|
|
|
time.Sleep(c.retryBackoff(attempt))
|
|
|
|
}
|
|
|
|
|
2018-09-11 13:09:16 +03:00
|
|
|
failedCmds := newCmdsMap()
|
|
|
|
var wg sync.WaitGroup
|
2016-12-13 18:28:39 +03:00
|
|
|
|
|
|
|
for node, cmds := range cmdsMap {
|
2018-09-11 13:09:16 +03:00
|
|
|
wg.Add(1)
|
|
|
|
go func(node *clusterNode, cmds []Cmder) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2019-06-04 14:05:29 +03:00
|
|
|
cn, err := node.Client.getConn(ctx)
|
2018-09-11 13:09:16 +03:00
|
|
|
if err != nil {
|
|
|
|
if err == pool.ErrClosed {
|
|
|
|
c.mapCmdsByNode(cmds, failedCmds)
|
|
|
|
} else {
|
|
|
|
setCmdsErr(cmds, err)
|
|
|
|
}
|
|
|
|
return
|
2018-01-17 13:55:20 +03:00
|
|
|
}
|
2016-12-13 18:28:39 +03:00
|
|
|
|
2019-06-08 15:02:51 +03:00
|
|
|
err = c.txPipelineProcessCmds(ctx, node, cn, cmds, failedCmds)
|
2018-10-14 11:27:34 +03:00
|
|
|
node.Client.releaseConnStrict(cn, err)
|
2018-09-11 13:09:16 +03:00
|
|
|
}(node, cmds)
|
2016-12-12 18:30:08 +03:00
|
|
|
}
|
|
|
|
|
2018-09-11 13:09:16 +03:00
|
|
|
wg.Wait()
|
|
|
|
if len(failedCmds.m) == 0 {
|
2016-12-13 18:28:39 +03:00
|
|
|
break
|
|
|
|
}
|
2018-09-11 13:09:16 +03:00
|
|
|
cmdsMap = failedCmds.m
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 11:11:01 +03:00
|
|
|
return cmdsFirstErr(cmds)
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
2016-10-13 13:56:24 +03:00
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
func (c *ClusterClient) mapCmdsBySlot(cmds []Cmder) map[int][]Cmder {
|
2016-12-13 18:28:39 +03:00
|
|
|
cmdsMap := make(map[int][]Cmder)
|
|
|
|
for _, cmd := range cmds {
|
2017-08-31 15:22:47 +03:00
|
|
|
slot := c.cmdSlot(cmd)
|
2016-12-13 18:28:39 +03:00
|
|
|
cmdsMap[slot] = append(cmdsMap[slot], cmd)
|
|
|
|
}
|
2017-08-31 15:22:47 +03:00
|
|
|
return cmdsMap
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
2016-10-13 13:56:24 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
func (c *ClusterClient) txPipelineProcessCmds(
|
2019-06-08 15:02:51 +03:00
|
|
|
ctx context.Context, node *clusterNode, cn *pool.Conn, cmds []Cmder, failedCmds *cmdsMap,
|
2016-12-13 18:28:39 +03:00
|
|
|
) error {
|
2019-06-08 15:02:51 +03:00
|
|
|
err := cn.WithWriter(ctx, c.opt.WriteTimeout, func(wr *proto.Writer) error {
|
2018-08-17 13:56:37 +03:00
|
|
|
return txPipelineWriteMulti(wr, cmds)
|
2018-08-15 11:53:15 +03:00
|
|
|
})
|
2018-08-15 09:38:58 +03:00
|
|
|
if err != nil {
|
2017-08-31 15:22:47 +03:00
|
|
|
setCmdsErr(cmds, err)
|
2018-09-11 13:09:16 +03:00
|
|
|
failedCmds.mu.Lock()
|
|
|
|
failedCmds.m[node] = cmds
|
|
|
|
failedCmds.mu.Unlock()
|
2016-12-13 18:28:39 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-08 15:02:51 +03:00
|
|
|
err = cn.WithReader(ctx, c.opt.ReadTimeout, func(rd *proto.Reader) error {
|
2018-08-15 11:53:15 +03:00
|
|
|
err := c.txPipelineReadQueued(rd, cmds, failedCmds)
|
|
|
|
if err != nil {
|
|
|
|
setCmdsErr(cmds, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return pipelineReadCmds(rd, cmds)
|
|
|
|
})
|
|
|
|
return err
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) txPipelineReadQueued(
|
2018-09-11 13:09:16 +03:00
|
|
|
rd *proto.Reader, cmds []Cmder, failedCmds *cmdsMap,
|
2016-12-13 18:28:39 +03:00
|
|
|
) error {
|
|
|
|
// Parse queued replies.
|
|
|
|
var statusCmd StatusCmd
|
2018-08-15 09:38:58 +03:00
|
|
|
if err := statusCmd.readReply(rd); err != nil {
|
2017-08-31 15:22:47 +03:00
|
|
|
return err
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, cmd := range cmds {
|
2018-08-15 09:38:58 +03:00
|
|
|
err := statusCmd.readReply(rd)
|
2016-12-13 18:28:39 +03:00
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
if c.checkMovedErr(cmd, err, failedCmds) || internal.IsRedisError(err) {
|
|
|
|
continue
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
return err
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
// Parse number of replies.
|
2018-08-15 09:38:58 +03:00
|
|
|
line, err := rd.ReadLine()
|
2016-12-13 18:28:39 +03:00
|
|
|
if err != nil {
|
|
|
|
if err == Nil {
|
|
|
|
err = TxFailedErr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch line[0] {
|
|
|
|
case proto.ErrorReply:
|
2017-08-31 15:22:47 +03:00
|
|
|
err := proto.ParseErrorReply(line)
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
if !c.checkMovedErr(cmd, err, failedCmds) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2016-12-13 18:28:39 +03:00
|
|
|
case proto.ArrayReply:
|
|
|
|
// ok
|
|
|
|
default:
|
|
|
|
err := fmt.Errorf("redis: expected '*', but got line %q", line)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
return nil
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
2017-06-30 00:43:19 +03:00
|
|
|
|
2019-05-31 16:36:57 +03:00
|
|
|
func (c *ClusterClient) Watch(fn func(*Tx) error, keys ...string) error {
|
|
|
|
if len(keys) == 0 {
|
|
|
|
return fmt.Errorf("redis: Watch requires at least one key")
|
|
|
|
}
|
|
|
|
|
|
|
|
slot := hashtag.Slot(keys[0])
|
|
|
|
for _, key := range keys[1:] {
|
|
|
|
if hashtag.Slot(key) != slot {
|
|
|
|
err := fmt.Errorf("redis: Watch requires all keys to be in the same slot")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node, err := c.slotMasterNode(slot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for attempt := 0; attempt <= c.opt.MaxRedirects; attempt++ {
|
|
|
|
if attempt > 0 {
|
|
|
|
time.Sleep(c.retryBackoff(attempt))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = node.Client.Watch(fn, keys...)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != Nil {
|
|
|
|
c.state.LazyReload()
|
|
|
|
}
|
|
|
|
|
|
|
|
moved, ask, addr := internal.IsMovedError(err)
|
|
|
|
if moved || ask {
|
|
|
|
node, err = c.nodes.GetOrCreate(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == pool.ErrClosed || internal.IsReadOnlyError(err) {
|
|
|
|
node, err = c.slotMasterNode(slot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if internal.IsRetryableError(err, true) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:38:53 +03:00
|
|
|
func (c *ClusterClient) pubSub() *PubSub {
|
2017-07-09 10:07:20 +03:00
|
|
|
var node *clusterNode
|
2018-07-23 15:55:13 +03:00
|
|
|
pubsub := &PubSub{
|
|
|
|
opt: c.opt.clientOptions(),
|
2017-07-09 10:07:20 +03:00
|
|
|
|
|
|
|
newConn: func(channels []string) (*pool.Conn, error) {
|
2019-01-21 17:38:53 +03:00
|
|
|
if node != nil {
|
|
|
|
panic("node != nil")
|
|
|
|
}
|
2017-07-09 10:07:20 +03:00
|
|
|
|
2019-01-21 17:38:53 +03:00
|
|
|
var err error
|
2019-03-07 13:19:03 +03:00
|
|
|
if len(channels) > 0 {
|
|
|
|
slot := hashtag.Slot(channels[0])
|
|
|
|
node, err = c.slotMasterNode(slot)
|
|
|
|
} else {
|
|
|
|
node, err = c.nodes.Random()
|
|
|
|
}
|
2019-01-21 17:38:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-07-09 10:07:20 +03:00
|
|
|
}
|
2019-01-21 17:38:53 +03:00
|
|
|
|
2019-06-14 16:00:03 +03:00
|
|
|
cn, err := node.Client.newConn(context.TODO())
|
2019-01-21 17:38:53 +03:00
|
|
|
if err != nil {
|
2019-04-18 19:06:16 +03:00
|
|
|
node = nil
|
|
|
|
|
2019-01-21 17:38:53 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cn, nil
|
2017-07-09 10:07:20 +03:00
|
|
|
},
|
|
|
|
closeConn: func(cn *pool.Conn) error {
|
2019-01-21 17:38:53 +03:00
|
|
|
err := node.Client.connPool.CloseConn(cn)
|
|
|
|
node = nil
|
|
|
|
return err
|
2017-07-09 10:07:20 +03:00
|
|
|
},
|
|
|
|
}
|
2018-07-23 15:55:13 +03:00
|
|
|
pubsub.init()
|
2019-01-21 17:38:53 +03:00
|
|
|
|
2018-07-23 15:55:13 +03:00
|
|
|
return pubsub
|
2017-07-09 10:07:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe subscribes the client to the specified channels.
|
|
|
|
// Channels can be omitted to create empty subscription.
|
|
|
|
func (c *ClusterClient) Subscribe(channels ...string) *PubSub {
|
2019-01-21 17:38:53 +03:00
|
|
|
pubsub := c.pubSub()
|
2017-07-09 10:07:20 +03:00
|
|
|
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 *ClusterClient) PSubscribe(channels ...string) *PubSub {
|
2019-01-21 17:38:53 +03:00
|
|
|
pubsub := c.pubSub()
|
2017-07-09 10:07:20 +03:00
|
|
|
if len(channels) > 0 {
|
|
|
|
_ = pubsub.PSubscribe(channels...)
|
|
|
|
}
|
|
|
|
return pubsub
|
|
|
|
}
|
|
|
|
|
2019-05-31 16:36:57 +03:00
|
|
|
func (c *ClusterClient) retryBackoff(attempt int) time.Duration {
|
|
|
|
return internal.RetryBackoff(attempt, c.opt.MinRetryBackoff, c.opt.MaxRetryBackoff)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) cmdsInfo() (map[string]*CommandInfo, error) {
|
|
|
|
addrs, err := c.nodes.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var firstErr error
|
|
|
|
for _, addr := range addrs {
|
|
|
|
node, err := c.nodes.Get(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if node == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := node.Client.Command().Result()
|
|
|
|
if err == nil {
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
if firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, firstErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) cmdInfo(name string) *CommandInfo {
|
|
|
|
cmdsInfo, err := c.cmdsInfoCache.Get()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
info := cmdsInfo[name]
|
|
|
|
if info == nil {
|
|
|
|
internal.Logf("info for cmd=%s not found", name)
|
|
|
|
}
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) cmdSlot(cmd Cmder) int {
|
|
|
|
args := cmd.Args()
|
|
|
|
if args[0] == "cluster" && args[1] == "getkeysinslot" {
|
|
|
|
return args[2].(int)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdInfo := c.cmdInfo(cmd.Name())
|
|
|
|
return cmdSlot(cmd, cmdFirstKeyPos(cmd, cmdInfo))
|
|
|
|
}
|
|
|
|
|
2019-06-16 13:58:15 +03:00
|
|
|
func cmdSlot(cmd Cmder, pos int) int {
|
|
|
|
if pos == 0 {
|
|
|
|
return hashtag.RandomSlot()
|
|
|
|
}
|
|
|
|
firstKey := cmd.stringArg(pos)
|
|
|
|
return hashtag.Slot(firstKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) cmdNode(cmdInfo *CommandInfo, slot int) (*clusterNode, error) {
|
2019-05-31 16:36:57 +03:00
|
|
|
state, err := c.state.Get()
|
|
|
|
if err != nil {
|
2019-06-16 13:58:15 +03:00
|
|
|
return nil, err
|
2019-05-31 16:36:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.opt.ReadOnly && cmdInfo != nil && cmdInfo.ReadOnly {
|
|
|
|
if c.opt.RouteByLatency {
|
2019-06-16 13:58:15 +03:00
|
|
|
return state.slotClosestNode(slot)
|
2019-05-31 16:36:57 +03:00
|
|
|
}
|
|
|
|
if c.opt.RouteRandomly {
|
2019-06-16 13:58:15 +03:00
|
|
|
return state.slotRandomNode(slot)
|
2019-05-31 16:36:57 +03:00
|
|
|
}
|
2019-06-16 13:58:15 +03:00
|
|
|
return state.slotSlaveNode(slot)
|
2019-05-31 16:36:57 +03:00
|
|
|
}
|
|
|
|
|
2019-06-16 13:58:15 +03:00
|
|
|
return state.slotMasterNode(slot)
|
2019-05-31 16:36:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) slotMasterNode(slot int) (*clusterNode, error) {
|
|
|
|
state, err := c.state.Get()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := state.slotNodes(slot)
|
|
|
|
if len(nodes) > 0 {
|
|
|
|
return nodes[0], nil
|
|
|
|
}
|
|
|
|
return c.nodes.Random()
|
|
|
|
}
|
|
|
|
|
2018-05-17 16:09:56 +03:00
|
|
|
func appendUniqueNode(nodes []*clusterNode, node *clusterNode) []*clusterNode {
|
2017-07-09 13:10:07 +03:00
|
|
|
for _, n := range nodes {
|
|
|
|
if n == node {
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(nodes, node)
|
|
|
|
}
|
2018-03-14 13:42:51 +03:00
|
|
|
|
|
|
|
func appendIfNotExists(ss []string, es ...string) []string {
|
|
|
|
loop:
|
|
|
|
for _, e := range es {
|
|
|
|
for _, s := range ss {
|
|
|
|
if s == e {
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ss = append(ss, e)
|
|
|
|
}
|
|
|
|
return ss
|
|
|
|
}
|
|
|
|
|
|
|
|
func remove(ss []string, es ...string) []string {
|
|
|
|
if len(es) == 0 {
|
|
|
|
return ss[:0]
|
|
|
|
}
|
|
|
|
for _, e := range es {
|
|
|
|
for i, s := range ss {
|
|
|
|
if s == e {
|
|
|
|
ss = append(ss[:i], ss[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ss
|
|
|
|
}
|