Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions test/tap/tests/test_read_only_actions_offline_hard_servers-t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,19 @@ int test_scenario_1(MYSQL* proxy_admin, const CommandLine& cl) {
MYSQL_QUERY__(proxy_admin, "DELETE FROM mysql_replication_hostgroups");
MYSQL_QUERY__(proxy_admin, "LOAD MYSQL SERVERS TO RUNTIME");

// Set default_hostgroup=0 to match writer_hostgroup used in this test
// Set default_hostgroup to match writer_hostgroup (WHG) used in this
// test. WHG defaults to 0 for the legacy infra but is overridden to
// 2900 by the mysql84-g4 group via TAP_MYSQL8_BACKEND_HG — see
// init_hostgroups() above. If we hardcode 0 here, the mysql84 group
// routes the test's BEGIN query to an empty hostgroup 0 and times
// out at 10 s. A previous fix (26c5a2572) hardcoded 0 because it
// predated the mysql84 hostgroup migration in 4f9ab49e7 — this
// commit finishes that fix by making the default_hostgroup follow
// WHG at runtime.
{
std::string update_user;
string_format("UPDATE mysql_users SET default_hostgroup=0 WHERE username='%s'", update_user, cl.root_username);
string_format("UPDATE mysql_users SET default_hostgroup=%d WHERE username='%s'",
update_user, WHG, cl.root_username);
MYSQL_QUERY__(proxy_admin, update_user.c_str());
MYSQL_QUERY__(proxy_admin, "LOAD MYSQL USERS TO RUNTIME");
}
Comment on lines +226 to 241
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block of code, including the detailed comment, is duplicated in test_scenario_2 (lines 396-411). To improve maintainability and reduce code duplication, consider extracting this logic into a helper function. This would also centralize the logic for updating the default hostgroup, making future changes easier.

For example, you could create a function like this:

int update_default_hostgroup(MYSQL* proxy_admin, const CommandLine& cl) {
    // Set default_hostgroup to match writer_hostgroup (WHG) used in this
    // test. WHG defaults to 0 for the legacy infra but is overridden to
    // 2900 by the mysql84-g4 group via TAP_MYSQL8_BACKEND_HG — see
    // init_hostgroups() above. If we hardcode 0 here, the mysql84 group
    // routes the test's BEGIN query to an empty hostgroup 0 and times
    // out at 10 s. A previous fix (26c5a2572) hardcoded 0 because it
    // predated the mysql84 hostgroup migration in 4f9ab49e7 — this
    // commit finishes that fix by making the default_hostgroup follow
    // WHG at runtime.
    std::string update_user;
    string_format("UPDATE mysql_users SET default_hostgroup=%d WHERE username='%s'",
                  update_user, WHG, cl.root_username);
    if (mysql_query(proxy_admin, update_user.c_str())) {
        fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxy_admin));
        return EXIT_FAILURE;
    }
    if (mysql_query(proxy_admin, "LOAD MYSQL USERS TO RUNTIME")) {
        fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxy_admin));
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

Then, you can replace the duplicated blocks in test_scenario_1 and test_scenario_2 with:

if (update_default_hostgroup(proxy_admin, cl) != EXIT_SUCCESS) {
    goto cleanup;
}

This avoids using the MYSQL_QUERY__ macro which relies on a goto cleanup statement, making the helper function more self-contained.

Expand Down Expand Up @@ -384,10 +393,19 @@ int test_scenario_2(MYSQL* proxy_admin, const CommandLine& cl) {
MYSQL_QUERY__(proxy_admin, "DELETE FROM mysql_replication_hostgroups");
MYSQL_QUERY__(proxy_admin, "LOAD MYSQL SERVERS TO RUNTIME");

// Set default_hostgroup=0 to match writer_hostgroup used in this test
// Set default_hostgroup to match writer_hostgroup (WHG) used in this
// test. WHG defaults to 0 for the legacy infra but is overridden to
// 2900 by the mysql84-g4 group via TAP_MYSQL8_BACKEND_HG — see
// init_hostgroups() above. If we hardcode 0 here, the mysql84 group
// routes the test's BEGIN query to an empty hostgroup 0 and times
// out at 10 s. A previous fix (26c5a2572) hardcoded 0 because it
// predated the mysql84 hostgroup migration in 4f9ab49e7 — this
// commit finishes that fix by making the default_hostgroup follow
// WHG at runtime.
{
std::string update_user;
string_format("UPDATE mysql_users SET default_hostgroup=0 WHERE username='%s'", update_user, cl.root_username);
string_format("UPDATE mysql_users SET default_hostgroup=%d WHERE username='%s'",
update_user, WHG, cl.root_username);
MYSQL_QUERY__(proxy_admin, update_user.c_str());
MYSQL_QUERY__(proxy_admin, "LOAD MYSQL USERS TO RUNTIME");
}
Expand Down
Loading