MXS-2424 Refactor for further changes

In subsequent change(s) persisted node information will be used
as a last resort to connect to a Clustrix node.
This commit is contained in:
Johan Wikman
2019-04-05 16:08:09 +03:00
parent 875146f53c
commit c422aafe1d
4 changed files with 71 additions and 51 deletions

View File

@ -274,41 +274,64 @@ void ClustrixMonitor::choose_hub(Clustrix::Softfailed softfailed)
{ {
mxb_assert(!m_pHub_con); mxb_assert(!m_pHub_con);
SERVER* pHub_server = nullptr;
MYSQL* pHub_con = nullptr;
set<string> ips; set<string> ips;
// First we check the dynamic servers, in case there are. // First we check the dynamic servers, in case there are,
for (auto it = m_nodes.begin(); !pHub_con && (it != m_nodes.end()); ++it) if (!choose_dynamic_hub(softfailed, ips))
{
// then we check the bootstrap servers, and
if (!choose_bootstrap_hub(softfailed, ips))
{
// 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 (m_pHub_con)
{
MXS_NOTICE("%s: Monitoring Clustrix cluster state using node %s:%d.",
name(), m_pHub_server->address, m_pHub_server->port);
}
else
{
MXS_ERROR("%s: Could not connect to any server or no server that could "
"be connected to was part of the quorum.", name());
}
}
bool ClustrixMonitor::choose_dynamic_hub(Clustrix::Softfailed softfailed, std::set<string>& ips_checked)
{
for (auto it = m_nodes.begin(); !m_pHub_con && (it != m_nodes.end()); ++it)
{ {
auto& element = *it; auto& element = *it;
ClustrixNode& node = element.second; ClustrixNode& node = element.second;
if (node.can_be_used_as_hub(name(), m_settings.conn_settings)) if (node.can_be_used_as_hub(name(), m_settings.conn_settings, softfailed))
{ {
pHub_con = node.release_connection(); m_pHub_con = node.release_connection();
pHub_server = node.server(); m_pHub_server = node.server();
} }
ips.insert(node.ip()); ips_checked.insert(node.ip());
} }
if (!pHub_con) return m_pHub_con != nullptr;
{ }
// 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) bool ClustrixMonitor::choose_bootstrap_hub(Clustrix::Softfailed softfailed, std::set<string>& ips_checked)
{
for (auto it = m_servers.begin(); !m_pHub_con && (it != m_servers.end()); ++it)
{ {
MonitorServer& ms = **it; MonitorServer& ms = **it;
if (ips.find(ms.server->address) == ips.end()) if (ips_checked.find(ms.server->address) == ips_checked.end())
{ {
if (Clustrix::ping_or_connect_to_hub(name(), m_settings.conn_settings, softfailed, ms)) if (Clustrix::ping_or_connect_to_hub(name(), m_settings.conn_settings, softfailed, ms))
{ {
pHub_con = ms.con; m_pHub_con = ms.con;
pHub_server = ms.server; m_pHub_server = ms.server;
} }
else if (ms.con) else if (ms.con)
{ {
@ -318,24 +341,14 @@ void ClustrixMonitor::choose_hub(Clustrix::Softfailed softfailed)
ms.con = nullptr; ms.con = nullptr;
} }
} }
return m_pHub_con != nullptr;
} }
if (pHub_con) bool ClustrixMonitor::choose_persisted_hub(Clustrix::Softfailed softfailed, std::set<string>& ips_checked)
{ {
MXS_NOTICE("%s: Monitoring Clustrix cluster state using node %s:%d.", // TODO: Check persisted servers.
name(), pHub_server->address, pHub_server->port); return false;
m_pHub_con = pHub_con;
m_pHub_server = pHub_server;
mxb_assert(m_pHub_con);
mxb_assert(m_pHub_con);
}
else
{
MXS_ERROR("%s: Could not connect to any server or no server that could "
"be connected to was part of the quorum.", name());
}
} }
void ClustrixMonitor::refresh_nodes() void ClustrixMonitor::refresh_nodes()

View File

@ -14,6 +14,7 @@
#include "clustrixmon.hh" #include "clustrixmon.hh"
#include <map> #include <map>
#include <set>
#include <sqlite3.h> #include <sqlite3.h>
#include <maxscale/monitor.hh> #include <maxscale/monitor.hh>
#include <maxbase/http.hh> #include <maxbase/http.hh>
@ -87,6 +88,11 @@ private:
void check_cluster(Clustrix::Softfailed softfailed); void check_cluster(Clustrix::Softfailed softfailed);
void check_hub(Clustrix::Softfailed softfailed); void check_hub(Clustrix::Softfailed softfailed);
void choose_hub(Clustrix::Softfailed softfailed); void choose_hub(Clustrix::Softfailed softfailed);
bool choose_dynamic_hub(Clustrix::Softfailed softfailed, std::set<std::string>& ips_checked);
bool choose_bootstrap_hub(Clustrix::Softfailed softfailed, std::set<std::string>& ips_checked);
bool choose_persisted_hub(Clustrix::Softfailed softfailed, std::set<std::string>& ips_checked);
void refresh_nodes(); void refresh_nodes();
bool check_cluster_membership(std::map<int, ClustrixMembership>* pMemberships); bool check_cluster_membership(std::map<int, ClustrixMembership>* pMemberships);

View File

@ -15,11 +15,11 @@
#include "clustrix.hh" #include "clustrix.hh"
bool ClustrixNode::can_be_used_as_hub(const char* zName, 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); mxb_assert(m_pServer);
bool rv = Clustrix::ping_or_connect_to_hub(zName, settings, Clustrix::Softfailed::REJECT, bool rv = Clustrix::ping_or_connect_to_hub(zName, settings, softfailed, *m_pServer, &m_pCon);
*m_pServer, &m_pCon);
if (!rv) if (!rv)
{ {

View File

@ -161,7 +161,8 @@ public:
} }
bool can_be_used_as_hub(const char* zName, 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 SERVER* server() const
{ {