2015-05-25 16:22:27 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-10-09 13:30:45 +03:00
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
2015-05-25 16:22:27 +03:00
|
|
|
"sync"
|
2016-10-02 15:44:01 +03:00
|
|
|
"sync/atomic"
|
2015-05-25 16:22:27 +03:00
|
|
|
"time"
|
|
|
|
|
2017-02-18 17:42:34 +03:00
|
|
|
"github.com/go-redis/redis/internal"
|
|
|
|
"github.com/go-redis/redis/internal/consistenthash"
|
|
|
|
"github.com/go-redis/redis/internal/hashtag"
|
|
|
|
"github.com/go-redis/redis/internal/pool"
|
2015-05-25 16:22:27 +03:00
|
|
|
)
|
|
|
|
|
2016-08-09 16:32:08 +03:00
|
|
|
var errRingShardsDown = errors.New("redis: all ring shards are down")
|
2015-05-25 16:22:27 +03:00
|
|
|
|
|
|
|
// RingOptions are used to configure a ring client and should be
|
|
|
|
// passed to NewRing.
|
|
|
|
type RingOptions struct {
|
2016-08-09 16:32:08 +03:00
|
|
|
// Map of name => host:port addresses of ring shards.
|
2015-05-25 16:22:27 +03:00
|
|
|
Addrs map[string]string
|
|
|
|
|
2016-08-09 16:32:08 +03:00
|
|
|
// Frequency of PING commands sent to check shards availability.
|
|
|
|
// Shard is considered down after 3 subsequent failed checks.
|
|
|
|
HeartbeatFrequency time.Duration
|
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
// Following options are copied from Options struct.
|
|
|
|
|
2017-05-25 14:16:39 +03:00
|
|
|
OnConnect func(*Conn) error
|
|
|
|
|
2016-06-05 12:45:39 +03:00
|
|
|
DB int
|
2015-05-25 16:22:27 +03:00
|
|
|
Password string
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
MaxRetries int
|
|
|
|
MinRetryBackoff time.Duration
|
|
|
|
MaxRetryBackoff time.Duration
|
2015-06-04 11:50:24 +03:00
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
DialTimeout time.Duration
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
WriteTimeout time.Duration
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
PoolSize int
|
|
|
|
PoolTimeout time.Duration
|
|
|
|
IdleTimeout time.Duration
|
|
|
|
IdleCheckFrequency time.Duration
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
|
2016-08-09 16:32:08 +03:00
|
|
|
func (opt *RingOptions) init() {
|
|
|
|
if opt.HeartbeatFrequency == 0 {
|
|
|
|
opt.HeartbeatFrequency = 500 * time.Millisecond
|
|
|
|
}
|
2017-08-31 15:22:47 +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-08-09 16:32:08 +03:00
|
|
|
}
|
2016-06-05 14:10:30 +03:00
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
func (opt *RingOptions) clientOptions() *Options {
|
|
|
|
return &Options{
|
2017-05-25 14:16:39 +03:00
|
|
|
OnConnect: opt.OnConnect,
|
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
DB: opt.DB,
|
|
|
|
Password: opt.Password,
|
|
|
|
|
|
|
|
DialTimeout: opt.DialTimeout,
|
|
|
|
ReadTimeout: opt.ReadTimeout,
|
|
|
|
WriteTimeout: opt.WriteTimeout,
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
PoolSize: opt.PoolSize,
|
|
|
|
PoolTimeout: opt.PoolTimeout,
|
|
|
|
IdleTimeout: opt.IdleTimeout,
|
|
|
|
IdleCheckFrequency: opt.IdleCheckFrequency,
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ringShard struct {
|
|
|
|
Client *Client
|
2016-10-02 15:44:01 +03:00
|
|
|
down int32
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (shard *ringShard) String() string {
|
|
|
|
var state string
|
|
|
|
if shard.IsUp() {
|
|
|
|
state = "up"
|
|
|
|
} else {
|
|
|
|
state = "down"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s is %s", shard.Client, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (shard *ringShard) IsDown() bool {
|
2016-08-09 16:32:08 +03:00
|
|
|
const threshold = 3
|
2016-10-02 15:44:01 +03:00
|
|
|
return atomic.LoadInt32(&shard.down) >= threshold
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (shard *ringShard) IsUp() bool {
|
|
|
|
return !shard.IsDown()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vote votes to set shard state and returns true if state was changed.
|
|
|
|
func (shard *ringShard) Vote(up bool) bool {
|
|
|
|
if up {
|
|
|
|
changed := shard.IsDown()
|
2016-10-02 15:44:01 +03:00
|
|
|
atomic.StoreInt32(&shard.down, 0)
|
2015-05-25 16:22:27 +03:00
|
|
|
return changed
|
|
|
|
}
|
|
|
|
|
|
|
|
if shard.IsDown() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
atomic.AddInt32(&shard.down, 1)
|
2015-05-25 16:22:27 +03:00
|
|
|
return shard.IsDown()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ring is a Redis client that uses constistent hashing to distribute
|
2015-09-12 09:36:03 +03:00
|
|
|
// keys across multiple Redis servers (shards). It's safe for
|
|
|
|
// concurrent use by multiple goroutines.
|
2015-05-25 16:22:27 +03:00
|
|
|
//
|
2015-11-21 11:20:01 +03:00
|
|
|
// Ring monitors the state of each shard and removes dead shards from
|
2016-07-02 11:07:27 +03:00
|
|
|
// the ring. When shard comes online it is added back to the ring. This
|
2015-05-25 16:22:27 +03:00
|
|
|
// gives you maximum availability and partition tolerance, but no
|
|
|
|
// consistency between different shards or even clients. Each client
|
|
|
|
// uses shards that are available to the client and does not do any
|
|
|
|
// coordination when shard state is changed.
|
|
|
|
//
|
2016-08-09 16:32:08 +03:00
|
|
|
// Ring should be used when you need multiple Redis servers for caching
|
2015-05-25 16:22:27 +03:00
|
|
|
// and can tolerate losing data when one of the servers dies.
|
|
|
|
// Otherwise you should use Redis Cluster.
|
|
|
|
type Ring struct {
|
2016-06-05 12:45:39 +03:00
|
|
|
cmdable
|
2015-05-25 16:22:27 +03:00
|
|
|
|
2015-06-04 11:50:24 +03:00
|
|
|
opt *RingOptions
|
2015-05-25 16:22:27 +03:00
|
|
|
nreplicas int
|
|
|
|
|
2017-09-22 12:23:46 +03:00
|
|
|
mu sync.RWMutex
|
|
|
|
hash *consistenthash.Map
|
|
|
|
shards map[string]*ringShard
|
|
|
|
shardsList []*ringShard
|
2015-05-25 16:22:27 +03:00
|
|
|
|
2018-01-20 13:26:33 +03:00
|
|
|
processPipeline func([]Cmder) error
|
|
|
|
|
2017-07-01 12:51:06 +03:00
|
|
|
cmdsInfoOnce internal.Once
|
2016-10-09 13:30:45 +03:00
|
|
|
cmdsInfo map[string]*CommandInfo
|
2016-05-06 21:12:31 +03:00
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRing(opt *RingOptions) *Ring {
|
|
|
|
const nreplicas = 100
|
2018-01-20 13:26:33 +03:00
|
|
|
|
2016-06-05 14:10:30 +03:00
|
|
|
opt.init()
|
2018-01-20 13:26:33 +03:00
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
ring := &Ring{
|
2015-06-04 11:50:24 +03:00
|
|
|
opt: opt,
|
2015-05-25 16:22:27 +03:00
|
|
|
nreplicas: nreplicas,
|
2015-06-04 11:50:24 +03:00
|
|
|
|
|
|
|
hash: consistenthash.New(nreplicas, nil),
|
|
|
|
shards: make(map[string]*ringShard),
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
2018-01-20 13:26:33 +03:00
|
|
|
ring.processPipeline = ring.defaultProcessPipeline
|
|
|
|
ring.cmdable.setProcessor(ring.Process)
|
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
for name, addr := range opt.Addrs {
|
|
|
|
clopt := opt.clientOptions()
|
|
|
|
clopt.Addr = addr
|
2017-09-22 12:23:46 +03:00
|
|
|
ring.addShard(name, NewClient(clopt))
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
2018-01-20 13:26:33 +03:00
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
go ring.heartbeat()
|
2018-01-20 13:26:33 +03:00
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
return ring
|
|
|
|
}
|
|
|
|
|
2017-09-22 12:23:46 +03:00
|
|
|
func (c *Ring) addShard(name string, cl *Client) {
|
|
|
|
shard := &ringShard{Client: cl}
|
|
|
|
c.mu.Lock()
|
|
|
|
c.hash.Add(name)
|
|
|
|
c.shards[name] = shard
|
|
|
|
c.shardsList = append(c.shardsList, shard)
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-03-20 13:15:21 +03:00
|
|
|
// Options returns read-only Options that were used to create the client.
|
|
|
|
func (c *Ring) Options() *RingOptions {
|
|
|
|
return c.opt
|
|
|
|
}
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
func (c *Ring) retryBackoff(attempt int) time.Duration {
|
|
|
|
return internal.RetryBackoff(attempt, c.opt.MinRetryBackoff, c.opt.MaxRetryBackoff)
|
|
|
|
}
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
// PoolStats returns accumulated connection pool stats.
|
|
|
|
func (c *Ring) PoolStats() *PoolStats {
|
2017-09-22 12:23:46 +03:00
|
|
|
c.mu.RLock()
|
|
|
|
shards := c.shardsList
|
|
|
|
c.mu.RUnlock()
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
var acc PoolStats
|
2017-09-22 12:23:46 +03:00
|
|
|
for _, shard := range shards {
|
2016-10-02 15:44:01 +03:00
|
|
|
s := shard.Client.connPool.Stats()
|
|
|
|
acc.Hits += s.Hits
|
2017-09-22 12:23:46 +03:00
|
|
|
acc.Misses += s.Misses
|
2016-10-02 15:44:01 +03:00
|
|
|
acc.Timeouts += s.Timeouts
|
|
|
|
acc.TotalConns += s.TotalConns
|
|
|
|
acc.FreeConns += s.FreeConns
|
|
|
|
}
|
|
|
|
return &acc
|
|
|
|
}
|
|
|
|
|
2017-04-11 17:29:31 +03:00
|
|
|
// Subscribe subscribes the client to the specified channels.
|
|
|
|
func (c *Ring) Subscribe(channels ...string) *PubSub {
|
|
|
|
if len(channels) == 0 {
|
|
|
|
panic("at least one channel is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
shard, err := c.shardByKey(channels[0])
|
|
|
|
if err != nil {
|
|
|
|
// TODO: return PubSub with sticky error
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return shard.Client.Subscribe(channels...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PSubscribe subscribes the client to the given patterns.
|
|
|
|
func (c *Ring) PSubscribe(channels ...string) *PubSub {
|
|
|
|
if len(channels) == 0 {
|
|
|
|
panic("at least one channel is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
shard, err := c.shardByKey(channels[0])
|
|
|
|
if err != nil {
|
|
|
|
// TODO: return PubSub with sticky error
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return shard.Client.PSubscribe(channels...)
|
|
|
|
}
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
// ForEachShard concurrently calls the fn on each live shard in the ring.
|
|
|
|
// It returns the first error if any.
|
|
|
|
func (c *Ring) ForEachShard(fn func(client *Client) error) error {
|
2017-09-22 12:23:46 +03:00
|
|
|
c.mu.RLock()
|
|
|
|
shards := c.shardsList
|
|
|
|
c.mu.RUnlock()
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
errCh := make(chan error, 1)
|
2017-09-22 12:23:46 +03:00
|
|
|
for _, shard := range shards {
|
2016-10-02 15:44:01 +03:00
|
|
|
if shard.IsDown() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func(shard *ringShard) {
|
|
|
|
defer wg.Done()
|
|
|
|
err := fn(shard.Client)
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case errCh <- err:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(shard)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 15:25:28 +03:00
|
|
|
func (c *Ring) cmdInfo(name string) *CommandInfo {
|
2017-06-17 12:34:39 +03:00
|
|
|
err := c.cmdsInfoOnce.Do(func() error {
|
2017-08-31 15:22:47 +03:00
|
|
|
c.mu.RLock()
|
2017-09-22 12:23:46 +03:00
|
|
|
shards := c.shardsList
|
|
|
|
c.mu.RUnlock()
|
2017-08-31 15:22:47 +03:00
|
|
|
|
2017-06-17 12:34:39 +03:00
|
|
|
var firstErr error
|
2017-09-22 12:23:46 +03:00
|
|
|
for _, shard := range shards {
|
2016-05-06 21:12:31 +03:00
|
|
|
cmdsInfo, err := shard.Client.Command().Result()
|
|
|
|
if err == nil {
|
2016-07-01 15:25:28 +03:00
|
|
|
c.cmdsInfo = cmdsInfo
|
2017-06-17 12:34:39 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if firstErr == nil {
|
|
|
|
firstErr = err
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
|
|
|
}
|
2017-06-17 12:34:39 +03:00
|
|
|
return firstErr
|
2016-05-06 21:12:31 +03:00
|
|
|
})
|
2017-06-17 12:34:39 +03:00
|
|
|
if err != nil {
|
2016-05-06 21:12:31 +03:00
|
|
|
return nil
|
|
|
|
}
|
2017-12-28 18:00:22 +03:00
|
|
|
if c.cmdsInfo == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-31 15:22:47 +03:00
|
|
|
info := c.cmdsInfo[name]
|
|
|
|
if info == nil {
|
|
|
|
internal.Logf("info for cmd=%s not found", name)
|
|
|
|
}
|
|
|
|
return info
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
|
|
|
|
2016-11-30 13:39:14 +03:00
|
|
|
func (c *Ring) shardByKey(key string) (*ringShard, error) {
|
2016-10-09 13:30:45 +03:00
|
|
|
key = hashtag.Key(key)
|
|
|
|
|
2016-07-01 15:25:28 +03:00
|
|
|
c.mu.RLock()
|
2015-05-25 16:22:27 +03:00
|
|
|
|
2016-07-01 15:25:28 +03:00
|
|
|
if c.closed {
|
2016-10-09 13:30:45 +03:00
|
|
|
c.mu.RUnlock()
|
2016-03-14 14:17:33 +03:00
|
|
|
return nil, pool.ErrClosed
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
|
2016-10-09 13:30:45 +03:00
|
|
|
name := c.hash.Get(key)
|
2015-05-25 16:22:27 +03:00
|
|
|
if name == "" {
|
2016-07-01 15:25:28 +03:00
|
|
|
c.mu.RUnlock()
|
2015-05-25 16:22:27 +03:00
|
|
|
return nil, errRingShardsDown
|
|
|
|
}
|
|
|
|
|
2016-11-30 13:39:14 +03:00
|
|
|
shard := c.shards[name]
|
2016-07-01 15:25:28 +03:00
|
|
|
c.mu.RUnlock()
|
2016-11-30 13:39:14 +03:00
|
|
|
return shard, nil
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
|
2016-11-30 13:39:14 +03:00
|
|
|
func (c *Ring) randomShard() (*ringShard, error) {
|
2016-10-09 13:30:45 +03:00
|
|
|
return c.shardByKey(strconv.Itoa(rand.Int()))
|
|
|
|
}
|
|
|
|
|
2016-11-30 13:39:14 +03:00
|
|
|
func (c *Ring) shardByName(name string) (*ringShard, error) {
|
2016-10-09 13:30:45 +03:00
|
|
|
if name == "" {
|
|
|
|
return c.randomShard()
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mu.RLock()
|
2016-11-30 13:39:14 +03:00
|
|
|
shard := c.shards[name]
|
2016-10-09 13:30:45 +03:00
|
|
|
c.mu.RUnlock()
|
2016-11-30 13:39:14 +03:00
|
|
|
return shard, nil
|
2016-10-09 13:30:45 +03:00
|
|
|
}
|
|
|
|
|
2016-11-30 13:39:14 +03:00
|
|
|
func (c *Ring) cmdShard(cmd Cmder) (*ringShard, error) {
|
2017-05-09 12:44:36 +03:00
|
|
|
cmdInfo := c.cmdInfo(cmd.Name())
|
2017-12-28 17:41:05 +03:00
|
|
|
pos := cmdFirstKeyPos(cmd, cmdInfo)
|
|
|
|
if pos == 0 {
|
|
|
|
return c.randomShard()
|
|
|
|
}
|
|
|
|
firstKey := cmd.stringArg(pos)
|
2016-10-09 13:30:45 +03:00
|
|
|
return c.shardByKey(firstKey)
|
|
|
|
}
|
|
|
|
|
2018-01-20 13:26:33 +03:00
|
|
|
func (c *Ring) WrapProcess(fn func(oldProcess func(cmd Cmder) error) func(cmd Cmder) error) {
|
|
|
|
c.ForEachShard(func(c *Client) error {
|
|
|
|
c.WrapProcess(fn)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-01 15:25:28 +03:00
|
|
|
func (c *Ring) Process(cmd Cmder) error {
|
2016-11-30 13:39:14 +03:00
|
|
|
shard, err := c.cmdShard(cmd)
|
2015-05-25 16:22:27 +03:00
|
|
|
if err != nil {
|
|
|
|
cmd.setErr(err)
|
2016-06-17 15:09:38 +03:00
|
|
|
return err
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
2016-11-30 13:39:14 +03:00
|
|
|
return shard.Client.Process(cmd)
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
|
2016-10-09 13:30:45 +03:00
|
|
|
// rebalance removes dead shards from the Ring.
|
2016-07-01 15:25:28 +03:00
|
|
|
func (c *Ring) rebalance() {
|
2016-10-09 13:30:45 +03:00
|
|
|
hash := consistenthash.New(c.nreplicas, nil)
|
2016-07-01 15:25:28 +03:00
|
|
|
for name, shard := range c.shards {
|
2015-05-25 16:22:27 +03:00
|
|
|
if shard.IsUp() {
|
2016-10-09 13:30:45 +03:00
|
|
|
hash.Add(name)
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-09 13:30:45 +03:00
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
c.hash = hash
|
|
|
|
c.mu.Unlock()
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
|
2016-07-02 11:07:27 +03:00
|
|
|
// heartbeat monitors state of each shard in the ring.
|
2016-07-01 15:25:28 +03:00
|
|
|
func (c *Ring) heartbeat() {
|
2016-08-09 16:32:08 +03:00
|
|
|
ticker := time.NewTicker(c.opt.HeartbeatFrequency)
|
2015-05-25 16:22:27 +03:00
|
|
|
defer ticker.Stop()
|
2017-04-02 17:10:47 +03:00
|
|
|
for range ticker.C {
|
2015-05-25 16:22:27 +03:00
|
|
|
var rebalance bool
|
|
|
|
|
2016-07-01 15:25:28 +03:00
|
|
|
c.mu.RLock()
|
2015-05-25 16:22:27 +03:00
|
|
|
|
2016-07-01 15:25:28 +03:00
|
|
|
if c.closed {
|
|
|
|
c.mu.RUnlock()
|
2015-05-25 16:22:27 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-09-22 12:23:46 +03:00
|
|
|
shards := c.shardsList
|
|
|
|
c.mu.RUnlock()
|
|
|
|
|
|
|
|
for _, shard := range shards {
|
2015-05-25 16:22:27 +03:00
|
|
|
err := shard.Client.Ping().Err()
|
2016-03-12 11:52:13 +03:00
|
|
|
if shard.Vote(err == nil || err == pool.ErrPoolTimeout) {
|
2016-04-09 14:52:01 +03:00
|
|
|
internal.Logf("ring shard state changed: %s", shard)
|
2015-05-25 16:22:27 +03:00
|
|
|
rebalance = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rebalance {
|
2016-07-01 15:25:28 +03:00
|
|
|
c.rebalance()
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the ring client, releasing any open resources.
|
|
|
|
//
|
2015-09-12 09:36:03 +03:00
|
|
|
// It is rare to Close a Ring, as the Ring is meant to be long-lived
|
|
|
|
// and shared between many goroutines.
|
2016-12-03 18:30:13 +03:00
|
|
|
func (c *Ring) Close() error {
|
2016-07-01 15:25:28 +03:00
|
|
|
c.mu.Lock()
|
2017-01-28 11:53:10 +03:00
|
|
|
defer c.mu.Unlock()
|
2015-05-25 16:22:27 +03:00
|
|
|
|
2016-07-01 15:25:28 +03:00
|
|
|
if c.closed {
|
2015-05-25 16:22:27 +03:00
|
|
|
return nil
|
|
|
|
}
|
2016-07-01 15:25:28 +03:00
|
|
|
c.closed = true
|
2015-05-25 16:22:27 +03:00
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
var firstErr error
|
2016-07-01 15:25:28 +03:00
|
|
|
for _, shard := range c.shards {
|
2016-12-03 18:30:13 +03:00
|
|
|
if err := shard.Client.Close(); err != nil && firstErr == nil {
|
|
|
|
firstErr = err
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
}
|
2016-07-01 15:25:28 +03:00
|
|
|
c.hash = nil
|
|
|
|
c.shards = nil
|
2017-09-22 12:23:46 +03:00
|
|
|
c.shardsList = nil
|
2015-05-25 16:22:27 +03:00
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
return firstErr
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
2015-06-04 11:50:24 +03:00
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
func (c *Ring) Pipeline() Pipeliner {
|
2016-06-05 12:45:39 +03:00
|
|
|
pipe := Pipeline{
|
2018-01-20 13:26:33 +03:00
|
|
|
exec: c.processPipeline,
|
2015-06-04 11:50:24 +03:00
|
|
|
}
|
2018-01-20 13:26:33 +03:00
|
|
|
pipe.cmdable.setProcessor(pipe.Process)
|
2016-06-05 12:45:39 +03:00
|
|
|
return &pipe
|
2015-06-04 11:50:24 +03:00
|
|
|
}
|
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
func (c *Ring) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
|
2017-09-25 11:48:44 +03:00
|
|
|
return c.Pipeline().Pipelined(fn)
|
2015-06-04 11:50:24 +03:00
|
|
|
}
|
|
|
|
|
2018-01-20 13:26:33 +03:00
|
|
|
func (c *Ring) WrapProcessPipeline(
|
|
|
|
fn func(oldProcess func([]Cmder) error) func([]Cmder) error,
|
|
|
|
) {
|
|
|
|
c.processPipeline = fn(c.processPipeline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Ring) defaultProcessPipeline(cmds []Cmder) error {
|
2015-06-04 11:50:24 +03:00
|
|
|
cmdsMap := make(map[string][]Cmder)
|
|
|
|
for _, cmd := range cmds {
|
2017-05-09 12:44:36 +03:00
|
|
|
cmdInfo := c.cmdInfo(cmd.Name())
|
2017-09-26 11:29:22 +03:00
|
|
|
name := cmd.stringArg(cmdFirstKeyPos(cmd, cmdInfo))
|
2016-10-09 13:30:45 +03:00
|
|
|
if name != "" {
|
|
|
|
name = c.hash.Get(hashtag.Key(name))
|
2015-06-24 15:37:41 +03:00
|
|
|
}
|
2015-06-04 11:50:24 +03:00
|
|
|
cmdsMap[name] = append(cmdsMap[name], cmd)
|
|
|
|
}
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
for attempt := 0; attempt <= c.opt.MaxRetries; attempt++ {
|
|
|
|
if attempt > 0 {
|
|
|
|
time.Sleep(c.retryBackoff(attempt))
|
|
|
|
}
|
|
|
|
|
2016-10-13 13:56:24 +03:00
|
|
|
var failedCmdsMap map[string][]Cmder
|
2015-06-04 11:50:24 +03:00
|
|
|
|
|
|
|
for name, cmds := range cmdsMap {
|
2016-11-30 13:39:14 +03:00
|
|
|
shard, err := c.shardByName(name)
|
2016-10-09 13:30:45 +03:00
|
|
|
if err != nil {
|
|
|
|
setCmdsErr(cmds, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-07-09 10:07:20 +03:00
|
|
|
cn, _, err := shard.Client.getConn()
|
2015-06-04 11:50:24 +03:00
|
|
|
if err != nil {
|
|
|
|
setCmdsErr(cmds, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
canRetry, err := shard.Client.pipelineProcessCmds(cn, cmds)
|
2017-08-31 15:22:47 +03:00
|
|
|
if err == nil || internal.IsRedisError(err) {
|
|
|
|
_ = shard.Client.connPool.Put(cn)
|
2016-10-13 13:56:24 +03:00
|
|
|
continue
|
2015-06-04 11:50:24 +03:00
|
|
|
}
|
2017-08-31 15:22:47 +03:00
|
|
|
_ = shard.Client.connPool.Remove(cn)
|
|
|
|
|
2017-09-27 10:58:06 +03:00
|
|
|
if canRetry && internal.IsRetryableError(err, true) {
|
2016-10-13 13:56:24 +03:00
|
|
|
if failedCmdsMap == nil {
|
|
|
|
failedCmdsMap = make(map[string][]Cmder)
|
|
|
|
}
|
|
|
|
failedCmdsMap[name] = cmds
|
2015-06-04 11:50:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(failedCmdsMap) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cmdsMap = failedCmdsMap
|
|
|
|
}
|
|
|
|
|
2017-08-31 15:22:47 +03:00
|
|
|
return firstCmdsErr(cmds)
|
2015-06-04 11:50:24 +03:00
|
|
|
}
|
2017-09-25 11:48:44 +03:00
|
|
|
|
|
|
|
func (c *Ring) TxPipeline() Pipeliner {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Ring) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|