From 6c84092023b394adbf174c13773d502c28891fe7 Mon Sep 17 00:00:00 2001 From: Matej Ferencevic Date: Thu, 16 May 2019 14:23:55 +0200 Subject: [PATCH] Add `GetLeaderId` to `HAClient` Reviewers: msantl, dlozic, ipaljak Reviewed By: msantl, dlozic, ipaljak Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D2059 --- src/communication/bolt/ha_client.hpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/communication/bolt/ha_client.hpp b/src/communication/bolt/ha_client.hpp index d5f94e1f3..97ee193bf 100644 --- a/src/communication/bolt/ha_client.hpp +++ b/src/communication/bolt/ha_client.hpp @@ -1,6 +1,8 @@ #pragma once +#include #include +#include #include #include @@ -89,6 +91,15 @@ class HAClient final { num_retries_); } + /// Function that returns the current leader ID. + /// + /// @throws ClientFatalException when we couldn't find the leader server even + /// after `num_retries` tries + uint64_t GetLeaderId() { + Execute("SHOW RAFT INFO", {}); + return leader_id_; + } + private: void FindLeader() { // Reconnect clients that aren't available @@ -118,8 +129,10 @@ class HAClient final { // Determine which server is the leader leader_ = nullptr; - int64_t leader_id = -1; - for (const auto &client : clients_) { + uint64_t leader_id = 0; + int64_t leader_term = -1; + for (uint64_t i = 0; i < clients_.size(); ++i) { + auto &client = clients_[i]; try { auto ret = client->Execute("SHOW RAFT INFO", {}); int64_t term_id = -1; @@ -138,14 +151,16 @@ class HAClient final { continue; } } - if (is_leader && term_id > leader_id) { - leader_id = term_id; + if (is_leader && term_id > leader_term) { + leader_term = term_id; + leader_id = i + 1; leader_ = client.get(); } } catch (const utils::BasicException &) { continue; } } + leader_id_ = leader_id; if (!leader_) { throw ClientFatalException("Couldn't find leader server!"); } @@ -159,6 +174,7 @@ class HAClient final { std::chrono::milliseconds retry_delay_; std::string client_name_; + uint64_t leader_id_ = 0; Client *leader_ = nullptr; std::vector> clients_; };