Beispiel #1
0
func (c *conn) Do(cmd *redis.Array, timeout time.Duration) (redis.Resp, error) {
	if c.err != nil {
		return nil, c.err
	}
	if err := c.encodeResp(cmd, timeout); err != nil {
		c.err = err
		log.WarnErrorf(err, "encode resp failed")
		return nil, c.err
	}
	if rsp, err := c.decodeResp(timeout); err != nil {
		c.err = err
		log.WarnErrorf(err, "decode resp failed")
		return nil, c.err
	} else {
		c.last = time.Now()
		return rsp, nil
	}
}
Beispiel #2
0
func doMigrate(addr string, timeout time.Duration, db uint32, bins []*rdb.BinEntry) error {
	c, err := getSockConn(addr, timeout)
	if err != nil {
		log.WarnErrorf(err, "connect to %s failed, timeout = %d", addr, timeout)
		return err
	}
	defer putSockConn(addr, c)

	cmd1 := redis.NewArray()
	cmd1.AppendBulkBytes([]byte("select"))
	cmd1.AppendBulkBytes([]byte(FormatUint(uint64(db))))

	if err := c.DoMustOK(cmd1, timeout); err != nil {
		log.WarnErrorf(err, "command select failed, addr = %s, db = %d", addr, db)
		return err
	}
	log.Debugf("command select ok, addr = %s, db = %d", addr, db)

	cmd2 := redis.NewArray()
	cmd2.AppendBulkBytes([]byte("slotsrestore"))
	for _, bin := range bins {
		cmd2.AppendBulkBytes(bin.Key)
		ttlms := int64(0)
		if bin.ExpireAt != 0 {
			if v, ok := ExpireAtToTTLms(bin.ExpireAt); ok && v > 0 {
				ttlms = v
			} else {
				ttlms = 1
			}
		}
		cmd2.AppendBulkBytes([]byte(FormatInt(ttlms)))
		cmd2.AppendBulkBytes(bin.Value)
	}

	if err := c.DoMustOK(cmd2, timeout); err != nil {
		log.WarnErrorf(err, "command restore failed, addr = %s, db = %d, len(bins) = %d", addr, db, len(bins))
		return err
	} else {
		log.Debugf("command restore ok, addr = %s, db = %d, len(bins) = %d", addr, db, len(bins))
		return nil
	}
}
Beispiel #3
0
func (b *Rpdb) commit(bt *store.Batch, fw *Forward) error {
	if bt.Len() == 0 {
		return nil
	}
	if err := b.db.Commit(bt); err != nil {
		log.WarnErrorf(err, "rpdb commit failed")
		return err
	}
	for i := b.itlist.Len(); i != 0; i-- {
		v := b.itlist.Remove(b.itlist.Front()).(*rpdbIterator)
		v.Close()
	}
	b.serial++
	return nil
}
Beispiel #4
0
func (c *conn) serve(h *Handler) error {
	for {
		if c.timeout != 0 {
			deadline := time.Now().Add(c.timeout)
			if err := c.nc.SetReadDeadline(deadline); err != nil {
				return errors.Trace(err)
			}
		}
		request, err := redis.Decode(c.r)
		if err != nil {
			return err
		}
		h.counters.commands.Add(1)
		response, err := c.dispatch(h, request)
		if err != nil {
			h.counters.commandsFailed.Add(1)
			b, _ := redis.EncodeToBytes(request)
			log.WarnErrorf(err, "handle commands failed, conn = %s, request = '%s'", c.summ, base64.StdEncoding.EncodeToString(b))
		}
		if response == nil {
			continue
		}
		if c.timeout != 0 {
			deadline := time.Now().Add(c.timeout)
			if err := c.nc.SetWriteDeadline(deadline); err != nil {
				return errors.Trace(err)
			}
		}
		if err := redis.Encode(c.w, response); err != nil {
			return err
		}
		if err := errors.Trace(c.w.Flush()); err != nil {
			return err
		}
	}
}