Skip to content

Commit

Permalink
fix: add change_peer_mutex_ in PRaft::AddPeer and `PRaft::RemoveP…
Browse files Browse the repository at this point in the history
…eer`

issue: #30
issue: #280

Signed-off-by: HappyUncle <[email protected]>
  • Loading branch information
happy-v587 committed Oct 19, 2024
1 parent 42690cc commit 969eae4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/praft/praft.cc
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,12 @@ int PRaft::ProcessClusterRemoveCmdResponse(PClient* client, const char* start, i
}

butil::Status PRaft::AddPeer(const std::string& peer) {
std::unique_lock<std::mutex> lck(add_peer_mutex_);

if (!node_) {
ERROR_LOG_AND_STATUS("Node is not initialized");
return ERROR_LOG_AND_STATUS("Node is not initialized");
}

std::unique_lock<std::mutex> lck(change_peer_mutex_);

braft::SynchronizedClosure done;
node_->add_peer(peer, &done);
done.wait();
Expand All @@ -555,6 +555,8 @@ butil::Status PRaft::RemovePeer(const std::string& peer) {
return ERROR_LOG_AND_STATUS("Node is not initialized");
}

std::unique_lock<std::mutex> lck(change_peer_mutex_);

braft::SynchronizedClosure done;
node_->remove_peer(peer, &done);
done.wait();
Expand Down
2 changes: 1 addition & 1 deletion src/praft/praft.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class PRaft : public braft::StateMachine {
bool is_node_first_start_up_ = true;

// Concurrency `raft.cluster join` will throw an error, so use a mutex for restriction.
std::mutex add_peer_mutex_;
std::mutex change_peer_mutex_;
};

} // namespace kiwi

0 comments on commit 969eae4

Please sign in to comment.