diff --git a/test/tap/tests/test_query_rules_routing-t.cpp b/test/tap/tests/test_query_rules_routing-t.cpp index a83bb7881..48b759097 100644 --- a/test/tap/tests/test_query_rules_routing-t.cpp +++ b/test/tap/tests/test_query_rules_routing-t.cpp @@ -314,6 +314,9 @@ int main(int argc, char** argv) { return -1; } + diag("Test query rules routing functionality"); + diag("This test validates that queries are routed to correct hostgroups based on query rules"); + plan(dst_hostgroup_tests.size()); MYSQL* proxysql_admin = mysql_init(NULL); @@ -338,6 +341,98 @@ int main(int argc, char** argv) { MYSQL_QUERY(proxysql_admin, "SET mysql-auto_increment_delay_multiplex=0"); MYSQL_QUERY(proxysql_admin, "LOAD MYSQL VARIABLES TO RUNTIME"); + // Debug: Show current server configuration + diag("=== Current mysql_servers configuration ==="); + MYSQL_QUERY(proxysql_admin, "SELECT hostgroup_id, hostname, port, status FROM mysql_servers ORDER BY hostgroup_id"); + MYSQL_RES* servers_res = mysql_store_result(proxysql_admin); + MYSQL_ROW server_row; + while ((server_row = mysql_fetch_row(servers_res))) { + diag(" hostgroup=%s, hostname=%s, port=%s, status=%s", + server_row[0], server_row[1], server_row[2], server_row[3]); + } + mysql_free_result(servers_res); + + // Debug: Show user's default hostgroup + diag("=== Current mysql_users configuration ==="); + MYSQL_QUERY(proxysql_admin, "SELECT username, default_hostgroup FROM mysql_users WHERE username='root' OR username='sbtest1' LIMIT 5"); + MYSQL_RES* users_res = mysql_store_result(proxysql_admin); + MYSQL_ROW user_row; + while ((user_row = mysql_fetch_row(users_res))) { + diag(" username=%s, default_hostgroup=%s", user_row[0], user_row[1]); + } + mysql_free_result(users_res); + + // Discover the actual writer and reader hostgroups from the infrastructure + // The test uses hostgroups 0 and 1, but the infrastructure may use different ones (e.g., 1300, 1301) + int writer_hg = 0; + int reader_hg = 1; + + // Try to discover writer hostgroup from user's default_hostgroup + MYSQL_QUERY(proxysql_admin, "SELECT default_hostgroup FROM mysql_users WHERE username='root' LIMIT 1"); + MYSQL_RES* hg_res = mysql_store_result(proxysql_admin); + MYSQL_ROW hg_row = mysql_fetch_row(hg_res); + if (hg_row && hg_row[0]) { + writer_hg = atoi(hg_row[0]); + diag("Discovered writer hostgroup from user 'root': %d", writer_hg); + } + mysql_free_result(hg_res); + + // Try to discover reader hostgroup - it's typically the next hostgroup after writer + // or we can query mysql_servers to find hostgroups with servers in ONLINE status + MYSQL_QUERY(proxysql_admin, "SELECT DISTINCT hostgroup_id FROM mysql_servers WHERE status='ONLINE' AND hostgroup_id != 0 ORDER BY hostgroup_id LIMIT 2"); + hg_res = mysql_store_result(proxysql_admin); + std::vector online_hostgroups; + while ((hg_row = mysql_fetch_row(hg_res))) { + online_hostgroups.push_back(atoi(hg_row[0])); + } + mysql_free_result(hg_res); + + if (online_hostgroups.size() >= 2) { + // We have at least 2 hostgroups - use the first two for writer and reader + writer_hg = online_hostgroups[0]; + reader_hg = online_hostgroups[1]; + diag("Discovered hostgroups from mysql_servers: writer=%d, reader=%d", writer_hg, reader_hg); + } else if (online_hostgroups.size() == 1) { + writer_hg = online_hostgroups[0]; + reader_hg = writer_hg; // Use same hostgroup if only one exists + diag("Only one hostgroup found: writer=%d, reader=%d (same)", writer_hg, reader_hg); + } + + // Now update the test data with the discovered hostgroups + diag("Updating test query rules to use discovered hostgroups: writer=%d, reader=%d", writer_hg, reader_hg); + + // Update dst_hostgroup_tests with dynamic hostgroups + for (auto& test : dst_hostgroup_tests) { + for (auto& rule : test.first) { + // Replace hardcoded hostgroups with discovered ones + // Pattern: destination_hostgroup,0 -> destination_hostgroup,WRITER_HG + // Pattern: destination_hostgroup,1 -> destination_hostgroup,READER_HG + size_t pos; + pos = rule.find(",0,1)"); + if (pos != std::string::npos) { + rule.replace(pos, 5, "," + std::to_string(writer_hg) + ",1)"); + } + pos = rule.find(",1,1)"); + if (pos != std::string::npos) { + rule.replace(pos, 5, "," + std::to_string(reader_hg) + ",1)"); + } + pos = rule.find(",0,1)"); // Check again for any remaining + if (pos != std::string::npos) { + rule.replace(pos, 5, "," + std::to_string(writer_hg) + ",1)"); + } + } + for (auto& query_hid : test.second) { + // Update expected hostgroups in queries + if (query_hid.second == 0) { + query_hid.second = writer_hg; + } else if (query_hid.second == 1) { + query_hid.second = reader_hg; + } + } + } + + diag("Updated test configuration with dynamic hostgroups"); + // Create the testing table int c_table_res = create_testing_tables(proxysql_text, 2); if (c_table_res) { return EXIT_FAILURE; } @@ -384,11 +479,14 @@ int main(int argc, char** argv) { gen_random_str(&rnd_str[0], 20); string_format(query_hid.first, query, rnd_str.c_str()); + diag("Testing query: '%s' (expected hostgroup: %d)", query.c_str(), query_hid.second); + // First execute the query for text protocol // ******************************************************************** // Get the current hosgtroup queries int cur_hid_queries = get_hostgroup_query_count(proxysql_admin, query_hid.second); + diag(" Text protocol: current query count for HG %d = %d", query_hid.second, cur_hid_queries); // Perform the query in a text protocol connection int text_prot_res = perform_text_procotol_query(proxysql_text, query); @@ -403,10 +501,13 @@ int main(int argc, char** argv) { // Get the new hosgtroup queries int new_hid_queries = get_hostgroup_query_count(proxysql_admin, query_hid.second); + diag(" Text protocol: new query count for HG %d = %d (diff=%d, expected=1)", + query_hid.second, new_hid_queries, new_hid_queries - cur_hid_queries); if (new_hid_queries - cur_hid_queries != 1) { queries_properly_routed = false; text_queries_failed_to_route.push_back(query); + diag(" Text protocol: QUERY FAILED TO ROUTE to expected hostgroup %d", query_hid.second); } // Secondly execute the query for binary protocol @@ -414,6 +515,7 @@ int main(int argc, char** argv) { // Get the current hosgtroup queries cur_hid_queries = get_hostgroup_query_count(proxysql_admin, query_hid.second); + diag(" Binary protocol: current query count for HG %d = %d", query_hid.second, cur_hid_queries); // Perform the query in a stmt protocol connection int stmt_res = perform_stmt_query(proxysql_stmt, query); @@ -429,10 +531,13 @@ int main(int argc, char** argv) { // Get the new hosgtroup queries new_hid_queries = get_hostgroup_query_count(proxysql_admin, query_hid.second); + diag(" Binary protocol: new query count for HG %d = %d (diff=%d, expected=2)", + query_hid.second, new_hid_queries, new_hid_queries - cur_hid_queries); if (new_hid_queries - cur_hid_queries != 2) { queries_properly_routed = false; stmt_queries_failed_to_route.push_back(query); + diag(" Binary protocol: QUERY FAILED TO ROUTE to expected hostgroup %d", query_hid.second); } }