@@ -1564,6 +1564,10 @@ bool MySQL_Monitor_State_Data::create_new_connection() {
15641564 myrc=mysql_real_connect (mysql, " localhost" , mysql_thread___monitor_username, mysql_thread___monitor_password, NULL , 0 , hostname, 0 );
15651565 }
15661566 if (myrc==NULL ) {
1567+ // port == 0 means we are connecting to a unix socket
1568+ if (port) {
1569+ MySQL_Monitor::remove_dns_record_from_dns_cache (hostname);
1570+ }
15671571 mysql_error_msg=strdup (mysql_error (mysql));
15681572 int myerrno=mysql_errno (mysql);
15691573 MyHGM->p_update_mysql_error_counter (p_mysql_error_type::proxysql, hostgroup_id, hostname, port, myerrno);
@@ -6670,6 +6674,15 @@ bool MySQL_Monitor::update_dns_cache_from_mysql_conn(const MYSQL* mysql)
66706674 return result;
66716675}
66726676
6677+ void MySQL_Monitor::remove_dns_record_from_dns_cache (const std::string& hostname) {
6678+
6679+ // if IP was provided, no need to update dns cache
6680+ if (hostname.empty () || validate_ip (hostname))
6681+ return ;
6682+
6683+ _remove_dns_record_from_dns_cache (hostname);
6684+ }
6685+
66736686bool MySQL_Monitor::_dns_cache_update (const std::string &hostname, std::vector<std::string>&& ip_address) {
66746687 static thread_local std::shared_ptr<DNS_Cache> dns_cache_thread;
66756688
@@ -6686,6 +6699,18 @@ bool MySQL_Monitor::_dns_cache_update(const std::string &hostname, std::vector<s
66866699 return false ;
66876700}
66886701
6702+ void MySQL_Monitor::_remove_dns_record_from_dns_cache (const std::string& hostname) {
6703+ static thread_local std::shared_ptr<DNS_Cache> dns_cache_thread;
6704+
6705+ if (!dns_cache_thread && GloMyMon)
6706+ dns_cache_thread = GloMyMon->dns_cache ;
6707+
6708+ if (dns_cache_thread) {
6709+ dns_cache_thread->remove (trim (hostname));
6710+ proxy_debug (PROXY_DEBUG_MYSQL_CONNECTION, 5 , " Direct DNS cache record removed. (Hostname:[%s])\n " , hostname.c_str ());
6711+ }
6712+ }
6713+
66896714void MySQL_Monitor::trigger_dns_cache_update () {
66906715 if (GloMyMon) {
66916716 GloMyMon->force_dns_cache_update = true ;
@@ -6714,19 +6739,20 @@ bool DNS_Cache::add(const std::string& hostname, std::vector<std::string>&& ips)
67146739
67156740bool DNS_Cache::add_if_not_exist (const std::string& hostname, std::vector<std::string>&& ips) {
67166741 if (!enabled) return false ;
6717-
6742+ bool item_added = false ;
67186743 int rc = pthread_rwlock_wrlock (&rwlock_);
67196744 assert (rc == 0 );
67206745 if (records.find (hostname) == records.end ()) {
67216746 proxy_debug (PROXY_DEBUG_MYSQL_CONNECTION, 5 , " Updating DNS cache. (Hostname:[%s] IP:[%s])\n " , hostname.c_str (), debug_iplisttostring (ips).c_str ());
67226747 auto & ip_addr = records[hostname];
67236748 ip_addr.ips = std::move (ips);
67246749 __sync_fetch_and_and (&ip_addr.counter , 0 );
6750+ item_added = true ;
67256751 }
67266752 rc = pthread_rwlock_unlock (&rwlock_);
67276753 assert (rc == 0 );
67286754
6729- if (GloMyMon)
6755+ if (item_added && GloMyMon)
67306756 __sync_fetch_and_add (&GloMyMon->dns_cache_record_updated , 1 );
67316757
67326758 return true ;
0 commit comments