Skip to content
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

Execute "COMMAND" command only when readonly #2815

Merged
merged 6 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions bench_decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@ func NewClusterClientStub(resp []byte) *ClientStub {
},
})

// init command.
tmpClient := NewClient(&Options{Addr: ":6379"})
cmdsInfo, err := tmpClient.Command(ctx).Result()
_ = tmpClient.Close()
client.cmdsInfoCache = newCmdsInfoCache(func(_ context.Context) (map[string]*CommandInfo, error) {
return cmdsInfo, err
})

stub.Cmdable = client
return stub
}
Expand Down
6 changes: 1 addition & 5 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func writeCmd(wr *proto.Writer, cmd Cmder) error {
return wr.WriteArgs(cmd.Args())
}

func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int {
func cmdFirstKeyPos(cmd Cmder) int {
if pos := cmd.firstKeyPos(); pos != 0 {
return int(pos)
}
Expand All @@ -95,10 +95,6 @@ func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int {
return 2
}
}

if info != nil {
return int(info.FirstKeyPos)
}
return 1
}

Expand Down
15 changes: 8 additions & 7 deletions osscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,6 @@ func (c *ClusterClient) Process(ctx context.Context, cmd Cmder) error {
}

func (c *ClusterClient) process(ctx context.Context, cmd Cmder) error {
cmdInfo := c.cmdInfo(ctx, cmd.Name())
slot := c.cmdSlot(ctx, cmd)
var node *clusterNode
var ask bool
Expand All @@ -921,7 +920,7 @@ func (c *ClusterClient) process(ctx context.Context, cmd Cmder) error {

if node == nil {
var err error
node, err = c.cmdNode(ctx, cmdInfo, slot)
node, err = c.cmdNode(ctx, cmd.Name(), slot)
if err != nil {
return err
}
Expand Down Expand Up @@ -1783,8 +1782,7 @@ func (c *ClusterClient) cmdSlot(ctx context.Context, cmd Cmder) int {
return args[2].(int)
}

cmdInfo := c.cmdInfo(ctx, cmd.Name())
return cmdSlot(cmd, cmdFirstKeyPos(cmd, cmdInfo))
return cmdSlot(cmd, cmdFirstKeyPos(cmd))
}

func cmdSlot(cmd Cmder, pos int) int {
Expand All @@ -1797,16 +1795,19 @@ func cmdSlot(cmd Cmder, pos int) int {

func (c *ClusterClient) cmdNode(
ctx context.Context,
cmdInfo *CommandInfo,
cmdName string,
slot int,
) (*clusterNode, error) {
state, err := c.state.Get(ctx)
if err != nil {
return nil, err
}

if c.opt.ReadOnly && cmdInfo != nil && cmdInfo.ReadOnly {
return c.slotReadOnlyNode(state, slot)
if c.opt.ReadOnly {
cmdInfo := c.cmdInfo(ctx, cmdName)
if cmdInfo != nil && cmdInfo.ReadOnly {
return c.slotReadOnlyNode(state, slot)
}
}
return state.slotMasterNode(slot)
}
Expand Down
18 changes: 2 additions & 16 deletions ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,21 +678,8 @@ func (c *Ring) cmdsInfo(ctx context.Context) (map[string]*CommandInfo, error) {
return nil, firstErr
}

func (c *Ring) cmdInfo(ctx context.Context, name string) *CommandInfo {
cmdsInfo, err := c.cmdsInfoCache.Get(ctx)
if err != nil {
return nil
}
info := cmdsInfo[name]
if info == nil {
internal.Logger.Printf(ctx, "info for cmd=%s not found", name)
}
return info
}

func (c *Ring) cmdShard(ctx context.Context, cmd Cmder) (*ringShard, error) {
cmdInfo := c.cmdInfo(ctx, cmd.Name())
pos := cmdFirstKeyPos(cmd, cmdInfo)
pos := cmdFirstKeyPos(cmd)
if pos == 0 {
return c.sharding.Random()
}
Expand Down Expand Up @@ -760,8 +747,7 @@ func (c *Ring) generalProcessPipeline(
cmdsMap := make(map[string][]Cmder)

for _, cmd := range cmds {
cmdInfo := c.cmdInfo(ctx, cmd.Name())
hash := cmd.stringArg(cmdFirstKeyPos(cmd, cmdInfo))
hash := cmd.stringArg(cmdFirstKeyPos(cmd))
if hash != "" {
hash = c.sharding.Hash(hash)
}
Expand Down
Loading