소스 검색

wal: do not save empty state

Xiang Li 11 년 전
부모
커밋
90c0db3d42
3개의 변경된 파일13개의 추가작업 그리고 5개의 파일을 삭제
  1. 8 4
      raft/node.go
  2. 1 1
      raft/node_test.go
  3. 4 0
      wal/wal.go

+ 8 - 4
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

+ 1 - 1
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],
 	}

+ 4 - 0
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 {