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 race on precommit when cluster is changed #554

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/libnuraft/raft_server.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ protected:
/**
* Lock of handling client request and role change.
*/
std::mutex cli_lock_;
std::recursive_mutex cli_lock_;

/**
* Condition variable to invoke BG commit thread.
Expand Down
2 changes: 1 addition & 1 deletion src/handle_client_request.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ptr<resp_msg> raft_server::handle_cli_req_prelock(req_msg& req,
case raft_params::dual_mutex:
default: {
// TODO: Use RW lock here.
auto_lock(cli_lock_);
recur_lock(cli_lock_);
resp = handle_cli_req(req, ext_params, timestamp_us);
break;
}
Expand Down
7 changes: 4 additions & 3 deletions src/raft_server.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ void raft_server::become_leader() {
}

ptr<raft_params> params = ctx_->get_params();
{ auto_lock(cli_lock_);
{ recur_lock(cli_lock_);
role_ = srv_role::leader;
leader_ = id_;
srv_to_join_.reset();
Expand Down Expand Up @@ -1389,7 +1389,7 @@ bool raft_server::request_leadership() {
void raft_server::become_follower() {
// stop hb for all peers
p_in("[BECOME FOLLOWER] term %" PRIu64 "", state_->get_term());
{ std::lock_guard<std::mutex> ll(cli_lock_);
{ std::lock_guard<std::recursive_mutex> ll(cli_lock_);
for (peer_itor it = peers_.begin(); it != peers_.end(); ++it) {
it->second->enable_hb(false);
}
Expand Down Expand Up @@ -1446,7 +1446,7 @@ bool raft_server::update_term(ulong term) {
//
// To avoid this issue, we acquire `cli_lock_`,
// and change `role_` first before setting the term.
std::lock_guard<std::mutex> ll(cli_lock_);
std::lock_guard<std::recursive_mutex> ll(cli_lock_);
role_ = srv_role::follower;
state_->set_term(term);
}
Expand Down Expand Up @@ -1764,6 +1764,7 @@ ulong raft_server::store_log_entry(ptr<log_entry>& entry, ulong index) {
}

if ( role_ == srv_role::leader ) {
recur_lock(cli_lock_);
// Need to progress precommit index for config.
try_update_precommit_index(log_index);
}
Expand Down
Loading