Browse Source

raft: remove return bool

Blake Mizerany 11 years ago
parent
commit
9616162dbc
3 changed files with 24 additions and 10 deletions
  1. 12 0
      raft2/node.go
  2. 7 10
      raft2/raft.go
  3. 5 0
      raft2/raft_test.go

+ 12 - 0
raft2/node.go

@@ -104,6 +104,18 @@ func (n *Node) Propose(ctx context.Context, data []byte) error {
 	}
 	}
 }
 }
 
 
+// func (n *Node) SingleFlightPropose(ctx context.Context, id string, data []byte) error {
+// 	ch := n.getSingleFlightChan(id)
+// 	select {
+// 	case ch <- data:
+// 		return nil
+// 	case <-ctx.Done():
+// 		return ctx.Err()
+// 	case <-n.ctx.Done():
+// 		return n.ctx.Err()
+// 	}
+// }
+
 // Step advances the state machine using m.
 // Step advances the state machine using m.
 func (n *Node) Step(m Message) error {
 func (n *Node) Step(m Message) error {
 	select {
 	select {

+ 7 - 10
raft2/raft.go

@@ -405,9 +405,9 @@ func (r *raft) removeNode(id int64) {
 	r.pendingConf = false
 	r.pendingConf = false
 }
 }
 
 
-type stepFunc func(r *raft, m Message) bool
+type stepFunc func(r *raft, m Message)
 
 
-func stepLeader(r *raft, m Message) bool {
+func stepLeader(r *raft, m Message) {
 	switch m.Type {
 	switch m.Type {
 	case msgBeat:
 	case msgBeat:
 		r.bcastHeartbeat()
 		r.bcastHeartbeat()
@@ -418,7 +418,7 @@ func stepLeader(r *raft, m Message) bool {
 		e := m.Entries[0]
 		e := m.Entries[0]
 		if e.isConfig() {
 		if e.isConfig() {
 			if r.pendingConf {
 			if r.pendingConf {
-				return false
+				panic("pending conf")
 			}
 			}
 			r.pendingConf = true
 			r.pendingConf = true
 		}
 		}
@@ -437,13 +437,12 @@ func stepLeader(r *raft, m Message) bool {
 	case msgVote:
 	case msgVote:
 		r.send(Message{To: m.From, Type: msgVoteResp, Index: -1})
 		r.send(Message{To: m.From, Type: msgVoteResp, Index: -1})
 	}
 	}
-	return true
 }
 }
 
 
-func stepCandidate(r *raft, m Message) bool {
+func stepCandidate(r *raft, m Message) {
 	switch m.Type {
 	switch m.Type {
 	case msgProp:
 	case msgProp:
-		return false
+		panic("no leader")
 	case msgApp:
 	case msgApp:
 		r.becomeFollower(r.Term, m.From)
 		r.becomeFollower(r.Term, m.From)
 		r.handleAppendEntries(m)
 		r.handleAppendEntries(m)
@@ -462,14 +461,13 @@ func stepCandidate(r *raft, m Message) bool {
 			r.becomeFollower(r.Term, none)
 			r.becomeFollower(r.Term, none)
 		}
 		}
 	}
 	}
-	return true
 }
 }
 
 
-func stepFollower(r *raft, m Message) bool {
+func stepFollower(r *raft, m Message) {
 	switch m.Type {
 	switch m.Type {
 	case msgProp:
 	case msgProp:
 		if r.lead.Get() == none {
 		if r.lead.Get() == none {
-			return false
+			panic("no leader")
 		}
 		}
 		m.To = r.lead.Get()
 		m.To = r.lead.Get()
 		r.send(m)
 		r.send(m)
@@ -486,7 +484,6 @@ func stepFollower(r *raft, m Message) bool {
 			r.send(Message{To: m.From, Type: msgVoteResp, Index: -1})
 			r.send(Message{To: m.From, Type: msgVoteResp, Index: -1})
 		}
 		}
 	}
 	}
-	return true
 }
 }
 
 
 func (r *raft) compact(d []byte) {
 func (r *raft) compact(d []byte) {

+ 5 - 0
raft2/raft_test.go

@@ -629,7 +629,12 @@ func TestConf(t *testing.T) {
 	}
 	}
 
 
 	// deny the second configuration change request if there is a pending one
 	// deny the second configuration change request if there is a pending one
+	paniced := false
+	defer func() { recover(); paniced = true }()
 	sm.Step(Message{From: 0, To: 0, Type: msgProp, Entries: []Entry{{Type: AddNode}}})
 	sm.Step(Message{From: 0, To: 0, Type: msgProp, Entries: []Entry{{Type: AddNode}}})
+	if !paniced {
+		t.Errorf("expected panic")
+	}
 	if sm.raftLog.lastIndex() != 2 {
 	if sm.raftLog.lastIndex() != 2 {
 		t.Errorf("lastindex = %d, want %d", sm.raftLog.lastIndex(), 1)
 		t.Errorf("lastindex = %d, want %d", sm.raftLog.lastIndex(), 1)
 	}
 	}