2014-06-06 10:57:18 +04:00
|
|
|
package server
|
|
|
|
|
2014-06-08 12:43:59 +04:00
|
|
|
import (
|
|
|
|
"fmt"
|
2014-09-24 08:34:21 +04:00
|
|
|
"github.com/siddontang/go/hack"
|
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
|
|
|
|
2014-09-28 17:34:24 +04:00
|
|
|
if len(args) != 2 || len(args) != 3 {
|
2014-06-06 10:57:18 +04:00
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-06-08 12:43:59 +04:00
|
|
|
masterAddr := ""
|
2014-09-28 17:34:24 +04:00
|
|
|
restart := false
|
2014-06-08 12:43:59 +04:00
|
|
|
|
2014-09-24 08:34:21 +04:00
|
|
|
if strings.ToLower(hack.String(args[0])) == "no" &&
|
|
|
|
strings.ToLower(hack.String(args[1])) == "one" {
|
2014-06-08 12:43:59 +04:00
|
|
|
//stop replication, use master = ""
|
2014-09-28 17:34:24 +04:00
|
|
|
if len(args) != 2 {
|
|
|
|
return ErrCmdParams
|
|
|
|
}
|
2014-06-08 12:43:59 +04:00
|
|
|
} else {
|
2014-09-24 08:34:21 +04:00
|
|
|
if _, err := strconv.ParseInt(hack.String(args[1]), 10, 16); err != nil {
|
2014-06-08 12:43:59 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
masterAddr = fmt.Sprintf("%s:%s", args[0], args[1])
|
2014-09-28 17:34:24 +04:00
|
|
|
|
|
|
|
if len(args) == 3 && strings.ToLower(hack.String(args[2])) == "restart" {
|
|
|
|
restart = true
|
|
|
|
}
|
2014-06-06 10:57:18 +04:00
|
|
|
}
|
|
|
|
|
2014-09-28 17:34:24 +04:00
|
|
|
if err := c.app.slaveof(masterAddr, restart); 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
|
|
|
|
}
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
func syncCommand(c *client) error {
|
|
|
|
args := c.args
|
2014-09-22 13:50:51 +04:00
|
|
|
if len(args) != 1 {
|
2014-06-09 13:23:32 +04:00
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
var logId uint64
|
2014-06-09 13:23:32 +04:00
|
|
|
var err error
|
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
if logId, err = ledis.StrUint64(args[0], nil); err != nil {
|
2014-06-09 13:23:32 +04:00
|
|
|
return ErrCmdParams
|
|
|
|
}
|
|
|
|
|
2014-09-23 13:53:52 +04:00
|
|
|
c.lastLogID = logId - 1
|
2014-06-09 13:23:32 +04:00
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
if c.ack != nil && logId > c.ack.id {
|
2014-09-24 17:31:26 +04:00
|
|
|
asyncNotifyUint64(c.ack.ch, logId)
|
2014-09-23 13:28:09 +04:00
|
|
|
c.ack = nil
|
2014-06-09 13:23:32 +04:00
|
|
|
}
|
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
c.syncBuf.Reset()
|
2014-06-10 06:41:50 +04:00
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
if _, _, err := c.app.ldb.ReadLogsToTimeout(logId, &c.syncBuf, 30); 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
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
c.resp.writeBulk(buf)
|
2014-06-09 13:23:32 +04:00
|
|
|
}
|
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
c.app.addSlave(c)
|
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register("slaveof", slaveofCommand)
|
|
|
|
register("fullsync", fullsyncCommand)
|
|
|
|
register("sync", syncCommand)
|
|
|
|
}
|