Beispiel #1
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
}
Beispiel #2
0
func (msg *MessageTest) TestNullBulkStrings(c *C) {
	var (
		buf  *bytes.Buffer    = bytes.NewBufferString("$-1\r\n")
		br   *bufio.Reader    = bufio.NewReader(buf)
		pmsg protocol.Message = protocol.NewMessage()
		err  error
	)
	err = pmsg.ReadOne(br)
	c.Assert(err, IsNil)
	//	c.Error(pmsg)
}
Beispiel #3
0
func (msg *MessageTest) TestNullElementsArrays(c *C) {
	var (
		buf  *bytes.Buffer    = bytes.NewBufferString("*3\r\n$3\r\nfoo\r\n$-1\r\n$3\r\nbar\r\n")
		br   *bufio.Reader    = bufio.NewReader(buf)
		pmsg protocol.Message = protocol.NewMessage()
		err  error
	)
	err = pmsg.ReadOne(br)
	c.Assert(err, IsNil)
	//	c.Error(pmsg)
}
Beispiel #4
0
func (msg *MessageTest) TestMultiArrays(c *C) {
	var (
		buf  *bytes.Buffer    = bytes.NewBufferString("*2\r\n*3\r\n:1\r\n:2\r\n:3\r\n*2\r\n+Foo\r\n-Bar\r\n")
		br   *bufio.Reader    = bufio.NewReader(buf)
		pmsg protocol.Message = protocol.NewMessage()
		err  error
	)
	err = pmsg.ReadOne(br)
	c.Assert(err, IsNil)
	//	c.Error(pmsg)
}
Beispiel #5
0
func (msg *MessageTest) TestIntegersBulkStringArrays(c *C) {
	var (
		buf  *bytes.Buffer    = bytes.NewBufferString("*5\r\n:1\r\n:2\r\n:3\r\n:4\r\n$6\r\nfoobar\r\n")
		br   *bufio.Reader    = bufio.NewReader(buf)
		pmsg protocol.Message = protocol.NewMessage()
		err  error
	)
	err = pmsg.ReadOne(br)
	c.Assert(err, IsNil)
	//	c.Error(pmsg)
}
Beispiel #6
0
func (conn *conn) HandelMessage(msg protocol.Message) (msgAck protocol.Message, err error) {
	conn.c.SetWriteDeadline(time.Now().Add(conn.writeTimeout))
	if err = msg.WriteOne(conn.bw); err != nil {
		return
	}
	conn.c.SetReadDeadline(time.Now().Add(conn.readTimeout))
	msgAck = protocol.NewMessage()
	if err = msgAck.ReadOne(conn.br); err != nil {
		return
	}
	return
}
Beispiel #7
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())
}
Beispiel #8
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
}
Beispiel #9
0
func main() {

	c := commands.NewCommandTree()
	c.AddCommand([]byte("aBcD"), commands.NewCommand())

	fmt.Println(c.SearchCommand([]byte("ABCD")))
	fmt.Println(c.SearchCommand([]byte("AbCD")))
	fmt.Println(c.SearchCommand([]byte("ABCd")))
	fmt.Println(c.SearchCommand([]byte("ABC")))

	for i := 'a'; i <= 'z'; i++ {
		j := util.ToUpper(byte(i))
		k := util.ToLower(j)
		fmt.Println(i, j, k)
	}
	return
	socketClent, err := socket.NewTClientSocket("10.10.10.227:6679")
	//	socketClent, err := socket.NewTClientSocket("127.0.0.1:6679")
	fmt.Println(socketClent, err)
	conn, err := socketClent.DialTimeout(time.Duration(3 * time.Second))
	fmt.Println(conn, err)

	var (
		br     *bufio.Reader    = bufio.NewReader(conn)
		bw     *bufio.Writer    = bufio.NewWriter(conn)
		msgAck protocol.Message = protocol.NewMessage()
	)

	// 发数据
	var rawBytes []byte = []byte("*1\r\n$4\r\nINFO\r\n")
	//var rawBytes []byte = []byte{42, 50, 13, 10, 36, 55, 13, 10, 99, 108, 117, 115, 116, 101, 114, 13, 10, 36, 53, 13, 10, 115, 108, 111, 116, 115, 13, 10}
	//var rawBytes []byte = []byte("*2\r\n$7\r\nCLUSTER\r\n$5\r\nSLOTS\r\n")
	fmt.Println(string(rawBytes))
	if _, err = bw.Write(rawBytes); err != nil {
		fmt.Println(err)
		return
	}
	bw.Flush()

	// 收数据
	if err = msgAck.ReadOne(br); err != nil {
		fmt.Println(err)
	}
	fmt.Println(msgAck)
}
Beispiel #10
0
func ProcessCommand(msg protocol.Message) protocol.Message {
	var (
		name []byte
		argc int64
		cmd  command.Command
		ok   bool
	)
	switch msg.GetProtocolType() {
	case protocol.ArraysType:
		if argc = msg.GetIntegersValue(); argc > 0 {
			name = msg.GetArraysValue()[0].GetBytesValue()
		}
	}

	if cmd, ok = commandSet.SearchCommand(name); !ok {
		return protocol.NewMessageString(protocol.ERR_unknown_command, name)
	}
	if !cmd.CheckArgc(argc) {
		return protocol.NewMessageString(protocol.ERR_wrong_number_command, cmd.Name())
	}
	if cmd.CheckForbidden() {
		return protocol.NewMessageString(protocol.ERR_forbidden_command, cmd.Name())
	}
	return cmd.Proc(cmd, msg)
}
Beispiel #11
0
func PingCommand(cmd command.Command, msg protocol.Message) protocol.Message {
	if msg.GetIntegersValue() > 2 {
		return protocol.NewMessageString(protocol.ERR_wrong_number_command, cmd.Name())
	}
	if msg.GetIntegersValue() == 2 {
		return msg.GetArraysValue()[1]
	}
	return protocol.PONG
}
Beispiel #12
0
func (conn *conn) sendMessage(msg protocol.Message) (err error) {
	err = msg.WriteOne(conn.bw)
	conn.error(err, "sendMessage 出错")
	return
}
Beispiel #13
0
func EchoCommand(cmd command.Command, msg protocol.Message) protocol.Message {
	return msg.GetArraysValue()[1]
}
Beispiel #14
0
func (c *cluster) refresh(server string) (err error) {
	var (
		connPool    connection.Pool = c.GetNodePool(server)
		conn        connection.Conn
		slotsMsgAck protocol.Message
	)
	if conn, err = connPool.Get(); err != nil {
		return
	}
	// 发送cluster slots
	if slotsMsgAck, err = conn.HandelMessage(protocol.ClusterSlots); err != nil {
		connPool.Remove(conn)
		return
	}
	connPool.Put(conn)
	// 处理返回值
	if slotsMsgAck.GetProtocolType() != protocol.ArraysType || slotsMsgAck.GetIntegersValue() <= 0 {
		return fmt.Errorf("slots数据无效")
	}
	var slotsMsgAckValue protocol.Message
	for _, slotsMsgAckValue = range slotsMsgAck.GetArraysValue() {
		if slotsMsgAckValue.GetProtocolType() != protocol.ArraysType || slotsMsgAckValue.GetIntegersValue() < 3 {
			return fmt.Errorf("slots节点数据无效")
		}
		var (
			slotsMsgAckValueArrays []protocol.Message = slotsMsgAckValue.GetArraysValue()
			nodeMsgValue           protocol.Message
			nodeMsgValueArrays     []protocol.Message
			connPoolList           []connection.Pool
		)
		if slotsMsgAckValueArrays[0].GetProtocolType() != protocol.IntegersType || slotsMsgAckValueArrays[1].GetProtocolType() != protocol.IntegersType {
			return fmt.Errorf("slots节点起始或者结束数据无效")
		}
		for _, nodeMsgValue = range slotsMsgAckValueArrays[2:] {
			if nodeMsgValue.GetProtocolType() != protocol.ArraysType || nodeMsgValue.GetIntegersValue() < 2 {
				return fmt.Errorf("slot节点数据无效")
			}
			nodeMsgValueArrays = nodeMsgValue.GetArraysValue()
			if nodeMsgValueArrays[0].GetProtocolType() != protocol.BulkStringsType {
				return fmt.Errorf("slot节点IP无效")
			}
			if nodeMsgValueArrays[1].GetProtocolType() != protocol.IntegersType {
				return fmt.Errorf("slot节点PORT无效")
			}
			connPoolList = append(
				connPoolList,
				c.GetNodePool(fmt.Sprintf("%s:%d", nodeMsgValueArrays[0].GetBytesValue(), nodeMsgValueArrays[1].GetIntegersValue())),
			)
		}
		c.clusterSlots.AddSlots(
			slotsMsgAckValueArrays[0].GetIntegersValue(),
			slotsMsgAckValueArrays[1].GetIntegersValue(),
			nodes.NewNodes().Set(connPoolList), // TODO 同一个List用同一个Nodes
		)
	}
	return
}
Beispiel #15
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
}