Skip to content

Commit

Permalink
Merge pull request #79 from ClickHouse/fix-small-race-ch
Browse files Browse the repository at this point in the history
Fix race when removing a node
  • Loading branch information
antonio2368 authored Nov 19, 2024
2 parents 0a765ec + a8314d9 commit b4a3aec
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/libnuraft/raft_server.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,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
4 changes: 2 additions & 2 deletions src/handle_client_request.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ ptr<resp_msg> raft_server::handle_leader_status_req(req_msg& req) {
case raft_params::dual_mutex:
default: {
// TODO: Use RW lock here.
auto_lock(cli_lock_);
recur_lock(cli_lock_);
return get_leader_status();
}
}
Expand All @@ -89,7 +89,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 @@ -1030,7 +1030,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 @@ -1403,7 +1403,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 @@ -1460,7 +1460,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 @@ -1778,6 +1778,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

0 comments on commit b4a3aec

Please sign in to comment.