From 9616162dbc8cbebf95b98bea08a2e4cd8c4e5961 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Sat, 23 Aug 2014 17:22:39 -0700 Subject: [PATCH] raft: remove return bool --- raft2/node.go | 12 ++++++++++++ raft2/raft.go | 17 +++++++---------- raft2/raft_test.go | 5 +++++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/raft2/node.go b/raft2/node.go index d81021fac..1220ef928 100644 --- a/raft2/node.go +++ b/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. func (n *Node) Step(m Message) error { select { diff --git a/raft2/raft.go b/raft2/raft.go index 848790222..a0af663c5 100644 --- a/raft2/raft.go +++ b/raft2/raft.go @@ -405,9 +405,9 @@ func (r *raft) removeNode(id int64) { 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 { case msgBeat: r.bcastHeartbeat() @@ -418,7 +418,7 @@ func stepLeader(r *raft, m Message) bool { e := m.Entries[0] if e.isConfig() { if r.pendingConf { - return false + panic("pending conf") } r.pendingConf = true } @@ -437,13 +437,12 @@ func stepLeader(r *raft, m Message) bool { case msgVote: 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 { case msgProp: - return false + panic("no leader") case msgApp: r.becomeFollower(r.Term, m.From) r.handleAppendEntries(m) @@ -462,14 +461,13 @@ func stepCandidate(r *raft, m Message) bool { r.becomeFollower(r.Term, none) } } - return true } -func stepFollower(r *raft, m Message) bool { +func stepFollower(r *raft, m Message) { switch m.Type { case msgProp: if r.lead.Get() == none { - return false + panic("no leader") } m.To = r.lead.Get() r.send(m) @@ -486,7 +484,6 @@ func stepFollower(r *raft, m Message) bool { r.send(Message{To: m.From, Type: msgVoteResp, Index: -1}) } } - return true } func (r *raft) compact(d []byte) { diff --git a/raft2/raft_test.go b/raft2/raft_test.go index e0e8730f2..23dbcea32 100644 --- a/raft2/raft_test.go +++ b/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 + paniced := false + defer func() { recover(); paniced = true }() sm.Step(Message{From: 0, To: 0, Type: msgProp, Entries: []Entry{{Type: AddNode}}}) + if !paniced { + t.Errorf("expected panic") + } if sm.raftLog.lastIndex() != 2 { t.Errorf("lastindex = %d, want %d", sm.raftLog.lastIndex(), 1) }