123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587 |
- package redis
- import (
- "context"
- "crypto/tls"
- "errors"
- "fmt"
- "math"
- "math/rand"
- "net"
- "strings"
- "sync"
- "sync/atomic"
- "time"
- "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"
- "github.com/go-redis/redis/internal/singleflight"
- )
- var errClusterNoNodes = fmt.Errorf("redis: cluster has no nodes")
- // 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 8.
- MaxRedirects int
- // Enables read-only commands on slave nodes.
- ReadOnly bool
- // Allows routing read-only commands to the closest master or slave node.
- // It automatically enables ReadOnly.
- RouteByLatency bool
- // Allows routing read-only commands to the random master or slave node.
- RouteRandomly bool
- // Following options are copied from Options struct.
- OnConnect func(*Conn) error
- MaxRetries int
- MinRetryBackoff time.Duration
- MaxRetryBackoff time.Duration
- Password string
- 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
- TLSConfig *tls.Config
- }
- func (opt *ClusterOptions) init() {
- if opt.MaxRedirects == -1 {
- opt.MaxRedirects = 0
- } else if opt.MaxRedirects == 0 {
- opt.MaxRedirects = 8
- }
- if opt.RouteByLatency {
- opt.ReadOnly = true
- }
- 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
- }
- 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
- }
- }
- func (opt *ClusterOptions) clientOptions() *Options {
- const disableIdleCheck = -1
- return &Options{
- OnConnect: opt.OnConnect,
- MaxRetries: opt.MaxRetries,
- MinRetryBackoff: opt.MinRetryBackoff,
- MaxRetryBackoff: opt.MaxRetryBackoff,
- Password: opt.Password,
- readOnly: opt.ReadOnly,
- DialTimeout: opt.DialTimeout,
- ReadTimeout: opt.ReadTimeout,
- WriteTimeout: opt.WriteTimeout,
- PoolSize: opt.PoolSize,
- PoolTimeout: opt.PoolTimeout,
- IdleTimeout: opt.IdleTimeout,
- IdleCheckFrequency: disableIdleCheck,
- TLSConfig: opt.TLSConfig,
- }
- }
- //------------------------------------------------------------------------------
- type clusterNode struct {
- Client *Client
- latency uint32 // atomic
- generation uint32 // atomic
- loading uint32 // atomic
- }
- func newClusterNode(clOpt *ClusterOptions, addr string) *clusterNode {
- opt := clOpt.clientOptions()
- opt.Addr = addr
- node := clusterNode{
- Client: NewClient(opt),
- }
- node.latency = math.MaxUint32
- if clOpt.RouteByLatency {
- go node.updateLatency()
- }
- return &node
- }
- func (n *clusterNode) String() string {
- return n.Client.String()
- }
- func (n *clusterNode) Close() error {
- return n.Client.Close()
- }
- func (n *clusterNode) Test() error {
- return n.Client.ClusterInfo().Err()
- }
- func (n *clusterNode) updateLatency() {
- const probes = 10
- var latency uint32
- for i := 0; i < probes; i++ {
- start := time.Now()
- n.Client.Ping()
- probe := uint32(time.Since(start) / time.Microsecond)
- latency = (latency + probe) / 2
- }
- atomic.StoreUint32(&n.latency, latency)
- }
- func (n *clusterNode) Latency() time.Duration {
- latency := atomic.LoadUint32(&n.latency)
- return time.Duration(latency) * time.Microsecond
- }
- func (n *clusterNode) MarkAsLoading() {
- atomic.StoreUint32(&n.loading, uint32(time.Now().Unix()))
- }
- func (n *clusterNode) Loading() bool {
- const minute = int64(time.Minute / time.Second)
- loading := atomic.LoadUint32(&n.loading)
- if loading == 0 {
- return false
- }
- if time.Now().Unix()-int64(loading) < minute {
- return true
- }
- atomic.StoreUint32(&n.loading, 0)
- return false
- }
- func (n *clusterNode) Generation() uint32 {
- return atomic.LoadUint32(&n.generation)
- }
- func (n *clusterNode) SetGeneration(gen uint32) {
- for {
- v := atomic.LoadUint32(&n.generation)
- if gen < v || atomic.CompareAndSwapUint32(&n.generation, v, gen) {
- break
- }
- }
- }
- //------------------------------------------------------------------------------
- type clusterNodes struct {
- opt *ClusterOptions
- mu sync.RWMutex
- allAddrs []string
- allNodes map[string]*clusterNode
- clusterAddrs []string
- closed bool
- nodeCreateGroup singleflight.Group
- _generation uint32 // atomic
- }
- func newClusterNodes(opt *ClusterOptions) *clusterNodes {
- return &clusterNodes{
- opt: opt,
- allAddrs: opt.Addrs,
- allNodes: make(map[string]*clusterNode),
- }
- }
- func (c *clusterNodes) Close() error {
- c.mu.Lock()
- defer c.mu.Unlock()
- if c.closed {
- return nil
- }
- c.closed = true
- var firstErr error
- for _, node := range c.allNodes {
- if err := node.Client.Close(); err != nil && firstErr == nil {
- firstErr = err
- }
- }
- c.allNodes = nil
- c.clusterAddrs = nil
- return firstErr
- }
- func (c *clusterNodes) Addrs() ([]string, error) {
- var addrs []string
- c.mu.RLock()
- closed := c.closed
- if !closed {
- if len(c.clusterAddrs) > 0 {
- addrs = c.clusterAddrs
- } else {
- addrs = c.allAddrs
- }
- }
- c.mu.RUnlock()
- if closed {
- return nil, pool.ErrClosed
- }
- if len(addrs) == 0 {
- return nil, errClusterNoNodes
- }
- return addrs, nil
- }
- func (c *clusterNodes) NextGeneration() uint32 {
- return atomic.AddUint32(&c._generation, 1)
- }
- // GC removes unused nodes.
- func (c *clusterNodes) GC(generation uint32) {
- var collected []*clusterNode
- c.mu.Lock()
- for addr, node := range c.allNodes {
- if node.Generation() >= generation {
- continue
- }
- c.clusterAddrs = remove(c.clusterAddrs, addr)
- delete(c.allNodes, addr)
- collected = append(collected, node)
- }
- c.mu.Unlock()
- for _, node := range collected {
- _ = node.Client.Close()
- }
- }
- func (c *clusterNodes) Get(addr string) (*clusterNode, error) {
- var node *clusterNode
- var err error
- c.mu.RLock()
- if c.closed {
- err = pool.ErrClosed
- } else {
- node = c.allNodes[addr]
- }
- c.mu.RUnlock()
- return node, err
- }
- func (c *clusterNodes) GetOrCreate(addr string) (*clusterNode, error) {
- node, err := c.Get(addr)
- if err != nil {
- return nil, err
- }
- if node != nil {
- return node, nil
- }
- v, err := c.nodeCreateGroup.Do(addr, func() (interface{}, error) {
- node := newClusterNode(c.opt, addr)
- return node, node.Test()
- })
- c.mu.Lock()
- defer c.mu.Unlock()
- if c.closed {
- return nil, pool.ErrClosed
- }
- node, ok := c.allNodes[addr]
- if ok {
- _ = v.(*clusterNode).Close()
- return node, err
- }
- node = v.(*clusterNode)
- c.allAddrs = appendIfNotExists(c.allAddrs, addr)
- if err == nil {
- c.clusterAddrs = append(c.clusterAddrs, addr)
- }
- c.allNodes[addr] = node
- return node, err
- }
- 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
- }
- func (c *clusterNodes) Random() (*clusterNode, error) {
- addrs, err := c.Addrs()
- if err != nil {
- return nil, err
- }
- n := rand.Intn(len(addrs))
- return c.GetOrCreate(addrs[n])
- }
- //------------------------------------------------------------------------------
- type clusterState struct {
- nodes *clusterNodes
- Masters []*clusterNode
- Slaves []*clusterNode
- slots [][]*clusterNode
- generation uint32
- createdAt time.Time
- }
- func newClusterState(
- nodes *clusterNodes, slots []ClusterSlot, origin string,
- ) (*clusterState, error) {
- c := clusterState{
- nodes: nodes,
- slots: make([][]*clusterNode, hashtag.SlotNumber),
- generation: nodes.NextGeneration(),
- createdAt: time.Now(),
- }
- isLoopbackOrigin := isLoopbackAddr(origin)
- for _, slot := range slots {
- var nodes []*clusterNode
- for i, slotNode := range slot.Nodes {
- addr := slotNode.Addr
- if !isLoopbackOrigin && useOriginAddr(origin, addr) {
- addr = origin
- }
- node, err := c.nodes.GetOrCreate(addr)
- if err != nil {
- return nil, err
- }
- node.SetGeneration(c.generation)
- nodes = append(nodes, node)
- if i == 0 {
- c.Masters = appendUniqueNode(c.Masters, node)
- } else {
- c.Slaves = appendUniqueNode(c.Slaves, node)
- }
- }
- for i := slot.Start; i <= slot.End; i++ {
- c.slots[i] = nodes
- }
- }
- time.AfterFunc(time.Minute, func() {
- nodes.GC(c.generation)
- })
- return &c, nil
- }
- func (c *clusterState) slotMasterNode(slot int) (*clusterNode, error) {
- nodes := c.slotNodes(slot)
- if len(nodes) > 0 {
- return nodes[0], nil
- }
- return c.nodes.Random()
- }
- func (c *clusterState) slotSlaveNode(slot int) (*clusterNode, error) {
- nodes := c.slotNodes(slot)
- switch len(nodes) {
- case 0:
- return c.nodes.Random()
- case 1:
- return nodes[0], nil
- case 2:
- if slave := nodes[1]; !slave.Loading() {
- return slave, nil
- }
- return nodes[0], nil
- default:
- 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
- }
- }
- func (c *clusterState) slotClosestNode(slot int) (*clusterNode, error) {
- const threshold = time.Millisecond
- nodes := c.slotNodes(slot)
- if len(nodes) == 0 {
- return c.nodes.Random()
- }
- var node *clusterNode
- for _, n := range nodes {
- if n.Loading() {
- continue
- }
- if node == nil || node.Latency()-n.Latency() > threshold {
- node = n
- }
- }
- return node, nil
- }
- func (c *clusterState) slotRandomNode(slot int) *clusterNode {
- nodes := c.slotNodes(slot)
- n := rand.Intn(len(nodes))
- return nodes[n]
- }
- func (c *clusterState) slotNodes(slot int) []*clusterNode {
- if slot >= 0 && slot < len(c.slots) {
- return c.slots[slot]
- }
- return nil
- }
- func (c *clusterState) IsConsistent() bool {
- if len(c.Masters) > len(c.Slaves) {
- return false
- }
- for _, master := range c.Masters {
- s := master.Client.Info("replication").Val()
- if !strings.Contains(s, "role:master") {
- return false
- }
- }
- for _, slave := range c.Slaves {
- s := slave.Client.Info("replication").Val()
- if !strings.Contains(s, "role:slave") {
- return false
- }
- }
- return true
- }
- //------------------------------------------------------------------------------
- type clusterStateHolder struct {
- load func() (*clusterState, error)
- state atomic.Value
- firstErrMu sync.RWMutex
- firstErr error
- reloading uint32 // atomic
- }
- func newClusterStateHolder(fn func() (*clusterState, error)) *clusterStateHolder {
- return &clusterStateHolder{
- load: fn,
- }
- }
- func (c *clusterStateHolder) Reload() (*clusterState, error) {
- state, err := c.reload()
- if err != nil {
- return nil, err
- }
- if !state.IsConsistent() {
- c.LazyReload()
- }
- return state, nil
- }
- func (c *clusterStateHolder) reload() (*clusterState, error) {
- state, err := c.load()
- if err != nil {
- c.firstErrMu.Lock()
- if c.firstErr == nil {
- c.firstErr = err
- }
- c.firstErrMu.Unlock()
- 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)
- for {
- state, err := c.reload()
- if err != nil {
- return
- }
- time.Sleep(100 * time.Millisecond)
- if state.IsConsistent() {
- return
- }
- }
- }()
- }
- func (c *clusterStateHolder) Get() (*clusterState, error) {
- v := c.state.Load()
- if v != nil {
- state := v.(*clusterState)
- if time.Since(state.createdAt) > time.Minute {
- c.LazyReload()
- }
- return state, nil
- }
- c.firstErrMu.RLock()
- err := c.firstErr
- c.firstErrMu.RUnlock()
- if err != nil {
- return nil, err
- }
- return nil, errors.New("redis: cluster has no state")
- }
- //------------------------------------------------------------------------------
- // 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
- ctx context.Context
- opt *ClusterOptions
- nodes *clusterNodes
- state *clusterStateHolder
- cmdsInfoCache *cmdsInfoCache
- process func(Cmder) error
- processPipeline func([]Cmder) error
- processTxPipeline func([]Cmder) error
- }
- // 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.state = newClusterStateHolder(c.loadState)
- c.cmdsInfoCache = newCmdsInfoCache(c.cmdsInfo)
- c.process = c.defaultProcess
- c.processPipeline = c.defaultProcessPipeline
- c.processTxPipeline = c.defaultProcessTxPipeline
- c.init()
- _, _ = c.state.Reload()
- if opt.IdleCheckFrequency > 0 {
- go c.reaper(opt.IdleCheckFrequency)
- }
- return c
- }
- func (c *ClusterClient) init() {
- c.cmdable.setProcessor(c.Process)
- }
- 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")
- }
- c2 := c.copy()
- c2.ctx = ctx
- return c2
- }
- func (c *ClusterClient) copy() *ClusterClient {
- cp := *c
- cp.init()
- return &cp
- }
- // Options returns read-only Options that were used to create the client.
- func (c *ClusterClient) Options() *ClusterOptions {
- return c.opt
- }
- 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 cmdSlot(cmd Cmder, pos int) int {
- if pos == 0 {
- return hashtag.RandomSlot()
- }
- firstKey := cmd.stringArg(pos)
- return hashtag.Slot(firstKey)
- }
- func (c *ClusterClient) cmdSlot(cmd Cmder) int {
- cmdInfo := c.cmdInfo(cmd.Name())
- return cmdSlot(cmd, cmdFirstKeyPos(cmd, cmdInfo))
- }
- func (c *ClusterClient) cmdSlotAndNode(cmd Cmder) (int, *clusterNode, error) {
- state, err := c.state.Get()
- if err != nil {
- return 0, nil, err
- }
- cmdInfo := c.cmdInfo(cmd.Name())
- slot := cmdSlot(cmd, cmdFirstKeyPos(cmd, cmdInfo))
- if cmdInfo != nil && cmdInfo.ReadOnly && c.opt.ReadOnly {
- if c.opt.RouteByLatency {
- node, err := state.slotClosestNode(slot)
- return slot, node, err
- }
- if c.opt.RouteRandomly {
- node := state.slotRandomNode(slot)
- return slot, node, nil
- }
- node, err := state.slotSlaveNode(slot)
- return slot, node, err
- }
- node, err := state.slotMasterNode(slot)
- return slot, node, err
- }
- 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()
- }
- 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 internal.IsRetryableError(err, true) {
- continue
- }
- moved, ask, addr := internal.IsMovedError(err)
- if moved || ask {
- c.state.LazyReload()
- node, err = c.nodes.GetOrCreate(addr)
- if err != nil {
- return err
- }
- continue
- }
- if err == pool.ErrClosed {
- node, err = c.slotMasterNode(slot)
- if err != nil {
- return err
- }
- continue
- }
- return err
- }
- return err
- }
- // 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()
- }
- func (c *ClusterClient) WrapProcess(
- fn func(oldProcess func(Cmder) error) func(Cmder) error,
- ) {
- c.process = fn(c.process)
- }
- func (c *ClusterClient) Process(cmd Cmder) error {
- return c.process(cmd)
- }
- func (c *ClusterClient) defaultProcess(cmd Cmder) error {
- var node *clusterNode
- var ask bool
- for attempt := 0; attempt <= c.opt.MaxRedirects; attempt++ {
- if attempt > 0 {
- time.Sleep(c.retryBackoff(attempt))
- }
- if node == nil {
- var err error
- _, node, err = c.cmdSlotAndNode(cmd)
- if err != nil {
- cmd.setErr(err)
- break
- }
- }
- var err error
- if ask {
- pipe := node.Client.Pipeline()
- _ = pipe.Process(NewCmd("ASKING"))
- _ = pipe.Process(cmd)
- _, err = pipe.Exec()
- _ = pipe.Close()
- ask = false
- } else {
- err = node.Client.Process(cmd)
- }
- // If there is no error - we are done.
- if err == nil {
- break
- }
- // If slave is loading - read from master.
- if c.opt.ReadOnly && internal.IsLoadingError(err) {
- node.MarkAsLoading()
- continue
- }
- if internal.IsRetryableError(err, true) {
- // Firstly retry the same node.
- if attempt == 0 {
- continue
- }
- // Secondly try random node.
- node, err = c.nodes.Random()
- if err != nil {
- break
- }
- continue
- }
- var moved bool
- var addr string
- moved, ask, addr = internal.IsMovedError(err)
- if moved || ask {
- c.state.LazyReload()
- node, err = c.nodes.GetOrCreate(addr)
- if err != nil {
- break
- }
- continue
- }
- if err == pool.ErrClosed {
- node = nil
- continue
- }
- break
- }
- return cmd.Err()
- }
- // 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 {
- state, err := c.state.Reload()
- if err != nil {
- state, err = c.state.Get()
- if err != nil {
- return err
- }
- }
- var wg sync.WaitGroup
- errCh := make(chan error, 1)
- for _, master := range state.Masters {
- wg.Add(1)
- go func(node *clusterNode) {
- defer wg.Done()
- err := fn(node.Client)
- if err != nil {
- select {
- case errCh <- err:
- default:
- }
- }
- }(master)
- }
- wg.Wait()
- select {
- case err := <-errCh:
- return err
- default:
- return nil
- }
- }
- // ForEachSlave concurrently calls the fn on each slave node in the cluster.
- // It returns the first error if any.
- func (c *ClusterClient) ForEachSlave(fn func(client *Client) error) error {
- state, err := c.state.Reload()
- if err != nil {
- state, err = c.state.Get()
- if err != nil {
- return err
- }
- }
- var wg sync.WaitGroup
- errCh := make(chan error, 1)
- for _, slave := range state.Slaves {
- wg.Add(1)
- go func(node *clusterNode) {
- defer wg.Done()
- err := fn(node.Client)
- if err != nil {
- select {
- case errCh <- err:
- default:
- }
- }
- }(slave)
- }
- wg.Wait()
- select {
- case err := <-errCh:
- return err
- default:
- return nil
- }
- }
- // 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 {
- state, err := c.state.Reload()
- if err != nil {
- state, err = c.state.Get()
- if err != nil {
- return err
- }
- }
- 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:
- }
- }
- }
- for _, node := range state.Masters {
- wg.Add(1)
- go worker(node)
- }
- for _, node := range state.Slaves {
- wg.Add(1)
- go worker(node)
- }
- wg.Wait()
- select {
- case err := <-errCh:
- return err
- default:
- return nil
- }
- }
- // PoolStats returns accumulated connection pool stats.
- func (c *ClusterClient) PoolStats() *PoolStats {
- var acc PoolStats
- state, _ := c.state.Get()
- if state == nil {
- return &acc
- }
- for _, node := range state.Masters {
- s := node.Client.connPool.Stats()
- acc.Hits += s.Hits
- acc.Misses += s.Misses
- acc.Timeouts += s.Timeouts
- acc.TotalConns += s.TotalConns
- acc.FreeConns += s.FreeConns
- acc.StaleConns += s.StaleConns
- }
- for _, node := range state.Slaves {
- s := node.Client.connPool.Stats()
- acc.Hits += s.Hits
- acc.Misses += s.Misses
- acc.Timeouts += s.Timeouts
- acc.TotalConns += s.TotalConns
- acc.FreeConns += s.FreeConns
- acc.StaleConns += s.StaleConns
- }
- return &acc
- }
- func (c *ClusterClient) loadState() (*clusterState, error) {
- addrs, err := c.nodes.Addrs()
- if err != nil {
- return nil, err
- }
- var firstErr error
- for _, addr := range addrs {
- node, err := c.nodes.GetOrCreate(addr)
- if err != nil {
- if firstErr == nil {
- firstErr = err
- }
- continue
- }
- slots, err := node.Client.ClusterSlots().Result()
- if err != nil {
- if firstErr == nil {
- firstErr = err
- }
- continue
- }
- return newClusterState(c.nodes, slots, node.Client.opt.Addr)
- }
- return nil, firstErr
- }
- // reaper closes idle connections to the cluster.
- func (c *ClusterClient) reaper(idleCheckFrequency time.Duration) {
- ticker := time.NewTicker(idleCheckFrequency)
- defer ticker.Stop()
- for range ticker.C {
- nodes, err := c.nodes.All()
- if err != nil {
- break
- }
- for _, node := range nodes {
- _, err := node.Client.connPool.(*pool.ConnPool).ReapStaleConns()
- if err != nil {
- internal.Logf("ReapStaleConns failed: %s", err)
- }
- }
- }
- }
- func (c *ClusterClient) Pipeline() Pipeliner {
- pipe := Pipeline{
- exec: c.processPipeline,
- }
- pipe.statefulCmdable.setProcessor(pipe.Process)
- return &pipe
- }
- func (c *ClusterClient) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
- return c.Pipeline().Pipelined(fn)
- }
- func (c *ClusterClient) WrapProcessPipeline(
- fn func(oldProcess func([]Cmder) error) func([]Cmder) error,
- ) {
- c.processPipeline = fn(c.processPipeline)
- }
- func (c *ClusterClient) defaultProcessPipeline(cmds []Cmder) error {
- cmdsMap, err := c.mapCmdsByNode(cmds)
- if err != nil {
- setCmdsErr(cmds, err)
- return err
- }
- for attempt := 0; attempt <= c.opt.MaxRedirects; attempt++ {
- if attempt > 0 {
- time.Sleep(c.retryBackoff(attempt))
- }
- failedCmds := make(map[*clusterNode][]Cmder)
- for node, cmds := range cmdsMap {
- cn, err := node.Client.getConn()
- if err != nil {
- if err == pool.ErrClosed {
- c.remapCmds(cmds, failedCmds)
- } else {
- setCmdsErr(cmds, err)
- }
- continue
- }
- err = c.pipelineProcessCmds(node, cn, cmds, failedCmds)
- if err == nil || internal.IsRedisError(err) {
- node.Client.connPool.Put(cn)
- } else {
- node.Client.connPool.Remove(cn)
- }
- }
- if len(failedCmds) == 0 {
- break
- }
- cmdsMap = failedCmds
- }
- return firstCmdsErr(cmds)
- }
- func (c *ClusterClient) mapCmdsByNode(cmds []Cmder) (map[*clusterNode][]Cmder, error) {
- state, err := c.state.Get()
- if err != nil {
- setCmdsErr(cmds, err)
- return nil, err
- }
- cmdsMap := make(map[*clusterNode][]Cmder)
- cmdsAreReadOnly := c.cmdsAreReadOnly(cmds)
- for _, cmd := range cmds {
- var node *clusterNode
- var err error
- if cmdsAreReadOnly {
- _, node, err = c.cmdSlotAndNode(cmd)
- } else {
- slot := c.cmdSlot(cmd)
- node, err = state.slotMasterNode(slot)
- }
- if err != nil {
- return nil, err
- }
- cmdsMap[node] = append(cmdsMap[node], cmd)
- }
- return cmdsMap, nil
- }
- 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
- }
- func (c *ClusterClient) remapCmds(cmds []Cmder, failedCmds map[*clusterNode][]Cmder) {
- remappedCmds, err := c.mapCmdsByNode(cmds)
- if err != nil {
- setCmdsErr(cmds, err)
- return
- }
- for node, cmds := range remappedCmds {
- failedCmds[node] = cmds
- }
- }
- func (c *ClusterClient) pipelineProcessCmds(
- node *clusterNode, cn *pool.Conn, cmds []Cmder, failedCmds map[*clusterNode][]Cmder,
- ) error {
- _ = cn.SetWriteTimeout(c.opt.WriteTimeout)
- err := writeCmd(cn, cmds...)
- if err != nil {
- setCmdsErr(cmds, err)
- failedCmds[node] = cmds
- return err
- }
- // 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 {
- for _, cmd := range cmds {
- err := cmd.readReply(cn)
- if err == nil {
- continue
- }
- if c.checkMovedErr(cmd, err, failedCmds) {
- continue
- }
- if internal.IsRedisError(err) {
- continue
- }
- return err
- }
- return nil
- }
- func (c *ClusterClient) checkMovedErr(
- cmd Cmder, err error, failedCmds map[*clusterNode][]Cmder,
- ) bool {
- moved, ask, addr := internal.IsMovedError(err)
- if moved {
- c.state.LazyReload()
- node, err := c.nodes.GetOrCreate(addr)
- if err != nil {
- return false
- }
- failedCmds[node] = append(failedCmds[node], cmd)
- return true
- }
- if ask {
- node, err := c.nodes.GetOrCreate(addr)
- if err != nil {
- return false
- }
- failedCmds[node] = append(failedCmds[node], NewCmd("ASKING"), cmd)
- return true
- }
- return false
- }
- // TxPipeline acts like Pipeline, but wraps queued commands with MULTI/EXEC.
- func (c *ClusterClient) TxPipeline() Pipeliner {
- pipe := Pipeline{
- exec: c.processTxPipeline,
- }
- pipe.statefulCmdable.setProcessor(pipe.Process)
- return &pipe
- }
- func (c *ClusterClient) TxPipelined(fn func(Pipeliner) error) ([]Cmder, error) {
- return c.TxPipeline().Pipelined(fn)
- }
- func (c *ClusterClient) defaultProcessTxPipeline(cmds []Cmder) error {
- state, err := c.state.Get()
- if err != nil {
- return err
- }
- cmdsMap := c.mapCmdsBySlot(cmds)
- for slot, cmds := range cmdsMap {
- node, err := state.slotMasterNode(slot)
- if err != nil {
- setCmdsErr(cmds, err)
- continue
- }
- cmdsMap := map[*clusterNode][]Cmder{node: cmds}
- for attempt := 0; attempt <= c.opt.MaxRedirects; attempt++ {
- if attempt > 0 {
- time.Sleep(c.retryBackoff(attempt))
- }
- failedCmds := make(map[*clusterNode][]Cmder)
- for node, cmds := range cmdsMap {
- cn, err := node.Client.getConn()
- if err != nil {
- if err == pool.ErrClosed {
- c.remapCmds(cmds, failedCmds)
- } else {
- setCmdsErr(cmds, err)
- }
- continue
- }
- err = c.txPipelineProcessCmds(node, cn, cmds, failedCmds)
- if err == nil || internal.IsRedisError(err) {
- node.Client.connPool.Put(cn)
- } else {
- node.Client.connPool.Remove(cn)
- }
- }
- if len(failedCmds) == 0 {
- break
- }
- cmdsMap = failedCmds
- }
- }
- return firstCmdsErr(cmds)
- }
- func (c *ClusterClient) mapCmdsBySlot(cmds []Cmder) map[int][]Cmder {
- cmdsMap := make(map[int][]Cmder)
- for _, cmd := range cmds {
- slot := c.cmdSlot(cmd)
- cmdsMap[slot] = append(cmdsMap[slot], cmd)
- }
- return cmdsMap
- }
- 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
- }
- // Set read timeout for all commands.
- cn.SetReadTimeout(c.opt.ReadTimeout)
- if err := c.txPipelineReadQueued(cn, cmds, failedCmds); err != nil {
- setCmdsErr(cmds, err)
- return err
- }
- return pipelineReadCmds(cn, cmds)
- }
- func (c *ClusterClient) txPipelineReadQueued(
- cn *pool.Conn, cmds []Cmder, failedCmds map[*clusterNode][]Cmder,
- ) error {
- // Parse queued replies.
- var statusCmd StatusCmd
- if err := statusCmd.readReply(cn); err != nil {
- return err
- }
- for _, cmd := range cmds {
- err := statusCmd.readReply(cn)
- if err == nil {
- continue
- }
- if c.checkMovedErr(cmd, err, failedCmds) || internal.IsRedisError(err) {
- continue
- }
- return err
- }
- // 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:
- err := proto.ParseErrorReply(line)
- for _, cmd := range cmds {
- if !c.checkMovedErr(cmd, err, failedCmds) {
- break
- }
- }
- return err
- case proto.ArrayReply:
- // ok
- default:
- err := fmt.Errorf("redis: expected '*', but got line %q", line)
- return err
- }
- return nil
- }
- func (c *ClusterClient) pubSub(channels []string) *PubSub {
- opt := c.opt.clientOptions()
- var node *clusterNode
- return &PubSub{
- opt: opt,
- newConn: func(channels []string) (*pool.Conn, error) {
- if node == nil {
- var slot int
- if len(channels) > 0 {
- slot = hashtag.Slot(channels[0])
- } else {
- slot = -1
- }
- masterNode, err := c.slotMasterNode(slot)
- if err != nil {
- return nil, err
- }
- node = masterNode
- }
- return node.Client.newConn()
- },
- closeConn: func(cn *pool.Conn) error {
- return node.Client.connPool.CloseConn(cn)
- },
- }
- }
- // Subscribe subscribes the client to the specified channels.
- // Channels can be omitted to create empty subscription.
- func (c *ClusterClient) Subscribe(channels ...string) *PubSub {
- pubsub := c.pubSub(channels)
- 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 {
- pubsub := c.pubSub(channels)
- if len(channels) > 0 {
- _ = pubsub.PSubscribe(channels...)
- }
- return pubsub
- }
- func useOriginAddr(originAddr, nodeAddr string) bool {
- nodeHost, nodePort, err := net.SplitHostPort(nodeAddr)
- if err != nil {
- return false
- }
- nodeIP := net.ParseIP(nodeHost)
- if nodeIP == nil {
- return false
- }
- if !nodeIP.IsLoopback() {
- return false
- }
- _, originPort, err := net.SplitHostPort(originAddr)
- if err != nil {
- return false
- }
- return nodePort == originPort
- }
- func isLoopbackAddr(addr string) bool {
- host, _, err := net.SplitHostPort(addr)
- if err != nil {
- return false
- }
- ip := net.ParseIP(host)
- if ip == nil {
- return false
- }
- return ip.IsLoopback()
- }
- func appendUniqueNode(nodes []*clusterNode, node *clusterNode) []*clusterNode {
- for _, n := range nodes {
- if n == node {
- return nodes
- }
- }
- return append(nodes, node)
- }
- 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
- }
|