2015-05-25 16:22:27 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2018-03-07 16:39:56 +03:00
|
|
|
"context"
|
2015-05-25 16:22:27 +03:00
|
|
|
"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"
|
|
|
|
|
2020-03-11 17:29:16 +03:00
|
|
|
"github.com/go-redis/redis/v8/internal"
|
|
|
|
"github.com/go-redis/redis/v8/internal/consistenthash"
|
|
|
|
"github.com/go-redis/redis/v8/internal/hashtag"
|
|
|
|
"github.com/go-redis/redis/v8/internal/pool"
|
2015-05-25 16:22:27 +03:00
|
|
|
)
|
|
|
|
|
2018-07-22 10:59:50 +03:00
|
|
|
// Hash is type of hash function used in consistent hash.
|
2018-07-19 12:10:40 +03:00
|
|
|
type Hash consistenthash.Hash
|
2018-03-06 15:50:48 +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
|
|
|
|
|
2019-07-09 22:00:10 +03:00
|
|
|
// Map of name => password of ring shards, to allow different shards to have
|
|
|
|
// different passwords. It will be ignored if the Password field is set.
|
|
|
|
Passwords 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
|
|
|
|
|
2018-07-22 10:59:50 +03:00
|
|
|
// Hash function used in consistent hash.
|
|
|
|
// Default is crc32.ChecksumIEEE.
|
|
|
|
Hash Hash
|
2018-07-19 12:10:40 +03:00
|
|
|
|
2018-07-22 10:59:50 +03:00
|
|
|
// Number of replicas in consistent hash.
|
|
|
|
// Default is 100 replicas.
|
|
|
|
//
|
|
|
|
// Higher number of replicas will provide less deviation, that is keys will be
|
2018-07-19 12:10:40 +03:00
|
|
|
// distributed to nodes more evenly.
|
|
|
|
//
|
|
|
|
// Following is deviation for common nreplicas:
|
|
|
|
// --------------------------------------------------------
|
|
|
|
// | nreplicas | standard error | 99% confidence interval |
|
|
|
|
// | 10 | 0.3152 | (0.37, 1.98) |
|
|
|
|
// | 100 | 0.0997 | (0.76, 1.28) |
|
|
|
|
// | 1000 | 0.0316 | (0.92, 1.09) |
|
|
|
|
// --------------------------------------------------------
|
2018-07-22 10:59:50 +03:00
|
|
|
//
|
2018-07-19 12:10:40 +03:00
|
|
|
// See https://arxiv.org/abs/1406.2294 for reference
|
2018-07-22 10:59:50 +03:00
|
|
|
HashReplicas int
|
2018-07-19 12:10:40 +03:00
|
|
|
|
2020-03-27 16:48:18 +03:00
|
|
|
// NewClient creates a shard client with provided name and options.
|
|
|
|
NewClient func(name string, opt *Options) *Client
|
|
|
|
|
2019-08-23 14:46:40 +03:00
|
|
|
// Optional hook that is called when a new shard is created.
|
|
|
|
OnNewShard func(*Client)
|
|
|
|
|
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
|
2018-08-12 10:08:21 +03:00
|
|
|
MinIdleConns int
|
|
|
|
MaxConnAge time.Duration
|
2016-03-17 19:00:47 +03:00
|
|
|
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
|
|
|
|
2018-07-22 10:59:50 +03:00
|
|
|
if opt.HashReplicas == 0 {
|
|
|
|
opt.HashReplicas = 100
|
2018-07-19 12:10:40 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-07-09 22:00:10 +03:00
|
|
|
func (opt *RingOptions) clientOptions(shard string) *Options {
|
2015-05-25 16:22:27 +03:00
|
|
|
return &Options{
|
2017-05-25 14:16:39 +03:00
|
|
|
OnConnect: opt.OnConnect,
|
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
DB: opt.DB,
|
2019-07-09 22:00:10 +03:00
|
|
|
Password: opt.getPassword(shard),
|
2015-05-25 16:22:27 +03:00
|
|
|
|
|
|
|
DialTimeout: opt.DialTimeout,
|
|
|
|
ReadTimeout: opt.ReadTimeout,
|
|
|
|
WriteTimeout: opt.WriteTimeout,
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
PoolSize: opt.PoolSize,
|
2018-08-12 10:08:21 +03:00
|
|
|
MinIdleConns: opt.MinIdleConns,
|
|
|
|
MaxConnAge: opt.MaxConnAge,
|
2016-03-17 19:00:47 +03:00
|
|
|
PoolTimeout: opt.PoolTimeout,
|
|
|
|
IdleTimeout: opt.IdleTimeout,
|
|
|
|
IdleCheckFrequency: opt.IdleCheckFrequency,
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 22:00:10 +03:00
|
|
|
func (opt *RingOptions) getPassword(shard string) string {
|
|
|
|
if opt.Password == "" {
|
|
|
|
return opt.Passwords[shard]
|
|
|
|
}
|
|
|
|
return opt.Password
|
|
|
|
}
|
|
|
|
|
2018-03-07 15:08:40 +03:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2018-03-07 15:08:40 +03:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type ringShards struct {
|
2018-07-22 10:59:50 +03:00
|
|
|
opt *RingOptions
|
|
|
|
|
2018-03-07 15:08:40 +03:00
|
|
|
mu sync.RWMutex
|
|
|
|
hash *consistenthash.Map
|
|
|
|
shards map[string]*ringShard // read only
|
|
|
|
list []*ringShard // read only
|
2018-07-23 15:55:13 +03:00
|
|
|
len int
|
2018-03-07 15:08:40 +03:00
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
2018-07-22 10:59:50 +03:00
|
|
|
func newRingShards(opt *RingOptions) *ringShards {
|
2018-03-07 15:08:40 +03:00
|
|
|
return &ringShards{
|
2018-07-22 10:50:26 +03:00
|
|
|
opt: opt,
|
|
|
|
|
2018-07-22 10:59:50 +03:00
|
|
|
hash: newConsistentHash(opt),
|
2018-03-07 15:08:40 +03:00
|
|
|
shards: make(map[string]*ringShard),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ringShards) Add(name string, cl *Client) {
|
|
|
|
shard := &ringShard{Client: cl}
|
|
|
|
c.hash.Add(name)
|
|
|
|
c.shards[name] = shard
|
|
|
|
c.list = append(c.list, shard)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ringShards) List() []*ringShard {
|
|
|
|
c.mu.RLock()
|
|
|
|
list := c.list
|
|
|
|
c.mu.RUnlock()
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ringShards) Hash(key string) string {
|
|
|
|
c.mu.RLock()
|
|
|
|
hash := c.hash.Get(key)
|
|
|
|
c.mu.RUnlock()
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ringShards) GetByKey(key string) (*ringShard, error) {
|
|
|
|
key = hashtag.Key(key)
|
|
|
|
|
|
|
|
c.mu.RLock()
|
|
|
|
|
|
|
|
if c.closed {
|
|
|
|
c.mu.RUnlock()
|
|
|
|
return nil, pool.ErrClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := c.hash.Get(key)
|
|
|
|
if hash == "" {
|
|
|
|
c.mu.RUnlock()
|
|
|
|
return nil, errRingShardsDown
|
|
|
|
}
|
|
|
|
|
|
|
|
shard := c.shards[hash]
|
|
|
|
c.mu.RUnlock()
|
|
|
|
|
|
|
|
return shard, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ringShards) GetByHash(name string) (*ringShard, error) {
|
|
|
|
if name == "" {
|
|
|
|
return c.Random()
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mu.RLock()
|
|
|
|
shard := c.shards[name]
|
|
|
|
c.mu.RUnlock()
|
|
|
|
return shard, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ringShards) Random() (*ringShard, error) {
|
|
|
|
return c.GetByKey(strconv.Itoa(rand.Int()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// heartbeat monitors state of each shard in the ring.
|
|
|
|
func (c *ringShards) Heartbeat(frequency time.Duration) {
|
|
|
|
ticker := time.NewTicker(frequency)
|
|
|
|
defer ticker.Stop()
|
2020-03-11 17:26:42 +03:00
|
|
|
|
|
|
|
ctx := context.TODO()
|
2018-03-07 15:08:40 +03:00
|
|
|
for range ticker.C {
|
|
|
|
var rebalance bool
|
|
|
|
|
|
|
|
c.mu.RLock()
|
|
|
|
|
|
|
|
if c.closed {
|
|
|
|
c.mu.RUnlock()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
shards := c.list
|
|
|
|
c.mu.RUnlock()
|
|
|
|
|
|
|
|
for _, shard := range shards {
|
2020-03-11 17:26:42 +03:00
|
|
|
err := shard.Client.Ping(ctx).Err()
|
2018-03-07 15:08:40 +03:00
|
|
|
if shard.Vote(err == nil || err == pool.ErrPoolTimeout) {
|
2019-06-17 12:32:40 +03:00
|
|
|
internal.Logger.Printf("ring shard state changed: %s", shard)
|
2018-03-07 15:08:40 +03:00
|
|
|
rebalance = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rebalance {
|
|
|
|
c.rebalance()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// rebalance removes dead shards from the Ring.
|
|
|
|
func (c *ringShards) rebalance() {
|
2019-04-22 12:50:13 +03:00
|
|
|
c.mu.RLock()
|
|
|
|
shards := c.shards
|
|
|
|
c.mu.RUnlock()
|
|
|
|
|
2018-07-22 10:59:50 +03:00
|
|
|
hash := newConsistentHash(c.opt)
|
2018-07-23 15:55:13 +03:00
|
|
|
var shardsNum int
|
2019-04-22 12:50:13 +03:00
|
|
|
for name, shard := range shards {
|
2018-03-07 15:08:40 +03:00
|
|
|
if shard.IsUp() {
|
|
|
|
hash.Add(name)
|
2018-07-23 15:55:13 +03:00
|
|
|
shardsNum++
|
2018-03-07 15:08:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 12:50:13 +03:00
|
|
|
c.mu.Lock()
|
2018-03-07 15:08:40 +03:00
|
|
|
c.hash = hash
|
2018-07-23 15:55:13 +03:00
|
|
|
c.len = shardsNum
|
2018-03-07 15:08:40 +03:00
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-07-23 15:55:13 +03:00
|
|
|
func (c *ringShards) Len() int {
|
|
|
|
c.mu.RLock()
|
|
|
|
l := c.len
|
|
|
|
c.mu.RUnlock()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2018-03-07 15:08:40 +03:00
|
|
|
func (c *ringShards) Close() error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
if c.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.closed = true
|
|
|
|
|
|
|
|
var firstErr error
|
|
|
|
for _, shard := range c.shards {
|
|
|
|
if err := shard.Client.Close(); err != nil && firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.hash = nil
|
|
|
|
c.shards = nil
|
|
|
|
c.list = nil
|
|
|
|
|
|
|
|
return firstErr
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2019-05-31 17:03:20 +03:00
|
|
|
type ring struct {
|
|
|
|
opt *RingOptions
|
|
|
|
shards *ringShards
|
2019-07-25 13:53:00 +03:00
|
|
|
cmdsInfoCache *cmdsInfoCache //nolint:structcheck
|
2019-05-31 17:03:20 +03:00
|
|
|
}
|
|
|
|
|
2019-02-01 00:24:44 +03:00
|
|
|
// Ring is a Redis client that uses consistent 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
|
2019-02-01 00:24:44 +03:00
|
|
|
// the ring. When a 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 {
|
2019-05-31 17:03:20 +03:00
|
|
|
*ring
|
2019-08-24 12:22:52 +03:00
|
|
|
cmdable
|
|
|
|
hooks
|
2019-05-31 16:36:57 +03:00
|
|
|
ctx context.Context
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRing(opt *RingOptions) *Ring {
|
2016-06-05 14:10:30 +03:00
|
|
|
opt.init()
|
2018-01-20 13:26:33 +03:00
|
|
|
|
2019-05-31 17:03:20 +03:00
|
|
|
ring := Ring{
|
|
|
|
ring: &ring{
|
|
|
|
opt: opt,
|
|
|
|
shards: newRingShards(opt),
|
|
|
|
},
|
2019-07-04 11:18:06 +03:00
|
|
|
ctx: context.Background(),
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
2019-05-31 17:03:20 +03:00
|
|
|
ring.cmdsInfoCache = newCmdsInfoCache(ring.cmdsInfo)
|
2019-08-24 12:22:52 +03:00
|
|
|
ring.cmdable = ring.Process
|
2019-05-31 17:03:20 +03:00
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
for name, addr := range opt.Addrs {
|
2019-08-23 14:46:40 +03:00
|
|
|
shard := newRingShard(opt, name, addr)
|
|
|
|
ring.shards.Add(name, shard)
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
2018-01-20 13:26:33 +03:00
|
|
|
|
2018-03-07 15:08:40 +03:00
|
|
|
go ring.shards.Heartbeat(opt.HeartbeatFrequency)
|
2018-01-20 13:26:33 +03:00
|
|
|
|
2019-05-31 17:03:20 +03:00
|
|
|
return &ring
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
|
2019-08-23 14:46:40 +03:00
|
|
|
func newRingShard(opt *RingOptions, name, addr string) *Client {
|
|
|
|
clopt := opt.clientOptions(name)
|
|
|
|
clopt.Addr = addr
|
2020-03-27 16:48:18 +03:00
|
|
|
var shard *Client
|
|
|
|
if opt.NewClient != nil {
|
|
|
|
shard = opt.NewClient(name, clopt)
|
|
|
|
} else {
|
|
|
|
shard = NewClient(clopt)
|
|
|
|
}
|
2019-08-23 14:46:40 +03:00
|
|
|
if opt.OnNewShard != nil {
|
|
|
|
opt.OnNewShard(shard)
|
|
|
|
}
|
|
|
|
return shard
|
|
|
|
}
|
|
|
|
|
2018-03-07 16:39:56 +03:00
|
|
|
func (c *Ring) Context() context.Context {
|
2019-07-04 11:18:06 +03:00
|
|
|
return c.ctx
|
2018-03-07 16:39:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Ring) WithContext(ctx context.Context) *Ring {
|
|
|
|
if ctx == nil {
|
|
|
|
panic("nil context")
|
|
|
|
}
|
2019-05-31 17:03:20 +03:00
|
|
|
clone := *c
|
2019-08-24 12:22:52 +03:00
|
|
|
clone.cmdable = clone.Process
|
2020-02-12 14:02:09 +03:00
|
|
|
clone.hooks.lock()
|
2019-05-31 17:03:20 +03:00
|
|
|
clone.ctx = ctx
|
|
|
|
return &clone
|
2018-03-07 16:39:56 +03:00
|
|
|
}
|
|
|
|
|
2019-05-31 17:03:20 +03:00
|
|
|
// Do creates a Cmd from the args and processes the cmd.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *Ring) Do(ctx context.Context, args ...interface{}) *Cmd {
|
|
|
|
cmd := NewCmd(ctx, args...)
|
|
|
|
_ = c.Process(ctx, cmd)
|
2019-05-31 17:03:20 +03:00
|
|
|
return cmd
|
|
|
|
}
|
2019-04-24 09:33:36 +03:00
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *Ring) Process(ctx context.Context, cmd Cmder) error {
|
2019-06-04 13:30:47 +03:00
|
|
|
return c.hooks.process(ctx, cmd, c.process)
|
2017-09-22 12:23:46 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-03-07 15:08:40 +03:00
|
|
|
shards := c.shards.List()
|
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
|
2018-08-12 10:08:21 +03:00
|
|
|
acc.IdleConns += s.IdleConns
|
2016-10-02 15:44:01 +03:00
|
|
|
}
|
|
|
|
return &acc
|
|
|
|
}
|
|
|
|
|
2018-07-23 15:55:13 +03:00
|
|
|
// Len returns the current number of shards in the ring.
|
|
|
|
func (c *Ring) Len() int {
|
|
|
|
return c.shards.Len()
|
|
|
|
}
|
|
|
|
|
2017-04-11 17:29:31 +03:00
|
|
|
// Subscribe subscribes the client to the specified channels.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *Ring) Subscribe(ctx context.Context, channels ...string) *PubSub {
|
2017-04-11 17:29:31 +03:00
|
|
|
if len(channels) == 0 {
|
|
|
|
panic("at least one channel is required")
|
|
|
|
}
|
|
|
|
|
2018-03-07 15:08:40 +03:00
|
|
|
shard, err := c.shards.GetByKey(channels[0])
|
2017-04-11 17:29:31 +03:00
|
|
|
if err != nil {
|
2019-08-09 16:23:56 +03:00
|
|
|
//TODO: return PubSub with sticky error
|
2017-04-11 17:29:31 +03:00
|
|
|
panic(err)
|
|
|
|
}
|
2020-03-11 17:26:42 +03:00
|
|
|
return shard.Client.Subscribe(ctx, channels...)
|
2017-04-11 17:29:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// PSubscribe subscribes the client to the given patterns.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *Ring) PSubscribe(ctx context.Context, channels ...string) *PubSub {
|
2017-04-11 17:29:31 +03:00
|
|
|
if len(channels) == 0 {
|
|
|
|
panic("at least one channel is required")
|
|
|
|
}
|
|
|
|
|
2018-03-07 15:08:40 +03:00
|
|
|
shard, err := c.shards.GetByKey(channels[0])
|
2017-04-11 17:29:31 +03:00
|
|
|
if err != nil {
|
2019-08-09 16:23:56 +03:00
|
|
|
//TODO: return PubSub with sticky error
|
2017-04-11 17:29:31 +03:00
|
|
|
panic(err)
|
|
|
|
}
|
2020-03-11 17:26:42 +03:00
|
|
|
return shard.Client.PSubscribe(ctx, channels...)
|
2017-04-11 17:29:31 +03:00
|
|
|
}
|
|
|
|
|
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.
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *Ring) ForEachShard(
|
|
|
|
ctx context.Context,
|
|
|
|
fn func(ctx context.Context, client *Client) error,
|
|
|
|
) error {
|
2018-03-07 15:08:40 +03:00
|
|
|
shards := c.shards.List()
|
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()
|
2020-03-11 17:26:42 +03:00
|
|
|
err := fn(ctx, shard.Client)
|
2016-10-02 15:44:01 +03:00
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case errCh <- err:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(shard)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 15:21:51 +03:00
|
|
|
func (c *Ring) cmdsInfo() (map[string]*CommandInfo, error) {
|
|
|
|
shards := c.shards.List()
|
|
|
|
firstErr := errRingShardsDown
|
|
|
|
for _, shard := range shards {
|
2020-03-11 17:26:42 +03:00
|
|
|
cmdsInfo, err := shard.Client.Command(context.TODO()).Result()
|
2018-05-17 15:21:51 +03:00
|
|
|
if err == nil {
|
|
|
|
return cmdsInfo, nil
|
2016-05-06 21:12:31 +03:00
|
|
|
}
|
2018-05-17 15:21:51 +03:00
|
|
|
if firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, firstErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Ring) cmdInfo(name string) *CommandInfo {
|
|
|
|
cmdsInfo, err := c.cmdsInfoCache.Get()
|
2017-06-17 12:34:39 +03:00
|
|
|
if err != nil {
|
2016-05-06 21:12:31 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-03-06 15:50:48 +03:00
|
|
|
info := cmdsInfo[name]
|
2017-08-31 15:22:47 +03:00
|
|
|
if info == nil {
|
2019-06-17 12:32:40 +03:00
|
|
|
internal.Logger.Printf("info for cmd=%s not found", name)
|
2017-08-31 15:22:47 +03:00
|
|
|
}
|
|
|
|
return info
|
2016-05-06 21:12:31 +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 {
|
2018-03-07 15:08:40 +03:00
|
|
|
return c.shards.Random()
|
2017-12-28 17:41:05 +03:00
|
|
|
}
|
|
|
|
firstKey := cmd.stringArg(pos)
|
2018-03-07 15:08:40 +03:00
|
|
|
return c.shards.GetByKey(firstKey)
|
2016-10-09 13:30:45 +03:00
|
|
|
}
|
|
|
|
|
2019-06-04 13:30:47 +03:00
|
|
|
func (c *Ring) process(ctx context.Context, cmd Cmder) error {
|
2019-08-08 16:54:38 +03:00
|
|
|
err := c._process(ctx, cmd)
|
|
|
|
if err != nil {
|
2020-02-03 12:53:47 +03:00
|
|
|
cmd.SetErr(err)
|
2019-08-08 16:54:38 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Ring) _process(ctx context.Context, cmd Cmder) error {
|
|
|
|
var lastErr error
|
2018-09-07 11:45:56 +03:00
|
|
|
for attempt := 0; attempt <= c.opt.MaxRetries; attempt++ {
|
|
|
|
if attempt > 0 {
|
2019-07-30 12:13:00 +03:00
|
|
|
if err := internal.Sleep(ctx, c.retryBackoff(attempt)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-07 11:45:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
shard, err := c.cmdShard(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
lastErr = shard.Client.Process(ctx, cmd)
|
2019-08-08 16:54:38 +03:00
|
|
|
if lastErr == nil || !isRetryableError(lastErr, cmd.readTimeout() == nil) {
|
|
|
|
return lastErr
|
2018-09-07 11:45:56 +03:00
|
|
|
}
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
2019-08-08 16:54:38 +03:00
|
|
|
return lastErr
|
2015-05-25 16:22:27 +03:00
|
|
|
}
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *Ring) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
|
|
|
|
return c.Pipeline().Pipelined(ctx, fn)
|
2019-05-31 17:03:20 +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{
|
2019-07-25 13:28:15 +03:00
|
|
|
ctx: c.ctx,
|
2018-01-20 13:26:33 +03:00
|
|
|
exec: c.processPipeline,
|
2015-06-04 11:50:24 +03:00
|
|
|
}
|
2019-05-31 17:03:20 +03:00
|
|
|
pipe.init()
|
2016-06-05 12:45:39 +03:00
|
|
|
return &pipe
|
2015-06-04 11:50:24 +03:00
|
|
|
}
|
|
|
|
|
2019-06-04 13:30:47 +03:00
|
|
|
func (c *Ring) processPipeline(ctx context.Context, cmds []Cmder) error {
|
2019-07-19 12:00:11 +03:00
|
|
|
return c.hooks.processPipeline(ctx, cmds, func(ctx context.Context, cmds []Cmder) error {
|
|
|
|
return c.generalProcessPipeline(ctx, cmds, false)
|
|
|
|
})
|
2018-01-20 13:26:33 +03:00
|
|
|
}
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *Ring) TxPipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
|
|
|
|
return c.TxPipeline().Pipelined(ctx, fn)
|
2019-07-19 12:00:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Ring) TxPipeline() Pipeliner {
|
|
|
|
pipe := Pipeline{
|
2019-07-25 13:28:15 +03:00
|
|
|
ctx: c.ctx,
|
2019-07-19 12:00:11 +03:00
|
|
|
exec: c.processTxPipeline,
|
|
|
|
}
|
|
|
|
pipe.init()
|
|
|
|
return &pipe
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Ring) processTxPipeline(ctx context.Context, cmds []Cmder) error {
|
|
|
|
return c.hooks.processPipeline(ctx, cmds, func(ctx context.Context, cmds []Cmder) error {
|
|
|
|
return c.generalProcessPipeline(ctx, cmds, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Ring) generalProcessPipeline(
|
|
|
|
ctx context.Context, cmds []Cmder, tx bool,
|
|
|
|
) 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())
|
2018-03-07 15:08:40 +03:00
|
|
|
hash := cmd.stringArg(cmdFirstKeyPos(cmd, cmdInfo))
|
|
|
|
if hash != "" {
|
|
|
|
hash = c.shards.Hash(hashtag.Key(hash))
|
2015-06-24 15:37:41 +03:00
|
|
|
}
|
2018-03-07 15:08:40 +03:00
|
|
|
cmdsMap[hash] = append(cmdsMap[hash], cmd)
|
2015-06-04 11:50:24 +03:00
|
|
|
}
|
|
|
|
|
2019-08-08 16:54:38 +03:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for hash, cmds := range cmdsMap {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(hash string, cmds []Cmder) {
|
|
|
|
defer wg.Done()
|
2015-06-04 11:50:24 +03:00
|
|
|
|
2020-02-14 15:30:07 +03:00
|
|
|
_ = c.processShardPipeline(ctx, hash, cmds, tx)
|
2019-08-08 16:54:38 +03:00
|
|
|
}(hash, cmds)
|
|
|
|
}
|
2017-08-31 15:22:47 +03:00
|
|
|
|
2019-08-08 16:54:38 +03:00
|
|
|
wg.Wait()
|
|
|
|
return cmdsFirstErr(cmds)
|
|
|
|
}
|
2015-06-04 11:50:24 +03:00
|
|
|
|
2019-08-08 16:54:38 +03:00
|
|
|
func (c *Ring) processShardPipeline(
|
|
|
|
ctx context.Context, hash string, cmds []Cmder, tx bool,
|
|
|
|
) error {
|
|
|
|
//TODO: retry?
|
|
|
|
shard, err := c.shards.GetByHash(hash)
|
|
|
|
if err != nil {
|
2020-02-14 15:30:07 +03:00
|
|
|
setCmdsErr(cmds, err)
|
2019-08-08 16:54:38 +03:00
|
|
|
return err
|
2015-06-04 11:50:24 +03:00
|
|
|
}
|
|
|
|
|
2019-08-08 16:54:38 +03:00
|
|
|
if tx {
|
2020-02-14 15:30:07 +03:00
|
|
|
err = shard.Client.processTxPipeline(ctx, cmds)
|
2020-02-14 16:37:35 +03:00
|
|
|
} else {
|
|
|
|
err = shard.Client.processPipeline(ctx, cmds)
|
2019-08-08 16:54:38 +03:00
|
|
|
}
|
|
|
|
return err
|
2015-06-04 11:50:24 +03:00
|
|
|
}
|
2017-09-25 11:48:44 +03:00
|
|
|
|
2018-03-07 15:08:40 +03:00
|
|
|
// Close closes the ring client, releasing any open resources.
|
|
|
|
//
|
|
|
|
// It is rare to Close a Ring, as the Ring is meant to be long-lived
|
|
|
|
// and shared between many goroutines.
|
|
|
|
func (c *Ring) Close() error {
|
|
|
|
return c.shards.Close()
|
|
|
|
}
|
2018-07-22 10:59:50 +03:00
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
func (c *Ring) Watch(ctx context.Context, fn func(*Tx) error, keys ...string) error {
|
2019-04-22 12:48:06 +03:00
|
|
|
if len(keys) == 0 {
|
|
|
|
return fmt.Errorf("redis: Watch requires at least one key")
|
|
|
|
}
|
|
|
|
|
|
|
|
var shards []*ringShard
|
|
|
|
for _, key := range keys {
|
|
|
|
if key != "" {
|
|
|
|
shard, err := c.shards.GetByKey(hashtag.Key(key))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
shards = append(shards, shard)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(shards) == 0 {
|
|
|
|
return fmt.Errorf("redis: Watch requires at least one shard")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(shards) > 1 {
|
|
|
|
for _, shard := range shards[1:] {
|
|
|
|
if shard.Client != shards[0].Client {
|
|
|
|
err := fmt.Errorf("redis: Watch requires all keys to be in the same shard")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 17:26:42 +03:00
|
|
|
return shards[0].Client.Watch(ctx, fn, keys...)
|
2019-04-22 12:48:06 +03:00
|
|
|
}
|
|
|
|
|
2018-07-22 10:59:50 +03:00
|
|
|
func newConsistentHash(opt *RingOptions) *consistenthash.Map {
|
|
|
|
return consistenthash.New(opt.HashReplicas, consistenthash.Hash(opt.Hash))
|
|
|
|
}
|