mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
raft: record previous state is enough
This commit is contained in:
parent
0060c0749a
commit
b0d865e845
16
raft/node.go
16
raft/node.go
@ -9,7 +9,10 @@ import (
|
|||||||
"github.com/coreos/etcd/third_party/code.google.com/p/go.net/context"
|
"github.com/coreos/etcd/third_party/code.google.com/p/go.net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrStopped = errors.New("raft: stopped")
|
var (
|
||||||
|
EmptyState = pb.State{}
|
||||||
|
ErrStopped = errors.New("raft: stopped")
|
||||||
|
)
|
||||||
|
|
||||||
// Ready encapsulates the entries and messages that are ready to be saved to
|
// Ready encapsulates the entries and messages that are ready to be saved to
|
||||||
// stable storage, committed or sent to other peers.
|
// stable storage, committed or sent to other peers.
|
||||||
@ -35,8 +38,8 @@ func isStateEqual(a, b pb.State) bool {
|
|||||||
return a.Term == b.Term && a.Vote == b.Vote && a.LastIndex == b.LastIndex
|
return a.Term == b.Term && a.Vote == b.Vote && a.LastIndex == b.LastIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rd Ready) containsUpdates(prev Ready) bool {
|
func (rd Ready) containsUpdates(prevSt pb.State) bool {
|
||||||
return !isStateEqual(prev.State, rd.State) || len(rd.Entries) > 0 || len(rd.CommittedEntries) > 0 || len(rd.Messages) > 0
|
return !isStateEqual(prevSt, rd.State) || len(rd.Entries) > 0 || len(rd.CommittedEntries) > 0 || len(rd.Messages) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
@ -83,8 +86,7 @@ func (n *Node) run(r *raft) {
|
|||||||
readyc := n.readyc
|
readyc := n.readyc
|
||||||
|
|
||||||
var lead int64
|
var lead int64
|
||||||
var prev Ready
|
prevSt := r.State
|
||||||
prev.State = r.State
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if lead != r.lead {
|
if lead != r.lead {
|
||||||
@ -104,7 +106,7 @@ func (n *Node) run(r *raft) {
|
|||||||
r.msgs,
|
r.msgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
if rd.containsUpdates(prev) {
|
if rd.containsUpdates(prevSt) {
|
||||||
readyc = n.readyc
|
readyc = n.readyc
|
||||||
} else {
|
} else {
|
||||||
readyc = nil
|
readyc = nil
|
||||||
@ -121,7 +123,7 @@ func (n *Node) run(r *raft) {
|
|||||||
case readyc <- rd:
|
case readyc <- rd:
|
||||||
r.raftLog.resetNextEnts()
|
r.raftLog.resetNextEnts()
|
||||||
r.raftLog.resetUnstable()
|
r.raftLog.resetUnstable()
|
||||||
prev = rd
|
prevSt = rd.State
|
||||||
r.msgs = nil
|
r.msgs = nil
|
||||||
case <-n.done:
|
case <-n.done:
|
||||||
return
|
return
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user