Skip to content

[Bug]: Proxy CN draining resets LAST_INSERT_ID on the migrated JDBC session #29184

Closed

Description

@Ariznawlll

1. Is there an existing issue for this?

  • I searched open and closed MatrixOne issues for Proxy migration / connection transfer / LAST_INSERT_ID and found no duplicate.

2. Bug description and error info with logs

When Proxy migrates an idle JDBC session away from a draining CN, the session's LAST_INSERT_ID() state is reset to 0. Other session status from the same source session—ROW_COUNT(), FOUND_ROWS(), and a user variable—is restored correctly.

This changes the observable state of one logical client connection after a transparent Proxy handoff. Applications that execute an auto-increment insert and read LAST_INSERT_ID() in a later statement can receive 0 if a CN drain occurs between those statements.

Environment

  • MatrixOne main: 57c280ed48fdccec3c2eb25042820deaf1eca3c1
  • Topology: 1 Log / 1 TN / 2 CN / Proxy
  • Endpoints: CN1 127.0.0.1:16001, CN2 127.0.0.1:16002, Proxy 127.0.0.1:6001
  • Client: Connector/J 8.3.0
  • Both useServerPrepStmts=true and false covered

Reproducer

Through one Proxy JDBC connection:

create table jdbc_proxy_status(
  id bigint unsigned auto_increment primary key,
  v int,
  note varchar(32)
) auto_increment=424242;

insert into jdbc_proxy_status(v,note)
values(10,'seed-a'),(20,'seed-b'),(30,'seed-c'),(40,'seed-d');

Then on the same JDBC connection:

  1. Execute another auto-increment insert and capture its generated key.
  2. Execute a limited SQL_CALC_FOUND_ROWS query.
  3. Execute an update that affects exactly two rows.
  4. Set a user variable.
  5. Mark the session's source CN Draining and the other CN Working.
  6. Wait until the Proxy log contains connection migrate from server, connection migrate to server, and transfer end for this connection.
  7. On the original JDBC connection, execute:
select last_insert_id(), row_count(), found_rows(), @proxy_status_tag;

Actual behavior

Three confirmed handoffs, alternating both CN directions:

server prepared, CN1 -> CN2:
expected LAST_INSERT_ID=434242
actual=0,2,5,migration

server prepared, CN2 -> CN1:
expected LAST_INSERT_ID=424246
actual=0,2,5,migration

client prepared, CN1 -> CN2:
expected LAST_INSERT_ID=424246
actual=0,2,5,migration

Each round had exactly one successful transfer end in the Proxy log. In all three rounds only LAST_INSERT_ID() changed; ROW_COUNT()=2, FOUND_ROWS()=5, and @proxy_status_tag='migration' were preserved.

Non-migration control

The same sequence without a CN drain preserves all four values:

expected LAST_INSERT_ID=434242
actual=434242,2,5,control

3. Expected and actual behavior

  • Expected: a successful transparent Proxy migration preserves the source session's LAST_INSERT_ID() value.
  • Actual: the target session returns 0 after migration.

4. Code analysis

The migration payload restores adjacent session state but does not contain lastInsertID:

  • pkg/frontend/routine.go exports LastAffectedRows and FoundRows.
  • pkg/proxy/conn_migration.go forwards those fields.
  • pkg/frontend/session.go restores ROW_COUNT() and FOUND_ROWS() after replay.
  • proto/query.proto has no corresponding field for the session's lastInsertID.

Session already owns this state through SetLastInsertID / GetLastInsertID; it is simply absent from the migration snapshot and restore path.

5. Suggested regression coverage

Add a two-CN + Proxy JDBC migration case that sets non-zero values for LAST_INSERT_ID, ROW_COUNT, and FOUND_ROWS, drains the owning CN, verifies one successful handoff, and checks all three values plus user variables on the original client connection. Cover both server- and client-prepared Connector/J modes.

This is session-state correctness and does not require big-data coverage.

Activity

  1. added a commit that references this issue on Sep 22, 2026
    1634996
  2. Lundomn commented on Sep 22, 2026

    @Lundomn
    Contributor

    转 QA 验收:PR #29192 已合并(merge commit 1634996)。

    请在 2 CN + Proxy 环境验证 drain migration 后 JDBC 会话的 LAST_INSERT_ID(),覆盖 client/server prepared 两种模式及迁移后 INSERT。请回报测试环境、版本/commit、结果;失败时附最小复现。

  3. added this to the MO-202609 milestone on Sep 23, 2026
  4. added theissue type on Sep 23, 2026
  5. Ariznawlll commented on Sep 24, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • Commit:d7d940e93bd037e9aacdc0f1256b91e32df84642
    • 拓扑:1 Log + 1 TN + 2 CN + Proxy
    • 客户端:Connector/J 8.4.0,分别验证 client prepared 和 server prepared

    原问题验证

    按 issue 中的真实 CN draining 流程执行:同一 JDBC 连接先完成自增 INSERT 并读取 LAST_INSERT_ID(),随后将当前 CN 切到 draining,等待 Proxy 把原连接迁移到另一 CN,再次读取 LAST_INSERT_ID() 并执行新的 INSERT。

    两种 prepared 模式各连续执行 3 轮,共发生 6 次真实 CN 迁移。迁移前后的连接身份由 @@server_id 确认,6/6 均保持原 LAST_INSERT_ID(),迁移后的 INSERT 也都返回并落盘正确的新自增 ID。Proxy 日志同时确认 session transfer;server-prepared 路径确认迁移了 3 个 prepared statements。

    正常场景覆盖

    • client prepared:迁移前 INSERT、迁移后读取旧 ID、迁移后再次 INSERT,3/3 通过。
    • server prepared:同样流程 3/3 通过。
    • 每轮均确认连接从 source CN 切换到另一 CN,而不是重连得到的新 session。

    异常与边界覆盖

    • 每轮结束恢复 source CN work state,下一轮重新选择实际承载连接的 CN。
    • 同时校验 session 状态保持和迁移后新写入,避免只验证查询可用但 session state 已丢失。
    • 补充 MOTR 双 CN Proxy 场景,默认跳过,只有显式启用多 CN Proxy 环境时运行,避免在单 CN 线路产生伪通过。

    白盒验证

    • Proxy 日志显示原连接发生 transfer。
    • server-prepared 日志显示 prepared statement 数量为 3,确认覆盖服务端 prepared 迁移路径。

    回归测试

    测试证据

    • Connector/J:client prepared 3/3、server prepared 3/3。
    • MOTR 场景:3 轮 × 3 项断言,全部通过。
    • 关键结果:迁移到另一 CN、旧 LAST_INSERT_ID() 保持、迁移后新 INSERT ID 正确。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions