ClickHouse/src/Coordination/InMemoryStateManager.cpp

63 lines
2.1 KiB
C++
Raw Normal View History

2021-01-13 10:32:20 +00:00
#include <Coordination/InMemoryStateManager.h>
2021-02-11 09:17:57 +00:00
#include <Common/Exception.h>
2021-01-13 10:32:20 +00:00
namespace DB
{
2021-02-11 09:17:57 +00:00
namespace ErrorCodes
{
extern const int RAFT_ERROR;
}
InMemoryStateManager::InMemoryStateManager(
int my_server_id_,
const std::string & config_prefix,
const Poco::Util::AbstractConfiguration & config)
2021-01-13 10:32:20 +00:00
: my_server_id(my_server_id_)
, log_store(nuraft::cs_new<InMemoryLogStore>())
, cluster_config(nuraft::cs_new<nuraft::cluster_config>())
{
2021-02-11 09:17:57 +00:00
Poco::Util::AbstractConfiguration::Keys keys;
config.keys(config_prefix, keys);
for (const auto & server_key : keys)
{
std::string full_prefix = config_prefix + "." + server_key;
int server_id = config.getInt(full_prefix + ".id");
std::string hostname = config.getString(full_prefix + ".hostname");
int port = config.getInt(full_prefix + ".port");
bool can_become_leader = config.getBool(full_prefix + ".can_become_leader", true);
int32_t priority = config.getInt(full_prefix + ".priority", 1);
auto endpoint = hostname + ":" + std::to_string(port);
auto peer_config = nuraft::cs_new<nuraft::srv_config>(server_id, 0, endpoint, "", !can_become_leader, priority);
if (server_id == my_server_id)
{
my_server_config = peer_config;
my_port = port;
}
cluster_config->get_servers().push_back(peer_config);
}
if (!my_server_config)
throw Exception(ErrorCodes::RAFT_ERROR, "Our server id {} not found in raft_configuration section");
2021-01-13 10:32:20 +00:00
}
void InMemoryStateManager::save_config(const nuraft::cluster_config & config)
{
// Just keep in memory in this example.
// Need to write to disk here, if want to make it durable.
nuraft::ptr<nuraft::buffer> buf = config.serialize();
cluster_config = nuraft::cluster_config::deserialize(*buf);
}
void InMemoryStateManager::save_state(const nuraft::srv_state & state)
{
// Just keep in memory in this example.
// Need to write to disk here, if want to make it durable.
nuraft::ptr<nuraft::buffer> buf = state.serialize();
server_state = nuraft::srv_state::deserialize(*buf);
}
}