From 969eae4b37b2bbd39014b41a39a1bf8ffedc3229 Mon Sep 17 00:00:00 2001 From: HappyUncle Date: Sat, 19 Oct 2024 12:41:05 +0800 Subject: [PATCH] fix: add `change_peer_mutex_` in `PRaft::AddPeer` and `PRaft::RemovePeer` issue: #30 issue: #280 Signed-off-by: HappyUncle --- src/praft/praft.cc | 8 +++++--- src/praft/praft.h | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/praft/praft.cc b/src/praft/praft.cc index ae0d939..f98b2ea 100644 --- a/src/praft/praft.cc +++ b/src/praft/praft.cc @@ -532,12 +532,12 @@ int PRaft::ProcessClusterRemoveCmdResponse(PClient* client, const char* start, i } butil::Status PRaft::AddPeer(const std::string& peer) { - std::unique_lock 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 lck(change_peer_mutex_); + braft::SynchronizedClosure done; node_->add_peer(peer, &done); done.wait(); @@ -555,6 +555,8 @@ butil::Status PRaft::RemovePeer(const std::string& peer) { return ERROR_LOG_AND_STATUS("Node is not initialized"); } + std::unique_lock lck(change_peer_mutex_); + braft::SynchronizedClosure done; node_->remove_peer(peer, &done); done.wait(); diff --git a/src/praft/praft.h b/src/praft/praft.h index 72ef166..c9f7bff 100644 --- a/src/praft/praft.h +++ b/src/praft/praft.h @@ -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