From 8c92f9292a2878b4666885dd85afde15f52e04fe Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Tue, 23 Apr 2024 11:40:32 +0500 Subject: [PATCH 1/9] A hostgroup can now be configured to have a maximum number of online servers simultaneously. Once this limit is reached, any new connections attempting to access the connection pool will be denied, and existing connections will gradually be reassigned to the available connection pool until the number of online servers is reduced to meet or fall below the specified maximum. --- include/MySQL_HostGroups_Manager.h | 35 +++++++++- lib/MySQL_HostGroups_Manager.cpp | 108 ++++++++++++++++++++++------- lib/MySQL_Session.cpp | 18 +++-- lib/mysql_connection.cpp | 4 +- 4 files changed, 132 insertions(+), 33 deletions(-) diff --git a/include/MySQL_HostGroups_Manager.h b/include/MySQL_HostGroups_Manager.h index f01a030ee2..e27755aedb 100644 --- a/include/MySQL_HostGroups_Manager.h +++ b/include/MySQL_HostGroups_Manager.h @@ -190,6 +190,29 @@ class MySrvConnList { MySQL_Connection *index(unsigned int); }; +class MySrvC; +class MySrvStatus { +public: + MySrvStatus(MySrvC* _mysrvc); + ~MySrvStatus(); + + enum MySerStatus operator=(enum MySerStatus _status); + + inline + operator enum MySerStatus() const { + return status; + } + + inline + bool operator==(enum MySerStatus _status) const { + return status == _status; + } + +private: + MySrvC *mysrvc; + enum MySerStatus status; +}; + class MySrvC { // MySQL Server Container public: MyHGC *myhgc; @@ -198,7 +221,7 @@ class MySrvC { // MySQL Server Container uint16_t gtid_port; uint16_t flags; int64_t weight; - enum MySerStatus status; + MySrvStatus status; unsigned int compression; int64_t max_connections; unsigned int aws_aurora_current_lag_us; @@ -279,6 +302,8 @@ class MySrvList { // MySQL Server List class MyHGC { // MySQL Host Group Container public: unsigned int hid; + std::atomic num_online_servers; + unsigned long long last_log_time_num_online_servers; unsigned long long current_time_now; uint32_t new_connections_now; MySrvList *mysrvs; @@ -310,6 +335,13 @@ class MyHGC { // MySQL Host Group Container MyHGC(int); ~MyHGC(); MySrvC *get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_lag_ms, MySQL_Session *sess); + void refresh_online_server_count(); + void log_num_online_server_count_error(); + inline + bool online_servers_within_threshold() const { + if (num_online_servers.load(std::memory_order_relaxed) <= attributes.max_num_online_servers) return true; + return false; + } }; class Group_Replication_Info { @@ -927,6 +959,7 @@ class MySQL_HostGroups_Manager { void init(); void wrlock(); void wrunlock(); + bool is_locked = false; // will be removed after testing int servers_add(SQLite3_result *resultset); /** * @brief Generates a new global checksum for module 'mysql_servers_v2' using the provided hash. diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index b185416af8..216aba8aa1 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -703,6 +703,7 @@ void MySrvList::add(MySrvC *s) { s->myhgc=myhgc; } servers->add(s); + myhgc->refresh_online_server_count(); } @@ -720,6 +721,7 @@ void MySrvList::remove(MySrvC *s) { int i=find_idx(s); assert(i>=0); servers->remove_index_fast((unsigned int)i); + myhgc->refresh_online_server_count(); } void MySrvConnList::drop_all_connections() { @@ -735,12 +737,11 @@ MySrvC::MySrvC( char* add, uint16_t p, uint16_t gp, int64_t _weight, enum MySerStatus _status, unsigned int _compression, int64_t _max_connections, unsigned int _max_replication_lag, int32_t _use_ssl, unsigned int _max_latency_ms, char* _comment -) { +) : status(this) { address=strdup(add); port=p; gtid_port=gp; weight=_weight; - status=_status; compression=_compression; max_connections=_max_connections; max_replication_lag=_max_replication_lag; @@ -766,6 +767,7 @@ MySrvC::MySrvC( comment=strdup(_comment); ConnectionsUsed=new MySrvConnList(this); ConnectionsFree=new MySrvConnList(this); + status=_status; } void MySrvC::connect_error(int err_num, bool get_mutex) { @@ -879,6 +881,17 @@ MySrvList::~MySrvList() { } +MySrvStatus::MySrvStatus(MySrvC* _mysrvc) : mysrvc(_mysrvc) {} + +MySrvStatus::~MySrvStatus() { mysrvc->myhgc->refresh_online_server_count(); } + +enum MySerStatus MySrvStatus::operator=(enum MySerStatus _status) { + status = _status; + if (mysrvc->myhgc) + mysrvc->myhgc->refresh_online_server_count(); +} + + MyHGC::MyHGC(int _hid) { hid=_hid; mysrvs=new MySrvList(this); @@ -890,6 +903,8 @@ MyHGC::MyHGC(int _hid) { servers_defaults.weight = -1; servers_defaults.max_connections = -1; servers_defaults.use_ssl = -1; + num_online_servers.store(0, std::memory_order_relaxed);; + last_log_time_num_online_servers = 0; } void MyHGC::reset_attributes() { @@ -921,6 +936,31 @@ MyHGC::~MyHGC() { delete mysrvs; } +void MyHGC::refresh_online_server_count() { + assert(MyHGM->is_locked); + + unsigned int online_servers_count = 0; + for (unsigned int i=0; iservers->len; i++) { + MySrvC *mysrvc=(MySrvC *)mysrvs->servers->index(i); + if (mysrvc->status == MYSQL_SERVER_STATUS_ONLINE) { + online_servers_count++; + } + } + num_online_servers.store(online_servers_count, std::memory_order_relaxed); +} + +void MyHGC::log_num_online_server_count_error() { + const unsigned long long curtime = monotonic_time(); + // if this is the first time the method is called or if more than 10 seconds have passed since the last log + if (last_log_time_num_online_servers == 0 || + (curtime-last_log_time_num_online_servers > 10*1000*1000)) { + last_log_time_num_online_servers = curtime; + proxy_error( + "Number of online servers detected in a hostgroup exceeds the configured maximum online servers. hostgroup:%u, num_online_servers:%u, max_online_servers:%u\n", + hid, num_online_servers.load(std::memory_order_relaxed), attributes.max_num_online_servers); + } +} + using metric_name = std::string; using metric_help = std::string; using metric_tags = std::map; @@ -1441,6 +1481,7 @@ void MySQL_HostGroups_Manager::wrlock() { #else spin_wrlock(&rwlock); #endif + is_locked = true; } void MySQL_HostGroups_Manager::p_update_mysql_error_counter(p_mysql_error_type err_type, unsigned int hid, char* address, uint16_t port, unsigned int code) { @@ -1480,6 +1521,7 @@ void MySQL_HostGroups_Manager::wrunlock() { #else spin_wrunlock(&rwlock); #endif + is_locked = false; } @@ -2464,6 +2506,8 @@ void MySQL_HostGroups_Manager::purge_mysql_servers_table() { if (mysrvc->ConnectionsUsed->conns_length()==0 && mysrvc->ConnectionsFree->conns_length()==0) { // no more connections for OFFLINE_HARD server, removing it mysrvc=(MySrvC *)myhgc->mysrvs->servers->remove_index_fast(j); + // already being refreshed in MySrvC destructor + //myhgc->refresh_online_server_count(); j--; delete mysrvc; } @@ -3075,8 +3119,9 @@ void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c, bool _lo if (mysrvc->status==MYSQL_SERVER_STATUS_ONLINE) { if (c->async_state_machine==ASYNC_IDLE) { if (GloMTH == NULL) { goto __exit_push_MyConn_to_pool; } - // Check if the connection has too many prepared statements - if (c->local_stmts->get_num_backend_stmts() > (unsigned int)GloMTH->variables.max_stmts_per_connection) { + const bool online_servers_within_threshold = c->parent->myhgc->online_servers_within_threshold(); + if (/*online_servers_within_threshold == true &&*/ // if online servers in a hostgroup surpass the configured maximum, add connection to ConnectionFree pool + c->local_stmts->get_num_backend_stmts() > (unsigned int)GloMTH->variables.max_stmts_per_connection) { // Check if the connection has too many prepared statements // Log debug information about destroying the connection due to too many prepared statements proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d because has too many prepared statements\n", c, mysrvc->address, mysrvc->port, mysrvc->status); // delete c; @@ -3085,6 +3130,9 @@ void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c, bool _lo } else { c->optimize(); // Optimize the connection mysrvc->ConnectionsFree->add(c); // Add the connection to the list of free connections + if (online_servers_within_threshold == false) { + mysrvc->myhgc->log_num_online_server_count_error(); + } } } else { // Log debug information about destroying the connection @@ -3164,35 +3212,43 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ for (j=0; jidx(j); if (mysrvc->status==MYSQL_SERVER_STATUS_ONLINE) { // consider this server only if ONLINE - if (mysrvc->ConnectionsUsed->conns_length() < mysrvc->max_connections) { // consider this server only if didn't reach max_connections - if ( mysrvc->current_latency_us < ( mysrvc->max_latency_us ? mysrvc->max_latency_us : mysql_thread___default_max_latency_ms*1000 ) ) { // consider the host only if not too far - if (gtid_trxid) { - if (MyHGM->gtid_exists(mysrvc, gtid_uuid, gtid_trxid)) { - sum+=mysrvc->weight; - TotalUsedConn+=mysrvc->ConnectionsUsed->conns_length(); - mysrvcCandidates[num_candidates]=mysrvc; - num_candidates++; + if (mysrvc->myhgc->num_online_servers.load(std::memory_order_relaxed) <= mysrvc->myhgc->attributes.max_num_online_servers) { // number of online servers in HG is within configured range + if (mysrvc->ConnectionsUsed->conns_length() < mysrvc->max_connections) { // consider this server only if didn't reach max_connections + if (mysrvc->current_latency_us < (mysrvc->max_latency_us ? mysrvc->max_latency_us : mysql_thread___default_max_latency_ms * 1000)) { // consider the host only if not too far + if (gtid_trxid) { + if (MyHGM->gtid_exists(mysrvc, gtid_uuid, gtid_trxid)) { + sum += mysrvc->weight; + TotalUsedConn += mysrvc->ConnectionsUsed->conns_length(); + mysrvcCandidates[num_candidates] = mysrvc; + num_candidates++; + } } - } else { - if (max_lag_ms >= 0) { - if ((unsigned int)max_lag_ms >= mysrvc->aws_aurora_current_lag_us/1000) { - sum+=mysrvc->weight; - TotalUsedConn+=mysrvc->ConnectionsUsed->conns_length(); - mysrvcCandidates[num_candidates]=mysrvc; + else { + if (max_lag_ms >= 0) { + if ((unsigned int)max_lag_ms >= mysrvc->aws_aurora_current_lag_us / 1000) { + sum += mysrvc->weight; + TotalUsedConn += mysrvc->ConnectionsUsed->conns_length(); + mysrvcCandidates[num_candidates] = mysrvc; + num_candidates++; + } + else { + sess->thread->status_variables.stvar[st_var_aws_aurora_replicas_skipped_during_query]++; + } + } + else { + sum += mysrvc->weight; + TotalUsedConn += mysrvc->ConnectionsUsed->conns_length(); + mysrvcCandidates[num_candidates] = mysrvc; num_candidates++; - } else { - sess->thread->status_variables.stvar[st_var_aws_aurora_replicas_skipped_during_query]++; } - } else { - sum+=mysrvc->weight; - TotalUsedConn+=mysrvc->ConnectionsUsed->conns_length(); - mysrvcCandidates[num_candidates]=mysrvc; - num_candidates++; } } } + else { + max_connections_reached = true; + } } else { - max_connections_reached = true; + mysrvc->myhgc->log_num_online_server_count_error(); } } else { if (mysrvc->status==MYSQL_SERVER_STATUS_SHUNNED) { diff --git a/lib/MySQL_Session.cpp b/lib/MySQL_Session.cpp index 1dc1c19436..31fc36001e 100644 --- a/lib/MySQL_Session.cpp +++ b/lib/MySQL_Session.cpp @@ -4649,16 +4649,20 @@ int MySQL_Session::get_pkts_from_client(bool& wrong_pass, PtrSize_t& pkt) { // 0 : no action // -1 : the calling function will return // 1 : call to NEXT_IMMEDIATE -int MySQL_Session::handler_ProcessingQueryError_CheckBackendConnectionStatus(MySQL_Data_Stream *myds) { - MySQL_Connection *myconn = myds->myconn; +int MySQL_Session::handler_ProcessingQueryError_CheckBackendConnectionStatus(MySQL_Data_Stream* myds) { + MySQL_Connection* myconn = myds->myconn; + + const bool online_servers_within_threshold = myconn->parent->myhgc->online_servers_within_threshold(); // the query failed if ( // due to #774 , we now read myconn->server_status instead of myconn->parent->status - (myconn->server_status==MYSQL_SERVER_STATUS_OFFLINE_HARD) // the query failed because the server is offline hard + (myconn->server_status == MYSQL_SERVER_STATUS_OFFLINE_HARD) // the query failed because the server is offline hard + || + (myconn->server_status == MYSQL_SERVER_STATUS_SHUNNED && myconn->parent->shunned_automatic == true && myconn->parent->shunned_and_kill_all_connections == true) // the query failed because the server is shunned due to a serious failure || - (myconn->server_status==MYSQL_SERVER_STATUS_SHUNNED && myconn->parent->shunned_automatic==true && myconn->parent->shunned_and_kill_all_connections==true) // the query failed because the server is shunned due to a serious failure + (myconn->server_status == MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) // slave is lagging! see #774 || - (myconn->server_status==MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) // slave is lagging! see #774 + (online_servers_within_threshold == false) // number of online servers in a hostgroup exceeds the configured maximum servers ) { if (mysql_thread___connect_timeout_server_max) { myds->max_connect_time=thread->curtime+mysql_thread___connect_timeout_server_max*1000; @@ -4668,6 +4672,10 @@ int MySQL_Session::handler_ProcessingQueryError_CheckBackendConnectionStatus(MyS thread->status_variables.stvar[st_var_backend_lagging_during_query]++; proxy_error("Detected a lagging server during query: %s, %d, session_id:%u\n", myconn->parent->address, myconn->parent->port, this->thread_session_id); MyHGM->p_update_mysql_error_counter(p_mysql_error_type::proxysql, myconn->parent->myhgc->hid, myconn->parent->address, myconn->parent->port, ER_PROXYSQL_LAGGING_SRV); + } else if (myconn->server_status == MYSQL_SERVER_STATUS_ONLINE && online_servers_within_threshold == false) { + //proxy_error("Number of online servers detected in a hostgroup exceeds the configured maximum online servers. %s, %d, hostgroup:%u, num_online_servers:%u, max_online_servers:%u, session_id:%u\n", + // myconn->parent->address, myconn->parent->port, myconn->parent->myhgc->hid, num_online_servers, myconn->parent->myhgc->attributes.max_num_online_servers, this->thread_session_id); + myconn->parent->myhgc->log_num_online_server_count_error(); } else { thread->status_variables.stvar[st_var_backend_offline_during_query]++; proxy_error("Detected an offline server during query: %s, %d, session_id:%u\n", myconn->parent->address, myconn->parent->port, this->thread_session_id); diff --git a/lib/mysql_connection.cpp b/lib/mysql_connection.cpp index 37c24b2db2..c763179092 100644 --- a/lib/mysql_connection.cpp +++ b/lib/mysql_connection.cpp @@ -2041,7 +2041,9 @@ bool MySQL_Connection::IsServerOffline() { || (server_status==MYSQL_SERVER_STATUS_SHUNNED && parent->shunned_automatic==true && parent->shunned_and_kill_all_connections==true) // the server is SHUNNED due to a serious issue || - (server_status==MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) // slave is lagging! see #774 + (server_status==MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) // slave is lagging! see #774 + || + (parent->myhgc->online_servers_within_threshold() == false) // number of online servers in a hostgroup exceeds the configured maximum servers ) { ret=true; } From 42c8bb83c6b272305ec1f704b872129d689dbcdf Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Tue, 23 Apr 2024 12:46:35 +0500 Subject: [PATCH 2/9] Few fixes --- include/MySQL_HostGroups_Manager.h | 7 ++++++- lib/MySrvC.cpp | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/include/MySQL_HostGroups_Manager.h b/include/MySQL_HostGroups_Manager.h index 5311b57a50..416ef1e76b 100644 --- a/include/MySQL_HostGroups_Manager.h +++ b/include/MySQL_HostGroups_Manager.h @@ -199,7 +199,7 @@ class MySrvStatus { MySrvStatus(MySrvC* _mysrvc); ~MySrvStatus(); - enum MySerStatus operator=(enum MySerStatus _status); + MySrvStatus& operator=(enum MySerStatus _status); inline operator enum MySerStatus() const { @@ -211,6 +211,11 @@ class MySrvStatus { return status == _status; } + inline + bool operator==(const MySrvStatus& _status) const { + return status == _status.status; + } + private: MySrvC *mysrvc; enum MySerStatus status; diff --git a/lib/MySrvC.cpp b/lib/MySrvC.cpp index 4fd3e09d1d..bf6c94487a 100644 --- a/lib/MySrvC.cpp +++ b/lib/MySrvC.cpp @@ -199,10 +199,10 @@ MySrvC::~MySrvC() { MySrvStatus::MySrvStatus(MySrvC* _mysrvc) : mysrvc(_mysrvc) {} -MySrvStatus::~MySrvStatus() { mysrvc->myhgc->refresh_online_server_count(); } +MySrvStatus::~MySrvStatus() { if (mysrvc->myhgc) mysrvc->myhgc->refresh_online_server_count(); } -enum MySerStatus MySrvStatus::operator=(enum MySerStatus _status) { +MySrvStatus& MySrvStatus::operator=(enum MySerStatus _status) { status = _status; - if (mysrvc->myhgc) - mysrvc->myhgc->refresh_online_server_count(); + if (mysrvc->myhgc) mysrvc->myhgc->refresh_online_server_count(); + return *this; } From c846a3eaf9fbcf42f7e9b7352aaadded921a76d8 Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Tue, 23 Apr 2024 14:01:45 +0500 Subject: [PATCH 3/9] Removed online server error log from push_MyConn_to_pool --- lib/MySQL_HostGroups_Manager.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index 45e866d19b..8d4dcfede2 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -2362,9 +2362,7 @@ void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c, bool _lo if (mysrvc->status==MYSQL_SERVER_STATUS_ONLINE) { if (c->async_state_machine==ASYNC_IDLE) { if (GloMTH == NULL) { goto __exit_push_MyConn_to_pool; } - const bool online_servers_within_threshold = c->parent->myhgc->online_servers_within_threshold(); - if (/*online_servers_within_threshold == true &&*/ // if online servers in a hostgroup surpass the configured maximum, add connection to ConnectionFree pool - c->local_stmts->get_num_backend_stmts() > (unsigned int)GloMTH->variables.max_stmts_per_connection) { // Check if the connection has too many prepared statements + if (c->local_stmts->get_num_backend_stmts() > (unsigned int)GloMTH->variables.max_stmts_per_connection) { // Check if the connection has too many prepared statements // Log debug information about destroying the connection due to too many prepared statements proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d because has too many prepared statements\n", c, mysrvc->address, mysrvc->port, mysrvc->status); // delete c; @@ -2373,9 +2371,6 @@ void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c, bool _lo } else { c->optimize(); // Optimize the connection mysrvc->ConnectionsFree->add(c); // Add the connection to the list of free connections - if (online_servers_within_threshold == false) { - mysrvc->myhgc->log_num_online_server_count_error(); - } } } else { // Log debug information about destroying the connection From 363263ff451142e8bdac1a03134a78af6a1ba874 Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Tue, 23 Apr 2024 14:18:21 +0500 Subject: [PATCH 4/9] Implemented shutdown case handling within refresh_online_server_count --- lib/MyHGC.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/MyHGC.cpp b/lib/MyHGC.cpp index 746c41736d..bc347af9cc 100644 --- a/lib/MyHGC.cpp +++ b/lib/MyHGC.cpp @@ -390,6 +390,9 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ } void MyHGC::refresh_online_server_count() { + if (__sync_fetch_and_add(&glovars.shutdown, 0) != 0) + return; + assert(MyHGM->is_locked); unsigned int online_servers_count = 0; From 354bf8135fcdd79c37acf8a2c83041ce8970aa9f Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Wed, 24 Apr 2024 02:08:04 +0500 Subject: [PATCH 5/9] Added TAP test --- ..._hostgroup_attributes_online_servers-t.cpp | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 test/tap/tests/test_hostgroup_attributes_online_servers-t.cpp diff --git a/test/tap/tests/test_hostgroup_attributes_online_servers-t.cpp b/test/tap/tests/test_hostgroup_attributes_online_servers-t.cpp new file mode 100644 index 0000000000..e6d7863a19 --- /dev/null +++ b/test/tap/tests/test_hostgroup_attributes_online_servers-t.cpp @@ -0,0 +1,143 @@ +/** + * @file test_hostgroup_attributes_online_servers-t.cpp + * @brief This test will evaluate configured maximum number of online servers within a hostgroup operates correctly. + * Note: + * This test is based on the assumption that ProxySQL is configured with read and write splitting, with writer servers in hostgroup 0, + * and readers in hostgroup 1 (having multiple servers). + */ + +#include +#include +#include +#include +#include "mysql.h" +#include "mysqld_error.h" +#include "tap.h" +#include "command_line.h" +#include "utils.h" + +#define MYSQL_QUERY__(mysql, query) \ + do { \ + if (mysql_query(mysql, query)) { \ + fprintf(stderr, "File %s, line %d, Error: %s\n", \ + __FILE__, __LINE__, mysql_error(mysql)); \ + goto cleanup; \ + } \ + } while(0) + +#define MYSQL_CLEAR_RESULT(mysql) mysql_free_result(mysql_store_result(mysql)); +#define NUM_QUERY_EXEC 5 + + +std::tuple execute_query_and_check_result(MYSQL* proxysql, const char* query, bool is_select, + bool should_succeed, unsigned int iteration = 1) { + bool res = true; + unsigned int errcode = 0; + unsigned int i; + for (i = 0; i < iteration; i++) { + const int result = mysql_query(proxysql, query); + if (result) errcode = mysql_errno(proxysql); + if (result == 0) { MYSQL_CLEAR_RESULT(proxysql); } + if ((should_succeed && result != 0) || (!should_succeed && result == 0)) { + res = false; + break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + return std::make_tuple(res, errcode, i); +} + +int main(int argc, char** argv) { + CommandLine cl; + + if (cl.getEnv()) { + diag("Failed to get the required environmental variables."); + return -1; + } + + plan(4*3); + + // Initialize Admin connection + MYSQL* proxysql_admin = mysql_init(NULL); + if (!proxysql_admin) { + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_admin)); + return -1; + } + // Connnect to ProxySQL Admin + if (!mysql_real_connect(proxysql_admin, cl.host, cl.admin_username, cl.admin_password, NULL, cl.admin_port, NULL, 0)) { + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_admin)); + return -1; + } + + // Initialize ProxySQL connection + MYSQL* proxysql = mysql_init(NULL); + if (!proxysql) { + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql)); + return -1; + } + + // Connect to ProxySQL + if (!mysql_real_connect(proxysql, cl.host, cl.username, cl.password, NULL, cl.port, NULL, 0)) { + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql)); + return exit_status(); + } + + bool result; + unsigned int errcode; + unsigned int query_exec_count; + + diag("## Pre-test Check ##\n"); + diag("Executing query... [Reader HG]\n"); + MYSQL_QUERY__(proxysql, "SELECT 1"); + MYSQL_CLEAR_RESULT(proxysql); + diag("Executing query... [Writer HG]\n"); + MYSQL_QUERY__(proxysql, "DO 1"); + MYSQL_CLEAR_RESULT(proxysql); + diag("## Done\n"); + + diag("## Starting test ##\n"); + diag("Setting max_num_online_servers=1 in hostgroup: 1...\n"); + MYSQL_QUERY__(proxysql_admin, "DELETE FROM mysql_hostgroup_attributes WHERE hostgroup_id=1"); + MYSQL_QUERY__(proxysql_admin, "INSERT INTO mysql_hostgroup_attributes (hostgroup_id, max_num_online_servers) values (1,1)"); + MYSQL_QUERY__(proxysql_admin, "LOAD MYSQL SERVERS TO RUNTIME"); + diag("Done\n"); + + diag("Executing query... [Reader HG]\n"); + std::tie(result, errcode, query_exec_count) = execute_query_and_check_result(proxysql, "SELECT 1", true, false, NUM_QUERY_EXEC); + ok(result, "Query execution should fail"); + ok(errcode == 9001, "Error code should be '9001'. Actual value:'%u'", errcode); + ok(query_exec_count == NUM_QUERY_EXEC, "Query execution count should be '%u'. Actual value:'%u'", NUM_QUERY_EXEC, query_exec_count); + + diag("Executing query... [Writer HG]\n"); + std::tie(result, errcode, query_exec_count) = execute_query_and_check_result(proxysql, "DO 1", false, true, NUM_QUERY_EXEC); + ok(result, "Query execution should succeed"); + ok(errcode == 0, "Error code should be '0'. Actual value:'%u'", errcode); + ok(query_exec_count == NUM_QUERY_EXEC, "Query execution count should be '%u'. Actual value:'%u'", NUM_QUERY_EXEC, query_exec_count); + + diag("Setting max_num_online_servers=100 in hostgroup: 1...\n"); + MYSQL_QUERY__(proxysql_admin, "UPDATE mysql_hostgroup_attributes SET max_num_online_servers=100 WHERE hostgroup_id=1"); + MYSQL_QUERY__(proxysql_admin, "LOAD MYSQL SERVERS TO RUNTIME"); + diag("Done\n"); + + diag("Executing query... [Reader HG]...\n"); + std::tie(result, errcode, query_exec_count) = execute_query_and_check_result(proxysql, "SELECT 1", true, true, NUM_QUERY_EXEC); + ok(result, "Query execution should succeed"); + ok(errcode == 0, "Error code should be '0'. Actual value:'%u'", errcode); + ok(query_exec_count == NUM_QUERY_EXEC, "Query execution count should be '%u'. Actual value:'%u'", NUM_QUERY_EXEC, query_exec_count); + + diag("Executing query... [Writer HG]\n"); + std::tie(result, errcode, query_exec_count) = execute_query_and_check_result(proxysql, "DO 1", false, true, NUM_QUERY_EXEC); + ok(result, "Query execution should succeed"); + ok(errcode == 0, "Error code should be '0'. Actual value:'%u'", errcode); + ok(query_exec_count == NUM_QUERY_EXEC, "Query execution count should be '%u'. Actual value:'%u'", NUM_QUERY_EXEC, query_exec_count); + diag("## Done\n"); + +cleanup: + MYSQL_QUERY(proxysql_admin, "DELETE FROM mysql_hostgroup_attributes WHERE hostgroup_id=1"); + MYSQL_QUERY(proxysql_admin, "LOAD MYSQL SERVERS TO RUNTIME"); + + mysql_close(proxysql); + mysql_close(proxysql_admin); + + return exit_status(); +} From a081695ae96031239e1d1e4b2c85c9825e96fd4b Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Wed, 24 Apr 2024 12:57:22 +0500 Subject: [PATCH 6/9] * Solved few warnings * Enforced explicit conversion of MySrvStatus --- include/MySQL_HostGroups_Manager.h | 21 ++++++++++------- lib/MySQL_HostGroups_Manager.cpp | 36 +++++++++++++++--------------- lib/MySrvC.cpp | 2 +- lib/MySrvConnList.cpp | 2 +- lib/mysql_connection.cpp | 16 ++++++------- 5 files changed, 41 insertions(+), 36 deletions(-) diff --git a/include/MySQL_HostGroups_Manager.h b/include/MySQL_HostGroups_Manager.h index 416ef1e76b..6d14670b70 100644 --- a/include/MySQL_HostGroups_Manager.h +++ b/include/MySQL_HostGroups_Manager.h @@ -196,24 +196,29 @@ class MySrvConnList { class MySrvC; class MySrvStatus { public: - MySrvStatus(MySrvC* _mysrvc); + explicit MySrvStatus(MySrvC* _mysrvc); ~MySrvStatus(); - MySrvStatus& operator=(enum MySerStatus _status); + MySrvStatus& operator=(MySerStatus _status); inline - operator enum MySerStatus() const { - return status; + bool operator==(MySerStatus _status) const { + return status == _status; } inline - bool operator==(enum MySerStatus _status) const { - return status == _status; + bool operator!=(MySerStatus _status) const { + return status!=_status; + } + + inline + explicit operator MySerStatus() const { + return status; } inline - bool operator==(const MySrvStatus& _status) const { - return status == _status.status; + explicit operator int() const { + return (int)status; } private: diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index 8d4dcfede2..eb7705e990 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -759,12 +759,12 @@ void MySQL_HostGroups_Manager::p_update_mysql_error_counter(p_mysql_error_type e } void MySQL_HostGroups_Manager::wrunlock() { + is_locked = false; #ifdef MHM_PTHREAD_MUTEX pthread_mutex_unlock(&lock); #else spin_wrunlock(&rwlock); #endif - is_locked = false; } @@ -1392,7 +1392,7 @@ bool MySQL_HostGroups_Manager::commit( } if (change_server_status == true) { if (GloMTH->variables.hostgroup_manager_verbose) - proxy_info("Changing status for server %d:%s:%d (%s:%d) from %d (%d) to %d\n", mysrvc->myhgc->hid, mysrvc->address, mysrvc->port, r->fields[1], atoi(r->fields[2]), atoi(r->fields[5]), mysrvc->status, atoi(r->fields[15])); + proxy_info("Changing status for server %d:%s:%d (%s:%d) from %d (%d) to %d\n", mysrvc->myhgc->hid, mysrvc->address, mysrvc->port, r->fields[1], atoi(r->fields[2]), atoi(r->fields[5]), (int)mysrvc->status, atoi(r->fields[15])); mysrvc->status = (MySerStatus)atoi(r->fields[15]); } if (mysrvc->status==MYSQL_SERVER_STATUS_SHUNNED) { @@ -1440,7 +1440,7 @@ bool MySQL_HostGroups_Manager::commit( } if (run_update) { rc=(*proxy_sqlite3_bind_int64)(statement2, 1, mysrvc->weight); ASSERT_SQLITE_OK(rc, mydb); - rc=(*proxy_sqlite3_bind_int64)(statement2, 2, mysrvc->status); ASSERT_SQLITE_OK(rc, mydb); + rc=(*proxy_sqlite3_bind_int64)(statement2, 2, (int)mysrvc->status); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement2, 3, mysrvc->compression); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement2, 4, mysrvc->max_connections); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement2, 5, mysrvc->max_replication_lag); ASSERT_SQLITE_OK(rc, mydb); @@ -1800,7 +1800,7 @@ void MySQL_HostGroups_Manager::generate_mysql_servers_table(int *_onlyhg) { mysrvc=myhgc->mysrvs->idx(j); if (mysql_thread___hostgroup_manager_verbose) { char *st; - switch (mysrvc->status) { + switch ((int)mysrvc->status) { case 0: st=(char *)"ONLINE"; break; @@ -1830,7 +1830,7 @@ void MySQL_HostGroups_Manager::generate_mysql_servers_table(int *_onlyhg) { rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+3, mysrvc->port); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+4, mysrvc->gtid_port); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+5, mysrvc->weight); ASSERT_SQLITE_OK(rc, mydb); - rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+6, mysrvc->status); ASSERT_SQLITE_OK(rc, mydb); + rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+6, (int)mysrvc->status); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+7, mysrvc->compression); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+8, mysrvc->max_connections); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+9, mysrvc->max_replication_lag); ASSERT_SQLITE_OK(rc, mydb); @@ -1853,7 +1853,7 @@ void MySQL_HostGroups_Manager::generate_mysql_servers_table(int *_onlyhg) { rc=(*proxy_sqlite3_bind_int64)(statement1, 3, mysrvc->port); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement1, 4, mysrvc->gtid_port); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement1, 5, mysrvc->weight); ASSERT_SQLITE_OK(rc, mydb); - rc=(*proxy_sqlite3_bind_int64)(statement1, 6, mysrvc->status); ASSERT_SQLITE_OK(rc, mydb); + rc=(*proxy_sqlite3_bind_int64)(statement1, 6, (int)mysrvc->status); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement1, 7, mysrvc->compression); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement1, 8, mysrvc->max_connections); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement1, 9, mysrvc->max_replication_lag); ASSERT_SQLITE_OK(rc, mydb); @@ -2341,7 +2341,7 @@ void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c, bool _lo mysrvc = static_cast(c->parent); // Log debug information about the connection being returned to the pool - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySQL_Connection %p, server %s:%d with status %d\n", c, mysrvc->address, mysrvc->port, mysrvc->status); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySQL_Connection %p, server %s:%d with status %d\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->status); // Remove the connection from the list of used connections for the parent server mysrvc->ConnectionsUsed->remove(c); @@ -2353,7 +2353,7 @@ void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c, bool _lo // If the largest query length exceeds the threshold, destroy the connection if (c->largest_query_length > (unsigned int)GloMTH->variables.threshold_query_length) { - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d . largest_query_length = %lu\n", c, mysrvc->address, mysrvc->port, mysrvc->status, c->largest_query_length); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d . largest_query_length = %lu\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->status, c->largest_query_length); delete c; goto __exit_push_MyConn_to_pool; } @@ -2364,7 +2364,7 @@ void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c, bool _lo if (GloMTH == NULL) { goto __exit_push_MyConn_to_pool; } if (c->local_stmts->get_num_backend_stmts() > (unsigned int)GloMTH->variables.max_stmts_per_connection) { // Check if the connection has too many prepared statements // Log debug information about destroying the connection due to too many prepared statements - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d because has too many prepared statements\n", c, mysrvc->address, mysrvc->port, mysrvc->status); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d because has too many prepared statements\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->status); // delete c; mysrvc->ConnectionsUsed->add(c); // Add the connection back to the list of used connections destroy_MyConn_from_pool(c, false); // Destroy the connection from the pool @@ -2374,12 +2374,12 @@ void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c, bool _lo } } else { // Log debug information about destroying the connection - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d\n", c, mysrvc->address, mysrvc->port, mysrvc->status); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->status); delete c; // Destroy the connection } } else { // Log debug information about destroying the connection - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d\n", c, mysrvc->address, mysrvc->port, mysrvc->status); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->status); delete c; // Destroy the connection } @@ -2808,7 +2808,7 @@ void MySQL_HostGroups_Manager::group_replication_lag_action_set_server_status(My MySrvC *mysrvc=(MySrvC *)myhgc->mysrvs->servers->index(j); proxy_debug( PROXY_DEBUG_MONITOR, 6, "Server 'MySrvC' - address: %s, port: %d, status: %d\n", mysrvc->address, - mysrvc->port, mysrvc->status + mysrvc->port, (int)mysrvc->status ); if (strcmp(mysrvc->address,address)==0 && mysrvc->port==port) { @@ -3386,7 +3386,7 @@ void MySQL_HostGroups_Manager::p_update_connection_pool() { // proxysql_connection_pool_status metric p_update_connection_pool_update_gauge(endpoint_id, common_labels, - status.p_connection_pool_status_map, mysrvc->status + 1, p_hg_dyn_gauge::connection_pool_status); + status.p_connection_pool_status_map, ((int)mysrvc->status) + 1, p_hg_dyn_gauge::connection_pool_status); } } @@ -3469,7 +3469,7 @@ SQLite3_result * MySQL_HostGroups_Manager::SQL3_Connection_Pool(bool _reset, int pta[1]=strdup(mysrvc->address); sprintf(buf,"%d", mysrvc->port); pta[2]=strdup(buf); - switch (mysrvc->status) { + switch ((int)mysrvc->status) { case 0: pta[3]=strdup("ONLINE"); break; @@ -4057,7 +4057,7 @@ bool MySQL_HostGroups_Manager::shun_and_killall(char *hostname, int port) { for (j=0; jmysrvs->idx(j); if (mysrvc->port==port && strcmp(mysrvc->address,hostname)==0) { - switch (mysrvc->status) { + switch ((MySerStatus)mysrvc->status) { case MYSQL_SERVER_STATUS_SHUNNED: if (mysrvc->shunned_automatic==false) { break; @@ -7284,12 +7284,12 @@ MySrvC* MySQL_HostGroups_Manager::HostGroup_Server_Mapping::insert_HGM(unsigned if (!ret_srv) { if (GloMTH->variables.hostgroup_manager_verbose) { - proxy_info("Creating new server in HG %d : %s:%d , gtid_port=%d, weight=%ld, status=%d\n", hostgroup_id, srv->address, srv->port, srv->gtid_port, srv->weight, srv->status); + proxy_info("Creating new server in HG %d : %s:%d , gtid_port=%d, weight=%ld, status=%d\n", hostgroup_id, srv->address, srv->port, srv->gtid_port, srv->weight, (int)srv->status); } - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 5, "Adding new server %s:%d , weight=%ld, status=%d, mem_ptr=%p into hostgroup=%d\n", srv->address, srv->port, srv->weight, srv->status, srv, hostgroup_id); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 5, "Adding new server %s:%d , weight=%ld, status=%d, mem_ptr=%p into hostgroup=%d\n", srv->address, srv->port, srv->weight, (int)srv->status, srv, hostgroup_id); - ret_srv = new MySrvC(srv->address, srv->port, srv->gtid_port, srv->weight, srv->status, srv->compression, + ret_srv = new MySrvC(srv->address, srv->port, srv->gtid_port, srv->weight, (MySerStatus)srv->status, srv->compression, srv->max_connections, srv->max_replication_lag, srv->use_ssl, (srv->max_latency_us / 1000), srv->comment); myhgc->mysrvs->add(ret_srv); diff --git a/lib/MySrvC.cpp b/lib/MySrvC.cpp index bf6c94487a..162cb36f2a 100644 --- a/lib/MySrvC.cpp +++ b/lib/MySrvC.cpp @@ -201,7 +201,7 @@ MySrvStatus::MySrvStatus(MySrvC* _mysrvc) : mysrvc(_mysrvc) {} MySrvStatus::~MySrvStatus() { if (mysrvc->myhgc) mysrvc->myhgc->refresh_online_server_count(); } -MySrvStatus& MySrvStatus::operator=(enum MySerStatus _status) { +MySrvStatus& MySrvStatus::operator=(MySerStatus _status) { status = _status; if (mysrvc->myhgc) mysrvc->myhgc->refresh_online_server_count(); return *this; diff --git a/lib/MySrvConnList.cpp b/lib/MySrvConnList.cpp index abe0c44ee7..ace65635e2 100644 --- a/lib/MySrvConnList.cpp +++ b/lib/MySrvConnList.cpp @@ -40,7 +40,7 @@ MySrvConnList::~MySrvConnList() { } void MySrvConnList::drop_all_connections() { - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Dropping all connections (%u total) on MySrvConnList %p for server %s:%d , hostgroup=%d , status=%d\n", conns_length(), this, mysrvc->address, mysrvc->port, mysrvc->myhgc->hid, mysrvc->status); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Dropping all connections (%u total) on MySrvConnList %p for server %s:%d , hostgroup=%d , status=%d\n", conns_length(), this, mysrvc->address, mysrvc->port, mysrvc->myhgc->hid, (int)mysrvc->status); while (conns_length()) { MySQL_Connection *conn=(MySQL_Connection *)conns->remove_index_fast(0); delete conn; diff --git a/lib/mysql_connection.cpp b/lib/mysql_connection.cpp index c763179092..e360f9ad60 100644 --- a/lib/mysql_connection.cpp +++ b/lib/mysql_connection.cpp @@ -2035,7 +2035,7 @@ bool MySQL_Connection::IsServerOffline() { bool ret=false; if (parent==NULL) return ret; - server_status=parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this if ( (server_status==MYSQL_SERVER_STATUS_OFFLINE_HARD) // the server is OFFLINE as specific by the user || @@ -2059,7 +2059,7 @@ int MySQL_Connection::async_query(short event, char *stmt, unsigned long length, PROXY_TRACE2(); assert(mysql); assert(ret_mysql); - server_status=parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this if (IsServerOffline()) return -1; @@ -2198,7 +2198,7 @@ int MySQL_Connection::async_change_user(short event) { PROXY_TRACE(); assert(mysql); assert(ret_mysql); - server_status=parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this if (IsServerOffline()) return -1; @@ -2245,7 +2245,7 @@ int MySQL_Connection::async_select_db(short event) { PROXY_TRACE(); assert(mysql); assert(ret_mysql); - server_status=parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this if (IsServerOffline()) return -1; @@ -2286,7 +2286,7 @@ int MySQL_Connection::async_set_autocommit(short event, bool ac) { PROXY_TRACE(); assert(mysql); assert(ret_mysql); - server_status=parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this if (IsServerOffline()) return -1; @@ -2329,7 +2329,7 @@ int MySQL_Connection::async_set_names(short event, unsigned int c) { PROXY_TRACE(); assert(mysql); assert(ret_mysql); - server_status=parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this if (IsServerOffline()) return -1; @@ -2372,7 +2372,7 @@ int MySQL_Connection::async_set_option(short event, bool mask) { PROXY_TRACE(); assert(mysql); assert(ret_mysql); - server_status=parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this if (IsServerOffline()) return -1; @@ -2854,7 +2854,7 @@ int MySQL_Connection::async_send_simple_command(short event, char *stmt, unsigne PROXY_TRACE(); assert(mysql); assert(ret_mysql); - server_status=parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this if ( (parent->status==MYSQL_SERVER_STATUS_OFFLINE_HARD) // the server is OFFLINE as specific by the user || From e4fd764d86f0d6392ec8dcfa20714a12480996f3 Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Wed, 24 Apr 2024 16:22:26 +0500 Subject: [PATCH 7/9] Added is_locked flag within '#ifdef DEBUG' conditional block --- include/MySQL_HostGroups_Manager.h | 4 +++- lib/MyHGC.cpp | 4 ++-- lib/MySQL_HostGroups_Manager.cpp | 4 ++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/MySQL_HostGroups_Manager.h b/include/MySQL_HostGroups_Manager.h index 6d14670b70..b5d3cb854c 100644 --- a/include/MySQL_HostGroups_Manager.h +++ b/include/MySQL_HostGroups_Manager.h @@ -972,7 +972,9 @@ class MySQL_HostGroups_Manager { void init(); void wrlock(); void wrunlock(); - bool is_locked = false; // will be removed after testing +#ifdef DEBUG + bool is_locked = false; +#endif int servers_add(SQLite3_result *resultset); /** * @brief Generates a new global checksum for module 'mysql_servers_v2' using the provided hash. diff --git a/lib/MyHGC.cpp b/lib/MyHGC.cpp index bc347af9cc..72dd3f564c 100644 --- a/lib/MyHGC.cpp +++ b/lib/MyHGC.cpp @@ -392,9 +392,9 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ void MyHGC::refresh_online_server_count() { if (__sync_fetch_and_add(&glovars.shutdown, 0) != 0) return; - +#ifdef DEBUG assert(MyHGM->is_locked); - +#endif unsigned int online_servers_count = 0; for (unsigned int i = 0; i < mysrvs->servers->len; i++) { MySrvC* mysrvc = (MySrvC*)mysrvs->servers->index(i); diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index eb7705e990..9dae056eb7 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -724,7 +724,9 @@ void MySQL_HostGroups_Manager::wrlock() { #else spin_wrlock(&rwlock); #endif +#ifdef DEBUG is_locked = true; +#endif } void MySQL_HostGroups_Manager::p_update_mysql_error_counter(p_mysql_error_type err_type, unsigned int hid, char* address, uint16_t port, unsigned int code) { @@ -759,7 +761,9 @@ void MySQL_HostGroups_Manager::p_update_mysql_error_counter(p_mysql_error_type e } void MySQL_HostGroups_Manager::wrunlock() { +#ifdef DEBUG is_locked = false; +#endif #ifdef MHM_PTHREAD_MUTEX pthread_mutex_unlock(&lock); #else From 828ee01cdc4b7dbedc2faca394a5b31a71c664a5 Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Mon, 29 Apr 2024 12:33:37 +0500 Subject: [PATCH 8/9] Replaced monotonic_time() with time() --- include/MySQL_HostGroups_Manager.h | 2 +- lib/MyHGC.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/MySQL_HostGroups_Manager.h b/include/MySQL_HostGroups_Manager.h index b5d3cb854c..2909522fe6 100644 --- a/include/MySQL_HostGroups_Manager.h +++ b/include/MySQL_HostGroups_Manager.h @@ -316,7 +316,7 @@ class MyHGC { // MySQL Host Group Container public: unsigned int hid; std::atomic num_online_servers; - unsigned long long last_log_time_num_online_servers; + time_t last_log_time_num_online_servers; unsigned long long current_time_now; uint32_t new_connections_now; MySrvList *mysrvs; diff --git a/lib/MyHGC.cpp b/lib/MyHGC.cpp index 9919683cbf..66f296fd99 100644 --- a/lib/MyHGC.cpp +++ b/lib/MyHGC.cpp @@ -410,10 +410,10 @@ void MyHGC::refresh_online_server_count() { } void MyHGC::log_num_online_server_count_error() { - const unsigned long long curtime = monotonic_time(); + const time_t curtime = time(NULL); // if this is the first time the method is called or if more than 10 seconds have passed since the last log if (last_log_time_num_online_servers == 0 || - (curtime - last_log_time_num_online_servers > 10 * 1000 * 1000)) { + ((curtime - last_log_time_num_online_servers) > 10)) { last_log_time_num_online_servers = curtime; proxy_error( "Number of online servers detected in a hostgroup exceeds the configured maximum online servers. hostgroup:%u, num_online_servers:%u, max_online_servers:%u\n", From c7cf5ba344e0938085da3627e314da1dd0447090 Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Mon, 29 Apr 2024 16:50:28 +0500 Subject: [PATCH 9/9] Removed MySrvStatus class --- include/MySQL_HostGroups_Manager.h | 39 ++------- lib/MyHGC.cpp | 12 +-- lib/MySQL_HostGroups_Manager.cpp | 132 ++++++++++++++--------------- lib/MySQL_Thread.cpp | 2 +- lib/MySrvC.cpp | 13 +-- lib/MySrvConnList.cpp | 2 +- lib/mysql_connection.cpp | 20 ++--- 7 files changed, 93 insertions(+), 127 deletions(-) diff --git a/include/MySQL_HostGroups_Manager.h b/include/MySQL_HostGroups_Manager.h index 2909522fe6..fa86935907 100644 --- a/include/MySQL_HostGroups_Manager.h +++ b/include/MySQL_HostGroups_Manager.h @@ -193,39 +193,6 @@ class MySrvConnList { MySQL_Connection *index(unsigned int); }; -class MySrvC; -class MySrvStatus { -public: - explicit MySrvStatus(MySrvC* _mysrvc); - ~MySrvStatus(); - - MySrvStatus& operator=(MySerStatus _status); - - inline - bool operator==(MySerStatus _status) const { - return status == _status; - } - - inline - bool operator!=(MySerStatus _status) const { - return status!=_status; - } - - inline - explicit operator MySerStatus() const { - return status; - } - - inline - explicit operator int() const { - return (int)status; - } - -private: - MySrvC *mysrvc; - enum MySerStatus status; -}; - class MySrvC { // MySQL Server Container public: MyHGC *myhgc; @@ -234,7 +201,6 @@ class MySrvC { // MySQL Server Container uint16_t gtid_port; uint16_t flags; int64_t weight; - MySrvStatus status; unsigned int compression; int64_t max_connections; unsigned int aws_aurora_current_lag_us; @@ -296,6 +262,11 @@ class MySrvC { // MySQL Server Container max_connections_used = connections_used; return max_connections_used; } + void set_status(MySerStatus _status); + inline + MySerStatus get_status() const { return status; } +private: + enum MySerStatus status; }; class MySrvList { // MySQL Server List diff --git a/lib/MyHGC.cpp b/lib/MyHGC.cpp index 66f296fd99..72ce81f746 100644 --- a/lib/MyHGC.cpp +++ b/lib/MyHGC.cpp @@ -77,7 +77,7 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ //int j=0; for (j=0; jidx(j); - if (mysrvc->status==MYSQL_SERVER_STATUS_ONLINE) { // consider this server only if ONLINE + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_ONLINE) { // consider this server only if ONLINE if (mysrvc->myhgc->num_online_servers.load(std::memory_order_relaxed) <= mysrvc->myhgc->attributes.max_num_online_servers) { // number of online servers in HG is within configured range if (mysrvc->ConnectionsUsed->conns_length() < mysrvc->max_connections) { // consider this server only if didn't reach max_connections if (mysrvc->current_latency_us < (mysrvc->max_latency_us ? mysrvc->max_latency_us : mysql_thread___default_max_latency_ms*1000)) { // consider the host only if not too far @@ -113,7 +113,7 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ mysrvc->myhgc->log_num_online_server_count_error(); } } else { - if (mysrvc->status==MYSQL_SERVER_STATUS_SHUNNED) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_SHUNNED) { // try to recover shunned servers if (mysrvc->shunned_automatic && mysql_thread___shun_recovery_time_sec) { time_t t; @@ -138,7 +138,7 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ proxy_info("Unshunning server %s:%d.\n", mysrvc->address, mysrvc->port); } #endif - mysrvc->status=MYSQL_SERVER_STATUS_ONLINE; + mysrvc->set_status(MYSQL_SERVER_STATUS_ONLINE); mysrvc->shunned_automatic=false; mysrvc->shunned_and_kill_all_connections=false; mysrvc->connect_ERR_at_time_last_detected_error=0; @@ -229,9 +229,9 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ } for (j=0; jidx(j); - if (mysrvc->status==MYSQL_SERVER_STATUS_SHUNNED && mysrvc->shunned_automatic==true) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_SHUNNED && mysrvc->shunned_automatic == true) { if ((t - mysrvc->time_last_detected_error) > max_wait_sec) { - mysrvc->status=MYSQL_SERVER_STATUS_ONLINE; + mysrvc->set_status(MYSQL_SERVER_STATUS_ONLINE); mysrvc->shunned_automatic=false; mysrvc->connect_ERR_at_time_last_detected_error=0; mysrvc->time_last_detected_error=0; @@ -402,7 +402,7 @@ void MyHGC::refresh_online_server_count() { unsigned int online_servers_count = 0; for (unsigned int i = 0; i < mysrvs->servers->len; i++) { MySrvC* mysrvc = (MySrvC*)mysrvs->servers->index(i); - if (mysrvc->status == MYSQL_SERVER_STATUS_ONLINE) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_ONLINE) { online_servers_count++; } } diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index 376f7778eb..be931d31d9 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -1292,7 +1292,7 @@ bool MySQL_HostGroups_Manager::commit( long long ptr=atoll(r->fields[0]); proxy_warning("Removed server at address %lld, hostgroup %s, address %s port %s. Setting status OFFLINE HARD and immediately dropping all free connections. Used connections will be dropped when trying to use them\n", ptr, r->fields[1], r->fields[2], r->fields[3]); MySrvC *mysrvc=(MySrvC *)ptr; - mysrvc->status=MYSQL_SERVER_STATUS_OFFLINE_HARD; + mysrvc->set_status(MYSQL_SERVER_STATUS_OFFLINE_HARD); mysrvc->ConnectionsFree->drop_all_connections(); char *q1=(char *)"DELETE FROM mysql_servers WHERE mem_pointer=%lld"; char *q2=(char *)malloc(strlen(q1)+32); @@ -1338,14 +1338,14 @@ bool MySQL_HostGroups_Manager::commit( for (std::vector::iterator it = resultset->rows.begin() ; it != resultset->rows.end(); ++it) { SQLite3_row *r=*it; long long ptr=atoll(r->fields[12]); // increase this index every time a new column is added - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 5, "Server %s:%d , weight=%d, status=%d, mem_pointer=%llu, hostgroup=%d, compression=%d\n", r->fields[1], atoi(r->fields[2]), atoi(r->fields[4]), (MySerStatus) atoi(r->fields[5]), ptr, atoi(r->fields[0]), atoi(r->fields[6])); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 5, "Server %s:%d , weight=%d, status=%d, mem_pointer=%llu, hostgroup=%d, compression=%d\n", r->fields[1], atoi(r->fields[2]), atoi(r->fields[4]), atoi(r->fields[5]), ptr, atoi(r->fields[0]), atoi(r->fields[6])); //fprintf(stderr,"%lld\n", ptr); if (ptr==0) { if (GloMTH->variables.hostgroup_manager_verbose) { - proxy_info("Creating new server in HG %d : %s:%d , gtid_port=%d, weight=%d, status=%d\n", atoi(r->fields[0]), r->fields[1], atoi(r->fields[2]), atoi(r->fields[3]), atoi(r->fields[4]), (MySerStatus) atoi(r->fields[5])); + proxy_info("Creating new server in HG %d : %s:%d , gtid_port=%d, weight=%d, status=%d\n", atoi(r->fields[0]), r->fields[1], atoi(r->fields[2]), atoi(r->fields[3]), atoi(r->fields[4]), atoi(r->fields[5])); } - MySrvC *mysrvc=new MySrvC(r->fields[1], atoi(r->fields[2]), atoi(r->fields[3]), atoi(r->fields[4]), (MySerStatus) atoi(r->fields[5]), atoi(r->fields[6]), atoi(r->fields[7]), atoi(r->fields[8]), atoi(r->fields[9]), atoi(r->fields[10]), r->fields[11]); // add new fields here if adding more columns in mysql_servers - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 5, "Adding new server %s:%d , weight=%d, status=%d, mem_ptr=%p into hostgroup=%d\n", r->fields[1], atoi(r->fields[2]), atoi(r->fields[4]), (MySerStatus) atoi(r->fields[5]), mysrvc, atoi(r->fields[0])); + MySrvC *mysrvc=new MySrvC(r->fields[1], atoi(r->fields[2]), atoi(r->fields[3]), atoi(r->fields[4]), (MySerStatus)atoi(r->fields[5]), atoi(r->fields[6]), atoi(r->fields[7]), atoi(r->fields[8]), atoi(r->fields[9]), atoi(r->fields[10]), r->fields[11]); // add new fields here if adding more columns in mysql_servers + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 5, "Adding new server %s:%d , weight=%d, status=%d, mem_ptr=%p into hostgroup=%d\n", r->fields[1], atoi(r->fields[2]), atoi(r->fields[4]), atoi(r->fields[5]), mysrvc, atoi(r->fields[0])); add(mysrvc,atoi(r->fields[0])); ptr=(uintptr_t)mysrvc; rc=(*proxy_sqlite3_bind_int64)(statement1, 1, ptr); ASSERT_SQLITE_OK(rc, mydb); @@ -1378,7 +1378,7 @@ bool MySQL_HostGroups_Manager::commit( if (atoi(r->fields[5])!=atoi(r->fields[15])) { bool change_server_status = true; if (GloMTH->variables.evaluate_replication_lag_on_servers_load == 1) { - if (mysrvc->status == MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG && // currently server is shunned due to replication lag + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG && // currently server is shunned due to replication lag (MySerStatus)atoi(r->fields[15]) == MYSQL_SERVER_STATUS_ONLINE) { // new server status is online if (mysrvc->cur_replication_lag != -2) { // Master server? Seconds_Behind_Master column is not present const unsigned int new_max_repl_lag = atoi(r->fields[18]); @@ -1392,10 +1392,10 @@ bool MySQL_HostGroups_Manager::commit( } if (change_server_status == true) { if (GloMTH->variables.hostgroup_manager_verbose) - proxy_info("Changing status for server %d:%s:%d (%s:%d) from %d (%d) to %d\n", mysrvc->myhgc->hid, mysrvc->address, mysrvc->port, r->fields[1], atoi(r->fields[2]), atoi(r->fields[5]), (int)mysrvc->status, atoi(r->fields[15])); - mysrvc->status = (MySerStatus)atoi(r->fields[15]); + proxy_info("Changing status for server %d:%s:%d (%s:%d) from %d (%d) to %d\n", mysrvc->myhgc->hid, mysrvc->address, mysrvc->port, r->fields[1], atoi(r->fields[2]), atoi(r->fields[5]), (int)mysrvc->get_status(), atoi(r->fields[15])); + mysrvc->set_status((MySerStatus)atoi(r->fields[15])); } - if (mysrvc->status==MYSQL_SERVER_STATUS_SHUNNED) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_SHUNNED) { mysrvc->shunned_automatic=false; } } @@ -1414,11 +1414,11 @@ bool MySQL_HostGroups_Manager::commit( proxy_info("Changing max_replication_lag for server %u:%s:%d (%s:%d) from %d (%d) to %d\n" , mysrvc->myhgc->hid , mysrvc->address, mysrvc->port, r->fields[1], atoi(r->fields[2]), atoi(r->fields[8]) , mysrvc->max_replication_lag , atoi(r->fields[18])); mysrvc->max_replication_lag=atoi(r->fields[18]); if (mysrvc->max_replication_lag == 0) { // we just changed it to 0 - if (mysrvc->status == MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) { // the server is currently shunned due to replication lag // but we reset max_replication_lag to 0 // therefore we immediately reset the status too - mysrvc->status = MYSQL_SERVER_STATUS_ONLINE; + mysrvc->set_status(MYSQL_SERVER_STATUS_ONLINE); } } } @@ -1440,7 +1440,7 @@ bool MySQL_HostGroups_Manager::commit( } if (run_update) { rc=(*proxy_sqlite3_bind_int64)(statement2, 1, mysrvc->weight); ASSERT_SQLITE_OK(rc, mydb); - rc=(*proxy_sqlite3_bind_int64)(statement2, 2, (int)mysrvc->status); ASSERT_SQLITE_OK(rc, mydb); + rc=(*proxy_sqlite3_bind_int64)(statement2, 2, (int)mysrvc->get_status()); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement2, 3, mysrvc->compression); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement2, 4, mysrvc->max_connections); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement2, 5, mysrvc->max_replication_lag); ASSERT_SQLITE_OK(rc, mydb); @@ -1690,7 +1690,7 @@ void MySQL_HostGroups_Manager::generate_mysql_gtid_executed_tables() { } if (gtid_is) { gtid_is->active = true; - } else if (mysrvc->status != MYSQL_SERVER_STATUS_OFFLINE_HARD) { + } else if (mysrvc->get_status() != MYSQL_SERVER_STATUS_OFFLINE_HARD) { // we didn't find it. Create it /* struct ev_io *watcher = (struct ev_io *)malloc(sizeof(struct ev_io)); @@ -1745,7 +1745,7 @@ void MySQL_HostGroups_Manager::purge_mysql_servers_table() { MySrvC *mysrvc=NULL; for (unsigned int j=0; jmysrvs->servers->len; j++) { mysrvc=myhgc->mysrvs->idx(j); - if (mysrvc->status==MYSQL_SERVER_STATUS_OFFLINE_HARD) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_OFFLINE_HARD) { if (mysrvc->ConnectionsUsed->conns_length()==0 && mysrvc->ConnectionsFree->conns_length()==0) { // no more connections for OFFLINE_HARD server, removing it mysrvc=(MySrvC *)myhgc->mysrvs->servers->remove_index_fast(j); @@ -1800,7 +1800,7 @@ void MySQL_HostGroups_Manager::generate_mysql_servers_table(int *_onlyhg) { mysrvc=myhgc->mysrvs->idx(j); if (mysql_thread___hostgroup_manager_verbose) { char *st; - switch ((int)mysrvc->status) { + switch ((int)mysrvc->get_status()) { case 0: st=(char *)"ONLINE"; break; @@ -1830,7 +1830,7 @@ void MySQL_HostGroups_Manager::generate_mysql_servers_table(int *_onlyhg) { rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+3, mysrvc->port); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+4, mysrvc->gtid_port); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+5, mysrvc->weight); ASSERT_SQLITE_OK(rc, mydb); - rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+6, (int)mysrvc->status); ASSERT_SQLITE_OK(rc, mydb); + rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+6, (int)mysrvc->get_status()); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+7, mysrvc->compression); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+8, mysrvc->max_connections); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement32, (i*13)+9, mysrvc->max_replication_lag); ASSERT_SQLITE_OK(rc, mydb); @@ -1853,7 +1853,7 @@ void MySQL_HostGroups_Manager::generate_mysql_servers_table(int *_onlyhg) { rc=(*proxy_sqlite3_bind_int64)(statement1, 3, mysrvc->port); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement1, 4, mysrvc->gtid_port); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement1, 5, mysrvc->weight); ASSERT_SQLITE_OK(rc, mydb); - rc=(*proxy_sqlite3_bind_int64)(statement1, 6, (int)mysrvc->status); ASSERT_SQLITE_OK(rc, mydb); + rc=(*proxy_sqlite3_bind_int64)(statement1, 6, (int)mysrvc->get_status()); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement1, 7, mysrvc->compression); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement1, 8, mysrvc->max_connections); ASSERT_SQLITE_OK(rc, mydb); rc=(*proxy_sqlite3_bind_int64)(statement1, 9, mysrvc->max_replication_lag); ASSERT_SQLITE_OK(rc, mydb); @@ -2341,7 +2341,7 @@ void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c, bool _lo mysrvc = static_cast(c->parent); // Log debug information about the connection being returned to the pool - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySQL_Connection %p, server %s:%d with status %d\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->status); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySQL_Connection %p, server %s:%d with status %d\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->get_status()); // Remove the connection from the list of used connections for the parent server mysrvc->ConnectionsUsed->remove(c); @@ -2353,18 +2353,18 @@ void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c, bool _lo // If the largest query length exceeds the threshold, destroy the connection if (c->largest_query_length > (unsigned int)GloMTH->variables.threshold_query_length) { - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d . largest_query_length = %lu\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->status, c->largest_query_length); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d . largest_query_length = %lu\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->get_status(), c->largest_query_length); delete c; goto __exit_push_MyConn_to_pool; } // If the server is online and the connection is in the idle state - if (mysrvc->status==MYSQL_SERVER_STATUS_ONLINE) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_ONLINE) { if (c->async_state_machine==ASYNC_IDLE) { if (GloMTH == NULL) { goto __exit_push_MyConn_to_pool; } if (c->local_stmts->get_num_backend_stmts() > (unsigned int)GloMTH->variables.max_stmts_per_connection) { // Check if the connection has too many prepared statements // Log debug information about destroying the connection due to too many prepared statements - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d because has too many prepared statements\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->status); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d because has too many prepared statements\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->get_status()); // delete c; mysrvc->ConnectionsUsed->add(c); // Add the connection back to the list of used connections destroy_MyConn_from_pool(c, false); // Destroy the connection from the pool @@ -2374,12 +2374,12 @@ void MySQL_HostGroups_Manager::push_MyConn_to_pool(MySQL_Connection *c, bool _lo } } else { // Log debug information about destroying the connection - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->status); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->get_status()); delete c; // Destroy the connection } } else { // Log debug information about destroying the connection - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->status); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Destroying MySQL_Connection %p, server %s:%d with status %d\n", c, mysrvc->address, mysrvc->port, (int)mysrvc->get_status()); delete c; // Destroy the connection } @@ -2445,7 +2445,7 @@ void MySQL_HostGroups_Manager::unshun_server_all_hostgroups(const char * address bool found = false; // was this server already found in this hostgroup? for (j=0; found==false && j<(int)myhgc->mysrvs->cnt(); j++) { MySrvC *mysrvc=(MySrvC *)myhgc->mysrvs->servers->index(j); - if (mysrvc->status==MYSQL_SERVER_STATUS_SHUNNED) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_SHUNNED) { // we only care for SHUNNED nodes // Note that we check for address and port only for status==MYSQL_SERVER_STATUS_SHUNNED , // that means that potentially we will pass by the matching node and still looping . @@ -2463,7 +2463,7 @@ void MySQL_HostGroups_Manager::unshun_server_all_hostgroups(const char * address if (GloMTH->variables.hostgroup_manager_verbose >= 3) { proxy_info("Unshunning server %d:%s:%d . time_last_detected_error=%lu\n", mysrvc->myhgc->hid, address, port, mysrvc->time_last_detected_error); } - mysrvc->status=MYSQL_SERVER_STATUS_ONLINE; + mysrvc->set_status(MYSQL_SERVER_STATUS_ONLINE); mysrvc->shunned_automatic=false; mysrvc->shunned_and_kill_all_connections=false; mysrvc->connect_ERR_at_time_last_detected_error=0; @@ -2538,7 +2538,7 @@ MySQL_Connection * MySQL_HostGroups_Manager::get_MyConn_from_pool(unsigned int _ void MySQL_HostGroups_Manager::destroy_MyConn_from_pool(MySQL_Connection *c, bool _lock) { bool to_del=true; // the default, legacy behavior MySrvC *mysrvc=(MySrvC *)c->parent; - if (mysrvc->status==MYSQL_SERVER_STATUS_ONLINE && c->send_quit && queue.size() < __sync_fetch_and_add(&GloMTH->variables.connpoll_reset_queue_length,0)) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_ONLINE && c->send_quit && queue.size() < __sync_fetch_and_add(&GloMTH->variables.connpoll_reset_queue_length, 0)) { if (c->async_state_machine==ASYNC_IDLE) { // overall, the backend seems healthy and so it is the connection. Try to reset it int myerr=mysql_errno(c->mysql); @@ -2698,7 +2698,7 @@ void MySQL_HostGroups_Manager::replication_lag_action_inner(MyHGC *myhgc, const MySrvC *mysrvc=(MySrvC *)myhgc->mysrvs->servers->index(j); if (strcmp(mysrvc->address,address)==0 && mysrvc->port==port) { mysrvc->cur_replication_lag = current_replication_lag; - if (mysrvc->status==MYSQL_SERVER_STATUS_ONLINE) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_ONLINE) { if ( // (current_replication_lag==-1 ) // || @@ -2712,7 +2712,7 @@ void MySQL_HostGroups_Manager::replication_lag_action_inner(MyHGC *myhgc, const mysrvc->cur_replication_lag_count += 1; if (mysrvc->cur_replication_lag_count >= (unsigned int)mysql_thread___monitor_replication_lag_count) { proxy_warning("Shunning server %s:%d from HG %u with replication lag of %d second, count number: '%d'\n", address, port, myhgc->hid, current_replication_lag, mysrvc->cur_replication_lag_count); - mysrvc->status=MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG; + mysrvc->set_status(MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG); } else { proxy_info( "Not shunning server %s:%d from HG %u with replication lag of %d second, count number: '%d' < replication_lag_count: '%d'\n", @@ -2728,13 +2728,13 @@ void MySQL_HostGroups_Manager::replication_lag_action_inner(MyHGC *myhgc, const mysrvc->cur_replication_lag_count = 0; } } else { - if (mysrvc->status==MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) { if ( (current_replication_lag>=0 && ((unsigned int)current_replication_lag <= mysrvc->max_replication_lag)) || (current_replication_lag==-2) // see issue 959 ) { - mysrvc->status=MYSQL_SERVER_STATUS_ONLINE; + mysrvc->set_status(MYSQL_SERVER_STATUS_ONLINE); proxy_warning("Re-enabling server %s:%d from HG %u with replication lag of %d second\n", address, port, myhgc->hid, current_replication_lag); mysrvc->cur_replication_lag_count = 0; } @@ -2808,25 +2808,25 @@ void MySQL_HostGroups_Manager::group_replication_lag_action_set_server_status(My MySrvC *mysrvc=(MySrvC *)myhgc->mysrvs->servers->index(j); proxy_debug( PROXY_DEBUG_MONITOR, 6, "Server 'MySrvC' - address: %s, port: %d, status: %d\n", mysrvc->address, - mysrvc->port, (int)mysrvc->status + mysrvc->port, (int)mysrvc->get_status() ); if (strcmp(mysrvc->address,address)==0 && mysrvc->port==port) { if (enable == true) { - if (mysrvc->status==MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG || mysrvc->status==MYSQL_SERVER_STATUS_SHUNNED) { - mysrvc->status=MYSQL_SERVER_STATUS_ONLINE; + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG || mysrvc->get_status() == MYSQL_SERVER_STATUS_SHUNNED) { + mysrvc->set_status(MYSQL_SERVER_STATUS_ONLINE); proxy_info("Re-enabling server %u:%s:%d from replication lag\n", myhgc->hid, address, port); } } else { - if (mysrvc->status==MYSQL_SERVER_STATUS_ONLINE) { + if (mysrvc->get_status()==MYSQL_SERVER_STATUS_ONLINE) { proxy_warning("Shunning 'soft' server %u:%s:%d with replication lag, count number: %d\n", myhgc->hid, address, port, lag_count); - mysrvc->status=MYSQL_SERVER_STATUS_SHUNNED; + mysrvc->set_status(MYSQL_SERVER_STATUS_SHUNNED); } else { - if (mysrvc->status==MYSQL_SERVER_STATUS_SHUNNED) { + if (mysrvc->get_status()==MYSQL_SERVER_STATUS_SHUNNED) { if (lag_count >= ( mysql_thread___monitor_groupreplication_max_transactions_behind_count * 2 )) { proxy_warning("Shunning 'hard' server %u:%s:%d with replication lag, count number: %d\n", myhgc->hid, address, port, lag_count); - mysrvc->status=MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG; + mysrvc->set_status(MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG); } } } @@ -2911,7 +2911,7 @@ void MySQL_HostGroups_Manager::drop_all_idle_connections() { MyHGC *myhgc=(MyHGC *)MyHostGroups->index(i); for (j=0; j<(int)myhgc->mysrvs->cnt(); j++) { MySrvC *mysrvc=(MySrvC *)myhgc->mysrvs->servers->index(j); - if (mysrvc->status!=MYSQL_SERVER_STATUS_ONLINE) { + if (mysrvc->get_status()!=MYSQL_SERVER_STATUS_ONLINE) { proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 5, "Server %s:%d is not online\n", mysrvc->address, mysrvc->port); //__sync_fetch_and_sub(&status.server_connections_connected, mysrvc->ConnectionsFree->conns->len); mysrvc->ConnectionsFree->drop_all_connections(); @@ -3187,7 +3187,7 @@ SQLite3_result * MySQL_HostGroups_Manager::SQL3_Free_Connections() { MyHGC *myhgc=(MyHGC *)MyHostGroups->index(i); for (j=0; j<(int)myhgc->mysrvs->cnt(); j++) { MySrvC *mysrvc=(MySrvC *)myhgc->mysrvs->servers->index(j); - if (mysrvc->status!=MYSQL_SERVER_STATUS_ONLINE) { + if (mysrvc->get_status()!=MYSQL_SERVER_STATUS_ONLINE) { proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 5, "Server %s:%d is not online\n", mysrvc->address, mysrvc->port); mysrvc->ConnectionsFree->drop_all_connections(); } @@ -3386,7 +3386,7 @@ void MySQL_HostGroups_Manager::p_update_connection_pool() { // proxysql_connection_pool_status metric p_update_connection_pool_update_gauge(endpoint_id, common_labels, - status.p_connection_pool_status_map, ((int)mysrvc->status) + 1, p_hg_dyn_gauge::connection_pool_status); + status.p_connection_pool_status_map, ((int)mysrvc->get_status()) + 1, p_hg_dyn_gauge::connection_pool_status); } } @@ -3445,7 +3445,7 @@ SQLite3_result * MySQL_HostGroups_Manager::SQL3_Connection_Pool(bool _reset, int for (j=0; j<(int)myhgc->mysrvs->cnt(); j++) { MySrvC *mysrvc=(MySrvC *)myhgc->mysrvs->servers->index(j); if (hid == NULL) { - if (mysrvc->status!=MYSQL_SERVER_STATUS_ONLINE) { + if (mysrvc->get_status()!=MYSQL_SERVER_STATUS_ONLINE) { proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 5, "Server %s:%d is not online\n", mysrvc->address, mysrvc->port); //__sync_fetch_and_sub(&status.server_connections_connected, mysrvc->ConnectionsFree->conns->len); mysrvc->ConnectionsFree->drop_all_connections(); @@ -3469,7 +3469,7 @@ SQLite3_result * MySQL_HostGroups_Manager::SQL3_Connection_Pool(bool _reset, int pta[1]=strdup(mysrvc->address); sprintf(buf,"%d", mysrvc->port); pta[2]=strdup(buf); - switch ((int)mysrvc->status) { + switch ((int)mysrvc->get_status()) { case 0: pta[3]=strdup("ONLINE"); break; @@ -4057,16 +4057,16 @@ bool MySQL_HostGroups_Manager::shun_and_killall(char *hostname, int port) { for (j=0; jmysrvs->idx(j); if (mysrvc->port==port && strcmp(mysrvc->address,hostname)==0) { - switch ((MySerStatus)mysrvc->status) { + switch ((MySerStatus)mysrvc->get_status()) { case MYSQL_SERVER_STATUS_SHUNNED: if (mysrvc->shunned_automatic==false) { break; } case MYSQL_SERVER_STATUS_ONLINE: - if (mysrvc->status == MYSQL_SERVER_STATUS_ONLINE) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_ONLINE) { ret = true; } - mysrvc->status=MYSQL_SERVER_STATUS_SHUNNED; + mysrvc->set_status(MYSQL_SERVER_STATUS_SHUNNED); case MYSQL_SERVER_STATUS_OFFLINE_SOFT: mysrvc->shunned_automatic=true; mysrvc->shunned_and_kill_all_connections=true; @@ -4914,7 +4914,7 @@ void MySQL_HostGroups_Manager::update_group_replication_add_autodiscovered( // the servers to runtime. if (strcmp(mysrvc->address,_host.c_str())==0 && mysrvc->port==_port) { srv_found = true; - if (mysrvc->status == MYSQL_SERVER_STATUS_OFFLINE_HARD) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_OFFLINE_HARD) { reset_hg_attrs_server_defaults(mysrvc); update_hg_attrs_server_defaults(mysrvc, mysrvc->myhgc); proxy_info( @@ -4922,7 +4922,7 @@ void MySQL_HostGroups_Manager::update_group_replication_add_autodiscovered( " hostgroup=%d, weight=%ld, max_connections=%ld, use_ssl=%d\n", _host.c_str(), _port, reader_hg, mysrvc->weight, mysrvc->max_connections, mysrvc->use_ssl ); - mysrvc->status = MYSQL_SERVER_STATUS_ONLINE; + mysrvc->set_status(MYSQL_SERVER_STATUS_ONLINE); srv_found_offline = true; } } @@ -6594,22 +6594,22 @@ bool MySQL_HostGroups_Manager::aws_aurora_replication_lag_action(int _whid, int if (strcmp(mysrvc->address,address)==0 && mysrvc->port==port) { // we found the server if (enable==false) { - if (mysrvc->status == MYSQL_SERVER_STATUS_ONLINE) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_ONLINE) { if (verbose) { proxy_warning("Shunning server %s:%d from HG %u with replication lag of %f microseconds\n", address, port, myhgc->hid, current_replication_lag_ms); } - mysrvc->status = MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG; + mysrvc->set_status(MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG); } } else { - if (mysrvc->status == MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) { if (verbose) { proxy_warning("Re-enabling server %s:%d from HG %u with replication lag of %f microseconds\n", address, port, myhgc->hid, current_replication_lag_ms); } - mysrvc->status = MYSQL_SERVER_STATUS_ONLINE; + mysrvc->set_status(MYSQL_SERVER_STATUS_ONLINE); } } mysrvc->aws_aurora_current_lag_us = current_replication_lag_ms * 1000; - if (mysrvc->status == MYSQL_SERVER_STATUS_ONLINE || mysrvc->status == MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_ONLINE || mysrvc->get_status() == MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) { // we perform check only if ONLINE or lagging if (ret) { if (_whid==(int)myhgc->hid && is_writer==false) { @@ -6638,8 +6638,8 @@ bool MySQL_HostGroups_Manager::aws_aurora_replication_lag_action(int _whid, int if (is_writer==true) if (enable==true) if (_whid==(int)myhgc->hid) - if (mysrvc->status == MYSQL_SERVER_STATUS_OFFLINE_HARD) { - mysrvc->status = MYSQL_SERVER_STATUS_ONLINE; + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_OFFLINE_HARD) { + mysrvc->set_status(MYSQL_SERVER_STATUS_ONLINE); proxy_warning("Re-enabling server %s:%d from HG %u because it is a writer\n", address, port, myhgc->hid); ret = true; } @@ -6686,10 +6686,10 @@ int MySQL_HostGroups_Manager::create_new_server_in_hg( // 'servers_defaults' attributes from its corresponding 'MyHGC'. This way we ensure uniform behavior // of new servers, and 'OFFLINE_HARD' ones when a user update 'servers_defaults' values, and reloads // the servers to runtime. - if (mysrvc && mysrvc->status == MYSQL_SERVER_STATUS_OFFLINE_HARD) { + if (mysrvc && mysrvc->get_status() == MYSQL_SERVER_STATUS_OFFLINE_HARD) { reset_hg_attrs_server_defaults(mysrvc); update_hg_attrs_server_defaults(mysrvc, mysrvc->myhgc); - mysrvc->status = MYSQL_SERVER_STATUS_ONLINE; + mysrvc->set_status(MYSQL_SERVER_STATUS_ONLINE); proxy_info( "Found healthy previously discovered %s node %s:%d as 'OFFLINE_HARD', setting back as 'ONLINE' with:" @@ -6721,7 +6721,7 @@ int MySQL_HostGroups_Manager::remove_server_in_hg(uint32_t hid, const string& ad ); // Set the server status - mysrvc->status=MYSQL_SERVER_STATUS_OFFLINE_HARD; + mysrvc->set_status(MYSQL_SERVER_STATUS_OFFLINE_HARD); mysrvc->ConnectionsFree->drop_all_connections(); // TODO-NOTE: This is only required in case the caller isn't going to perform: @@ -7190,11 +7190,11 @@ void MySQL_HostGroups_Manager::HostGroup_Server_Mapping::copy_if_not_exists(Type for (auto& node : append) { - if (node.srv->status == MYSQL_SERVER_STATUS_SHUNNED || - node.srv->status == MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) { + if (node.srv->get_status() == MYSQL_SERVER_STATUS_SHUNNED || + node.srv->get_status() == MYSQL_SERVER_STATUS_SHUNNED_REPLICATION_LAG) { // Status updated from "*SHUNNED" to "ONLINE" as "read_only" value was successfully // retrieved from the backend server, indicating server is now online. - node.srv->status = MYSQL_SERVER_STATUS_ONLINE; + node.srv->set_status(MYSQL_SERVER_STATUS_ONLINE); } MySrvC* new_srv = insert_HGM(get_hostgroup_id(dest_type, node), node.srv); @@ -7254,7 +7254,7 @@ MySrvC* MySQL_HostGroups_Manager::HostGroup_Server_Mapping::insert_HGM(unsigned for (uint32_t j = 0; j < myhgc->mysrvs->cnt(); j++) { MySrvC* mysrvc = static_cast(myhgc->mysrvs->servers->index(j)); if (strcmp(mysrvc->address, srv->address) == 0 && mysrvc->port == srv->port) { - if (mysrvc->status == MYSQL_SERVER_STATUS_OFFLINE_HARD) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_OFFLINE_HARD) { mysrvc->gtid_port = srv->gtid_port; mysrvc->weight = srv->weight; @@ -7264,7 +7264,7 @@ MySrvC* MySQL_HostGroups_Manager::HostGroup_Server_Mapping::insert_HGM(unsigned mysrvc->use_ssl = srv->use_ssl; mysrvc->max_latency_us = srv->max_latency_us; mysrvc->comment = strdup(srv->comment); - mysrvc->status = MYSQL_SERVER_STATUS_ONLINE; + mysrvc->set_status(MYSQL_SERVER_STATUS_ONLINE); if (GloMTH->variables.hostgroup_manager_verbose) { proxy_info( @@ -7284,12 +7284,12 @@ MySrvC* MySQL_HostGroups_Manager::HostGroup_Server_Mapping::insert_HGM(unsigned if (!ret_srv) { if (GloMTH->variables.hostgroup_manager_verbose) { - proxy_info("Creating new server in HG %d : %s:%d , gtid_port=%d, weight=%ld, status=%d\n", hostgroup_id, srv->address, srv->port, srv->gtid_port, srv->weight, (int)srv->status); + proxy_info("Creating new server in HG %d : %s:%d , gtid_port=%d, weight=%ld, status=%d\n", hostgroup_id, srv->address, srv->port, srv->gtid_port, srv->weight, (int)srv->get_status()); } - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 5, "Adding new server %s:%d , weight=%ld, status=%d, mem_ptr=%p into hostgroup=%d\n", srv->address, srv->port, srv->weight, (int)srv->status, srv, hostgroup_id); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 5, "Adding new server %s:%d , weight=%ld, status=%d, mem_ptr=%p into hostgroup=%d\n", srv->address, srv->port, srv->weight, (int)srv->get_status(), srv, hostgroup_id); - ret_srv = new MySrvC(srv->address, srv->port, srv->gtid_port, srv->weight, (MySerStatus)srv->status, srv->compression, + ret_srv = new MySrvC(srv->address, srv->port, srv->gtid_port, srv->weight, srv->get_status(), srv->compression, srv->max_connections, srv->max_replication_lag, srv->use_ssl, (srv->max_latency_us / 1000), srv->comment); myhgc->mysrvs->add(ret_srv); @@ -7300,7 +7300,7 @@ MySrvC* MySQL_HostGroups_Manager::HostGroup_Server_Mapping::insert_HGM(unsigned void MySQL_HostGroups_Manager::HostGroup_Server_Mapping::remove_HGM(MySrvC* srv) { proxy_warning("Removed server at address %p, hostgroup %d, address %s port %d. Setting status OFFLINE HARD and immediately dropping all free connections. Used connections will be dropped when trying to use them\n", (void*)srv, srv->myhgc->hid, srv->address, srv->port); - srv->status = MYSQL_SERVER_STATUS_OFFLINE_HARD; + srv->set_status(MYSQL_SERVER_STATUS_OFFLINE_HARD); srv->ConnectionsFree->drop_all_connections(); } diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 167b2aa733..f6c54f0dbc 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -5706,7 +5706,7 @@ void MySQL_Thread::push_MyConn_local(MySQL_Connection *c) { mysrvc=(MySrvC *)c->parent; // reset insert_id #1093 c->mysql->insert_id = 0; - if (mysrvc->status==MYSQL_SERVER_STATUS_ONLINE) { + if (mysrvc->get_status() == MYSQL_SERVER_STATUS_ONLINE) { if (c->async_state_machine==ASYNC_IDLE) { cached_connections->add(c); return; // all went well diff --git a/lib/MySrvC.cpp b/lib/MySrvC.cpp index 07143dcaa5..860ae56efe 100644 --- a/lib/MySrvC.cpp +++ b/lib/MySrvC.cpp @@ -9,11 +9,12 @@ MySrvC::MySrvC( char* add, uint16_t p, uint16_t gp, int64_t _weight, enum MySerStatus _status, unsigned int _compression, int64_t _max_connections, unsigned int _max_replication_lag, int32_t _use_ssl, unsigned int _max_latency_ms, char* _comment -) : status(this) { +) { address=strdup(add); port=p; gtid_port=gp; weight=_weight; + status = _status; compression=_compression; max_connections=_max_connections; max_replication_lag=_max_replication_lag; @@ -39,7 +40,6 @@ MySrvC::MySrvC( comment=strdup(_comment); ConnectionsUsed=new MySrvConnList(this); ConnectionsFree=new MySrvConnList(this); - status = _status; } void MySrvC::connect_error(int err_num, bool get_mutex) { @@ -143,12 +143,7 @@ MySrvC::~MySrvC() { delete ConnectionsFree; } -MySrvStatus::MySrvStatus(MySrvC* _mysrvc) : mysrvc(_mysrvc) {} - -MySrvStatus::~MySrvStatus() { if (mysrvc->myhgc) mysrvc->myhgc->refresh_online_server_count(); } - -MySrvStatus& MySrvStatus::operator=(MySerStatus _status) { +void MySrvC::set_status(MySerStatus _status) { status = _status; - if (mysrvc->myhgc) mysrvc->myhgc->refresh_online_server_count(); - return *this; + if (myhgc)myhgc->refresh_online_server_count(); } diff --git a/lib/MySrvConnList.cpp b/lib/MySrvConnList.cpp index ace65635e2..e2175c830b 100644 --- a/lib/MySrvConnList.cpp +++ b/lib/MySrvConnList.cpp @@ -40,7 +40,7 @@ MySrvConnList::~MySrvConnList() { } void MySrvConnList::drop_all_connections() { - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Dropping all connections (%u total) on MySrvConnList %p for server %s:%d , hostgroup=%d , status=%d\n", conns_length(), this, mysrvc->address, mysrvc->port, mysrvc->myhgc->hid, (int)mysrvc->status); + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Dropping all connections (%u total) on MySrvConnList %p for server %s:%d , hostgroup=%d , status=%d\n", conns_length(), this, mysrvc->address, mysrvc->port, mysrvc->myhgc->hid, (int)mysrvc->get_status()); while (conns_length()) { MySQL_Connection *conn=(MySQL_Connection *)conns->remove_index_fast(0); delete conn; diff --git a/lib/mysql_connection.cpp b/lib/mysql_connection.cpp index e360f9ad60..246481195f 100644 --- a/lib/mysql_connection.cpp +++ b/lib/mysql_connection.cpp @@ -2035,7 +2035,7 @@ bool MySQL_Connection::IsServerOffline() { bool ret=false; if (parent==NULL) return ret; - server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=parent->get_status(); // we copy it here to avoid race condition. The caller will see this if ( (server_status==MYSQL_SERVER_STATUS_OFFLINE_HARD) // the server is OFFLINE as specific by the user || @@ -2059,7 +2059,7 @@ int MySQL_Connection::async_query(short event, char *stmt, unsigned long length, PROXY_TRACE2(); assert(mysql); assert(ret_mysql); - server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=parent->get_status(); // we copy it here to avoid race condition. The caller will see this if (IsServerOffline()) return -1; @@ -2198,7 +2198,7 @@ int MySQL_Connection::async_change_user(short event) { PROXY_TRACE(); assert(mysql); assert(ret_mysql); - server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=parent->get_status(); // we copy it here to avoid race condition. The caller will see this if (IsServerOffline()) return -1; @@ -2245,7 +2245,7 @@ int MySQL_Connection::async_select_db(short event) { PROXY_TRACE(); assert(mysql); assert(ret_mysql); - server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=parent->get_status(); // we copy it here to avoid race condition. The caller will see this if (IsServerOffline()) return -1; @@ -2286,7 +2286,7 @@ int MySQL_Connection::async_set_autocommit(short event, bool ac) { PROXY_TRACE(); assert(mysql); assert(ret_mysql); - server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=parent->get_status(); // we copy it here to avoid race condition. The caller will see this if (IsServerOffline()) return -1; @@ -2329,7 +2329,7 @@ int MySQL_Connection::async_set_names(short event, unsigned int c) { PROXY_TRACE(); assert(mysql); assert(ret_mysql); - server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=parent->get_status(); // we copy it here to avoid race condition. The caller will see this if (IsServerOffline()) return -1; @@ -2372,7 +2372,7 @@ int MySQL_Connection::async_set_option(short event, bool mask) { PROXY_TRACE(); assert(mysql); assert(ret_mysql); - server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=parent->get_status(); // we copy it here to avoid race condition. The caller will see this if (IsServerOffline()) return -1; @@ -2854,11 +2854,11 @@ int MySQL_Connection::async_send_simple_command(short event, char *stmt, unsigne PROXY_TRACE(); assert(mysql); assert(ret_mysql); - server_status=(MySerStatus)parent->status; // we copy it here to avoid race condition. The caller will see this + server_status=parent->get_status(); // we copy it here to avoid race condition. The caller will see this if ( - (parent->status==MYSQL_SERVER_STATUS_OFFLINE_HARD) // the server is OFFLINE as specific by the user + (parent->get_status()==MYSQL_SERVER_STATUS_OFFLINE_HARD) // the server is OFFLINE as specific by the user || - (parent->status==MYSQL_SERVER_STATUS_SHUNNED && parent->shunned_automatic==true && parent->shunned_and_kill_all_connections==true) // the server is SHUNNED due to a serious issue + (parent->get_status()==MYSQL_SERVER_STATUS_SHUNNED && parent->shunned_automatic==true && parent->shunned_and_kill_all_connections==true) // the server is SHUNNED due to a serious issue ) { return -1; }