From 11ccd8a6dd9e9b738be98076396b1f38f4db30f5 Mon Sep 17 00:00:00 2001 From: lzydmxy <13126752315@163.com> Date: Thu, 16 Nov 2023 15:27:17 +0800 Subject: [PATCH 1/2] add cluster_config_lock_ --- src/Service/NuRaftStateManager.cpp | 30 ++++++++++++++++++++++-------- src/Service/NuRaftStateManager.h | 10 +++++++++- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/Service/NuRaftStateManager.cpp b/src/Service/NuRaftStateManager.cpp index 535ff2da44..79e8e81099 100644 --- a/src/Service/NuRaftStateManager.cpp +++ b/src/Service/NuRaftStateManager.cpp @@ -24,12 +24,26 @@ NuRaftStateManager::NuRaftStateManager(int id_, const Poco::Util::AbstractConfig cur_cluster_config = parseClusterConfig(config_, "keeper.cluster"); } +ptr NuRaftStateManager::get_cluster_config() const +{ + std::lock_guard l(cluster_config_lock_); + ptr ret = cur_cluster_config; + return ret; +} + +void NuRaftStateManager::set_cluster_config(const ptr& new_config) +{ + std::lock_guard l(cluster_config_lock_); + cur_cluster_config = new_config; + LOG_INFO(log, "set config with log index {}", cur_cluster_config->get_log_idx()); +} + ptr NuRaftStateManager::load_config() { if (!Poco::File(cluster_config_file).exists()) { LOG_INFO(log, "load config with initial cluster config."); - return cur_cluster_config; + return get_cluster_config(); } std::unique_ptr read_file_buf = std::make_unique(cluster_config_file, 4096); @@ -37,9 +51,9 @@ ptr NuRaftStateManager::load_config() readVarUInt(size, *read_file_buf); ptr buf = nuraft::buffer::alloc(size); read_file_buf->readStrict(reinterpret_cast(buf->data()), size); - cur_cluster_config = nuraft::cluster_config::deserialize(*buf); - LOG_INFO(log, "load config with log index {}", cur_cluster_config->get_log_idx()); - return cur_cluster_config; + auto new_cluster_config = nuraft::cluster_config::deserialize(*buf); + set_cluster_config(new_cluster_config); + return new_cluster_config; } void NuRaftStateManager::save_config(const cluster_config & config) @@ -51,8 +65,7 @@ void NuRaftStateManager::save_config(const cluster_config & config) out_file_buf->write(reinterpret_cast(data->data()), data->size()); out_file_buf->finalize(); out_file_buf->sync(); - LOG_INFO(log, "save config with log index {}", config.get_log_idx()); - cur_cluster_config = cluster_config::deserialize(*data); + set_cluster_config(cluster_config::deserialize(*data)); } void NuRaftStateManager::save_state(const srv_state & state) @@ -144,13 +157,14 @@ ptr NuRaftStateManager::parseClusterConfig( ConfigUpdateActions NuRaftStateManager::getConfigurationDiff(const Poco::Util::AbstractConfiguration & config) { auto new_cluster_config = parseClusterConfig(config, "keeper.cluster"); + auto old_cluster_config = get_cluster_config(); std::unordered_map new_ids, old_ids; for (const auto & new_server : new_cluster_config->get_servers()) new_ids[new_server->get_id()] = new_server; { - for (const auto & old_server : cur_cluster_config->get_servers()) + for (const auto & old_server : old_cluster_config->get_servers()) old_ids[old_server->get_id()] = old_server; } @@ -172,7 +186,7 @@ ConfigUpdateActions NuRaftStateManager::getConfigurationDiff(const Poco::Util::A { /// And update priority if required - for (const auto & old_server : cur_cluster_config->get_servers()) + for (const auto & old_server : old_cluster_config->get_servers()) { for (const auto & new_server : new_cluster_config->get_servers()) { diff --git a/src/Service/NuRaftStateManager.h b/src/Service/NuRaftStateManager.h index dd026ed22c..7be82db490 100644 --- a/src/Service/NuRaftStateManager.h +++ b/src/Service/NuRaftStateManager.h @@ -66,7 +66,9 @@ class NuRaftStateManager : public nuraft::state_mgr //ptr get_srv_config() const { return curr_srv_config; } - ptr get_cluster_config() const { return cur_cluster_config; } + ptr get_cluster_config() const; + + void set_cluster_config(const ptr& new_config); /// Get configuration diff between proposed XML and current state in RAFT ConfigUpdateActions getConfigurationDiff(const Poco::Util::AbstractConfiguration & config); @@ -88,6 +90,12 @@ class NuRaftStateManager : public nuraft::state_mgr ptr cur_cluster_config; + /** + * Lock for cluster config. + */ + mutable std::mutex cluster_config_lock_; + + protected: Poco::Logger * log; String srv_state_file; From fd04a4aec9498dd6a0ef4a347b951ba7bb888b20 Mon Sep 17 00:00:00 2001 From: lzydmxy <13126752315@163.com> Date: Fri, 17 Nov 2023 16:24:10 +0800 Subject: [PATCH 2/2] fix code style --- src/Service/KeeperServer.cpp | 2 +- src/Service/NuRaftStateManager.cpp | 20 +++++++++++--------- src/Service/NuRaftStateManager.h | 6 +++--- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/Service/KeeperServer.cpp b/src/Service/KeeperServer.cpp index 1d5b61793d..bd4374e42f 100644 --- a/src/Service/KeeperServer.cpp +++ b/src/Service/KeeperServer.cpp @@ -332,7 +332,7 @@ bool KeeperServer::isLeader() const bool KeeperServer::isObserver() const { - auto cluster_config = state_manager->get_cluster_config(); + auto cluster_config = state_manager->getClusterConfig(); return cluster_config->get_server(server_id)->is_learner(); } diff --git a/src/Service/NuRaftStateManager.cpp b/src/Service/NuRaftStateManager.cpp index 79e8e81099..23f3d6a9b1 100644 --- a/src/Service/NuRaftStateManager.cpp +++ b/src/Service/NuRaftStateManager.cpp @@ -24,18 +24,17 @@ NuRaftStateManager::NuRaftStateManager(int id_, const Poco::Util::AbstractConfig cur_cluster_config = parseClusterConfig(config_, "keeper.cluster"); } -ptr NuRaftStateManager::get_cluster_config() const +ptr NuRaftStateManager::getClusterConfig() const { - std::lock_guard l(cluster_config_lock_); + std::lock_guard lock(cluster_config_mutex); ptr ret = cur_cluster_config; return ret; } -void NuRaftStateManager::set_cluster_config(const ptr& new_config) +void NuRaftStateManager::setClusterConfig(const ptr& new_config) { - std::lock_guard l(cluster_config_lock_); + std::lock_guard lock(cluster_config_mutex); cur_cluster_config = new_config; - LOG_INFO(log, "set config with log index {}", cur_cluster_config->get_log_idx()); } ptr NuRaftStateManager::load_config() @@ -43,7 +42,7 @@ ptr NuRaftStateManager::load_config() if (!Poco::File(cluster_config_file).exists()) { LOG_INFO(log, "load config with initial cluster config."); - return get_cluster_config(); + return getClusterConfig(); } std::unique_ptr read_file_buf = std::make_unique(cluster_config_file, 4096); @@ -52,7 +51,8 @@ ptr NuRaftStateManager::load_config() ptr buf = nuraft::buffer::alloc(size); read_file_buf->readStrict(reinterpret_cast(buf->data()), size); auto new_cluster_config = nuraft::cluster_config::deserialize(*buf); - set_cluster_config(new_cluster_config); + setClusterConfig(new_cluster_config); + LOG_INFO(log, "load config with log index {}", new_cluster_config->get_log_idx()); return new_cluster_config; } @@ -65,7 +65,9 @@ void NuRaftStateManager::save_config(const cluster_config & config) out_file_buf->write(reinterpret_cast(data->data()), data->size()); out_file_buf->finalize(); out_file_buf->sync(); - set_cluster_config(cluster_config::deserialize(*data)); + auto new_cluster_config = cluster_config::deserialize(*data); + setClusterConfig(new_cluster_config); + LOG_INFO(log, "save config with log index {}", new_cluster_config->get_log_idx()); } void NuRaftStateManager::save_state(const srv_state & state) @@ -157,7 +159,7 @@ ptr NuRaftStateManager::parseClusterConfig( ConfigUpdateActions NuRaftStateManager::getConfigurationDiff(const Poco::Util::AbstractConfiguration & config) { auto new_cluster_config = parseClusterConfig(config, "keeper.cluster"); - auto old_cluster_config = get_cluster_config(); + auto old_cluster_config = getClusterConfig(); std::unordered_map new_ids, old_ids; for (const auto & new_server : new_cluster_config->get_servers()) diff --git a/src/Service/NuRaftStateManager.h b/src/Service/NuRaftStateManager.h index 7be82db490..e9ae020e8b 100644 --- a/src/Service/NuRaftStateManager.h +++ b/src/Service/NuRaftStateManager.h @@ -66,9 +66,9 @@ class NuRaftStateManager : public nuraft::state_mgr //ptr get_srv_config() const { return curr_srv_config; } - ptr get_cluster_config() const; + ptr getClusterConfig() const; - void set_cluster_config(const ptr& new_config); + void setClusterConfig(const ptr& new_config); /// Get configuration diff between proposed XML and current state in RAFT ConfigUpdateActions getConfigurationDiff(const Poco::Util::AbstractConfiguration & config); @@ -93,7 +93,7 @@ class NuRaftStateManager : public nuraft::state_mgr /** * Lock for cluster config. */ - mutable std::mutex cluster_config_lock_; + mutable std::mutex cluster_config_mutex; protected: