Skip to content

Commit

Permalink
Merge pull request #449 from go-redis/fix/use-first-slot-when-there-a…
Browse files Browse the repository at this point in the history
…re-no-keys

Use first slot/shard when key is not defined.
  • Loading branch information
vmihailenco committed Dec 16, 2016
2 parents 4ba635e + 6cd7a09 commit b49d47e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
17 changes: 9 additions & 8 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ func newClusterState(nodes *clusterNodes, slots []ClusterSlot) (*clusterState, e

func (c *clusterState) slotMasterNode(slot int) (*clusterNode, error) {
nodes := c.slotNodes(slot)
if len(nodes) == 0 {
return c.nodes.Random()
if len(nodes) > 0 {
return nodes[0], nil
}
return nodes[0], nil
return c.nodes.Random()
}

func (c *clusterState) slotSlaveNode(slot int) (*clusterNode, error) {
Expand Down Expand Up @@ -364,15 +364,16 @@ func (c *ClusterClient) state() *clusterState {
}

func (c *ClusterClient) cmdSlotAndNode(state *clusterState, cmd Cmder) (int, *clusterNode, error) {
cmdInfo := c.cmds[cmd.arg(0)]
firstKey := cmd.arg(cmdFirstKeyPos(cmd, cmdInfo))
if firstKey == "" || cmdInfo == nil {
if state == nil {
node, err := c.nodes.Random()
return -1, node, err
return 0, node, err
}

cmdInfo := c.cmds[cmd.arg(0)]
firstKey := cmd.arg(cmdFirstKeyPos(cmd, cmdInfo))
slot := hashtag.Slot(firstKey)

if cmdInfo.ReadOnly && c.opt.ReadOnly {
if cmdInfo != nil && cmdInfo.ReadOnly && c.opt.ReadOnly {
if c.opt.RouteByLatency {
node, err := state.slotClosestNode(slot)
return slot, node, err
Expand Down
3 changes: 0 additions & 3 deletions ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,6 @@ func (c *Ring) shardByName(name string) (*ringShard, error) {
func (c *Ring) cmdShard(cmd Cmder) (*ringShard, error) {
cmdInfo := c.cmdInfo(cmd.arg(0))
firstKey := cmd.arg(cmdFirstKeyPos(cmd, cmdInfo))
if firstKey == "" {
return c.randomShard()
}
return c.shardByKey(firstKey)
}

Expand Down

0 comments on commit b49d47e

Please sign in to comment.