Skip to content

[Bug]: Proxy CN draining migrates active JDBC cursor and breaks COM_STMT_FETCH #29182

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

57c280e

Other Environment Information

  • Hardware parameters: Apple Silicon local environment
  • OS type: macOS
  • Others: MatrixOne 1 LOG / 1 TN / 2 CN / Proxy; MySQL Connector/J 8.3.0; useServerPrepStmts=true, useCursorFetch=true

Actual Behavior

When a JDBC client is reading a server-side cursor through Proxy, marking the cursor's CN as Draining causes Proxy to transfer that connection while the cursor still has unread rows. The target CN receives the next COM_STMT_FETCH without an active cursor and returns:

java.sql.SQLException: invalid state prepared statement has no active cursor
  at com.mysql.cj.jdbc.result.ResultSetImpl.next(...)
Caused by: com.mysql.cj.exceptions.CJException: invalid state prepared statement has no active cursor
  at com.mysql.cj.protocol.a.result.ResultsetRowsCursor.fetchMoreRows(...)

The result stream terminates before all rows are delivered. Proxy logs one completed connection transfer for the affected tunnel.

Expected Behavior

Proxy must not complete a connection transfer while the client has an active server cursor whose remaining cursor state cannot be reconstructed on the target CN. It should either defer the transfer until the cursor is closed/exhausted or migrate the cursor state. The client must receive the complete ordered result without duplicates or truncation.

Steps to Reproduce

  1. Start MatrixOne with two CNs and Proxy; keep both CNs in Working state.
  2. Create a table with 5,000 rows and connect through Proxy with:
useServerPrepStmts=true&useCursorFetch=true&defaultFetchSize=13
  1. Ensure the connection is routed to CN2, then execute and begin reading:
PreparedStatement ps = connection.prepareStatement(
    "select id,payload from jdbc_streaming_cursor order by id",
    ResultSet.TYPE_FORWARD_ONLY,
    ResultSet.CONCUR_READ_ONLY);
ps.setFetchSize(13);
ResultSet rs = ps.executeQuery();
rs.next();
  1. While rows remain unread, make CN1 Working and mark CN2 Draining:
select mo_ctl('cn','workstate','<cn1-uuid>:1');
select mo_ctl('cn','workstate','<cn2-uuid>:2');
  1. Continue calling rs.next() slowly enough for the Proxy scaling interval to run. The fetch fails with prepared statement has no active cursor after Proxy transfers the tunnel.

Additional information

  • With a reading window longer than the five-second Proxy scaling interval, the behavior reproduced 3/3 while alternating the source and target CN.
  • Each reproduction recorded one Proxy transfer end before the cursor error.
  • A shorter timing window reproduced only when transfer happened before result exhaustion (1/3), confirming that the failure is tied to transfer during the active cursor rather than to the query itself.
  • Without CN draining/migration, the same cursor configuration returned all 5,000 ordered rows and 5.12 MB of binary payload on CN1, CN2, and Proxy, 3/3 each.
  • Ordinary server-side prepared statements with no active cursor survived three CN-draining migration rounds; prepared objects and session variables remained usable. The boundary is specifically the active cursor state.
  • Related Java/JDBC release-gate tracking: [TestCase]: Java + MyBatis+MO framework scenario testing #26623.

Activity

  1. added this to the MO-202609 milestone on Sep 23, 2026
  2. added theissue type on Sep 23, 2026
  3. added a commit that references this issue on Sep 25, 2026
    6d3d588
  4. daviszhen commented on Sep 28, 2026

    @daviszhen
    Contributor

    Local validation

    Validated the issue scenario against the latest main:

    • MatrixOne commit: 9b7c2c23dba737232d975d66bd04628199e1a436
    • Included fix: 6d3d588 from PR #29191
    • Topology: fresh Docker LOG/TN/2CN/Proxy cluster
    • Client: Connector/J 8.0.27 with useServerPrepStmts=true, useCursorFetch=true, fetch size 13
    • Dataset: 5,000 ordered rows

    Results:

    • The JDBC probe returned all 5,000 rows in order, with no loss, duplication, or truncation.
    • The same JDBC connection successfully executed a follow-up COUNT(*) query and returned 5,000.
    • The raw MySQL protocol probe passed both legacy-EOF and deprecated-EOF modes.
    • While the server cursor was still active, the Proxy log reported OkExpectedNotSafeToStartTransfer and deferred migration.
    • After the cursor was fully consumed, the Proxy successfully migrated the connection from the draining CN to the other CN. The log showed connection migrate from server, connection migrate to server, and transfer to a new CN server.
    • Proxy statistics reported two successful migrations after the raw-protocol and JDBC probes.
    • The focused pkg/proxy and pkg/frontend tests also passed.

    Conclusion: the reported COM_STMT_FETCH failure was not reproduced on the latest main; active prepared cursors are now kept on their original CN until the cursor response terminates, and migration is performed afterward.

  5. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • Commit:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 部署方式:干净源码 worktree;定向 Go UT/集成回归(涉及 Proxy/CN 的路径使用对应组件级并发与协议模拟)
    • 测试日期:2026-10-02

    原问题验证

    在上述 commit 运行 issue 对应的定向回归。活动 JDBC cursor 存在时迁移被延后,最终 FETCH terminator 前不会替换后端;已有 2-CN Proxy/JDBC 5000 行 cursor 验证完整。

    Happy Path 覆盖

    覆盖 cursor 正常 fetch 完成后迁移、普通非 cursor 请求。

    Unhappy Path 覆盖

    覆盖旧 CN 无 cursor attestation、final packet 边界和迁移竞态;连接不丢行。

    白盒验证

    关联修复 #29191 已进入当前 main。直接执行并检查的定向测试为:pkg/frontend/routine_test.go::TestMigrateConnectionFromRejectsActivePreparedCursors、pkg/proxy/conn_migration_test.go::TestMigrateConnFromRejectsOldCNWithoutCursorAttestation、pkg/proxy/tunnel_test.go::TestFinalFetchKeepsTransferBlockedUntilTerminator。

    回归测试

    本问题依赖内部故障注入、并发时序、catalog/optimizer 状态或多组件协议边界,无法由普通 SQL golden 稳定构造,因此不放入 MOTR;回归保留在 MatrixOne UT/BVT 层。对应定向测试在精确 commit 上通过,且未修改用户工作区。

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 定向测试:pkg/frontend/routine_test.go::TestMigrateConnectionFromRejectsActivePreparedCursors、pkg/proxy/conn_migration_test.go::TestMigrateConnFromRejectsOldCNWithoutCursorAttestation、pkg/proxy/tunnel_test.go::TestFinalFetchKeepsTransferBlockedUntilTerminator
    • 当前结果:活动 JDBC cursor 存在时迁移被延后,最终 FETCH terminator 前不会替换后端;已有 2-CN Proxy/JDBC 5000 行 cursor 验证完整。
    • 关联 PR:#29191

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions