Skip to content

fix(tool/mysql-list-active-queries): compute transaction durations in the server's system time zone - #4517

Open
beskvladimir-create wants to merge 1 commit into
googleapis:mainfrom
beskvladimir-create:fix/mysql-list-active-queries-timezone
Open

beskvladimir-create wants to merge 1 commit into
googleapis:mainfrom
beskvladimir-create:fix/mysql-list-active-queries-timezone

Conversation

@beskvladimir-create

Copy link
Copy Markdown
Contributor

Description

mysql-list-active-queries computed trx_duration_seconds and trx_wait_duration_seconds as UNIX_TIMESTAMP(UTC_TIMESTAMP()) - UNIX_TIMESTAMP(t.trx_started). information_schema.innodb_trx.trx_started / trx_wait_started are in the server's system time zone, while UTC_TIMESTAMP() is UTC wall-clock time, so both columns were off by the server's UTC offset whenever the system time zone is not UTC (e.g. -19794 instead of 6 on a server in IST, see #4516).

This change compares against UNIX_TIMESTAMP() (current Unix time) and converts the start time from the system time zone to the session time zone before UNIX_TIMESTAMP() interprets it:

(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_started, 'SYSTEM', @@session.time_zone))) AS trx_duration_seconds,
(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_wait_started, 'SYSTEM', @@session.time_zone))) AS trx_wait_duration_seconds,

CONVERT_TZ is a no-op when the session time zone is SYSTEM, and NULL stays NULL for transactions that are not waiting. Both statements (MySQL and Cloud SQL for MySQL) are changed so they stay identical.

Verification: with the mysql client against MySQL 8.0.46 started with TZ=Asia/Kolkata, I ran the updated listActiveQueriesStatementMySQL as a prepared statement while one transaction held a row lock and a second waited on it: it returned trx_duration_seconds = 6 for both and trx_wait_duration_seconds = 6 for the waiting one (the old statement returns -19794 for those values). I also checked the new expression on that server with session time_zone +00:00 and -08:00, and on a server with system time zone UTC (session SYSTEM and +05:00), where it returns the same values as the old one.

Test: no test change. CI runs MySQL with a UTC system time zone, where the old and new expressions return the same values, so an integration test there would not catch this. The existing RunMySQLListActiveQueriesTest still exercises the statement. I could not run the Go tests locally (my environment cannot download the Go toolchain required by go.mod or the test images), so I'm relying on CI for them.

Security: the change only touches the SELECT list of a fixed statement; the parameters and their binding are unchanged.

PR Checklist

  • Make sure you reviewed CONTRIBUTING.md
  • Make sure to open an issue as a bug/issue before writing your code
  • Ensure you have manually reviewed the entire diff before requesting a review
  • Ensure the tests and linter pass (left to CI, see Test above)
  • Code coverage does not decrease (no Go logic changed, only the SQL text)
  • Appropriate docs were updated (no docs change needed)
  • Make sure to add ! if this involve a breaking change (not breaking)

🛠️ Fixes #4516

… the server's system time zone

information_schema.innodb_trx.trx_started and trx_wait_started are in the
server's system time zone, but the durations were computed against
UTC_TIMESTAMP(), so they were off by the UTC offset on servers whose system
time zone is not UTC. Compare against UNIX_TIMESTAMP() and convert the start
times from SYSTEM to the session time zone before UNIX_TIMESTAMP() reads them.

Fixes googleapis#4516

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the active query listing statements for MySQL and CloudSQL to calculate transaction and wait durations by converting transaction start times from the system timezone to the session timezone. The reviewer points out that using @@session.time_zone with CONVERT_TZ can result in NULL values if timezone tables are unpopulated, and suggests a more robust alternative using TIMESTAMPDIFF and converting to UTC ('+00:00').

Comment on lines +39 to +40
(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_started, 'SYSTEM', @@session.time_zone))) AS trx_duration_seconds,
(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_wait_started, 'SYSTEM', @@session.time_zone))) AS trx_wait_duration_seconds,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using @@session.time_zone in CONVERT_TZ can return NULL if the session timezone is set to a named timezone (e.g., 'Asia/Kolkata') and the MySQL timezone tables are not populated (which is common in default or minimal MySQL installations).

A more robust approach that is completely immune to unpopulated timezone tables is to convert the system time to UTC ('+00:00') and compare it with UTC_TIMESTAMP() using TIMESTAMPDIFF. Both 'SYSTEM' and '+00:00' are always recognized by CONVERT_TZ without requiring timezone tables.

Suggested change
(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_started, 'SYSTEM', @@session.time_zone))) AS trx_duration_seconds,
(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_wait_started, 'SYSTEM', @@session.time_zone))) AS trx_wait_duration_seconds,
TIMESTAMPDIFF(SECOND, CONVERT_TZ(t.trx_started, 'SYSTEM', '+00:00'), UTC_TIMESTAMP()) AS trx_duration_seconds,
TIMESTAMPDIFF(SECOND, CONVERT_TZ(t.trx_wait_started, 'SYSTEM', '+00:00'), UTC_TIMESTAMP()) AS trx_wait_duration_seconds,

Comment on lines +69 to +70
(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_started, 'SYSTEM', @@session.time_zone))) AS trx_duration_seconds,
(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_wait_started, 'SYSTEM', @@session.time_zone))) AS trx_wait_duration_seconds,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using @@session.time_zone in CONVERT_TZ can return NULL if the session timezone is set to a named timezone (e.g., 'Asia/Kolkata') and the MySQL timezone tables are not populated (which is common in default or minimal MySQL installations).

A more robust approach that is completely immune to unpopulated timezone tables is to convert the system time to UTC ('+00:00') and compare it with UTC_TIMESTAMP() using TIMESTAMPDIFF. Both 'SYSTEM' and '+00:00' are always recognized by CONVERT_TZ without requiring timezone tables.

Suggested change
(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_started, 'SYSTEM', @@session.time_zone))) AS trx_duration_seconds,
(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(CONVERT_TZ(t.trx_wait_started, 'SYSTEM', @@session.time_zone))) AS trx_wait_duration_seconds,
TIMESTAMPDIFF(SECOND, CONVERT_TZ(t.trx_started, 'SYSTEM', '+00:00'), UTC_TIMESTAMP()) AS trx_duration_seconds,
TIMESTAMPDIFF(SECOND, CONVERT_TZ(t.trx_wait_started, 'SYSTEM', '+00:00'), UTC_TIMESTAMP()) AS trx_wait_duration_seconds,

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants