From 53e7ddbc66f9c052fbfa36fca1490b3e8f8c75a1 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Sun, 13 Mar 2016 20:34:37 -0700 Subject: [PATCH] wal: do not call fsync when it is not necessary --- wal/wal.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/wal/wal.go b/wal/wal.go index 8a61fd564..99f1a9c11 100644 --- a/wal/wal.go +++ b/wal/wal.go @@ -502,6 +502,8 @@ func (w *WAL) Save(st raftpb.HardState, ents []raftpb.Entry) error { return nil } + mustSync := mustSync(st, w.state, len(ents)) + // TODO(xiangli): no more reference operator for i := range ents { if err := w.saveEntry(&ents[i]); err != nil { @@ -517,7 +519,10 @@ func (w *WAL) Save(st raftpb.HardState, ents []raftpb.Entry) error { return err } if fstat.Size() < segmentSizeBytes { - return w.sync() + if mustSync { + return w.sync() + } + return nil } // TODO: add a test for this code path when refactoring the tests return w.cut() @@ -543,3 +548,15 @@ func (w *WAL) SaveSnapshot(e walpb.Snapshot) error { func (w *WAL) saveCrc(prevCrc uint32) error { return w.encoder.encode(&walpb.Record{Type: crcType, Crc: prevCrc}) } + +func mustSync(st, prevst raftpb.HardState, entsnum int) bool { + // Persistent state on all servers: + // (Updated on stable storage before responding to RPCs) + // currentTerm + // votedFor + // log entries[] + if entsnum != 0 || st.Vote != prevst.Vote || st.Term != prevst.Term { + return true + } + return false +}