Skip to content

Commit

Permalink
r/vote_stm: assert we're not becoming a leader in the background
Browse files Browse the repository at this point in the history
  • Loading branch information
bashtanov committed Jul 16, 2024
1 parent 2223844 commit deabb87
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/v/raft/vote_stm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
}
/**
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/v/raft/vote_stm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<election_success> do_vote();
Expand Down

0 comments on commit deabb87

Please sign in to comment.