From f815b778126b517ae1f07a2a4e33701c79f33efe Mon Sep 17 00:00:00 2001 From: Wazir Ahmed Date: Mon, 20 Apr 2026 18:20:37 +0000 Subject: [PATCH] fix: make server_backoff_time atomic Signed-off-by: Wazir Ahmed --- include/MySQL_HostGroups_Manager.h | 2 +- lib/MyHGC.cpp | 5 ++++- lib/MySQL_Session.cpp | 6 ++++-- lib/MySQL_Thread.cpp | 7 ++++++- lib/MySrvC.cpp | 2 +- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/MySQL_HostGroups_Manager.h b/include/MySQL_HostGroups_Manager.h index 4c2bb6fde..9f30f3bf9 100644 --- a/include/MySQL_HostGroups_Manager.h +++ b/include/MySQL_HostGroups_Manager.h @@ -212,7 +212,7 @@ class MySrvC { // MySQL Server Container // This is primarily used when `session_track_variables::ENFORCED` mode is active. // If a server lacks the required capabilities in this mode, it is temporarily // excluded from selection for a specified duration. - unsigned long long server_backoff_time; + std::atomic server_backoff_time; MySrvConnList *ConnectionsUsed; MySrvConnList *ConnectionsFree; diff --git a/lib/MyHGC.cpp b/lib/MyHGC.cpp index 8208f7707..c7ae3e203 100644 --- a/lib/MyHGC.cpp +++ b/lib/MyHGC.cpp @@ -18,6 +18,7 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ unsigned int TotalUsedConn=0; unsigned int l=mysrvs->cnt(); static time_t last_hg_log = 0; + unsigned long long server_backoff_time; #ifdef TEST_AURORA unsigned long long a1 = array_mysrvc_total/10000; array_mysrvc_total += l; @@ -39,8 +40,10 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ mysrvc=mysrvs->idx(j); if (mysrvc->get_status() == MYSQL_SERVER_STATUS_ONLINE) { // consider this server only if ONLINE // skip servers that are in backoff period - if (mysrvc->server_backoff_time > sess->thread->curtime) + server_backoff_time = mysrvc->server_backoff_time.load(std::memory_order_relaxed); + if (server_backoff_time > sess->thread->curtime) { continue; + } 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 diff --git a/lib/MySQL_Session.cpp b/lib/MySQL_Session.cpp index 8e30a1cdb..60aa6ad92 100644 --- a/lib/MySQL_Session.cpp +++ b/lib/MySQL_Session.cpp @@ -1,4 +1,5 @@ #include "../deps/json/json.hpp" +#include using json = nlohmann::json; #define PROXYJSON @@ -9764,8 +9765,9 @@ bool MySQL_Session::handle_session_track_capabilities() { } if (!server_support_session_track) { - // be_conn->parent->server_backoff_time = thread->curtime + (600 * 1000000); // 10 minutes - be_conn->parent->server_backoff_time = thread->curtime + (30 * 1000000); // 30 seconds + // set 30 seconds backoff time + be_conn->parent->server_backoff_time.store(thread->curtime + (30 * 1000000), std::memory_order_relaxed); + if (session_fast_forward) { mybe->server_myds->destroy_MySQL_Connection_From_Pool(false); } else { diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index fa5024b35..5c64354fe 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -1,4 +1,5 @@ #include "../deps/json/json.hpp" +#include using json = nlohmann::json; #define PROXYJSON @@ -6388,13 +6389,17 @@ MySQL_Connection * MySQL_Thread::get_MyConn_local(unsigned int _hid, MySQL_Sessi if (sess->client_myds->myconn == NULL) return NULL; if (sess->client_myds->myconn->userinfo == NULL) return NULL; unsigned int i; + unsigned long long server_backoff_time; std::vector parents; // this is a vector of srvers that needs to be excluded in case gtid_uuid is used MySQL_Connection *c=NULL; for (i=0; ilen; i++) { c = (MySQL_Connection *) cached_connections->index(i); + // skip servers that are in backoff period - if (c->parent->server_backoff_time > curtime) + server_backoff_time = c->parent->server_backoff_time.load(std::memory_order_relaxed); + if (server_backoff_time > curtime) { continue; + } if (c->parent->myhgc->hid==_hid && sess->client_myds->myconn->match_tracked_options(c)) { // options are all identical if ( diff --git a/lib/MySrvC.cpp b/lib/MySrvC.cpp index 1e94b41aa..4f8cd2db9 100644 --- a/lib/MySrvC.cpp +++ b/lib/MySrvC.cpp @@ -31,7 +31,7 @@ MySrvC::MySrvC( bytes_recv=0; max_connections_used=0; queries_gtid_sync=0; - server_backoff_time = 0; + server_backoff_time.store(0); time_last_detected_error=0; connect_ERR_at_time_last_detected_error=0; shunned_automatic=false;