Пример #1
0
func forwardMsg(msg protocol.Message, node nodes.Nodes, readonly bool) (msgAck protocol.Message) {
	var (
		clusterConnPool connection.Pool
		isMaster        bool
	)
	if readonly {
		clusterConnPool, isMaster = node.GetRandom()
	} else {
		clusterConnPool = node.GetMaster()
		isMaster = true
	}
	msgAck = forwardMsgToPool(msg, clusterConnPool, isMaster, false)
	if msgAck.GetProtocolType() == protocol.ErrorsStringsType {
		var msgAckBytesValueSplit [][]byte = bytes.Fields(msgAck.GetBytesValue())
		if len(msgAckBytesValueSplit) == 3 {
			switch {
			case bytes.EqualFold(msgAckBytesValueSplit[0], MOVED):
				msgAck = forwardMsgToPool(msg, cluster.GetClusterParameter().GetNodePool(string(msgAckBytesValueSplit[2])), true, false)
			case bytes.EqualFold(msgAckBytesValueSplit[0], ASK):
				msgAck = forwardMsgToPool(msg, cluster.GetClusterParameter().GetNodePool(string(msgAckBytesValueSplit[2])), true, true)
			}
		}
	}
	return
}
Пример #2
0
func DelCommand(cmd command.Command, msg protocol.Message) (msgAck protocol.Message) {
	var (
		elements       []protocol.Message = msg.GetArraysValue()
		element        protocol.Message
		hashSlot       uint16
		classification map[uint16]protocol.Message = make(map[uint16]protocol.Message)
		msgSpice       protocol.Message
		ok             bool
	)
	for _, element = range elements[1:] {
		hashSlot = crc16.HashSlot(element.GetBytesValue())
		if msgSpice, ok = classification[hashSlot]; !ok {
			msgSpice = protocol.NewMessage().AppendArraysValue(elements[0])
		}
		classification[hashSlot] = msgSpice.AppendArraysValue(element)
	}
	// 按slot分命令
	var (
		node        nodes.Nodes
		msgAckSplit protocol.Message
	)
	msgAck = protocol.NewMessage()
	for hashSlot, msgSpice = range classification {
		if node, ok = cluster.GetClusterParameter().GetSlot(hashSlot); !ok {
			logger.Warningf("获取不到slot节点: %d", hashSlot)
			continue
		}
		msgAckSplit = forwardMsg(msgSpice, node, cmd.CheckReadonly())
		msgAck.AppendIntegersValue(msgAckSplit)
	}
	return
}
Пример #3
0
func MSetCommand(cmd command.Command, msg protocol.Message) (msgAck protocol.Message) {
	var elements []protocol.Message = msg.GetArraysValue()
	if msg.GetIntegersValue()%2 != 1 {
		return protocol.NewMessageString(protocol.ERR_wrong_number_command, cmd.Name())
	}

	var (
		keyIndex       int64
		hashSlot       uint16
		classification map[uint16]protocol.Message = make(map[uint16]protocol.Message)
		msgSpice       protocol.Message
		ok             bool
	)
	for keyIndex = 1; keyIndex < msg.GetIntegersValue(); keyIndex += 2 {
		hashSlot = crc16.HashSlot(elements[keyIndex].GetBytesValue())
		if msgSpice, ok = classification[hashSlot]; !ok {
			msgSpice = protocol.NewMessage().AppendArraysValue(elements[0])
		}
		classification[hashSlot] = msgSpice.AppendArraysValue(elements[keyIndex]).AppendArraysValue(elements[keyIndex+1])
	}
	// 按slot分命令
	var node nodes.Nodes
	for hashSlot, msgSpice = range classification {
		if node, ok = cluster.GetClusterParameter().GetSlot(hashSlot); !ok {
			logger.Warningf("获取不到slot节点: %d", hashSlot)
			continue
		}
		forwardMsg(msgSpice, node, cmd.CheckReadonly())
	}
	return protocol.OK
}
Пример #4
0
func MGetCommand(cmd command.Command, msg protocol.Message) (msgAck protocol.Message) {
	var (
		elements       []protocol.Message = msg.GetArraysValue()
		element        protocol.Message
		hashSlot       uint16
		classification map[uint16]protocol.Message = make(map[uint16]protocol.Message)
		msgSpice       protocol.Message
		ok             bool
	)
	for _, element = range elements[1:] {
		hashSlot = crc16.HashSlot(element.GetBytesValue())
		if msgSpice, ok = classification[hashSlot]; !ok {
			msgSpice = protocol.NewMessage().AppendArraysValue(elements[0])
		}
		classification[hashSlot] = msgSpice.AppendArraysValue(element)
	}
	// 按slot分命令
	var (
		node        nodes.Nodes
		msgAckSplit protocol.Message
		msgAckMap   map[protocol.Message]protocol.Message = make(map[protocol.Message]protocol.Message)

		msgKey   int
		msgValue protocol.Message
	)
	for hashSlot, msgSpice = range classification {
		if node, ok = cluster.GetClusterParameter().GetSlot(hashSlot); !ok {
			logger.Warningf("获取不到slot节点: %d", hashSlot)
			continue
		}
		msgAckSplit = forwardMsg(msgSpice, node, cmd.CheckReadonly())
		if msgAckSplit.GetProtocolType() != protocol.ArraysType {
			logger.Warningf("命令结果预期不符: %v", msgSpice)
			continue
		}
		if msgSpice.GetIntegersValue()-1 != msgAckSplit.GetIntegersValue() {
			logger.Warningf("命令结果预期不符: %v", msgSpice)
			continue
		}
		for msgKey, msgValue = range msgSpice.GetArraysValue()[1:] {
			msgAckMap[msgValue] = msgAckSplit.GetArraysValue()[msgKey]
		}
	}
	// 整合结果
	msgAck = protocol.NewMessage()
	for _, element = range elements[1:] {
		if msgValue, ok = msgAckMap[element]; ok {
			msgAck.AppendArraysValue(msgAckMap[element])
		} else {
			msgAck.AppendArraysValue(protocol.NullBulkString)
		}
	}
	return
}
Пример #5
0
func ProcKeyCommand(cmd command.Command, msg protocol.Message) (msgAck protocol.Message) {
	var (
		slot uint16 = crc16.HashSlot(msg.GetArraysValue()[1].GetBytesValue()) // 第一个参数是Key
		node nodes.Nodes
		ok   bool
	)
	if node, ok = cluster.GetClusterParameter().GetSlot(slot); !ok {
		logger.Warningf("获取不到slot节点: %d", slot)
		return protocol.ERR_ClusterSlots
	}
	return forwardMsg(msg, node, cmd.CheckReadonly())
}