Use first slot/shard when key is not defined.

This commit is contained in:
Vladimir Mihailenco 2016-12-16 14:05:21 +02:00
parent 152cc1ee34
commit 6cd7a09b22
2 changed files with 9 additions and 11 deletions

View File

@ -258,10 +258,10 @@ func newClusterState(nodes *clusterNodes, slots []ClusterSlot) (*clusterState, e
func (c *clusterState) slotMasterNode(slot int) (*clusterNode, error) { func (c *clusterState) slotMasterNode(slot int) (*clusterNode, error) {
nodes := c.slotNodes(slot) nodes := c.slotNodes(slot)
if len(nodes) == 0 { if len(nodes) > 0 {
return c.nodes.Random() return nodes[0], nil
} }
return nodes[0], nil return c.nodes.Random()
} }
func (c *clusterState) slotSlaveNode(slot int) (*clusterNode, error) { func (c *clusterState) slotSlaveNode(slot int) (*clusterNode, error) {
@ -364,15 +364,16 @@ func (c *ClusterClient) state() *clusterState {
} }
func (c *ClusterClient) cmdSlotAndNode(state *clusterState, cmd Cmder) (int, *clusterNode, error) { func (c *ClusterClient) cmdSlotAndNode(state *clusterState, cmd Cmder) (int, *clusterNode, error) {
if state == nil {
node, err := c.nodes.Random()
return 0, node, err
}
cmdInfo := c.cmds[cmd.arg(0)] cmdInfo := c.cmds[cmd.arg(0)]
firstKey := cmd.arg(cmdFirstKeyPos(cmd, cmdInfo)) firstKey := cmd.arg(cmdFirstKeyPos(cmd, cmdInfo))
if firstKey == "" || cmdInfo == nil {
node, err := c.nodes.Random()
return -1, node, err
}
slot := hashtag.Slot(firstKey) slot := hashtag.Slot(firstKey)
if cmdInfo.ReadOnly && c.opt.ReadOnly { if cmdInfo != nil && cmdInfo.ReadOnly && c.opt.ReadOnly {
if c.opt.RouteByLatency { if c.opt.RouteByLatency {
node, err := state.slotClosestNode(slot) node, err := state.slotClosestNode(slot)
return slot, node, err return slot, node, err

View File

@ -267,9 +267,6 @@ func (c *Ring) shardByName(name string) (*ringShard, error) {
func (c *Ring) cmdShard(cmd Cmder) (*ringShard, error) { func (c *Ring) cmdShard(cmd Cmder) (*ringShard, error) {
cmdInfo := c.cmdInfo(cmd.arg(0)) cmdInfo := c.cmdInfo(cmd.arg(0))
firstKey := cmd.arg(cmdFirstKeyPos(cmd, cmdInfo)) firstKey := cmd.arg(cmdFirstKeyPos(cmd, cmdInfo))
if firstKey == "" {
return c.randomShard()
}
return c.shardByKey(firstKey) return c.shardByKey(firstKey)
} }