From 7e8a78afef9702419bdc5ad38b3a33247276fb56 Mon Sep 17 00:00:00 2001 From: Arne Juul Date: Fri, 11 Jun 2021 11:50:24 +0000 Subject: rename and refactor * make a proper Accumulator class * rename some internal variables --- configd/src/apps/sentinel/connectivity.cpp | 27 ++++++++++++++------------- configd/src/apps/sentinel/connectivity.h | 13 ++++++++----- 2 files changed, 22 insertions(+), 18 deletions(-) (limited to 'configd') diff --git a/configd/src/apps/sentinel/connectivity.cpp b/configd/src/apps/sentinel/connectivity.cpp index 2cdd6f8a914..0e91b583868 100644 --- a/configd/src/apps/sentinel/connectivity.cpp +++ b/configd/src/apps/sentinel/connectivity.cpp @@ -145,7 +145,7 @@ Connectivity::checkConnectivity(RpcServer &rpcServer) { } checkContext.latch.await(); classifyConnFails(connectivityMap, _checkSpecs, rpcServer); - Accumulated accumulated; + Accumulator accumulated; for (const auto & [hostname, check] : connectivityMap) { std::string detail = toString(check.result()); std::string prev = _detailsPerHost[hostname]; @@ -154,42 +154,43 @@ Connectivity::checkConnectivity(RpcServer &rpcServer) { } _detailsPerHost[hostname] = detail; LOG_ASSERT(check.result() != CcResult::UNKNOWN); - accumulate(accumulated, check.result()); + accumulated.handleResult(check.result()); } - return enoughOk(accumulated, clusterSize); + return accumulated.enoughOk(_config); } -void Connectivity::accumulate(Accumulated &target, CcResult value) { +void Connectivity::Accumulator::handleResult(CcResult value) { + ++_numHandled; switch (value) { case CcResult::UNKNOWN: case CcResult::UNREACHABLE_UP: case CcResult::INDIRECT_PING_FAIL: - ++target.numSeriousIssues; + ++_numBad; break; case CcResult::CONN_FAIL: // not OK, but not a serious issue either break; case CcResult::INDIRECT_PING_UNAVAIL: case CcResult::ALL_OK: - ++target.numUpAndOk; + ++_numOk; break; } } -bool Connectivity::enoughOk(const Accumulated &results, size_t clusterSize) { +bool Connectivity::Accumulator::enoughOk(const SentinelConfig::Connectivity &config) const { bool enough = true; - if (results.numSeriousIssues > size_t(_config.maxBadCount)) { + if (_numBad > size_t(config.maxBadCount)) { LOG(warning, "%zu of %zu nodes up but with network connectivity problems (max is %d)", - results.numSeriousIssues, clusterSize, _config.maxBadCount); + _numBad, _numHandled, config.maxBadCount); enough = false; } - if (results.numUpAndOk * 100.0 < _config.minOkPercent * clusterSize) { - double pct = results.numUpAndOk * 100.0 / clusterSize; + if (_numOk * 100.0 < config.minOkPercent * _numHandled) { + double pct = _numOk * 100.0 / _numHandled; LOG(warning, "Only %zu of %zu nodes are up and OK, %.1f%% (min is %d%%)", - results.numUpAndOk, clusterSize, pct, _config.minOkPercent); + _numOk, _numHandled, pct, config.minOkPercent); enough = false; } - if (results.numUpAndOk == clusterSize) { + if (_numOk == _numHandled) { LOG(info, "All connectivity checks OK, proceeding with service startup"); } else if (enough) { LOG(info, "Enough connectivity checks OK, proceeding with service startup"); diff --git a/configd/src/apps/sentinel/connectivity.h b/configd/src/apps/sentinel/connectivity.h index 6b0e2d7523e..c1e41249dd5 100644 --- a/configd/src/apps/sentinel/connectivity.h +++ b/configd/src/apps/sentinel/connectivity.h @@ -27,12 +27,15 @@ public: void configure(const SentinelConfig::Connectivity &config); bool checkConnectivity(RpcServer &rpcServer); private: - struct Accumulated { - size_t numUpAndOk = 0; - size_t numSeriousIssues = 0; + class Accumulator { + private: + size_t _numOk = 0; + size_t _numBad = 0; + size_t _numHandled = 0; + public: + void handleResult(CcResult value); + bool enoughOk(const SentinelConfig::Connectivity &config) const; }; - void accumulate(Accumulated &target, CcResult value); - bool enoughOk(const Accumulated &results, size_t clusterSize); SentinelConfig::Connectivity _config; SpecMap _checkSpecs; std::map _detailsPerHost; -- cgit v1.2.3