Skip to content

Commit

Permalink
fix: add mutex to support concurrency raft.cluster join (#52)
Browse files Browse the repository at this point in the history
* fix: add mutex to support concurrency `raft.cluster join`

#25

Signed-off-by: HappyUncle <code4happy@gmail.com>

* fix: add `change_peer_mutex_` in `PRaft::AddPeer` and `PRaft::RemovePeer`

Signed-off-by: HappyUncle <code4happy@gmail.com>

---------

Signed-off-by: HappyUncle <code4happy@gmail.com>
  • Loading branch information
happy-v587 authored Nov 9, 2024
1 parent 457b09b commit f4ccc01
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/praft/praft.cc
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,11 @@ int PRaft::ProcessClusterRemoveCmdResponse(PClient* client, const char* start, i

butil::Status PRaft::AddPeer(const std::string& peer) {
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 @@ -553,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
3 changes: 3 additions & 0 deletions src/praft/praft.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ class PRaft : public braft::StateMachine {
int db_id_ = 0; // db_id

bool is_node_first_start_up_ = true;

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

} // namespace kiwi

0 comments on commit f4ccc01

Please sign in to comment.