Skip to content

fix(colexec): honor cancellation at spill phase boundaries - #26675

Merged
XuPeng-SH merged 7 commits into
matrixorigin:mainfrom
XuPeng-SH:fix/spill-phase-boundary-cancellation
Aug 4, 2026
Merged

XuPeng-SH merged 7 commits into
matrixorigin:mainfrom
XuPeng-SH:fix/spill-phase-boundary-cancellation

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

What changed

  • observe cancellation before Group, MergeGroup, MergeOrder, and Top cross child EOF or a non-empty child batch into spill/evaluation work
  • stop streaming Group output at the publish ownership boundary when cancellation arrives during the same Build -> Eval call
  • check Group spill work at wave, group-batch, non-empty-bucket, bucket-flush, and reload-record boundaries
  • check MergeOrder at cached/current batch serialization/write boundaries and bounded merge/materialization chunks
  • guard MergeOrder spill-reader rollover before each serialized-batch refill and after synchronous read/unmarshal, before exposing the refilled batch
  • check Top before serialization, before write, after write, heap-pop chunks, spill reads, and result publication
  • keep partial file ownership singular: Group clears source slots as buckets transfer; MergeOrder transfers source runs before first read
  • discard uncommitted MergeOrder active-run buffers during cleanup instead of flushing after cancellation
  • preserve Reset as the generation boundary and verify cancellation -> Reset -> fresh context -> Prepare -> successful reuse

First-principles contract

Once the process context is canceled, an operator must not begin another expensive spill work unit or publish another result batch. A single synchronous marshal/write or serialized-batch read/unmarshal cannot be interrupted safely, so each is guarded before and after. Subsequent work is bounded to one Group bucket, one MergeOrder chunk (at most 256 rows), one serialized spill batch, or one Top batch write. Result ownership advances only after the publish-boundary cancellation check. Every partial resource has exactly one cleanup owner, and Reset fully terminates generation N before generation N+1 is prepared.

Validation

  • unfixed baseline: the reported EOF spill regressions return a nil error after cancellation; the new multi-batch reader rollover test fails both before-refill and during-I/O subcases on 8a3a50d197 and passes on 4ce82117ee
  • review matrix covers Group streaming publication, 0/partial/all bucket transfer and partial reload; MergeOrder partial readers, non-empty nextRuns, multi-batch rollover, and chunk materialization; and Top pre/post write plus the 8,192-row heap-pop cancellation
  • full tests pass for ./pkg/sql/colexec/... and ./pkg/sql/compile
  • package coverage: colexec 63.2%, group 80.6%, mergeorder 78.2%, top 74.8%; MergeOrder readNextBatch is 100% covered and both new cancellation bodies execute
  • GOWORK=off package selection, build, and vet pass for all four owning packages
  • all 28 newly added tests pass under -race -count=100
  • all four owning packages pass a complete race run
  • explicitly rebased onto latest origin/main@6091b418fd

Fixes #26444
Fixes #26446
Fixes #26447

Related: #26563

@XuPeng-SH
XuPeng-SH requested a review from aunjgr as a code owner August 4, 2026 05:44
@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 Author

Choose a reason for hiding this comment

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

Reviewed latest (10177e8).

按 correctness、spill/cancel 控制路径、resource lifecycle、unhappy path 和功能闭环看,这轮没有看到新的 merge-blocking correctness、cancellation、cleanup 或状态机问题。

GitHub 不允许我用当前账号 approve 自己的 PR,所以这里留 comment 记录结论:从这轮 review 看是 LGTM。

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deep review completed on exact head 10177e8.

Conclusion: LGTM — no merge-blocking correctness, cancellation, lifecycle, or bounded-work findings. If this were reviewed from a non-author account, my decision would be APPROVE. GitHub forbids approving or requesting changes on one's own PR, so this formal review is necessarily recorded as COMMENTED.

Reviewed the EOF-to-evaluation transitions in Group, MergeGroup, MergeOrder, and Top; cancellation during bucket/run/chunk spill work; ownership transfer for spill runs/readers and unattached reduction runs; Reset/Free idempotence; partial materialization cleanup; and mock-child reuse semantics. The checks stay at phase/work-unit boundaries and do not add per-row hot-path overhead.

Local validation passed: affected-package full tests; the five focused cancellation/mock tests repeated 10 times; focused race tests; colexec subtree compile; go vet; and git diff --check. Remote CI was still running and was not awaited.

@XuPeng-SH
XuPeng-SH force-pushed the fix/spill-phase-boundary-cancellation branch from 10177e8 to 33e6458 Compare August 4, 2026 06:47
@matrix-meow matrix-meow added size/L Denotes a PR that changes [500,999] lines and removed size/M Denotes a PR that changes [100,499] lines labels Aug 4, 2026

@ck89119 ck89119 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 8a3a50d197b7f058db48ba5700fa834174a28e25 against PR base 6091b418fd529badca4e1855843bd38804de396f.

Conclusion: APPROVE. After the moving-head fixes, the full phase × ownership × terminal matrix has no remaining confirmed merge-blocking correctness, cancellation, cleanup, race, or unbounded-state finding.

Verified closure:

  • Group / MergeGroup: EOF and non-EOF cancellation; final spill and respill; wave, group-batch, non-empty-bucket, bucket-flush, and reload-record boundaries; zero/partial/all bucket ownership transfer; streaming intermediate-result publication; and cancel -> Reset -> fresh Prepare -> successful reuse.
  • MergeOrder: cached/current batch spill; serialization and pre/post-write boundaries; uncommitted active-run discard; partial reader ownership; non-empty nextRuns; later read failure; bounded reduction/result materialization chunks; pre-publication cancellation; and Reset/reuse.
  • Top: non-EOF spill entry; serialization and pre/post-write boundaries; the real 8,192 heap-pop checkpoint; spill read/materialization and pre-publication checks; and Reset/reuse.
  • Q1/Q2/Q3: every partial file/batch owner reaches exactly one cleanup path, every reviewed cancellation path terminates at a bounded work-unit boundary, and no new retained state is unbounded.

Fresh local validation on this exact head passed:

  • full ordinary and full -race tests for pkg/sql/colexec, group, mergeorder, and top;
  • 26 focused tests under -race -count=100, plus the heavier Group reload/reuse test under -race -count=93;
  • full ./pkg/sql/colexec/... and ./pkg/sql/compile tests;
  • package selection, subtree build, subtree vet, coverage, and git diff --check.

Coverage was 63.2% for colexec, 80.6% for Group, 78.1% for MergeOrder, and 74.8% for Top; the new outputOneBatch path was 91.7% covered.

Remote CI at approval time has all completed checks passing. Ubuntu UT, SCA, coverage UT, Proxy BVT, and Pessimistic BVT are still pending, so CI readiness remains separate from this code approval.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

结论:request changes,但这次范围已经显著收敛。

基于最新 head 8a3a50d197b7f058db48ba5700fa834174a28e25 相对已刷新的 origin/main@6091b418fd529badca4e1855843bd38804de396f 再看一轮:上一轮我提的 Group/MergeGroup spill write、MergeOrder receiving->spill、Top spill write 这三类取消边界问题,这版基本都补上了;现在只剩 1 个 我仍认为会挡合的生产级取消缺口。

MergeOrder 的 spill reader rollover 仍然可能在取消后继续做下一批 spill I/O。

  • 位置:pkg/sql/colexec/mergeorder/spill.go:615-623、pkg/sql/colexec/mergeorder/types.go:390-419,以及调用它们的 pkg/sql/colexec/mergeorder/spill.go:773-815 / 1046-1175
  • 具体失败场景:当一个 spill run 里有 多个序列化 batch 时,某个 reader 正好把当前 batch 的最后一段 rows 复制完,取消在这之后、reader refill 之前到达。此时 advanceSpillReaderByChunk 会立刻进 reader.readNextBatch(...),而 readNextBatch / readSpillBatches 都没有任何 CancelCheck。如果这次 refill 成功,外层 merge/send loop 也不是每次迭代都重新检查取消(只有 rows >= nextSizeCheck 时才检查),所以代码仍可能先把下一批 spill 数据读出来、反序列化出来,甚至继续复制一段新 batch 的 rows,然后才把 context.Canceled 抛上来。
  • 为什么我认为这点还没被现有新测试覆盖到:新增 mergeorder 取消用例用的 helper newCancellationTestSpillRun(...) 还是单批 run(order_test.go:2158-2178 里写死 run.batchCount = 1),所以目前覆盖到的是 EOF/owner-transfer/chunk-boundary;没有命中“同一个 reader 成功切到下一批” 这个 refill 路径。
  • 建议:在 advanceSpillReaderByChunk 调 reader.readNextBatch 前补一次取消检查,并且最好把检查再下沉到 readNextBatch / readSpillBatches(或至少在 refill 成功后、重新进入 copy loop 前再补一次),把 reader rollover 本身也变成硬边界;同时补一个 multi-batch spill run 的回归用例。

如果这里因为 GitHub 的 author self-review 限制而显示成 COMMENTED,请把这条结论按 request changes 理解;就 latest 这版而言,我现在只剩这个 blocker。

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

最新评论成立,已在最终 head 4ce82117ee 系统性采纳。

修复:

  • spillRunReader.readNextBatch 现在在释放当前 batch / 开始下一 serialized batch I/O 前检查 cancellation。
  • 同步 read + unmarshal + order-column restore 完成后,在把 refilled batch 暴露给 heap/copy loop 前再检查一次。
  • 没有在 advanceSpillReaderByChunk 叠加同一时刻的重复检查,也没有在 payload 内做半 batch 中断。serialized batch 是格式与内存所有权的原子、有限工作单元;内部中断会留下不可恢复的部分反序列化状态。成本固定为每次 refill 两次 CancelCheck,不进入 per-row 热路径。

回归:

  • 新增真实 two-batch spill run,取消发生在第一批最后一行被消费后、第二批 refill 前;断言返回 context.Canceled、buffered bytes 不减少、旧 batch 不被清空、reader 仍是唯一 owner,最终 fd 关闭且 mpool 归零。
  • 新增独立 reader injector,在第一次真实底层 read 后取消;断言最多完成当前 serialized batch,post-I/O check 阻止新 batch 被发布。
  • 这两个子用例在未修复 head 8a3a50d197 上都稳定失败,在当前 head 上通过。

反省:上一轮把 64-row merge chunk boundary 与 serialized-reader rollover boundary 当成了可互相覆盖的取消层级;它们其实是两个独立的 work-unit 轴。更根本的问题是测试 helper 写死单 batch,把 current batch exhausted × next batch exists 这一产品状态压没了;同时只依赖 cancelOnDoneCheckContext 会让测试只能命中已经存在的 checkpoint,无法证明缺失的 checkpoint。后续会先展开 batch position(within/end) × successor(next/EOF/error) × cancel(before/during/after refill) × terminal(cleanup/Reset),并至少保留一个不依赖生产 CancelCheck 的 injector。

最终验证:

  • 最新 origin/main@6091b418fd 已显式 rebase,merge-base 精确一致;
  • ./pkg/sql/colexec/... 与 ./pkg/sql/compile 普通测试通过;
  • 四个 owning packages 的 list/build/vet、完整普通测试与完整 race 通过;
  • 整 PR 28 个新增测试全部 -race -count=100 通过;
  • coverage:colexec 63.2%、group 80.6%、mergeorder 78.2%、top 74.8%;readNextBatch 100%,两个新增取消 body 都有实际命中。

新一轮远端 CI 已由 push 触发。

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

结论:LGTM / approve。

基于最新 head 4ce82117ee174e5a65266289869488f9f54da429 相对 origin/main@6091b418fd529badca4e1855843bd38804de396f 再做了一轮 latest deep review:这版把上一轮剩下的 mergeorder spill-reader rollover 取消边界也补上了,readNextBatch 现在在 reader refill 前先观察取消,而且新增测试已经把 refill/read 这条路径补到了。

按 correctness、spill/cancel 控制路径、resource lifecycle、unhappy path 和功能闭环再扫一遍,这轮我没有再看到新的 merge-blocking correctness、cancellation、cleanup 或状态机问题。若不是作者自评限制,我这轮会直接给 APPROVE。

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

Codex automated review

No findings. The final head closes the remaining MergeOrder reader-rollover gap with cancellation checks before refill and after synchronous read/unmarshal, while preserving single-owner cleanup. Prior Group, MergeGroup, MergeOrder, and Top cancellation, resource-lifecycle, and reuse concerns are addressed with focused regressions. Diff and formatting checks passed; local test execution was unavailable because the checkout lacks required MatrixOne native libraries and headers.

@XuPeng-SH
XuPeng-SH merged commit 7ca0dd2 into matrixorigin:main Aug 4, 2026
22 of 23 checks passed
@XuPeng-SH
XuPeng-SH deleted the fix/spill-phase-boundary-cancellation branch August 4, 2026 10:36

@LeftHandCold LeftHandCold left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Deep-reviewed on exact head d074a9b0a544 against base 1f8cd465b827. The rebase preserved the reviewed change exactly (stable patch-id matches the pre-rebase head), with no semantic conflict-resolution delta.

Verified closure:

  • Group/MergeGroup observe cancellation at EOF, spill wave/batch/bucket, reload-record, and streaming publication boundaries; partial bucket transfers keep a single cleanup owner.
  • MergeOrder covers cached/current writes, pre/post I/O, active-run discard, partial reader ownership, multi-pass reduction, bounded materialization chunks, multi-batch reader rollover, and pre-publication cancellation.
  • Top covers pre/post spill write, bounded heap/read materialization, cleanup of uncommitted output, and cancellation -> Reset -> fresh Prepare reuse.
  • Q1/Q2/Q3: no leak/double-cleanup break, every reviewed cancellation path terminates after a bounded synchronous unit, and no new unbounded retained state. Added checks stay off per-row resident hot paths.

No blocking correctness, cancellation, lifecycle, performance, or compatibility issue found. The current-head CI rerun is still in progress; this approval is the code-review conclusion.

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

size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

5 participants