From 962a0873300d88e12c76ca5d964fe6aa9f261f68 Mon Sep 17 00:00:00 2001 From: siddontang Date: Thu, 25 Sep 2014 16:03:29 +0800 Subject: [PATCH] update slaveof and readonly --- config/config.toml | 1 + ledis/ledis.go | 2 +- server/app.go | 8 +++++++- server/cmd_server.go | 14 -------------- server/replication.go | 10 ++++++---- 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/config/config.toml b/config/config.toml index ac0aab9..d3faf17 100644 --- a/config/config.toml +++ b/config/config.toml @@ -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: diff --git a/ledis/ledis.go b/ledis/ledis.go index 27f2ad3..0052b3a 100644 --- a/ledis/ledis.go +++ b/ledis/ledis.go @@ -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 diff --git a/server/app.go b/server/app.go index 74a0b23..62f8514 100644 --- a/server/app.go +++ b/server/app.go @@ -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 } diff --git a/server/cmd_server.go b/server/cmd_server.go index 1d7a1cb..ab8051f 100644 --- a/server/cmd_server.go +++ b/server/cmd_server.go @@ -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) diff --git a/server/replication.go b/server/replication.go index d90caab..8d49f43 100644 --- a/server/replication.go +++ b/server/replication.go @@ -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) }