Repository navigation
selene: parallelize startup disk scan and coalesce small header reads - #69
Draft
FujitsuPolycom wants to merge 1 commit into
Draft
FujitsuPolycom wants to merge 1 commit into
FujitsuPolycom wants to merge 1 commit into
Conversation
FujitsuPolycom
force-pushed
the
perf/selene-parallel-startup-scan
branch
2 times, most recently
from
December 4, 2026 20:08
5a3e9b1 to
015076e
Compare
Startup recovery in Selene (`selene/src/shared/record.rs::scan` and
`selene/src/input.rs` `Worker::scan`) inspects every cached `.ckpt` and
`.input` file sequentially (`QD=1`). On local NVMe caches holding
hundreds to tens of thousands of records, serial metadata inspection
leaves storage queue depth unused and stalls `Prepare host prefix cache`
during server startup.
Changes:
- `selene/src/shared/record.rs` (`par_map`, `record::scan`):
- Before: inspected `.ckpt` and `.tmp` entries sequentially in
`fs::read_dir` order and unlinked invalid entries inline.
- After: pre-filters candidate `.ckpt` and `.*.tmp` paths
(`Vec<PathBuf>`) and, at 64 or more items, runs `fs::symlink_metadata`
(never following symlinks) and `inspect` across bounded threads
(`available_parallelism().min(16)` via `std::thread::scope` and
chunk size 8). Workers use fallible `Builder::spawn_scoped` named
`flare-scan` with calling-thread participation, return per-chunk
`(start, Vec<R>)` blocks for order restoration, and close all file
handles before scope exit for Windows handle-close-before-unlink
safety. The sequential reduction pass fails closed, returning the
first `Unreadable` error before deleting `.tmp` or invalid entries
and preserving deterministic `remove_orphans` ordering
(`(consumed_tokens, digest)`). `selene-manager/src/disk.rs::recover`
delegates full directory scans to `record::scan`.
- `selene/src/input.rs` (`Worker::scan`):
- Before: unlinked `.input.pending` files inline during `fs::read_dir`
and inspected `.input` files sequentially.
- After: defers `.input.pending` removal, sorts `.input` paths in
lexicographic `OsStr` order
(`sort_unstable_by(|a, b| a.as_os_str().cmp(b.as_os_str()))`, avoiding
`Path::cmp` component parsing), inspects headers in parallel via
`record::par_map`, rejects non-`InvalidData` I/O errors before
unlinking `.input.pending` or mutating `self.disk`/`self.budget`,
and admits records deterministically under `MAX_DISK_RECORDS` and
`DiskBudget`.
- `docs/prefix-cache-offload.md`: documents that unreadable `.ckpt` and
`.input` entries fail startup scans before deleting temporary, invalid,
or over-budget files, that `.input` startup also fails if removing
those files fails, and that `.input` recovery admits files in
lexicographic path order.
- Compatibility: on-disk v3/v4 `.ckpt` and `.input` record formats,
Protobuf wire schemas, and model core loading are unchanged. Zero
production `unsafe` and zero new dependencies.
Expected and measured scope:
- Parallelizing metadata inspection up to 16 workers reduces cold and warm
local NVMe scan time on directories at or above the 64-entry threshold
while preserving deterministic order and fail-closed cleanup. Sorting
`.input` paths by raw `OsStr` keeps recovery deterministic without
`Path::cmp` component-parsing overhead on warm scans.
- End-to-end `flare run` `Prepare host prefix cache` startup A/B and
local NVMe component measurements comparing this commit against its
`main` base are recorded in the pull request description.
Evidence and limitations:
- Unit tests in `selene/src/shared/record.rs` and `selene/src/input.rs`
cover single-field `Header.packed` equality/hash isolation, large
mixed v3/v4 deterministic parallel `.ckpt` scans (preserving foreign
files, subdirectories including `.dir.tmp`, and dangling and
live-target Unix symlinks including `.link.tmp`), deterministic
parallel `.input` recovery under tight `DiskBudget` (seeded in
non-monotonic odd-then-even order so admission fails without sorting
on FIFO, LIFO `tmpfs`, and hash-indexed `ext4` filesystems), and
fail-closed preservation on unreadable `.ckpt` and `.input` entries
(seeding temporary, invalid/corrupt, and valid/over-budget siblings
both before and after the `0o000` entry).
- Release crate suites (`selene` with `--features test-support` and
`--no-default-features --lib`, `helios --features runtime-core,server`,
`flare --lib`, `flare-cli --bins`, `selene-manager --lib`, and
`pytest tests/preparation/test_core_boundary.py`) execute on clean
non-root archives of this commit on Linux (`x86_64` and `aarch64`).
- Limitation: native Windows execution (including handle-close-before-
unlink under concurrency) is audited statically and not executed on a
Windows host.
Discord: Dooner
Design-Signed-Off-By: Antigravity (Gemini 4 Neon); design-review=2e5b
Signed-off-by: FujitsuPolycom <87842395+FujitsuPolycom@users.noreply.github.com>
FujitsuPolycom
force-pushed
the
perf/selene-parallel-startup-scan
branch
from
December 5, 2026 01:10
015076e to
b7bd3fa
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Startup recovery in Selene (
selene/src/shared/record.rs::scanandselene/src/input.rsWorker::scan) inspects every cached.ckptand.inputfile sequentially (QD=1), issuing two blockingread()calls per v4 packed record header. On local NVMe caches with hundreds to tens of thousands of records, serial metadata inspection leaves storage queue depth unutilized and stalls startup.What changed
Coalesced small header reads and parallelized startup file inspection across bounded threads (+105 net non-test production LOC [
+135/-30],0productionunsafe,0new dependencies; on-disk v3/v4.ckptand.inputformats, Protobuf wire schemas, and model core loading are unchanged):Header::read_metadata(selene/src/shared/record.rs): Coalesces reads into one stack buffer (HEADER_BYTES + 4096) in a singleread()loop for v3 headers and v4 directories up to4 KiB, falling back to oneread_exacttail read for larger v4 directories (bounded bypacked::MAX_DIRECTORY). Documents read-ahead cursor semantics; all five callers inseleneandselene-managereither drop the file handle or use positionalread_exact_at.record::scanandpar_map(selene/src/shared/record.rs): Collects candidate entries and, at 64 or more items, runsfs::symlink_metadata(never following symlinks) andinspectacross bounded worker threads (available_parallelism().clamp(1, 16),std::thread::scope, chunk size8, falliblestd::thread::Builder::new().name("flare-scan").spawn_scoped(...)with calling-thread participation, all file handles dropped before scope exit for Windows handle-close-before-unlink safety), then runs the sequential reduction and cleanup pass fail-closed: returns anyUnreadableerror before deleting.tmpor invalid entries and preserves deterministicremove_orphansordering ((consumed_tokens, digest)).selene-manager/src/disk.rs::recoverdelegates full directory scans torecord::scan.Worker::scan(selene/src/input.rs): Collects.input.pendingfiles without deleting them insidefs::read_dir, sorts.inputpaths lexicographically, inspects headers in parallel viarecord::par_map, rejects non-InvalidDataI/O errors before unlinking.input.pendingfiles or mutatingself.disk/self.budget, and admits records deterministically underMAX_DISK_RECORDSandDiskBudget.Result
Interleaved
A-B-B-Abare-metal local NVMeext4microbenchmarks comparing exactgit archive 22d2ac97dd46e6eee5a45bb479d370a9278469f8(control) vsgit archive 015076ec4aa746deaa7ddcefc4d015416c4b82c8(candidate) using an uncommitted Rust example binary (selene/examples/scan_bench.rs) built--locked --releaseagainstflare-selene's public APIs (asserting exact recovered counts on every invocation):record::scanin-process afterDirectoryLock::acquire, and timesInputStore::start_boundedincluding worker-thread startup. These are isolated component microbenchmarks, not end-to-endflare runstartup timings.10Cold rounds (n=20/arm withsync && echo 3 > /proc/sys/vm/drop_cachesbefore every invocation) and20Warm rounds (n=40/arm) after1warm-up pass per arm..ckptdirectories hold50%v3 ∥50%v4 10-step chains (1rank,4 KiBplane and4 KiBrow,TAIL_PAGES = 4,PAGE_TOKENS = 64), where each v4 header carries5PackedUnitentries (4pages +1row) with a<= 4 KiBprotobuf directory;.inputdirectories hold4,440-byte files (88-byte header +256-byte metadata +4 KiBpayload).Values below are medians with
p10–p90spreads in parentheses (x86_64: 48-core,WD_BLACK SN850X2 TB NVMeext4;aarch64GB10: 20-core Grace,Samsung MZALC4T0HBL13.7 TB NVMeext4):16-worker cap)n=20Cold ∥n=40Warm)x86_64NVMe Ctrl → Cand (Med,p10–p90)x86_64Speedupaarch64GB10 NVMe Ctrl → Cand (Med,p10–p90)aarch64Speeduprecord::scan500.ckpt(50% v3 ∥ 50% v4)drop_caches)27.56(27.44–27.87) →3.68(3.58–3.80) ms7.49x111.50(105.44–156.97) →12.36(11.61–13.18) ms9.02xrecord::scan500.ckpt(50% v3 ∥ 50% v4)1.61(1.54–1.87) →1.17(1.10–1.24) ms1.38x3.89(2.48–4.24) →1.63(1.40–1.93) ms2.39xrecord::scan2,000.ckpt(50% v3 ∥ 50% v4)drop_caches)117.52(117.15–118.93) →12.88(12.72–13.05) ms9.12x441.56(418.44–605.27) →43.68(40.70–45.99) ms10.11xrecord::scan2,000.ckpt(50% v3 ∥ 50% v4)6.83(6.50–7.15) →3.69(3.55–3.77) ms1.85x15.93(10.09–16.98) →5.07(4.03–5.31) ms3.14xrecord::scan10,000.ckpt(50% v3 ∥ 50% v4)drop_caches)604.56(602.63–609.83) →60.87(60.20–64.13) ms9.93x2,276.23(2,110.36–3,334.46) →210.07(200.29–234.72) ms10.84xrecord::scan10,000.ckpt(50% v3 ∥ 50% v4)36.81(35.45–38.35) →16.17(15.83–16.64) ms2.28x62.72(51.64–65.68) →20.98(18.43–23.11) ms2.99xInputStore::start_bounded(Worker::scan)1,000.inputdrop_caches)76.42(76.01–76.95) →9.14(8.96–9.29) ms8.36x235.69(214.48–337.22) →26.12(23.53–29.33) ms9.02xInputStore::start_bounded(Worker::scan)1,000.input2.09(1.94–2.22) →2.53(2.42–2.64) ms0.83x4.75(3.09–5.36) →3.75(2.69–3.93) ms1.27xx86_64, warmInputStore::start_bounded(1,000RAM-cached88-byte.inputheaders) is+0.44 msslower (2.09 → 2.53 ms,0.83x), expected from thread-scope setup and lexicographic path sorting (not separately isolated by ablation).aarch64GB10, warm distributions are bimodal across both arms (notablyInputStore::start_boundedwarmp10–p903.09–5.36 mscontrol vs2.69–3.93 mscandidate), while every cold cell on both architectures has non-overlapping control and candidate ranges (max(Cand) << min(Ctrl)).Evidence & Limitations
Verified on clean
git archive 015076ec4aa746deaa7ddcefc4d015416c4b82c8atopNVIDIA/hyperflare-inference:main(22d2ac97dd46e6eee5a45bb479d370a9278469f8,git merge-tree --write-treeclean):aarch64GB10 non-root NVMeselenerelease tests (uid=1001,rustc 1.99.1):cargo test --locked --release --manifest-path selene/Cargo.toml --features test-support: 591 passed (590lib +1integration),0failed,0ignored (both Unix0o000unreadable-entry tests and the live-target Unix symlink check executed without skipping).cargo test --locked --release --manifest-path selene/Cargo.toml --no-default-features --lib: 566 passed,0failed,0ignored.x86_64non-rootselenerelease tests on host NVMe (uid=1000,rustc 1.94.1):cargo test --locked --release --manifest-path selene/Cargo.toml --features test-support: 591 passed (590lib +1integration),0failed,0ignored (both Unix0o000unreadable-entry tests and the live-target Unix symlink check executed without skipping).cargo test --locked --release --manifest-path selene/Cargo.toml --no-default-features --lib: 566 passed,0failed,0ignored.--releasecrate suites (x86_64Linux non-rootuid=1000,rustc 1.100.0,3,907Rust +4Python passed,0failed,94ignored):cargo test --locked --release --manifest-path selene/Cargo.toml --features test-support: 591 passed (590lib +1integration),0failed,0ignored.cargo test --locked --release --manifest-path selene/Cargo.toml --no-default-features --lib: 566 passed,0failed,0ignored.FLARE_TEST_TEMPLATE_DIR=<dir> cargo test --locked --release --manifest-path helios/Cargo.toml --features runtime-core,server(templates populated viatests/frontend/prepare_templates.py --output <dir>): 1,976 passed (1,914lib +62integration),0failed,86ignored (49lib +37integration).cargo test --locked --release --manifest-path flare/Cargo.toml --lib: 247 passed,0failed,4ignored.cargo test --locked --release --manifest-path flare-cli/Cargo.toml --bins: 224 passed,0failed,3ignored.cargo test --locked --release --manifest-path selene-manager/Cargo.toml --lib: 205 passed,0failed,1ignored.cargo test --locked --release --manifest-path selene-manager/Cargo.toml --test packed --test client: 98 passed (86client+12packed),0failed,0ignored.pytest tests/preparation/test_core_boundary.py: 4 passed,0failed.read_metadata_uses_single_read_for_v3_and_small_v4_and_handles_large_v4(including 37-byte short-read coverage on both v3 and v4 plus one-shotEINTRretry),parallel_scan_handles_large_mixed_v3_v4_directory_deterministically(preserving foreign files, subdirectories, dangling symlinks, and live Unix symlinks pointing to existing regular files),parallel_scan_preserves_directory_on_unreadable_entry,parallel_scan_recovers_large_directory_deterministically_under_tight_budget, andparallel_scan_preserves_directory_and_budget_on_unreadable_entry(>= 64valid files + corrupt.input+.input.pending+0o000unreadable.inputunder tightDiskBudget).selene/examples/scan_bench.rsinside clean archive checkouts of control (22d2ac97) and candidate (015076ec), build both withcargo build --locked --release --manifest-path selene/Cargo.toml --example scan_bench, remove the example file, prepare datasets with<bin> prep-ckpt <dir> <500|2000|10000>and<bin> prep-input <dir> 1000, and time interleavedA-B-B-Ainvocations of<bin> scan-ckpt <dir> <N>and<bin> scan-input <dir> 1000(runningsync && echo 3 > /proc/sys/vm/drop_cachesbefore each cold run).Microbenchmark reproduction harness (against public flare_selene APIs)
record::scanandInputStore::start_boundedcalls rather than end-to-endflare runstartup.Discord: Dooner