diff options
author | Arne Juul <arnej@verizonmedia.com> | 2021-06-11 13:08:49 +0000 |
---|---|---|
committer | Arne Juul <arnej@verizonmedia.com> | 2021-06-11 13:09:51 +0000 |
commit | d32a461a49354922d81e55aabddbd13cc8902c30 (patch) | |
tree | d5df32f1f2619b01e9deb980f77b218e1490db9f /configd | |
parent | e97e12fec351222ff111c066a1dd8c831e436a36 (diff) |
simplify using new PeerCheck apis
Diffstat (limited to 'configd')
-rw-r--r-- | configd/src/apps/sentinel/report-connectivity.cpp | 42 | ||||
-rw-r--r-- | configd/src/apps/sentinel/report-connectivity.h | 39 |
2 files changed, 18 insertions, 63 deletions
diff --git a/configd/src/apps/sentinel/report-connectivity.cpp b/configd/src/apps/sentinel/report-connectivity.cpp index e23db85f4e2..a98bbdc0eec 100644 --- a/configd/src/apps/sentinel/report-connectivity.cpp +++ b/configd/src/apps/sentinel/report-connectivity.cpp @@ -13,33 +13,19 @@ using namespace std::chrono_literals; namespace config::sentinel { -SinglePing::~SinglePing() = default; - -void SinglePing::returnStatus(bool ok) { - status = ok ? "ok" : "ping failed"; - LOG(debug, "peer %s [port %d] -> %s", peerName.c_str(), peerPort, status.c_str()); - parent.requestDone(); -} - -void SinglePing::startCheck(FRT_Supervisor &orb) { - check = std::make_unique<PeerCheck>(*this, peerName, peerPort, orb, 2500); -} ReportConnectivity::ReportConnectivity(FRT_RPCRequest *req, FRT_Supervisor &orb, ModelSubscriber &modelSubscriber) : _parentRequest(req), _orb(orb), - _result() + _checks() { auto cfg = modelSubscriber.getModelConfig(); if (cfg.has_value()) { auto map = Connectivity::specsFrom(cfg.value()); - LOG(debug, "making connectivity report for %zd peers", _result.size()); - _remaining = _result.size(); + LOG(debug, "making connectivity report for %zd peers", map.size()); + _remaining = map.size(); for (const auto & [ hostname, port ] : map) { - _result.emplace_back(*this, hostname, port); - } - for (auto & peer : _result) { - peer.startCheck(_orb); + _checks.emplace_back(std::make_unique<PeerCheck>(*this, hostname, port, _orb, 2500)); } } else { _parentRequest->SetError(FRTE_RPC_METHOD_FAILED, "failed getting model config"); @@ -49,23 +35,19 @@ ReportConnectivity::ReportConnectivity(FRT_RPCRequest *req, FRT_Supervisor &orb, ReportConnectivity::~ReportConnectivity() = default; -void ReportConnectivity::requestDone() { - { - std::lock_guard<std::mutex> guard(_lock); - if (--_remaining != 0) { - return; - } +void ReportConnectivity::returnStatus(bool) { + if (--_remaining == 0) { + finish(); } - finish(); } void ReportConnectivity::finish() const { FRT_Values *dst = _parentRequest->GetReturn(); - FRT_StringValue *pt_hn = dst->AddStringArray(_result.size()); - FRT_StringValue *pt_ss = dst->AddStringArray(_result.size()); - for (const auto & peer : _result) { - dst->SetString(pt_hn++, peer.peerName.c_str()); - dst->SetString(pt_ss++, peer.status.c_str()); + FRT_StringValue *pt_hn = dst->AddStringArray(_checks.size()); + FRT_StringValue *pt_ss = dst->AddStringArray(_checks.size()); + for (const auto & peer : _checks) { + dst->SetString(pt_hn++, peer->getHostname().c_str()); + dst->SetString(pt_ss++, peer->okStatus() ? "ok" : "ping failed"); } _parentRequest->Return(); } diff --git a/configd/src/apps/sentinel/report-connectivity.h b/configd/src/apps/sentinel/report-connectivity.h index ed012e2e07e..19b2d74fce1 100644 --- a/configd/src/apps/sentinel/report-connectivity.h +++ b/configd/src/apps/sentinel/report-connectivity.h @@ -10,52 +10,25 @@ #include "peer-check.h" #include "status-callback.h" +#include <atomic> #include <memory> -#include <mutex> #include <string> #include <vector> namespace config::sentinel { -class ReportConnectivity; - -struct SinglePing : StatusCallback { - ReportConnectivity& parent; - std::string peerName; - int peerPort; - std::string status; - std::unique_ptr<PeerCheck> check; - - SinglePing(ReportConnectivity& owner, const std::string &hostname, int port) - : parent(owner), - peerName(hostname), - peerPort(port), - status("unknown"), - check(nullptr) - {} - - SinglePing(SinglePing &&) = default; - SinglePing(const SinglePing &) = default; - - virtual ~SinglePing(); - void startCheck(FRT_Supervisor &orb); - void returnStatus(bool ok) override; -}; - - -class ReportConnectivity +class ReportConnectivity : public StatusCallback { public: ReportConnectivity(FRT_RPCRequest *req, FRT_Supervisor &orb, ModelSubscriber &modelSubscriber); - ~ReportConnectivity(); - void requestDone(); + virtual ~ReportConnectivity(); + void returnStatus(bool ok) override; private: void finish() const; FRT_RPCRequest *_parentRequest; FRT_Supervisor &_orb; - std::vector<SinglePing> _result; - std::mutex _lock; - size_t _remaining; + std::vector<std::unique_ptr<PeerCheck>> _checks; + std::atomic<size_t> _remaining; }; } |