Skip to content

[Bug]: GROUP and COUNT(DISTINCT) spill cleanup orphan files after transient delete failure #30098

Description

@Ariznawlll

Description

GROUP and COUNT(DISTINCT) spill cleanup can orphan a physical spill file after a transient RemoveFile error. The operator discards its last bucket reference and releases the disk reservation, so resource accounting reports zero while the file still exists. This is a white-box fault-injection finding, not a claim that a production filesystem has already returned this error.

Environment

  • Branch: official main
  • Commit: 175a645a85994f2c95992d3aa8d6dacb6d9e167f
  • Deployment: isolated local Go test process using the production Group and distinct-spill bucket code with a wrapper around MutableFileService; no cluster configuration change.

Steps to reproduce

  1. Open a Group spill bucket through container.openSpillBucket, write a physical record, and confirm SpillDiskUsed > 0.
  2. Wrap RemoveFile so its first call returns an injected error and every later call delegates to the normal file service.
  3. Put the bucket in currentSpillBkt and call freeSpillBkts. Repeat with a distinct-spill controller owning a root bucket and call close.
  4. Inspect the physical spill file, SpillDiskUsed, and the number of deletion attempts. Call cleanup again.

Actual behavior

  • Both Group and distinct-spill cleanup attempt deletion only once and lose the bucket reference.
  • The physical file remains after cleanup, but SpillDiskUsed=0 and SpillFDUsed=0. A later cleanup has nothing to retry.
  • The injected error is transient: a second RemoveFile call succeeds.

Expected behavior

Cleanup should remove the file after a transient deletion error. If deletion continues to fail, the bucket and disk reservation must remain owned so a later cleanup can retry, and the failure must be observable.

Stability and controls

  • Group and distinct-spill fault paths: reproduced 3/3 ordinary runs and 20/20 -race runs on the original main code.
  • No-fault Group control: file removed and disk/FD counters return to zero.
  • Failure atomicity/data check: no user table is modified by this unit-level reproducer.

Evidence

  • On original main, acceptance tests requiring two deletion attempts failed with expected: 2, actual: 1 for both Group and distinct-spill.
  • Source: pkg/sql/colexec/group/types2.go: spillBucket.free drops fileFS and releases diskToken even if RemoveFile fails; freeSpillBkts discards its bucket list. pkg/sql/colexec/group/distinct_spill.go: controller close ignores free errors and clears its bucket arrays.

Code analysis

This is a cleanup-ownership defect, not an aggregate-result mismatch. The file service was otherwise healthy: its next delete would succeed. A bounded deletion retry plus preserving failed buckets for a later cleanup repairs the reproduced case and keeps accounting aligned with physical file ownership if deletion remains unavailable.

Regression coverage

Add Group Go UTs for normal deletion, one-shot RemoveFile failure, and persistent failure followed by service recovery, covering both generic Group buckets and distinct-spill buckets. Verify file existence, retry count, disk/FD reservations, and -race stability.

Related

#30097 concerns JOIN probe spill cleanup and has a separate writer implementation. The two defects share the same resource-accounting invariant but require separate code changes and tests.

Activity

  1. added this to the MO-202610 milestone on Oct 13, 2026
  2. added
    kind/bug
    on Oct 13, 2026
  3. added theissue type on Oct 13, 2026
  4. Ariznawlll commented on Nov 4, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试不通过。GROUP 与 DISTINCT spill 清理在删除失败时丢失文件引用并释放磁盘记账,恢复后再次清理不能删除残留文件。

    关键证据

    • 2026-11-04 官方 main@0f6c07639604c40d7ca880cb63d1d282239def08,macOS arm64,独立 Go 测试进程;通过生产 openSpillBucket 创建并写入真实本地 spill 文件,关闭写入侧后确认磁盘记账大于零、FD 记账为零,仅对首次 RemoveFile 注入错误。
    • 将文件分别交给 GROUP 的 currentSpillBkt/freeSpillBkts 和 DISTINCT 的 root/close:两个入口故障各 3/3,物理文件仍可打开,磁盘和 FD 记账均为零,owner 引用已清空;再次清理没有第二次删除。两种入口各 3/3 正常对照均删除文件、释放记账。每轮结束后显式清理诊断文件。
    • spillBucket.free 在删除报错后仍清除文件服务并释放磁盘 reservation;上层两个清理入口继续丢弃 bucket。测试中的 PASS 表示对故障现象的观察断言成功,不表示产品通过。

    待解决点

    • 请研发处理 GROUP 和 DISTINCT 两条路径的删除失败、所有权和资源记账;补充一次性失败、持续失败后恢复以及正常清理的回归。相关 PR #30099 尚未合入且当前有冲突。本轮没有执行生产环境文件系统故障或并发注入。
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