diff --git a/include/proxysql_config.h b/include/proxysql_config.h index bc095b76c..1a98cce35 100644 --- a/include/proxysql_config.h +++ b/include/proxysql_config.h @@ -7,8 +7,8 @@ class SQLite3DB; extern const char* config_header; class ProxySQL_Config { - SQLite3DB* admindb; public: + SQLite3DB* admindb; ProxySQL_Config(SQLite3DB* db); virtual ~ProxySQL_Config(); diff --git a/include/proxysql_glovars.hpp b/include/proxysql_glovars.hpp index 097708c0b..6b8b299f4 100644 --- a/include/proxysql_glovars.hpp +++ b/include/proxysql_glovars.hpp @@ -92,6 +92,7 @@ class ProxySQL_GlobalVariables { #ifdef PROXYSQLCLICKHOUSE bool clickhouse_server; #endif /* PROXYSQLCLICKHOUSE */ + pthread_mutex_t ext_glomth_mutex; } global; struct mysql { char *server_version; diff --git a/include/proxysql_restapi.h b/include/proxysql_restapi.h index 9660a1264..b1de2bd82 100644 --- a/include/proxysql_restapi.h +++ b/include/proxysql_restapi.h @@ -22,8 +22,8 @@ public: }; class ProxySQL_Restapi { - SQLite3DB* admindb; public: + SQLite3DB* admindb; ProxySQL_Restapi(SQLite3DB* db); virtual ~ProxySQL_Restapi(); diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 5675e1feb..d7abc6e7d 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -4733,6 +4733,7 @@ void MySQL_Thread::process_all_sessions() { } void MySQL_Thread::refresh_variables() { + pthread_mutex_lock(&GloVars.global.ext_glomth_mutex); if (GloMTH==NULL) { return; } @@ -4912,6 +4913,7 @@ void MySQL_Thread::refresh_variables() { mysql_thread___session_debug=(bool)GloMTH->get_variable_int((char *)"session_debug"); #endif /* DEBUG */ GloMTH->wrunlock(); + pthread_mutex_unlock(&GloVars.global.ext_glomth_mutex); } MySQL_Thread::MySQL_Thread() { diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index 0dd2d40c0..b228af85c 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -778,11 +778,17 @@ int ProxySQL_Test___GetDigestTable(bool reset, bool use_swap) { ProxySQL_Config& ProxySQL_Admin::proxysql_config() { static ProxySQL_Config instance = ProxySQL_Config(admindb); + if (instance.admindb != admindb) { + instance.admindb = admindb; + } return instance; } ProxySQL_Restapi& ProxySQL_Admin::proxysql_restapi() { static ProxySQL_Restapi instance = ProxySQL_Restapi(admindb); + if (instance.admindb != admindb) { + instance.admindb = admindb; + } return instance; } @@ -1200,7 +1206,13 @@ bool admin_handler_command_proxysql(char *query_no_space, unsigned int query_no_ rc=__sync_bool_compare_and_swap(&GloVars.global.nostart,1,0); } if (rc) { + // Set the status variable 'threads_initialized' to 0 because it's initialized back + // in main 'init_phase3'. After GloMTH have been initialized again. + __sync_bool_compare_and_swap(&GloMTH->status_variables.threads_initialized, 1, 0); proxy_debug(PROXY_DEBUG_ADMIN, 4, "Starting ProxySQL following PROXYSQL START command\n"); + while(__sync_fetch_and_add(&GloMTH->status_variables.threads_initialized, 0) == 1) { + usleep(1000); + } SPA->send_MySQL_OK(&sess->client_myds->myprot, NULL); } else { proxy_warning("ProxySQL was already started when received PROXYSQL START command\n"); @@ -1231,6 +1243,17 @@ bool admin_handler_command_proxysql(char *query_no_space, unsigned int query_no_ GloMTH->commit(); glovars.reload=2; __sync_bool_compare_and_swap(&glovars.shutdown,0,1); + // After setting the shutdown flag, we should wake all threads and wait for + // the shutdown phase to complete. + GloMTH->signal_all_threads(0); + while (__sync_fetch_and_add(&glovars.shutdown,0)==1) { + usleep(1000); + } + // After shutdown phase is completed, we must to send a 'OK' to the + // mysql client, otherwise, since this session might not be drop due + // to the waiting condition, the client wont disconnect and will + // keep forever waiting for acknowledgement. + SPA->send_MySQL_OK(&sess->client_myds->myprot, NULL); return false; } @@ -4605,6 +4628,7 @@ void ProxySQL_Admin::vacuum_stats(bool is_admin) { void *child_mysql(void *arg) { + if (GloMTH == nullptr) { return NULL; } pthread_attr_t thread_attr; size_t tmp_stack_size=0; diff --git a/lib/ProxySQL_GloVars.cpp b/lib/ProxySQL_GloVars.cpp index 44570d7ad..323e7949b 100644 --- a/lib/ProxySQL_GloVars.cpp +++ b/lib/ProxySQL_GloVars.cpp @@ -100,6 +100,7 @@ ProxySQL_GlobalVariables::ProxySQL_GlobalVariables() : // global.use_proxysql_mem=false; pthread_mutex_init(&global.start_mutex,NULL); pthread_mutex_init(&checksum_mutex,NULL); + pthread_mutex_init(&global.ext_glomth_mutex,NULL); epoch_version = 0; checksums_values.updates_cnt = 0; checksums_values.dumped_at = 0; diff --git a/src/main.cpp b/src/main.cpp index 0e1427878..59c139831 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -766,6 +766,7 @@ void * mysql_worker_thread_func(void *arg) { worker->run(); //delete worker; delete worker; + mysql_thread->worker=NULL; // l_mem_destroy(__thr_sfp); __sync_fetch_and_sub(&GloVars.statuses.stack_memory_mysql_threads,tmp_stack_size); return NULL; @@ -1172,8 +1173,10 @@ void ProxySQL_Main_shutdown_all_modules() { } if (GloMTH) { cpu_timer t; + pthread_mutex_lock(&GloVars.global.ext_glomth_mutex); delete GloMTH; GloMTH=NULL; + pthread_mutex_unlock(&GloVars.global.ext_glomth_mutex); #ifdef DEBUG std::cerr << "GloMTH shutdown in "; #endif