raft: panic on self-addressed messages

These are nonsensical and a network implementation is not required
to handle them correctly, so panic instead of sending them out.

Signed-off-by: Nathan VanBenschoten <nvanbenschoten@gmail.com>
This commit is contained in:
Nathan VanBenschoten 2022-10-06 20:24:03 -04:00
parent a932fb58f2
commit 419ee8a9c6
3 changed files with 12 additions and 6 deletions

View File

@ -415,6 +415,9 @@ func (r *raft) send(m pb.Message) {
m.Term = r.Term
}
}
if m.To == r.id {
r.logger.Panicf("message should not be self-addressed when sending %s", m.Type)
}
r.msgs = append(r.msgs, m)
}
@ -1282,7 +1285,7 @@ func stepLeader(r *raft, m pb.Message) error {
// we have more entries to send, send as many messages as we
// can (without sending empty messages for the commit index)
if r.id != m.From {
for r.maybeSendAppend(m.From, false) {
for r.maybeSendAppend(m.From, false /* sendIfEmpty */) {
}
}
// Transfer leadership is in progress.
@ -1699,6 +1702,9 @@ func (r *raft) switchToConfig(cfg tracker.Config, prs tracker.ProgressMap) pb.Co
// let them wait out a heartbeat interval (or the next incoming
// proposal).
r.prs.Visit(func(id uint64, pr *tracker.Progress) {
if id == r.id {
return
}
r.maybeSendAppend(id, false /* sendIfEmpty */)
})
}

View File

@ -240,12 +240,12 @@ func TestFollowerVote(t *testing.T) {
nvote uint64
wreject bool
}{
{None, 1, false},
{None, 2, false},
{1, 1, false},
{None, 3, false},
{2, 2, false},
{1, 2, true},
{2, 1, true},
{3, 3, false},
{2, 3, true},
{3, 2, true},
}
for i, tt := range tests {
r := newTestRaft(1, 10, 1, newTestMemoryStorage(withPeers(1, 2, 3)))

View File

@ -952,7 +952,7 @@ func TestRawNodeCommitPaginationAfterRestart(t *testing.T) {
rawNode.Step(pb.Message{
Type: pb.MsgHeartbeat,
To: 1,
From: 1, // illegal, but we get away with it
From: 2, // illegal, but we get away with it
Term: 1,
Commit: 11,
})