func (t *tableWriter) close() { if t.meta != nil { if err := t.meta.Close(); err != nil { log.Fatalf("close log meta error %s", err.Error()) } t.meta = nil } if t.data != nil { if _, err := t.data.Write(magic); err != nil { log.Fatalf("write magic error %s", err.Error()) } if err := t.data.Close(); err != nil { log.Fatalf("close log data error %s", err.Error()) } t.data = nil } }
func (c *respClient) run() { defer func() { if e := recover(); e != nil { buf := make([]byte, 4096) n := runtime.Stack(buf, false) buf = buf[0:n] log.Fatalf("client run panic %s:%v", buf, e) } c.client.close() c.conn.Close() // if c.tx != nil { // c.tx.Rollback() // c.tx = nil // } c.app.removeSlave(c.client, c.activeQuit) c.app.delRespClient(c) c.app.connWait.Done() }() select { case <-c.app.quit: //check app closed return default: break } kc := time.Duration(c.app.cfg.ConnKeepaliveInterval) * time.Second for { if kc > 0 { c.conn.SetReadDeadline(time.Now().Add(kc)) } c.cmd = "" c.args = nil reqData, err := c.respReader.ParseRequest() if err == nil { err = c.handleRequest(reqData) } if err != nil { return } } }
func (l *Ledis) handleCommit(g commitDataGetter, c commiter) error { l.commitLock.Lock() var err error if l.r != nil { var rl *rpl.Log if rl, err = l.r.Log(g.Data()); err != nil { l.commitLock.Unlock() log.Fatalf("write wal error %s", err.Error()) return err } l.propagate(rl) if err = c.Commit(); err != nil { l.commitLock.Unlock() log.Fatalf("commit error %s", err.Error()) l.noticeReplication() return err } if err = l.r.UpdateCommitID(rl.ID); err != nil { l.commitLock.Unlock() log.Fatalf("update commit id error %s", err.Error()) l.noticeReplication() return err } } else { err = c.Commit() } l.commitLock.Unlock() return err }
func (l *Ledis) flushAll() error { it := l.ldb.NewIterator() defer it.Close() it.SeekToFirst() w := l.ldb.NewWriteBatch() defer w.Rollback() n := 0 for ; it.Valid(); it.Next() { n++ if n == 10000 { if err := w.Commit(); err != nil { log.Fatalf("flush all commit error: %s", err.Error()) return err } n = 0 } w.Delete(it.RawKey()) } if err := w.Commit(); err != nil { log.Fatalf("flush all commit error: %s", err.Error()) return err } if l.r != nil { if err := l.r.Clear(); err != nil { log.Fatalf("flush all replication clear error: %s", err.Error()) return err } } return nil }
func (m *master) runReplication(restart bool) { defer func() { m.state.Set(replConnectState) m.wg.Done() }() for { m.state.Set(replConnectState) if m.isQuited() { return } if err := m.checkConn(); err != nil { log.Errorf("check master %s connection error %s, try 3s later", m.addr, err.Error()) select { case <-time.After(3 * time.Second): case <-m.quit: return } continue } if m.isQuited() { return } m.state.Set(replConnectedState) if err := m.replConf(); err != nil { if strings.Contains(err.Error(), ledis.ErrRplNotSupport.Error()) { log.Fatalf("master doesn't support replication, wait 10s and retry") select { case <-time.After(10 * time.Second): case <-m.quit: return } } else { log.Errorf("replconf error %s", err.Error()) } continue } if restart { if err := m.fullSync(); err != nil { log.Errorf("restart fullsync error %s", err.Error()) continue } m.state.Set(replConnectedState) } for { if err := m.sync(); err != nil { log.Errorf("sync error %s", err.Error()) break } m.state.Set(replConnectedState) if m.isQuited() { return } } } return }