control commit id flush

This commit is contained in:
siddontang 2014-11-10 14:13:29 +08:00
parent a0abd79328
commit 9b4c23353e
1 changed files with 21 additions and 10 deletions

View File

@ -26,6 +26,7 @@ type Replication struct {
commitID uint64
commitLog *os.File
commitLastTime time.Time
quit chan struct{}
@ -92,6 +93,10 @@ func (r *Replication) Close() error {
r.s = nil
}
if err := r.updateCommitID(r.commitID, true); err != nil {
log.Error("update commit id err %s", err.Error())
}
if r.commitLog != nil {
r.commitLog.Close()
r.commitLog = nil
@ -185,7 +190,7 @@ func (r *Replication) UpdateCommitID(id uint64) error {
r.m.Lock()
defer r.m.Unlock()
return r.updateCommitID(id)
return r.updateCommitID(id, r.cfg.Replication.SyncLog == 2)
}
func (r *Replication) Stat() (*Stat, error) {
@ -207,7 +212,10 @@ func (r *Replication) Stat() (*Stat, error) {
return s, nil
}
func (r *Replication) updateCommitID(id uint64) error {
func (r *Replication) updateCommitID(id uint64, force bool) error {
n := time.Now()
if force || n.Sub(r.commitLastTime) > time.Second {
if _, err := r.commitLog.Seek(0, os.SEEK_SET); err != nil {
return err
}
@ -215,9 +223,12 @@ func (r *Replication) updateCommitID(id uint64) error {
if err := binary.Write(r.commitLog, binary.BigEndian, id); err != nil {
return err
}
}
r.commitID = id
r.commitLastTime = n
return nil
}
@ -266,7 +277,7 @@ func (r *Replication) ClearWithCommitID(id uint64) error {
return err
}
return r.updateCommitID(id)
return r.updateCommitID(id, true)
}
func (r *Replication) onPurgeExpired() {