Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cluster_config_lock_ to avoid data race #106

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Service/KeeperServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
32 changes: 24 additions & 8 deletions src/Service/NuRaftStateManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,36 @@ NuRaftStateManager::NuRaftStateManager(int id_, const Poco::Util::AbstractConfig
cur_cluster_config = parseClusterConfig(config_, "keeper.cluster");
}

ptr<cluster_config> NuRaftStateManager::getClusterConfig() const
{
std::lock_guard<std::mutex> lock(cluster_config_mutex);
ptr<cluster_config> ret = cur_cluster_config;
return ret;
}

void NuRaftStateManager::setClusterConfig(const ptr<cluster_config>& new_config)
{
std::lock_guard<std::mutex> lock(cluster_config_mutex);
cur_cluster_config = new_config;
}

ptr<cluster_config> NuRaftStateManager::load_config()
{
if (!Poco::File(cluster_config_file).exists())
{
LOG_INFO(log, "load config with initial cluster config.");
return cur_cluster_config;
return getClusterConfig();
}

std::unique_ptr<ReadBufferFromFile> read_file_buf = std::make_unique<ReadBufferFromFile>(cluster_config_file, 4096);
size_t size;
readVarUInt(size, *read_file_buf);
ptr<nuraft::buffer> buf = nuraft::buffer::alloc(size);
read_file_buf->readStrict(reinterpret_cast<char *>(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);
setClusterConfig(new_cluster_config);
LOG_INFO(log, "load config with log index {}", new_cluster_config->get_log_idx());
return new_cluster_config;
}

void NuRaftStateManager::save_config(const cluster_config & config)
Expand All @@ -51,8 +65,9 @@ void NuRaftStateManager::save_config(const cluster_config & config)
out_file_buf->write(reinterpret_cast<char *>(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);
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)
Expand Down Expand Up @@ -144,13 +159,14 @@ ptr<cluster_config> NuRaftStateManager::parseClusterConfig(
ConfigUpdateActions NuRaftStateManager::getConfigurationDiff(const Poco::Util::AbstractConfiguration & config)
{
auto new_cluster_config = parseClusterConfig(config, "keeper.cluster");
auto old_cluster_config = getClusterConfig();

std::unordered_map<int, KeeperServerConfigPtr> 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;
}

Expand All @@ -172,7 +188,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())
{
Expand Down
10 changes: 9 additions & 1 deletion src/Service/NuRaftStateManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ class NuRaftStateManager : public nuraft::state_mgr

//ptr<srv_config> get_srv_config() const { return curr_srv_config; }

ptr<cluster_config> get_cluster_config() const { return cur_cluster_config; }
ptr<cluster_config> getClusterConfig() const;

void setClusterConfig(const ptr<cluster_config>& new_config);

/// Get configuration diff between proposed XML and current state in RAFT
ConfigUpdateActions getConfigurationDiff(const Poco::Util::AbstractConfiguration & config);
Expand All @@ -88,6 +90,12 @@ class NuRaftStateManager : public nuraft::state_mgr

ptr<cluster_config> cur_cluster_config;

/**
* Lock for cluster config.
*/
mutable std::mutex cluster_config_mutex;


protected:
Poco::Logger * log;
String srv_state_file;
Expand Down