2014-06-06 10:57:18 +04:00
|
|
|
package server
|
|
|
|
|
2014-06-08 12:43:59 +04:00
|
|
|
import (
|
2014-06-09 13:23:32 +04:00
|
|
|
"encoding/binary"
|
2014-06-08 12:43:59 +04:00
|
|
|
"fmt"
|
2014-07-04 13:55:47 +04:00
|
|
|
"github.com/siddontang/go-snappy/snappy"
|
2014-06-08 12:43:59 +04:00
|
|
|
"github.com/siddontang/ledisdb/ledis"
|
2014-06-09 13:23:32 +04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-06-08 12:43:59 +04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
func slaveofCommand(c *client) error {
|
|
|
|
args := c.args
|
2014-06-08 12:43:59 +04:00
|
|
|
|
|
|
|
if len(args) != 2 {
|
2014-06-06 10:57:18 +04:00
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-06-08 12:43:59 +04:00
|
|
|
masterAddr := ""
|
|
|
|
|
|
|
|
if strings.ToLower(ledis.String(args[0])) == "no" &&
|
|
|
|
strings.ToLower(ledis.String(args[1])) == "one" {
|
|
|
|
//stop replication, use master = ""
|
|
|
|
} else {
|
|
|
|
if _, err := strconv.ParseInt(ledis.String(args[1]), 10, 16); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
masterAddr = fmt.Sprintf("%s:%s", args[0], args[1])
|
2014-06-06 10:57:18 +04:00
|
|
|
}
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
if err := c.app.slaveof(masterAddr); err != nil {
|
2014-06-06 10:57:18 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
c.resp.writeStatus(OK)
|
2014-06-06 10:57:18 +04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-06-09 13:23:32 +04:00
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
func fullsyncCommand(c *client) error {
|
2014-06-09 13:23:32 +04:00
|
|
|
//todo, multi fullsync may use same dump file
|
2014-08-25 10:18:23 +04:00
|
|
|
dumpFile, err := ioutil.TempFile(c.app.cfg.DataDir, "dump_")
|
2014-06-09 13:23:32 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
if err = c.app.ldb.Dump(dumpFile); err != nil {
|
2014-06-09 13:23:32 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
st, _ := dumpFile.Stat()
|
|
|
|
n := st.Size()
|
|
|
|
|
|
|
|
dumpFile.Seek(0, os.SEEK_SET)
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
c.resp.writeBulkFrom(n, dumpFile)
|
2014-06-09 13:23:32 +04:00
|
|
|
|
|
|
|
name := dumpFile.Name()
|
|
|
|
dumpFile.Close()
|
|
|
|
|
|
|
|
os.Remove(name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var reserveInfoSpace = make([]byte, 16)
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
func syncCommand(c *client) error {
|
|
|
|
args := c.args
|
2014-06-09 13:23:32 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
c.syncBuf.Reset()
|
2014-06-09 13:23:32 +04:00
|
|
|
|
2014-09-04 19:52:09 +04:00
|
|
|
//reserve space to write binlog anchor
|
2014-08-25 10:18:23 +04:00
|
|
|
if _, err := c.syncBuf.Write(reserveInfoSpace); err != nil {
|
2014-06-09 13:23:32 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-04 18:43:56 +04:00
|
|
|
m := &ledis.BinLogAnchor{logIndex, logPos}
|
2014-06-10 06:41:50 +04:00
|
|
|
|
2014-09-05 06:01:38 +04:00
|
|
|
if _, err := c.app.ldb.ReadEventsToTimeout(m, &c.syncBuf, 5); err != nil {
|
2014-06-09 13:23:32 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-08-25 10:18:23 +04:00
|
|
|
buf := c.syncBuf.Bytes()
|
2014-06-09 13:23:32 +04:00
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(buf[0:], uint64(m.LogFileIndex))
|
|
|
|
binary.BigEndian.PutUint64(buf[8:], uint64(m.LogPos))
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
if len(c.compressBuf) < snappy.MaxEncodedLen(len(buf)) {
|
|
|
|
c.compressBuf = make([]byte, snappy.MaxEncodedLen(len(buf)))
|
2014-07-04 13:55:47 +04:00
|
|
|
}
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
if buf, err = snappy.Encode(c.compressBuf, buf); err != nil {
|
2014-07-04 13:55:47 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
c.resp.writeBulk(buf)
|
2014-06-09 13:23:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register("slaveof", slaveofCommand)
|
|
|
|
register("fullsync", fullsyncCommand)
|
|
|
|
register("sync", syncCommand)
|
|
|
|
}
|