From cfaefba2de48a7b2356bec0239e6a0d8d0f52780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Thu, 11 Apr 2019 20:07:12 +1000 Subject: [PATCH] Implementing purging in Monitor Connection Pool --- lib/MySQL_Monitor.cpp | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/MySQL_Monitor.cpp b/lib/MySQL_Monitor.cpp index b5667b5f5..fc45b7e42 100644 --- a/lib/MySQL_Monitor.cpp +++ b/lib/MySQL_Monitor.cpp @@ -159,7 +159,7 @@ private: public: MYSQL * get_connection(char *hostname, int port, MySQL_Monitor_State_Data *mmsd); void put_connection(char *hostname, int port, MYSQL *my); -// void purge_idle_connections(); + void purge_some_connections(); MySQL_Monitor_Connection_Pool() { servers = new PtrArray(); conns = new PtrArray(); @@ -277,6 +277,32 @@ void MySQL_Monitor_Connection_Pool::put_connection(char *hostname, int port, MYS } +void MySQL_Monitor_Connection_Pool::purge_some_connections() { + unsigned long long now = monotonic_time(); + std::lock_guard lock(mutex); + pthread_mutex_lock(&m2); + for (unsigned int i=0; ilen; i++) { + MonMySrvC *srv = (MonMySrvC *)servers->index(i); + while (srv->conns->len > 4) { + MYSQL *my = (MYSQL *)srv->conns->remove_index_fast(0); + MySQL_Monitor_State_Data *mmsd= new MySQL_Monitor_State_Data((char *)"",0,NULL,false); + mmsd->mysql=my; + GloMyMon->queue->add(new WorkItem(mmsd,NULL)); + } + for (unsigned int j=0 ; jconns->len ; j++) { + MYSQL *my = (MYSQL *)srv->conns->index(j); + unsigned long long then = *(unsigned long long*)my->net.buff; + if (now > (then + mysql_thread___monitor_ping_interval*1000 * 10)) { + srv->conns->remove_index_fast(j); + MySQL_Monitor_State_Data *mmsd= new MySQL_Monitor_State_Data((char *)"",0,NULL,false); + mmsd->mysql=my; + GloMyMon->queue->add(new WorkItem(mmsd,NULL)); + } + } + } + pthread_mutex_unlock(&m2); +} + /* void MySQL_Monitor_Connection_Pool::purge_idle_connections() { unsigned long long now = monotonic_time(); @@ -306,6 +332,7 @@ void MySQL_Monitor_Connection_Pool::purge_idle_connections() { } } */ + /* MYSQL * MySQL_Monitor_Connection_Pool::get_connection(char *hostname, int port) { std::lock_guard lock(mutex); @@ -2731,8 +2758,8 @@ __monitor_run: } } monitor_enabled=mysql_thread___monitor_enabled; - if ( rand()%5 == 0) { // purge once in a while - //My_Conn_Pool->purge_idle_connections(); + if ( rand()%10 == 0) { // purge once in a while + My_Conn_Pool->purge_some_connections(); } usleep(200000); int qsize=queue->size();