diff --git a/server/modules/monitor/clustrixmon/clustrixmonitor.cc b/server/modules/monitor/clustrixmon/clustrixmonitor.cc index 606361248..503c6cde0 100644 --- a/server/modules/monitor/clustrixmon/clustrixmonitor.cc +++ b/server/modules/monitor/clustrixmon/clustrixmonitor.cc @@ -274,62 +274,25 @@ void ClustrixMonitor::choose_hub(Clustrix::Softfailed softfailed) { mxb_assert(!m_pHub_con); - SERVER* pHub_server = nullptr; - MYSQL* pHub_con = nullptr; - set ips; - // First we check the dynamic servers, in case there are. - for (auto it = m_nodes.begin(); !pHub_con && (it != m_nodes.end()); ++it) + // First we check the dynamic servers, in case there are, + if (!choose_dynamic_hub(softfailed, ips)) { - auto& element = *it; - ClustrixNode& node = element.second; - - if (node.can_be_used_as_hub(name(), m_settings.conn_settings)) + // then we check the bootstrap servers, and + if (!choose_bootstrap_hub(softfailed, ips)) { - pHub_con = node.release_connection(); - pHub_server = node.server(); - } - - ips.insert(node.ip()); - } - - if (!pHub_con) - { - // If that fails, then we check the bootstrap servers, but only if - // it was not checked above. - - for (auto it = m_servers.begin(); !pHub_con && (it != m_servers.end()); ++it) - { - MonitorServer& ms = **it; - - if (ips.find(ms.server->address) == ips.end()) - { - if (Clustrix::ping_or_connect_to_hub(name(), m_settings.conn_settings, softfailed, ms)) - { - pHub_con = ms.con; - pHub_server = ms.server; - } - else if (ms.con) - { - mysql_close(ms.con); - } - - ms.con = nullptr; - } + // finally, if all else fails, we check servers that have been persisted. + // In practise we will only get here at startup (no dynamic servers) + // if the bootstrap servers cannot be contacted. + choose_persisted_hub(softfailed, ips); } } - if (pHub_con) + if (m_pHub_con) { MXS_NOTICE("%s: Monitoring Clustrix cluster state using node %s:%d.", - name(), pHub_server->address, pHub_server->port); - - m_pHub_con = pHub_con; - m_pHub_server = pHub_server; - - mxb_assert(m_pHub_con); - mxb_assert(m_pHub_con); + name(), m_pHub_server->address, m_pHub_server->port); } else { @@ -338,6 +301,56 @@ void ClustrixMonitor::choose_hub(Clustrix::Softfailed softfailed) } } +bool ClustrixMonitor::choose_dynamic_hub(Clustrix::Softfailed softfailed, std::set& ips_checked) +{ + for (auto it = m_nodes.begin(); !m_pHub_con && (it != m_nodes.end()); ++it) + { + auto& element = *it; + ClustrixNode& node = element.second; + + if (node.can_be_used_as_hub(name(), m_settings.conn_settings, softfailed)) + { + m_pHub_con = node.release_connection(); + m_pHub_server = node.server(); + } + + ips_checked.insert(node.ip()); + } + + return m_pHub_con != nullptr; +} + +bool ClustrixMonitor::choose_bootstrap_hub(Clustrix::Softfailed softfailed, std::set& ips_checked) +{ + for (auto it = m_servers.begin(); !m_pHub_con && (it != m_servers.end()); ++it) + { + MonitorServer& ms = **it; + + if (ips_checked.find(ms.server->address) == ips_checked.end()) + { + if (Clustrix::ping_or_connect_to_hub(name(), m_settings.conn_settings, softfailed, ms)) + { + m_pHub_con = ms.con; + m_pHub_server = ms.server; + } + else if (ms.con) + { + mysql_close(ms.con); + } + + ms.con = nullptr; + } + } + + return m_pHub_con != nullptr; +} + +bool ClustrixMonitor::choose_persisted_hub(Clustrix::Softfailed softfailed, std::set& ips_checked) +{ + // TODO: Check persisted servers. + return false; +} + void ClustrixMonitor::refresh_nodes() { mxb_assert(m_pHub_con); diff --git a/server/modules/monitor/clustrixmon/clustrixmonitor.hh b/server/modules/monitor/clustrixmon/clustrixmonitor.hh index 456dd2158..40a239a42 100644 --- a/server/modules/monitor/clustrixmon/clustrixmonitor.hh +++ b/server/modules/monitor/clustrixmon/clustrixmonitor.hh @@ -14,6 +14,7 @@ #include "clustrixmon.hh" #include +#include #include #include #include @@ -87,6 +88,11 @@ private: void check_cluster(Clustrix::Softfailed softfailed); void check_hub(Clustrix::Softfailed softfailed); void choose_hub(Clustrix::Softfailed softfailed); + + bool choose_dynamic_hub(Clustrix::Softfailed softfailed, std::set& ips_checked); + bool choose_bootstrap_hub(Clustrix::Softfailed softfailed, std::set& ips_checked); + bool choose_persisted_hub(Clustrix::Softfailed softfailed, std::set& ips_checked); + void refresh_nodes(); bool check_cluster_membership(std::map* pMemberships); diff --git a/server/modules/monitor/clustrixmon/clustrixnode.cc b/server/modules/monitor/clustrixmon/clustrixnode.cc index 45b47b06a..cb65d5e2e 100644 --- a/server/modules/monitor/clustrixmon/clustrixnode.cc +++ b/server/modules/monitor/clustrixmon/clustrixnode.cc @@ -15,11 +15,11 @@ #include "clustrix.hh" bool ClustrixNode::can_be_used_as_hub(const char* zName, - const mxs::MonitorServer::ConnectionSettings& settings) + const mxs::MonitorServer::ConnectionSettings& settings, + Clustrix::Softfailed softfailed) { mxb_assert(m_pServer); - bool rv = Clustrix::ping_or_connect_to_hub(zName, settings, Clustrix::Softfailed::REJECT, - *m_pServer, &m_pCon); + bool rv = Clustrix::ping_or_connect_to_hub(zName, settings, softfailed, *m_pServer, &m_pCon); if (!rv) { diff --git a/server/modules/monitor/clustrixmon/clustrixnode.hh b/server/modules/monitor/clustrixmon/clustrixnode.hh index 74692856f..5f85caee0 100644 --- a/server/modules/monitor/clustrixmon/clustrixnode.hh +++ b/server/modules/monitor/clustrixmon/clustrixnode.hh @@ -161,7 +161,8 @@ public: } bool can_be_used_as_hub(const char* zName, - const mxs::MonitorServer::ConnectionSettings& settings); + const mxs::MonitorServer::ConnectionSettings& settings, + Clustrix::Softfailed softfailed); SERVER* server() const {