diff --git a/conanfile.py b/conanfile.py index 469636f..ada1ebf 100644 --- a/conanfile.py +++ b/conanfile.py @@ -90,7 +90,7 @@ def build(self): def package(self): lib_dir = join(self.package_folder, "lib") copy(self, "LICENSE", self.source_folder, join(self.package_folder, "licenses"), keep_path=False) - copy(self, "*.h*", join(self.source_folder, "src", "include"), join(self.package_folder, "include", "nuraft_mesg"), keep_path=True) + copy(self, "*.h*", join(self.source_folder, "src", "include"), join(self.package_folder, "include"), keep_path=True) copy(self, "*.lib", self.build_folder, lib_dir, keep_path=False) copy(self, "*.a", self.build_folder, lib_dir, keep_path=False) copy(self, "*.so*", self.build_folder, lib_dir, keep_path=False) diff --git a/src/lib/service.cpp b/src/lib/service.cpp index a13be15..e21fc83 100644 --- a/src/lib/service.cpp +++ b/src/lib/service.cpp @@ -230,7 +230,7 @@ bool msg_service::raftStep(const sisl::AsyncRpcDataPtr< Messaging, RaftGroupMsg, } else { LOGDEBUGMOD(nuraft_mesg, "Missing RAFT group: {}", group_name); } - rpc_data->set_status(::grpc::Status(::grpc::NOT_FOUND, "Missing RAFT group")); + rpc_data->set_status(::grpc::Status(::grpc::NOT_FOUND, fmt::format("Missing RAFT group {}", group_name))); return true; } diff --git a/test_package/example_client.cpp b/test_package/example_client.cpp index daef259..83bde8a 100644 --- a/test_package/example_client.cpp +++ b/test_package/example_client.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -11,7 +12,7 @@ SISL_OPTION_GROUP(client, (add, "a", "add", "Add a server to the cluster", cxxopts::value< uint32_t >(), "id"), (clean, "", "clean", "Reset all persistence", cxxopts::value< bool >(), ""), - (group, "g", "group", "Group ID", cxxopts::value< std::string >(), "id"), + (group, "g", "group", "Group ID", cxxopts::value< uint32_t >(), ""), (server, "", "server", "Server to send message to", cxxopts::value< uint32_t >()->default_value("0"), "id"), (echo, "m", "echo", "Send message to echo service", cxxopts::value< std::string >(), "message"), @@ -26,21 +27,23 @@ using nuraft_mesg::mesg_factory; using namespace nuraft; struct example_factory : public nuraft_mesg::group_factory { - example_factory(int const threads, std::string const& name) : + example_factory(int const threads, nuraft_mesg::group_id_t const& name) : nuraft_mesg::group_factory::group_factory(threads, name, nullptr) {} - std::string lookupEndpoint(std::string const& client) override { + std::string lookupEndpoint(nuraft_mesg::peer_id_t const& client) override { + auto id_str = to_string(client); for (auto i = 0u; i < 5; ++i) { - if (uuids[i] == client) { return fmt::format(FMT_STRING("127.0.0.1:{}"), 9000 + i); } + if (uuids[i] == id_str) { return fmt::format(FMT_STRING("127.0.0.1:{}"), 9000 + i); } } - return client; + RELEASE_ASSERT(false, "Missing Peer: {}", client); + return std::string(); } }; -int send_message(uint32_t leader_id, std::string const& group_id, std::string const& message) { +int send_message(uint32_t leader_id, nuraft_mesg::group_id_t const& group_id, std::string const& message) { auto g_factory = std::make_shared< example_factory >(2, group_id); auto factory = std::make_shared< mesg_factory >(g_factory, group_id, "test_package"); - auto const dest_cfg = srv_config(leader_id, uuids[stol(group_id) + leader_id]); + auto const dest_cfg = srv_config(leader_id, uuids[leader_id]); auto buf = buffer::alloc(message.length() + 1); buf->put(message.c_str()); @@ -58,10 +61,10 @@ int send_message(uint32_t leader_id, std::string const& group_id, std::string co return ret; } -int add_new_server(uint32_t leader_id, uint32_t srv_id, std::string const& group_id) { +int add_new_server(uint32_t leader_id, uint32_t srv_id, nuraft_mesg::group_id_t const& group_id) { auto g_factory = std::make_shared< example_factory >(2, group_id); auto factory = std::make_shared< mesg_factory >(g_factory, group_id, "test_package"); - auto const dest_cfg = srv_config(leader_id, uuids[stol(group_id) + leader_id]); + auto const dest_cfg = srv_config(leader_id, uuids[leader_id]); nuraft::cmd_result_code rc = nuraft::SERVER_IS_JOINING; while (nuraft::SERVER_IS_JOINING == rc || nuraft::CONFIG_CHANGING == rc) { @@ -75,10 +78,10 @@ int add_new_server(uint32_t leader_id, uint32_t srv_id, std::string const& group return ret; } -int remove_server(uint32_t leader_id, std::string const& group_id, uint32_t srv_id) { +int remove_server(uint32_t leader_id, nuraft_mesg::group_id_t const& group_id, uint32_t srv_id) { auto g_factory = std::make_shared< example_factory >(2, group_id); auto factory = std::make_shared< mesg_factory >(g_factory, group_id, "test_package"); - auto const dest_cfg = srv_config(leader_id, uuids[stol(group_id) + leader_id]); + auto const dest_cfg = srv_config(leader_id, uuids[leader_id]); nuraft::cmd_result_code rc = nuraft::SERVER_IS_JOINING; while (nuraft::SERVER_IS_JOINING == rc || nuraft::CONFIG_CHANGING == rc) { @@ -107,15 +110,22 @@ int main(int argc, char** argv) { return 0; } - auto const group_id = SISL_OPTIONS["group"].as< std::string >(); + auto guid_str = guids[SISL_OPTIONS["group"].as< uint32_t >()]; + auto gid = boost::uuids::uuid(); + try { + gid = boost::uuids::string_generator()(guid_str); + } catch (std::runtime_error const&) { + LOGCRITICAL("Invalid uuid: {}", guid_str); + return -1; + } auto const server_id = SISL_OPTIONS["server"].as< uint32_t >(); if (SISL_OPTIONS.count("echo")) { - return send_message(server_id, group_id, SISL_OPTIONS["echo"].as< std::string >()); + return send_message(server_id, gid, SISL_OPTIONS["echo"].as< std::string >()); } else if (SISL_OPTIONS.count("add")) { - return add_new_server(server_id, SISL_OPTIONS["add"].as< uint32_t >(), group_id); + return add_new_server(server_id, SISL_OPTIONS["add"].as< uint32_t >(), gid); } else if (SISL_OPTIONS.count("remove")) { - return remove_server(server_id, group_id, SISL_OPTIONS["remove"].as< uint32_t >()); + return remove_server(server_id, gid, SISL_OPTIONS["remove"].as< uint32_t >()); } else { std::cout << SISL_PARSER.help({}) << std::endl; } diff --git a/test_package/example_server.cpp b/test_package/example_server.cpp index 4df35a9..6888497 100644 --- a/test_package/example_server.cpp +++ b/test_package/example_server.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -12,7 +13,7 @@ #include "uuids.h" SISL_OPTION_GROUP(server, (server_id, "", "server_id", "Servers ID (0-9)", cxxopts::value< uint32_t >(), ""), - (start_group, "", "create", "Group Name to create initialy", cxxopts::value< std::string >(), "")) + (start_group, "", "create", "Group to create", cxxopts::value< uint32_t >(), "")) SISL_OPTIONS_ENABLE(logging, server, nuraft_mesg) SISL_LOGGING_INIT(nuraft, nuraft_mesg, grpc_server, flip) @@ -47,30 +48,28 @@ void handle(int signal) { class Application : public nuraft_mesg::MessagingApplication, public std::enable_shared_from_this< Application > { public: - std::string name_; uint32_t port_; - std::string id_; + nuraft_mesg::peer_id_t id_; std::shared_ptr< nuraft_mesg::Manager > manager_; - Application(std::string const& name, uint32_t port) : name_(name), port_(port) { id_ = name; } + Application(nuraft_mesg::peer_id_t const& name, uint32_t port) : port_(port) { id_ = name; } ~Application() override = default; - std::string lookup_peer(std::string const& peer) override { + std::string lookup_peer(nuraft_mesg::peer_id_t const& peer) override { // Provide a method for the service layer to lookup an IPv4:port address // from a uuid; however the process wants to do that. + auto id_str = to_string(peer); for (auto i = 0u; i < 5; ++i) { - if (uuids[i] == peer) { return fmt::format(FMT_STRING("127.0.0.1:{}"), 9000 + i); } + if (uuids[i] == id_str) { return fmt::format(FMT_STRING("127.0.0.1:{}"), 9000 + i); } } RELEASE_ASSERT(false, "Missing Peer: {}", peer); + return std::string(); } std::shared_ptr< nuraft_mesg::mesg_state_mgr > create_state_mgr(int32_t const srv_id, - std::string const& group_id) override { - // Each group has a type so we can attach different state_machines upon Join request. - // This callback should provide a mechanism to return a new state_manager. - auto [it, _] = state_mgr_map.emplace( - std::make_pair(group_id + "_" + name_, std::make_shared< simple_state_mgr >(srv_id, id_, group_id))); - return std::static_pointer_cast< nuraft_mesg::mesg_state_mgr >(it->second); + nuraft_mesg::group_id_t const& group_id) override { + return std::static_pointer_cast< nuraft_mesg::mesg_state_mgr >( + std::make_shared< simple_state_mgr >(srv_id, id_, group_id)); } void start() { @@ -89,9 +88,6 @@ class Application : public nuraft_mesg::MessagingApplication, public std::enable .with_snapshot_enabled(0); manager_->register_mgr_type(params.default_group_type_, r_params); } - -private: - std::map< std::string, std::shared_ptr< simple_state_mgr > > state_mgr_map; }; int main(int argc, char** argv) { @@ -101,7 +97,7 @@ int main(int argc, char** argv) { // defined in uuids.h from the CLI without having to iterate // and store multiple maps in the code and test script. auto const offset_id = SISL_OPTIONS["server_id"].as< uint32_t >(); - auto const server_uuid = uuids[offset_id]; + auto const server_uuid = boost::uuids::string_generator()(uuids[offset_id]); // Can start using LOG from this point onward. sisl::logging::SetLogger(fmt::format(FMT_STRING("server_{}"), offset_id)); @@ -123,7 +119,8 @@ int main(int argc, char** argv) { // Create a new group with ourself as the only member if (0 < SISL_OPTIONS.count("create")) { - app->manager_->create_group(SISL_OPTIONS["create"].as< std::string >(), "test_package"); + auto gid = boost::uuids::string_generator()(guids[SISL_OPTIONS["create"].as< uint32_t >()]); + app->manager_->create_group(gid, "test_package"); } // Just prevent main() from exiting, require a SIGNAL diff --git a/test_package/example_state_manager.cpp b/test_package/example_state_manager.cpp index 9ecf587..942874f 100644 --- a/test_package/example_state_manager.cpp +++ b/test_package/example_state_manager.cpp @@ -74,8 +74,9 @@ nuraft::ptr< nuraft::cluster_config > fromClusterConfig(json const& cluster_conf return raft_config; } -simple_state_mgr::simple_state_mgr(int32_t srv_id, std::string const& srv_addr, std::string const& group_id) : - nuraft_mesg::mesg_state_mgr(), _srv_id(srv_id), _srv_addr(srv_addr), _group_id(group_id.c_str()) {} +simple_state_mgr::simple_state_mgr(int32_t srv_id, nuraft_mesg::peer_id_t const& srv_addr, + nuraft_mesg::group_id_t const& group_id) : + nuraft_mesg::mesg_state_mgr(), _srv_id(srv_id), _srv_addr(to_string(srv_addr)), _group_id(to_string(group_id)) {} nuraft::ptr< nuraft::cluster_config > simple_state_mgr::load_config() { LOGDEBUG("Loading config for [{}]", _group_id); @@ -129,7 +130,7 @@ void simple_state_mgr::save_state(const nuraft::srv_state& state) { uint32_t simple_state_mgr::get_logstore_id() const { return 0; } std::shared_ptr< nuraft::state_machine > simple_state_mgr::get_state_machine() { - return std::make_shared(); + return std::make_shared< echo_state_machine >(); } void simple_state_mgr::permanent_destroy() {} diff --git a/test_package/example_state_manager.h b/test_package/example_state_manager.h index 4d005cb..874fbcd 100644 --- a/test_package/example_state_manager.h +++ b/test_package/example_state_manager.h @@ -5,7 +5,7 @@ class simple_state_mgr : public nuraft_mesg::mesg_state_mgr { public: - simple_state_mgr(int32_t srv_id, std::string const& srv_addr, std::string const& group_id); + simple_state_mgr(int32_t srv_id, nuraft_mesg::peer_id_t const& srv_addr, nuraft_mesg::group_id_t const& group_id); nuraft::ptr< nuraft::cluster_config > load_config() override; void save_config(const nuraft::cluster_config& config) override; diff --git a/test_package/uuids.h b/test_package/uuids.h index 2befcd9..ef24dbe 100644 --- a/test_package/uuids.h +++ b/test_package/uuids.h @@ -5,3 +5,9 @@ std::string const uuids[] = {"f0d3ec17-9075-429b-afa7-68d7542f7403", "d22e63a2-4993-4f0e-b19d-bfa5ace64e87", "d68a6947-b976-4e78-9cf0-8cc6520ea266", "2da54edb-8e1d-4fc6-9ac3-4eade3aac0b4"}; + +std::string const guids[] = {"f0d3ec17-9075-429b-afa7-68d7542f7404", + "aa2018b3-2556-4d8e-a575-0a90d360b0bc", + "d22e63a2-4993-4f0e-b19d-bfa5ace64e88", + "d68a6947-b976-4e78-9cf0-8cc6520ea267", + "2da54edb-8e1d-4fc6-9ac3-4eade3aac0b5"};