From 357ce44bed249a8f5058c52b385476e5a0efea2c Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Mon, 5 Dec 2022 19:10:11 +0500 Subject: [PATCH 1/3] Purge all backend connections when monitor is disabled --- lib/MySQL_Monitor.cpp | 47 ++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/lib/MySQL_Monitor.cpp b/lib/MySQL_Monitor.cpp index a826bc8ec..8cf3a10d2 100644 --- a/lib/MySQL_Monitor.cpp +++ b/lib/MySQL_Monitor.cpp @@ -178,6 +178,7 @@ public: MYSQL * get_connection(char *hostname, int port, MySQL_Monitor_State_Data *mmsd); void put_connection(char *hostname, int port, MYSQL *my); void purge_some_connections(); + void purge_all_connections(); MySQL_Monitor_Connection_Pool() { servers = std::unique_ptr(new PtrArray()); #ifdef DEBUG @@ -186,15 +187,10 @@ public: #endif // DEBUG }; ~MySQL_Monitor_Connection_Pool() { - if (servers) { - while(servers->len) { - MonMySrvC *srv = static_cast(servers->index(0)); - if (srv) { - delete srv; - } - servers->remove_index_fast(0); - } - } + purge_all_connections(); +#ifdef DEBUG + pthread_mutex_destroy(&m2); +#endif // DEBUG } void conn_register(MySQL_Monitor_State_Data *mmsd) { #ifdef DEBUG @@ -248,6 +244,26 @@ __conn_register_label: }; }; +void MySQL_Monitor_Connection_Pool::purge_all_connections() { + std::lock_guard lock(mutex); +#ifdef DEBUG + pthread_mutex_lock(&m2); +#endif + if (servers) { + while (servers->len) { + MonMySrvC* srv = static_cast(servers->index(0)); + if (srv) { + delete srv; + } + servers->remove_index_fast(0); + } + } +#ifdef DEBUG + conns->reset(); + pthread_mutex_unlock(&m2); +#endif +} + MYSQL * MySQL_Monitor_Connection_Pool::get_connection(char *hostname, int port, MySQL_Monitor_State_Data *mmsd) { std::lock_guard lock(mutex); #ifdef DEBUG @@ -278,11 +294,6 @@ MYSQL * MySQL_Monitor_Connection_Pool::get_connection(char *hostname, int port, assert(my!=my1); assert(my->net.fd!=my1->net.fd); } - for (unsigned int l=0; llen; l++) { - MYSQL *my1 = (MYSQL *)conns->index(l); - assert(my!=my1); - assert(my->net.fd!=my1->net.fd); - } //proxy_info("Registering MYSQL with FD %d from mmsd %p and MYSQL %p\n", my->net.fd, mmsd, my); conns->add(my); #endif // DEBUG @@ -3527,6 +3538,9 @@ __monitor_run: pthread_join(monitor_galera_thread,NULL); pthread_join(monitor_aws_aurora_thread,NULL); pthread_join(monitor_replication_lag_thread,NULL); + + bool purged_all_connections = false; + while (shutdown==false) { unsigned int glover; if (GloMTH) { @@ -3540,6 +3554,11 @@ __monitor_run: if (mysql_thread___monitor_enabled==true) { goto __monitor_run; } + else if (purged_all_connections == false) { + My_Conn_Pool->purge_all_connections(); + purged_all_connections = true; + } + usleep(200000); } if (mysql_thr) { From ee0059a65c5c53a35390c1807c250703cf84464d Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Tue, 6 Dec 2022 12:02:28 +0500 Subject: [PATCH 2/3] Purge all backend connections when monitor is disabled --- lib/MySQL_Monitor.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/MySQL_Monitor.cpp b/lib/MySQL_Monitor.cpp index 8cf3a10d2..78f9016bc 100644 --- a/lib/MySQL_Monitor.cpp +++ b/lib/MySQL_Monitor.cpp @@ -3539,7 +3539,7 @@ __monitor_run: pthread_join(monitor_aws_aurora_thread,NULL); pthread_join(monitor_replication_lag_thread,NULL); - bool purged_all_connections = false; + My_Conn_Pool->purge_all_connections(); while (shutdown==false) { unsigned int glover; @@ -3554,10 +3554,6 @@ __monitor_run: if (mysql_thread___monitor_enabled==true) { goto __monitor_run; } - else if (purged_all_connections == false) { - My_Conn_Pool->purge_all_connections(); - purged_all_connections = true; - } usleep(200000); } From 29677cb983a5c0cdf6a04196c43afc21f19c810b Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Thu, 8 Dec 2022 17:09:56 +0500 Subject: [PATCH 3/3] Check and remove connection from monitor connection pool if not used for long time. --- lib/MySQL_Monitor.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/MySQL_Monitor.cpp b/lib/MySQL_Monitor.cpp index 78f9016bc..b4aea22ff 100644 --- a/lib/MySQL_Monitor.cpp +++ b/lib/MySQL_Monitor.cpp @@ -270,6 +270,7 @@ MYSQL * MySQL_Monitor_Connection_Pool::get_connection(char *hostname, int port, pthread_mutex_lock(&m2); #endif // DEBUG MYSQL *my = NULL; + unsigned long long now = monotonic_time(); for (unsigned int i=0; ilen; i++) { MonMySrvC *srv = (MonMySrvC *)servers->index(i); if (srv->port == port && strcmp(hostname,srv->address)==0) { @@ -286,8 +287,24 @@ MYSQL * MySQL_Monitor_Connection_Pool::get_connection(char *hostname, int port, } } #endif // DEBUG - unsigned int idx = rand()%srv->conns->len; - my = (MYSQL *)srv->conns->remove_index_fast(idx); + while (srv->conns->len) { + unsigned int idx = rand() % srv->conns->len; + MYSQL* mysql = (MYSQL*)srv->conns->remove_index_fast(idx); + + if (!mysql) continue; + + // close connection if not used for a while + unsigned long long then = *(unsigned long long*)mysql->net.buff; + if (now > (then + mysql_thread___monitor_ping_interval * 1000 * 10)) { + MySQL_Monitor_State_Data* mmsd = new MySQL_Monitor_State_Data((char*)"", 0, NULL, false); + mmsd->mysql = mysql; + GloMyMon->queue->add(new WorkItem(mmsd, NULL)); + continue; + } + + my = mysql; + break; + } #ifdef DEBUG for (unsigned int j=0; jlen; j++) { MYSQL *my1 = (MYSQL *)conns->index(j); @@ -295,7 +312,8 @@ MYSQL * MySQL_Monitor_Connection_Pool::get_connection(char *hostname, int port, assert(my->net.fd!=my1->net.fd); } //proxy_info("Registering MYSQL with FD %d from mmsd %p and MYSQL %p\n", my->net.fd, mmsd, my); - conns->add(my); + if (my) + conns->add(my); #endif // DEBUG } #ifdef DEBUG