Skip to content

Commit 4d55e14

Browse files
Fix logging of pre-vote messages (#7458)
Co-authored-by: Amaury Chamayou <amaury@xargs.fr> Co-authored-by: Amaury Chamayou <amchamay@microsoft.com>
1 parent a94b745 commit 4d55e14

File tree

2 files changed

+51
-46
lines changed

2 files changed

+51
-46
lines changed

src/consensus/aft/raft.h

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,7 @@ namespace aft
18921892
.last_committable_idx = r.last_committable_idx,
18931893
.term_of_last_committable_idx = r.term_of_last_committable_idx,
18941894
};
1895+
rv.msg = RaftMsgType::raft_request_pre_vote;
18951896
recv_request_vote_unsafe(from, rv, ElectionType::PreVote);
18961897
}
18971898

@@ -1947,50 +1948,6 @@ namespace aft
19471948
RAFT_TRACE_JSON_OUT(j);
19481949
#endif
19491950

1950-
if (
1951-
state->leadership_state != ccf::kv::LeadershipState::PreVoteCandidate &&
1952-
state->leadership_state != ccf::kv::LeadershipState::Candidate)
1953-
{
1954-
RAFT_INFO_FMT(
1955-
"Recv {} to {} from: {}: we aren't a candidate",
1956-
r.msg,
1957-
state->node_id,
1958-
from);
1959-
return;
1960-
}
1961-
1962-
// Stale message
1963-
if (
1964-
election_type == ElectionType::PreVote &&
1965-
state->leadership_state == ccf::kv::LeadershipState::Candidate)
1966-
{
1967-
RAFT_INFO_FMT(
1968-
"Recv {} to {} from {}: no longer in pre-vote",
1969-
r.msg,
1970-
state->node_id,
1971-
from);
1972-
return;
1973-
}
1974-
1975-
// To receive a RequestVoteResponse(ElectionType::RegularVote), we must
1976-
// have sent a RequestVote(ElectionType::RegularVote), which only
1977-
// candidates do.
1978-
// Hence if we receive a RequestVoteResponse(ElectionType::RegularVote)
1979-
// while still in PreVoteCandidate state something illegal must have
1980-
// happened.
1981-
if (
1982-
election_type == ElectionType::RegularVote &&
1983-
state->leadership_state == ccf::kv::LeadershipState::PreVoteCandidate)
1984-
{
1985-
RAFT_FAIL_FMT(
1986-
"Recv {} to {} from {}: We should not yet have sent a request "
1987-
"vote, as we are still a PreVoteCandidate yet received a response",
1988-
r.msg,
1989-
state->node_id,
1990-
from);
1991-
return;
1992-
}
1993-
19941951
// Ignore if we don't recognise the node.
19951952
auto node = all_other_nodes.find(from);
19961953
if (node == all_other_nodes.end())
@@ -2024,7 +1981,53 @@ namespace aft
20241981
r.term);
20251982
return;
20261983
}
2027-
else if (!r.vote_granted)
1984+
1985+
if (
1986+
state->leadership_state != ccf::kv::LeadershipState::PreVoteCandidate &&
1987+
state->leadership_state != ccf::kv::LeadershipState::Candidate)
1988+
{
1989+
RAFT_INFO_FMT(
1990+
"Recv {} to {} from: {}: we aren't a candidate",
1991+
r.msg,
1992+
state->node_id,
1993+
from);
1994+
return;
1995+
}
1996+
else if (
1997+
election_type == ElectionType::RegularVote &&
1998+
state->leadership_state != ccf::kv::LeadershipState::Candidate)
1999+
{
2000+
// Stale message from previous candidacy
2001+
// Candidate(T) -> Follower(T) -> PreVoteCandidate(T)
2002+
RAFT_INFO_FMT(
2003+
"Recv {} to {} from {}: no longer a candidate in {}",
2004+
r.msg,
2005+
state->node_id,
2006+
from,
2007+
r.term);
2008+
return;
2009+
}
2010+
else if (
2011+
election_type == ElectionType::PreVote &&
2012+
state->leadership_state != ccf::kv::LeadershipState::PreVoteCandidate)
2013+
{
2014+
// To receive a PreVoteResponse, we must have been a PreVoteCandidate in
2015+
// that term.
2016+
// Since we are a Candidate for term T, we can only have transitioned
2017+
// from PreVoteCandidate for term (T-1). Since terms are monotonic this
2018+
// is impossible.
2019+
RAFT_FAIL_FMT(
2020+
"Recv {} to {} from {}: unexpected message in {} when "
2021+
"Candidate for {}",
2022+
r.msg,
2023+
state->node_id,
2024+
from,
2025+
r.term,
2026+
state->current_view);
2027+
return;
2028+
}
2029+
2030+
if (!r.vote_granted)
20282031
{
20292032
// Do nothing.
20302033
RAFT_INFO_FMT(
@@ -2052,6 +2055,7 @@ namespace aft
20522055
const ccf::NodeId& from, RequestPreVoteResponse r)
20532056
{
20542057
RequestVoteResponse rvr{.term = r.term, .vote_granted = r.vote_granted};
2058+
rvr.msg = RaftMsgType::raft_request_pre_vote_response;
20552059
recv_request_vote_response(from, rvr, ElectionType::PreVote);
20562060
}
20572061

tla/consensus/Traceccfraft.tla

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ IsExecuteAppendEntries ==
392392
/\ (logline.msg.state.pre_vote_enabled => PreVoteEnabled \in preVoteStatus[logline.msg.state.node_id])
393393

394394
IsRcvRequestVoteResponse ==
395-
/\ IsEvent("recv_request_vote_response")
395+
/\ \/ IsEvent("recv_request_vote_response")
396+
\/ IsEvent("recv_request_pre_vote_response")
396397
/\ LET i == logline.msg.state.node_id
397398
j == logline.msg.from_node_id
398399
IN \E m \in Network!MessagesTo(i, j):

0 commit comments

Comments
 (0)