Skip to content
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
75 changes: 73 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/redis/go-redis/v9/internal"
"github.com/redis/go-redis/v9/internal/hscan"
"github.com/redis/go-redis/v9/internal/proto"
"github.com/redis/go-redis/v9/internal/routing"
"github.com/redis/go-redis/v9/internal/util"
)

Expand Down Expand Up @@ -3413,6 +3414,7 @@ type CommandInfo struct {
LastKeyPos int8
StepCount int8
ReadOnly bool
Tips map[string]string
}

type CommandsInfoCmd struct {
Expand Down Expand Up @@ -3451,7 +3453,7 @@ func (cmd *CommandsInfoCmd) String() string {
func (cmd *CommandsInfoCmd) readReply(rd *proto.Reader) error {
const numArgRedis5 = 6
const numArgRedis6 = 7
const numArgRedis7 = 10
const numArgRedis7 = 10 // Also matches redis 8

n, err := rd.ReadArrayLen()
if err != nil {
Expand Down Expand Up @@ -3539,9 +3541,34 @@ func (cmd *CommandsInfoCmd) readReply(rd *proto.Reader) error {
}

if nn >= numArgRedis7 {
if err := rd.DiscardNext(); err != nil {
// The 8th argument is an array of tips.
tipsLen, err := rd.ReadArrayLen()
if err != nil {
return err
}

cmdInfo.Tips = make(map[string]string, tipsLen)

for f := 0; f < tipsLen; f++ {
tip, err := rd.ReadString()
if err != nil {
return err
}

// Handle tips that don't have a colon (like "nondeterministic_output")
if !strings.Contains(tip, ":") {
cmdInfo.Tips[tip] = ""
continue
}

// Handle normal key:value tips
k, v, ok := strings.Cut(tip, ":")
if !ok {
return fmt.Errorf("redis: unexpected tip %q in COMMAND reply", tip)
}
cmdInfo.Tips[k] = v
}

if err := rd.DiscardNext(); err != nil {
return err
}
Expand Down Expand Up @@ -3592,6 +3619,50 @@ func (c *cmdsInfoCache) Get(ctx context.Context) (map[string]*CommandInfo, error
return c.cmds, err
}

// ------------------------------------------------------------------------------
var BuiltinPolicies = map[string]routing.CommandPolicy{
"ft.create": {Request: routing.ReqSpecial, Response: routing.RespAllSucceeded},
"ft.alter": {Request: routing.ReqSpecial, Response: routing.RespAllSucceeded},
"ft.drop": {Request: routing.ReqSpecial, Response: routing.RespAllSucceeded},

"mset": {Request: routing.ReqMultiShard, Response: routing.RespAllSucceeded},
"mget": {Request: routing.ReqMultiShard, Response: routing.RespSpecial},
"del": {Request: routing.ReqMultiShard, Response: routing.RespAggSum},
}

func newCommandPolicies(commandInfo map[string]*CommandInfo) map[string]routing.CommandPolicy {

table := make(map[string]routing.CommandPolicy, len(commandInfo))

for name, info := range commandInfo {
req := routing.ReqDefault
resp := routing.RespAllSucceeded

if tips := info.Tips; tips != nil {
if v, ok := tips["request_policy"]; ok {
if p, err := routing.ParseRequestPolicy(v); err == nil {
req = p
}
}
if v, ok := tips["response_policy"]; ok {
if p, err := routing.ParseResponsePolicy(v); err == nil {
resp = p
}
}
} else {
return BuiltinPolicies
}
table[name] = routing.CommandPolicy{Request: req, Response: resp}
}

if len(table) == 0 {
for k, v := range BuiltinPolicies {
table[k] = v
}
}
return table
}

//------------------------------------------------------------------------------

type SlowLog struct {
Expand Down
16 changes: 16 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,22 @@ var _ = Describe("Commands", func() {
Expect(cmd.StepCount).To(Equal(int8(0)))
})

It("should Command Tips", Label("NonRedisEnterprise"), func() {
SkipAfterRedisVersion(7.9, "Redis 8 changed the COMMAND reply format")
cmds, err := client.Command(ctx).Result()
Expect(err).NotTo(HaveOccurred())

cmd := cmds["touch"]
Expect(cmd.Name).To(Equal("touch"))
Expect(cmd.Tips["request_policy"]).To(Equal("multi_shard"))
Expect(cmd.Tips["response_policy"]).To(Equal("agg_sum"))

cmd = cmds["flushall"]
Expect(cmd.Name).To(Equal("flushall"))
Expect(cmd.Tips["request_policy"]).To(Equal("all_shards"))
Expect(cmd.Tips["response_policy"]).To(Equal("all_succeeded"))
})

It("should return all command names", func() {
cmdList := client.CommandList(ctx, nil)
Expect(cmdList.Err()).NotTo(HaveOccurred())
Expand Down
12 changes: 7 additions & 5 deletions osscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/redis/go-redis/v9/internal/pool"
"github.com/redis/go-redis/v9/internal/proto"
"github.com/redis/go-redis/v9/internal/rand"
"github.com/redis/go-redis/v9/internal/routing"
)

const (
Expand Down Expand Up @@ -912,10 +913,11 @@ func (c *clusterStateHolder) ReloadOrGet(ctx context.Context) (*clusterState, er
// or more underlying connections. It's safe for concurrent use by
// multiple goroutines.
type ClusterClient struct {
opt *ClusterOptions
nodes *clusterNodes
state *clusterStateHolder
cmdsInfoCache *cmdsInfoCache
opt *ClusterOptions
nodes *clusterNodes
state *clusterStateHolder
cmdsInfoCache *cmdsInfoCache
commandPolicies map[string]routing.CommandPolicy
cmdable
hooksMixin
}
Expand All @@ -932,8 +934,8 @@ func NewClusterClient(opt *ClusterOptions) *ClusterClient {

c.state = newClusterStateHolder(c.loadState)
c.cmdsInfoCache = newCmdsInfoCache(c.cmdsInfo)
c.commandPolicies = newCommandPolicies(c.cmdsInfoCache.cmds)
c.cmdable = c.Process

c.initHooks(hooks{
dial: nil,
process: c.process,
Expand Down