update slaveof and readonly

This commit is contained in:
siddontang 2014-09-25 16:03:29 +08:00
parent dca71891c3
commit 962a087330
5 changed files with 15 additions and 20 deletions

View File

@ -13,6 +13,7 @@ data_dir = "/tmp/ledis_server"
access_log = ""
# Set slaveof to enable replication from master, empty, no replication
# Any write operations except flushall and replication will be disabled in slave mode.
slaveof = ""
# Choose which backend storage to use, now support:

View File

@ -33,7 +33,7 @@ type Ledis struct {
wLock sync.RWMutex //allow one write at same time
commitLock sync.Mutex //allow one write commit at same time
// for readonly mode, only replication can write
// for readonly mode, only replication and flushall can write
readOnly bool
lock io.Closer

View File

@ -88,7 +88,13 @@ func NewApp(cfg *config.Config) (*App, error) {
}
}
if app.ldb, err = ledis.Open(cfg); err != nil {
flag := ledis.RDWRMode
if len(app.cfg.SlaveOf) > 0 {
//slave must readonly
flag = ledis.ROnlyMode
}
if app.ldb, err = ledis.Open2(cfg, flag); err != nil {
return nil, err
}

View File

@ -82,20 +82,6 @@ func flushdbCommand(c *client) error {
return nil
}
func readonlyCommand(c *client) error {
if len(c.args) != 1 {
return ErrCmdParams
}
if flag, err := strconv.Atoi(hack.String(c.args[0])); err != nil {
return err
} else {
c.app.ldb.SetReadOnly(flag != 0)
c.resp.writeStatus(OK)
}
return nil
}
func init() {
register("ping", pingCommand)
register("echo", echoCommand)

View File

@ -87,8 +87,6 @@ func (m *master) connect() error {
func (m *master) stopReplication() error {
m.Close()
m.app.ldb.SetReadOnly(false)
return nil
}
@ -126,7 +124,7 @@ func (m *master) runReplication() {
if err := m.sync(); err != nil {
if m.conn != nil {
//if conn == nil, other close the replication, not error
log.Warn("sync error %s", err.Error())
log.Error("sync error %s", err.Error())
}
return
}
@ -237,7 +235,11 @@ func (app *App) slaveof(masterAddr string) error {
}
if len(masterAddr) == 0 {
return app.m.stopReplication()
if err := app.m.stopReplication(); err != nil {
return err
}
app.ldb.SetReadOnly(false)
} else {
return app.m.startReplication(masterAddr)
}