Skip to content

Bug: data branch merge fails with ErrTxnNeedRetryWithDefChanged in multi-CN PESSIMISTIC mode #24099

Closed

Description

@XuPeng-SH

Summary

data branch merge fails intermittently in multi-CN PESSIMISTIC mode with:

txn need retry in rc mode, def changed

Error code: ErrTxnNeedRetryWithDefChanged (20631)

Reproduction

Observed in CI: multi cn e2e bvt test docker compose(PESSIMISTIC)
Test file: test/distributed/cases/git4data/branch/merge/merge_1.sql line 85

data branch merge t2 into t1

Root Cause

data branch merge executes internal SQL (CREATE TABLE, INSERT, DELETE) via runSql() in data_branch_helpers.go:168:

opts := executor.Options{}.
    WithDisableIncrStatement().  // disables compile retry
    WithTxn(backSes.GetTxnHandler().GetTxn()).
    WithKeepTxnAlive()

WithDisableIncrStatement() sets disableRetry = true in the compile path (sql_executor.go:418), which prevents compile2.go from retrying on ErrTxnNeedRetryWithDefChanged.

When any internal INSERT/DELETE hits a lock conflict on mo_tables catalog rows (common in multi-CN PESSIMISTIC mode), lock_meta.go:198-203 converts it to ErrTxnNeedRetryWithDefChanged. Since retry is disabled, the error propagates directly to the client.

Additionally, handleBranchMerge in self_handle.go has no retry wrapper of its own.

Call chain

handleBranchMerge (self_handle.go)
  → diffMergeAgency (data_branch.go)
    → mergeDiffs / diffOnBase (parallel)
      → flushSqlValues → execSQLStatements → runSql (data_branch_helpers.go)
        → SQL executor with disableRetry=true
          → compile2 (retry disabled)
            → lock_meta.go:198 → ErrTxnNeedRetryWithDefChanged
              → error surfaces to client

Why it is flaky

In multi-CN PESSIMISTIC mode, distributed lock contention on catalog metadata rows is non-deterministic. The merge creates temp tables via CTAS and then applies INSERT/DELETE to the base table, creating multiple lock points where conflicts can occur. Single-CN tests rarely trigger this.

Suggested Fix Directions

  1. Add retry logic at handleBranchMerge level — wrap the merge operation in a retry loop for ErrTxnNeedRetryWithDefChanged
  2. Remove WithDisableIncrStatement() for these internal SQL executions (evaluate if safe)
  3. Add a dedicated retry wrapper in diffMergeAgency

Related Files

  • pkg/frontend/data_branch_helpers.go:168 — WithDisableIncrStatement() usage
  • pkg/frontend/data_branch.go — diffMergeAgency
  • pkg/frontend/self_handle.go — handleBranchMerge entry point
  • pkg/sql/compile/compile2.go:245-303 — retry loop that is bypassed
  • pkg/sql/compile/lock_meta.go:195-210 — error generation
  • pkg/sql/compile/sql_executor.go:418 — disableRetry mapping

Activity

  1. added
    kind/bug
    on Apr 9, 2026
  2. added
    severity/s0
    on Apr 9, 2026
  3. added this to the MO-202604 milestone on Apr 9, 2026
  4. ghost removed this from the MO-202604 milestone on May 29, 2026
  5. ghost added this to the MO-202605 milestone on May 29, 2026
  6. modified the milestones: MO-202605, MO-202607 on Jun 30, 2026
  7. added
    ai-heavy
    and removed
    severity/s0
    on Jul 1, 2026
  8. gouhongshen commented on Sep 4, 2026

    @gouhongshen
    Contributor

    Automated AI triage

    This issue requires human involvement and cannot currently be completed independently by the
    automated fix loop.

    Reason

    Requires the transaction owner to define the safe retry boundary and atomicity contract, including whether the enclosing merge transaction is replayable.

    Evidence

    The path disables compile retry, reproduction depends on nondeterministic multi-CN conflicts, and retries at handler, agency, or internal-executor level have materially different semantics.

    Please use this analysis to provide the missing decision, environment, evidence, or ownership
    needed before the issue is returned to the AI candidate pool.

  9. added
    severity/s0
    ai-easy
    and removed on Sep 5, 2026
  10. gouhongshen commented on Sep 5, 2026

    @gouhongshen
    Contributor

    Investigation on current main found a narrower remaining gap than the original report:

    • The earlier data-branch implementation already routes __mo_diff_del_ and __mo_diff_ins_ staging SQL through frontend statement execution, preserving compile retry.
    • In-place update apply later added __mo_diff_upd_, but the routing predicate still recognized only delete/insert staging names. Generated update-stage INSERTs and UPDATE JOINs therefore returned to the WithDisableIncrStatement path, where ErrTxnNeedRetryWithDefChanged can escape.
    • The fix centralizes the reserved __mo_diff_ prefix for all apply staging names and routes every generated statement referencing it through the existing statement retry owner. No whole-merge retry was added.

    Executable proof:

    • The two new update-staging routing assertions fail before the production change and pass afterward.
    • Full CGo-enabled pkg/frontend tests and make build pass on the rebased commit.
    • A local multi-process two-CN PESSIMISTIC/READ-COMMITTED cluster passed two consecutive BVT runs covering merge_1, merge_schema_evolve, and substring-matched merge_10: 244/244 assertions per run, with checked-in result semantics verified.

    Draft PR: #28271

    The issue remains open for manual validation.

  11. gouhongshen commented on Sep 5, 2026

    @gouhongshen
    Contributor

    PR #28271 is trying to fix this issue.

  12. gouhongshen commented on Sep 6, 2026

    @gouhongshen
    Contributor

    PR #28271 has merged and is awaiting manual validation. The issue remains open.

  13. added and removed
    severity/s0
    on Sep 16, 2026
  14. Ariznawlll commented on Sep 16, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • commit:9572bd3311abb16d1df939956408a727bcf83eba
    • 部署方式:TKE,3 CN;两个直连 CN 会话并发执行
    • 测试日期:2026-09-16

    原问题验证

    建立两组互不共享的 source/base Data Branch 表,每轮通过两个不同 CN 同时执行 DATA BRANCH MERGE branch INTO base。连续 10 轮、共 20 次 MERGE 均成功,每侧目标表均从 1 行变为 2 行,未出现 ErrTxnNeedRetryWithDefChanged、20631、死锁或超时。

    Happy Path 覆盖

    1. 两个 CN 的并发 MERGE 均完成,20/20 返回成功。
    2. 每次 MERGE 后目标表行数均为 2,确认不是只返回成功而未应用分支数据。

    Unhappy Path 覆盖

    1. 每轮使用独立数据库、在轮次结束后删除,排除命名冲突和上轮目录/表状态残留。
    2. 检查所有客户端输出,均不存在 20631、retry、deadlock 或 timeout。

    白盒验证

    PR #28271 将 Data Branch apply staging DDL/DML 路由回可重试的 frontend statement 路径,并包含 pkg/frontend、pkg/sql/compile 及 routing 单测;本次没有以此替代多 CN SQL 验收。

    回归测试

    PR #28271 保留 merge_1.sql 的原 MERGE 路径覆盖,并新增生成 staging 名称与用户 __mo_diff_ 标识符的回归;该问题是多 CN 并发专项,现有 BVT/UT 作为常规回归入口。

    测试证据

    PR #28271;环境 10 轮汇总均为 setup=0, A=0[2], B=0[2]。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

ai-easyai-heavykind/bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions