Skip to content

Commit 11cb475

Browse files
committed
docs: Add comprehensive Doxygen documentation for session variable tracking
Add detailed architectural documentation for PR 5166 session variable tracking: - High-level architecture overview in session_track_variables struct explaining the 3-phase workflow - Detailed documentation for handler_rc0_Process_Variables explaining the core processing workflow - Technical implementation details for MySQL_Connection::get_variables protocol interface - Configuration logic documentation for handler_again___verify_backend_session_track_variables - Added inline comments explaining why session tracking is needed and performance considerations This documentation provides a complete understanding of how MySQL session variable tracking integrates with ProxySQL's existing state machine and leverages MySQL's native session tracking capabilities.
1 parent c5aed61 commit 11cb475

File tree

3 files changed

+151
-5
lines changed

3 files changed

+151
-5
lines changed

include/MySQL_Session.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,35 @@ class MySQL_Session: public Base_Session<MySQL_Session, MySQL_Data_Stream, MySQL
225225
bool handler_again___verify_ldap_user_variable();
226226
bool handler_again___verify_backend_autocommit();
227227
bool handler_again___verify_backend_session_track_gtids();
228+
/**
229+
* @brief Verify and configure session variable tracking on backend connections.
230+
*
231+
* === PR 5166: Backend Configuration Entry Point ===
232+
*
233+
* This function is the main orchestrator for setting up session tracking on backend
234+
* connections. It's called during connection initialization to ensure tracking is
235+
* properly configured before query processing begins.
236+
*
237+
* CONFIGURATION LOGIC:
238+
* 1. Check if global mysql-session_track_variables is enabled
239+
* 2. For each tracking flag that's not yet set on the connection:
240+
* - Mark the flag as sent (prevents duplicate configuration)
241+
* - Transition session to appropriate configuration state
242+
* - Return true to indicate state machine needs re-processing
243+
*
244+
* STATE MACHINE INTEGRATION:
245+
* - SETTING_SESSION_TRACK_VARIABLES: Sets session_track_system_variables="*"
246+
* - SETTING_SESSION_TRACK_STATE: Sets session_track_state_change=ON
247+
* - Returns true to continue state machine processing until both are configured
248+
*
249+
* WHY THIS APPROACH:
250+
* - Ensures tracking is configured exactly once per backend connection
251+
* - Integrates cleanly with existing ProxySQL session state machine
252+
* - Handles both tracking capabilities independently for flexibility
253+
* - Prevents redundant SET commands on already configured connections
254+
*
255+
* @return true if session state needs to be re-processed (configuration pending), false otherwise
256+
*/
228257
bool handler_again___verify_backend_session_track_variables();
229258
bool handler_again___verify_backend_multi_statement();
230259
bool handler_again___verify_backend_user_schema();
@@ -233,7 +262,27 @@ class MySQL_Session: public Base_Session<MySQL_Session, MySQL_Data_Stream, MySQL
233262
bool handler_again___status_SETTING_LDAP_USER_VARIABLE(int *);
234263
bool handler_again___status_SETTING_SQL_MODE(int *);
235264
bool handler_again___status_SETTING_SESSION_TRACK_GTIDS(int *);
265+
/**
266+
* @brief Handle the SETTING_SESSION_TRACK_VARIABLES state.
267+
*
268+
* This method executes the SET command to configure session_track_system_variables="*"
269+
* on the backend connection, enabling the server to track changes to all system
270+
* variables and report them back to ProxySQL.
271+
*
272+
* @param _rc Pointer to return code that will be set with the operation result
273+
* @return true if session state needs to be re-processed, false otherwise
274+
*/
236275
bool handler_again___status_SETTING_SESSION_TRACK_VARIABLES(int *);
276+
/**
277+
* @brief Handle the SETTING_SESSION_TRACK_STATE state.
278+
*
279+
* This method executes the SET command to configure session_track_state_change=ON
280+
* on the backend connection, enabling the server to report when session state
281+
* changes occur (including system variable changes).
282+
*
283+
* @param _rc Pointer to return code that will be set with the operation result
284+
* @return true if session state needs to be re-processed, false otherwise
285+
*/
237286
bool handler_again___status_SETTING_SESSION_TRACK_STATE(int *);
238287
bool handler_again___status_CHANGING_CHARSET(int *_rc);
239288
bool handler_again___status_CHANGING_SCHEMA(int *);
@@ -283,6 +332,34 @@ class MySQL_Session: public Base_Session<MySQL_Session, MySQL_Data_Stream, MySQL
283332
int RunQuery(MySQL_Data_Stream *myds, MySQL_Connection *myconn);
284333
void handler___status_WAITING_CLIENT_DATA();
285334
void handler_rc0_Process_GTID(MySQL_Connection *myconn);
335+
/**
336+
* @brief Process session variable changes from backend connection response.
337+
*
338+
* === PR 5166: Variable Processing Workflow ===
339+
*
340+
* This function is the core of the variable tracking system and is called after
341+
* every successful query execution when SERVER_SESSION_STATE_CHANGED flag is set.
342+
*
343+
* DETAILED WORKFLOW:
344+
* 1. Extract variable changes from MySQL's session tracking data via get_variables()
345+
* 2. Iterate through all tracked variables in mysql_tracked_variables array
346+
* 3. For each variable that changed in the backend:
347+
* - Update both client and server variable maps for state consistency
348+
* - Handle character set variables specially (convert names to internal IDs)
349+
* 4. This ensures ProxySQL's internal state matches the actual backend state
350+
*
351+
* WHY THIS IS NEEDED:
352+
* - SQL statement parsing cannot detect all variable changes (e.g., stored procedures)
353+
* - Some variables are changed implicitly by MySQL server operations
354+
* - Without this tracking, client and backend states can diverge
355+
*
356+
* PERFORMANCE CONSIDERATIONS:
357+
* - Only called when SERVER_SESSION_STATE_CHANGED flag is set
358+
* - Processes all tracked variables but only updates those that actually changed
359+
* - Character set conversions are done only for relevant variables
360+
*
361+
* @param myconn Pointer to the MySQL connection from which to extract variable changes
362+
*/
286363
void handler_rc0_Process_Variables(MySQL_Connection *myconn);
287364
void handler_rc0_RefreshActiveTransactions(MySQL_Connection* myconn);
288365
void handler___status_WAITING_CLIENT_DATA___STATE_SLEEP___MYSQL_COM_INIT_DB_replace_CLICKHOUSE(PtrSize_t& pkt);

include/MySQL_Thread.h

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,41 @@ struct th_metrics_map_idx {
328328
};
329329
};
330330

331+
/**
332+
* @brief Configuration structure for session variable tracking functionality.
333+
*
334+
* === PR 5166: Session Variable Tracking Architecture ===
335+
*
336+
* This PR introduces comprehensive tracking of session-specific system variables
337+
* by leveraging MySQL's native session tracking capabilities. The overall workflow
338+
* consists of three main phases:
339+
*
340+
* 1. CONFIGURATION PHASE:
341+
* - Global enable/disable via mysql-session_track_variables variable
342+
* - Per-connection tracking state managed via flags in MySQL_Connection
343+
* - New session states handle configuration of tracking on backends
344+
*
345+
* 2. BACKEND SETUP PHASE:
346+
* - When enabled, configure session_track_system_variables="*"
347+
* - Configure session_track_state_change=ON
348+
* - MySQL server then automatically tracks all system variable changes
349+
*
350+
* 3. PROCESSING PHASE:
351+
* - After each query, check SERVER_SESSION_STATE_CHANGED flag
352+
* - Extract variable changes via MySQL's session tracking protocol
353+
* - Update both client and server variable maps for state consistency
354+
* - Special handling for character set variables (name → ID conversion)
355+
*
356+
* BENEFITS:
357+
* - Captures changes that SQL parsing alone cannot detect (stored procedures, etc.)
358+
* - Redundant tracking ensures accurate client/backend state synchronization
359+
* - Leverages MySQL server's native capabilities for reliability
360+
* - Performance optimized: only processes when session state actually changes
361+
*/
331362
struct session_track_variables {
332363
enum mode {
333-
DISABLED = 0,
334-
ENABLED
364+
DISABLED = 0, ///< Session variable tracking is disabled (default)
365+
ENABLED ///< Session variable tracking is enabled
335366
};
336367
};
337368

@@ -589,6 +620,15 @@ class MySQL_Threads_Handler
589620
int processlist_max_query_length;
590621

591622
bool ignore_min_gtid_annotations;
623+
/**
624+
* @brief Configuration flag to enable/disable session variable tracking.
625+
*
626+
* When set to session_track_variables::ENABLED (1), ProxySQL will configure
627+
* backend connections to track system variable changes using MySQL's
628+
* session_track_system_variables and session_track_state_change capabilities.
629+
*
630+
* Default: session_track_variables::DISABLED (0)
631+
*/
592632
int session_track_variables;
593633
} variables;
594634
struct {

include/mysql_connection.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ class MySQL_Connection {
8888
char * session_track_gtids;
8989
char *ldap_user_variable;
9090
char *ldap_user_variable_value;
91-
bool session_track_gtids_sent;
92-
bool session_track_variables_sent;
93-
bool session_track_state_sent;
91+
bool session_track_gtids_sent; ///< Flag indicating if GTID session tracking has been configured on this connection
92+
bool session_track_variables_sent; ///< Flag indicating if session_track_system_variables has been configured on this connection
93+
bool session_track_state_sent; ///< Flag indicating if session_track_state_change has been configured on this connection
9494
bool ldap_user_variable_sent;
9595
uint8_t protocol_version;
9696
int8_t last_set_autocommit;
@@ -264,6 +264,35 @@ class MySQL_Connection {
264264
void reset();
265265

266266
bool get_gtid(char *buff, uint64_t *trx_id);
267+
/**
268+
* @brief Extract session variable changes from MySQL's session tracking system.
269+
*
270+
* === PR 5166: Backend Variable Extraction ===
271+
*
272+
* This method is the interface to MySQL's native session tracking protocol and
273+
* extracts system variable changes that occurred during the last query execution.
274+
*
275+
* TECHNICAL DETAILS:
276+
* - Uses mysql_session_track_get_first/next() to iterate through SESSION_TRACK_SYSTEM_VARIABLES
277+
* - Only processes when SERVER_SESSION_STATE_CHANGED flag is set and no errors occurred
278+
* - Handles the variable/value pairing protocol where variables and values are returned alternately
279+
* - Populates a map with variable names as keys and their new values as values
280+
*
281+
* PROTOCOL FLOW:
282+
* 1. Check if session state changed and no errors occurred
283+
* 2. Get first tracked system variable (name)
284+
* 3. Iterate to get corresponding value, next variable name, etc.
285+
* 4. Build variable_name → value mappings in the provided map
286+
*
287+
* INTEGRATION POINT:
288+
* This is called by handler_rc0_Process_Variables() which then processes the
289+
* extracted changes to update ProxySQL's internal state. The separation allows
290+
* for testing and potential future extensions of the tracking protocol.
291+
*
292+
* @param variables Reference to unordered_map that will be populated with
293+
* variable names as keys and their new values as values
294+
* @return true if session variable changes were found and extracted, false otherwise
295+
*/
267296
bool get_variables(std::unordered_map<std::string, std::string>&);
268297
void reduce_auto_increment_delay_token() { if (auto_increment_delay_token) auto_increment_delay_token--; };
269298

0 commit comments

Comments
 (0)