Skip to content

[bug] system.statement_info COUNT queries stuck indefinitely, blocking CN draining #24872

Closed

Description

@xzxiong

版本信息

Bug Description

Queries on system.statement_info with COUNT(*) aggregation get stuck indefinitely on a CN pod, blocking connection migration (draining) and triggering CNDrainingTooLong alert.

Environment

  • Cluster: prod freetier-01
  • CN Pod: freetier-01-s2-56c954c888-cn-mwmhj
  • Node ID: 35313934-6664-6433-3939-613132616631
  • Alert: CNDrainingTooLong — CN stuck in draining state for 2+ hours

Symptoms

4 concurrent SELECT COUNT(*) queries on system.statement_info have been running for 2+ hours without returning:

-- Pattern 1: COUNT with request_at filter (3 sessions)
SELECT COUNT(*) FROM system.statement_info
WHERE 1=1
  AND `account` = 'ws_d604d949'
  AND `user` = 'u_a08698ecd6d142b891bb5bf57c41eeb9'
  AND `request_at` >= '2026-06-07 11:36:11'
  AND `sql_source_type` IN ('cloud_user_sql', 'external_sql')

-- Pattern 2: COUNT + SUM(CASE) aggregation (1 session)
SELECT COUNT(*) AS total,
  COALESCE(SUM(CASE WHEN `status` = 'Success' THEN 1 ELSE 0 END), 0) AS success,
  COALESCE(SUM(CASE WHEN `status` = 'Running' THEN 1 ELSE 0 END), 0) AS running,
  COALESCE(SUM(CASE WHEN `status` = 'Failed' THEN 1 ELSE 0 END), 0) AS failed
FROM system.statement_info
WHERE 1=1
  AND `account` = 'ws_d604d949'
  AND `user` = 'u_a08698ecd6d142b891bb5bf57c41eeb9'
  AND `sql_source_type` IN ('cloud_user_sql', 'external_sql')

These queries block CN draining because active sessions cannot be migrated away.

Processlist Evidence

mysql> select * from processlist() p where p.node_id = '35313934-6664-6433-3939-613132616631'\G

-- Row 1: started 13:36:13, still running at 15:38+
node_id: 35313934-6664-6433-3939-613132616631
conn_id: 3879487042
session_id: 019ea24c-5916-7ebb-b898-667a8cb86517
account: ws_d604d949
query_start: 2026-06-07 13:36:13.949785
sql_source_type: cloud_nonuser_sql

-- Row 2: started 13:37:02
conn_id: 3879493716
query_start: 2026-06-07 13:37:02.043596

-- Row 3 (overview query): started 13:37:02
conn_id: 3879493394
query_start: 2026-06-07 13:37:02.221351

-- Row 4: started 13:37:05
conn_id: 3879493787
query_start: 2026-06-07 13:37:05.662776

CN Draining Status

NAME                                 PHASE         MIGRAT
freetier-01-s2-56c954c888-cn-mwmhj   Terminating   y        Running   2026-06-04T09:30:04Z

Pod has been in Terminating with MIGRAT=y since 2026-06-04, stuck for 3+ days because these sessions cannot be migrated.

Impact

  1. CN pod cannot complete draining → resource leak (pod stuck in Terminating)
  2. CNDrainingTooLong alert fires continuously
  3. Potential cascading impact if more queries pile up on the same CN

Expected Behavior

  • Queries on system.statement_info with account/user/request_at filters should complete in reasonable time (seconds, not hours)
  • If a query runs too long, the CN draining mechanism should be able to forcefully cancel stuck queries after a configurable timeout

Analysis

The system.statement_info table in prod environments accumulates massive amounts of data. A COUNT(*) without time-bounded partition pruning can result in full table scans that take extremely long. The query includes request_at >= '2026-06-07 11:36:11' (only ~2h window) but if partition/index pruning is not effective, the scan is unbounded.

Additionally, the CN draining logic appears to wait indefinitely for all active sessions to complete — there is no forced cancellation timeout for long-running queries during migration.

Suggested Fix

  1. Query performance: Ensure system.statement_info queries with account + request_at filters can leverage partition pruning effectively
  2. Draining timeout: Add a configurable max-wait-time for CN draining; after the timeout, forcefully cancel remaining queries and proceed with migration
  3. Query timeout at engine level: Consider supporting a session-level or statement-level query timeout that the CN can enforce

Labels

  • Type: Bug
  • Component: CN, statement_info, connection migration

Activity

  1. changed the title [-]system.statement_info COUNT queries stuck indefinitely, blocking CN draining[/-] [+][bug] system.statement_info COUNT queries stuck indefinitely, blocking CN draining[/+] on Jun 7, 2026
  2. xzxiong commented on Jun 7, 2026

    @xzxiong
    ContributorAuthor
    遗留的Conn具体的 processlist info
    mysql> select * from processlist() p where p.node_id = '35313934-6664-6433-3939-613132616631'\G
    *************************** 1. row ***************************
            node_id: 35313934-6664-6433-3939-613132616631
            conn_id: 3879487042
         session_id: 019ea24c-5916-7ebb-b898-667a8cb86517
            account: ws_d604d949
               user: u_a08698ecd6d142b891bb5bf57c41eeb9
               host: 172.20.9.4:6001
                 db: moi
      session_start: 2026-06-07 13:36:13.078956
            command: COM_STMT_EXECUTE
               info: execute __mo_stmt_id_1 // SELECT COUNT(*) FROM system.statement_info WHERE 1=1 AND `account` = ? AND `user` = ? AND `request_at` >= ? AND `sql_source_type` IN (?,?) ; ws_d604d949 ; u_a08698ecd6d142b891bb5bf57c41eeb9 ; 2026-06-07 11:36:11 ; cloud_user_sql ; external_sql
             txn_id: 2bbbc671e8f6fa4d18b5d6ce94f30930
       statement_id: 019ea24c-5c7d-7b72-a7c9-e3f56032d896
     statement_type: Select
         query_type: DQL
    sql_source_type: cloud_nonuser_sql
        query_start: 2026-06-07 13:36:13.949785
        client_host: 121.40.50.238:25414
               role: accountadmin
         proxy_host: 172.20.74.19:55448
    *************************** 2. row ***************************
            node_id: 35313934-6664-6433-3939-613132616631
            conn_id: 3879493716
         session_id: 019ea24d-17ba-7298-a6ef-10443629eaed
            account: ws_d604d949
               user: u_a08698ecd6d142b891bb5bf57c41eeb9
               host: 172.20.9.4:6001
                 db: moi
      session_start: 2026-06-07 13:37:01.882164
            command: COM_STMT_EXECUTE
               info: execute __mo_stmt_id_1 // SELECT COUNT(*) FROM system.statement_info WHERE 1=1 AND `account` = ? AND `user` = ? AND `request_at` >= ? AND `sql_source_type` IN (?,?) ; ws_d604d949 ; u_a08698ecd6d142b891bb5bf57c41eeb9 ; 2026-06-07 11:36:59 ; cloud_user_sql ; external_sql
             txn_id: 2bbbc671e8f6fa4d18b5d6ce94f312bc
       statement_id: 019ea24d-185b-78be-84e1-3fcd24e46b7b
     statement_type: Select
         query_type: DQL
    sql_source_type: cloud_nonuser_sql
        query_start: 2026-06-07 13:37:02.043596
        client_host: 121.40.50.238:57974
               role: accountadmin
         proxy_host: 172.20.74.19:58502
    *************************** 3. row ***************************
            node_id: 35313934-6664-6433-3939-613132616631
            conn_id: 3879493394
         session_id: 019ea24d-17d9-7997-9240-348217a98034
            account: ws_d604d949
               user: u_a08698ecd6d142b891bb5bf57c41eeb9
               host: 172.20.9.4:6001
                 db: moi
      session_start: 2026-06-07 13:37:01.913609
            command: COM_STMT_EXECUTE
               info: execute __mo_stmt_id_1 // SELECT COUNT(*) AS total, COALESCE(SUM(CASE WHEN `status` = 'Success' THEN 1 ELSE 0 END), 0) AS success, COALESCE(SUM(CASE WHEN `status` = 'Running' THEN 1 ELSE 0 END), 0) AS running, COALESCE(SUM(CASE WHEN `status` = 'Failed' THEN 1 ELSE 0 END), 0) AS failed FROM system.statement_info WHERE 1=1 AND `account` = ? AND `user` = ? AND `sql_source_type` IN (?,?) ; ws_d604d949 ; u_a08698ecd6d142b891bb5bf57c41eeb9 ; cloud_user_sql ; external_sql
             txn_id: 2bbbc671e8f6fa4d18b5d6ce94f312bd
       statement_id: 019ea24d-190d-74d3-9162-f5fa1ade17f8
     statement_type: Select
         query_type: DQL
    sql_source_type: cloud_nonuser_sql
        query_start: 2026-06-07 13:37:02.221351
        client_host: 121.40.50.238:1369
               role: accountadmin
         proxy_host: 172.20.74.146:33004
    *************************** 4. row ***************************
            node_id: 35313934-6664-6433-3939-613132616631
            conn_id: 3879493787
         session_id: 019ea24d-259e-7f13-a8a3-3428c8895d59
            account: ws_d604d949
               user: u_a08698ecd6d142b891bb5bf57c41eeb9
               host: 172.20.9.4:6001
                 db: moi
      session_start: 2026-06-07 13:37:05.438980
            command: COM_STMT_EXECUTE
               info: execute __mo_stmt_id_1 // SELECT COUNT(*) FROM system.statement_info WHERE 1=1 AND `account` = ? AND `user` = ? AND `request_at` >= ? AND `sql_source_type` IN (?,?) ; ws_d604d949 ; u_a08698ecd6d142b891bb5bf57c41eeb9 ; 2026-06-07 11:36:11 ; cloud_user_sql ; external_sql
             txn_id: 2bbbc671e8f6fa4d18b5d6ce94f31306
       statement_id: 019ea24d-267e-7aaf-af51-636b320fdc8b
     statement_type: Select
         query_type: DQL
    sql_source_type: cloud_nonuser_sql
        query_start: 2026-06-07 13:37:05.662776
        client_host: 121.40.50.238:33966
               role: accountadmin
         proxy_host: 172.20.74.19:58508
    4 rows in set (0.07 sec)
    
  3. Repository owner unassigned on Jun 11, 2026
  4. added
    kind/bug
    severity/s0
    on Jun 15, 2026
  5. added theissue type on Jun 15, 2026
  6. added this to the MO-202606 milestone on Jun 15, 2026
  7. added
    severity/s1
    and removed
    severity/s0
    on Jun 29, 2026
  8. aptend commented on Jun 29, 2026

    @aptend
    Contributor

    optimized in 4.0, #23149

  9. added
    severity/s0
    and removed
    severity/s1
    on Jun 30, 2026
  10. modified the milestones: MO-202606, MO-202607 on Jun 30, 2026
  11. added
    ai-heavy
    ai-light
    and removed
    severity/s0
    ai-heavy
    on Jul 2, 2026
  12. assigned and unassigned on Jul 6, 2026
  13. ghost closed this as completedon Jul 30, 2026
  14. slyunnn commented on Aug 28, 2026

    @slyunnn

    Regression and extended verification for #24872 (2026-08-28)

    Verification Environment

    • Verified on latest origin/4.2-dev commit 635de0535a21807c02ee49f37331406921c7889b: [4.2] Backport fixes for issues #26720 and #25114 (#27313).
    • Related fix: PR #23149, merge commit e3fd7fe1e83b251d0af13c86f072e10e85cbece2, Optimize CLUSTER BY for statement_info and metric tables; it is reachable from 4.2-dev.
    • Isolated topology: 1 Log + 1 TN + 1 CN; Log/Hakeeper 127.0.0.1:62001, TN 127.0.0.1:59100, CN SQL endpoint 127.0.0.1:36001.
    • Client protocol: Go MySQL driver over MySQL TCP, using ordinary tenant issue24872:root and actual COM_STMT_PREPARE / COM_STMT_EXECUTE.
    • One SQL endpoint was used. Proxy and a second/third CN endpoint were not covered because this request was explicitly limited to dev-branch original regression without extension.

    What Was Covered

    • OK - The ordinary-tenant system.statement_info definition reports CLUSTER BY (request_at, account_id).
    • OK - The original prepared COUNT(*) SQL was executed concurrently in three sessions with the original account, user, time, and sql_source_type predicate shape; all returned 0 in 0-1 ms.
    • OK - The original prepared overview aggregation with COUNT(*) and SUM(CASE ...) was executed concurrently and returned total=0, success=0, running=0, and failed=0 in 1 ms.
    • OK - EXPLAIN showed automatic account_id = 10001 injection and block filters on both request_at and account_id.
    • Not covered - Proxy, multi-CN, migration/draining cancellation, and other expanded scenarios were intentionally not added per the request.
    • Not covered - The production-scale accumulated statement_info data and the reported multi-hour execution were unavailable locally. Therefore no production-scale timing or CN draining completion measurement was made.

    Findings

    The #23149 cluster-key and tenant predicate path is present in 4.2-dev. The original MySQL prepared-statement query shapes complete promptly on an ordinary tenant, and the plan exposes the expected (request_at, account_id) pruning predicates.

    MOTR / BVT Coverage

    No MOTR test, branch, commit, or PR was created: this request was limited to dev-branch regression verification without extension.

    Final Conclusion

    Partially fixed. The original prepared-query functional path and the expected cluster/block pruning predicates passed on 4.2-dev. A large-data regression is required to fully close the production performance and CN-draining aspect, because those outcomes depend on the number of historical rows and blocks; increasing unrelated CI data is not useful, but a controlled large multi-block statement_info dataset is necessary.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai-lightkind/bug

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions