Skip to content

Commit

Permalink
Merge pull request #4213 from sysown/v.2x_dns_cache_issue_4210
Browse files Browse the repository at this point in the history
DNS module no longer regards the UNIX socket path as a hostname
  • Loading branch information
renecannao authored May 11, 2023
2 parents b5f6998 + cd352ff commit 7f727b3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion include/MySQL_Monitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ class MySQL_Monitor {
public:
static std::string dns_lookup(const std::string& hostname, bool return_hostname_if_lookup_fails = true, size_t* ip_count = NULL);
static std::string dns_lookup(const char* hostname, bool return_hostname_if_lookup_fails = true, size_t* ip_count = NULL);
static bool dns_cache_update_socket(const std::string& hostname, int socket_fd);
static bool update_dns_cache_from_mysql_conn(const MYSQL* mysql);
static void trigger_dns_cache_update();


Expand Down
2 changes: 1 addition & 1 deletion include/ProxySQL_Cluster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ProxySQL_Node_Address {
uint16_t port;
ProxySQL_Node_Address(char *h, uint16_t p) : ProxySQL_Node_Address(h, p, NULL) {
// resolving DNS if available in Cache
if (h) {
if (h && p) {
size_t ip_count = 0;
const std::string& ip = MySQL_Monitor::dns_lookup(h, false, &ip_count);

Expand Down
20 changes: 15 additions & 5 deletions lib/MySQL_Monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,7 @@ bool MySQL_Monitor_State_Data::set_wait_timeout() {
bool MySQL_Monitor_State_Data::create_new_connection() {
mysql=mysql_init(NULL);
assert(mysql);
if (use_ssl) {
if (use_ssl && port) {
mysql_ssl_set(mysql,
mysql_thread___ssl_p2s_key,
mysql_thread___ssl_p2s_cert,
Expand Down Expand Up @@ -1543,7 +1543,7 @@ bool MySQL_Monitor_State_Data::create_new_connection() {
#else
fcntl(mysql->net.fd, F_SETFL, f|O_NONBLOCK);
#endif /* FD_CLOEXEC */
MySQL_Monitor::dns_cache_update_socket(mysql->host, mysql->net.fd);
MySQL_Monitor::update_dns_cache_from_mysql_conn(mysql);
}
return true;
}
Expand Down Expand Up @@ -4527,7 +4527,9 @@ void* MySQL_Monitor::monitor_dns_cache() {
int cols = 0;
int affected_rows = 0;
SQLite3_result* resultset = NULL;
const char* query = (char*)"SELECT trim(hostname) FROM monitor_internal.mysql_servers UNION SELECT trim(hostname) FROM monitor_internal.proxysql_servers";
const char* query = (char*)"SELECT trim(hostname) FROM monitor_internal.mysql_servers WHERE port!=0"
" UNION "
"SELECT trim(hostname) FROM monitor_internal.proxysql_servers WHERE port!=0";

t1 = monotonic_time();

Expand Down Expand Up @@ -6478,15 +6480,23 @@ std::string MySQL_Monitor::dns_lookup(const char* hostname, bool return_hostname
return MySQL_Monitor::dns_lookup(std::string(hostname), return_hostname_if_lookup_fails, ip_count);
}

bool MySQL_Monitor::dns_cache_update_socket(const std::string& hostname, int socket_fd)
bool MySQL_Monitor::update_dns_cache_from_mysql_conn(const MYSQL* mysql)
{
assert(mysql);

// if port==0, UNIX socket is used
if (mysql->port == 0)
return false;

const std::string& hostname = mysql->host;

// if IP was provided, no need to update dns cache
if (hostname.empty() || validate_ip(hostname))
return false;

bool result = false;

const std::string& ip_addr = get_connected_peer_ip_from_socket(socket_fd);
const std::string& ip_addr = get_connected_peer_ip_from_socket(mysql->net.fd);

if (ip_addr.empty() == false) {
result = _dns_cache_update(hostname, { ip_addr });
Expand Down
4 changes: 2 additions & 2 deletions lib/MySQL_Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ bool Session_Regex::match(char *m) {
KillArgs::KillArgs(char* u, char* p, char* h, unsigned int P, unsigned int _hid, unsigned long i, int kt, MySQL_Thread* _mt) :
KillArgs(u, p, h, P, _hid, i, kt, _mt, NULL) {
// resolving DNS if available in Cache
if (h) {
if (h && P) {
const std::string& ip = MySQL_Monitor::dns_lookup(h, false);

if (ip.empty() == false) {
Expand Down Expand Up @@ -265,7 +265,7 @@ void * kill_query_thread(void *arg) {
goto __exit_kill_query_thread;
}

MySQL_Monitor::dns_cache_update_socket(mysql->host, mysql->net.fd);
MySQL_Monitor::update_dns_cache_from_mysql_conn(mysql);

char buf[100];
switch (ka->kill_type) {
Expand Down
16 changes: 8 additions & 8 deletions lib/ProxySQL_Cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void * ProxySQL_Cluster_Monitor_thread(void *args) {
// }
//char *query = query1;
if (rc_conn) {
MySQL_Monitor::dns_cache_update_socket(conn->host, conn->net.fd);
MySQL_Monitor::update_dns_cache_from_mysql_conn(conn);

rc_query = mysql_query(conn,(char *)"SELECT @@version");
if (rc_query == 0) {
Expand Down Expand Up @@ -308,7 +308,7 @@ void ProxySQL_Node_Metrics::reset() {
ProxySQL_Node_Entry::ProxySQL_Node_Entry(char *_hostname, uint16_t _port, uint64_t _weight, char * _comment) :
ProxySQL_Node_Entry(_hostname, _port, _weight, _comment, NULL) {
// resolving DNS if available in Cache
if (_hostname) {
if (_hostname && _port) {
size_t ip_count = 0;
const std::string& ip = MySQL_Monitor::dns_lookup(_hostname, false, &ip_count);

Expand Down Expand Up @@ -1041,7 +1041,7 @@ void ProxySQL_Cluster::pull_mysql_query_rules_from_peer(const string& expected_c
proxy_info("Cluster: Fetching MySQL Query Rules from peer %s:%d started. Expected checksum: %s\n", hostname, port, expected_checksum.c_str());
rc_conn = mysql_real_connect(conn, ip_address ? ip_address : hostname, username, password, NULL, port, NULL, 0);
if (rc_conn) {
MySQL_Monitor::dns_cache_update_socket(conn->host, conn->net.fd);
MySQL_Monitor::update_dns_cache_from_mysql_conn(conn);

MYSQL_RES *result1 = NULL;
MYSQL_RES *result2 = NULL;
Expand Down Expand Up @@ -1323,8 +1323,8 @@ void ProxySQL_Cluster::pull_mysql_users_from_peer(const string& expected_checksu

goto __exit_pull_mysql_users_from_peer;
}
MySQL_Monitor::dns_cache_update_socket(conn->host, conn->net.fd);

MySQL_Monitor::update_dns_cache_from_mysql_conn(conn);

rc_query = mysql_query(conn, CLUSTER_QUERY_MYSQL_USERS);
if (rc_query == 0) {
Expand Down Expand Up @@ -1635,7 +1635,7 @@ void ProxySQL_Cluster::pull_mysql_servers_from_peer(const std::string& checksum,
proxy_info("Cluster: Fetching MySQL Servers from peer %s:%d started. Expected checksum %s\n", hostname, port, peer_checksum);
rc_conn = mysql_real_connect(conn, ip_address ? ip_address : hostname, username, password, NULL, port, NULL, 0);
if (rc_conn) {
MySQL_Monitor::dns_cache_update_socket(conn->host, conn->net.fd);
MySQL_Monitor::update_dns_cache_from_mysql_conn(conn);

std::vector<MYSQL_RES*> results {};

Expand Down Expand Up @@ -2054,7 +2054,7 @@ void ProxySQL_Cluster::pull_global_variables_from_peer(const string& var_type, c
rc_conn = mysql_real_connect(conn, ip_address ? ip_address : hostname, username, password, NULL, port, NULL, 0);

if (rc_conn) {
MySQL_Monitor::dns_cache_update_socket(conn->host, conn->net.fd);
MySQL_Monitor::update_dns_cache_from_mysql_conn(conn);

std::string s_query = "";
string_format("SELECT * FROM runtime_global_variables WHERE variable_name LIKE '%s-%%'", s_query, var_type.c_str());
Expand Down Expand Up @@ -2208,7 +2208,7 @@ void ProxySQL_Cluster::pull_proxysql_servers_from_peer(const std::string& expect
);
rc_conn = mysql_real_connect(conn, ip_address ? ip_address : hostname, username, password, NULL, port, NULL, 0);
if (rc_conn) {
MySQL_Monitor::dns_cache_update_socket(conn->host, conn->net.fd);
MySQL_Monitor::update_dns_cache_from_mysql_conn(conn);

rc_query = mysql_query(conn,"SELECT hostname, port, weight, comment FROM runtime_proxysql_servers ORDER BY hostname, port");
if ( rc_query == 0 ) {
Expand Down
5 changes: 3 additions & 2 deletions lib/mysql_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ void MySQL_Connection::connect_start() {
}
mysql_options4(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "mysql_bug_102266", "Avoid MySQL bug https://bugs.mysql.com/bug.php?id=102266 , https://github.com/sysown/proxysql/issues/3276");
}
if (parent->use_ssl) {
if (parent->use_ssl && parent->port) {
mysql_ssl_set(mysql,
mysql_thread___ssl_p2s_key,
mysql_thread___ssl_p2s_cert,
Expand Down Expand Up @@ -857,6 +857,7 @@ void MySQL_Connection::connect_start() {

async_exit_status=mysql_real_connect_start(&ret_mysql, mysql, host_ip, userinfo->username, auth_password, userinfo->schemaname, parent->port, NULL, client_flags);
} else {
client_flags &= ~(CLIENT_COMPRESS); // disabling compression for connections made via Unix socket
async_exit_status=mysql_real_connect_start(&ret_mysql, mysql, "localhost", userinfo->username, auth_password, userinfo->schemaname, parent->port, parent->address, client_flags);
}
fd=mysql_get_socket(mysql);
Expand Down Expand Up @@ -1166,7 +1167,7 @@ MDB_ASYNC_ST MySQL_Connection::handler(short event) {
//vio_blocking(mysql->net.vio, FALSE, 0);
//fcntl(mysql->net.vio->sd, F_SETFL, O_RDWR|O_NONBLOCK);
//}
MySQL_Monitor::dns_cache_update_socket(mysql->host, mysql->net.fd);
MySQL_Monitor::update_dns_cache_from_mysql_conn(mysql);
break;
case ASYNC_CONNECT_FAILED:
MyHGM->p_update_mysql_error_counter(p_mysql_error_type::mysql, parent->myhgc->hid, parent->address, parent->port, mysql_errno(mysql));
Expand Down

0 comments on commit 7f727b3

Please sign in to comment.