Skip to content

[Bug]: spill-disk admission rejection surfaces as 20101 for ORDER BY, GROUP BY and TOP #27627

Closed

Description

@Ariznawlll

Description

When a query reaches the configured processLimitationSpillSize limit,
the supported spill paths for ordinary ORDER BY (MergeOrder), high-cardinality
GROUP BY, and ORDER BY ... LIMIT (Top) expose an internal admission error
to MySQL clients instead of a stable resource-exhausted error.

Environment

  • Branch: main
  • Commit: 5bc051abc44f34ccb6813e2a0bb5585d5a1db67e
  • Date: 2026-08-26
  • Deployment: fresh embedded 1 Log + 1 TN + 1 CN cluster for every run.
  • CN limits: GuestMmuLimitation=1GiB, ProcessLimitationSize=1GiB,
    ProcessLimitationSpillSize=8MiB.
  • Session: set @@max_dop = 1.

Steps to reproduce

Create a wide relation so the spill-disk cap, rather than the process-memory
cap, is the limiting resource:

create table spill_probe (
  id bigint not null,
  k bigint not null,
  payload varchar(256) not null
);
insert into spill_probe
select result,
       -result,
       concat(lpad(cast(result as char), 16, '0'),
              repeat(char(65 + mod(result, 26)), 240))
from generate_series(1, 250000) g;

MergeOrder / ordinary sort

set @@sort_spill_mem = 1;
select id, k, payload from spill_probe order by k, id;

Top

set @@sort_spill_mem = 1;
select id, k, payload from spill_probe order by k, id limit 250000;

High-cardinality Group

set @@agg_spill_mem = 33554432;
select id, max(payload) from group_probe group by id;

For the group variant, use group_probe(id bigint not null, payload varchar(256) not null)
and insert the same 250,000-row id,payload data above.

Actual behavior

All three paths spill and then fail at exactly the configured 8 MiB cap. The
client error leaks the internal admission implementation:

Error 20101 (HY000): internal error: execution resource admission rejected:
requested=65536 used=8388608 cap=8388608

Confirmed through the SQL protocol:

Path Reproduction Result
MergeOrder (ORDER BY) 3/3 20101/HY000 internal admission error
Group (250k distinct groups) 1/1 same 20101/HY000 error
Top (ORDER BY ... LIMIT 250000) 1/1 same 20101/HY000 error

The failed queries published zero result rows. In the same connection, the
normal 10,000-row sort control immediately succeeded after every failure;
EXPLAIN ANALYZE for that control reported SpillSize=, proving the control
uses the real sort spill path. The 10,000-key aggregate control also succeeded.

Expected behavior

The configured spill-disk cap is a supported resource limit, so an unrecoverable
admission rejection must be returned as a stable resource-exhausted error:

3015 / HY000: resource exhausted: <operator> spill disk budget exceeded

The response must not expose execution resource admission rejected or use the
generic internal-error code 20101.

Stability and controls

  • Ordinary sort reproducer: 3/3 on a fresh official-main cluster.
  • The process-memory ceiling remained 1 GiB, so this is not a process-memory OOM.
  • The 8 MiB cap is charged in 64 KiB writes; the rejection consistently occurred
    at used=8388608, equal to the configured cap.
  • No rows were returned before the terminal error, and the post-failure control
    confirmed per-query cleanup and connection health.

Evidence

Executed SQL-protocol probes:

CGO_CFLAGS="-I$PWD/cgo -I$PWD/thirdparties/install/include" env -u GOROOT \
  go test ./pkg/tests/issues/isolated \
  -run '^TestCodexMergeOrderSpillCapProbe$' -count=3 -v -timeout=12m

CGO_CFLAGS="-I$PWD/cgo -I$PWD/thirdparties/install/include" env -u GOROOT \
  go test ./pkg/tests/issues/isolated \
  -run '^TestCodexGroupSpillCapProbe$' -count=1 -v -timeout=8m

The disposable probes were removed after verification; the isolated worktree
was clean on 5bc051abc44f34ccb6813e2a0bb5585d5a1db67e.

Focused cleanup tests also pass, which distinguishes this from a leak:

go test ./pkg/sql/colexec/mergeorder ./pkg/sql/colexec/group ./pkg/sql/colexec/top \
  -run 'TestAccounted(MergeOrderSpillResourceAdmissionCleans|GroupSpillResourceAdmissionCleans|TopSpillResourceAdmissionCleans|MergeOrderForcedSpillKeepsOrdering|GroupForcedSpillReleasesMemoryDiskAndFD)' \
  -count=1 -v -timeout=5m

Code analysis

hashbuild.TerminalBudgetError already maps unrecoverable execution-resource
admission failures to a client-safe resource-exhausted error. The CTE-specific
fix in #27106 uses that path. In contrast, the verified MergeOrder, Group,
and Top call paths return spill reservation/write errors directly, allowing
the typed ExecutionResourceError to reach the frontend as 20101.

Fill has a similar direct spill-reservation shape in static review, but it was
not included in the confirmed scope because no equivalent SQL reproducer was run.

Regression coverage

Do not baseline the current 20101 behavior. After the fix, add:

  1. SQL-protocol regression coverage for the three verified operators that
    asserts 3015/HY000, zero result rows, and a succeeding same-session control.
  2. Operator unit tests that require admission errors to be normalized before
    leaving MergeOrder, Group, and Top.
  3. A threshold/spill regression at a larger scale in big-data once the
    client-facing contract is fixed.

Related

Activity

  1. added this to the MO-202608 milestone on Aug 26, 2026
  2. added
    kind/bug
    on Aug 26, 2026
  3. added theissue type on Aug 26, 2026
  4. Ariznawlll commented on Aug 26, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充受影响范围:Time Window 的 GAPFILL(PARTITION) + FILL(next) 也会将 spill 资源准入拒绝原样映射为 20101/HY000。

    验证版本:最新 main 5bc051abc44f34ccb6813e2a0bb5585d5a1db67e。

    项目 结果
    SQL 路径 单 CN;按 series_id 分组;1 秒窗口、10,001 个生成桶,GAPFILL(PARTITION) FILL(next)
    触发方式 为用小数据进入 FILL 自身的 spill 写入路径,在一次性本地隔离 worktree 中临时将 FILL 的 spill 阈值设为测试级别;CN 的 spill 配额为 1 B。该 hook 已删除,未提交任何产品代码。
    重复性 3/3 均失败:20101 / HY000,消息为 internal error: execution resource admission rejected: requested=65536 used=0 cap=1
    结果原子性与恢复 失败前未读取到结果行;同一连接随后 SELECT 1 通过。

    结论:FILL 的 newFillSpill 返回 ExecutionResourceError 后同样没有在 SQL 边界归一化为资源耗尽错误,和本 issue 已覆盖的 ORDER BY / GROUP BY / TOP 属于同一根因;修复时请将该路径纳入回归。

    补充说明:普通 FILL(next) 不会凭 WHERE 时间范围生成空桶;需要 GAPFILL(PARTITION) 才会生成连续空窗,这是本次探针首次未触发 spill 后确认的语义。

  5. assigned and unassigned on Sep 22, 2026
  6. Ariznawlll commented on Sep 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    当前 origin/main 66da3877e9e77f01083c4aac0cb9f94ab6e11fe6(2026-09-22)上,这条 20101 的客户端映射还在,但已经不是所有 spill 算子都会走到。原始 SQL 没有在这个提交上重跑。

    客户端入口是 pkg/frontend/protocol.go 的 sendErrorResponse(264-272 行)。errors.As 会沿着 Unwrap 找到 *moerr.Error。*process.ExecutionResourceError(Kind=Admission)的 Unwrap 返回 ErrExecutionResourceAdmission(pkg/vm/process/execution_resource_budget.go:129,194-200),这是 ErrInternal 20101,MySQL code 是 ER_UNKNOWN_ERROR。于是 sendErrorResponse 使用 ErrorCode() 20101,报文仍是外层 Error():internal error: execution resource admission rejected: requested=... used=... cap=...。

    hashbuild.TerminalBudgetErrorForOperator(pkg/sql/colexec/hashbuild/errors.go:38-136)会把同一类准入错误转成 moerr.NewResourceExhaustedf,即 ErrOOM 20103,MySQL code ER_ENGINE_OUT_OF_MEMORY 3015(pkg/common/moerr/mysql_error_define.go:946)。

    当前 main 已在这些 Call 边界转换:

    算子 位置
    Group pkg/sql/colexec/group/exec2.go:363-367
    MergeOrder pkg/sql/colexec/mergeorder/order.go:541-546
    Top pkg/sql/colexec/top/top.go:174-176
    Fill pkg/sql/colexec/fill/fill.go:159,e4511d6af41(2026-09-17)加入
    HashBuild / HashJoin / DedupJoin / RightDedupJoin 各 Call 返回点调用 TerminalBudgetError

    上面 2026-08-26 的 FILL 补充对应的是 5bc051a。那个提交早于 e4511d6af41。当前 Fill.Call 会转换 newFillSpill 返回的准入错误。这条路径没有在 66da3877e9 上重跑。

    本 issue 原始的 ORDER BY、本地 GROUP BY、大 K TOP 复现,在当前 main 上会先经过 MergeOrder / Group / Top 的 Call 包装。这些 SQL 同样没有重跑,所以不能据此把 issue 标成已修复。

    当前 main 上仍会把 spill 准入原样送进 sendErrorResponse、从而仍是 20101 的路径:

    1. MergeGroup.Call(pkg/sql/colexec/group/mergeGroup.go:119)没有包装。Prepare 在 allocationAccount != nil 时设置 ctr.budget(42-52 行),和 Group.Prepare 的门槛相同。spill 写入走 spillDataToDisk → openSpillBucket(helper.go:734-742)的 ReserveSpillFD / ReserveSpillDisk。失败时 Call 在 147-154、191-192、202-203 行直接返回。多 scope GROUP BY 的 merge 侧因此仍是 20101。Group.Call 对同一错误会转成 3015。

    2. Window.Call(pkg/sql/colexec/window/window.go:295)没有包装。带 ORDER BY 的窗口在 processOrder 里调用 mergeorder.SortBatchWithPrecomputedOrderAndRelease(2422-2433 行),错误从 397-398 行直接返回。sortBatch(pkg/sql/colexec/mergeorder/batch_sort.go:67-74)总会取 GetExecutionResourceBudget(),spill 文件在 spill.go:303-308 做 ReserveSpillFD(1) 和 ReserveSpillDisk(0)。窗口排序 spill 的准入失败因此仍是 20101。MergeOrder.Call 对同一类错误会转成 3015。

    修复这两处时,在各自 Call 的对外返回点使用 TerminalBudgetErrorForOperator,并加上 spill disk / spill FD 配额被拒绝的回归。内部 spill 恢复路径应继续保留 typed admission error,和 errors.go:27-29 的约定一致。

  7. Ariznawlll commented on Sep 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    再补两条仍会把未包装的 *ExecutionResourceError 交到客户端、从而显示成 20101 的路径。对照 origin/main 66da3877e9e77f01083c4aac0cb9f94ab6e11fe6。没有在集群上重跑 SQL。

    客户端映射和上次评论相同:pkg/frontend/protocol.go 的 sendErrorResponse 用 errors.As 走到内层 ErrInternal(20101 / ER_UNKNOWN_ERROR),文案仍是外层 admission 字符串。TerminalBudgetErrorForOperator 会把它收成 ErrOOM(20103 / MySQL 3015)。

    3. table_changes

    TableFunction.Call(pkg/sql/colexec/table_function/table_function.go:98-100)直接返回 state.call 的错误,没有 TerminalBudgetErrorForOperator。

    tableChangesSpillConfig(pkg/sql/colexec/table_function/table_changes.go:241-264)把 budget.ReserveSpillDisk / budget.ReserveSpillFD 原样交给 change-range spill。newRangeSpillFile(pkg/vm/engine/disttae/logtailreplay/change_handle.go:322-335)和 rangeSpillFile.Append 的 Grow(同文件 :364-365)失败时直接 return。CollectChanges 的错误从 tableChangesState.call(table_changes.go:202-204)原样出去。

    GetExecutionResourceBudget 在没有 generation 时会创建一笔。disk/FD 拒绝时客户端看到 20101,而不是 3015。

    写到一半的 record 不会被后续读到:调用方出错就 Close 这个文件,CreateAndRemoveFile 在 close 时 unlink。这里只补错误码,不是结果错误。

    4. DML RETURNING spool

    returningSpool.BeginAttempt(pkg/frontend/returning_spool.go:84-91)ReserveSpillDisk(0) / ReserveSpillFD(1) 失败直接返回。Write 的 Grow 失败(:152-153)同样直接返回。

    pkg/sql/compile/compile2.go:572 和 :797 把 BeginAttempt 的错误返回给查询。Output.Call(pkg/sql/colexec/output/output.go:87-90)把 resultSink.Write 原样返回。

    短写会 ReconcileDown 再返回,文件由 spool 的 close 路径收掉。不是泄漏,是同一个 20101 映射。

    建议

    在这两个对外边界包 TerminalBudgetErrorForOperator,和已经包过的 Group / MergeOrder / Top / Fill / HashBuild / HashJoin / DedupJoin / RightDedupJoin 一样。内部恢复路径继续保留 typed error(pkg/sql/colexec/hashbuild/errors.go:27-29)。上次列出的 MergeGroup 和 Window 仍然没包。

  8. Ariznawlll commented on Oct 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:2305954d0bf4aa5f062adb8b9379e471240abab5
    • 部署方式:本地全新 embedded 1 Log + 1 TN + 1 CN 集群;CN 配置 GuestMmuLimitation=1GiB、ProcessLimitationSize=1GiB、ProcessLimitationSpillSize=8MiB
    • 测试日期:2026-10-16

    原问题验证

    按原 issue 的 250,000 行宽表场景,设置 max_dop=1、sort_spill_mem=1、agg_spill_mem=33554432,经 MySQL SQL 协议复测:

    路径 重复次数 结果
    普通 ORDER BY(MergeOrder) 3/3 3015/HY000,merge order spill disk budget exceeded
    ORDER BY ... LIMIT 250000(Top) 3/3 3015/HY000,top spill disk budget exceeded
    高基数 GROUP BY(Group) 3/3 3015/HY000,group spill disk budget exceeded

    三条查询均返回 0 行,没有暴露 execution resource admission rejected,也没有返回 20101。源表基线校验为 count=250000, min(id)=1, max(id)=250000, sum(id)=31250125000。每次失败后,同一连接执行 SELECT 1 成功。

    Happy Path 覆盖

    Window 排序使用相同 SQL 的 100 行小数据控制,成功返回 100 行。失败查询后的同连接健康检查 SELECT 1 也均成功。

    Unhappy Path 覆盖

    原 issue 的三个算子都触发 8 MiB spill 配额上限,并稳定返回 3015/HY000。补测 Window 有序 spill 时,30,000 行、每行 2,048 字节的输入 3/3 返回 20101/HY000,错误文本为 execution resource admission rejected: requested=2048 used=8387625 cap=8388608;失败前 0 行发布,随后同连接 SELECT 1 成功。这个新问题已单独提交到 #30391。

    旧评论中提到的 MergeGroup、table_changes 和 DML RETURNING 路径本次未复测;本次结论针对原 issue 描述的 MergeOrder、Group、Top 范围。Window 路径另见 #30391。

    白盒验证

    未运行 UT 或 -race。本次通过 SQL 协议验证客户端错误码、返回行数和失败后的连接状态。

    回归测试

    本次未提交 MOTR、UT 或 big-data 回归用例。#30391 已记录修复后应增加的 Window SQL 协议回归断言。

    测试证据

    原问题 disposable SQL-protocol probe:三个算子均 3/3 返回 3015/HY000、0 行;补充的 Window probe 命令及三次原始错误输出已记录在 #30391。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions