@@ -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);
0 commit comments