Skip to content

Commit

Permalink
Add get_raft_status() API
Browse files Browse the repository at this point in the history
This API will returns Peer_info struct for all members including leader.

The main purpose of this API is for HO->SM->CM report so SM/CM can decide
which peers are considered up-to-date.

Signed-off-by: Xiaoxi Chen <[email protected]>
  • Loading branch information
xiaoxichen committed Jan 23, 2024
1 parent f4e3d74 commit 2792b13
Show file tree
Hide file tree
Showing 3 changed files with 34 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.2.2"
version = "2.3.2"

homepage = "https://github.com/eBay/nuraft_mesg"
description = "A gRPC service for NuRAFT"
Expand Down
10 changes: 10 additions & 0 deletions include/nuraft_mesg/mesg_state_mgr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ struct replica_config {
std::string aux;
};

struct peer_info {
// Peer ID.
std::string id_;
// The last log index that the peer has, from this server's point of view.
ulong last_log_idx_;
// The elapsed time since the last successful response from this peer, set to 0 on leader
ulong last_succ_resp_us_;
};

class repl_service_ctx {
public:
repl_service_ctx(nuraft::raft_server* server);
Expand All @@ -37,6 +46,7 @@ class repl_service_ctx {
nuraft::raft_server* _server;
bool is_raft_leader() const;
const std::string& raft_leader_id() const;
std::vector< peer_info >get_raft_status() 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
23 changes: 23 additions & 0 deletions src/lib/repl_service_ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ const std::string& repl_service_ctx::raft_leader_id() const {
return empty;
}

std::vector< peer_info > repl_service_ctx::get_raft_status() const {
std::vector< peer_info > peers;
if (!is_raft_leader()) return peers;
if (!_server) return peers;

auto pinfo_all = _server->get_peer_info_all();
// add leader to the list
nuraft::raft_server::peer_info pi_leader;
pi_leader.id_ = _server->get_id();
pi_leader.last_log_idx_ = _server->get_last_log_idx();
pinfo_all.emplace_back(pi_leader);

for (auto const& pinfo : pinfo_all) {
std::string_view peer_id;
if (auto srv_config = _server->get_srv_config(pinfo.id_); nullptr != srv_config) {
peer_id = srv_config->get_endpoint();
}
DEBUG_ASSERT(!peer_id.empty(), "Unknown peer in config");
peers.emplace_back(peer_info{std::string(peer_id), pinfo.last_log_idx_, pinfo.last_succ_resp_us_});
}
return peers;
}

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

0 comments on commit 2792b13

Please sign in to comment.