Browse Source

raft: update based on example

Blake Mizerany 11 years ago
parent
commit
92bdb1390d
2 changed files with 26 additions and 29 deletions
  1. 13 16
      raft2/example_test.go
  2. 13 13
      raft2/node.go

+ 13 - 16
raft2/example_test.go

@@ -6,36 +6,33 @@ import (
 	"code.google.com/p/go.net/context"
 	"code.google.com/p/go.net/context"
 )
 )
 
 
-func apply(e Entry)               {}
-func sendMessages(msgs []Message) {}
-func saveToDisk(ents []Entry)     {}
+func applyToStore(ents []Entry)        {}
+func sendMessages(msgs []Message)      {}
+func saveStateToDisk(st State)         {}
+func saveToDisk(ents []Entry)          {}
+func stateChanged(prev, st State) bool { return false }
 
 
 func Example_Node() {
 func Example_Node() {
 	n := Start(context.Background(), "", 0, 0)
 	n := Start(context.Background(), "", 0, 0)
 
 
 	// stuff to n happens in other gorotines
 	// stuff to n happens in other gorotines
 
 
-	// a cache of entries that have been saved to disk, but not yet
-	// committed the the store
-	var cents []Entry
+	// the last known state
+	var prev State
 	for {
 	for {
 		// ReadState blocks until there is new state ready.
 		// ReadState blocks until there is new state ready.
-		st, ents, msgs, err := n.ReadState()
+		st, ents, cents, msgs, err := n.ReadState()
 		if err != nil {
 		if err != nil {
 			log.Fatal(err)
 			log.Fatal(err)
 		}
 		}
 
 
-		saveToDisk(ents)
-
-		cents = append(cents, ents...)
-		for i, e := range cents {
-			if e.Index > st.Commit {
-				cents = cents[i:]
-				break
-			}
-			apply(e)
+		if stateChanged(prev, st) {
+			saveStateToDisk(st)
+			prev = st
 		}
 		}
 
 
+		saveToDisk(ents)
+		applyToStore(cents)
 		sendMessages(msgs)
 		sendMessages(msgs)
 	}
 	}
 }
 }

+ 13 - 13
raft2/node.go

@@ -4,9 +4,9 @@ package raft
 import "code.google.com/p/go.net/context"
 import "code.google.com/p/go.net/context"
 
 
 type stateResp struct {
 type stateResp struct {
-	state State
-	ents  []Entry
-	msgs  []Message
+	state       State
+	ents, cents []Entry
+	msgs        []Message
 }
 }
 
 
 type Node struct {
 type Node struct {
@@ -47,12 +47,12 @@ 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()...)
+		sr := stateResp{
+			r.State,
+			r.raftLog.unstableEnts(),
+			r.raftLog.nextEnts(),
+			r.msgs,
+		}
 
 
 		select {
 		select {
 		case p := <-propc:
 		case p := <-propc:
@@ -61,7 +61,7 @@ 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, ents, r.msgs}:
+		case n.statec <- sr:
 			r.raftLog.resetNextEnts()
 			r.raftLog.resetNextEnts()
 			r.raftLog.resetUnstable()
 			r.raftLog.resetUnstable()
 			r.msgs = nil
 			r.msgs = nil
@@ -101,11 +101,11 @@ func (n *Node) Step(m Message) error {
 }
 }
 
 
 // ReadState returns the current point-in-time state.
 // ReadState returns the current point-in-time state.
-func (n *Node) ReadState() (State, []Entry, []Message, error) {
+func (n *Node) ReadState() (st State, ents, cents []Entry, msgs []Message, err error) {
 	select {
 	select {
 	case sr := <-n.statec:
 	case sr := <-n.statec:
-		return sr.state, sr.ents, sr.msgs, nil
+		return sr.state, sr.ents, sr.cents, sr.msgs, nil
 	case <-n.ctx.Done():
 	case <-n.ctx.Done():
-		return State{}, nil, nil, n.ctx.Err()
+		return State{}, nil, nil, nil, n.ctx.Err()
 	}
 	}
 }
 }