Skip to content

Commit

Permalink
Execute "COMMAND" command only when readonly (#2815)
Browse files Browse the repository at this point in the history
* remove command command from oss cluster

* remove command command from oss cluster

* remove cmdInfo from ring

---------

Co-authored-by: Chayim <chayim@users.noreply.github.com>
  • Loading branch information
ofekshenawa and chayim committed Dec 17, 2023
1 parent 8c69548 commit 86c68be
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 36 deletions.
8 changes: 0 additions & 8 deletions bench_decode_test.go
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
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
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
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

0 comments on commit 86c68be

Please sign in to comment.