Browse Source

raft: implement ReadState

Blake Mizerany 11 years ago
parent
commit
71ed92b6bd
1 changed files with 11 additions and 2 deletions
  1. 11 2
      raft2/node.go

+ 11 - 2
raft2/node.go

@@ -47,6 +47,13 @@ func (n *Node) run(r *raft) {
 			propc = nil
 			propc = nil
 		}
 		}
 
 
+		// TODO(bmizerany): move to raft.go or log.go by removing the
+		// idea "unstable" in those files. Callers of ReadState can
+		// determine what is committed by comparing State.Commit to
+		// each Entry.Index. This will also avoid this horrible copy
+		// and alloc.
+		ents := append(r.raftLog.nextEnts(), r.raftLog.unstableEnts()...)
+
 		select {
 		select {
 		case p := <-propc:
 		case p := <-propc:
 			r.propose(p)
 			r.propose(p)
@@ -54,8 +61,10 @@ func (n *Node) run(r *raft) {
 			r.Step(m) // raft never returns an error
 			r.Step(m) // raft never returns an error
 		case <-n.tickc:
 		case <-n.tickc:
 			// r.tick()
 			// r.tick()
-		// case n.statec <- stateResp{r.State, r.ents, r.msgs}:
-		// r.resetState()
+		case n.statec <- stateResp{r.State, ents, r.msgs}:
+			r.raftLog.resetNextEnts()
+			r.raftLog.resetUnstable()
+			r.msgs = nil
 		case <-n.ctx.Done():
 		case <-n.ctx.Done():
 			return
 			return
 		}
 		}