From 62aa52f644cd8b260d3275c24b1f9d3bb2cbc31b 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..c1efd5a802bfe 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; + lose(); 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; + lose(); 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; + lose(); 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::lose() { + 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..4012569e8d2d8 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 lose(); + friend std::ostream& operator<<(std::ostream&, const vmeta&); ss::future do_vote();