diff --git a/src/v/raft/vote_stm.cc b/src/v/raft/vote_stm.cc index f655f0971640..a122a4c0733f 100644 --- a/src/v/raft/vote_stm.cc +++ b/src/v/raft/vote_stm.cc @@ -357,7 +357,7 @@ ss::future<> vote_stm::update_vote_state(ssx::semaphore_units u) { term); _ptr->_term = term; _ptr->_voted_for = {}; - _ptr->_vstate = consensus::vote_state::follower; + fail_election(); co_return; } } @@ -379,7 +379,7 @@ ss::future<> vote_stm::update_vote_state(ssx::semaphore_units u) { } if (!_success) { vlog(_ctxlog.info, "[pre-vote: {}] vote failed", _prevote); - _ptr->_vstate = consensus::vote_state::follower; + fail_election(); co_return; } /** @@ -397,7 +397,7 @@ ss::future<> vote_stm::update_vote_state(ssx::semaphore_units u) { "[pre-vote: false] Ignoring successful vote. Node priority too low: " "{}", _ptr->_node_priority_override.value()); - _ptr->_vstate = consensus::vote_state::follower; + fail_election(); co_return; } @@ -479,4 +479,13 @@ ss::future<> vote_stm::self_vote() { auto m = _replies.find(_ptr->self()); m->second.set_value(reply); } + +void vote_stm::fail_election() { + vassert( + _ptr->_vstate != consensus::vote_state::leader + && _ptr->_hbeat != clock_type::time_point::max(), + "Became a leader outside current election"); + _ptr->_vstate = consensus::vote_state::follower; +} + } // namespace raft diff --git a/src/v/raft/vote_stm.h b/src/v/raft/vote_stm.h index 6a7272f0ac0e..6a95d7a3a7ff 100644 --- a/src/v/raft/vote_stm.h +++ b/src/v/raft/vote_stm.h @@ -120,6 +120,8 @@ class vote_stm { ss::future<> wait_for_next_reply(); + void fail_election(); + friend std::ostream& operator<<(std::ostream&, const vmeta&); ss::future do_vote();