19 std::string config_file,
20 std::string state_file,
21 std::vector<network::endpoint_t> raft_endpoints)
23 m_config_file(std::move(config_file)),
24 m_state_file(std::move(state_file)),
25 m_log_dir(std::move(log_dir)),
26 m_raft_endpoints(std::move(raft_endpoints)) {}
30 auto buf = obj.serialize();
31 std::vector<char> char_buf(buf->size());
32 std::memcpy(char_buf.data(), buf->data_begin(), char_buf.size());
34 const auto tmp_file = filename +
".tmp";
35 std::ofstream file(tmp_file, std::ios::binary | std::ios::trunc);
38 file.write(char_buf.data(),
39 static_cast<std::streamsize
>(char_buf.size()));
43 std::filesystem::rename(tmp_file, filename);
47 auto load_object(
const std::string& filename) -> nuraft::ptr<T> {
48 std::ifstream file(filename, std::ios::binary);
53 auto file_sz = std::filesystem::file_size(filename);
54 std::vector<char> buf(file_sz);
55 if(!file.read(buf.data(),
static_cast<std::streamsize
>(file_sz))) {
59 auto nuraft_buf = nuraft::buffer::alloc(file_sz);
60 std::memcpy(nuraft_buf->data_begin(), buf.data(), nuraft_buf->size());
62 auto obj = T::deserialize(*nuraft_buf);
69 auto cluster_config = nuraft::cs_new<nuraft::cluster_config>();
70 for(
size_t i = 0; i < m_raft_endpoints.size(); i++) {
71 auto& ep = m_raft_endpoints[i];
72 auto ep_str = ep.first +
":" + std::to_string(ep.second);
74 = nuraft::cs_new<nuraft::srv_config>(i + 1, ep_str);
76 constexpr auto leader_priority = 100;
77 srv_config->set_priority(leader_priority);
79 cluster_config->get_servers().emplace_back(
80 std::move(srv_config));
82 return cluster_config;
102 auto log = nuraft::cs_new<log_store>();
103 if(!log->load(m_log_dir)) {
115 std::exit(exit_code);
state_manager(int32_t srv_id, std::string log_dir, std::string config_file, std::string state_file, std::vector< network::endpoint_t > raft_endpoints)
Constructor.
void save_state(const nuraft::srv_state &state) override
Serialize and write the given server state.
void save_config(const nuraft::cluster_config &config) override
Serialize and write the given cluster configuration.
auto read_state() -> nuraft::ptr< nuraft::srv_state > override
Read and deserialize the server state.
auto load_config() -> nuraft::ptr< nuraft::cluster_config > override
Read and deserialize the cluster configuration.
auto load_log_store() -> nuraft::ptr< nuraft::log_store > override
Load and return the log store.
void system_exit(int exit_code) override
Terminate the application with the given exit code.
auto server_id() -> int32_t override
Return the server ID.
auto load_object(const std::string &filename) -> nuraft::ptr< T >
void save_object(const T &obj, const std::string &filename)