Browse Source

raft: add progress into status

Xiang Li 11 years ago
parent
commit
b34936b097
2 changed files with 30 additions and 22 deletions
  1. 9 7
      raft/node.go
  2. 21 15
      raft/status.go

+ 9 - 7
raft/node.go

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

+ 21 - 15
raft/status.go

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