2015-01-24 15:12:48 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2016-12-13 18:28:39 +03:00
|
|
|
"fmt"
|
2015-01-24 15:12:48 +03:00
|
|
|
"math/rand"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2016-10-09 14:38:31 +03:00
|
|
|
var errClusterNoNodes = internal.RedisError("redis: cluster has no nodes")
|
2017-03-04 14:04:27 +03:00
|
|
|
var errNilClusterState = internal.RedisError("redis: cannot load cluster slots")
|
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.
|
|
|
|
// Default is 16.
|
|
|
|
MaxRedirects int
|
|
|
|
|
|
|
|
// Enables read queries for a connection to a Redis Cluster slave node.
|
|
|
|
ReadOnly bool
|
|
|
|
|
|
|
|
// Enables routing read-only queries to the closest master or slave node.
|
|
|
|
RouteByLatency bool
|
|
|
|
|
|
|
|
// Following options are copied from Options struct.
|
|
|
|
|
2017-02-17 13:12:06 +03:00
|
|
|
MaxRetries int
|
|
|
|
Password string
|
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
|
|
|
|
PoolTimeout time.Duration
|
|
|
|
IdleTimeout time.Duration
|
|
|
|
IdleCheckFrequency time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opt *ClusterOptions) init() {
|
|
|
|
if opt.MaxRedirects == -1 {
|
|
|
|
opt.MaxRedirects = 0
|
|
|
|
} else if opt.MaxRedirects == 0 {
|
|
|
|
opt.MaxRedirects = 16
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.RouteByLatency {
|
|
|
|
opt.ReadOnly = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opt *ClusterOptions) clientOptions() *Options {
|
|
|
|
const disableIdleCheck = -1
|
|
|
|
|
|
|
|
return &Options{
|
2017-02-17 13:12:06 +03:00
|
|
|
MaxRetries: opt.MaxRetries,
|
|
|
|
Password: opt.Password,
|
|
|
|
ReadOnly: opt.ReadOnly,
|
2016-10-09 13:30:45 +03:00
|
|
|
|
|
|
|
DialTimeout: opt.DialTimeout,
|
|
|
|
ReadTimeout: opt.ReadTimeout,
|
|
|
|
WriteTimeout: opt.WriteTimeout,
|
|
|
|
|
|
|
|
PoolSize: opt.PoolSize,
|
|
|
|
PoolTimeout: opt.PoolTimeout,
|
|
|
|
IdleTimeout: opt.IdleTimeout,
|
|
|
|
|
|
|
|
// IdleCheckFrequency is not copied to disable reaper
|
|
|
|
IdleCheckFrequency: disableIdleCheck,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-05-06 21:12:31 +03:00
|
|
|
type clusterNode struct {
|
|
|
|
Client *Client
|
2016-06-17 15:09:38 +03:00
|
|
|
Latency time.Duration
|
2016-10-09 11:18:57 +03:00
|
|
|
loading time.Time
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
|
|
|
if clOpt.RouteByLatency {
|
2016-12-16 17:26:48 +03:00
|
|
|
node.updateLatency()
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return &node
|
|
|
|
}
|
|
|
|
|
2016-12-16 17:26:48 +03:00
|
|
|
func (n *clusterNode) updateLatency() {
|
|
|
|
const probes = 10
|
|
|
|
for i := 0; i < probes; i++ {
|
|
|
|
start := time.Now()
|
|
|
|
n.Client.Ping()
|
|
|
|
n.Latency += time.Since(start)
|
|
|
|
}
|
|
|
|
n.Latency = n.Latency / probes
|
|
|
|
}
|
|
|
|
|
2016-10-09 11:18:57 +03:00
|
|
|
func (n *clusterNode) Loading() bool {
|
|
|
|
return !n.loading.IsZero() && time.Since(n.loading) < time.Minute
|
2016-05-06 21:12:31 +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
|
|
|
|
|
2016-05-06 21:12:31 +03:00
|
|
|
mu sync.RWMutex
|
|
|
|
addrs []string
|
|
|
|
nodes map[string]*clusterNode
|
|
|
|
closed bool
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func newClusterNodes(opt *ClusterOptions) *clusterNodes {
|
|
|
|
return &clusterNodes{
|
2016-05-06 21:12:31 +03:00
|
|
|
opt: opt,
|
|
|
|
nodes: 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
|
|
|
|
for _, node := range c.nodes {
|
|
|
|
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
|
|
|
}
|
2016-11-08 12:46:44 +03:00
|
|
|
c.addrs = nil
|
|
|
|
c.nodes = nil
|
|
|
|
|
|
|
|
return firstErr
|
2016-03-17 19:00:47 +03:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func (c *clusterNodes) All() ([]*clusterNode, error) {
|
2016-10-09 14:12:32 +03:00
|
|
|
c.mu.RLock()
|
2016-11-08 12:46:44 +03:00
|
|
|
defer c.mu.RUnlock()
|
2016-05-06 21:12:31 +03:00
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
if c.closed {
|
|
|
|
return nil, pool.ErrClosed
|
2016-06-05 14:30:56 +03:00
|
|
|
}
|
2015-12-16 17:11:52 +03:00
|
|
|
|
2016-12-12 18:30:08 +03:00
|
|
|
nodes := make([]*clusterNode, 0, len(c.nodes))
|
2016-11-08 12:46:44 +03:00
|
|
|
for _, node := range c.nodes {
|
|
|
|
nodes = append(nodes, node)
|
2016-01-19 19:36:40 +03:00
|
|
|
}
|
2016-11-08 12:46:44 +03:00
|
|
|
return nodes, nil
|
2016-01-19 19:36:40 +03:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func (c *clusterNodes) Get(addr string) (*clusterNode, error) {
|
|
|
|
var node *clusterNode
|
|
|
|
var ok bool
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2016-05-06 21:12:31 +03:00
|
|
|
c.mu.RLock()
|
2016-11-08 12:46:44 +03:00
|
|
|
if !c.closed {
|
|
|
|
node, ok = c.nodes[addr]
|
|
|
|
}
|
2016-05-06 21:12:31 +03:00
|
|
|
c.mu.RUnlock()
|
|
|
|
if ok {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
node, ok = c.nodes[addr]
|
2016-11-08 12:46:44 +03:00
|
|
|
if ok {
|
|
|
|
return node, nil
|
2015-03-18 13:41:24 +03:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
c.addrs = append(c.addrs, addr)
|
|
|
|
node = newClusterNode(c.opt, addr)
|
|
|
|
c.nodes[addr] = node
|
2016-06-05 14:30:56 +03:00
|
|
|
return node, nil
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func (c *clusterNodes) Random() (*clusterNode, error) {
|
2016-05-06 21:12:31 +03:00
|
|
|
c.mu.RLock()
|
2016-11-08 12:46:44 +03:00
|
|
|
closed := c.closed
|
|
|
|
addrs := c.addrs
|
2016-05-06 21:12:31 +03:00
|
|
|
c.mu.RUnlock()
|
2015-03-18 13:41:24 +03:00
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
if closed {
|
|
|
|
return nil, pool.ErrClosed
|
|
|
|
}
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
return nil, errClusterNoNodes
|
|
|
|
}
|
|
|
|
|
2016-09-23 14:52:19 +03:00
|
|
|
var nodeErr error
|
2016-12-12 18:30:08 +03:00
|
|
|
for i := 0; i <= c.opt.MaxRedirects; i++ {
|
2016-05-06 21:12:31 +03:00
|
|
|
n := rand.Intn(len(addrs))
|
2016-11-08 12:46:44 +03:00
|
|
|
node, err := c.Get(addrs[n])
|
2016-06-05 14:30:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-06 21:12:31 +03:00
|
|
|
|
2016-09-23 14:52:19 +03:00
|
|
|
nodeErr = node.Client.ClusterInfo().Err()
|
|
|
|
if nodeErr == nil {
|
|
|
|
return node, nil
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-23 14:52:19 +03:00
|
|
|
return nil, nodeErr
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type clusterState struct {
|
|
|
|
nodes *clusterNodes
|
|
|
|
slots [][]*clusterNode
|
|
|
|
}
|
|
|
|
|
|
|
|
func newClusterState(nodes *clusterNodes, slots []ClusterSlot) (*clusterState, error) {
|
|
|
|
c := clusterState{
|
|
|
|
nodes: nodes,
|
|
|
|
slots: make([][]*clusterNode, hashtag.SlotNumber),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, slot := range slots {
|
|
|
|
var nodes []*clusterNode
|
|
|
|
for _, slotNode := range slot.Nodes {
|
|
|
|
node, err := c.nodes.Get(slotNode.Addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := slot.Start; i <= slot.End; i++ {
|
|
|
|
c.slots[i] = nodes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2016-10-09 11:18:57 +03:00
|
|
|
if slave := nodes[1]; !slave.Loading() {
|
|
|
|
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]
|
|
|
|
if !slave.Loading() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return slave, 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 {
|
2016-12-16 17:26:48 +03:00
|
|
|
if n.Loading() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func (c *clusterState) slotNodes(slot int) []*clusterNode {
|
|
|
|
if slot < len(c.slots) {
|
|
|
|
return c.slots[slot]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
cmdable
|
|
|
|
|
|
|
|
opt *ClusterOptions
|
|
|
|
cmds map[string]*CommandInfo
|
|
|
|
nodes *clusterNodes
|
|
|
|
_state atomic.Value
|
|
|
|
|
|
|
|
// Reports where slots reloading is in progress.
|
|
|
|
reloading uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClusterClient returns a Redis Cluster client as described in
|
|
|
|
// http://redis.io/topics/cluster-spec.
|
|
|
|
func NewClusterClient(opt *ClusterOptions) *ClusterClient {
|
|
|
|
opt.init()
|
|
|
|
|
|
|
|
c := &ClusterClient{
|
|
|
|
opt: opt,
|
|
|
|
nodes: newClusterNodes(opt),
|
|
|
|
}
|
|
|
|
c.cmdable.process = c.Process
|
|
|
|
|
|
|
|
// Add initial nodes.
|
|
|
|
for _, addr := range opt.Addrs {
|
|
|
|
_, _ = c.nodes.Get(addr)
|
|
|
|
}
|
|
|
|
|
2017-03-04 14:04:27 +03:00
|
|
|
// Preload cluster slots.
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
state, err := c.reloadSlots()
|
|
|
|
if err == nil {
|
|
|
|
c._state.Store(state)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-11-08 12:46:44 +03:00
|
|
|
|
|
|
|
if opt.IdleCheckFrequency > 0 {
|
|
|
|
go c.reaper(opt.IdleCheckFrequency)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func (c *ClusterClient) state() *clusterState {
|
|
|
|
v := c._state.Load()
|
2017-03-04 14:04:27 +03:00
|
|
|
if v != nil {
|
|
|
|
return v.(*clusterState)
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
2017-03-04 14:04:27 +03:00
|
|
|
c.lazyReloadSlots()
|
|
|
|
return nil
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) cmdSlotAndNode(state *clusterState, cmd Cmder) (int, *clusterNode, error) {
|
2016-12-16 15:05:21 +03:00
|
|
|
if state == nil {
|
2016-11-08 12:46:44 +03:00
|
|
|
node, err := c.nodes.Random()
|
2016-12-16 15:05:21 +03:00
|
|
|
return 0, node, err
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
2016-12-16 15:05:21 +03:00
|
|
|
|
2017-01-13 14:39:59 +03:00
|
|
|
cmdInfo := c.cmds[cmd.name()]
|
2016-12-16 15:05:21 +03:00
|
|
|
firstKey := cmd.arg(cmdFirstKeyPos(cmd, cmdInfo))
|
2016-05-06 21:12:31 +03:00
|
|
|
slot := hashtag.Slot(firstKey)
|
2016-12-09 16:52:36 +03:00
|
|
|
|
2016-12-16 15:05:21 +03:00
|
|
|
if cmdInfo != nil && cmdInfo.ReadOnly && c.opt.ReadOnly {
|
2016-05-06 21:12:31 +03:00
|
|
|
if c.opt.RouteByLatency {
|
2016-11-08 12:46:44 +03:00
|
|
|
node, err := state.slotClosestNode(slot)
|
2016-06-05 14:30:56 +03:00
|
|
|
return slot, node, err
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
2016-06-05 14:30:56 +03:00
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
node, err := state.slotSlaveNode(slot)
|
2016-06-05 14:30:56 +03:00
|
|
|
return slot, node, err
|
2015-04-04 16:46:57 +03:00
|
|
|
}
|
2016-06-05 14:30:56 +03:00
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
node, err := state.slotMasterNode(slot)
|
2016-06-05 14:30:56 +03:00
|
|
|
return slot, node, err
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
func (c *ClusterClient) Watch(fn func(*Tx) error, keys ...string) error {
|
2017-03-04 14:04:27 +03:00
|
|
|
state := c.state()
|
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
var node *clusterNode
|
|
|
|
var err error
|
2017-03-04 14:04:27 +03:00
|
|
|
if state != nil && len(keys) > 0 {
|
|
|
|
node, err = state.slotMasterNode(hashtag.Slot(keys[0]))
|
2016-12-03 18:30:13 +03:00
|
|
|
} else {
|
|
|
|
node, err = c.nodes.Random()
|
|
|
|
}
|
2016-11-08 12:46:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return node.Client.Watch(fn, keys...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
2016-06-17 15:09:38 +03:00
|
|
|
func (c *ClusterClient) Process(cmd Cmder) error {
|
2016-11-08 12:46:44 +03:00
|
|
|
slot, node, err := c.cmdSlotAndNode(c.state(), cmd)
|
2016-06-17 15:09:38 +03:00
|
|
|
if err != nil {
|
|
|
|
cmd.setErr(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ask bool
|
2016-06-05 14:10:30 +03:00
|
|
|
for attempt := 0; attempt <= c.opt.MaxRedirects; attempt++ {
|
2015-01-24 15:12:48 +03:00
|
|
|
if ask {
|
2016-05-06 21:12:31 +03:00
|
|
|
pipe := node.Client.Pipeline()
|
2015-01-24 15:12:48 +03:00
|
|
|
pipe.Process(NewCmd("ASKING"))
|
|
|
|
pipe.Process(cmd)
|
2016-10-09 11:18:57 +03:00
|
|
|
_, err = pipe.Exec()
|
2015-11-27 14:35:30 +03:00
|
|
|
pipe.Close()
|
2015-01-24 15:12:48 +03:00
|
|
|
ask = false
|
|
|
|
} else {
|
2016-10-09 11:18:57 +03:00
|
|
|
err = node.Client.Process(cmd)
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2016-10-09 11:18:57 +03:00
|
|
|
// If there is no (real) error - we are done.
|
2016-03-19 17:33:14 +03:00
|
|
|
if err == nil {
|
2016-06-17 15:09:38 +03:00
|
|
|
return nil
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2016-10-09 11:18:57 +03:00
|
|
|
// If slave is loading - read from master.
|
2016-10-09 14:38:31 +03:00
|
|
|
if c.opt.ReadOnly && internal.IsLoadingError(err) {
|
2016-10-09 11:18:57 +03:00
|
|
|
node.loading = time.Now()
|
|
|
|
continue
|
2016-10-09 06:30:01 +03:00
|
|
|
}
|
|
|
|
|
2015-04-07 12:30:06 +03:00
|
|
|
// On network errors try random node.
|
2016-10-09 14:38:31 +03:00
|
|
|
if internal.IsRetryableError(err) {
|
2016-11-08 12:46:44 +03:00
|
|
|
node, err = c.nodes.Random()
|
2016-12-12 18:30:08 +03:00
|
|
|
if err != nil {
|
|
|
|
cmd.setErr(err)
|
|
|
|
return err
|
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-03-04 14:04:27 +03:00
|
|
|
state := c.state()
|
|
|
|
if state != nil && slot >= 0 {
|
|
|
|
master, _ := state.slotMasterNode(slot)
|
2016-10-09 13:30:45 +03:00
|
|
|
if moved && (master == nil || master.Client.getAddr() != addr) {
|
|
|
|
c.lazyReloadSlots()
|
|
|
|
}
|
2015-03-18 13:41:24 +03:00
|
|
|
}
|
2016-05-06 21:12:31 +03:00
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
node, err = c.nodes.Get(addr)
|
2016-06-17 15:09:38 +03:00
|
|
|
if err != nil {
|
|
|
|
cmd.setErr(err)
|
|
|
|
return err
|
|
|
|
}
|
2016-11-08 12:46:44 +03:00
|
|
|
|
2015-03-18 13:41:24 +03:00
|
|
|
continue
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2016-12-12 18:30:08 +03:00
|
|
|
// ForEachNode concurrently calls the fn on each ever known node in the cluster.
|
|
|
|
// It returns the first error if any.
|
|
|
|
func (c *ClusterClient) ForEachNode(fn func(client *Client) error) error {
|
|
|
|
nodes, err := c.nodes.All()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
errCh := make(chan error, 1)
|
|
|
|
for _, node := range nodes {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(node *clusterNode) {
|
|
|
|
defer wg.Done()
|
|
|
|
err := fn(node.Client)
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case errCh <- err:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(node)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 15:09:38 +03:00
|
|
|
// ForEachMaster concurrently calls the fn on each master node in the cluster.
|
|
|
|
// It returns the first error if any.
|
|
|
|
func (c *ClusterClient) ForEachMaster(fn func(client *Client) error) error {
|
2016-11-08 12:46:44 +03:00
|
|
|
state := c.state()
|
|
|
|
if state == nil {
|
2017-03-04 14:04:27 +03:00
|
|
|
return errNilClusterState
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
2016-06-17 15:09:38 +03:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
visited := make(map[*clusterNode]struct{})
|
2016-10-02 15:44:01 +03:00
|
|
|
errCh := make(chan error, 1)
|
2016-11-08 12:46:44 +03:00
|
|
|
for _, nodes := range state.slots {
|
2016-06-17 15:09:38 +03:00
|
|
|
if len(nodes) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
master := nodes[0]
|
|
|
|
if _, ok := visited[master]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
visited[master] = struct{}{}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}(master)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
nodes, err := c.nodes.All()
|
|
|
|
if err != 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
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
for _, node := range nodes {
|
|
|
|
s := node.Client.connPool.Stats()
|
|
|
|
acc.Requests += s.Requests
|
|
|
|
acc.Hits += s.Hits
|
|
|
|
acc.Timeouts += s.Timeouts
|
|
|
|
acc.TotalConns += s.TotalConns
|
|
|
|
acc.FreeConns += s.FreeConns
|
2015-01-24 15:12:48 +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
|
|
|
|
2016-06-17 15:09:38 +03:00
|
|
|
func (c *ClusterClient) lazyReloadSlots() {
|
|
|
|
if !atomic.CompareAndSwapUint32(&c.reloading, 0, 1) {
|
|
|
|
return
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
2017-03-04 14:04:27 +03:00
|
|
|
|
2016-11-08 12:46:44 +03:00
|
|
|
go func() {
|
2017-03-04 14:04:27 +03:00
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
state, err := c.reloadSlots()
|
|
|
|
if err == pool.ErrClosed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
c._state.Store(state)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(3 * time.Second)
|
2016-11-08 12:46:44 +03:00
|
|
|
atomic.StoreUint32(&c.reloading, 0)
|
|
|
|
}()
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2017-03-04 14:04:27 +03:00
|
|
|
func (c *ClusterClient) reloadSlots() (*clusterState, error) {
|
|
|
|
node, err := c.nodes.Random()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-06 21:12:31 +03:00
|
|
|
|
2017-03-04 14:04:27 +03:00
|
|
|
// TODO: fix race
|
|
|
|
if c.cmds == nil {
|
|
|
|
cmds, err := node.Client.Command().Result()
|
2016-11-08 12:46:44 +03:00
|
|
|
if err != nil {
|
2017-03-04 14:04:27 +03:00
|
|
|
return nil, err
|
2016-11-08 12:46:44 +03:00
|
|
|
}
|
2017-03-04 14:04:27 +03:00
|
|
|
c.cmds = cmds
|
|
|
|
}
|
2015-01-24 15:12:48 +03:00
|
|
|
|
2017-03-04 14:04:27 +03:00
|
|
|
slots, err := node.Client.ClusterSlots().Result()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-05-01 10:42:58 +03:00
|
|
|
}
|
2017-03-04 14:04:27 +03:00
|
|
|
|
|
|
|
return newClusterState(c.nodes, slots)
|
2015-01-24 15:12:48 +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-03-17 19:00:47 +03:00
|
|
|
var n int
|
2016-05-06 21:12:31 +03:00
|
|
|
for _, node := range nodes {
|
|
|
|
nn, 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)
|
2016-03-17 19:00:47 +03:00
|
|
|
} else {
|
|
|
|
n += nn
|
2015-04-05 15:41:16 +03:00
|
|
|
}
|
|
|
|
}
|
2015-05-01 09:33:47 +03:00
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
s := c.PoolStats()
|
2016-04-09 14:52:01 +03:00
|
|
|
internal.Logf(
|
2016-03-17 19:00:47 +03:00
|
|
|
"reaper: removed %d stale conns (TotalConns=%d FreeConns=%d Requests=%d Hits=%d Timeouts=%d)",
|
|
|
|
n, s.TotalConns, s.FreeConns, s.Requests, s.Hits, s.Timeouts,
|
|
|
|
)
|
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{
|
2016-04-09 10:47:15 +03:00
|
|
|
exec: c.pipelineExec,
|
|
|
|
}
|
2016-06-05 12:45:39 +03:00
|
|
|
pipe.cmdable.process = pipe.Process
|
|
|
|
pipe.statefulCmdable.process = pipe.Process
|
|
|
|
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) {
|
2016-04-09 10:47:15 +03:00
|
|
|
return c.Pipeline().pipelined(fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) pipelineExec(cmds []Cmder) error {
|
2016-12-13 18:28:39 +03:00
|
|
|
cmdsMap, err := c.mapCmdsByNode(cmds)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
for i := 0; i <= c.opt.MaxRedirects; i++ {
|
2016-05-06 21:12:31 +03:00
|
|
|
failedCmds := make(map[*clusterNode][]Cmder)
|
2016-04-09 10:47:15 +03:00
|
|
|
|
2016-05-06 21:12:31 +03:00
|
|
|
for node, cmds := range cmdsMap {
|
2016-09-29 15:07:04 +03:00
|
|
|
cn, _, err := node.Client.conn()
|
2016-04-09 10:47:15 +03:00
|
|
|
if err != nil {
|
2016-06-17 15:09:38 +03:00
|
|
|
setCmdsErr(cmds, err)
|
2016-04-09 10:47:15 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
err = c.pipelineProcessCmds(cn, cmds, failedCmds)
|
2017-04-24 12:43:15 +03:00
|
|
|
node.Client.putConn(cn, err)
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
if len(failedCmds) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2016-04-09 10:47:15 +03:00
|
|
|
cmdsMap = failedCmds
|
|
|
|
}
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
var firstErr error
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
if err := cmd.Err(); err != nil {
|
|
|
|
firstErr = err
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-12-03 18:30:13 +03:00
|
|
|
return firstErr
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
func (c *ClusterClient) mapCmdsByNode(cmds []Cmder) (map[*clusterNode][]Cmder, error) {
|
|
|
|
state := c.state()
|
|
|
|
cmdsMap := make(map[*clusterNode][]Cmder)
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
_, node, err := c.cmdSlotAndNode(state, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cmdsMap[node] = append(cmdsMap[node], cmd)
|
|
|
|
}
|
|
|
|
return cmdsMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) pipelineProcessCmds(
|
2016-05-06 21:12:31 +03:00
|
|
|
cn *pool.Conn, cmds []Cmder, failedCmds map[*clusterNode][]Cmder,
|
2016-12-13 18:28:39 +03:00
|
|
|
) error {
|
2016-12-03 18:30:13 +03:00
|
|
|
cn.SetWriteTimeout(c.opt.WriteTimeout)
|
2016-04-09 10:47:15 +03:00
|
|
|
if err := writeCmd(cn, cmds...); err != nil {
|
|
|
|
setCmdsErr(cmds, err)
|
2016-12-13 18:28:39 +03:00
|
|
|
return err
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
// Set read timeout for all commands.
|
|
|
|
cn.SetReadTimeout(c.opt.ReadTimeout)
|
|
|
|
|
|
|
|
return c.pipelineReadCmds(cn, cmds, failedCmds)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) pipelineReadCmds(
|
|
|
|
cn *pool.Conn, cmds []Cmder, failedCmds map[*clusterNode][]Cmder,
|
|
|
|
) error {
|
2016-12-03 18:30:13 +03:00
|
|
|
var firstErr error
|
2016-12-13 18:28:39 +03:00
|
|
|
for _, cmd := range cmds {
|
|
|
|
err := cmd.readReply(cn)
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
if firstErr == nil {
|
|
|
|
firstErr = err
|
2016-06-05 14:30:56 +03:00
|
|
|
}
|
2016-12-13 18:28:39 +03:00
|
|
|
|
|
|
|
err = c.checkMovedErr(cmd, failedCmds)
|
|
|
|
if err != nil && firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
2016-06-05 14:30:56 +03:00
|
|
|
}
|
2016-12-13 18:28:39 +03:00
|
|
|
return firstErr
|
|
|
|
}
|
2016-06-05 14:30:56 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
func (c *ClusterClient) checkMovedErr(cmd Cmder, failedCmds map[*clusterNode][]Cmder) error {
|
|
|
|
moved, ask, addr := internal.IsMovedError(cmd.Err())
|
|
|
|
if moved {
|
|
|
|
c.lazyReloadSlots()
|
2016-12-03 18:30:13 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
node, err := c.nodes.Get(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
failedCmds[node] = append(failedCmds[node], cmd)
|
|
|
|
}
|
|
|
|
if ask {
|
|
|
|
node, err := c.nodes.Get(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
failedCmds[node] = append(failedCmds[node], NewCmd("ASKING"), cmd)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
exec: c.txPipelineExec,
|
|
|
|
}
|
|
|
|
pipe.cmdable.process = pipe.Process
|
|
|
|
pipe.statefulCmdable.process = pipe.Process
|
|
|
|
return &pipe
|
|
|
|
}
|
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
func (c *ClusterClient) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) {
|
2017-04-21 15:50:26 +03:00
|
|
|
return c.TxPipeline().pipelined(fn)
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) txPipelineExec(cmds []Cmder) error {
|
|
|
|
cmdsMap, err := c.mapCmdsBySlot(cmds)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-04 14:04:27 +03:00
|
|
|
state := c.state()
|
|
|
|
if state == nil {
|
|
|
|
return errNilClusterState
|
|
|
|
}
|
|
|
|
|
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-10-13 13:56:24 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
cmdsMap := map[*clusterNode][]Cmder{node: cmds}
|
|
|
|
for i := 0; i <= c.opt.MaxRedirects; i++ {
|
|
|
|
failedCmds := make(map[*clusterNode][]Cmder)
|
|
|
|
|
|
|
|
for node, cmds := range cmdsMap {
|
|
|
|
cn, _, err := node.Client.conn()
|
|
|
|
if err != nil {
|
|
|
|
setCmdsErr(cmds, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.txPipelineProcessCmds(node, cn, cmds, failedCmds)
|
2017-04-24 12:43:15 +03:00
|
|
|
node.Client.putConn(cn, err)
|
2016-12-12 18:30:08 +03:00
|
|
|
}
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
if len(failedCmds) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cmdsMap = failedCmds
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var firstErr error
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
if err := cmd.Err(); err != nil {
|
|
|
|
firstErr = err
|
2016-04-09 10:47:15 +03:00
|
|
|
break
|
2016-10-13 11:27:43 +03:00
|
|
|
}
|
2016-12-13 18:28:39 +03:00
|
|
|
}
|
|
|
|
return firstErr
|
|
|
|
}
|
2016-10-13 13:56:24 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
func (c *ClusterClient) mapCmdsBySlot(cmds []Cmder) (map[int][]Cmder, error) {
|
|
|
|
state := c.state()
|
|
|
|
cmdsMap := make(map[int][]Cmder)
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
slot, _, err := c.cmdSlotAndNode(state, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cmdsMap[slot] = append(cmdsMap[slot], cmd)
|
|
|
|
}
|
|
|
|
return cmdsMap, nil
|
|
|
|
}
|
2016-10-13 13:56:24 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
func (c *ClusterClient) txPipelineProcessCmds(
|
|
|
|
node *clusterNode, cn *pool.Conn, cmds []Cmder, failedCmds map[*clusterNode][]Cmder,
|
|
|
|
) error {
|
|
|
|
cn.SetWriteTimeout(c.opt.WriteTimeout)
|
|
|
|
if err := txPipelineWriteMulti(cn, cmds); err != nil {
|
|
|
|
setCmdsErr(cmds, err)
|
|
|
|
failedCmds[node] = cmds
|
|
|
|
return err
|
|
|
|
}
|
2016-10-13 13:56:24 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
// Set read timeout for all commands.
|
|
|
|
cn.SetReadTimeout(c.opt.ReadTimeout)
|
2016-10-13 13:56:24 +03:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
if err := c.txPipelineReadQueued(cn, cmds, failedCmds); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := pipelineReadCmds(cn, cmds)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClusterClient) txPipelineReadQueued(
|
|
|
|
cn *pool.Conn, cmds []Cmder, failedCmds map[*clusterNode][]Cmder,
|
|
|
|
) error {
|
|
|
|
var firstErr error
|
|
|
|
|
|
|
|
// Parse queued replies.
|
|
|
|
var statusCmd StatusCmd
|
|
|
|
if err := statusCmd.readReply(cn); err != nil && firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
err := statusCmd.readReply(cn)
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.setErr(err)
|
|
|
|
if firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.checkMovedErr(cmd, failedCmds)
|
|
|
|
if err != nil && firstErr == nil {
|
|
|
|
firstErr = err
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
// Parse number of replies.
|
|
|
|
line, err := cn.Rd.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
if err == Nil {
|
|
|
|
err = TxFailedErr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch line[0] {
|
|
|
|
case proto.ErrorReply:
|
|
|
|
return proto.ParseErrorReply(line)
|
|
|
|
case proto.ArrayReply:
|
|
|
|
// ok
|
|
|
|
default:
|
|
|
|
err := fmt.Errorf("redis: expected '*', but got line %q", line)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return firstErr
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|