示例#1
0
func (s *testProxyRouterSuite) TestHandleSpecCommand(c *C) {
	var tbl = map[string]string{
		"PING":   "+PONG\r\n",
		"QUIT":   string(OK_BYTES),
		"SELECT": string(OK_BYTES),
	}

	for k, v := range tbl {
		resp, err := parser.Parse(bufio.NewReader(bytes.NewBufferString(k + string(parser.NEW_LINE))))
		c.Assert(err, IsNil)

		_, keys, err := resp.GetOpKeys()
		c.Assert(err, IsNil)

		result, _, _, err := handleSpecCommand(k, keys, 5)
		c.Assert(err, IsNil)
		c.Assert(string(result), Equals, v)
	}

	// "ECHO xxxx": "xxxx\r\n",
	{
		resp, err := parser.Parse(bufio.NewReader(bytes.NewBufferString("ECHO xxxx\r\n")))
		c.Assert(err, IsNil)

		_, keys, _ := resp.GetOpKeys()
		result, _, _, err := handleSpecCommand("ECHO", keys, 5)
		c.Assert(err, IsNil)
		c.Assert(string(result), Equals, "$4\r\nxxxx\r\n")
	}

	// test empty key
	{
		resp, err := parser.Parse(bufio.NewReader(bytes.NewBufferString("ECHO\r\n")))
		c.Assert(err, IsNil)

		_, keys, _ := resp.GetOpKeys()
		_, shouldClose, _, err := handleSpecCommand("ECHO", keys, 5)
		c.Assert(shouldClose, Equals, true)
	}

	// test not specific command
	{
		_, _, handled, err := handleSpecCommand("GET", nil, 5)
		c.Assert(handled, Equals, false)
		c.Assert(err, IsNil)
	}
}
示例#2
0
func write2Client(redisReader *bufio.Reader, clientWriter io.Writer) (redisErr error, clientErr error) {
	resp, err := parser.Parse(redisReader)
	if err != nil {
		return errors.Trace(err), errors.Trace(err)
	}

	err = resp.WriteTo(clientWriter)
	return nil, errors.Trace(err)
}
示例#3
0
文件: router.go 项目: GavinHwa/reborn
func (s *Server) handleMigrateState(slotIndex int, keys ...[]byte) error {
	shd := s.slots[slotIndex]
	if shd.slotInfo.State.Status != models.SLOT_STATUS_MIGRATE {
		return nil
	}

	if shd.migrateFrom == nil {
		log.Fatalf("migrateFrom not exist %+v", shd)
	}

	if shd.dst.Master() == shd.migrateFrom.Master() {
		log.Fatalf("the same migrate src and dst, %+v", shd)
	}

	redisConn, err := s.pools.GetConn(shd.migrateFrom.Master())
	if err != nil {
		return errors.Trace(err)
	}

	defer s.pools.PutConn(redisConn)

	err = writeMigrateKeyCmd(redisConn, shd.dst.Master(), MigrateKeyTimeoutMs, keys...)
	if err != nil {
		redisConn.Close()
		log.Errorf("migrate key %s error, from %s to %s, err:%v",
			string(keys[0]), shd.migrateFrom.Master(), shd.dst.Master(), err)
		return errors.Trace(err)
	}

	redisReader := redisConn.BufioReader()

	// handle migrate result
	for i := 0; i < len(keys); i++ {
		resp, err := parser.Parse(redisReader)
		if err != nil {
			log.Errorf("migrate key %s error, from %s to %s, err:%v",
				string(keys[i]), shd.migrateFrom.Master(), shd.dst.Master(), err)
			redisConn.Close()
			return errors.Trace(err)
		}

		result, err := resp.Bytes()

		log.Debug("migrate", string(keys[0]), "from", shd.migrateFrom.Master(), "to", shd.dst.Master(),
			string(result))

		if resp.Type == parser.ErrorResp {
			redisConn.Close()
			log.Error(string(keys[0]), string(resp.Raw), "migrateFrom", shd.migrateFrom.Master())
			return errors.New(string(resp.Raw))
		}
	}

	s.counter.Add("Migrate", int64(len(keys)))

	return nil
}
示例#4
0
func (tr *taskRunner) readloop() {
	for {
		resp, err := parser.Parse(tr.c.BufioReader())
		if err != nil {
			tr.out <- err
			return
		}

		tr.out <- resp
	}
}
示例#5
0
func doCommand(c *redisconn.Conn, cmd string, args ...interface{}) (*parser.Resp, error) {
	writeCommand(c, cmd, args...)
	if err := c.Flush(); err != nil {
		return nil, errors.Trace(err)
	}

	resp, err := parser.Parse(c.BufioReader())
	if err != nil {
		return nil, errors.Trace(err)
	}
	return resp, nil
}
示例#6
0
func (s *testProxyRouterSuite) TestForward(c *C) {
	client := &fakeDeadlineReadWriter{r: bufio.NewReader(bytes.NewBuffer([]byte(simpleRequest))),
		w: bufio.NewWriter(&bytes.Buffer{})}
	redis := &fakeDeadlineReadWriter{r: bufio.NewReader(bytes.NewBuffer([]byte(simpleRequest))),
		w: bufio.NewWriter(&bytes.Buffer{})}

	resp, err := parser.Parse(bufio.NewReader(bytes.NewBuffer([]byte(simpleRequest))))
	c.Assert(err, IsNil)

	_, clientErr := forward(client, redis, resp, 5)
	c.Assert(clientErr, IsNil)
}
示例#7
0
func (s *testProxyRouterSuite) TestWrite2Redis(c *C) {
	var result bytes.Buffer
	var input bytes.Buffer
	input.WriteString(simpleRequest)
	resp, err := parser.Parse(bufio.NewReader(&input))
	c.Assert(err, IsNil)

	err = resp.WriteTo(&result)
	c.Assert(err, IsNil)

	c.Assert(string(result.Bytes()), Equals, simpleRequest)
}
示例#8
0
文件: router.go 项目: GavinHwa/reborn
func (s *Server) sendBack(c *session, op []byte, keys [][]byte, resp *parser.Resp, result []byte) {
	c.pipelineSeq++
	pr := &PipelineRequest{
		op:    op,
		keys:  keys,
		seq:   c.pipelineSeq,
		backQ: c.backQ,
		req:   resp,
	}

	resp, err := parser.Parse(bufio.NewReader(bytes.NewReader(result)))
	// just send to backQ
	c.backQ <- &PipelineResponse{ctx: pr, err: err, resp: resp}
}
示例#9
0
func getRespOpKeys(c *session) (*parser.Resp, []byte, [][]byte, error) {
	resp, err := parser.Parse(c.r) // read client request
	if err != nil {
		return nil, nil, nil, errors.Trace(err)
	}

	op, keys, err := resp.GetOpKeys()
	if err != nil {
		return nil, nil, nil, errors.Trace(err)
	}

	if len(keys) == 0 {
		keys = [][]byte{[]byte("fakeKey")}
	}

	return resp, op, keys, nil
}