Skip to content

[Bug]: Proxy CN migration loses temporary tables and retries prepared sessions #27602

Closed

Description

@Ariznawlll

Description

A connection routed through MatrixOne Proxy loses its session temporary tables after transparent CN migration. If a server-side prepared statement references the temporary table, migration cannot complete and Proxy retries the migration every 5 seconds while the session remains on the draining CN.

Environment

  • Branch: main
  • Commit: 705d5fbb625d32090b9a8acadcead08c3e1d4535
  • Test date: 2026-08-25
  • Deployment: one Log service, one TN, two CNs and one Proxy started as separate processes with etc/launch-with-proxy/*.toml
  • Client: MySQL Connector/J 8.4.0 with useServerPrepStmts=true
  • Proxy port: 6001; CN SQL ports: 16001 and 16002

Steps to reproduce

  1. Open a long-lived JDBC connection through Proxy and create a temporary table:
create database if not exists proxy_migration_probe;
use proxy_migration_probe;
create temporary table tmp_group (
    id int primary key,
    grp int,
    val varchar(30)
);
insert into tmp_group values
    (1, 1, 'beta'),
    (2, 1, 'alpha'),
    (3, 2, 'gamma');

select group_concat(val order by id separator '|')
from tmp_group where grp = 1;
-- beta|alpha

select @@server_id;
  1. From a separate administrative connection, mark the current backend CN as draining:
select mo_ctl('cn', 'workstate', '<current-cn-uuid>:2');
  1. Poll select @@server_id on the original JDBC connection until the UUID changes.

  2. Execute the same query on the original connection:

select group_concat(val order by id separator '|')
from tmp_group where grp = 1;
  1. Variant: before step 2, create both a SQL PREPARE and a Connector/J server prepared statement that reference tmp_group.

Actual behavior

Temporary table without prepared statements

Proxy completes the migration (prepare stmt num: 0), but the same JDBC connection loses the temporary table:

TEMP_TABLE_MIGRATION=dd1dccb5->dd1dccb4
SQL parser error: table "tmp_group" does not exist

Prepared statements reference the temporary table

The source exports the prepared statements, but the target CN cannot rebuild them because the temporary table does not exist. Proxy keeps retrying every 5 seconds:

connection migrate from server ... DB=proxy_migration_probe prepare stmt num=2
connection migrate to server ...
failed to migrate connection to cn, will retry ...
error="SQL parser error: table \"tmp_group\" does not exist"

The original connection remains usable on the draining CN, but cannot be drained while those prepared statements are present.

Expected behavior

Transparent Proxy migration must preserve session-visible temporary tables and prepared statements that reference them. If temporary tables are intentionally not migratable, Proxy must detect that state before migration and use an explicit, documented policy that does not silently lose tables or endlessly retry draining.

Stability and controls

  • Minimal temporary-table reproducer: 3/3 failed after successful CN migration, alternating both CN directions.
  • Prepared-statement variant: reproduced; migration retried every 5 seconds with the same target-side error.
  • Passing control: 3/3 passed for ordinary server prepared statements across CN migration.
  • Passing control: a pre-migration server prepared statement successfully inserted and read back a deterministic 256 KiB streamed BLOB after migration in every repetition.
  • Transaction controls passed: an active transaction stayed on the source CN; commit and rollback both allowed migration afterward; uncommitted and rolled-back values did not leak.
  • No CN crash, restart, panic or client disconnection was observed.

Evidence

Successful migration without prepared statements followed by table loss:

connection migrate from server ... ConnID=822 ... prepare stmt num=0
connection migrate to server ... ConnID=822 ...
TEMP_TABLE_MIGRATION=dd1dccb5->dd1dccb4
SQL parser error: table "tmp_group" does not exist

Prepared-statement migration failure:

failed to migrate conn to: SQL parser error: table "tmp_group" does not exist
failed to migrate connection to cn, will retry

Code analysis

  • pkg/frontend/session.go keeps temporary-table aliases in CN-local session state.
  • pkg/proxy/conn_migration.go transfers the current database, prepared statements, variables, affected/found rows and related state, but no temporary-table mapping or temporary-table objects are included in MigrateConnToRequest.
  • The observed target-side failure occurs while replaying exported prepared statements before the temporary table exists.

Regression coverage

After the product fix, add a multi-process/JDBC regression covering:

  1. temporary-table data survives Proxy CN migration;
  2. SQL and binary-protocol prepared statements referencing a temporary table remain executable;
  3. commit/rollback migration gating remains unchanged;
  4. draining does not enter an unbounded retry loop.

This belongs in motr because it requires Connector/J, Proxy, two CNs and deterministic work-state switching.

Related

Discovered during extended Connector/J, PREPARE, GROUP_CONCAT and cross-CN Proxy migration exploration. No matching open/closed MatrixOne issue or fix PR was found.

Activity

  1. added this to the MO-202608 milestone on Aug 25, 2026
  2. added
    kind/bug
    on Aug 25, 2026
  3. added theissue type on Aug 25, 2026
  4. added
    severity/s0
    and removed on Aug 25, 2026
  5. added
    ai-easy
    on Sep 2, 2026
  6. Ariznawlll commented on Sep 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • Commit:d37cc0f7b14c3dbd5bc810fc85950cf4da1e9a9b
    • 环境:TKE mo-search-commit-d37cc0f7b-20260903,3 CN、2 Proxy、1 TN、3 Log
    • 日期:2026-09-03

    原问题验证

    通过 Proxy 建立长连接,在会话中创建临时表和 server-side prepared statement;随后将当前后端 CN 置为 Draining,等待 Proxy 完成 CN migration,再在同一会话执行原 prepared statement。

    Happy Path 覆盖

    • 临时表数据迁移前查询结果为 beta|alpha。
    • 3 轮均观察到 Proxy 会话从源 CN 切换到另一 CN。
    • 迁移后执行原 prepared statement,3/3 轮均返回 beta|alpha。
    • 迁移过程中客户端连接未断开,CN 状态在每轮结束后恢复为 Working。

    Unhappy Path 覆盖

    • 迁移触发使用当前会话实际 @@server_id,避免误切换其他 CN。
    • 每轮切换后检查临时表和 prepared statement 是否仍可用,并检查连接可继续执行 SQL。
    • 未观察到目标 CN 缺少临时表、prepared statement 重试循环或客户端断连。

    白盒验证

    本轮未做白盒注入,仅验证 Proxy/CN migration 的客户端可见行为。

    回归测试

    已完成 3 轮 Proxy + 多 CN prepared 临时表迁移验证。后续 MOTR 应保留 Connector/J binary prepared、双向迁移和事务 gating 场景。

    测试证据

    run1: before=beta|alpha after=beta|alpha migrated=True pass=True;run2、run3 同样通过。测试结束后已恢复 CN 状态并清理测试对象。

    测试结论

    测试通过。

  7. Ariznawlll commented on Sep 5, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试验收结论

    • MatrixOne:main
    • Commit:d37cc0f7b14c3dbd5bc810fc85950cf4da1e9a9b
    • 环境:TKE mo-search-commit-d37cc0f7b-20260903
    • 拓扑:3 CN、2 Proxy、1 TN、3 Log

    测试步骤

    1. 通过 Proxy 建立长连接,在同一会话创建临时表和 server-side prepared statement。
    2. 记录迁移前查询结果 beta|alpha。
    3. 将当前后端 CN 置为 Draining,等待 Proxy 完成 CN migration。
    4. 在同一会话执行原 prepared statement,并检查临时表、连接和 CN 状态。
    5. 连续执行 3 轮,并在每轮结束后确认 CN 恢复为 Working。

    结果

    • 3/3 轮均完成 Proxy 会话迁移。
    • 迁移后原 prepared statement 均返回 beta|alpha。
    • 临时表数据保持一致,客户端连接未断开。
    • 未观察到 prepared statement 重试循环或 CN migration 后临时表丢失。
    • 每轮结束后 CN 均恢复为 Working。

    结论:原问题在当前 main/TKE 多 CN + Proxy 环境下验证通过,现关闭 issue。

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

Metadata

Metadata

Assignees

Labels

ai-easykind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions