mysqlclient icon indicating copy to clipboard operation
mysqlclient copied to clipboard

Feature Request: Support session state tracking

Open nickwilliams-eventbrite opened this issue 5 years ago • 1 comments

Note: I may be able to work on putting together a pull request for this feature request, but I wanted to make sure you were okay with this feature request and the approach I'm taking before doing all that work, hence I am filing this feature request for discussion and approval.

MySQL has a feature enabling server tracking of client session state changes. When this is enabled on the server and client, the server can actually notify the connected client about changes to its session state. This enables the client to, for example (our use case), determine which Global Transaction ID (GTID) or GTIDs were created when the client commits a transaction.

You can read more about this here:

  • https://dev.mysql.com/doc/refman/5.7/en/session-state-tracking.html
  • https://dev.mysql.com/doc/refman/5.7/en/mysql-session-track-get-first.html
  • https://dev.mysql.com/doc/refman/5.7/en/mysql-session-track-get-next.html

Here's what is needed to support this:

  1. A new constant in MySQLdb.constants.CLIENT: SESSION_TRACK = 1 << 23
  2. A new method on _mysql.connection named get_session_track_gtids which returns a list of GTIDs using C functions mysql_session_track_get_first/mysql_session_track_get_next and the enum value SESSION_TRACK_GTIDS.
  3. A new method on _mysql.connection named get_session_track_variables which returns a list of namedtuples containing the session variable names and values for changed variables using the same C functions and the enum value SESSION_TRACK_SYSTEM_VARIABLES.
  4. A new method on _mysql.connection named get_session_track_tx_characteristics which returns a list of strings of the characteristics that changed using the same C functions and the enum value SESSION_TRACK_TRANSACTION_CHARACTERISTICS.
  5. A new method on _mysql.connection named get_session_track_tx_state which returns a list of namedtuples containing information parsed from the data as described in the documentation for SESSION_TRACK_TRANSACTION_STATE using the same C functions and the enum value SESSION_TRACK_TRANSACTION_STATE.

The bare minimum that we need is items 1 and 2, but implementing also 3 through 5 would be more complete.

nickwilliams-eventbrite avatar Jul 26 '19 18:07 nickwilliams-eventbrite

I have created #373, which is a rough first-pass at implementing items 1 and 2 above (but no more). It may need some refinement, it may have memory leaks. I'm looking for initial feedback about the above proposal and the approach I'm taking with the PR, and then I can take it further.

nickwilliams-eventbrite avatar Jul 27 '19 02:07 nickwilliams-eventbrite