From 90c0db3d421d88d4a56c0a0d43d18871fbf45c65 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Mon, 8 Sep 2014 21:45:10 -0700 Subject: [PATCH] wal: do not save empty state --- raft/node.go | 12 ++++++++---- raft/node_test.go | 2 +- wal/wal.go | 4 ++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/raft/node.go b/raft/node.go index 502c1e378..9ebf19bd3 100644 --- a/raft/node.go +++ b/raft/node.go @@ -10,7 +10,7 @@ import ( ) var ( - EmptyState = pb.State{} + emptyState = pb.State{} ErrStopped = errors.New("raft: stopped") ) @@ -38,8 +38,12 @@ func isStateEqual(a, b pb.State) bool { return a.Term == b.Term && a.Vote == b.Vote && a.LastIndex == b.LastIndex } +func IsEmptyState(st pb.State) bool { + return isStateEqual(st, emptyState) +} + func (rd Ready) containsUpdates() bool { - return !isStateEqual(EmptyState, rd.State) || len(rd.Entries) > 0 || len(rd.CommittedEntries) > 0 || len(rd.Messages) > 0 + return !isStateEqual(emptyState, rd.State) || len(rd.Entries) > 0 || len(rd.CommittedEntries) > 0 || len(rd.Messages) > 0 } type Node struct { @@ -106,7 +110,7 @@ func (n *Node) run(r *raft) { } if isStateEqual(r.State, prevSt) { - rd.State = EmptyState + rd.State = emptyState } else { rd.State = r.State } @@ -128,7 +132,7 @@ func (n *Node) run(r *raft) { case readyc <- rd: r.raftLog.resetNextEnts() r.raftLog.resetUnstable() - if !isStateEqual(rd.State, EmptyState) { + if !IsEmptyState(rd.State) { prevSt = rd.State } r.msgs = nil diff --git a/raft/node_test.go b/raft/node_test.go index 21b800483..10e586e74 100644 --- a/raft/node_test.go +++ b/raft/node_test.go @@ -51,7 +51,7 @@ func TestNodeRestart(t *testing.T) { st := raftpb.State{Term: 1, Vote: -1, Commit: 1, LastIndex: 2} want := Ready{ - State: EmptyState, + State: emptyState, // commit upto index commit index in st CommittedEntries: entries[:st.Commit], } diff --git a/wal/wal.go b/wal/wal.go index 3335fb3d1..bd0a1140a 100644 --- a/wal/wal.go +++ b/wal/wal.go @@ -26,6 +26,7 @@ import ( "path" "sort" + "github.com/coreos/etcd/raft" "github.com/coreos/etcd/raft/raftpb" "github.com/coreos/etcd/wal/walpb" ) @@ -253,6 +254,9 @@ func (w *WAL) SaveEntry(e *raftpb.Entry) error { } func (w *WAL) SaveState(s *raftpb.State) error { + if raft.IsEmptyState(*s) { + return nil + } log.Printf("path=%s wal.saveState state=\"%+v\"", w.f.Name(), s) b, err := s.Marshal() if err != nil {