Skip to content

Commit 0158e98

Browse files
authored
Merge pull request #4733 from sysown/v2.7_issue_4707_threshold_resultset_size
Fix Overflow in mysql_thread___threshold_resultset_size During Multiplication
2 parents b95f1dd + 936c721 commit 0158e98

File tree

4 files changed

+130
-5
lines changed

4 files changed

+130
-5
lines changed

include/gen_utils.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,18 @@ inline unsigned long long realtime_time() {
314314
return (((unsigned long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000);
315315
}
316316

317+
template<int FACTOR, typename T>
318+
inline T overflow_safe_multiply(T val) {
319+
static_assert(std::is_integral<T>::value, "T must be an integer type.");
320+
static_assert(std::is_unsigned_v<T>, "T must be an unsigned integer type.");
321+
static_assert(FACTOR > 0, "Negative factors are not supported.");
322+
323+
if constexpr (FACTOR == 0) return 0;
324+
if (val == 0) return 0;
325+
if (val > std::numeric_limits<T>::max() / FACTOR) return std::numeric_limits<T>::max();
326+
return (val * FACTOR);
327+
}
328+
317329
#endif /* __GEN_FUNCTIONS */
318330

319331
bool Proxy_file_exists(const char *);

lib/MySQL_Thread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5995,7 +5995,7 @@ bool MySQL_Thread::set_backend_to_be_skipped_if_frontend_is_slow(MySQL_Data_Stre
59955995
// we pause receiving from backend at mysql_thread___threshold_resultset_size * 8
59965996
// but assuming that client isn't completely blocked, we will stop checking for data
59975997
// only at mysql_thread___threshold_resultset_size * 4
5998-
if (buffered_data > (unsigned int)mysql_thread___threshold_resultset_size*4) {
5998+
if (buffered_data > overflow_safe_multiply<4,unsigned int>(mysql_thread___threshold_resultset_size)) {
59995999
mypolls.fds[n].events = 0;
60006000
return true;
60016001
}

lib/mysql_connection.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ MDB_ASYNC_ST MySQL_Connection::handler(short event) {
15091509
unsigned int buffered_data=0;
15101510
buffered_data = myds->sess->client_myds->PSarrayOUT->len * RESULTSET_BUFLEN;
15111511
buffered_data += myds->sess->client_myds->resultset->len * RESULTSET_BUFLEN;
1512-
if (buffered_data > (unsigned int)mysql_thread___threshold_resultset_size*8) {
1512+
if (buffered_data > overflow_safe_multiply<8,unsigned int>(mysql_thread___threshold_resultset_size)) {
15131513
next_event(ASYNC_STMT_EXECUTE_STORE_RESULT_CONT); // we temporarily pause . See #1232
15141514
break;
15151515
}
@@ -1535,7 +1535,7 @@ MDB_ASYNC_ST MySQL_Connection::handler(short event) {
15351535
if (rows_read_inner > 1) {
15361536
process_rows_in_ASYNC_STMT_EXECUTE_STORE_RESULT_CONT(processed_bytes);
15371537
if (
1538-
(processed_bytes > (unsigned int)mysql_thread___threshold_resultset_size*8)
1538+
(processed_bytes > overflow_safe_multiply<8,unsigned int>(mysql_thread___threshold_resultset_size))
15391539
||
15401540
( mysql_thread___throttle_ratio_server_to_client && mysql_thread___throttle_max_bytes_per_second_to_client && (processed_bytes > (unsigned long long)mysql_thread___throttle_max_bytes_per_second_to_client/10*(unsigned long long)mysql_thread___throttle_ratio_server_to_client) )
15411541
) {
@@ -1688,7 +1688,7 @@ MDB_ASYNC_ST MySQL_Connection::handler(short event) {
16881688
unsigned int buffered_data=0;
16891689
buffered_data = myds->sess->client_myds->PSarrayOUT->len * RESULTSET_BUFLEN;
16901690
buffered_data += myds->sess->client_myds->resultset->len * RESULTSET_BUFLEN;
1691-
if (buffered_data > (unsigned int)mysql_thread___threshold_resultset_size*8) {
1691+
if (buffered_data > overflow_safe_multiply<8,unsigned int>(mysql_thread___threshold_resultset_size)) {
16921692
next_event(ASYNC_USE_RESULT_CONT); // we temporarily pause . See #1232
16931693
break;
16941694
}
@@ -1742,7 +1742,7 @@ MDB_ASYNC_ST MySQL_Connection::handler(short event) {
17421742
bytes_info.bytes_recv += br;
17431743
processed_bytes+=br; // issue #527 : this variable will store the amount of bytes processed during this event
17441744
if (
1745-
(processed_bytes > (unsigned int)mysql_thread___threshold_resultset_size*8)
1745+
(processed_bytes > overflow_safe_multiply<8,unsigned int>(mysql_thread___threshold_resultset_size))
17461746
||
17471747
( mysql_thread___throttle_ratio_server_to_client && mysql_thread___throttle_max_bytes_per_second_to_client && (processed_bytes > (unsigned long long)mysql_thread___throttle_max_bytes_per_second_to_client/10*(unsigned long long)mysql_thread___throttle_ratio_server_to_client) )
17481748
) {
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* @file mysql-reg_test_4707_threshold_resultset_size-t.cpp
3+
* @brief The test specifically examines the impact of different mysql-threshold_resultset_size threshold values on query response times
4+
* and addresses an identified issue caused by variable overflow, which results in slow performance.
5+
*/
6+
7+
#include <string>
8+
#include <sstream>
9+
#include <chrono>
10+
#include "mysql.h"
11+
#include "command_line.h"
12+
#include "tap.h"
13+
#include "utils.h"
14+
15+
CommandLine cl;
16+
17+
int main(int argc, char** argv) {
18+
19+
plan(6); // Total number of tests planned
20+
21+
if (cl.getEnv())
22+
return exit_status();
23+
24+
// Initialize Admin connection
25+
MYSQL* proxysql_admin = mysql_init(NULL);
26+
if (!proxysql_admin) {
27+
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_admin));
28+
return -1;
29+
}
30+
31+
// Connnect to ProxySQL Admin
32+
if (!mysql_real_connect(proxysql_admin, cl.admin_host, cl.admin_username, cl.admin_password, NULL, cl.admin_port, NULL, 0)) {
33+
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_admin));
34+
return -1;
35+
}
36+
37+
// Initialize Backend connection
38+
MYSQL* proxysql_backend = mysql_init(NULL);
39+
if (!proxysql_backend) {
40+
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_backend));
41+
return -1;
42+
}
43+
44+
// Connnect to ProxySQL Backend
45+
if (!mysql_real_connect(proxysql_backend, cl.host, cl.username, cl.password, NULL, cl.port, NULL, 0)) {
46+
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_backend));
47+
return -1;
48+
}
49+
MYSQL_QUERY(proxysql_admin, "DELETE FROM mysql_query_rules");
50+
MYSQL_QUERY(proxysql_admin, "LOAD MYSQL QUERY RULES TO RUNTIME");
51+
MYSQL_QUERY(proxysql_admin, "SET mysql-poll_timeout=2000");
52+
MYSQL_QUERY(proxysql_admin, "SET mysql-threshold_resultset_size=8000");
53+
MYSQL_QUERY(proxysql_admin, "LOAD MYSQL VARIABLES TO RUNTIME");
54+
55+
int rc;
56+
57+
auto start = std::chrono::high_resolution_clock::now();
58+
rc = mysql_query(proxysql_backend, "SELECT 1");
59+
auto end = std::chrono::high_resolution_clock::now();
60+
61+
if (rc == 0) {
62+
MYSQL_RES* res = mysql_store_result(proxysql_backend);
63+
ok(res != nullptr, "Query executed successfully. %s", mysql_error(proxysql_backend));
64+
mysql_free_result(res);
65+
}
66+
else {
67+
ok(false, "Error executing query. %s", mysql_error(proxysql_admin));
68+
}
69+
70+
std::chrono::duration<double, std::milli> duration = end - start;
71+
ok(duration.count() < 10.00, "Execution time should be less than 10 ms. Actual: %f ms", duration.count());
72+
73+
MYSQL_QUERY(proxysql_admin, "SET mysql-threshold_resultset_size=536870912");
74+
MYSQL_QUERY(proxysql_admin, "LOAD MYSQL VARIABLES TO RUNTIME");
75+
76+
start = std::chrono::high_resolution_clock::now();
77+
rc = mysql_query(proxysql_backend, "SELECT 1");
78+
end = std::chrono::high_resolution_clock::now();
79+
80+
if (rc == 0) {
81+
MYSQL_RES* res = mysql_store_result(proxysql_backend);
82+
ok(res != nullptr, "Query executed successfully. %s", mysql_error(proxysql_backend));
83+
mysql_free_result(res);
84+
}
85+
else {
86+
ok(false, "Error executing query. %s", mysql_error(proxysql_admin));
87+
}
88+
duration = end - start;
89+
ok(duration.count() < 10.00, "Execution time should be less than 10 ms. Actual: %f ms", duration.count());
90+
91+
MYSQL_QUERY(proxysql_admin, "SET mysql-threshold_resultset_size=1073741824");
92+
MYSQL_QUERY(proxysql_admin, "LOAD MYSQL VARIABLES TO RUNTIME");
93+
94+
start = std::chrono::high_resolution_clock::now();
95+
rc = mysql_query(proxysql_backend, "SELECT 1");
96+
end = std::chrono::high_resolution_clock::now();
97+
98+
if (rc == 0) {
99+
MYSQL_RES* res = mysql_store_result(proxysql_backend);
100+
ok(res != nullptr, "Query executed successfully. %s", mysql_error(proxysql_backend));
101+
mysql_free_result(res);
102+
}
103+
else {
104+
ok(false, "Error executing query. %s", mysql_error(proxysql_admin));
105+
}
106+
duration = end - start;
107+
ok(duration.count() < 10.00, "Execution time should be less than 10 ms. Actual: %f ms", duration.count());
108+
109+
mysql_close(proxysql_backend);
110+
mysql_close(proxysql_admin);
111+
112+
return exit_status();
113+
}

0 commit comments

Comments
 (0)