@@ -1549,6 +1549,10 @@ bool MySQL_Monitor_State_Data::create_new_connection() {
15491549 myrc=mysql_real_connect (mysql, " localhost" , mysql_thread___monitor_username, mysql_thread___monitor_password, NULL , 0 , hostname, 0 );
15501550 }
15511551 if (myrc==NULL ) {
1552+ // port == 0 means we are connecting to a unix socket
1553+ if (port) {
1554+ MySQL_Monitor::remove_dns_record_from_dns_cache (hostname);
1555+ }
15521556 mysql_error_msg=strdup (mysql_error (mysql));
15531557 int myerrno=mysql_errno (mysql);
15541558 MyHGM->p_update_mysql_error_counter (p_mysql_error_type::proxysql, hostgroup_id, hostname, port, myerrno);
@@ -6653,6 +6657,15 @@ bool MySQL_Monitor::update_dns_cache_from_mysql_conn(const MYSQL* mysql)
66536657 return result;
66546658}
66556659
6660+ void MySQL_Monitor::remove_dns_record_from_dns_cache (const std::string& hostname) {
6661+
6662+ // if IP was provided, no need to update dns cache
6663+ if (hostname.empty () || validate_ip (hostname))
6664+ return ;
6665+
6666+ _remove_dns_record_from_dns_cache (hostname);
6667+ }
6668+
66566669bool MySQL_Monitor::_dns_cache_update (const std::string &hostname, std::vector<std::string>&& ip_address) {
66576670 static thread_local std::shared_ptr<DNS_Cache> dns_cache_thread;
66586671
@@ -6669,6 +6682,18 @@ bool MySQL_Monitor::_dns_cache_update(const std::string &hostname, std::vector<s
66696682 return false ;
66706683}
66716684
6685+ void MySQL_Monitor::_remove_dns_record_from_dns_cache (const std::string& hostname) {
6686+ static thread_local std::shared_ptr<DNS_Cache> dns_cache_thread;
6687+
6688+ if (!dns_cache_thread && GloMyMon)
6689+ dns_cache_thread = GloMyMon->dns_cache ;
6690+
6691+ if (dns_cache_thread) {
6692+ dns_cache_thread->remove (trim (hostname));
6693+ proxy_debug (PROXY_DEBUG_MYSQL_CONNECTION, 5 , " Direct DNS cache record removed. (Hostname:[%s])\n " , hostname.c_str ());
6694+ }
6695+ }
6696+
66726697void MySQL_Monitor::trigger_dns_cache_update () {
66736698 if (GloMyMon) {
66746699 GloMyMon->force_dns_cache_update = true ;
@@ -6697,19 +6722,20 @@ bool DNS_Cache::add(const std::string& hostname, std::vector<std::string>&& ips)
66976722
66986723bool DNS_Cache::add_if_not_exist (const std::string& hostname, std::vector<std::string>&& ips) {
66996724 if (!enabled) return false ;
6700-
6725+ bool item_added = false ;
67016726 int rc = pthread_rwlock_wrlock (&rwlock_);
67026727 assert (rc == 0 );
67036728 if (records.find (hostname) == records.end ()) {
67046729 proxy_debug (PROXY_DEBUG_MYSQL_CONNECTION, 5 , " Updating DNS cache. (Hostname:[%s] IP:[%s])\n " , hostname.c_str (), debug_iplisttostring (ips).c_str ());
67056730 auto & ip_addr = records[hostname];
67066731 ip_addr.ips = std::move (ips);
67076732 __sync_fetch_and_and (&ip_addr.counter , 0 );
6733+ item_added = true ;
67086734 }
67096735 rc = pthread_rwlock_unlock (&rwlock_);
67106736 assert (rc == 0 );
67116737
6712- if (GloMyMon)
6738+ if (item_added && GloMyMon)
67136739 __sync_fetch_and_add (&GloMyMon->dns_cache_record_updated , 1 );
67146740
67156741 return true ;
0 commit comments