From 12e1223125787f64610a7b8230c0ea554186d2c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Sat, 27 Aug 2016 00:13:55 +0000 Subject: [PATCH 1/2] Local connection cache #644 This is an attempt to improve scalability adding a local connection cache per thread --- include/MySQL_HostGroups_Manager.h | 3 +- include/MySQL_Thread.h | 4 +++ lib/MySQL_HostGroups_Manager.cpp | 21 +++++++++--- lib/MySQL_Session.cpp | 6 +++- lib/MySQL_Thread.cpp | 53 +++++++++++++++++++++++++++++- lib/mysql_data_stream.cpp | 3 +- 6 files changed, 82 insertions(+), 8 deletions(-) diff --git a/include/MySQL_HostGroups_Manager.h b/include/MySQL_HostGroups_Manager.h index 26d549e21..10f5580a8 100644 --- a/include/MySQL_HostGroups_Manager.h +++ b/include/MySQL_HostGroups_Manager.h @@ -154,7 +154,8 @@ class MySQL_HostGroups_Manager { int get_multiple_idle_connections(int, unsigned long long, MySQL_Connection **, int); SQLite3_result * SQL3_Connection_Pool(); - void push_MyConn_to_pool(MySQL_Connection *); + void push_MyConn_to_pool(MySQL_Connection *, bool _lock=true); + void push_MyConn_to_pool_array(MySQL_Connection **); void destroy_MyConn_from_pool(MySQL_Connection *); void replication_lag_action(int, char*, unsigned int, int); diff --git a/include/MySQL_Thread.h b/include/MySQL_Thread.h index 91aea0e05..85a9beb6b 100644 --- a/include/MySQL_Thread.h +++ b/include/MySQL_Thread.h @@ -143,6 +143,7 @@ class MySQL_Thread bool processing_idles; bool maintenance_loop; + PtrArray *cached_connections; protected: int nfds; @@ -195,6 +196,9 @@ class MySQL_Thread //void myds_backend_first_packet_after_connect(MySQL_Data_Stream *myds, unsigned int n); void listener_handle_new_connection(MySQL_Data_Stream *myds, unsigned int n); void Get_Memory_Stats(); + MySQL_Connection * get_MyConn_local(unsigned int); + void push_MyConn_local(MySQL_Connection *); + void return_local_connections(); }; diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index d18614dcb..25afd6def 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -613,7 +613,7 @@ MySrvC * MyHGC::MySrvC_lookup_with_coordinates(MySQL_Connection *c) { */ //MyHGC * MySQL_HostGroups_Manager::MyConn_add_to_pool(MySQL_Connection *c, int _hid) { -void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c) { +void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c, bool _lock) { assert(c->parent); MySrvC *mysrvc=NULL; // if (c->parent) { @@ -622,7 +622,8 @@ void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c) { // MyHGC=MyHGC_lookup(_hid); // MySrvC=MyHGC->MySrvC_lookup_with_coordinates(c); // } - wrlock(); + if (_lock) + wrlock(); status.myconnpoll_push++; mysrvc=(MySrvC *)c->parent; proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySQL_Connection %p, server %s:%d with status %d\n", c, mysrvc->address, mysrvc->port, mysrvc->status); @@ -645,10 +646,22 @@ void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c) { delete c; } __exit_push_MyConn_to_pool: - wrunlock(); + if (_lock) + wrunlock(); } - +void MySQL_HostGroups_Manager::push_MyConn_to_pool_array(MySQL_Connection **ca) { + unsigned int i=0; + MySQL_Connection *c=NULL; + c=ca[i]; + wrlock(); + while (c) { + push_MyConn_to_pool(c,false); + i++; + c=ca[i]; + } + wrunlock(); +} MySrvC *MyHGC::get_random_MySrvC() { MySrvC *mysrvc=NULL; diff --git a/lib/MySQL_Session.cpp b/lib/MySQL_Session.cpp index 8c8837411..e5149efc2 100644 --- a/lib/MySQL_Session.cpp +++ b/lib/MySQL_Session.cpp @@ -2123,7 +2123,11 @@ void MySQL_Session::handler___client_DSS_QUERY_SENT___server_DSS_NOT_INITIALIZED // Get a MySQL Connection // if (rand()%3==0) { - MySQL_Connection *mc=MyHGM->get_MyConn_from_pool(mybe->hostgroup_id); + MySQL_Connection *mc=NULL; + mc=thread->get_MyConn_local(mybe->hostgroup_id); // experimental , #644 + if (mc==NULL) { + mc=MyHGM->get_MyConn_from_pool(mybe->hostgroup_id); + } if (mc) { mybe->server_myds->attach_connection(mc); } diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 45c55886c..1a686bea9 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -1521,6 +1521,15 @@ MySQL_Thread::~MySQL_Thread() { } delete mysql_sessions; } + + if (cached_connections) { + while(cached_connections->len) { + MySQL_Connection *c=(MySQL_Connection *)cached_connections->remove_index_fast(0); + delete c; + } + delete cached_connections; + } + unsigned int i; for (i=0;ilen; i++) { + c=(MySQL_Connection *)cached_connections->index(i); + if (c->parent->myhgc->hid==_hid) { + c=(MySQL_Connection *)cached_connections->remove_index_fast(i); + return c; + } + } + return NULL; +} + +void MySQL_Thread::push_MyConn_local(MySQL_Connection *c) { + MySrvC *mysrvc=NULL; + mysrvc=(MySrvC *)c->parent; + if (mysrvc->status==MYSQL_SERVER_STATUS_ONLINE) { + if (c->async_state_machine==ASYNC_IDLE) { + cached_connections->add(c); + return; // all went well + } + } + MyHGM->push_MyConn_to_pool(c); +} + +void MySQL_Thread::return_local_connections() { + if (cached_connections->len==0) { + return; + } + MySQL_Connection **ca=(MySQL_Connection **)malloc(sizeof(MySQL_Connection *)*(cached_connections->len+1)); + unsigned int i=0; + while (cached_connections->len) { + ca[i]=(MySQL_Connection *)cached_connections->remove_index_fast(0); + i++; + } + ca[i]=NULL; + MyHGM->push_MyConn_to_pool_array(ca); + free(ca); +} diff --git a/lib/mysql_data_stream.cpp b/lib/mysql_data_stream.cpp index 1f3333cc5..dd26ca89f 100644 --- a/lib/mysql_data_stream.cpp +++ b/lib/mysql_data_stream.cpp @@ -969,7 +969,8 @@ void MySQL_Data_Stream::return_MySQL_Connection_To_Pool() { detach_connection(); unplug_backend(); //mc->async_state_machine=ASYNC_IDLE; - MyHGM->push_MyConn_to_pool(mc); + sess->thread->push_MyConn_local(mc); + //MyHGM->push_MyConn_to_pool(mc); // #644 } } From fb12719ba7446699a325101a628afb88f96f079d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Sat, 27 Aug 2016 01:05:53 +0000 Subject: [PATCH 2/2] Quick return from MySQL_Logger::flush_log() #644 --- lib/MySQL_Logger.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/MySQL_Logger.cpp b/lib/MySQL_Logger.cpp index 327648d3d..3051c1cb4 100644 --- a/lib/MySQL_Logger.cpp +++ b/lib/MySQL_Logger.cpp @@ -170,6 +170,7 @@ void MySQL_Logger::wrunlock() { }; void MySQL_Logger::flush_log() { + if (enabled==false) return; wrlock(); flush_log_unlocked(); wrunlock();