From 8a933e6976fa7bc9ebb93942fcb36d59e3e46b1c Mon Sep 17 00:00:00 2001 From: alexey bashtanov Date: Fri, 12 Jul 2024 17:17:05 +0100 Subject: [PATCH] r/vote_stm: assert we're not becoming a leader in the background --- src/v/raft/vote_stm.cc | 15 ++++++++++++--- src/v/raft/vote_stm.h | 2 ++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/v/raft/vote_stm.cc b/src/v/raft/vote_stm.cc index f655f09716406..a122a4c0733f1 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 6a7272f0ac0e7..6a95d7a3a7ff6 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();