Skip to content

fix(frontend): keep data branch update staging retryable - #28271

Merged
mergify[bot] merged 2 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-24099-21c27da8
Sep 6, 2026
Merged

mergify[bot] merged 2 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-24099-21c27da8

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Related to #24099

What this PR does / why we need it:

Root cause

Data branch merge applies row diffs through generated staging-table DDL and DML. The generic internal SQL executor disables statement increments, so it also bypasses the existing compile retry for ErrTxnNeedRetryWithDefChanged. Under multi-CN pessimistic catalog contention, that error could therefore escape from an apply statement.

The prior routing recognized delete and insert staging names by re-parsing SQL text. Extending that predicate to every __mo_diff_ identifier fixed update staging but also classified legitimate user database, table, and column names. Streaming CSV diff and PICK callers discard collected BackExec batches and consume their stream channels, so such misclassification could silently lose rows and materialize an otherwise streamed result.

Changes

  • Keep one shared prefix for generated delete, insert, and update apply-table names.
  • Remove execution-mode inference from SQL text.
  • Make the apply-table owner explicitly execute staging DDL/DML through the existing retry-capable frontend statement path, including update-stage INSERT and base-table UPDATE JOIN statements.
  • Keep generic user SQL on the internal streaming path, including identifiers beginning with __mo_diff_.
  • Add public CSV and PICK regressions for ordinary user identifiers sharing that prefix.

Retry remains owned by the existing statement/compile boundary; this PR does not replay the merge transaction or add a retry loop.

Issue-to-test proof

  • Before the correction, focused tests showed every user __mo_diff_* database/table/column case entering BackExec and failing before the streaming executor was reached. The final unit cases prove those statements stay on internal execution while all generated delete/insert/update staging forms use BackExec.
  • TestDataBranchDiffAsFile/csv_user_diff_prefix_identifier proves two CSV rows are streamed, reloaded, and equal to a user table named __mo_diff_orders.
  • pick_5.sql proves a KEYS subquery using database __mo_diff_orders, table __mo_diff_keys, and column __mo_diff_flag preserves and applies rows 2 and 3. The checked-in result was inspected and matches the intended final rows 1, 2, and 3.
  • Existing merge_1.sql covers the reported DATA BRANCH MERGE t2 INTO t1 reproduction. merge_schema_evolve.sql covers insert/update/delete, composite-key, schema-evolution, rejection, and atomicity boundaries.

Tests run

  • make build — passed.
  • Full CGo-enabled pkg/frontend — passed (30.769s on latest rebased head).
  • Full CGo-enabled pkg/sql/compile — passed (7.786s on latest rebased head).
  • Focused final-head routing and output tests — passed.
  • Embedded-cluster CSV streaming regression — passed (13.449s on latest rebased head).
  • Local BVT pick_5.sql, twice on the same instance — 94/94 each run; both error reports were empty.
  • Local BVT merge_1.sql, substring-matched merge_10.sql, and merge_schema_evolve.sql — 244/244; the error report was empty.

Rebase follow-up

  • Rebased onto origin/main at 109bf926fd without conflicts. git range-diff shows both PR commits are patch-identical after the rebase.
  • The new base adds shared-computation reuse for analytic plans. It does not overlap this PR's six files, but it changes the compile execution dependency used by the retry path, so frontend and compile evidence was treated as stale.
  • On rebased head 6893b29606, the focused routing/output tests, full frontend and compile packages, and embedded CSV streaming regression all passed. The pushed head also restarts repository CI, including multi-CN PESSIMISTIC BVT.

Residual risks

The catalog conflict is nondeterministic. Deterministic unit coverage proves retry-capable routing for every generated apply family, while end-to-end BVT proves merge and streaming semantics. The pushed head will also run the repository's multi-CN PESSIMISTIC CI. No storage format, transaction-level retry, concurrency state, or resource ownership changes are introduced; #24099 remains open for manual validation.

@mergify mergify Bot added the kind/bug Something isn't working label Sep 5, 2026
@matrix-meow matrix-meow added the size/S Denotes a PR that changes [10,99] lines label Sep 5, 2026
@gouhongshen
gouhongshen force-pushed the agent/issue-24099-21c27da8 branch from 0e8bb63 to 8c553a0 Compare September 5, 2026 17:37
@gouhongshen
gouhongshen marked this pull request as ready for review September 5, 2026 18:25
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep review of exact head 8c553a07dd76d870bbeb627562a174f9df862028 against base 4f90107c8c486369b58cda81837ded520d86c21a.

The root-cause analysis and retry ownership are sound: update-stage INSERT/UPDATE JOIN statements do need the existing frontend statement/compile retry boundary, and this PR correctly avoids replaying the whole merge transaction. One counterexample in the broadened detector is merge-blocking.

[P1][correctness / streaming / memory] Matching every __mo_diff_ identifier misroutes legitimate user SQL and can silently produce an empty streamed result.

containsDataBranchTempTableName now accepts any identifier beginning __mo_diff_ (pkg/frontend/data_branch_helpers.go:129-134), not just the generated del_, ins_, and upd_ apply tables. That prefix is not globally reserved or rejected by DDL: the existing feature itself exposes __mo_diff_source / __mo_diff_flag columns, and users can create a database/table such as __mo_diff_orders.

The concrete failure is a Data Branch CSV diff whose target table (or database) is named __mo_diff_orders. writeCSV executes SELECT * FROM <target> with streamChan/errChan (pkg/frontend/data_branch_output.go:1612-1625, 1755-1767). The broad predicate routes it to BackExec. That branch ignores the streaming channels and returns collected batches (data_branch_helpers.go:321-366), while the caller discards the returned executor.Result and consumes only streamChan. The goroutine then closes an empty channel, so the operation can report success with an empty CSV. It also replaces bounded streaming with full BackExec result materialization, adding an O(result-size) memory/OOM risk. DATA BRANCH PICK ... KEYS (<subquery mentioning such an identifier>) has the same discarded-result shape at data_branch_pick.go:815-855.

Please keep the shared generation prefix if useful, but route only the actual apply-table families (__mo_diff_del_, __mo_diff_ins_, __mo_diff_upd_) or, preferably, pass an explicit execution mode from the generator instead of re-parsing SQL text. Add negative detector tests for ordinary __mo_diff_* database/table/column names and a public streaming regression proving CSV/PICK rows are preserved for such a user schema. The new update-stage tests should still prove both the stage INSERT and base UPDATE JOIN use BackExec.

Unhappy-path closure:

Check Result
Q1 cleanup ownership No resource-owner transfer changed; existing result cleanup remains valid on correctly classified paths.
Q2 wait/retry termination No new retry loop; compile owns the bounded statement retry. The misclassified streaming path terminates, but with lost output.
Q3 accumulation bound Fails for the counterexample: a streamable result is materialized by BackExec and the returned batches are discarded by the caller.

Exact-head UT, BVT, SCA, coverage, and build checks are green; they do not cover a legitimate user identifier under the broadened prefix. No other confirmed retry-generation, atomicity, cleanup, or performance blocker was found in the three-file diff.

@gouhongshen

Copy link
Copy Markdown
Contributor Author

The failed PROXY BVT is unrelated to this PR and is tracked by #28277. The execution CN detected the expected composite-key duplicate, but the remote pipeline failed to deliver its terminal error and mo-tester received null; the Coverage failure is only a downstream consequence of that producer failure.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep re-review of exact head 6893b29 against current main 109bf92.

The previous P1 is fixed at the correct ownership boundary. Generated delete, insert, and update staging DDL/DML now opt into the existing retry-capable frontend statement path explicitly; generic SQL no longer infers execution mode from tokens in SQL text. This preserves CSV and PICK streaming for legitimate database, table, and column names beginning with _mo_diff, while both update-stage INSERT and base-table UPDATE JOIN remain retryable.

I read the complete six-file diff and traced online merge, PICK, portable-SQL output, staging initialization, flush, clear, and deferred cleanup. Counterexamples checked include quoted and schema-qualified user identifiers, empty statements, write-file mode, partial apply failure, retry during stage insert/update/delete, cleanup after an earlier error, and repeated execution in one transaction.

Unhappy-path closure:

  • Q1: executor.Result batches remain closed by the statement owner; generated-table cleanup stays in the existing deferred owner, and write-file mode transfers ownership to the output file without invoking either executor.
  • Q2: this adds no retry loop. Retry remains bounded by the existing frontend statement/compile boundary and caller context; streaming callers retain their original channel/cancellation path.
  • Q3: ordinary CSV/PICK reads remain streaming and no longer risk accidental BackExec materialization. Staging value buffers retain their existing size/count flush bounds.

The focused route tests, public CSV and PICK regressions, merge/schema-evolution coverage, and exact-head green UT/SCA/BVT/coverage align with the implementation. I found no remaining correctness, atomicity, retry-generation, performance, leak, hang, or unbounded-growth blocker.

@mergify mergify Bot added the queued label Sep 6, 2026
@mergify

mergify Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-06 07:46 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-09-06 07:46 UTC · at f3e1599c72686ce794d878343abc83ae6b4b9fef · squash

This pull request spent 16 seconds in the queue, including 1 second running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)

@mergify
mergify Bot merged commit f3e1599 into matrixorigin:main Sep 6, 2026
49 of 52 checks passed
@mergify mergify Bot removed the queued label Sep 6, 2026

This branch was previously deployed

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

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants