diff --git a/conanfile.py b/conanfile.py index 8b8b57c..9e029c1 100644 --- a/conanfile.py +++ b/conanfile.py @@ -8,7 +8,7 @@ class NuRaftMesgConan(ConanFile): name = "nuraft_mesg" - version = "2.3.2" + version = "2.3.3" homepage = "https://github.com/eBay/nuraft_mesg" description = "A gRPC service for NuRAFT" diff --git a/include/nuraft_mesg/common.hpp b/include/nuraft_mesg/common.hpp index 2ff3e00..8967442 100644 --- a/include/nuraft_mesg/common.hpp +++ b/include/nuraft_mesg/common.hpp @@ -21,7 +21,7 @@ namespace nuraft_mesg { using peer_id_t = boost::uuids::uuid; using group_id_t = boost::uuids::uuid; using group_type_t = std::string; - +using svr_id_t = int32_t; using io_blob_list_t = folly::small_vector< sisl::io_blob, 4 >; template < typename T > @@ -33,7 +33,7 @@ using NullResult = Result< folly::Unit >; using NullAsyncResult = AsyncResult< folly::Unit >; ENUM(role_regex, uint8_t, LEADER, FOLLOWER, ALL, ANY); -using destination_t = std::variant< peer_id_t, role_regex >; +using destination_t = std::variant< peer_id_t, role_regex, svr_id_t >; } // namespace nuraft_mesg diff --git a/src/lib/repl_service_ctx.cpp b/src/lib/repl_service_ctx.cpp index f4a5c35..5da798b 100644 --- a/src/lib/repl_service_ctx.cpp +++ b/src/lib/repl_service_ctx.cpp @@ -19,11 +19,20 @@ const std::string repl_service_ctx_grpc::id_to_str(int32_t const id) const { const std::optional< Result< peer_id_t > > repl_service_ctx_grpc::get_peer_id(destination_t const& dest) const { if (std::holds_alternative< peer_id_t >(dest)) return std::get< peer_id_t >(dest); - if (std::holds_alternative< role_regex >(dest)) { - if (!_server) { - LOGW("server not initialized"); - return folly::makeUnexpected(nuraft::cmd_result_code::SERVER_NOT_FOUND); + + if (!_server) { + LOGW("server not initialized"); + return folly::makeUnexpected(nuraft::cmd_result_code::SERVER_NOT_FOUND); + } + + if (std::holds_alternative< svr_id_t >(dest)) { + if (auto const id_str = id_to_str(std::get< svr_id_t >(dest)); !id_str.empty()) { + return boost::uuids::string_generator()(id_str); } + return folly::makeUnexpected(nuraft::cmd_result_code::SERVER_NOT_FOUND); + } + + if (std::holds_alternative< role_regex >(dest)) { switch (std::get< role_regex >(dest)) { case role_regex::LEADER: { if (is_raft_leader()) return folly::makeUnexpected(nuraft::cmd_result_code::BAD_REQUEST); diff --git a/src/tests/data_service_tests.cpp b/src/tests/data_service_tests.cpp index 8767bf5..abfbf36 100644 --- a/src/tests/data_service_tests.cpp +++ b/src/tests/data_service_tests.cpp @@ -1,4 +1,5 @@ #include "test_fixture.ipp" +#include class DataServiceFixture : public MessagingFixtureBase { protected: @@ -93,13 +94,23 @@ TEST_F(DataServiceFixture, BasicTest1) { return folly::Unit(); })); + auto repl_ctx1 = sm1->get_repl_context(); + for (auto svr : repl_ctx1->_server->get_config()->get_servers()) { + if (svr->get_endpoint() == to_string(app_1_->id_)) continue; + LOGINFO("Sending request to server [{}]", svr->get_id()) + results.push_back(sm1->data_service_request_bidirectional(svr->get_id(), REQUEST_DATA, cli_buf) + .deferValue([](auto e) -> NullResult { + EXPECT_TRUE(e.hasValue()); + return folly::Unit(); + })); + } + folly::collectAll(results).via(folly::getGlobalCPUExecutor()).get(); // add a new member to data_service_test_group and check if repl_ctx4 sends data to newly added member auto add_3 = app_4->instance_->add_member(data_group, app_3_->id_); std::this_thread::sleep_for(std::chrono::seconds(1)); EXPECT_TRUE(std::move(add_3).get()); - auto sm3 = app_3_->state_mgr_map_[data_group]; sm4->data_service_request_unidirectional(nuraft_mesg::role_regex::ALL, SEND_DATA, cli_buf) .deferValue([](auto e) -> folly::Unit { EXPECT_TRUE(e.hasValue()); @@ -108,9 +119,9 @@ TEST_F(DataServiceFixture, BasicTest1) { .get(); // TODO REVIEW THIS - // test_group: 4 (1 SEND_DATA) + 1 (1 REQUEST_DATA) + 1 (SEND_DATA to a peer) = 6 + // test_group: 4 (1 SEND_DATA) + 5 (1 REQUEST_DATA) + 1 (SEND_DATA to a peer) = 10 // data_service_test_group: 1 (1 REQUEST_DATA) + 4 (1 SEND_DATA) = 5 - EXPECT_EQ(test_state_mgr::get_server_counter(), 11); + EXPECT_EQ(test_state_mgr::get_server_counter(), 15); app_5->instance_->leave_group(data_group); app_5->instance_->leave_group(group_id_); app_4->instance_->leave_group(data_group); @@ -215,6 +226,14 @@ TEST_F(DataServiceFixture, NegativeTests) { return folly::Unit(); })); + // invalid svr id + results.push_back( + sm1->data_service_request_unidirectional(-1, REQUEST_DATA, cli_buf).deferValue([](auto e) -> NullResult { + EXPECT_TRUE(e.hasError()); + EXPECT_EQ(nuraft::cmd_result_code::SERVER_NOT_FOUND, e.error()); + return folly::Unit(); + })); + // unimplemented methods results.push_back(sm1->data_service_request_bidirectional(nuraft_mesg::role_regex::ALL, REQUEST_DATA, cli_buf) .deferValue([](auto e) -> NullResult {