MXS-2546 Add DNS-resolving to topology detection
When matching hostnames between MaxScale server configuration and the SHOW SLAVE STATUS-output, use DNS-resolution if a simple string comparison doesn't find an answer. Results of the resolution are saved to avoid repeating the operation for the same address.
This commit is contained in:
@ -12,11 +12,13 @@
|
||||
*/
|
||||
|
||||
#include "mariadbmon.hh"
|
||||
|
||||
#include <algorithm>
|
||||
#include <inttypes.h>
|
||||
#include <string>
|
||||
#include <queue>
|
||||
#include <maxbase/format.hh>
|
||||
#include <maxbase/host.hh>
|
||||
#include <maxscale/modutil.hh>
|
||||
#include <maxscale/mysql_utils.hh>
|
||||
|
||||
@ -1002,3 +1004,35 @@ bool MariaDBMonitor::is_candidate_valid(MariaDBServer* cand, RequireRunning req_
|
||||
}
|
||||
return is_valid;
|
||||
};
|
||||
|
||||
string MariaDBMonitor::DNSResolver::resolve_server(const string& host)
|
||||
{
|
||||
auto now = mxb::Clock::now();
|
||||
const auto MAX_AGE = mxb::Duration((double)5*60); // Refresh interval for cache entries.
|
||||
auto recent_time = now - MAX_AGE;
|
||||
|
||||
string rval;
|
||||
auto iter = m_mapping.find(host);
|
||||
|
||||
if (iter == m_mapping.end() || iter->second.timestamp < recent_time)
|
||||
{
|
||||
// Map did not have a record, or it was too old. In either case, generate a new one.
|
||||
string addr;
|
||||
string error_msg;
|
||||
bool dns_success = mxb::name_lookup(host, &addr, &error_msg);
|
||||
if (!dns_success)
|
||||
{
|
||||
MXB_ERROR("Could not resolve host '%s'. %s", host.c_str(), error_msg.c_str());
|
||||
}
|
||||
// If dns failed, addr will be empty. Add the element anyway to prevent repeated lookups.
|
||||
MapElement newelem = {addr, now};
|
||||
m_mapping[host] = newelem;
|
||||
rval = addr;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Return recent value.
|
||||
rval = iter->second.address;
|
||||
}
|
||||
return rval;
|
||||
}
|
||||
|
||||
@ -94,6 +94,8 @@ void MariaDBMonitor::reset_server_info()
|
||||
{
|
||||
m_servers.push_back(new MariaDBServer(mon_server, m_servers.size(), m_settings.shared));
|
||||
}
|
||||
|
||||
m_resolver = DNSResolver(); // Erases result cache.
|
||||
}
|
||||
|
||||
void MariaDBMonitor::reset_node_index_info()
|
||||
@ -106,10 +108,9 @@ void MariaDBMonitor::reset_node_index_info()
|
||||
|
||||
MariaDBServer* MariaDBMonitor::get_server(const EndPoint& search_ep)
|
||||
{
|
||||
// TODO: Do this with a map lookup
|
||||
// TODO: Add DNS check here.
|
||||
MariaDBServer* found = NULL;
|
||||
for (MariaDBServer* server : m_servers)
|
||||
// Phase 1: Direct string compare
|
||||
for (auto server : m_servers)
|
||||
{
|
||||
EndPoint srv(server->m_server_base->server);
|
||||
if (srv == search_ep)
|
||||
@ -118,6 +119,28 @@ MariaDBServer* MariaDBMonitor::get_server(const EndPoint& search_ep)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
// Phase 2: Was not found with simple string compare. Try DNS resolving for endpoints with
|
||||
// matching ports.
|
||||
string target_addr = m_resolver.resolve_server(search_ep.host());
|
||||
if (!target_addr.empty())
|
||||
{
|
||||
for (auto server : m_servers)
|
||||
{
|
||||
if (server->m_server_base->server->port == search_ep.port())
|
||||
{
|
||||
string server_addr = m_resolver.resolve_server(server->m_server_base->server->address);
|
||||
if (server_addr == target_addr)
|
||||
{
|
||||
found = server;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
|
||||
@ -169,6 +169,21 @@ private:
|
||||
bool result_waiting = false; /* Guard variable for has_result */
|
||||
};
|
||||
|
||||
class DNSResolver
|
||||
{
|
||||
public:
|
||||
std::string resolve_server(const std::string& host);
|
||||
|
||||
private:
|
||||
struct MapElement
|
||||
{
|
||||
std::string address;
|
||||
mxb::TimePoint timestamp;
|
||||
};
|
||||
|
||||
std::unordered_map<std::string, MapElement> m_mapping; // hostname -> address cache
|
||||
};
|
||||
|
||||
ManualCommand m_manual_cmd; /* Communicates manual commands and results */
|
||||
|
||||
// Server containers, mostly constant.
|
||||
@ -185,6 +200,8 @@ private:
|
||||
bool m_cluster_modified = false; /* Has a cluster operation been performed this loop? Prevents
|
||||
* other operations during this tick. */
|
||||
|
||||
DNSResolver m_resolver; /* DNS-resolver with cache */
|
||||
|
||||
/* Counter for temporary automatic cluster operation disabling. */
|
||||
int cluster_operation_disable_timer = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user