Skip to content

Commit

Permalink
Exposing Leader to upper layer
Browse files Browse the repository at this point in the history
SM needs to return leader in both response of some API call
as well as PGStat report.

This helps client(of SM)  to know up-to-date leader and send
requests to leader.

Signed-off-by: Xiaoxi Chen <[email protected]>
  • Loading branch information
xiaoxichen committed Jan 19, 2024
1 parent 6abaabb commit 7c708e5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class NuRaftMesgConan(ConanFile):
name = "nuraft_mesg"
version = "2.1.2"
version = "2.2.2"

homepage = "https://github.com/eBay/nuraft_mesg"
description = "A gRPC service for NuRAFT"
Expand Down
1 change: 1 addition & 0 deletions include/nuraft_mesg/mesg_state_mgr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class repl_service_ctx {
// we do not own this pointer. Use this only if the life cycle of the pointer is well known
nuraft::raft_server* _server;
bool is_raft_leader() const;
const std::string& raft_leader_id() const;

// return a list of replica configs for the peers of the raft group
void get_cluster_config(std::list< replica_config >& cluster_config) const;
Expand Down
12 changes: 12 additions & 0 deletions src/lib/repl_service_ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ repl_service_ctx::repl_service_ctx(nuraft::raft_server* server) : _server(server

bool repl_service_ctx::is_raft_leader() const { return _server->is_leader(); }

const std::string& repl_service_ctx::raft_leader_id() const {
// when adding member to raft, the id recorded in raft is a hash
// of passed-in id (new_id in add_member()), the new_id was stored
// in endpoint field.
static std::string const empty;
if (!_server) return empty;
auto const leader = _server->get_leader();
if (leader == -1) return empty;
auto const& srv_config = _server->get_srv_config(leader);
return (srv_config) ? srv_config->get_endpoint() : empty;
}

void repl_service_ctx::get_cluster_config(std::list< replica_config >& cluster_config) const {
auto const& srv_configs = _server->get_config()->get_servers();
for (auto const& srv_config : srv_configs) {
Expand Down
4 changes: 4 additions & 0 deletions src/tests/test_fixture.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ protected:
auto const dest_cfg = nuraft::srv_config(to_server_id(app_1_->id_), to_string(app_1_->id_));
EXPECT_TRUE(factory->add_server(to_server_id(app_3_->id_), app_3_->id_, dest_cfg).get());
std::this_thread::sleep_for(std::chrono::seconds(1));

// Check leader
EXPECT_TRUE(app_1_->state_mgr_map_[group_id_]->get_repl_context()->is_raft_leader());
EXPECT_TRUE(app_1_->state_mgr_map_[group_id_]->get_repl_context()->raft_leader_id() == app_1_->id_);
}
};

Expand Down

0 comments on commit 7c708e5

Please sign in to comment.