redis / go-redis

Redis Go client
https://redis.uptrace.dev
BSD 2-Clause "Simplified" License
19.89k stars 2.34k forks source link

Why sentinel client add sentinel name to sentinelAddrs? #1091

Closed Tevic closed 3 years ago

Tevic commented 5 years ago

https://github.com/go-redis/redis/blob/75795aa4236dc7341eefac3bbe945e68c99ef9df/sentinel.go#L377

In discoverSentinels function, sentinel names were added to the sentinelAddrs, i've print the sentinelAddrs, it becomes like this.

[10.200.20.96:26379 bec6046c8e4df72607c6543e07e8e3ccee97e4cc 85cb11379919639d4758e2a2b4e8448da9bb4407]

And in this line. https://github.com/go-redis/redis/blob/75795aa4236dc7341eefac3bbe945e68c99ef9df/sentinel.go#L258 The addr is used to get masterAddr,and those added names will never success if all nodes before them failed.

alpha-baby commented 3 years ago

it's seem a bug, @Tevic

get sentinels info using redis-cli:

centos7:30002> sentinel sentinels mymaster
1)  1) "name"
    2) "9a8829a2c3ea7839c5fc505724d2c0f1117bbaf2"
    3) "ip"
    4) "10.244.0.14"
    5) "port"
    6) "26379"
    7) "runid"
    8) "9a8829a2c3ea7839c5fc505724d2c0f1117bbaf2"
    9) "flags"
   10) "sentinel"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "66"
   19) "last-ping-reply"
   20) "66"
   21) "down-after-milliseconds"
   22) "60000"
   23) "last-hello-message"
   24) "388"
   25) "voted-leader"
   26) "?"
   27) "voted-leader-epoch"
   28) "0"

https://github.com/go-redis/redis/blob/master/sentinel.go#L661

func (c *sentinelFailover) discoverSentinels(ctx context.Context) {
    sentinels, err := c.sentinel.Sentinels(ctx, c.opt.MasterName).Result()
    if err != nil {
        internal.Logger.Printf(ctx, "sentinel: Sentinels master=%q failed: %s", c.opt.MasterName, err)
        return
    }
    for _, sentinel := range sentinels {
        vals := sentinel.([]interface{})
        for i := 0; i < len(vals); i += 2 {
            key := vals[i].(string)
            if key == "name" {
                sentinelAddr := vals[i+1].(string) // get ip and port error
                if !contains(c.sentinelAddrs, sentinelAddr) {
                    internal.Logger.Printf(ctx, "sentinel: discovered new sentinel=%q for master=%q",
                        sentinelAddr, c.opt.MasterName)
                    c.sentinelAddrs = append(c.sentinelAddrs, sentinelAddr)
                }
            }
        }
    }
}

i think go-redis/redis not will switch other sentinel, so it's not seem a bug.