raft: remove panic when we see a proposal with no leader.

This panic can never be reached when using raft.Node, because we only
read from propc when there is a leader. However, it is possible to see
this error when using raft the raft object directly (as in MultiNode),
and in this case it is better to simply drop the proposal (as if we had
sent it to a leader that immediately vanished).

Add an error return to MemoryStorage.Append for consistency.
This commit is contained in:
Ben Darnell 2014-12-10 17:34:40 -05:00
parent d4dcd39b83
commit fa247d09cc
2 changed files with 8 additions and 5 deletions

View File

@ -479,7 +479,8 @@ func stepLeader(r *raft, m pb.Message) {
func stepCandidate(r *raft, m pb.Message) { func stepCandidate(r *raft, m pb.Message) {
switch m.Type { switch m.Type {
case pb.MsgProp: case pb.MsgProp:
panic("no leader") log.Printf("raft: no leader; dropping proposal")
return
case pb.MsgApp: case pb.MsgApp:
r.becomeFollower(r.Term, m.From) r.becomeFollower(r.Term, m.From)
r.handleAppendEntries(m) r.handleAppendEntries(m)
@ -510,7 +511,8 @@ func stepFollower(r *raft, m pb.Message) {
switch m.Type { switch m.Type {
case pb.MsgProp: case pb.MsgProp:
if r.lead == None { if r.lead == None {
panic("no leader") log.Printf("raft: no leader; dropping proposal")
return
} }
m.To = r.lead m.To = r.lead
r.send(m) r.send(m)

View File

@ -180,18 +180,18 @@ func (ms *MemoryStorage) Compact(i uint64, cs *pb.ConfState, data []byte) error
} }
// Append the new entries to storage. // Append the new entries to storage.
func (ms *MemoryStorage) Append(entries []pb.Entry) { func (ms *MemoryStorage) Append(entries []pb.Entry) error {
ms.Lock() ms.Lock()
defer ms.Unlock() defer ms.Unlock()
if len(entries) == 0 { if len(entries) == 0 {
return return nil
} }
first := ms.snapshot.Metadata.Index + 1 first := ms.snapshot.Metadata.Index + 1
last := entries[0].Index + uint64(len(entries)) - 1 last := entries[0].Index + uint64(len(entries)) - 1
// shortcut if there is no new entry. // shortcut if there is no new entry.
if last < first { if last < first {
return return nil
} }
// truncate old entries // truncate old entries
if first > entries[0].Index { if first > entries[0].Index {
@ -209,4 +209,5 @@ func (ms *MemoryStorage) Append(entries []pb.Entry) {
log.Panicf("missing log entry [last: %d, append at: %d]", log.Panicf("missing log entry [last: %d, append at: %d]",
ms.snapshot.Metadata.Index+uint64(len(ms.ents)), entries[0].Index) ms.snapshot.Metadata.Index+uint64(len(ms.ents)), entries[0].Index)
} }
return nil
} }