Skip to content

Commit f99c63b

Browse files
authored
modify Tips and command pplicy in commandInfo (#5)
1 parent 74407a0 commit f99c63b

File tree

4 files changed

+36
-49
lines changed

4 files changed

+36
-49
lines changed

command.go

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3414,7 +3414,7 @@ type CommandInfo struct {
34143414
LastKeyPos int8
34153415
StepCount int8
34163416
ReadOnly bool
3417-
Tips map[string]string
3417+
Tips *routing.CommandPolicy
34183418
}
34193419

34203420
type CommandsInfoCmd struct {
@@ -3547,8 +3547,7 @@ func (cmd *CommandsInfoCmd) readReply(rd *proto.Reader) error {
35473547
return err
35483548
}
35493549

3550-
cmdInfo.Tips = make(map[string]string, tipsLen)
3551-
3550+
rawTips := make(map[string]string, tipsLen)
35523551
for f := 0; f < tipsLen; f++ {
35533552
tip, err := rd.ReadString()
35543553
if err != nil {
@@ -3557,7 +3556,7 @@ func (cmd *CommandsInfoCmd) readReply(rd *proto.Reader) error {
35573556

35583557
// Handle tips that don't have a colon (like "nondeterministic_output")
35593558
if !strings.Contains(tip, ":") {
3560-
cmdInfo.Tips[tip] = ""
3559+
rawTips[tip] = ""
35613560
continue
35623561
}
35633562

@@ -3566,8 +3565,9 @@ func (cmd *CommandsInfoCmd) readReply(rd *proto.Reader) error {
35663565
if !ok {
35673566
return fmt.Errorf("redis: unexpected tip %q in COMMAND reply", tip)
35683567
}
3569-
cmdInfo.Tips[k] = v
3568+
rawTips[k] = v
35703569
}
3570+
cmdInfo.Tips = parseCommandPolicies(rawTips)
35713571

35723572
if err := rd.DiscardNext(); err != nil {
35733573
return err
@@ -3620,47 +3620,33 @@ func (c *cmdsInfoCache) Get(ctx context.Context) (map[string]*CommandInfo, error
36203620
}
36213621

36223622
// ------------------------------------------------------------------------------
3623-
var BuiltinPolicies = map[string]routing.CommandPolicy{
3624-
"ft.create": {Request: routing.ReqSpecial, Response: routing.RespAllSucceeded},
3625-
"ft.alter": {Request: routing.ReqSpecial, Response: routing.RespAllSucceeded},
3626-
"ft.drop": {Request: routing.ReqSpecial, Response: routing.RespAllSucceeded},
3627-
3628-
"mset": {Request: routing.ReqMultiShard, Response: routing.RespAllSucceeded},
3629-
"mget": {Request: routing.ReqMultiShard, Response: routing.RespSpecial},
3630-
"del": {Request: routing.ReqMultiShard, Response: routing.RespAggSum},
3631-
}
3632-
3633-
func newCommandPolicies(commandInfo map[string]*CommandInfo) map[string]routing.CommandPolicy {
3634-
3635-
table := make(map[string]routing.CommandPolicy, len(commandInfo))
3623+
const requestPolicy = "request_policy"
3624+
const responsePolicy = "response_policy"
36363625

3637-
for name, info := range commandInfo {
3638-
req := routing.ReqDefault
3639-
resp := routing.RespAllSucceeded
3626+
func parseCommandPolicies(commandInfoTips map[string]string) *routing.CommandPolicy {
3627+
req := routing.ReqDefault
3628+
resp := routing.RespAllSucceeded
36403629

3641-
if tips := info.Tips; tips != nil {
3642-
if v, ok := tips["request_policy"]; ok {
3643-
if p, err := routing.ParseRequestPolicy(v); err == nil {
3644-
req = p
3645-
}
3630+
if commandInfoTips != nil {
3631+
if v, ok := commandInfoTips[requestPolicy]; ok {
3632+
if p, err := routing.ParseRequestPolicy(v); err == nil {
3633+
req = p
36463634
}
3647-
if v, ok := tips["response_policy"]; ok {
3648-
if p, err := routing.ParseResponsePolicy(v); err == nil {
3649-
resp = p
3650-
}
3635+
}
3636+
if v, ok := commandInfoTips[responsePolicy]; ok {
3637+
if p, err := routing.ParseResponsePolicy(v); err == nil {
3638+
resp = p
36513639
}
3652-
} else {
3653-
return BuiltinPolicies
36543640
}
3655-
table[name] = routing.CommandPolicy{Request: req, Response: resp}
36563641
}
3657-
3658-
if len(table) == 0 {
3659-
for k, v := range BuiltinPolicies {
3660-
table[k] = v
3642+
tips := make(map[string]string, len(commandInfoTips))
3643+
for k, v := range commandInfoTips {
3644+
if k == requestPolicy || k == responsePolicy {
3645+
continue
36613646
}
3647+
tips[k] = v
36623648
}
3663-
return table
3649+
return &routing.CommandPolicy{Request: req, Response: resp, Tips: tips}
36643650
}
36653651

36663652
//------------------------------------------------------------------------------

commands_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/redis/go-redis/v9"
1515
"github.com/redis/go-redis/v9/internal/proto"
16+
"github.com/redis/go-redis/v9/internal/routing"
1617
)
1718

1819
type TimeValue struct {
@@ -664,13 +665,13 @@ var _ = Describe("Commands", func() {
664665

665666
cmd := cmds["touch"]
666667
Expect(cmd.Name).To(Equal("touch"))
667-
Expect(cmd.Tips["request_policy"]).To(Equal("multi_shard"))
668-
Expect(cmd.Tips["response_policy"]).To(Equal("agg_sum"))
668+
Expect(cmd.Tips.Request).To(Equal(routing.ReqMultiShard))
669+
Expect(cmd.Tips.Response).To(Equal(routing.RespAggSum))
669670

670671
cmd = cmds["flushall"]
671672
Expect(cmd.Name).To(Equal("flushall"))
672-
Expect(cmd.Tips["request_policy"]).To(Equal("all_shards"))
673-
Expect(cmd.Tips["response_policy"]).To(Equal("all_succeeded"))
673+
Expect(cmd.Tips.Request).To(Equal(routing.ReqAllShards))
674+
Expect(cmd.Tips.Response).To(Equal(routing.RespAllSucceeded))
674675
})
675676

676677
It("should return all command names", func() {

internal/routing/policy.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,7 @@ func ParseResponsePolicy(raw string) (ResponsePolicy, error) {
115115
type CommandPolicy struct {
116116
Request RequestPolicy
117117
Response ResponsePolicy
118+
// Tips that are not request_policy or response_policy
119+
// e.g nondeterministic_output, nondeterministic_output_order.
120+
Tips map[string]string
118121
}

osscluster.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/redis/go-redis/v9/internal/pool"
2020
"github.com/redis/go-redis/v9/internal/proto"
2121
"github.com/redis/go-redis/v9/internal/rand"
22-
"github.com/redis/go-redis/v9/internal/routing"
2322
)
2423

2524
const (
@@ -913,11 +912,10 @@ func (c *clusterStateHolder) ReloadOrGet(ctx context.Context) (*clusterState, er
913912
// or more underlying connections. It's safe for concurrent use by
914913
// multiple goroutines.
915914
type ClusterClient struct {
916-
opt *ClusterOptions
917-
nodes *clusterNodes
918-
state *clusterStateHolder
919-
cmdsInfoCache *cmdsInfoCache
920-
commandPolicies map[string]routing.CommandPolicy
915+
opt *ClusterOptions
916+
nodes *clusterNodes
917+
state *clusterStateHolder
918+
cmdsInfoCache *cmdsInfoCache
921919
cmdable
922920
hooksMixin
923921
}
@@ -934,7 +932,6 @@ func NewClusterClient(opt *ClusterOptions) *ClusterClient {
934932

935933
c.state = newClusterStateHolder(c.loadState)
936934
c.cmdsInfoCache = newCmdsInfoCache(c.cmdsInfo)
937-
c.commandPolicies = newCommandPolicies(c.cmdsInfoCache.cmds)
938935
c.cmdable = c.Process
939936
c.initHooks(hooks{
940937
dial: nil,

0 commit comments

Comments
 (0)