ledisdb/server/cmd_replication.go

115 lines
2.0 KiB
Go
Raw Normal View History

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"
"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"
)
func slaveofCommand(c *client) error {
2014-06-08 12:43:59 +04:00
args := c.args
if len(args) != 2 {
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-08 12:43:59 +04:00
if err := c.app.slaveof(masterAddr); err != nil {
return err
}
c.writeStatus(OK)
return nil
}
2014-06-09 13:23:32 +04:00
func fullsyncCommand(c *client) error {
//todo, multi fullsync may use same dump file
dumpFile, err := ioutil.TempFile(c.app.cfg.DataDir, "dump_")
if err != nil {
return err
}
if err = c.app.ldb.Dump(dumpFile); err != nil {
return err
}
st, _ := dumpFile.Stat()
n := st.Size()
dumpFile.Seek(0, os.SEEK_SET)
c.writeBulkFrom(n, dumpFile)
name := dumpFile.Name()
dumpFile.Close()
os.Remove(name)
return nil
}
var reserveInfoSpace = make([]byte, 16)
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 master info
if _, err := c.syncBuf.Write(reserveInfoSpace); err != nil {
return err
}
if m, err := c.app.ldb.ReadEventsTo(logIndex, logPos, &c.syncBuf); 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))
c.writeBulk(buf)
}
return nil
}
func init() {
register("slaveof", slaveofCommand)
register("fullsync", fullsyncCommand)
register("sync", syncCommand)
}