From d76b79c7fb86400ebb8f2c14e0b27c2e908fcd5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Sun, 24 May 2020 01:30:42 +0200 Subject: [PATCH 1/2] Removed define USE_MYSRVC_ARRAY Removed define USE_MYSRVC_ARRAY from MySQL_HostGroups_Manager , making it default --- lib/MySQL_HostGroups_Manager.cpp | 119 +++---------------------------- 1 file changed, 10 insertions(+), 109 deletions(-) diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index c174b3800..f70dfd09c 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -27,12 +27,10 @@ #include -#define USE_MYSRVC_ARRAY - -#ifdef USE_MYSRVC_ARRAY +#ifdef TEST_AURORA static unsigned long long array_mysrvc_total = 0; static unsigned long long array_mysrvc_cands = 0; -#endif // USE_MYSRVC_ARRAY +#endif // TEST_AURORA #define SAFE_SQLITE3_STEP(_stmt) do {\ do {\ @@ -2655,7 +2653,6 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ unsigned int sum=0; unsigned int TotalUsedConn=0; unsigned int l=mysrvs->cnt(); -#ifdef USE_MYSRVC_ARRAY #ifdef TEST_AURORA unsigned long long a1 = array_mysrvc_total/10000; array_mysrvc_total += l; @@ -2670,7 +2667,6 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ if (l>32) { mysrvcCandidates = (MySrvC **)malloc(sizeof(MySrvC *)*l); } -#endif // USE_MYSRVC_ARRAY if (l) { //int j=0; for (j=0; jgtid_exists(mysrvc, gtid_uuid, gtid_trxid)) { sum+=mysrvc->weight; TotalUsedConn+=mysrvc->ConnectionsUsed->conns_length(); -#ifdef USE_MYSRVC_ARRAY mysrvcCandidates[num_candidates]=mysrvc; num_candidates++; -#endif // USE_MYSRVC_ARRAY } } else { if (max_lag_ms >= 0) { if (max_lag_ms >= mysrvc->aws_aurora_current_lag_us/1000) { sum+=mysrvc->weight; TotalUsedConn+=mysrvc->ConnectionsUsed->conns_length(); -#ifdef USE_MYSRVC_ARRAY mysrvcCandidates[num_candidates]=mysrvc; num_candidates++; -#endif // USE_MYSRVC_ARRAY } else { sess->thread->status_variables.stvar[st_var_aws_aurora_replicas_skipped_during_query]++; } } else { sum+=mysrvc->weight; TotalUsedConn+=mysrvc->ConnectionsUsed->conns_length(); -#ifdef USE_MYSRVC_ARRAY mysrvcCandidates[num_candidates]=mysrvc; num_candidates++; -#endif // USE_MYSRVC_ARRAY } } } @@ -2742,28 +2732,22 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ if (MyHGM->gtid_exists(mysrvc, gtid_uuid, gtid_trxid)) { sum+=mysrvc->weight; TotalUsedConn+=mysrvc->ConnectionsUsed->conns_length(); -#ifdef USE_MYSRVC_ARRAY mysrvcCandidates[num_candidates]=mysrvc; num_candidates++; -#endif // USE_MYSRVC_ARRAY } } else { if (max_lag_ms >= 0) { if (max_lag_ms >= mysrvc->aws_aurora_current_lag_us/1000) { sum+=mysrvc->weight; TotalUsedConn+=mysrvc->ConnectionsUsed->conns_length(); -#ifdef USE_MYSRVC_ARRAY mysrvcCandidates[num_candidates]=mysrvc; num_candidates++; -#endif // USE_MYSRVC_ARRAY } } else { sum+=mysrvc->weight; TotalUsedConn+=mysrvc->ConnectionsUsed->conns_length(); -#ifdef USE_MYSRVC_ARRAY mysrvcCandidates[num_candidates]=mysrvc; num_candidates++; -#endif // USE_MYSRVC_ARRAY } } } @@ -2773,7 +2757,6 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ } } } -#ifdef USE_MYSRVC_ARRAY if (max_lag_ms) { // we are using AWS Aurora, as this logic is implemented only here unsigned int min_num_replicas = sess->thread->variables.aurora_max_lag_ms_only_read_from_replicas; if (min_num_replicas) { @@ -2800,7 +2783,6 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ } } } -#endif // USE_MYSRVC_ARRAY if (sum==0) { // per issue #531 , we try a desperate attempt to bring back online any shunned server // we do this lowering the maximum wait time to 10% @@ -2825,28 +2807,22 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ if (MyHGM->gtid_exists(mysrvc, gtid_uuid, gtid_trxid)) { sum+=mysrvc->weight; TotalUsedConn+=mysrvc->ConnectionsUsed->conns_length(); -#ifdef USE_MYSRVC_ARRAY mysrvcCandidates[num_candidates]=mysrvc; num_candidates++; -#endif // USE_MYSRVC_ARRAY } } else { if (max_lag_ms >= 0) { if (max_lag_ms >= mysrvc->aws_aurora_current_lag_us/1000) { sum+=mysrvc->weight; TotalUsedConn+=mysrvc->ConnectionsUsed->conns_length(); -#ifdef USE_MYSRVC_ARRAY mysrvcCandidates[num_candidates]=mysrvc; num_candidates++; -#endif // USE_MYSRVC_ARRAY } } else { sum+=mysrvc->weight; TotalUsedConn+=mysrvc->ConnectionsUsed->conns_length(); -#ifdef USE_MYSRVC_ARRAY mysrvcCandidates[num_candidates]=mysrvc; num_candidates++; -#endif // USE_MYSRVC_ARRAY } } } @@ -2856,12 +2832,12 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ } if (sum==0) { proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySrvC NULL because no backend ONLINE or with weight\n"); -#ifdef USE_MYSRVC_ARRAY if (l>32) { free(mysrvcCandidates); } +#ifdef TEST_AURORA array_mysrvc_cands += num_candidates; -#endif // USE_MYSRVC_ARRAY +#endif // TEST_AURORA return NULL; // if we reach here, we couldn't find any target } @@ -2869,45 +2845,13 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ unsigned int New_TotalUsedConn=0; // we will now scan again to ignore overloaded servers -#ifdef USE_MYSRVC_ARRAY for (j=0; jidx(j); - if (mysrvc->status==MYSQL_SERVER_STATUS_ONLINE) { // consider this server only if ONLINE -#endif // USE_MYSRVC_ARRAY unsigned int len=mysrvc->ConnectionsUsed->conns_length(); -#ifdef USE_MYSRVC_ARRAY -#else - - if (len < 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 -#endif // USE_MYSRVC_ARRAY if ((len * sum) <= (TotalUsedConn * mysrvc->weight * 1.5 + 1)) { -#ifdef USE_MYSRVC_ARRAY New_sum+=mysrvc->weight; New_TotalUsedConn+=len; -#else - if (gtid_trxid) { - if (MyHGM->gtid_exists(mysrvc, gtid_uuid, gtid_trxid)) { - New_sum+=mysrvc->weight; - New_TotalUsedConn+=mysrvc->ConnectionsUsed->conns_length(); - } - } else { - if (max_lag_ms >= 0) { - if (max_lag_ms >= mysrvc->aws_aurora_current_lag_us/1000) { - New_sum+=mysrvc->weight; - New_TotalUsedConn+=len; - } - } else { - New_sum+=mysrvc->weight; - New_TotalUsedConn+=len; - } - } -#endif // USE_MYSRVC_ARRAY -#ifdef USE_MYSRVC_ARRAY } else { // remove the candidate if (j+1 < num_candidates) { @@ -2915,29 +2859,21 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ } j--; num_candidates--; -#endif // USE_MYSRVC_ARRAY } -#ifdef USE_MYSRVC_ARRAY -#else - } - } - } -#endif // USE_MYSRVC_ARRAY } if (New_sum==0) { proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySrvC NULL because no backend ONLINE or with weight\n"); -#ifdef USE_MYSRVC_ARRAY if (l>32) { free(mysrvcCandidates); } +#ifdef TEST_AURORA array_mysrvc_cands += num_candidates; -#endif // USE_MYSRVC_ARRAY +#endif // TEST_AURORA return NULL; // if we reach here, we couldn't find any target } -#ifdef USE_MYSRVC_ARRAY // latency awareness algorithm is enabled only when compiled with USE_MYSRVC_ARRAY if (sess->thread->variables.min_num_servers_lantency_awareness) { if (num_candidates >= sess->thread->variables.min_num_servers_lantency_awareness) { @@ -2978,7 +2914,6 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ } } } -#endif // USE_MYSRVC_ARRAY unsigned int k; @@ -2990,62 +2925,28 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ k++; New_sum=0; -#ifdef USE_MYSRVC_ARRAY for (j=0; jidx(j); - if (mysrvc->status==MYSQL_SERVER_STATUS_ONLINE) { // consider this server only if ONLINE - unsigned int len=mysrvc->ConnectionsUsed->conns_length(); - if (len < 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 ((len * sum) <= (TotalUsedConn * mysrvc->weight * 1.5 + 1)) { -#endif // USE_MYSRVC_ARRAY -#ifdef USE_MYSRVC_ARRAY New_sum+=mysrvc->weight; -#else - if (gtid_trxid) { - if (MyHGM->gtid_exists(mysrvc, gtid_uuid, gtid_trxid)) { - New_sum+=mysrvc->weight; - //TotalUsedConn+=mysrvc->ConnectionsUsed->conns_length(); // this line is a bug - } - } else { - if (max_lag_ms >= 0) { - if (max_lag_ms >= mysrvc->aws_aurora_current_lag_us/1000) { - New_sum+=mysrvc->weight; - } - } else { - New_sum+=mysrvc->weight; - } - } -#endif // USE_MYSRVC_ARRAY if (k<=New_sum) { proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySrvC %p, server %s:%d\n", mysrvc, mysrvc->address, mysrvc->port); -#ifdef USE_MYSRVC_ARRAY if (l>32) { free(mysrvcCandidates); } +#ifdef TEST_AURORA array_mysrvc_cands += num_candidates; -#endif // USE_MYSRVC_ARRAY +#endif // TEST_AURORA return mysrvc; } -#ifdef USE_MYSRVC_ARRAY -#else - } - } - } - } -#endif // USE_MYSRVC_ARRAY } } proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySrvC NULL\n"); -#ifdef USE_MYSRVC_ARRAY if (l>32) { free(mysrvcCandidates); } +#ifdef TEST_AURORA array_mysrvc_cands += num_candidates; -#endif // USE_MYSRVC_ARRAY +#endif // TEST_AURORA return NULL; // if we reach here, we couldn't find any target } From 1c0bd25658311c24bf18f07923d3cef48d551e07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Sun, 24 May 2020 11:53:04 +0200 Subject: [PATCH 2/2] Fixing some indentation --- lib/MySQL_HostGroups_Manager.cpp | 42 ++++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index f70dfd09c..e6925597c 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -2847,19 +2847,19 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ // we will now scan again to ignore overloaded servers for (j=0; jConnectionsUsed->conns_length(); - if ((len * sum) <= (TotalUsedConn * mysrvc->weight * 1.5 + 1)) { + unsigned int len=mysrvc->ConnectionsUsed->conns_length(); + if ((len * sum) <= (TotalUsedConn * mysrvc->weight * 1.5 + 1)) { - New_sum+=mysrvc->weight; - New_TotalUsedConn+=len; - } else { - // remove the candidate - if (j+1 < num_candidates) { - mysrvcCandidates[j] = mysrvcCandidates[num_candidates-1]; - } - j--; - num_candidates--; - } + New_sum+=mysrvc->weight; + New_TotalUsedConn+=len; + } else { + // remove the candidate + if (j+1 < num_candidates) { + mysrvcCandidates[j] = mysrvcCandidates[num_candidates-1]; + } + j--; + num_candidates--; + } } @@ -2927,17 +2927,17 @@ MySrvC *MyHGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid, int max_ for (j=0; jweight; - if (k<=New_sum) { - proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySrvC %p, server %s:%d\n", mysrvc, mysrvc->address, mysrvc->port); - if (l>32) { - free(mysrvcCandidates); - } + New_sum+=mysrvc->weight; + if (k<=New_sum) { + proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySrvC %p, server %s:%d\n", mysrvc, mysrvc->address, mysrvc->port); + if (l>32) { + free(mysrvcCandidates); + } #ifdef TEST_AURORA - array_mysrvc_cands += num_candidates; + array_mysrvc_cands += num_candidates; #endif // TEST_AURORA - return mysrvc; - } + return mysrvc; + } } } proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySrvC NULL\n");