From 77dbe919741cc3ea3990aef4bfd3298a70ec3a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Tue, 7 Apr 2015 23:58:28 +0000 Subject: [PATCH] Implemented variable mysql-poll_timeout_on_failure (issue #251) --- include/mysql_thread.h | 1 + include/proxysql_structs.h | 2 ++ lib/MySQL_Session.cpp | 10 +++++++++- lib/Standard_MySQL_Thread.cpp | 17 +++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/include/mysql_thread.h b/include/mysql_thread.h index b98ad3c8f..d260f9896 100644 --- a/include/mysql_thread.h +++ b/include/mysql_thread.h @@ -343,6 +343,7 @@ class Standard_MySQL_Threads_Handler: public MySQL_Threads_Handler #endif /* DEBUG */ uint16_t server_capabilities; int poll_timeout; + int poll_timeout_on_failure; } variables; PtrArray *bind_fds; MySQL_Listeners_Manager *MLM; diff --git a/include/proxysql_structs.h b/include/proxysql_structs.h index 5ca2a0bf2..ba1022a7f 100644 --- a/include/proxysql_structs.h +++ b/include/proxysql_structs.h @@ -621,6 +621,7 @@ __thread char *mysql_thread___connect_timeout_server_error; __thread uint16_t mysql_thread___server_capabilities; __thread uint8_t mysql_thread___default_charset; __thread int mysql_thread___poll_timeout; +__thread int mysql_thread___poll_timeout_on_failure; __thread bool mysql_thread___have_compress; __thread bool mysql_thread___servers_stats; #ifdef DEBUG @@ -641,6 +642,7 @@ extern __thread char *mysql_thread___connect_timeout_server_error; extern __thread uint16_t mysql_thread___server_capabilities; extern __thread uint8_t mysql_thread___default_charset; extern __thread int mysql_thread___poll_timeout; +extern __thread int mysql_thread___poll_timeout_on_failure; extern __thread bool mysql_thread___have_compress; extern __thread bool mysql_thread___servers_stats; #ifdef DEBUG diff --git a/lib/MySQL_Session.cpp b/lib/MySQL_Session.cpp index 3e1bfea04..b257086ca 100644 --- a/lib/MySQL_Session.cpp +++ b/lib/MySQL_Session.cpp @@ -1280,7 +1280,15 @@ void MySQL_Session::handler___client_DSS_QUERY_SENT___server_DSS_NOT_INITIALIZED mybe->server_myds->myconn=MyHGM->get_MyConn_from_pool(mybe->hostgroup_id); // } proxy_debug(PROXY_DEBUG_MYSQL_CONNECTION, 5, "Sess=%p -- server_myds=%p -- MySQL_Connection %p\n", this, mybe->server_myds, mybe->server_myds->myconn); - if (mybe->server_myds->myconn==NULL) { return; } + if (mybe->server_myds->myconn==NULL) { + // we couldn't get a connection for whatever reason, ex: no backends, or too busy + if (thread->mypolls.poll_timeout==0) { // tune poll timeout + if (thread->mypolls.poll_timeout > mysql_thread___poll_timeout_on_failure) { + thread->mypolls.poll_timeout = mysql_thread___poll_timeout_on_failure; + } + } + return; + } if (mybe->server_myds->myconn->fd==-1) { // we didn't get a valid connection, we need to create one proxy_debug(PROXY_DEBUG_MYSQL_CONNECTION, 5, "Sess=%p -- MySQL Connection has no FD\n", this); diff --git a/lib/Standard_MySQL_Thread.cpp b/lib/Standard_MySQL_Thread.cpp index 2f07d226e..40053b679 100644 --- a/lib/Standard_MySQL_Thread.cpp +++ b/lib/Standard_MySQL_Thread.cpp @@ -121,6 +121,7 @@ static char * mysql_thread_variables_names[]= { (char *)"ping_timeout_server", (char *)"default_schema", (char *)"poll_timeout", + (char *)"poll_timeout_on_failure", (char *)"server_capabilities", (char *)"server_version", (char *)"servers_stats", @@ -157,6 +158,7 @@ Standard_MySQL_Threads_Handler::Standard_MySQL_Threads_Handler() { variables.server_version=strdup((char *)"5.1.30"); variables.server_capabilities=CLIENT_FOUND_ROWS | CLIENT_PROTOCOL_41 | CLIENT_IGNORE_SIGPIPE | CLIENT_TRANSACTIONS | CLIENT_SECURE_CONNECTION | CLIENT_CONNECT_WITH_DB | CLIENT_SSL; variables.poll_timeout=2000; + variables.poll_timeout_on_failure=100; variables.have_compress=true; variables.servers_stats=true; #ifdef DEBUG @@ -261,6 +263,7 @@ int Standard_MySQL_Threads_Handler::get_variable_int(char *name) { if (!strcmp(name,"have_compress")) return (int)variables.have_compress; if (!strcmp(name,"servers_stats")) return (int)variables.servers_stats; if (!strcmp(name,"poll_timeout")) return variables.poll_timeout; + if (!strcmp(name,"poll_timeout_on_failure")) return variables.poll_timeout_on_failure; if (!strcmp(name,"stacksize")) return ( stacksize ? stacksize : DEFAULT_STACK_SIZE); proxy_error("Not existing variable: %s\n", name); assert(0); return 0; @@ -298,6 +301,10 @@ char * Standard_MySQL_Threads_Handler::get_variable(char *name) { // this is the sprintf(intbuf,"%d",variables.poll_timeout); return strdup(intbuf); } + if (!strcmp(name,"poll_timeout_on_failure")) { + sprintf(intbuf,"%d",variables.poll_timeout_on_failure); + return strdup(intbuf); + } if (!strcmp(name,"threads")) { sprintf(intbuf,"%d", (num_threads ? num_threads : DEFAULT_NUM_THREADS)); return strdup(intbuf); @@ -415,6 +422,15 @@ bool Standard_MySQL_Threads_Handler::set_variable(char *name, char *value) { // return false; } } + if (!strcmp(name,"poll_timeout_on_failure")) { + int intv=atoi(value); + if (intv > 10 && intv < 20000) { + variables.poll_timeout_on_failure=intv; + return true; + } else { + return false; + } + } if (!strcmp(name,"default_charset")) { int intv=atoi(value); if (intv > 0 && intv < 256) { @@ -977,6 +993,7 @@ void Standard_MySQL_Thread::refresh_variables() { mysql_thread___server_capabilities=GloMTH->get_variable_uint16((char *)"server_capabilities"); mysql_thread___default_charset=GloMTH->get_variable_uint8((char *)"default_charset"); mysql_thread___poll_timeout=GloMTH->get_variable_int((char *)"poll_timeout"); + mysql_thread___poll_timeout_on_failure=GloMTH->get_variable_int((char *)"poll_timeout_on_failure"); mysql_thread___have_compress=(bool)GloMTH->get_variable_int((char *)"have_compress"); mysql_thread___servers_stats=(bool)GloMTH->get_variable_int((char *)"servers_stats"); #ifdef DEBUG