|
| 1 | +/** |
| 2 | + * @file mysql-session_track_variables_ff_enforced-t.cpp |
| 3 | + * @brief This test verifies that ProxySQL properly handles session variable tracking |
| 4 | + * in ENFORCED mode with fast_forward enabled. Session tracking should work |
| 5 | + * on MySQL 5.7+ and backends without support should be rejected (queries fail). |
| 6 | + */ |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include "mysql.h" |
| 11 | +#include "tap.h" |
| 12 | +#include "command_line.h" |
| 13 | +#include "utils.h" |
| 14 | + |
| 15 | +bool get_server_version(MYSQL* proxy, int& major, int& minor) { |
| 16 | + MYSQL_QUERY_T(proxy, "SELECT @@version"); |
| 17 | + MYSQL_RES* result = mysql_store_result(proxy); |
| 18 | + if (!result) { |
| 19 | + return false; |
| 20 | + } |
| 21 | + |
| 22 | + MYSQL_ROW row = mysql_fetch_row(result); |
| 23 | + if (!row) { |
| 24 | + mysql_free_result(result); |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + // Parse version string like "5.7.33" or "8.0.25" |
| 29 | + if (sscanf(row[0], "%d.%d", &major, &minor) != 2) { |
| 30 | + mysql_free_result(result); |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + mysql_free_result(result); |
| 35 | + return true; |
| 36 | +} |
| 37 | + |
| 38 | +bool extract_session_variable(MYSQL* proxy, const char* var_name, int& tracked_value) { |
| 39 | + tracked_value = -1; |
| 40 | + |
| 41 | + if ((proxy != nullptr) |
| 42 | + && (proxy->net.last_errno == 0) |
| 43 | + && (proxy->server_status & SERVER_SESSION_STATE_CHANGED)) { |
| 44 | + const char *data; |
| 45 | + size_t length; |
| 46 | + |
| 47 | + if (mysql_session_track_get_first(proxy, SESSION_TRACK_SYSTEM_VARIABLES, &data, &length) == 0) { |
| 48 | + std::string current_var_name(data, length); |
| 49 | + // get_first() returns a variable_name |
| 50 | + // get_next() will return the value |
| 51 | + bool expect_value = true; |
| 52 | + |
| 53 | + while (mysql_session_track_get_next(proxy, SESSION_TRACK_SYSTEM_VARIABLES, &data, &length) == 0) { |
| 54 | + if (expect_value) { |
| 55 | + // This is the value for current_var_name |
| 56 | + if (current_var_name == var_name) { |
| 57 | + std::string value_str(data, length); |
| 58 | + tracked_value = atoi(value_str.c_str()); |
| 59 | + return true; |
| 60 | + } |
| 61 | + // got a value in this iteration |
| 62 | + // in the next iteration, we have to expect a variable_name |
| 63 | + expect_value = false; |
| 64 | + } else { |
| 65 | + current_var_name = std::string(data, length); |
| 66 | + // got a variable_name in this iteration |
| 67 | + // in the next iteration, we have to expect the value of this variable |
| 68 | + expect_value = true; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return false; |
| 75 | +} |
| 76 | + |
| 77 | +bool test_session_variables(MYSQL* proxy, int& set_value, int& tracked_value) { |
| 78 | + set_value = -1; |
| 79 | + tracked_value = -1; |
| 80 | + |
| 81 | + MYSQL_QUERY_T(proxy, "CREATE DATABASE IF NOT EXISTS test"); |
| 82 | + MYSQL_QUERY_T(proxy, "SELECT 1"); |
| 83 | + mysql_free_result(mysql_store_result(proxy)); |
| 84 | + |
| 85 | + MYSQL_QUERY_T(proxy, "DROP PROCEDURE IF EXISTS test.set_innodb_lock_wait_timeout"); |
| 86 | + const char* create_proc = |
| 87 | + "CREATE PROCEDURE test.set_innodb_lock_wait_timeout() " |
| 88 | + "BEGIN " |
| 89 | + " SET innodb_lock_wait_timeout = CAST(FLOOR(50 + (RAND() * 100)) AS UNSIGNED); " |
| 90 | + "END"; |
| 91 | + |
| 92 | + MYSQL_QUERY_T(proxy, create_proc); |
| 93 | + |
| 94 | + MYSQL_QUERY_T(proxy, "CALL test.set_innodb_lock_wait_timeout()"); |
| 95 | + |
| 96 | + extract_session_variable(proxy, "innodb_lock_wait_timeout", tracked_value); |
| 97 | + |
| 98 | + MYSQL_QUERY_T(proxy, "SELECT @@innodb_lock_wait_timeout"); |
| 99 | + MYSQL_RES* result = mysql_store_result(proxy); |
| 100 | + if (result) { |
| 101 | + MYSQL_ROW row = mysql_fetch_row(result); |
| 102 | + if (row) { |
| 103 | + set_value = atoi(row[0]); |
| 104 | + } |
| 105 | + mysql_free_result(result); |
| 106 | + } |
| 107 | + |
| 108 | + return true; |
| 109 | +} |
| 110 | + |
| 111 | +int main(int argc, char** argv) { |
| 112 | + CommandLine cl; |
| 113 | + if (cl.getEnv()) { |
| 114 | + diag("Failed to get the required environmental variables."); |
| 115 | + return exit_status(); |
| 116 | + } |
| 117 | + |
| 118 | + plan(1); |
| 119 | + |
| 120 | + MYSQL* admin = init_mysql_conn(cl.admin_host, cl.admin_port, cl.admin_username, cl.admin_password); |
| 121 | + if (!admin) { |
| 122 | + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(admin)); |
| 123 | + return exit_status(); |
| 124 | + } |
| 125 | + |
| 126 | + // Set session_track_variables to ENFORCED mode (value 2) |
| 127 | + MYSQL_QUERY_T(admin, "SET mysql-session_track_variables=2"); |
| 128 | + MYSQL_QUERY_T(admin, "LOAD MYSQL VARIABLES TO RUNTIME"); |
| 129 | + |
| 130 | + // Enable fast_forward for test user |
| 131 | + MYSQL_QUERY_T(admin, "DELETE FROM mysql_users WHERE username='ff_test_user'"); |
| 132 | + MYSQL_QUERY_T(admin, "INSERT INTO mysql_users (username,password,fast_forward,default_hostgroup) VALUES ('ff_test_user','ff_test_pass',1,0)"); |
| 133 | + MYSQL_QUERY_T(admin, "LOAD MYSQL USERS TO RUNTIME"); |
| 134 | + |
| 135 | + MYSQL* proxy = mysql_init(NULL); |
| 136 | + // Enable CLIENT_DEPRECATE_EOF. This is required for session tracking |
| 137 | + proxy->options.client_flag |= CLIENT_DEPRECATE_EOF; |
| 138 | + if (!mysql_real_connect(proxy, cl.host, "ff_test_user", "ff_test_pass", NULL, cl.port, NULL, 0)) { |
| 139 | + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxy)); |
| 140 | + return exit_status(); |
| 141 | + } |
| 142 | + |
| 143 | + // Enable session tracking on client for fast_forward mode |
| 144 | + MYSQL_QUERY_T(proxy, "SET session_track_system_variables='*'"); |
| 145 | + MYSQL_QUERY_T(proxy, "SET session_track_state_change=ON"); |
| 146 | + |
| 147 | + int major = 0, minor = 0; |
| 148 | + if (!get_server_version(proxy, major, minor)) { |
| 149 | + diag("Failed to get server version"); |
| 150 | + return exit_status(); |
| 151 | + } |
| 152 | + |
| 153 | + diag("Detected MySQL version: %d.%d", major, minor); |
| 154 | + |
| 155 | + int set_value = -1; |
| 156 | + int tracked_value = -1; |
| 157 | + bool test_result = test_session_variables(proxy, set_value, tracked_value); |
| 158 | + |
| 159 | + // Verify results based on server version |
| 160 | + bool mysql57_plus = (major > 5) || (major == 5 && minor >= 7); |
| 161 | + |
| 162 | + if (mysql57_plus) { |
| 163 | + if (!test_result) { |
| 164 | + diag("Failed to run test"); |
| 165 | + return exit_status(); |
| 166 | + } |
| 167 | + |
| 168 | + ok(set_value == tracked_value, |
| 169 | + "MySQL %d.%d supports session tracking. Fast forward with ENFORCED mode: Expected: %d, Tracked: %d", |
| 170 | + major, minor, set_value, tracked_value); |
| 171 | + } else { |
| 172 | + // MySQL 5.6 or below: enforced mode should reject connections with error 9001 |
| 173 | + if (test_result) { |
| 174 | + diag("Test unexpectedly passed on MySQL %d.%d without session tracking support", major, minor); |
| 175 | + return exit_status(); |
| 176 | + } |
| 177 | + |
| 178 | + int error_code = mysql_errno(proxy); |
| 179 | + const char* error_msg = mysql_error(proxy); |
| 180 | + |
| 181 | + ok(error_code == 9001, |
| 182 | + "MySQL %d.%d lacks session tracking. Fast forward with ENFORCED mode. Connection should timeout with 9001. Error: %d, Message: %s", |
| 183 | + major, minor, error_code, error_msg); |
| 184 | + } |
| 185 | + |
| 186 | + // Cleanup |
| 187 | + MYSQL_QUERY_T(admin, "DELETE FROM mysql_users WHERE username='ff_test_user'"); |
| 188 | + MYSQL_QUERY_T(admin, "LOAD MYSQL USERS TO RUNTIME"); |
| 189 | + MYSQL_QUERY_T(admin, "SET mysql-session_track_variables=0"); |
| 190 | + MYSQL_QUERY_T(admin, "LOAD MYSQL VARIABLES TO RUNTIME"); |
| 191 | + |
| 192 | + mysql_close(proxy); |
| 193 | + mysql_close(admin); |
| 194 | + return exit_status(); |
| 195 | +} |
0 commit comments