go-redis icon indicating copy to clipboard operation
go-redis copied to clipboard

go-redis/v8 ClusterClient always reads/writes to DB 0

Open gkodali-zededa opened this issue 2 years ago • 1 comments

Issue tracker is used for reporting bugs and discussing new features. Please use stackoverflow for supporting issues.

go-redis/redis version: v8.11.4

sentinelOptions = &redis.FailoverOptions{
            MasterName:       options.SentinelMaster,
            SentinelUsername: options.SentinelUser,
            SentinelPassword: options.SentinelPass,
            Username:         options.RedisUser,
            Password:         options.RedisPass,
            SentinelAddrs:    options.SentinelServers,
            RouteRandomly:    true,
            DB:               options.DBNumber,                  // XXX: Setting this to non-zero DB number
            DialTimeout:      30 * time.Second,
}
clusterClient := redis.NewFailoverClusterClient(sentinelOptions)

This is how I setup my failover cluster client. All writes/reads (eg: clusterClient.Set(ctx, key, value)) go into DB 0. I expect these operations to be performed on the selected DB that is passed while creating ClusterClient object.

Expected Behavior

Operations should go to the selected DB (non-zero in my tests) used during the creation of ClusterClient object

Current Behavior

With current code all operations go to DB 0 irrespective of the DB value selected during client creation.

Possible Solution

Steps to Reproduce

Create a failover cluster client as above and try to make reads/writes to non-zero DB numbers.

Context (Environment)

Detailed Description

Possible Implementation

gkodali-zededa avatar Feb 16 '22 10:02 gkodali-zededa

this caused by hard-coded DB number in sentinelOptions func which generate Options from FailoverOptions, but with some reason, it uses fixed DB 0

anwam avatar Mar 03 '22 04:03 anwam

Is there any reason for this fixed DB: 0 ?!

rwrz avatar Nov 08 '22 00:11 rwrz

Oh, checking here: #2040 Then checking the code:


	failover := &sentinelFailover{
		opt:           failoverOpt,
		sentinelAddrs: sentinelAddrs,
	}

	opt := failoverOpt.clientOptions()
	opt.Dialer = masterSlaveDialer(failover)
	opt.init()

Any client connection will proceed with the DB number. No reason to use DB numbers with sentinels connections. So, I don't think this issue here really exists.

I'm doing some more testing, but seems it is not a real issue.

rwrz avatar Nov 08 '22 00:11 rwrz

https://redis.io/commands/select/

When using Redis Cluster, the SELECT command cannot be used, since Redis Cluster only supports database zero.

I think this is not a bug in clusterClient because redis don't support other values in cluster mode.

Hamper avatar Mar 22 '23 06:03 Hamper