mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #3877 from bdarnell/campaign-while-leader
raft: no-op instead of panic for Campaigning while leader
This commit is contained in:
commit
5d0268aa2e
10
raft/raft.go
10
raft/raft.go
@ -485,9 +485,13 @@ func (r *raft) poll(id uint64, v bool) (granted int) {
|
|||||||
|
|
||||||
func (r *raft) Step(m pb.Message) error {
|
func (r *raft) Step(m pb.Message) error {
|
||||||
if m.Type == pb.MsgHup {
|
if m.Type == pb.MsgHup {
|
||||||
r.logger.Infof("%x is starting a new election at term %d", r.id, r.Term)
|
if r.state != StateLeader {
|
||||||
r.campaign()
|
r.logger.Infof("%x is starting a new election at term %d", r.id, r.Term)
|
||||||
r.Commit = r.raftLog.committed
|
r.campaign()
|
||||||
|
r.Commit = r.raftLog.committed
|
||||||
|
} else {
|
||||||
|
r.logger.Debugf("%x ignoring MsgHup because already leader", r.id)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1751,6 +1751,27 @@ func TestRaftNodes(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCampaignWhileLeader(t *testing.T) {
|
||||||
|
r := newTestRaft(1, []uint64{1}, 5, 1, NewMemoryStorage())
|
||||||
|
if r.state != StateFollower {
|
||||||
|
t.Errorf("expected new node to be follower but got %s", r.state)
|
||||||
|
}
|
||||||
|
// We don't call campaign() directly because it comes after the check
|
||||||
|
// for our current state.
|
||||||
|
r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgHup})
|
||||||
|
if r.state != StateLeader {
|
||||||
|
t.Errorf("expected single-node election to become leader but got %s", r.state)
|
||||||
|
}
|
||||||
|
term := r.Term
|
||||||
|
r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgHup})
|
||||||
|
if r.state != StateLeader {
|
||||||
|
t.Errorf("expected to remain leader but got %s", r.state)
|
||||||
|
}
|
||||||
|
if r.Term != term {
|
||||||
|
t.Errorf("expected to remain in term %v but got %v", term, r.Term)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func ents(terms ...uint64) *raft {
|
func ents(terms ...uint64) *raft {
|
||||||
storage := NewMemoryStorage()
|
storage := NewMemoryStorage()
|
||||||
for i, term := range terms {
|
for i, term := range terms {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user