Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Raft nodes not to erase their votes on leader step down #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 40 additions & 22 deletions src/v/raft/consensus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ void consensus::setup_metrics() {
}

void consensus::do_step_down() {
_voted_for = {};
_hbeat = clock_type::now();
_vstate = vote_state::follower;
}
Expand Down Expand Up @@ -700,6 +699,7 @@ void consensus::read_voted_for() {
* Initial values
*/
_voted_for = model::node_id{};
_last_election = model::term_id(-1);
_term = model::term_id(0);

/*
Expand Down Expand Up @@ -800,8 +800,9 @@ ss::future<vote_reply> consensus::do_vote(vote_request&& r) {
r.node_id,
r.term,
_term);
reply.term = r.term;
_term = r.term;
reply.term = _term;
_voted_for = {};
do_step_down();
// even tough we step down we do not want to update the hbeat as it
// would cause subsequent votes to fail (_hbeat is updated by the
Expand All @@ -814,33 +815,48 @@ ss::future<vote_reply> consensus::do_vote(vote_request&& r) {
return ss::make_ready_future<vote_reply>(reply);
}

auto f = ss::make_ready_future<>();

if (_voted_for() < 0) {
_voted_for = model::node_id(r.node_id);
vlog(_ctxlog.trace, "Voting for {} in term {}", r.node_id, _term);
f = f.then([this] {
return write_voted_for({_voted_for, _term})
.handle_exception([this](const std::exception_ptr& e) {
if (_last_election == _term) {
// another fiber is trying to vote at the current term
reply.granted = false;
return ss::make_ready_future<vote_reply>(std::move(reply));
}

_last_election = _term;

return write_voted_for({r.node_id, _term})
.then_wrapped([this,
reply = std::move(reply),
voting_started_at = _term,
r = std::move(r)](ss::future<> f) {
bool granted = false;

if (f.failed()) {
vlog(
_ctxlog.warn,
"Unable to persist raft group state, vote not granted "
"- {}",
e);
_voted_for = {};
});
});
}

// vote for the same term, same server_id
reply.granted = (r.node_id == _voted_for);
if (reply.granted) {
_hbeat = clock_type::now();
}
f.get_exception());
} else {
if (voting_started_at == _term) {
_voted_for = r.node_id;
granted = true;
}
}

return f.then([reply = std::move(reply)] {
vote_reply response;
response.term = reply.term;
response.log_ok = reply.log_ok;
response.granted = granted;
return ss::make_ready_future<vote_reply>(std::move(response));
});
} else {
reply.granted = (r.node_id == _voted_for);
if (reply.granted) {
_hbeat = clock_type::now();
}
return ss::make_ready_future<vote_reply>(std::move(reply));
});
}
}

ss::future<append_entries_reply>
Expand Down Expand Up @@ -883,6 +899,7 @@ consensus::do_append_entries(append_entries_request&& r) {
r.meta.term,
_term);
_term = r.meta.term;
_voted_for = {};
return do_append_entries(std::move(r));
}

Expand Down Expand Up @@ -1130,6 +1147,7 @@ consensus::do_install_snapshot(install_snapshot_request&& r) {
// request received from new leader
if (r.term > _term) {
_term = r.term;
_voted_for = {};
do_step_down();
return do_install_snapshot(std::move(r));
}
Expand Down
2 changes: 2 additions & 0 deletions src/v/raft/consensus.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class consensus {
// term backward
if (term > _term) {
_term = term;
_voted_for = {};
do_step_down();
}
});
Expand Down Expand Up @@ -307,6 +308,7 @@ class consensus {

// read at `ss::future<> start()`
model::node_id _voted_for;
model::term_id _last_election;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do not need to store _last_election this is what the voted_for is for. Vote casting is done under the semaphore even if another fiber requested vote with the same term the voted_for would be already set to some value so the vote not be granted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, I haven't noticed the semaphore and wanted to avoid concurrency issues, will send v2

std::optional<model::node_id> _leader_id;
bool _transferring_leadership{false};

Expand Down
12 changes: 12 additions & 0 deletions src/v/raft/vote_stm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ ss::future<> vote_stm::vote(bool leadership_transfer) {
_ptr->trigger_leadership_notification();
// 5.2.1.2
_ptr->_term += model::term_id(1);
_ptr->_voted_for = {};

// vote is the only method under _op_sem
_ptr->config().for_each_voter(
Expand Down Expand Up @@ -186,15 +187,26 @@ ss::future<> vote_stm::wait() { return _vote_bg.close(); }

void vote_stm::update_vote_state(ss::semaphore_units<> u) {
// use reply term to update voter term
bool is_term_behind = false;

for (auto& [_, r] : _replies) {
if (r.value && r.value->has_value()) {
auto term = r.value->value().term;
if (term > _ptr->_term) {
vlog(
_ctxlog.info, "Vote failed - received larger term: {}", term);
_ptr->_term = term;
_ptr->_voted_for = {};
is_term_behind = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simply return here. then we wouldn't need the is_term_behind.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you also need the vote_state::follower assignment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I'll include it in v2

}
}
}

if (is_term_behind) {
_ptr->_vstate = consensus::vote_state::follower;
return;
}

if (_ptr->_vstate != consensus::vote_state::candidate) {
vlog(_ctxlog.info, "No longer a candidate, ignoring vote replies");
return;
Expand Down