diff --git a/include/MySQL_Thread.h b/include/MySQL_Thread.h index c69fb53f1..fe959026c 100644 --- a/include/MySQL_Thread.h +++ b/include/MySQL_Thread.h @@ -293,6 +293,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 efa51eed3..4179c7e24 100644 --- a/include/proxysql_structs.h +++ b/include/proxysql_structs.h @@ -710,6 +710,7 @@ __thread unsigned int mysql_thread___default_charset; __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; @@ -865,6 +866,7 @@ extern __thread unsigned int mysql_thread___default_charset; 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 38c4fcb62..c6b681287 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -2684,7 +2684,15 @@ 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; + 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) { i=rand()%l; } else { diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index e97132a0a..90a3fa708 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -354,6 +354,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 @@ -564,6 +565,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; @@ -983,6 +985,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; @@ -1636,6 +1639,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")); } @@ -3145,6 +3151,17 @@ bool MySQL_Threads_Handler::set_variable(char *name, char *value) { // this is t } 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) { @@ -4797,6 +4814,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");