Skip to content

Commit 86c68be

Browse files
ofekshenawachayim
andauthored
Execute "COMMAND" command only when readonly (#2815)
* remove command command from oss cluster * remove command command from oss cluster * remove cmdInfo from ring --------- Co-authored-by: Chayim <[email protected]>
1 parent 8c69548 commit 86c68be

File tree

4 files changed

+11
-36
lines changed

4 files changed

+11
-36
lines changed

bench_decode_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ func NewClusterClientStub(resp []byte) *ClientStub {
5959
},
6060
})
6161

62-
// init command.
63-
tmpClient := NewClient(&Options{Addr: ":6379"})
64-
cmdsInfo, err := tmpClient.Command(ctx).Result()
65-
_ = tmpClient.Close()
66-
client.cmdsInfoCache = newCmdsInfoCache(func(_ context.Context) (map[string]*CommandInfo, error) {
67-
return cmdsInfo, err
68-
})
69-
7062
stub.Cmdable = client
7163
return stub
7264
}

command.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func writeCmd(wr *proto.Writer, cmd Cmder) error {
7575
return wr.WriteArgs(cmd.Args())
7676
}
7777

78-
func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int {
78+
func cmdFirstKeyPos(cmd Cmder) int {
7979
if pos := cmd.firstKeyPos(); pos != 0 {
8080
return int(pos)
8181
}
@@ -95,10 +95,6 @@ func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int {
9595
return 2
9696
}
9797
}
98-
99-
if info != nil {
100-
return int(info.FirstKeyPos)
101-
}
10298
return 1
10399
}
104100

osscluster.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,6 @@ func (c *ClusterClient) Process(ctx context.Context, cmd Cmder) error {
907907
}
908908

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

922921
if node == nil {
923922
var err error
924-
node, err = c.cmdNode(ctx, cmdInfo, slot)
923+
node, err = c.cmdNode(ctx, cmd.Name(), slot)
925924
if err != nil {
926925
return err
927926
}
@@ -1783,8 +1782,7 @@ func (c *ClusterClient) cmdSlot(ctx context.Context, cmd Cmder) int {
17831782
return args[2].(int)
17841783
}
17851784

1786-
cmdInfo := c.cmdInfo(ctx, cmd.Name())
1787-
return cmdSlot(cmd, cmdFirstKeyPos(cmd, cmdInfo))
1785+
return cmdSlot(cmd, cmdFirstKeyPos(cmd))
17881786
}
17891787

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

17981796
func (c *ClusterClient) cmdNode(
17991797
ctx context.Context,
1800-
cmdInfo *CommandInfo,
1798+
cmdName string,
18011799
slot int,
18021800
) (*clusterNode, error) {
18031801
state, err := c.state.Get(ctx)
18041802
if err != nil {
18051803
return nil, err
18061804
}
18071805

1808-
if c.opt.ReadOnly && cmdInfo != nil && cmdInfo.ReadOnly {
1809-
return c.slotReadOnlyNode(state, slot)
1806+
if c.opt.ReadOnly {
1807+
cmdInfo := c.cmdInfo(ctx, cmdName)
1808+
if cmdInfo != nil && cmdInfo.ReadOnly {
1809+
return c.slotReadOnlyNode(state, slot)
1810+
}
18101811
}
18111812
return state.slotMasterNode(slot)
18121813
}

ring.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -678,21 +678,8 @@ func (c *Ring) cmdsInfo(ctx context.Context) (map[string]*CommandInfo, error) {
678678
return nil, firstErr
679679
}
680680

681-
func (c *Ring) cmdInfo(ctx context.Context, name string) *CommandInfo {
682-
cmdsInfo, err := c.cmdsInfoCache.Get(ctx)
683-
if err != nil {
684-
return nil
685-
}
686-
info := cmdsInfo[name]
687-
if info == nil {
688-
internal.Logger.Printf(ctx, "info for cmd=%s not found", name)
689-
}
690-
return info
691-
}
692-
693681
func (c *Ring) cmdShard(ctx context.Context, cmd Cmder) (*ringShard, error) {
694-
cmdInfo := c.cmdInfo(ctx, cmd.Name())
695-
pos := cmdFirstKeyPos(cmd, cmdInfo)
682+
pos := cmdFirstKeyPos(cmd)
696683
if pos == 0 {
697684
return c.sharding.Random()
698685
}
@@ -760,8 +747,7 @@ func (c *Ring) generalProcessPipeline(
760747
cmdsMap := make(map[string][]Cmder)
761748

762749
for _, cmd := range cmds {
763-
cmdInfo := c.cmdInfo(ctx, cmd.Name())
764-
hash := cmd.stringArg(cmdFirstKeyPos(cmd, cmdInfo))
750+
hash := cmd.stringArg(cmdFirstKeyPos(cmd))
765751
if hash != "" {
766752
hash = c.sharding.Hash(hash)
767753
}

0 commit comments

Comments
 (0)