Skip to content

fix: keep data branch diff after branch compaction and GC - #27464

Merged
mergify[bot] merged 10 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-27451-e98b84e7
Aug 22, 2026
Merged

mergify[bot] merged 10 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-27451-e98b84e7

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Aug 21, 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 #27451

What this PR does / why we need it:

Root cause

After branch-side mergeobjects, incremental replay can encounter a GC-removed post-clone predecessor object. Snapshot-range recovery reaches the live merge output, but that TN-created compacted object has no per-row commit timestamp metadata for evaluating the requested range. The replay layer previously reported this unevaluable condition as ErrFileNotFound, conflating missing metadata with a missing file and re-entering strict replay.

Exact recovery therefore has to compare visible rows at the range boundaries. Two additional constraints are required for correctness and safety:

  • an ALTER/clone range must read its start state from the protected parent relation, with that relation's historical schema, rather than the child endpoint schema or the child's GC-eligible bootstrap objects;
  • the start snapshot and the batches held for failure-atomic range replay must share Data Branch's governed memory budget and spill under pressure instead of growing unbounded on the Go heap.

A separate initialization gap remained after those recovery paths were added: NewChangesHandlerWithPartitionStateRange can synchronously return ErrFileNotFound while resolving a delete chain or loading object metadata. This happens before a range handle exists, so the iteration-time recovery in bufferCurrentRange cannot run. The constructor path recognized only the distinct non-evaluable-metadata error and returned physical file loss directly.

Changes

  • Return a distinct commit-ts-block-not-evaluable error for a physically present compacted object whose row timestamps cannot evaluate the range.
  • Under SnapshotReadPolicyVisibleState, rebuild an unevaluable range from its visible start and end states after normal replay and snapshot-range recovery are exhausted.
  • Use one recovery-error classifier for both range construction and iteration. If snapshot-range construction reports either physical ErrFileNotFound or non-evaluable commit-ts metadata, initialize the exact boundary-state reader; preserve every unrelated constructor error unchanged.
  • Carry the protected parent relation across each Data Branch lineage edge. Clear it when a BETWEEN lower bound advances inside the range, where it would no longer represent the requested boundary.
  • Build each boundary reader from its own historical TableDef. Match comparable columns by stable sequence number and exact type, compare by logical primary key/payload, project endpoint columns/defaults into output, and ignore physical RowID rewrites caused only by compaction.
  • Store start-side rows in a spillable BranchHashmap adapter using the existing shared Data Branch 80% memory throttler. Strict-capacity mode returns ErrMPoolCapacity instead of falling back to unaccounted heap memory.
  • Reserve buffered range batches against the same throttler and release reservations on delivery, retry, capacity failure, and close.
  • Use a repository-compliant moerr sentinel for bounded drain termination; identity matching preserves callback and storage errors while satisfying the SCA error-construction policy.
  • Extend diff_9 with both the original multi-object merge/GC reproduction and an ALTER-add-column variant that proves historical projection and default behavior through three forced-GC cycles.

Issue-to-test proof

  • diff_9 Case 4 reproduces [Bug]: DATA BRANCH DIFF loses history after branch-table mergeobjects and GC #27451 with six flushed source objects, changes on both sides, branch compaction, three checkpoint/GC cycles, exact four-row DIFF assertions, output count, table counts, and protection-row checks.
  • diff_9 Case 5 adds a branch-only defaulted column, changes only that column on an otherwise unchanged row, compacts the branch, runs three checkpoint/GC cycles, and proves the unchanged row is not emitted while the exact update/delete/update/insert rows and final table counts remain correct.
  • TestInitializeVisibleStateRangeRecoversConstructorFileNotFound proves constructor-time physical file loss selects exact visible-state recovery and forwards the original cause to that transition.
  • TestInitializeVisibleStateRangePreservesNonRecoveryError, TestInitializeVisibleStateRangeKeepsSnapshotReaderOnSuccess, and TestInitializeVisibleStateRangePropagatesRecoveryFailure cover the other initializer transitions: unrelated errors remain visible, successful snapshot construction does not invoke recovery, and recovery-constructor failures propagate.
  • TestVisibleStateChangesHandleProjectsHistoricalSchema proves start and endpoint readers use compatible per-snapshot projections and that endpoint-only columns neither corrupt the historical scan nor create bogus updates.
  • TestNewVisibleStateChangesHandle proves the supplied protected parent relation is the start boundary and verifies reader/RowID ownership.
  • TestVisibleStateStoreSpillsAndDrainsHighCardinality exercises 5,000 keys under a 256-byte allocator and verifies spill, lookup, bounded draining, cleanup, and zero retained allocation.
  • TestVisibleStateStoreRejectsUnspillableEntryAtCapacity proves a single oversized entry fails with controlled ErrMPoolCapacity and leaks no allocation.
  • TestVisibleStateStoreBoundaryErrors covers empty and invalid input, missing keys, exact callback-error propagation, zero-sized drain, idempotent close, allocator cleanup, and rejection after close.
  • TestPartitionChangesHandleBufferCurrentRangeCapacity proves a later reservation failure cleans already-buffered batches, the failing batch, and all reservations.
  • The remaining visible-state unit tests cover update/insert/delete, chunking, delete suppression, physical RowID rewrites, invalid schema/timestamps/RowIDs, reader failures, partial-reader cleanup, repeated completion, and nil/partial close paths.

Verification

  • make build
  • controlled CGo go test -count=1 for ./pkg/vm/engine, ./pkg/vm/engine/disttae, ./pkg/vm/engine/disttae/logtailreplay, ./pkg/frontend/databranchutils, and ./pkg/frontend; the full set passed again after merging current main
  • controlled CGo focused go test -race -count=20 for disttae visible-state/partition recovery tests
  • controlled CGo focused go test -race -count=20 for the spill/capacity store tests
  • after the constructor-time recovery update: focused initializer tests, full ./pkg/vm/engine/disttae, focused initializer -race -count=20, full disttae -race -count=1, and disttae go vet
  • after the SCA sentinel update: make err-check, controlled CGo build, vet, and full ./pkg/frontend/databranchutils tests
  • after the coverage update: TestVisibleStateStoreBoundaryErrors with a focused coverage profile, full databranchutils tests, focused -race -count=100 (measured duration below timer resolution), and full databranchutils -race; the focused profile covers 11 blocks that the failed CI profile missed
  • controlled CGo go vet for all changed packages
  • mo-tester -p test/distributed/cases/git4data/branch/diff/diff_9.sql -m run -g -n: 133/133 statements passed; generated results were manually checked for exact SQL semantics before being accepted
  • CI on 38692335d3 passed build, UT, arm64 SCA, UT coverage, compose BVT, and standalone BVT. Changed-code coverage alone failed at 297/400 (74.25%, required >75%); the focused profile for TestVisibleStateStoreBoundaryErrors covers 11 previously missed changed blocks. CI for 73f7d35242 is pending.

Residual risk

Visible-state recovery scans both boundary snapshots and can spill the start side to local disk, so it is more I/O-intensive than incremental replay. It is restricted to the Data Branch visible-state policy and activates only after replay metadata cannot evaluate the compacted range or the snapshot-range constructor proves required physical history has already been removed. Memory is governed by the same admission budget as the downstream diff hashmaps; an unspillable entry or buffered batch returns a controlled capacity error with cleanup rather than risking CN OOM. Snapshot, catalog, schema, spill, reader, and unrelated constructor failures are propagated rather than interpreted as empty state.

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

@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 completed on exact head 3869233. I re-read all historical reviews, replies, and resolved threads; checked the one-file increment after approved head 4eb4799 and re-evaluated the complete PR diff. The moerr drain sentinel is propagated by identity through shardCursor.ForEach, so the equality check preserves bounded drain semantics; the historical-schema, bounded-memory, and constructor-time recovery fixes remain closed. Validation passed under the required CGO setup: owning/dependent package list and build, full databranchutils and disttae tests, focused high-cardinality spill/drain under -race -count=20, go vet, and git diff --check.

@gouhongshen

gouhongshen commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor Author

The current CI blocker is unrelated to this PR and remains tracked by canonical issue #27408. On workflow run 32569549932 attempt 1, the only failed coverage package was again pkg/sql/plan; the 14 GiB cgroup reached its exact limit and OOM-killed plan.test at 11,689,004 KiB anon RSS. No assertion, panic, compilation, or timeout error preceded it, and the PR diff does not touch planner code. Ordinary UT, SCA, Proxy BVT, and Pessimistic BVT all passed; the derived Coverage job failed only because the producer profile was unavailable. The single allowed failed-jobs rerun is now workflow attempt 2.

@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 completed on exact head 73f7d35242f356f0fde534d1611ec3ed0d0fac18. The two prior P1 blockers are closed: historical snapshot scans now use the exact lineage boundary and schema-compatible Seqnum/type projection, while visible-state recovery uses a strict-capacity spillable store plus shared-throttler admission for buffered batches. Constructor-time FileNotFound now follows the same exact-recovery classifier as iteration-time failures. The latest commit only adds boundary/error lifecycle coverage and does not weaken behavior.

Unhappy-path audit: store/readers have one effective cleanup owner on constructor failure, exhaustion, and Close; buffer reservations are released on delivery, retry, capacity failure, and Close; accumulation is bounded by the shared Data Branch throttler and spill path, with controlled ErrMPoolCapacity for an unspillable entry.

Validation on this head: full tests for pkg/frontend/databranchutils, pkg/vm/engine, pkg/vm/engine/disttae, and pkg/vm/engine/disttae/logtailreplay; focused visible-state store and recovery/resource tests under -race -count=10.

@mergify

mergify Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-22 09:48 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ❌ Checks failed · in-place
  • 🚫 Left the queue — 2026-08-22 10:15 UTC · at 7b7244bd7f72912196a5f6d47a9b465f661bd08d

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

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

Reason

The merge conditions cannot be satisfied due to failing checks

Failing checks:

Hint

You may have to fix your CI before adding the pull request to the queue again.
If you update this pull request, to fix the CI, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

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

@mergify

mergify Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-22 11:09 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ❌ Checks failed · in-place
  • 🚫 Left the queue — 2026-08-22 11:37 UTC · at feaf4f5a2c9829cd11a63e0a85c8780c5115b2f1

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

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

Reason

The merge conditions cannot be satisfied due to failing checks

Failing checks:

Hint

You may have to fix your CI before adding the pull request to the queue again.
If you update this pull request, to fix the CI, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

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

@mergify

mergify Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-22 12:33 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-08-22 12:33 UTC · at feaf4f5a2c9829cd11a63e0a85c8780c5115b2f1 · squash

This pull request spent 34 seconds in the queue, including 2 seconds 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)

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/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants