Make "CanConnect" timeout configurable in watchdog

* see also https://swift-project.slack.com/archives/G7GD2UP9C/p1522624024000024
* added signatures
* changed timeout
This commit is contained in:
Klaus Basan
2018-04-02 19:32:48 +02:00
parent 9192d13a6c
commit 57b3760b61
4 changed files with 35 additions and 13 deletions

View File

@@ -54,7 +54,7 @@ namespace BlackCore
CUrl CNetworkWatchdog::getWorkingSharedUrl() const CUrl CNetworkWatchdog::getWorkingSharedUrl() const
{ {
if (!m_networkAccessible) { return CUrl(); } if (!m_networkAccessible) { return CUrl(); }
QReadLocker l(&m_lockSharedUrl); QReadLocker l(&m_lockUrl);
return m_workingSharedUrl; return m_workingSharedUrl;
} }
@@ -68,17 +68,25 @@ namespace BlackCore
return n; // triggered return n; // triggered
} }
QString CNetworkWatchdog::getLastPingDbUrl() const
{
QReadLocker l(&m_lockUrl);
return m_lastPingUrl;
}
QString CNetworkWatchdog::getCheckInfo() const QString CNetworkWatchdog::getCheckInfo() const
{ {
static const QString info("Internet accessible: %1 (good: %2 / bad: %3), swift DB accessible: %4 (good: %5 / bad: %6)"); static const QString info("Internet accessible: %1 (good: %2 / bad: %3), swift DB accessible: %4 (good: %5 / bad: %6) DB last ping URL: '%7' canConnect: %8ms");
const QString pUrl(this->getLastPingDbUrl());
return info. return info.
arg(boolToYesNo(this->isInternetAccessible())).arg(m_goodCountInternet).arg(m_badCountInternet). arg(boolToYesNo(this->isInternetAccessible())).arg(m_goodCountInternet).arg(m_badCountInternet).
arg(boolToYesNo(this->isSwiftDbAccessible())).arg(m_goodCountDb).arg(m_badCountDb); arg(boolToYesNo(this->isSwiftDbAccessible())).arg(m_goodCountDb).arg(m_badCountDb).
arg(pUrl).arg(CanConnectTimeMs);
} }
void CNetworkWatchdog::setWorkingSharedUrl(const CUrl &workingUrl) void CNetworkWatchdog::setWorkingSharedUrl(const CUrl &workingUrl)
{ {
QWriteLocker l(&m_lockSharedUrl); QWriteLocker l(&m_lockUrl);
m_workingSharedUrl = workingUrl; m_workingSharedUrl = workingUrl;
} }
@@ -99,8 +107,9 @@ namespace BlackCore
const bool wasDbAvailable = m_dbAccessible; const bool wasDbAvailable = m_dbAccessible;
const bool wasInternetAvailable = m_internetAccessible; const bool wasInternetAvailable = m_internetAccessible;
const bool networkAccess = m_networkAccessible; const bool networkAccess = m_networkAccessible;
const CUrl testUrl(CNetworkWatchdog::dbTestUrl());
bool canConnectDb = m_checkDbAccessibility && networkAccess && bool canConnectDb = m_checkDbAccessibility && networkAccess &&
CNetworkUtils::canConnect(dbTestUrl()); // running here in background worker CNetworkUtils::canConnect(testUrl, CanConnectTimeMs); // running here in background worker
if (m_checkDbAccessibility && m_doDetailedCheck && canConnectDb) if (m_checkDbAccessibility && m_doDetailedCheck && canConnectDb)
{ {
// test against real HTTP response // test against real HTTP response
@@ -155,11 +164,11 @@ namespace BlackCore
{ {
QString message; QString message;
static const QString testHost1("www.google.com"); // what else? static const QString testHost1("www.google.com"); // what else?
canConnectInternet = CNetworkUtils::canConnect(testHost1, 443, message); // running in background worker canConnectInternet = CNetworkUtils::canConnect(testHost1, 443, message, CanConnectTimeMs); // running in background worker
if (!canConnectInternet) if (!canConnectInternet)
{ {
static const QString testHost2("www.microsoft.com"); // secondary test static const QString testHost2("www.microsoft.com"); // secondary test
canConnectInternet = CNetworkUtils::canConnect(testHost2, 80, message); // running in background worker canConnectInternet = CNetworkUtils::canConnect(testHost2, 80, message, CanConnectTimeMs); // running in background worker
} }
if (canConnectInternet) { m_goodCountInternet++; } if (canConnectInternet) { m_goodCountInternet++; }
else { m_badCountInternet++; } else { m_badCountInternet++; }
@@ -235,6 +244,12 @@ namespace BlackCore
nw->close(); nw->close();
if (!sApp || sApp->isShuttingDown()) { return; } if (!sApp || sApp->isShuttingDown()) { return; }
m_lastClientPingSuccess = ok; m_lastClientPingSuccess = ok;
const QString url = nwReply->url().toString();
{
QWriteLocker l(&m_lockUrl);
m_lastPingUrl = url;
}
if (ok) { m_goodCountDb++; } if (ok) { m_goodCountDb++; }
else { m_badCountDb++; } else { m_badCountDb++; }
this->setDbAccessibility(ok); this->setDbAccessibility(ok);
@@ -248,7 +263,7 @@ namespace BlackCore
if (oldDbAccessible != m_dbAccessible) if (oldDbAccessible != m_dbAccessible)
{ {
const CUrl testUrl(this->dbTestUrl()); const CUrl testUrl(this->dbTestUrl());
QTimer::singleShot(0, this, [=] { emit this->changedSwiftDbAccessibility(m_dbAccessible, testUrl); }); QTimer::singleShot(0, this, [ = ] { emit this->changedSwiftDbAccessibility(m_dbAccessible, testUrl); });
} }
if (oldInternetAccessible != m_internetAccessible) if (oldInternetAccessible != m_internetAccessible)
{ {
@@ -279,7 +294,7 @@ namespace BlackCore
{ {
const CUrlList urls(sApp->getGlobalSetup().getSwiftSharedUrls()); const CUrlList urls(sApp->getGlobalSetup().getSwiftSharedUrls());
CFailoverUrlList failoverUrls(urls); CFailoverUrlList failoverUrls(urls);
return failoverUrls.getRandomWorkingUrl(); // use CNetworkUtils::canConnect return failoverUrls.getRandomWorkingUrl(2, CanConnectTimeMs); // uses CNetworkUtils::canConnect
} }
} // ns } // ns
} // ns } // ns

View File

@@ -79,6 +79,9 @@ namespace BlackCore
//! \threadsafe //! \threadsafe
int getCheckCount() const { return m_checkCount; } int getCheckCount() const { return m_checkCount; }
//! Last URL used for ping /DB ping service)
QString getLastPingDbUrl() const;
//! Number of completed checks //! Number of completed checks
//! \threadsafe //! \threadsafe
QString getCheckInfo() const; QString getCheckInfo() const;
@@ -119,6 +122,8 @@ namespace BlackCore
void changedInternetAccessibility(bool available); void changedInternetAccessibility(bool available);
private: private:
static constexpr int CanConnectTimeMs = 5000;
//! Do work, i.e. check connectivity //! Do work, i.e. check connectivity
void doWork(); void doWork();
@@ -159,8 +164,9 @@ namespace BlackCore
std::atomic_int m_badCountInternet { 0 }; //! Total number of Internet failing count (only when network is accessible) std::atomic_int m_badCountInternet { 0 }; //! Total number of Internet failing count (only when network is accessible)
std::atomic_int m_goodCountDb { 0 }; std::atomic_int m_goodCountDb { 0 };
std::atomic_int m_goodCountInternet { 0 }; std::atomic_int m_goodCountInternet { 0 };
QString m_lastPingUrl;
BlackMisc::Network::CUrl m_workingSharedUrl; BlackMisc::Network::CUrl m_workingSharedUrl;
mutable QReadWriteLock m_lockSharedUrl; mutable QReadWriteLock m_lockUrl;
}; };
} // ns } // ns
} // ns } // ns

View File

@@ -45,18 +45,19 @@ namespace BlackMisc
return (*this)[i]; return (*this)[i];
} }
CUrl CUrlList::getRandomWorkingUrl(int maxTrials) const CUrl CUrlList::getRandomWorkingUrl(int maxTrials, int timeoutMs) const
{ {
if (this->isEmpty()) { return CUrl(); } if (this->isEmpty()) { return CUrl(); }
if (maxTrials < 1) { return CUrl();} if (maxTrials < 1) { return CUrl();}
CUrlList trials; CUrlList trials;
if (timeoutMs < 0) { timeoutMs = CNetworkUtils::getTimeoutMs(); }
for (int t = 0; t < maxTrials && t < this->size(); t++) for (int t = 0; t < maxTrials && t < this->size(); t++)
{ {
CUrl url(getRandomWithout(trials)); CUrl url(getRandomWithout(trials));
trials.push_back(url); trials.push_back(url);
QString message; QString message;
if (CNetworkUtils::canConnect(url, message)) { return url; } if (CNetworkUtils::canConnect(url, message, timeoutMs)) { return url; }
} }
return CUrl(); return CUrl();
} }

View File

@@ -47,7 +47,7 @@ namespace BlackMisc
CUrl getRandomUrl() const; CUrl getRandomUrl() const;
//! Random location for distributed load, tested //! Random location for distributed load, tested
CUrl getRandomWorkingUrl(int maxTrials = 2) const; CUrl getRandomWorkingUrl(int maxTrials = 2, int timeoutMs = -1) const;
//! Random location for distributed load //! Random location for distributed load
CUrl getRandomWithout(const CUrlList &exclude) const; CUrl getRandomWithout(const CUrlList &exclude) const;