Repository navigation
fix(scanner): check a dropped mount's root without loading its files - #3955
Conversation
…them A reachable but empty root (the bare mountpoint of a dropped drive) is protected when the catalog still holds files under it. The full scan decided that by loading every file row under the root, with all track, chapter and marker JSON, only to test the length. For a large TV root that is gigabytes of decoded rows on every scan for as long as the drive stays missing. Ask the existing EXISTS query instead, once for all empty roots.
A full scan with one configured root emptied keeps that root's cataloged files, reports it in SuspectEmptyRoots, and still scans the other root.
ListRootsWithCatalogedFiles matched descendants with a LIKE pattern taken from the unnested roots, ORed with the exact match. Neither can become an index condition, so a root with no cataloged files read every file row of the folder. The descendants are now matched by bytewise range in an EXISTS of their own, which is an index-only range probe, and the exact match has its own EXISTS. The matched files are the same. A table test pins which roots count: files present or missing, at or under the root, never a sibling that shares a string prefix.
|
Caution Review failedThe pull request is closed. Pre-merge checks:
|
This comment has been minimized.
This comment has been minimized.
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
@kody start-review |
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
Quick104
left a comment
There was a problem hiding this comment.
🤖 Claude Code (claude-opus-5-5)
Three small findings, none of them in the scan logic. The rewritten query matches the same roots as the old LIKE for every root shape I checked. On a 200,000-row folder under a generic plan it made two index-only probes (0.12 ms), where the old query read the whole folder sequentially (66 ms). I'm fixing all three in a commit on this branch, so there's no need to change anything yourself.
Both tests this branch adds also pass on main, so nothing failed if the query went back to a LIKE or an OR inside one EXISTS. Pin its generic plan beside the existing folderPathPrefixQuery pin, sharing that test's EXPLAIN helper. The SQL moves into a package constant so the pin can plan it. Give TestListRootsWithCatalogedFilesDB a per-run path prefix, since media_files.file_path is unique across folders, and add a root with a LIKE wildcard and no files of its own. Correct DescendantRange's comment for the filesystem root. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
🤖 Claude Code (claude-opus-5-5) Review summaryReviewed head: What I checked. The rewritten Addressed in
Left for maintainers. Nothing blocks this PR. As context, the beta book scanners still load full rows for the same yes/no question in one place ( Cross-provider review. One read-only round by Codex (gpt-6.1-sol) on Validated features. #1231 C1 (targeted scans, passed) uses the rewritten query through the targeted scan's empty-root check. The matched files are the same, so its result should hold. Full library scans are covered by cases that haven't been run yet: #1250 C1 (series), #197 (movies), #1251 C3, and the beta #1254 C1 and #1255 C3. Whoever runs them should use a build that includes this change. No passed case changes. User manual: unaffected. This is an internal change, and the scan protects the same files. Fit: 9/10 · Readiness: 9/10 — ready (assuming CI is green) Fit: it fixes real memory and time costs in the existing dropped-mount protection, which are paid exactly when a drive is missing. It reuses the query two other code paths already use and adds no setting or surface. Readiness: the query matches the same files, the plan change is shown live and now pinned by a test that fails on the old query, and the DB tests pass on a freshly migrated database. |
Main's Silo-Server#3956 extracted the same pathscope.DescendantRange helper. Keep this branch's corrected comment for the filesystem root. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Problem
Related issue: N/A
Validation tasks: none changed. The query also serves the empty-root check of targeted scans (#1231 C1, passed) and matches the same files. Full library scans fall under #1250 C1, which has not been run yet.
When a drive drops out, its mountpoint is usually still there as an empty directory. A full library scan protects that case: a reachable but empty configured root that still has cataloged files is treated as a suspect empty root and nothing under it is marked missing. To decide whether the catalog has files under the root, the scan loaded every file row under it in full, with all track, chapter and marker JSON, only to test whether the list was empty. It did that on every scan for as long as the drive stayed missing, so the scans that run while a drive is down are the ones that allocate the most.
Measured on a synthetic 225,000-file series root with rows sized like my real catalog, that load peaked at 1.6 GB of live Go heap, allocated 4.7 GB and took 6.2 s, for each empty root on each scan. The existence check allocates nothing measurable.
The scan now asks the existing
ListRootsWithCatalogedFilesquery, once for all empty roots, which only checks whether a row exists.That query had its own cost: it matched descendants with a
LIKEpattern taken from the unnested roots, ORed with the exact match, and neither can be an index condition. A root with no cataloged files read every file row of the folder. It now matches descendants by bytewise range in anEXISTSof its own, so each root is an index-only range probe. Targeted scans already used this query for their empty-root check and get the same improvement.I've titled this
fixrather thanperf. The extra work only happens while a drive is missing, and it is a full decode of every row of that root on top of what the scan already holds, which is the situation most likely to push a server over its memory limit.Approach
scanFolderByRootscollects the reachable empty roots and passes them toListRootsWithCatalogedFilesin one call, instead of callingGetByFolderAndPathPrefixper root.ListRootsWithCatalogedFilespasses each root's bounds frompathscope.DescendantRange(the bound calculationRangeCoverageClausesalready did, pulled out) and checks descendants with~>=~/~<~in oneEXISTSand the exact path in another. The matched files are the same: files present or marked missing, at or under the root, never a sibling that only shares a string prefix. Roots still come back in input order.Validation
go vet ./internal/scanner/: pass.scripts/ci/db-contracts.txt.ListRootsWithCatalogedFilesgets a table test: a root with present files, one with only missing files, one with none, a sibling that shares a prefix (/lib/moviesbeside/lib/movies2,/lib/fbeside/lib/file.mkv), a root with%and_in its name, a file that is itself the root, a root with a trailing slash, and input order.SuspectEmptyRoots, and still marks a file removed under the other root missing. This pins the dropped-mount behaviour end to end. It would also pass onmain: the post-walk re-probe protects the same root, so it guards the behaviour rather than the query.EXPLAIN ANALYZEon 200,000 rows under a generic plan: an empty root with no rows went from a sequential scan of the folder (200,000 rows filtered, 22.9 ms warm) to index-only range probes (0.37 ms).c9b13a989caddsTestListRootsWithCatalogedFilesUsesPathRangeUnderGenericPlanDBtoscripts/ci/db-pins.txt. It checks that the query's generic plan readsmedia_filesonly through an index condition boundingfile_path, and it fails when the oldLIKEquery is restored. The table test now uses a per-run path prefix.13db7efd3, including Go lint and Go DB pins, which ran both new DB tests (run). Web jobs were skipped; no web code changed.Benchmarks
Measured with a temporary harness (not committed; its source is on the evidence page), one measurement per process, five runs each, medians with the range in brackets.
main5159df6dc, which callsGetByFolderAndPathPrefixfor each reachable empty root. After:13db7efd3, which callsListRootsWithCatalogedFilesonce. Both calls ran on the same data./gc/heap/live:bytes, sampled every millisecond withGOGC=10), bytes allocated, wall time.GOMAXPROCS=6andGOMEMLIMIT=3GiB.ListRootsWithCatalogedFilesThe query plan numbers for a root with no rows are under Validation. On a server the before cost is paid only while a configured root is empty; I have no server reading for it, because none of my drives dropped during the measurements.
Evidence
Evidence: none, no user-visible change. The scan protects and reconciles the same files. The benchmark output is on the evidence page.
Evidence: https://evidence.siloserver.org/r/silo-server/pr-3955/
Risks
Checklist
AI Disclosure
mainfor equivalence with the old check (escaping, root/, trailing slashes, missing rows, order, nil results), query cost and test quality. It found two things, both fixed here. The end-to-end test still passed with the changed line removed, because the post-walk re-probe protects the same root, so the query now has its own table test. And theEXISTSquery could not use the path index, so a root with no rows read the whole folder; I confirmed that withEXPLAIN ANALYZEand switched it to range probes. It also asked for a less dramatic code comment, which I changed.c9b13a989c(the plan pin, the per-run test paths, and a comment fix).Note
Fix dropped-root detection in
Scanner.scanFolderByRootsto use one query without loading filesscanFolderByRootsnow collects all reachable empty roots and checks them with a singleListRootsWithCatalogedFilescall, instead of oneGetByFolderAndPathPrefixper root that loaded full file rows.listRootsWithCatalogedFilesSQLquery in file_repo.go matches cataloged files with bytewise lower/upper bounds frompathscope.DescendantRangeplus an exact-path EXISTS clause, and returns roots in input order.requirePathRangeIndexedhelper in path_prefix_plan_db_test.go that asserts the generic plan uses a path-range index instead of scanningmedia_files.Macroscope summarized b14d02f.