Skip to content

Commit 93226d0

Browse files
committed
Optimize keyless command routing to respect ShardPicker policy
1 parent ceeb2c8 commit 93226d0

File tree

2 files changed

+11
-25
lines changed

2 files changed

+11
-25
lines changed

osscluster.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,16 @@ func (c *ClusterClient) cmdNode(
20982098
return nil, err
20992099
}
21002100

2101+
// For keyless commands (slot == -1), use ShardPicker to select a shard
2102+
// This respects the user's configured ShardPicker policy
2103+
if slot == -1 {
2104+
if len(state.Masters) == 0 {
2105+
return nil, errClusterNoNodes
2106+
}
2107+
idx := c.opt.ShardPicker.Next(len(state.Masters))
2108+
return state.Masters[idx], nil
2109+
}
2110+
21012111
if c.opt.ReadOnly {
21022112
cmdInfo := c.cmdInfo(ctx, cmdName)
21032113
if cmdInfo != nil && cmdInfo.ReadOnly {

osscluster_router.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,12 @@ func (c *ClusterClient) routeAndRun(ctx context.Context, cmd Cmder, node *cluste
5454

5555
// executeDefault handles standard command routing based on keys
5656
func (c *ClusterClient) executeDefault(ctx context.Context, cmd Cmder, policy *routing.CommandPolicy, node *clusterNode) error {
57-
if c.hasKeys(cmd) {
58-
// execute on key based shard
59-
return node.Client.Process(ctx, cmd)
60-
}
61-
if policy != nil {
57+
if policy != nil && !c.hasKeys(cmd) {
6258
if c.readOnlyEnabled() && policy.IsReadOnly() {
6359
return c.executeOnArbitraryNode(ctx, cmd)
6460
}
6561
}
6662

67-
return c.executeOnArbitraryShard(ctx, cmd)
68-
}
69-
70-
// executeOnArbitraryShard routes command to an arbitrary shard
71-
func (c *ClusterClient) executeOnArbitraryShard(ctx context.Context, cmd Cmder) error {
72-
node := c.pickArbitraryShard(ctx)
73-
if node == nil {
74-
return errClusterNoNodes
75-
}
7663
return node.Client.Process(ctx, cmd)
7764
}
7865

@@ -492,17 +479,6 @@ func (c *ClusterClient) finishAggregation(cmd Cmder, aggregator routing.Response
492479
return c.setCommandValue(cmd, finalValue)
493480
}
494481

495-
// pickArbitraryShard selects a master shard using the configured ShardPicker
496-
func (c *ClusterClient) pickArbitraryShard(ctx context.Context) *clusterNode {
497-
state, err := c.state.Get(ctx)
498-
if err != nil || len(state.Masters) == 0 {
499-
return nil
500-
}
501-
502-
idx := c.opt.ShardPicker.Next(len(state.Masters))
503-
return state.Masters[idx]
504-
}
505-
506482
// pickArbitraryNode selects a master or slave shard using the configured ShardPicker
507483
func (c *ClusterClient) pickArbitraryNode(ctx context.Context) *clusterNode {
508484
state, err := c.state.Get(ctx)

0 commit comments

Comments
 (0)