From 29142e6e4f3261945b48ed4059a7f8873a0596e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Sat, 4 Apr 2020 00:40:25 +0200 Subject: [PATCH 1/2] Global connection warming support Port of PR #2496 to 2.0.11 --- include/MySQL_Thread.h | 1 + include/proxysql_structs.h | 2 ++ lib/MySQL_HostGroups_Manager.cpp | 8 +++++++- lib/MySQL_Thread.cpp | 18 ++++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/include/MySQL_Thread.h b/include/MySQL_Thread.h index 96b968da7..116305d63 100644 --- a/include/MySQL_Thread.h +++ b/include/MySQL_Thread.h @@ -284,6 +284,7 @@ class MySQL_Threads_Handler int shun_recovery_time_sec; int query_retries_on_failure; bool client_multi_statements; + bool connection_warming; int connect_retries_on_failure; int connect_retries_delay; int connection_delay_multiplex_ms; diff --git a/include/proxysql_structs.h b/include/proxysql_structs.h index b6b303be7..22a8aa196 100644 --- a/include/proxysql_structs.h +++ b/include/proxysql_structs.h @@ -740,6 +740,7 @@ __thread int mysql_thread___auto_increment_delay_multiplex; __thread unsigned int mysql_thread___handle_unknown_charset; __thread int mysql_thread___poll_timeout; __thread int mysql_thread___poll_timeout_on_failure; +__thread bool mysql_thread___connection_warming; __thread bool mysql_thread___have_compress; __thread bool mysql_thread___have_ssl; __thread bool mysql_thread___client_found_rows; @@ -883,6 +884,7 @@ extern __thread int mysql_thread___auto_increment_delay_multiplex; extern __thread unsigned int mysql_thread___handle_unknown_charset; extern __thread int mysql_thread___poll_timeout; extern __thread int mysql_thread___poll_timeout_on_failure; +extern __thread bool mysql_thread___connection_warming; extern __thread bool mysql_thread___have_compress; extern __thread bool mysql_thread___have_ssl; extern __thread bool mysql_thread___client_found_rows; diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index 46228d129..178eb066c 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -2684,7 +2684,13 @@ MySQL_Connection * MySrvConnList::get_random_MyConn(MySQL_Session *sess, bool ff MySQL_Connection * conn=NULL; unsigned int i; unsigned int l=conns_length(); - if (l && ff==false) { + bool needs_warming = false; + unsigned int total_connections = mysrvc->ConnectionsFree->conns_length()+mysrvc->ConnectionsUsed->conns_length(); + unsigned int expected_warm_connections = mysql_thread___free_connections_pct*mysrvc->max_connections/100; + if (mysql_thread___connection_warming && (total_connections < expected_warm_connections)) { + needs_warming = true; + } + if (l && ff==false && needs_warming==false) { if (l>32768) { i=rand()%l; } else { diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 7f5e3c63e..ecc690154 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -368,6 +368,7 @@ static char * mysql_thread_variables_names[]= { (char *)"default_charset", (char *)"handle_unknown_charset", (char *)"free_connections_pct", + (char *)"connection_warming", #ifdef IDLE_THREADS (char *)"session_idle_ms", #endif // IDLE_THREADS @@ -566,6 +567,7 @@ MySQL_Threads_Handler::MySQL_Threads_Handler() { variables.wait_timeout=8*3600*1000; variables.throttle_max_bytes_per_second_to_client=0; variables.throttle_ratio_server_to_client=0; + variables.connection_warming=false; variables.max_connections=10*1000; variables.max_stmts_per_connection=20; variables.max_stmts_cache=10000; @@ -912,6 +914,7 @@ int MySQL_Threads_Handler::get_variable_int(const char *name) { if (!strcmp(name,"connect_timeout_server_max")) return (int)variables.connect_timeout_server_max; if (!strcmp(name,"connection_delay_multiplex_ms")) return (int)variables.connection_delay_multiplex_ms; if (!strcmp(name,"connection_max_age_ms")) return (int)variables.connection_max_age_ms; + if (!strcmp(name,"connection_warming")) return (int)variables.connection_warming; if (!strcmp(name,"connpoll_reset_queue_length")) return (int)variables.connpoll_reset_queue_length; } break; @@ -1490,6 +1493,9 @@ char * MySQL_Threads_Handler::get_variable(char *name) { // this is the public f if (!strcasecmp(name,"forward_autocommit")) { return strdup((variables.forward_autocommit ? "true" : "false")); } + if (!strcasecmp(name,"connection_warming")) { + return strdup((variables.connection_warming ? "true" : "false")); + } if (!strcasecmp(name,"enforce_autocommit_on_reads")) { return strdup((variables.enforce_autocommit_on_reads ? "true" : "false")); } @@ -2847,6 +2853,17 @@ bool MySQL_Threads_Handler::set_variable(char *name, const char *value) { // thi } return false; } + if (!strcasecmp(name,"connection_warming")) { + if (strcasecmp(value,"true")==0 || strcasecmp(value,"1")==0) { + variables.connection_warming=true; + return true; + } + if (strcasecmp(value,"false")==0 || strcasecmp(value,"0")==0) { + variables.connection_warming=false; + return true; + } + return false; + } #ifdef IDLE_THREADS if (!strcasecmp(name,"session_idle_show_processlist")) { if (strcasecmp(value,"true")==0 || strcasecmp(value,"1")==0) { @@ -4439,6 +4456,7 @@ void MySQL_Thread::refresh_variables() { mysql_thread___client_found_rows=(bool)GloMTH->get_variable_int((char *)"client_found_rows"); mysql_thread___multiplexing=(bool)GloMTH->get_variable_int((char *)"multiplexing"); mysql_thread___log_unhealthy_connections=(bool)GloMTH->get_variable_int((char *)"log_unhealthy_connections"); + mysql_thread___connection_warming=(bool)GloMTH->get_variable_int((char*)"connection_warming"); mysql_thread___forward_autocommit=(bool)GloMTH->get_variable_int((char *)"forward_autocommit"); mysql_thread___enforce_autocommit_on_reads=(bool)GloMTH->get_variable_int((char *)"enforce_autocommit_on_reads"); mysql_thread___autocommit_false_not_reusable=(bool)GloMTH->get_variable_int((char *)"autocommit_false_not_reusable"); From 95e62b0b4b9cf0fb2861a9c10688a68e1f54b4a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Sat, 4 Apr 2020 00:42:57 +0200 Subject: [PATCH 2/2] Minor opimization on previous commit --- lib/MySQL_HostGroups_Manager.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index 178eb066c..cce7f218e 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -2685,10 +2685,12 @@ MySQL_Connection * MySrvConnList::get_random_MyConn(MySQL_Session *sess, bool ff unsigned int i; unsigned int l=conns_length(); bool needs_warming = false; - unsigned int total_connections = mysrvc->ConnectionsFree->conns_length()+mysrvc->ConnectionsUsed->conns_length(); - unsigned int expected_warm_connections = mysql_thread___free_connections_pct*mysrvc->max_connections/100; - if (mysql_thread___connection_warming && (total_connections < expected_warm_connections)) { - needs_warming = true; + if (mysql_thread___connection_warming) { + unsigned int total_connections = mysrvc->ConnectionsFree->conns_length()+mysrvc->ConnectionsUsed->conns_length(); + unsigned int expected_warm_connections = mysql_thread___free_connections_pct*mysrvc->max_connections/100; + if (total_connections < expected_warm_connections) { + needs_warming = true; + } } if (l && ff==false && needs_warming==false) { if (l>32768) {