raft: add progress into status

This commit is contained in:
Xiang Li 2015-01-18 15:23:50 -08:00
parent 0eaaad0e48
commit b34936b097
2 changed files with 30 additions and 22 deletions

View File

@ -192,7 +192,7 @@ type node struct {
tickc chan struct{} tickc chan struct{}
done chan struct{} done chan struct{}
stop chan struct{} stop chan struct{}
status chan Status status chan chan Status
} }
func newNode() node { func newNode() node {
@ -206,7 +206,7 @@ func newNode() node {
tickc: make(chan struct{}), tickc: make(chan struct{}),
done: make(chan struct{}), done: make(chan struct{}),
stop: make(chan struct{}), stop: make(chan struct{}),
status: make(chan Status), status: make(chan chan Status),
} }
} }
@ -234,11 +234,8 @@ func (n *node) run(r *raft) {
lead := None lead := None
prevSoftSt := r.softState() prevSoftSt := r.softState()
prevHardSt := r.HardState prevHardSt := r.HardState
status := &Status{ID: r.id}
for { for {
status.update(r)
if advancec != nil { if advancec != nil {
readyc = nil readyc = nil
} else { } else {
@ -334,7 +331,8 @@ func (n *node) run(r *raft) {
} }
r.raftLog.stableSnapTo(prevSnapi) r.raftLog.stableSnapTo(prevSnapi)
advancec = nil advancec = nil
case n.status <- status.get(): case c := <-n.status:
c <- getStatus(r)
case <-n.stop: case <-n.stop:
close(n.done) close(n.done)
return return
@ -414,7 +412,11 @@ func (n *node) ApplyConfChange(cc pb.ConfChange) *pb.ConfState {
return &cs return &cs
} }
func (n *node) Status() Status { return <-n.status } func (n *node) Status() Status {
c := make(chan Status)
n.status <- c
return <-c
}
func newReady(r *raft, prevSoftSt *SoftState, prevHardSt pb.HardState) Ready { func newReady(r *raft, prevSoftSt *SoftState, prevHardSt pb.HardState) Ready {
rd := Ready{ rd := Ready{

View File

@ -16,27 +16,33 @@
package raft package raft
import (
pb "github.com/coreos/etcd/raft/raftpb"
)
type Status struct { type Status struct {
ID uint64 ID uint64
Lead uint64 pb.HardState
Term uint64 SoftState
Vote uint64
AppliedIndex uint64 Applied uint64
CommitIndex uint64 Progress map[uint64]progress
} }
func (s *Status) update(r *raft) { func getStatus(r *raft) Status {
s.Lead = r.lead s := Status{ID: r.id}
s.Term = r.Term s.HardState = r.HardState
s.Vote = r.Vote s.SoftState = *r.softState()
s.AppliedIndex = r.raftLog.applied s.Applied = r.raftLog.applied
s.CommitIndex = r.raftLog.committed
}
func (s *Status) get() Status { if s.RaftState == StateLeader {
ns := *s s.Progress = make(map[uint64]progress)
return ns for id, p := range r.prs {
s.Progress[id] = *p
}
}
return s
} }