flushall will restart replication if possible

This commit is contained in:
siddontang 2014-10-03 20:31:24 +08:00
parent a75791f72c
commit 3f93676d4d
3 changed files with 31 additions and 1 deletions

View File

@ -120,4 +120,14 @@ func TestReplication(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
slave.tryReSlaveof()
time.Sleep(1 * time.Second)
slave.ldb.WaitReplication()
if err = checkDataEqual(master, slave); err != nil {
t.Fatal(err)
}
} }

View File

@ -68,6 +68,9 @@ func flushallCommand(c *client) error {
return err return err
} }
//we will restart the replication from master if possible
c.app.tryReSlaveof()
c.resp.writeStatus(OK) c.resp.writeStatus(OK)
return nil return nil
} }

View File

@ -94,12 +94,12 @@ func (m *master) startReplication(masterAddr string, restart bool) error {
m.app.ldb.SetReadOnly(true) m.app.ldb.SetReadOnly(true)
m.wg.Add(1)
go m.runReplication(restart) go m.runReplication(restart)
return nil return nil
} }
func (m *master) runReplication(restart bool) { func (m *master) runReplication(restart bool) {
m.wg.Add(1)
defer m.wg.Done() defer m.wg.Done()
for { for {
@ -245,6 +245,8 @@ func (app *App) slaveof(masterAddr string, restart bool) error {
return fmt.Errorf("slaveof must enable replication") return fmt.Errorf("slaveof must enable replication")
} }
app.cfg.SlaveOf = masterAddr
if len(masterAddr) == 0 { if len(masterAddr) == 0 {
if err := app.m.stopReplication(); err != nil { if err := app.m.stopReplication(); err != nil {
return err return err
@ -258,6 +260,21 @@ func (app *App) slaveof(masterAddr string, restart bool) error {
return nil return nil
} }
func (app *App) tryReSlaveof() error {
app.m.Lock()
defer app.m.Unlock()
if !app.ldb.ReplicationUsed() {
return nil
}
if len(app.cfg.SlaveOf) == 0 {
return nil
} else {
return app.m.startReplication(app.cfg.SlaveOf, true)
}
}
func (app *App) addSlave(c *client) { func (app *App) addSlave(c *client) {
app.slock.Lock() app.slock.Lock()
defer app.slock.Unlock() defer app.slock.Unlock()