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 = "" access_log = ""
# Set slaveof to enable replication from master, empty, no replication # Set slaveof to enable replication from master, empty, no replication
# Any write operations except flushall and replication will be disabled in slave mode.
slaveof = "" slaveof = ""
# Choose which backend storage to use, now support: # 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 wLock sync.RWMutex //allow one write at same time
commitLock sync.Mutex //allow one write commit 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 readOnly bool
lock io.Closer 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 return nil, err
} }

View File

@ -82,20 +82,6 @@ func flushdbCommand(c *client) error {
return nil 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() { func init() {
register("ping", pingCommand) register("ping", pingCommand)
register("echo", echoCommand) register("echo", echoCommand)

View File

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