Skip to content

Commit 2c46b1a

Browse files
committed
ref: revamp the weight==0 behaviour to avoid touching weights
1 parent dbe0269 commit 2c46b1a

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

lib/PgSQL_HostGroups_Manager.cpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,17 +1974,6 @@ PgSQL_SrvC *PgSQL_HGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid,
19741974
}
19751975
}
19761976
}
1977-
if (sum==0 && num_candidates > 0) {
1978-
proxy_info("No available server in hostgroup %u with weight > 0, but %u candidates found. This is probably due to max_connections reached for all servers. Using candidates with weight 0\n", hid, num_candidates);
1979-
// Iterate on all candidates and increase their weight
1980-
for (j=0; j<num_candidates; j++) {
1981-
mysrvc = mysrvcCandidates[j];
1982-
// increase the weight of the server
1983-
mysrvc->weight++;
1984-
proxy_debug("Increasing weight of server %s:%d to %ld\n", mysrvc->address, mysrvc->port, mysrvc->weight);
1985-
sum += 1;
1986-
}
1987-
}
19881977
if (sum==0) {
19891978
// per issue #531 , we try a desperate attempt to bring back online any shunned server
19901979
// we do this lowering the maximum wait time to 10%
@@ -2044,15 +2033,21 @@ PgSQL_SrvC *PgSQL_HGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid,
20442033
}
20452034
}
20462035
}
2036+
bool use_index_instead_of_weight = false;
20472037
if (sum==0) {
2048-
proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning PgSQL_SrvC NULL because no backend ONLINE or with weight\n");
2049-
if (l>32) {
2050-
free(mysrvcCandidates);
2038+
if (num_candidates==0) {
2039+
proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning PgSQL_SrvC NULL because no backend ONLINE or with weight\n");
2040+
if (l>32) {
2041+
free(mysrvcCandidates);
2042+
}
2043+
#ifdef TEST_AURORA
2044+
array_mysrvc_cands += num_candidates;
2045+
#endif // TEST_AURORA
2046+
return NULL; // if we reach here, we couldn't find any target
20512047
}
2052-
#ifdef TEST_AURORA
2053-
array_mysrvc_cands += num_candidates;
2054-
#endif // TEST_AURORA
2055-
return NULL; // if we reach here, we couldn't find any target
2048+
// All servers have weight 0, but we have some candidates - pick a random one (assume they all have weight 1)
2049+
proxy_info("No available server in hostgroup %u with weight > 0, but %u candidates found. This is probably due to max_connections reached for all servers. Using candidates with weight 0\n", hid, num_candidates);
2050+
use_index_instead_of_weight = true;
20562051
}
20572052

20582053
/*
@@ -2077,7 +2072,7 @@ PgSQL_SrvC *PgSQL_HGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid,
20772072
}
20782073
*/
20792074

2080-
unsigned int New_sum=sum;
2075+
unsigned int New_sum=use_index_instead_of_weight ? num_candidates : sum;
20812076

20822077
if (New_sum==0) {
20832078
proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning PgSQL_SrvC NULL because no backend ONLINE or with weight\n");
@@ -2125,7 +2120,7 @@ PgSQL_SrvC *PgSQL_HGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid,
21252120
New_sum = 0;
21262121
for (j=0; j<num_candidates; j++) {
21272122
mysrvc = mysrvcCandidates[j];
2128-
New_sum+=mysrvc->weight;
2123+
New_sum+=use_index_instead_of_weight ? 1 : mysrvc->weight;
21292124
}
21302125
}
21312126
}
@@ -2143,7 +2138,7 @@ PgSQL_SrvC *PgSQL_HGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid,
21432138

21442139
for (j=0; j<num_candidates; j++) {
21452140
mysrvc = mysrvcCandidates[j];
2146-
New_sum+=mysrvc->weight;
2141+
New_sum+=use_index_instead_of_weight ? 1 : mysrvc->weight;
21472142
if (k<=New_sum) {
21482143
proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning PgSQL_SrvC %p, server %s:%d\n", mysrvc, mysrvc->address, mysrvc->port);
21492144
if (l>32) {

0 commit comments

Comments
 (0)