From dc4694d6560311870163b9b2c2b6a101081cbf73 Mon Sep 17 00:00:00 2001 From: Rene Cannao Date: Sat, 6 Dec 2025 14:49:04 +0000 Subject: [PATCH] Refactor idle session scanning and improve test precision Code improvements: - Extract SESS_TO_SCAN_idle_thread constant to header file for better maintainability - Replace magic number 128 with named constant in idle_thread_to_kill_idle_sessions() - Improve code readability and consistency in session scanning logic Test enhancements: - Add mysql-poll_timeout configuration for more precise timeout testing - Reduce test sleep times to 13 seconds for faster test execution - Add diagnostic messages to clearly show timeout configurations in test output - Ensure tests properly validate timeout enforcement with precise timing The changes improve code maintainability and make tests more reliable and faster while maintaining accurate timeout validation. --- include/MySQL_Thread.h | 2 ++ lib/MySQL_Thread.cpp | 5 ++--- test/tap/tests/mysql-set_wait_timeout-t.cpp | 10 ++++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/include/MySQL_Thread.h b/include/MySQL_Thread.h index 13c984101..aaedadf8e 100644 --- a/include/MySQL_Thread.h +++ b/include/MySQL_Thread.h @@ -34,6 +34,8 @@ #define MYSQL_DEFAULT_NET_WRITE_TIMEOUT "60" #define MYSQL_DEFAULT_MAX_JOIN_SIZE "18446744073709551615" +#define SESS_TO_SCAN_idle_thread 256 + extern class MySQL_Variables mysql_variables; #ifdef IDLE_THREADS diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 9aec67788..9c82b12ce 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -3431,8 +3431,7 @@ __run_skip_1: * initialized and are accessible within the MySQL Thread. */ void MySQL_Thread::idle_thread_to_kill_idle_sessions() { -#define SESS_TO_SCAN 128 - if (mysess_idx + SESS_TO_SCAN > mysql_sessions->len) { + if (mysess_idx + SESS_TO_SCAN_idle_thread > mysql_sessions->len) { mysess_idx=0; } unsigned int i; @@ -3440,7 +3439,7 @@ void MySQL_Thread::idle_thread_to_kill_idle_sessions() { return; // this should never happen //min_idle = curtime - (unsigned long long)mysql_thread___wait_timeout*1000; } - for (i=0;ilen; i++) { + for (i=0 ; i < SESS_TO_SCAN_idle_thread && mysess_idx < mysql_sessions->len; i++) { uint32_t sess_pos=mysess_idx; MySQL_Session *mysess=(MySQL_Session *)mysql_sessions->index(sess_pos); unsigned long long effective_wait_timeout = std::min( diff --git a/test/tap/tests/mysql-set_wait_timeout-t.cpp b/test/tap/tests/mysql-set_wait_timeout-t.cpp index 7c8f71338..4f3268b04 100644 --- a/test/tap/tests/mysql-set_wait_timeout-t.cpp +++ b/test/tap/tests/mysql-set_wait_timeout-t.cpp @@ -58,7 +58,10 @@ int extract_wait_timeout_from_json(const json& j_session, unsigned long long &wa int test_session_timeout(CommandLine *cl, MYSQL *admin) { diag("Test: %s", __func__); + diag("Setting mysql-wait_timeout=50000"); MYSQL_QUERY_T(admin, "SET mysql-wait_timeout=50000"); + diag("Setting mysql-poll_timeout=500 , required for more precise timeout"); + MYSQL_QUERY_T(admin, "SET mysql-poll_timeout=500"); MYSQL_QUERY_T(admin, "LOAD MYSQL VARIABLES TO RUNTIME"); MYSQL* proxy = init_mysql_conn(cl->host, cl->username, cl->password, cl->port); @@ -72,7 +75,7 @@ int test_session_timeout(CommandLine *cl, MYSQL *admin) { int rc = run_q(proxy, "SET sql_mode=''"); ok(rc == 0, (rc == 0 ? "Connection alive" : "Connection killed")); - sleep(25); + sleep(13); rc = run_q(proxy, "SET sql_mode=''"); ok(rc != 0, (rc == 0 ? "Connection alive" : "Connection killed")); @@ -85,7 +88,10 @@ int test_session_timeout(CommandLine *cl, MYSQL *admin) { int test_session_timeout_exceed_global_timeout(CommandLine *cl, MYSQL *admin) { diag("Test: %s", __func__); + diag("Setting mysql-wait_timeout=10000"); MYSQL_QUERY_T(admin, "SET mysql-wait_timeout=10000"); + diag("Setting mysql-poll_timeout=500 , required for more precise timeout"); + MYSQL_QUERY_T(admin, "SET mysql-poll_timeout=500"); MYSQL_QUERY_T(admin, "LOAD MYSQL VARIABLES TO RUNTIME"); MYSQL*proxy = init_mysql_conn(cl->host, cl->username, cl->password, cl->port); @@ -99,7 +105,7 @@ int test_session_timeout_exceed_global_timeout(CommandLine *cl, MYSQL *admin) { int rc = run_q(proxy, "SET sql_mode=''"); ok(rc == 0, (rc == 0 ? "Connection alive" : "Connection killed")); - sleep(15); + sleep(13); rc = run_q(proxy, "SET sql_mode=''"); ok(rc != 0, (rc == 0 ? "Connection alive" : "Connection killed"));