示例#1
0
func TestCmd(t *testing.T) {
	str := "*2\r\n$3\r\nget\r\n$4\r\nname\r\n"
	cmd, err := proxy.ParseRedisCmd(str)
	if err == nil {
		t.Log(cmd)
	} else {
		t.Error(err)
	}
}
示例#2
0
func (self *YundisServer) StartAgent() {
	listen, err := net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP(self.Host), self.Port, ""})
	if err != nil {
		log.Errorf("Error when start the agent, err:%s", err)
		return
	}
	log.Info("Inited the connection, waitting for client...")
	for {
		conn, err := listen.Accept()
		if err != nil {
			log.Errorf("error when accept the connect from client:%s", err.Error())
			continue
		}
		log.Infof("client connected from:%s", conn.RemoteAddr().String())
		go func() {
			data := make([]byte, 8192)
			defer conn.Close()
			for {
				i, err := conn.Read(data)
				if err != nil {
					log.Warnf("error when read data from client:%s", err.Error())
					break
				}
				redisCmd, err := proxy.ParseRedisCmd(string(data[0:i]))
				if err != nil {
					log.Errorf("Error when parse the redis cmd from client:%s", string(data[0:i]))
					break
				}
				log.Infof("cmd: %s", redisCmd)
				if adminHandler.IsAdminCmd(redisCmd) {
					//execute the admin cmd.
					adminHandler.ExecuteCmd(redisCmd, conn)
				} else {
					//send to redis
					hashNode := self.slotHashRing.Get(redisCmd.Key)
					slotId := hashNode.Entry.(int)
					log.Debugf("Target slot is %d", slotId)
					if nodeId, ok := self.GetAllocations().Allocations[strconv.Itoa(slotId)]; ok {
						log.Debugf("Target node is %d", nodeId)
						if nodeInfo, ok := self.nodeinfoMaps.GetNodeInfoMap()[strconv.Itoa(nodeId)]; ok {
							nodeInfo.GetRedisProxy().SendToRedis(data[0:i], conn)
						} else {
							log.Errorf("Can not find the nodeinfo by nodeId %d", nodeId)
						}
					} else {
						log.Errorf("Can not get the nodeId by slotId %d", slotId)
						break
					}
				}
			}
		}()
	}
}