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

fix(vote): corner case of transfer leader #347

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/braft/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ static inline int heartbeat_timeout(int election_timeout) {
NodeImpl::NodeImpl(const GroupId& group_id, const PeerId& peer_id)
: _state(STATE_UNINITIALIZED)
, _current_term(0)
, _disrupted_term(0)
, _group_id(group_id)
, _server_id(peer_id)
, _conf_ctx(this)
Expand Down Expand Up @@ -1764,6 +1765,7 @@ void NodeImpl::step_down(const int64_t term, bool wakeup_a_candidate,
} else if (_state == STATE_FOLLOWER) {
_pre_vote_ctx.reset(this);
} else if (_state <= STATE_TRANSFERRING) {
_disrupted_term = _current_term;
_stepdown_timer.stop();
_ballot_box->clear_pending_tasks();

Expand Down Expand Up @@ -2163,7 +2165,6 @@ int NodeImpl::handle_request_vote_request(const RequestVoteRequest* request,
_follower_lease.expire();
}

bool disrupted = false;
int64_t previous_term = _current_term;
bool rejected_by_lease = false;
do {
Expand All @@ -2183,7 +2184,7 @@ int NodeImpl::handle_request_vote_request(const RequestVoteRequest* request,
lck.lock();

// vote need ABA check after unlock&lock
if (previous_term != _current_term) {
if (previous_term != _current_term && previous_term != _disrupted_term) {
LOG(WARNING) << "node " << _group_id << ":" << _server_id
<< " raise term " << _current_term << " when get last_log_id";
break;
Expand Down Expand Up @@ -2211,7 +2212,6 @@ int NodeImpl::handle_request_vote_request(const RequestVoteRequest* request,
butil::Status status;
Yriuns marked this conversation as resolved.
Show resolved Hide resolved
status.set_error(EHIGHERTERMREQUEST, "Raft node receives higher term "
"request_vote_request.");
disrupted = (_state <= STATE_TRANSFERRING);
step_down(request->term(), false, status);
}

Expand All @@ -2236,10 +2236,15 @@ int NodeImpl::handle_request_vote_request(const RequestVoteRequest* request,
}
} while (0);

response->set_disrupted(disrupted);
bool granted = request->term() == _current_term && _voted_id == candidate_id;
response->set_granted(granted);
if (granted) {
response->set_disrupted(_disrupted_term == previous_term);
} else {
response->set_disrupted(false);
}
response->set_previous_term(previous_term);
response->set_term(_current_term);
response->set_granted(request->term() == _current_term && _voted_id == candidate_id);
response->set_rejected_by_lease(rejected_by_lease);
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/braft/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ friend class butil::RefCountedThreadSafe<NodeImpl>;

State _state;
int64_t _current_term;
int64_t _disrupted_term;
PeerId _leader_id;
PeerId _voted_id;
VoteBallotCtx _vote_ctx; // candidate vote ctx
Expand Down