From bb3ee8adba9f94d5ee1f638eab3a93998a06b58e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jaramago=20Fern=C3=A1ndez?= Date: Fri, 18 Mar 2022 08:40:00 +0100 Subject: [PATCH] Fix 'text_admin_stats-t' timing issue --- test/tap/tests/test_admin_stats-t.cpp | 80 ++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 15 deletions(-) diff --git a/test/tap/tests/test_admin_stats-t.cpp b/test/tap/tests/test_admin_stats-t.cpp index 7cc617bea..5658cfad1 100644 --- a/test/tap/tests/test_admin_stats-t.cpp +++ b/test/tap/tests/test_admin_stats-t.cpp @@ -27,6 +27,43 @@ using std::string; using std::to_string; +using std::vector; + +int wait_for_history_update(MYSQL* proxysql_admin, uint32_t timeout) { + uint64_t previous_timestamp = 0; + uint32_t retries = 0; + + int res = EXIT_FAILURE; + + while (true) { + MYSQL_QUERY(proxysql_admin, "SELECT TIMESTAMP FROM history_mysql_status_variables ORDER BY timestamp DESC LIMIT 1"); + MYSQL_RES* my_res = mysql_store_result(proxysql_admin); + vector rows = extract_mysql_rows(my_res); + + if (rows.empty() || rows[0].empty()) { + res = 3; + break; + } else { + uint64_t new_timestamp = std::strtoll(rows[0][0].c_str(), NULL, 10); + + if (previous_timestamp == 0) { + previous_timestamp = new_timestamp; + } else if (retries > timeout) { + res = 2; + break; + } else if (new_timestamp > previous_timestamp) { + res = EXIT_SUCCESS; + break; + } else { + diag("Waiting 'history_mysql_status_variables' update..."); + retries += 1; + sleep(1); + } + } + } + + return res; +} int main(int argc, char** argv) { CommandLine cl; @@ -72,7 +109,7 @@ int main(int argc, char** argv) { MYSQL_RES* result = mysql_store_result(proxysql_admin); MYSQL_ROW row = mysql_fetch_row(result); - if (row[0]) + if (row && row[0]) lookup_row_count = strtoll(row[0], nullptr, 10); mysql_free_result(result); @@ -91,7 +128,7 @@ int main(int argc, char** argv) { result = mysql_store_result(proxysql_admin); row = mysql_fetch_row(result); - if (row[0]) + if (row && row[0]) distinct_timestamp_count = strtoll(row[0], nullptr, 10); mysql_free_result(result); @@ -103,23 +140,29 @@ int main(int argc, char** argv) { ); // Test 3: Matching distinct variable_id counts in lookup and history table - int64_t distinct_var_ids_in_history = 0; - int64_t distinct_var_ids_in_lookup = 0; + int64_t distinct_var_ids_in_history = -1; + int64_t distinct_var_ids_in_lookup = -1; MYSQL_QUERY(proxysql_admin, "SELECT COUNT(DISTINCT(variable_id)) from history_mysql_status_variables_lookup"); + if (result == NULL) { + diag("NULL result received. Errno: '%d', Error: '%s'", mysql_errno(proxysql_admin), mysql_error(proxysql_admin)); + } result = mysql_store_result(proxysql_admin); row = mysql_fetch_row(result); - if (row[0]) + if (row && row[0]) distinct_var_ids_in_lookup = strtoll(row[0], nullptr, 10); mysql_free_result(result); MYSQL_QUERY(proxysql_admin, "SELECT COUNT(DISTINCT(variable_id)) from history_mysql_status_variables"); result = mysql_store_result(proxysql_admin); + if (result == NULL) { + diag("NULL result received. Errno: '%d', Error: '%s'", mysql_errno(proxysql_admin), mysql_error(proxysql_admin)); + } row = mysql_fetch_row(result); - if (row[0]) + if (row && row[0]) distinct_var_ids_in_history = strtoll(row[0], nullptr, 10); mysql_free_result(result); @@ -160,29 +203,36 @@ int main(int argc, char** argv) { int64_t history_rows_before = 0; int64_t history_rows_after = 0; + // Increase interval and wait for next round of inserts. + // distinct_var_ids_in_history should equal the # of records inserted. + // If the interval isn't updated, then there'd be double what's expected. + new_stats_interval_sec = 10; + MYSQL_QUERY(proxysql_admin, ("SET admin-stats_mysql_connections=" + to_string(new_stats_interval_sec)).c_str()); + MYSQL_QUERY(proxysql_admin, "LOAD ADMIN VARIABLES TO RUNTIME"); + + // Wait for the next 'history update' before waiting the specified time of the new interval. + int w_res = wait_for_history_update(proxysql_admin, 20); + if (w_res == EXIT_FAILURE) { + fprintf(stderr, "Line %d, 'wait_for_history_update' failed: %d\n", __LINE__, w_res); + return EXIT_FAILURE; + } + MYSQL_QUERY(proxysql_admin, "SELECT COUNT(*) FROM history_mysql_status_variables"); result = mysql_store_result(proxysql_admin); row = mysql_fetch_row(result); - if (row[0]) + if (row && row[0]) history_rows_before = strtoll(row[0], nullptr, 10); mysql_free_result(result); - // Increase interval and wait for next round of inserts. - // distinct_var_ids_in_history should equal the # of records inserted. - // If the interval isn't updated, then there'd be double what's expected. - new_stats_interval_sec = 10; - MYSQL_QUERY(proxysql_admin, ("SET admin-stats_mysql_connections=" + to_string(new_stats_interval_sec)).c_str()); - MYSQL_QUERY(proxysql_admin, "LOAD ADMIN VARIABLES TO RUNTIME"); - sleep(new_stats_interval_sec + 1); // give it time to insert next round of stats MYSQL_QUERY(proxysql_admin, "SELECT COUNT(*) FROM history_mysql_status_variables"); result = mysql_store_result(proxysql_admin); row = mysql_fetch_row(result); - if (row[0]) + if (row && row[0]) history_rows_after = strtoll(row[0], nullptr, 10); mysql_free_result(result);