wal: do not save empty state
parent
c28fef5fc4
commit
90c0db3d42
12
raft/node.go
12
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
|
||||
|
|
|
@ -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],
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue