1 #include "server_group.hpp"
8 #include <system_error>
11 #include <sys/types.h>
13 #include "classpath.hpp"
14 #include "configuration.hpp"
41 _servers = std::move(src._servers);
42 _conn_string = std::move(src._conn_string);
46 static void create_directory(
const std::string& path)
48 if (::mkdir(path.c_str(), 0755))
50 throw std::system_error(errno, std::system_category());
54 static void save_id_file(
const std::string& path,
const server_id&
id)
56 std::ofstream ofs(path.c_str());
58 throw std::runtime_error(
"IO failure");
65 auto base_settings = base_settings_in;
67 if (!base_settings.data_directory())
68 throw std::invalid_argument(
"Settings must specify a base directory");
72 : base_settings.client_port();
75 for (std::size_t idx = 0U; idx <
size; ++idx)
78 auto px = std::make_shared<info>(base_settings);
80 x.name = std::to_string(
id.value);
81 x.path = base_directory +
"/" + x.name;
83 .client_port(base_port++)
84 .data_directory(x.path +
"/data")
86 x.peer_port = base_port++;
87 x.leader_port = base_port++;
89 out._servers.emplace(
id, px);
92 std::ostringstream conn_str_os;
93 conn_str_os <<
"zk://";
96 create_directory(base_directory);
97 for (
auto& [
id, srvr] : out._servers)
99 for (
const auto& [id2, srvr2] : out._servers)
101 srvr->settings.add_server(id2,
"127.0.0.1", srvr2->peer_port, srvr2->leader_port);
104 create_directory(srvr->path);
105 create_directory(srvr->path +
"/data");
106 save_id_file(srvr->path +
"/data/myid",
id);
107 srvr->settings.save_file(srvr->path +
"/settings.cfg");
109 if (!std::exchange(first,
false))
111 conn_str_os <<
"127.0.0.1:" << srvr->settings.client_port();
114 out._conn_string = conn_str_os.str();
126 for (
auto& [name, srvr] : _servers)
128 static_cast<void>(name);
132 srvr->instance = std::make_shared<server>(packages, srvr->settings);
configuration settings
Settings for this server.
std::string name
Name of this server (string version of its ID)
Represents the ID of a server in the ensemble.
Create and manage a group of server instances on this local machine (most likely in a single ensemble...
Represents a collection of JARs or other Java entities that should be provided as the --classpath to ...
const optional< std::string > & data_directory() const
const std::string & get_connection_string()
Get a connection string which can connect to any the servers in the group.
std::uint16_t peer_port
settings.peer_port
static const std::uint16_t default_client_port
The default value for client_port.
std::shared_ptr< server > instance
Instance(if this is running)
void start_all_servers(const classpath &packages)
Start all servers in the group.
std::string path
settings.data_directory
std::size_t size() const
How many servers are in this group?
Represents a configuration which should be run by server instance.
server_group() noexcept
Create an empty server group.
std::uint16_t leader_port
settings.leader_port
static server_group make_ensemble(std::size_t size, const configuration &base_settings)
Create an ensemble of the given size.