mirror of https://github.com/go-redis/redis.git
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 <chayim@users.noreply.github.com>
This commit is contained in:
parent
8c695488a2
commit
86c68be278
|
@ -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
|
stub.Cmdable = client
|
||||||
return stub
|
return stub
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,7 +75,7 @@ func writeCmd(wr *proto.Writer, cmd Cmder) error {
|
||||||
return wr.WriteArgs(cmd.Args())
|
return wr.WriteArgs(cmd.Args())
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int {
|
func cmdFirstKeyPos(cmd Cmder) int {
|
||||||
if pos := cmd.firstKeyPos(); pos != 0 {
|
if pos := cmd.firstKeyPos(); pos != 0 {
|
||||||
return int(pos)
|
return int(pos)
|
||||||
}
|
}
|
||||||
|
@ -95,10 +95,6 @@ func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int {
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if info != nil {
|
|
||||||
return int(info.FirstKeyPos)
|
|
||||||
}
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -907,7 +907,6 @@ func (c *ClusterClient) Process(ctx context.Context, cmd Cmder) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
slot := c.cmdSlot(ctx, cmd)
|
||||||
var node *clusterNode
|
var node *clusterNode
|
||||||
var ask bool
|
var ask bool
|
||||||
|
@ -921,7 +920,7 @@ func (c *ClusterClient) process(ctx context.Context, cmd Cmder) error {
|
||||||
|
|
||||||
if node == nil {
|
if node == nil {
|
||||||
var err error
|
var err error
|
||||||
node, err = c.cmdNode(ctx, cmdInfo, slot)
|
node, err = c.cmdNode(ctx, cmd.Name(), slot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1783,8 +1782,7 @@ func (c *ClusterClient) cmdSlot(ctx context.Context, cmd Cmder) int {
|
||||||
return args[2].(int)
|
return args[2].(int)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdInfo := c.cmdInfo(ctx, cmd.Name())
|
return cmdSlot(cmd, cmdFirstKeyPos(cmd))
|
||||||
return cmdSlot(cmd, cmdFirstKeyPos(cmd, cmdInfo))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdSlot(cmd Cmder, pos int) int {
|
func cmdSlot(cmd Cmder, pos int) int {
|
||||||
|
@ -1797,7 +1795,7 @@ func cmdSlot(cmd Cmder, pos int) int {
|
||||||
|
|
||||||
func (c *ClusterClient) cmdNode(
|
func (c *ClusterClient) cmdNode(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
cmdInfo *CommandInfo,
|
cmdName string,
|
||||||
slot int,
|
slot int,
|
||||||
) (*clusterNode, error) {
|
) (*clusterNode, error) {
|
||||||
state, err := c.state.Get(ctx)
|
state, err := c.state.Get(ctx)
|
||||||
|
@ -1805,9 +1803,12 @@ func (c *ClusterClient) cmdNode(
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.opt.ReadOnly && cmdInfo != nil && cmdInfo.ReadOnly {
|
if c.opt.ReadOnly {
|
||||||
|
cmdInfo := c.cmdInfo(ctx, cmdName)
|
||||||
|
if cmdInfo != nil && cmdInfo.ReadOnly {
|
||||||
return c.slotReadOnlyNode(state, slot)
|
return c.slotReadOnlyNode(state, slot)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return state.slotMasterNode(slot)
|
return state.slotMasterNode(slot)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
18
ring.go
18
ring.go
|
@ -678,21 +678,8 @@ func (c *Ring) cmdsInfo(ctx context.Context) (map[string]*CommandInfo, error) {
|
||||||
return nil, firstErr
|
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) {
|
func (c *Ring) cmdShard(ctx context.Context, cmd Cmder) (*ringShard, error) {
|
||||||
cmdInfo := c.cmdInfo(ctx, cmd.Name())
|
pos := cmdFirstKeyPos(cmd)
|
||||||
pos := cmdFirstKeyPos(cmd, cmdInfo)
|
|
||||||
if pos == 0 {
|
if pos == 0 {
|
||||||
return c.sharding.Random()
|
return c.sharding.Random()
|
||||||
}
|
}
|
||||||
|
@ -760,8 +747,7 @@ func (c *Ring) generalProcessPipeline(
|
||||||
cmdsMap := make(map[string][]Cmder)
|
cmdsMap := make(map[string][]Cmder)
|
||||||
|
|
||||||
for _, cmd := range cmds {
|
for _, cmd := range cmds {
|
||||||
cmdInfo := c.cmdInfo(ctx, cmd.Name())
|
hash := cmd.stringArg(cmdFirstKeyPos(cmd))
|
||||||
hash := cmd.stringArg(cmdFirstKeyPos(cmd, cmdInfo))
|
|
||||||
if hash != "" {
|
if hash != "" {
|
||||||
hash = c.sharding.Hash(hash)
|
hash = c.sharding.Hash(hash)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue