-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix(txpipeline): keyless commands should take the slot of the keyed #3411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ndyakov
wants to merge
9
commits into
master
Choose a base branch
from
ndyakov/keyless-commands-tx-pipeline
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2a32680
fix(txpipeline): keyless commands should take the slot of the keyed c…
ndyakov fd2f704
Merge branch 'master' into ndyakov/keyless-commands-tx-pipeline
ndyakov fffa489
fix(txpipeline): extract only keyed cmds from all cmds
ndyakov 4320079
chore(test): Add tests for keyless cmds and txpipeline
ndyakov 0f66cd0
fix(cmdSlot): Add preferred random slot
ndyakov 185ec01
fix(cmdSlot): Add shortlist of keyless cmds
ndyakov c368e7e
chore(test): Fix ring test
ndyakov 49906ee
fix(keylessCommands): Add list of keyless commands
ndyakov 450ba06
Merge branch 'master' into ndyakov/keyless-commands-tx-pipeline
ndyakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -998,7 +998,7 @@ func (c *ClusterClient) Process(ctx context.Context, cmd Cmder) error { | |
} | ||
|
||
func (c *ClusterClient) process(ctx context.Context, cmd Cmder) error { | ||
slot := c.cmdSlot(cmd) | ||
slot := c.cmdSlot(cmd, -1) | ||
var node *clusterNode | ||
var moved bool | ||
var ask bool | ||
|
@@ -1344,9 +1344,13 @@ func (c *ClusterClient) mapCmdsByNode(ctx context.Context, cmdsMap *cmdsMap, cmd | |
return err | ||
} | ||
|
||
preferredRandomSlot := -1 | ||
if c.opt.ReadOnly && c.cmdsAreReadOnly(ctx, cmds) { | ||
for _, cmd := range cmds { | ||
slot := c.cmdSlot(cmd) | ||
slot := c.cmdSlot(cmd, preferredRandomSlot) | ||
if preferredRandomSlot == -1 { | ||
preferredRandomSlot = slot | ||
} | ||
node, err := c.slotReadOnlyNode(state, slot) | ||
if err != nil { | ||
return err | ||
|
@@ -1357,7 +1361,10 @@ func (c *ClusterClient) mapCmdsByNode(ctx context.Context, cmdsMap *cmdsMap, cmd | |
} | ||
|
||
for _, cmd := range cmds { | ||
slot := c.cmdSlot(cmd) | ||
slot := c.cmdSlot(cmd, preferredRandomSlot) | ||
if preferredRandomSlot == -1 { | ||
preferredRandomSlot = slot | ||
} | ||
node, err := state.slotMasterNode(slot) | ||
if err != nil { | ||
return err | ||
|
@@ -1519,8 +1526,36 @@ func (c *ClusterClient) processTxPipeline(ctx context.Context, cmds []Cmder) err | |
return err | ||
} | ||
|
||
cmdsMap := c.mapCmdsBySlot(cmds) | ||
cmdsMap := map[int][]Cmder{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider refactoring the slot determination logic for keyed commands (from lines 1528-1556) into a separate helper function to enhance readability and reduce complexity. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
slot := -1 | ||
// get only the keyed commands | ||
keyedCmds := c.keyedCmds(cmds) | ||
if len(keyedCmds) == 0 { | ||
// no keyed commands try random slot | ||
slot = hashtag.RandomSlot() | ||
} else { | ||
// keyed commands, get slot from them | ||
// if more than one slot, return cross slot error | ||
cmdsBySlot := c.mapCmdsBySlot(keyedCmds) | ||
if len(cmdsBySlot) > 1 { | ||
// cross slot error, we have more than one slot for keyed commands | ||
setCmdsErr(cmds, ErrCrossSlot) | ||
return ErrCrossSlot | ||
} | ||
// get the slot, should be only one | ||
for sl := range cmdsBySlot { | ||
slot = sl | ||
break | ||
} | ||
} | ||
// slot was not determined, try random one | ||
if slot == -1 { | ||
slot = hashtag.RandomSlot() | ||
} | ||
cmdsMap[slot] = cmds | ||
|
||
// TxPipeline does not support cross slot transaction. | ||
// double check the commands are in the same slot | ||
if len(cmdsMap) > 1 { | ||
setCmdsErr(cmds, ErrCrossSlot) | ||
return ErrCrossSlot | ||
|
@@ -1566,13 +1601,29 @@ func (c *ClusterClient) processTxPipeline(ctx context.Context, cmds []Cmder) err | |
|
||
func (c *ClusterClient) mapCmdsBySlot(cmds []Cmder) map[int][]Cmder { | ||
cmdsMap := make(map[int][]Cmder) | ||
preferredRandomSlot := -1 | ||
for _, cmd := range cmds { | ||
slot := c.cmdSlot(cmd) | ||
slot := c.cmdSlot(cmd, preferredRandomSlot) | ||
if preferredRandomSlot == -1 { | ||
preferredRandomSlot = slot | ||
} | ||
cmdsMap[slot] = append(cmdsMap[slot], cmd) | ||
} | ||
return cmdsMap | ||
} | ||
|
||
// keyedCmds returns all the keyed commands from the cmds slice | ||
// it determines keyed commands by checking if the command has a first key position | ||
func (c *ClusterClient) keyedCmds(cmds []Cmder) []Cmder { | ||
keyedCmds := make([]Cmder, 0, len(cmds)) | ||
for _, cmd := range cmds { | ||
if cmdFirstKeyPos(cmd) != 0 { | ||
keyedCmds = append(keyedCmds, cmd) | ||
} | ||
} | ||
return keyedCmds | ||
} | ||
|
||
func (c *ClusterClient) processTxPipelineNode( | ||
ctx context.Context, node *clusterNode, cmds []Cmder, failedCmds *cmdsMap, | ||
) { | ||
|
@@ -1885,17 +1936,20 @@ func (c *ClusterClient) cmdInfo(ctx context.Context, name string) *CommandInfo { | |
return info | ||
} | ||
|
||
func (c *ClusterClient) cmdSlot(cmd Cmder) int { | ||
func (c *ClusterClient) cmdSlot(cmd Cmder, preferredRandomSlot int) int { | ||
args := cmd.Args() | ||
if args[0] == "cluster" && (args[1] == "getkeysinslot" || args[1] == "countkeysinslot") { | ||
return args[2].(int) | ||
} | ||
|
||
return cmdSlot(cmd, cmdFirstKeyPos(cmd)) | ||
return cmdSlot(cmd, cmdFirstKeyPos(cmd), preferredRandomSlot) | ||
} | ||
|
||
func cmdSlot(cmd Cmder, pos int) int { | ||
func cmdSlot(cmd Cmder, pos int, preferredRandomSlot int) int { | ||
if pos == 0 { | ||
if preferredRandomSlot != -1 { | ||
return preferredRandomSlot | ||
} | ||
return hashtag.RandomSlot() | ||
} | ||
firstKey := cmd.stringArg(pos) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some other implementations ascertain this information dynamically with a call to https://redis.io/docs/latest/commands/command/ at initialization time. I don't necessarily think go-redis should do that, since it would require a command to execute successfully just for the library to initialize its internal state correctly, but it could be an interesting angle to consider, which would allow you to avoid needing to hardcode this list and maintain this source of truth manually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LINKIWI I agree. We are considering this since there is other data in the Commands output that can and should be used. We will probably start with a static list and update it on initialization, but it will be part of a separate feature.