From 9b4c23353ebf0cd981b68dbcc91da346e151944c Mon Sep 17 00:00:00 2001 From: siddontang Date: Mon, 10 Nov 2014 14:13:29 +0800 Subject: [PATCH] control commit id flush --- rpl/rpl.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/rpl/rpl.go b/rpl/rpl.go index 5e9eca8..b0453ef 100644 --- a/rpl/rpl.go +++ b/rpl/rpl.go @@ -24,8 +24,9 @@ type Replication struct { s LogStore - commitID uint64 - commitLog *os.File + 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,17 +212,23 @@ func (r *Replication) Stat() (*Stat, error) { return s, nil } -func (r *Replication) updateCommitID(id uint64) error { - if _, err := r.commitLog.Seek(0, os.SEEK_SET); err != nil { - return err - } +func (r *Replication) updateCommitID(id uint64, force bool) error { + n := time.Now() - if err := binary.Write(r.commitLog, binary.BigEndian, id); err != nil { - return err + if force || n.Sub(r.commitLastTime) > time.Second { + if _, err := r.commitLog.Seek(0, os.SEEK_SET); err != nil { + return err + } + + 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() {