redis / go-redis

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

Incorrect slot calculation for Xgroup commands using Redis cluster #2982

Closed daviddzxy closed 2 months ago

daviddzxy commented 2 months ago

Slot calculations for Xgroup commands are incorrect. It turns out that when f.e. XGroupCreateMkStream function is called the func cmdFirstKeyPos(cmd Cmder) int function returns wrong pos number (It returns 1 instead of 2). As a result of this, the function func Slot(key string) int always receives value "create" as argument. This causes incorrectly calculated slot number. Redis will almost always respond with MOVED error and then the command will be resent to the correct node. This bug is related to issue #2978. I will try to provide PR with a fix.

Expected Behavior

Client should calculate slot for a given key correctly.

Current Behavior

Client does not calculate slot for a given key correctly.

Possible Solution

Use cmd.SetFirstKeyPos(2) in func (c cmdable) XGroupCreate(ctx context.Context, stream, group, start string) *StatusCmd and in others.

Steps to Reproduce

  1. Start redis in cluster mode
  2. Call function XGroupCreate with valid arguments