예제 #1
0
func syncCommand(req *requestContext) error {
	args := req.args
	if len(args) != 2 {
		return ErrCmdParams
	}

	var logIndex int64
	var logPos int64
	var err error
	logIndex, err = ledis.StrInt64(args[0], nil)
	if err != nil {
		return ErrCmdParams
	}

	logPos, err = ledis.StrInt64(args[1], nil)
	if err != nil {
		return ErrCmdParams
	}

	req.syncBuf.Reset()

	//reserve space to write master info
	if _, err := req.syncBuf.Write(reserveInfoSpace); err != nil {
		return err
	}

	m := &ledis.MasterInfo{logIndex, logPos}

	if _, err := req.app.ldb.ReadEventsTo(m, &req.syncBuf); err != nil {
		return err
	} else {
		buf := req.syncBuf.Bytes()

		binary.BigEndian.PutUint64(buf[0:], uint64(m.LogFileIndex))
		binary.BigEndian.PutUint64(buf[8:], uint64(m.LogPos))

		if len(req.compressBuf) < snappy.MaxEncodedLen(len(buf)) {
			req.compressBuf = make([]byte, snappy.MaxEncodedLen(len(buf)))
		}

		if buf, err = snappy.Encode(req.compressBuf, buf); err != nil {
			return err
		}

		req.resp.writeBulk(buf)
	}

	return nil
}
예제 #2
0
func syncCommand(c *client) error {
	args := c.args
	if len(args) != 2 {
		return ErrCmdParams
	}

	var logIndex int64
	var logPos int64
	var err error
	logIndex, err = ledis.StrInt64(args[0], nil)
	if err != nil {
		return ErrCmdParams
	}

	logPos, err = ledis.StrInt64(args[1], nil)
	if err != nil {
		return ErrCmdParams
	}

	c.syncBuf.Reset()

	//reserve space to write binlog anchor
	if _, err := c.syncBuf.Write(reserveInfoSpace); err != nil {
		return err
	}

	m := &ledis.BinLogAnchor{logIndex, logPos}

	if _, err := c.app.ldb.ReadEventsToTimeout(m, &c.syncBuf, 5); err != nil {
		return err
	} else {
		buf := c.syncBuf.Bytes()

		binary.BigEndian.PutUint64(buf[0:], uint64(m.LogFileIndex))
		binary.BigEndian.PutUint64(buf[8:], uint64(m.LogPos))

		if len(c.compressBuf) < snappy.MaxEncodedLen(len(buf)) {
			c.compressBuf = make([]byte, snappy.MaxEncodedLen(len(buf)))
		}

		if buf, err = snappy.Encode(c.compressBuf, buf); err != nil {
			return err
		}

		c.resp.writeBulk(buf)
	}

	return nil
}