Skip to content

cdc: bound sink writes and use safe upserts - #28336

Merged
XuPeng-SH merged 9 commits into
matrixorigin:mainfrom
jiangxinmeng1:fix-27863-upsert-upstream
Sep 8, 2026
Merged

XuPeng-SH merged 9 commits into
matrixorigin:mainfrom
jiangxinmeng1:fix-27863-upsert-upstream

Conversation

@jiangxinmeng1

@jiangxinmeng1 jiangxinmeng1 commented Sep 7, 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:

issue #27863 #26949

What this PR does / why we need it:

CDC batch writes could emit an oversized statement when a large row followed a flushed statement, allocate the entire configured SQL limit for a single row, and generate invalid SQL for quoted identifiers. Unconditional upsert also changes secondary-unique conflict semantics and is rejected by MO for relevant schemas.

  • Use upsert when the primary key is the sole unique conflict arbiter, updating every visible column. Preserve REPLACE when any secondary unique index exists on either sink, including conflicts that identify multiple existing rows.
  • Share INSERT/DELETE statement assembly. Validate every row against a fresh complete statement, count the five driver-reserved bytes plus all SQL fragments, accept exact limits, and return no partial statements on errors.
  • Precompute escaped schema fragments once and allocate according to actual payload. Apply the same identifier quoting to sink initialization.
  • Preserve delete-before-insert execution, transaction rollback and watermark ownership. Replace duplicate or weak tests with exact SQL/order/count, boundary, metadata, composite-key, failure and cleanup assertions.

Validation

  • Controlled native-backed focused tests and the complete pkg/cdc package passed with -count=1; no test exclusions.
  • make build-with-prebuilt-native passed.
  • Candidate-generated SQL passed twice against real MO and MySQL 8.0.46: replay, primary-key movement, secondary unique conflicts across different rows, reserved/backtick identifiers, and failed-statement rollback with visibility checked from a second connection.
  • Repeated identical benchmark fixtures: four-column single-row allocation fell from 1,049,296 B/op to 730 B/op, with 16 to 11 allocations. Holding the payload constant at 1 KiB, 1 MiB and 16 MiB configured limits stays at 730 B/op. The 512-row case improved from roughly 132–138 us to 74–78 us. Forced 256-byte splits use fewer allocations with approximately 3% timing overhead for complete row-fit checks.

The existing source-to-created-sink schema correspondence is retained. Independently adding unique indexes to a target remains outside that contract. The public SQL checks validate the generator and sink semantics; they are not a full CDC daemon deployment test.

@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 →

@matrix-meow matrix-meow added size/XL Denotes a PR that changes [1000, 1999] lines and removed size/S Denotes a PR that changes [10,99] lines labels Sep 8, 2026

@aptend aptend 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 d24248a07654b365ca0b45e7bf044d8c3b2777db against base/merge-base 040356c3aba7c38e54778af701eaf0cb6f1745d2 and the previous reviewed head 8bc6a1991b3af9b1d0d4d9080a8fb323c934b97b. I read the complete review/comment/thread history (one prior review, no inline threads or author replies), checked issues #27863/#26949, reviewed the incremental and full five-file diff, and traced SQL generation through transaction execution, rollback, watermark ownership, batch cleanup, identifier quoting, size arithmetic, split ownership, PK/composite-PK handling, and secondary-unique conflict behavior.

The previous identifier blocker is closed: schema/table/index/column identifiers now double embedded backticks, split execution has exact expectations, delete-before-insert and rollback/watermark ownership have concrete assertions, and the PR body correctly scopes rather than claiming the two broader incidents are resolved. I found no remaining functional or lifecycle blocker in these paths.

[P2][CI] Exact-head SCA fails on two new preallocation findings

The current Matrixone CI / SCA Test on Linux/arm64 check fails with:

  • pkg/cdc/sinker_v2_sql_builder.go:74: preallocate insertColNames.
  • pkg/cdc/sinker_v2_sql_builder.go:155: preallocate suffix.

This is deterministic on every SCA run, not runner noise. I reproduced it locally with the repository golangci configuration and --enable-only prealloc; the command reports exactly those two issues and exits 2. Preallocate insertColNames from the known visible-column upper bound and give suffix a reasonable/exact capacity, then rerun SCA. Because the exact head does not pass the repository static-analysis gate, I am requesting changes rather than approving it.

Validation on the exact head with required local thirdparties/CGO artifacts: complete ./pkg/cdc passed with -count=1; focused statement-builder, insert/delete handler, and sink-creation tests passed with -race -count=3; git diff --check passed. At submission time the GitHub Ubuntu UT job is still in progress; coverage and BVT checks are green.

@aptend aptend 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 792abde2488a61316da245b31905408adf44ffdd against base 040356c3aba7c38e54778af701eaf0cb6f1745d2: APPROVE. I read both historical reviews and the issue comment (there are no inline threads), reviewed the incremental change from my prior exact head d24248a07654b365ca0b45e7bf044d8c3b2777db, and rechecked the complete five-file diff. The new commit only preallocates the two slices reported by SCA; it does not change generated SQL or ownership. The previous identifier-quoting blocker remains closed, and the full diff still preserves exact statement-size accounting, all-or-error assembly, delete-before-insert order, rollback/watermark ownership, primary-key-only upsert, and REPLACE fallback for secondary unique conflicts.\n\nThe prior CI blocker is closed: golangci-lint run --enable-only prealloc ./pkg/cdc/... reports 0 issues and the current exact-head GitHub SCA check passes. With the required native/CGo setup, complete ./pkg/cdc passed once and the focused builder, insert/delete handler, sink-creation, boundary, composite-key, and ownership set passed under -race -count=3; git diff --check passes. Required Ubuntu UT and compose jobs were still running at review time and are not claimed as completed, but I found no remaining code/review blocker.

@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 792abde2488a61316da245b31905408adf44ffdd against base 040356c3aba7c38e54778af701eaf0cb6f1745d2: APPROVE.

I read the complete five-file changed-hunk set and prior reviews, then traced builder output through snapshot/tail handlers and executor transaction ownership. The prior backtick-column blocker is closed by one identifier-quoting convention across cached stems/suffixes and setup SQL. PK-only schemas use an all-visible-column upsert; source secondary-unique schemas conservatively retain REPLACE. This relies on the existing source-created sink schema contract, not arbitrary independently added target constraints.

Size accounting includes the reserved driver header, timestamp prefix, separators and complete suffix on every statement, including the first row after a flush. Zero/tiny limits reject nonempty rows; uint64 limits no longer become eager allocation sizes. Completed statements have independent buffers and build errors return no partial SQL list. Immutable metadata/cache plus call-local assembly introduces no shared mutable build state. Existing delete-before-insert, active-transaction execution, rollback/watermark ownership and command/iterator cleanup remain intact. This bounds individual generated SQL, not total CDC backlog or every transient allocation.

Tests now check exact split count/content and mock expectations, quoted/composite identifiers, null/escaping, exact-minus-one and huge limits, post-flush errors, independent buffers, atomic order/dedup, and delete-success/insert-failure rollback with unchanged watermark; fixture ownership has explicit mpool accounting. No verified correctness, lifecycle or performance blocker remains. The scoped design avoids changing transaction/retry policy and removes max-limit-sized eager allocation; it is not evidence that broader snapshot-progress or tail-lag incidents are resolved.

Evidence: my independent exact-object source review and git diff --check pass; current exact-head CI run https://github.com/matrixorigin/matrixone/actions/runs/34196270380 has successful Ubuntu UT, SCA, coverage, proxy and pessimistic compose checks. I reuse aptend's explicitly reported exact-head complete CDC test pass and focused -race -count=3 evidence; these are not local reruns by this reviewer. Author-reported real-sink SQL/benchmark exercises are supporting evidence, not fresh full-daemon deployment acceptance. Keep #27863/#26949 incident closure and comparable end-to-end CDC acceptance separate from this bounded sink-write approval.

@mergify

mergify Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Queued — the merge queue status continues in this comment ↓.

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

mergify Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-08 12:14 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-08 13:03 UTC · at 1169dd60a7f1790d4d1c70d2f8cffb0cf57bf3b0

This pull request spent 48 minutes 28 seconds in the queue, with no time running CI.

Reason

The pull request can't be updated

This pull request seems to come from a fork, and Mergify needs the author's permission to update its branch.
The author needs to enable "Allow edits from maintainers" on this pull request, or update the branch manually.

Hint

You should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

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 kind/enhancement kind/refactor Code refactor kind/test-ci size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants