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 3b6f438
Show file tree
Hide file tree
Showing 4 changed files with 22 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
8 changes: 8 additions & 0 deletions src/tests/test_fixture.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ 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
auto sm1 = app_1_->state_mgr_map_[group_id_];
EXPECT_TRUE(sm1 != NULL);

auto repl_ctx = sm1->get_repl_context();
EXPECT_TRUE(repl_ctx && repl_ctx->is_raft_leader());
EXPECT_TRUE(repl_ctx && repl_ctx->raft_leader_id() == to_string(app_1_->id_));
}
};

Expand Down

0 comments on commit 3b6f438

Please sign in to comment.