Skip to content

fix(merge): enforce aggregate TN-memory headroom at admission - #33244

Merged
XuPeng-SH merged 7 commits into
matrixorigin:mainfrom
jiangxinmeng1:codex/fix-33203-merge-memory
Dec 2, 2026
Merged

XuPeng-SH merged 7 commits into
matrixorigin:mainfrom
jiangxinmeng1:codex/fix-33203-merge-memory

Conversation

@jiangxinmeng1

@jiangxinmeng1 jiangxinmeng1 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG
  • Improvement

Which issue(s) this PR fixes:

Related to #33203. This is an OOM-risk mitigation, not a claim that the incident's full allocation path is proven or that every OOM is prevented.

What this PR does / why we need it:

Merge candidate sizing already keeps a one-third margin against the TN process-RSS-adjusted headroom, but the final Acquire only checked each reservation against the full remaining headroom. Multiple candidates gathered against one snapshot could collectively consume the margin. In the MNL2 16-GiB DN incident, the Merge limit was 12 GiB and the logged outstanding estimated reservations reached 8.312 GiB while RSS rose rapidly; this motivates the aggregate guard but does not prove all RSS growth was caused by Merge.

For the specialized Merge throttler, final CAS admission now uses floor(2/3 × max(0, limit − sampled process RSS)) − existing reservations. Rejected tasks do not execute or acquire ownership. Ordinary throttlers and the raw Available() signal remain unchanged; the rejection log now includes the actual admission-available amount. This adds constant arithmetic without another RSS read. Reduced concurrent Merge admission is intentional.

A memory-denied one-shot manual Merge retains its already gathered candidates for a bounded retry instead of silently discarding them. The retry wakes when an active task completes, including when the table is paused and the normal scan interval exceeds the retry window. Completion notifications are tied to the scheduler generation. The same fix is in the 4.2-dev backport #33249.

The follow-up review of #33249 also found three cases present in this main branch: a same-ID table metadata commit could leave a queued retry attached to the old table projection; global memory pressure or PauseAll could retain an expired retry and its object metadata; and ordinary headroom recovery could happen after the last retry check but before expiry. This PR now refreshes the existing table projection, expires retries independently of admission, and schedules a final pre-expiry check.

A later review found three more shared edges. Named-object inspect Merge now resolves current visible object statistics before estimating and admitting the task. Delayed vacuum callbacks capture their table reference before scheduling, avoiding a race with metadata refresh. A retry gets one final admission attempt when its deadline timer fires slightly late, then its candidates expire after a bounded five-second grace period.

The latest review's two scheduler interactions are fixed: a scheduling pass applies empty-trigger backoff once even when many vacuum triggers drain together, and a deferred named-object merge rejects the whole requested set if any input is missing or has a drop intent. The exact-object task constructor checks the current catalog version again when the transaction task is created. Executor rejections retain one-shot work for bounded retry, and the reusable launch pad releases gathered catalog references after ownership transfers to a retry.

The scheduler now indexes the tables that actually have queued one-shot work. Task completions share one wake-up per scheduler generation. Pausing a table preserves an already-deferred vacuum assignment and its original deadline. Repeated empty notifications cap ordinary idle backoff at 24 hours, including when they arrive in separate scheduling passes.

The inspect command validates the 16-bit object number and rejects repeated object IDs before queuing the request. Different spellings of the same UUID and number identify the same object.

Validation

The branch is based on main at 914470d753. GitHub CI for fec087301f.

  • Five owning packages passed local go test -race -short -tags=matrixone_test on Darwin/arm64 with Go 1.27.1. The scope covers the resource throttler, merge scheduler, merge jobs, inspect RPC, and TN service. All six changed Go packages passed scoped go vet.
  • The repository target make static-check-changed selected all 511 packages. Full molint passed and golangci-lint reported zero issues.
  • All 35 selected scheduler and resource-admission tests and the TN constructor socket test passed 100 race repetitions each. A real TAE transaction rejected a mixed set containing a retired input and then committed a valid two-object merge in TestMergeBlocksIntoMultipleObjects. That transaction test passed 81 race repetitions.
  • The new assertions failed against the pre-fix code for a lost vacuum retry during a table pause, unbounded empty-trigger backoff across passes, and wrapped or duplicate object IDs.
  • With 10,000 registered tables and one waiter, the median cost of a release, an expiry scan, and a retry-timer calculation fell from 352 µs to 0.172 µs per iteration. Both versions allocated zero bytes per iteration across five samples with one Go CPU.
  • The previous Ubuntu unit-test job reached its two-hour timeout. Its archived test report records passing results for all four packages changed on that revision.
Validation on earlier revisions
  • The aggregate-admission regression failed before the production change because a second request was granted after the aggregate margin was exhausted; it passes after the change.

  • After merging current main, full pkg/common/rscthrottler and dependent pkg/vm/engine/tae/db/merge test suites passed.

  • The manual-denial, completion-wake, inline-completion, and promotion-reconciliation tests each passed focused -race -count=100. The full race suites for both affected packages passed once.

  • Both affected packages passed build and vet with the repository's CGo prerequisites.

  • The manual retry regressions cover success after capacity returns, bounded expiry, and a busy paused table whose active task completes within the retry window.

  • After the follow-up fixes, the full pkg/vm/engine/tae/db/merge suite passed, the new regression set passed -race -count=10, and the full merge package passed -race -count=1.

  • After merging current main at 52ae9b597d, both affected package suites and scoped go vet passed. The two prior review regressions plus the three new regressions passed -race -count=5. The preceding CI run passed UT, SCA, and BVT; its Coverage failure was dedicated worker identity mismatch, for which main now includes ci: roll out Coverage UT worker metadata fix #33375 and subsequent Coverage fixes. New CI is pending.

  • After merging main at 38c68d02ca, the new exact-input, executor-rejection, empty-vacuum, reference-release, and completion-order regressions passed 25 race repetitions. Full rscthrottler, Merge scheduler, and Merge job package race suites passed; scoped go vet, formatting, and git diff --check passed.

  • The subsequent CI passed Ubuntu UT, ARM64 SCA, and both BVT jobs. Coverage GROUP0 failed because an unrelated TN constructor test bound the shared default TCP port before reaching its metadata-failure assertion. After merging main at ba6081a1ea, that test uses a private Unix socket and passed 10 consecutive runs while TCP port 22000 was occupied. CI for this head is pending.

Remaining limits

The invariant is against the RSS sample used for each admission attempt. RSS may rise after admission, Merge estimates may understate real allocations, and unrelated TN work can grow. No pre-OOM heap profile exists for the historical incident. This PR does not hard-cap in-flight allocations. A manual request remains pending for at most one minute plus a five-second final-attempt grace after admission denial; if capacity does not return, the scheduler logs its expiry. The separate block-row estimator improvement is already on main and was not changed by this PR.

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

@mergify

mergify Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@XuPeng-SH

XuPeng-SH commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 6ca6f4788673c04a4a6b033e88c529ed49f097cb

Review completed. Read the assessment.

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

mo-bot review · mo-bot-local · review

Commit: 6ca6f4788673c04a4a6b033e88c529ed49f097cb

Assessment: request_changes

The aggregate margin is applied at the existing CAS admission owner and prevents simultaneous merge reservations from consuming the intended headroom. However, its stricter admission threshold can silently discard an accepted one-shot manual merge when another task temporarily holds memory. The scheduler needs to preserve or explicitly report that request while keeping the aggregate limit.

Model: gpt-6.1-terra / max

Session: 01a212e9-10a4-7203-9194-162fe0e55042 · slot 0 · generation 13

  • P2 pkg/common/rscthrottler/resource_throttler.go:459: A user triggers a one-shot Ln merge for a table whose automatic merging is paused while a different table holds a merge reservation. For example, with 9 GiB of sampled headroom, 4 GiB reserved, and a 3 GiB estimated task, gathering accepts the task using two thirds of the 5 GiB raw availability, but the new final admission budget is only 2 GiB.
    Impact: The scheduler has already removed the one-shot trigger before Acquire and simply skips a denied task. The command has returned successfully after enqueueing it, yet even after the competing reservation is released the requested merge is gone; on a paused table no automatic policy will regenerate it. This is a new stable-headroom denial: the merge-base admission would grant the same 3 GiB task against the 5 GiB available.
    Evidence: The manual Ln trigger is enqueued by pkg/vm/engine/tae/rpc/inspectMerge.go:522 and :554; candidate sizing uses pkg/vm/engine/tae/db/merge/utils.go:34, while scheduler.go:1237 removes one-shot triggers before the denied-task continue at :1274. A probe using the real scheduler and shared throttler, with a held reservation representing another task, passed its uncontended control but failed after the competing reservation was released on the candidate (manual-admission-candidate-retry.log; checks/1795617577740035308-2.json). The same probe passed with the merge-base version of the only changed production file overlaid (manual-admission-base.log; checks/1795617607153746829-2.json). Preserve and retry the unadmitted manual work, or return an explicit admission failure to its requester, without weakening the aggregate memory margin.

Validation:

  • The assigned private-/tmp and loopback preflight passed; checks/1795616249459526837-3.json records exit 0 on the assigned head.

  • The five changed and directly affected admission tests passed under the race detector, including aggregate, concurrent, CAS-retry, boundary and exhausted-memory cases; focused-race.log and checks/1795617451192499794-2.json retain the selected tests, command, head and exit 0.

  • The manual-trigger probe's uncontended control passed on both variants; its held-reservation case failed on the candidate and passed with the merge-base throttler file overlaid. The two commands and full logs are retained in the cited receipts and artifacts. The probe drives the scheduler directly with valid object metadata and the production throttler, not a real TN service.

  • Native prerequisites were rebuilt with the checkout's Makefiles in the diagnostic directory. native-input-verify.log and checks/1795616404121395525-2.json record that 195 copied native inputs matched the frozen checkout.

Limitations:

  • The original DN OOM's precise allocation cause remains unproven in the linked issue; this review does not claim the margin prevents every OOM.

  • The PR body reports complete owning and dependent package suites and repeated focused race tests; their raw outputs were not provided in local_validation and were not independently repeated here.

@matrix-meow matrix-meow added size/XL Denotes a PR that changes [1000, 1999] lines and removed size/L Denotes a PR that changes [500,999] lines labels Nov 27, 2026
@XuPeng-SH

XuPeng-SH commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: defc1c661e7f475a3ba738da72e7a473073e85f8

Review completed. Read the assessment.

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

mo-bot review · mo-bot-local · review

Commit: defc1c661e7f475a3ba738da72e7a473073e85f8

Assessment: request_changes

The aggregate admission guard reuses the existing throttler, and the bounded retry mechanism addresses the earlier denial, fairness, and cross-table wakeup concerns. Two remaining scheduler paths can leave an operator’s requested merge incomplete without a clear terminal outcome.

Model: gpt-6.1-terra / max

Session: 01a229e8-c998-7480-862c-3017417a9175 · slot 1 · generation 38

  • P2 pkg/vm/engine/tae/db/merge/scheduler.go:1777: An inspect merge specifies two or more objects, and available memory is sufficient for a subset but insufficient for the full requested set.
    Impact: Resolving the object sizes makes the existing memory limiter shorten the explicitly assigned task before admission. The executor receives only the retained subset, and the omitted objects are neither queued for retry nor reported as a failure. With two requested objects, this can submit a one-object task even though the inspect command requires at least two. Preserve the requested set and defer or report an inability to schedule it while retaining accurate memory accounting.
    Evidence: The scheduler probe admitted two objects with ample headroom but only one with limited headroom, with no queued trigger. The merge-base scheduler overlay retained both names in the same case, although it underestimated their admission size as zero. Results: candidate-merge-recovered.log and baseline-named-probe.log.

  • P2 pkg/vm/engine/tae/db/merge/scheduler.go:1558: A deferred manual merge reaches its final admission attempt at the retry deadline, while the aggregate admission budget still denies the task.
    Impact: The trigger has already been removed from the queue, and the denied task is not requeued because the deadline has arrived. No expiry event is emitted on this path, so the asynchronously accepted request disappears without the terminal signal promised for an expired retry. Log the terminal denial or retain it for the existing bounded expiry path.
    Evidence: A scheduler probe modeled 18 GiB of sampled headroom and 12 GiB reserved: the final denial removed the request without an expiry event. The corresponding post-grace control emitted MergeExecutorEvent-DeferredRetryExpired. See expiry-and-throttler-tests.log and expiry-log-oracle.json.

Validation:

  • The full pkg/common/rscthrottler and pkg/vm/engine/tae/db/merge suites passed once with the isolated executor; owning-packages.log, receipt checks/1796012558863182245-3.json.

  • Both affected package suites passed once with -race; owning-packages-race.log, receipt checks/1796012730780224051-3.json.

  • The named-object candidate probe failed only in the limited-headroom case; the ample-headroom control and merge-base scheduler overlay passed. Receipts: checks/1796012332195228560-3.json and checks/1796012370826968517-3.json.

  • The expiry transition probe and its log oracle completed with the expected contrasting observations; receipts checks/1796012452128567654-3.json and checks/1796012472145367522-4.json.

  • Isolation preflight passed. Native prerequisites were built outside the checkout from verified frozen-head inputs. The complete receipt and log audit is retained in review-evidence-audit.json.

Limitations:

  • The review probes exercise the scheduler boundary with controlled resource accounting and a recording executor; they are not full TN tests under physical memory pressure.

  • An initial check timed out while fetching Go modules from the configured proxy. The missing checks completed after switching to the public Go proxy with the assigned caches.

@XuPeng-SH

XuPeng-SH commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 61531cf206bd735faae2499131b2556bc4574f48

Review completed. Read the assessment.

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

mo-bot review · mo-bot-local · review

Commit: 61531cf206bd735faae2499131b2556bc4574f48

Assessment: request_changes

The aggregate margin reuses the existing reservation owner, and the bounded retry mechanism addresses the earlier fairness, pause, low-memory and cross-table wakeup findings. Two scheduler interactions remain: stale named-object retries can submit a partial merge, and draining empty vacuum triggers can postpone automatic scans for decades.

Model: gpt-6.1-terra / max

Session: 01a22aff-bb68-7e31-ae71-ee4ab48ad9d0 · slot 0 · generation 45

  • P2 pkg/vm/engine/tae/db/merge/scheduler.go:1453: Multiple vacuum checks queue non-user tombstone triggers while a table is busy, and the tombstone inputs disappear before those triggers are processed.
    Impact: Each empty trigger doubles the table's backoff in the same scheduling pass. With the default five-second interval, 32 empty triggers set the next scan about 96 years out, so automatic merging can stop for that table until another event resets its priority.
    Evidence: The candidate probe recorded nextDue=841136h46m46.290448384s after 32 triggers; its single-trigger control and the merge-base scheduler overlay both recorded 10 seconds. See model-switch-1796037618166576862/backoff-32-head.log and backoff-32-baseline.log. Bound the backoff or apply the no-work adjustment once per scheduling pass.

  • P2 pkg/vm/engine/tae/db/merge/scheduler.go:1515: A named-object merge is retained after admission denial, then a smaller overlapping merge completes and removes some of those objects before the retained request is admitted.
    Impact: The retry submits the saved object set without rechecking visibility. The executor skips missing entries, and the merge task filters objects with drop intent, so an acknowledged request for an exact set can execute on only the remaining objects.
    Evidence: A scheduler probe with 2 GiB raw availability and a reachable aggregate budget submitted three retained objects after only one remained visible; its all-visible control passed. See model-switch-1796037618166576862/stale-retry-reachable.log, executor.go:58 and mergeobjects.go:92. Revalidate the complete named set on retry and prevent the executor from silently shortening it.

Validation:

  • The private /tmp and loopback preflight passed.

  • Both affected package suites passed normally and with the race detector. The logs and receipts are attempt-1/owning-packages.log, checks/1796037435969241053-3.json, attempt-1/owning-packages-race.log and checks/1796037524206471087-3.json.

  • The stale-object probe failed on the candidate while its all-visible control passed; receipt model-switch-1796037618166576862/checks/1796037976312673329-3.json.

  • The 32-trigger backoff probe failed on the candidate and passed with the merge-base scheduler and reservation-test overlays; receipts model-switch-1796037618166576862/checks/1796037760355823801-3.json and 1796037775064570655-3.json.

  • The checkout remained clean. The discussion assessment, updated review plan and receipt audit are retained in model-switch-1796037618166576862/.

Limitations:

  • The probes exercise scheduler boundaries with a fake clock, resource controller and executor; they do not reproduce the scenarios in a live TN.

  • The linked incident establishes an OOM but does not establish its allocation source or prove that this margin prevents every OOM.

  • After the model switch, attempt-1 was mounted read-only, so the discussion assessment and updated review plan were saved in the writable model-switch directory.

@XuPeng-SH

XuPeng-SH commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: fec087301fcd9373ede3cd237e50a188ffa6b373

Review completed. Read the assessment.

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

mo-bot review · mo-bot-local · review

Commit: fec087301fcd9373ede3cd237e50a188ffa6b373

Assessment: approve

The aggregate guard reuses the existing reservation owner, and the bounded retry path preserves denied one-shot work. The earlier concerns about retry fairness, wakeups, expiry, named-object completeness, and empty-trigger backoff are addressed.

Model: gpt-6.1-terra / max

Session: 01a22aff-bb68-7e31-ae71-ee4ab48ad9d0 · slot 0 · generation 45

Validation:

  • Isolation preflight passed, and 195 native inputs plus six retained build artifacts matched the frozen checkout. Receipts: 1796168658890904442-3.json and 1796168673007783071-4.json.

  • Focused admission and scheduler tests passed under the race detector, as did both complete short owning-package suites. Receipts: 1796168849813895752-3.json and 1796168928488423997-3.json.

  • Inspect RPC and parser tests passed. A real TAE transaction rejected a stale exact input and committed a valid merge. The TN constructor test also passed. Receipts: 1796169198605224000-3.json and 1796169509966386500-3.json.

  • An isolated one-CN SQL probe verified valid, overflow, negative, and duplicate object IDs twice with cleanup on the same cluster. Receipt: 1796169352300749945-3.json.

  • git diff --check passed. Receipt: 1796169517046594196-3.json.

Limitations:

  • The SQL probe used nonexistent valid object names to check request validation and did not execute an end-to-end inspect-triggered merge; the transaction test covered a valid exact merge separately.

  • Live TN operation under physical memory pressure and merge throughput were not tested. The linked incident does not establish the original allocation source, and the guard cannot prevent every OOM.

This branch was successfully deployed

No deployments
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 size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants