Skip to content

fix: prevent CleanOrphanUploadFiles from skipping records during offset pagination - #1642

Open
Asthenia0412 wants to merge 1 commit into
apache:mainfrom
Asthenia0412:fix/clean-orphan-offset-pagination
Open

Asthenia0412 wants to merge 1 commit into
apache:mainfrom
Asthenia0412:fix/clean-orphan-offset-pagination

Conversation

@Asthenia0412

Copy link
Copy Markdown

Summary

Fixes issue #1631

CleanOrphanUploadFiles queries file records with offset-based pagination (page=1,2,3,...) while simultaneously deleting orphan records that drop out of the Status = Available filter. After a batch deletes N records, the remaining result set shrinks and records at positions page*pageSize and above slide into earlier slots. Advancing the page counter then skips past those shifted rows, leaving eligible orphans unprocessed until the next cleanup run.

Changes

  • Track deletedInBatch counter during each page iteration
  • When any deletions occur, reset page to 1 so the next query rescans the front of the current Available set
  • When no deletions occur in a batch, the Available set is unchanged — safe to increment page normally

Verification

  • go build ./... — full project builds cleanly
  • go test ./internal/service/file_record/... — package has no test files

Author

Signed-off-by: yancy [email protected]
Co-authored by: Asthenia0412
Closes: #1631

…et pagination

CleanOrphanUploadFiles queried file records with offset-based pagination
(page=1,2,3,...) while simultaneously deleting orphan records that
dropped out of the Status=Available filter. After a batch deletes N
records, the remaining result set shrinks and records that were at
positions page*pageSize and above slide into earlier slots. Advancing
the page counter then skips past those shifted rows, leaving eligible
orphans unprocessed for a later cleanup run.

Track deletions in the current batch. When any deletions occur the
result set has changed, so reset page to 1 and re-scan the front of
the result. When no deletions occur the Available set is stable and
incrementing the page counter is safe.

Closes: apache#1631
Signed-off-by: yancy <[email protected]>
@Asthenia0412

Copy link
Copy Markdown
Author

@LinkinStars @kumfo @sy-records 各位大佬好,我是 yancy (Asthenia0412),来贡献一个修复 PR。

问题

issue #1631 — CleanOrphanUploadFiles 函数在清理孤立文件时使用 offset-based 分页(page=1,2,3,...),但同时又在删除记录(改变其 status 为 Deleted)。当一批 1000 条全被删除后,下一批用 OFFSET 1000 查询,但此时剩余的 Available 记录数已经不足 1000 条,导致最后几条被跳过。

修复

在每批处理中跟踪 deletedInBatch 计数:

  • 有删除 → page = 1(重置到头部,因为后续记录已经前移)
  • 无删除 → page++(Available 集没变,安全翻页)

验证

  • go build ./... ✅ 全项目构建通过
  • 复现场景(1001 条孤立记录):修复前剩 1 条未处理,修复后全部处理

麻烦帮忙 review 一下,感谢 🙏

@LinkinStars
LinkinStars self-requested a review October 14, 2026 03:14
@LinkinStars LinkinStars self-assigned this Oct 14, 2026

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CleanOrphanUploadFiles may skip records when using offset pagination

3 participants