From 7a177ac934b849c1b9fa05ef4f6e93102a1ea55c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Wed, 7 Jan 2015 08:59:17 +0000 Subject: [PATCH] Issue #121 Calls from Standard_ProxySQL_Admin to MySQL_Threads_Handler are wrapped by lock LOAD MYSQL VARIABLES TO RUNTIME (or equivalent) calls commit() so that other threads will update --- include/mysql_thread.h | 1 + lib/Standard_MySQL_Thread.cpp | 6 ++++++ lib/Standard_ProxySQL_Admin.cpp | 10 ++++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/include/mysql_thread.h b/include/mysql_thread.h index 5d1893cdc..7b744de76 100644 --- a/include/mysql_thread.h +++ b/include/mysql_thread.h @@ -258,6 +258,7 @@ class MySQL_Threads_Handler virtual void shutdown_threads() {}; virtual void wrlock() {}; virtual void wrunlock() {}; + virtual void commit() {}; virtual char *get_variable(char *name) {return NULL;}; virtual bool set_variable(char *name, char *value) {return false;}; virtual char **get_variables_list() {return NULL;} diff --git a/lib/Standard_MySQL_Thread.cpp b/lib/Standard_MySQL_Thread.cpp index 635fbec8e..96499b739 100644 --- a/lib/Standard_MySQL_Thread.cpp +++ b/lib/Standard_MySQL_Thread.cpp @@ -220,6 +220,12 @@ class Standard_MySQL_Threads_Handler: public MySQL_Threads_Handler virtual void wrunlock() { spin_wrunlock(&rwlock); } + + virtual void commit() { + __sync_add_and_fetch(&__global_MySQL_Thread_Variables_version,1); + proxy_debug(PROXY_DEBUG_MYSQL_SERVER, 1, "Increasing version number to %d - all threads will notice this and refresh their variables\n", __global_MySQL_Thread_Variables_version); + }; + char *get_variable_string(char *name) { if (!strcmp(name,"server_version")) return strdup(variables.server_version); if (!strcmp(name,"default_schema")) return strdup(variables.default_schema); diff --git a/lib/Standard_ProxySQL_Admin.cpp b/lib/Standard_ProxySQL_Admin.cpp index 5ad3a39f6..dea8908de 100644 --- a/lib/Standard_ProxySQL_Admin.cpp +++ b/lib/Standard_ProxySQL_Admin.cpp @@ -708,6 +708,7 @@ void *child_mysql(void *arg) { int client = *(int *)arg; __thr_sfp=l_mem_init(); + GloMTH->wrlock(); mysql_thread___server_version=GloMTH->get_variable((char *)"server_version"); mysql_thread___default_schema=GloMTH->get_variable((char *)"default_schema"); { @@ -715,6 +716,7 @@ void *child_mysql(void *arg) { mysql_thread___server_capabilities=atoi(s); free(s); } + GloMTH->wrunlock(); struct pollfd fds[1]; nfds_t nfds=1; @@ -1150,6 +1152,7 @@ void Standard_ProxySQL_Admin::flush_mysql_variables___database_to_runtime(SQLite proxy_error("Error on %s : %s\n", q, error); return; } else { + GloMTH->wrlock(); for (std::vector::iterator it = resultset->rows.begin() ; it != resultset->rows.end(); ++it) { SQLite3_row *r=*it; bool rc=GloMTH->set_variable(r->fields[0],r->fields[1]); @@ -1173,6 +1176,8 @@ void Standard_ProxySQL_Admin::flush_mysql_variables___database_to_runtime(SQLite proxy_debug(PROXY_DEBUG_ADMIN, 4, "Set variable %s with value \"%s\"\n", r->fields[0],r->fields[1]); } } + GloMTH->commit(); + GloMTH->wrunlock(); } if (resultset) delete resultset; } @@ -1213,15 +1218,16 @@ void Standard_ProxySQL_Admin::flush_mysql_variables___runtime_to_database(SQLite a=(char *)"INSERT OR IGNORE INTO global_variables(variable_name, variable_value) VALUES(\"mysql-%s\",\"%s\")"; } int l=strlen(a)+200; + GloMTH->wrlock(); char **varnames=GloMTH->get_variables_list(); - // FIXME: add lock char *query=(char *)malloc(l); for (int i=0; varnames[i]; i++) { char *val=GloMTH->get_variable(varnames[i]); sprintf(query, a, varnames[i], val); db->execute(query); free(val); - } + } + GloMTH->wrunlock(); free(query); for (int i=0; varnames[i]; i++) { free(varnames[i]);