Repository navigation
perf(scan): parse manifests concurrently - #3670
Conversation
bdf262b to
b90f908
Compare
TableScan::plan_files loads manifests from one task spawned on the io runtime, and ManifestReader::read parsed each manifest on that task, so the Avro decoding of every manifest in a snapshot ran one after another while the other workers idled. Planning a 601-manifest table spent about 80% of its time in Manifest::try_from_avro_bytes on a single core. ManifestReader now takes an optional Runtime and runs the parse on its cpu handle, which is what Runtime::cpu is documented for; the fetch stays on the calling io task. The scan passes the table's runtime through ObjectCache::get_manifest, so up to concurrency_limit_manifest_files parses run in parallel. ObjectCache itself stores no runtime, so Table::with_runtime keeps sharing it. Spawning the whole per-manifest future on io() would parallelise but keep CPU work on the io runtime; spawning it on cpu() would run storage IO on a runtime that an engine using iceberg-rust for planning only may build without an IO driver. spawn rather than spawn_blocking keeps the parse within the worker count the embedding engine sized cpu() with, matching how the entry stages in plan_files already spawn their work. A panic or cancellation in a parse surfaces as ErrorKind::Unexpected through JoinHandle, and try_for_each_concurrent stops starting new manifests on the first error, so the plan stream ends with that error exactly as it does for a read error. plan_bench medians (4 workers): SF10 23.9 -> 7.0 ms, SF100 232.9 -> 65.4 ms. (cherry picked from commit ce885c9)
…fest on JoinError ManifestReader took an Option<Runtime>, which left two parse paths: the spawned one the scan uses and an inline one that catalog::utils' delete_data_files and several tests still took, so manifests read while dropping a table were parsed serially. The runtime is now a constructor argument, with_runtime is gone, and delete_data_files builds its reader from the table. The inline-vs-spawned equivalence test goes with the inline path it compared against. A JoinError from the spawned parse (a panic, or a cpu runtime shut down mid-plan) carried only "spawned task failed"; a parse error already names the manifest. The reader now attaches the manifest path as context, so every failure out of ManifestReader::read names the file. ObjectCache wraps the error with the path too, which is why the scan-level cancellation test passed without this; a reader-level test fails without it. The scan-level test also asserts the path now, matching its corrupt and missing siblings.
…ing it per call ObjectCache::get_manifest took a &Runtime on every call, which threaded a runtime through PlanContext, ManifestFileContext and validate_files_not_live only to hand it to the ManifestReader it builds. The cache now owns the runtime it parses on, taken at construction from the TableBuilder, and get_manifest has its original signature again. Table::with_runtime rebinds the cache through ObjectCache::with_runtime, which clones the moka cache: a moka Cache is a handle to shared storage, so the rebound table keeps every entry the original loaded while its misses parse on the new runtime. The with_runtime test now checks both, by building the table on a runtime it then shuts down: a cache that kept the old runtime fails its first miss, and a cache that did not share storage cannot answer for a manifest list whose file has been removed.
b90f908 to
780a428
Compare
JeonDaehong
left a comment
There was a problem hiding this comment.
Measured plan_files on main and on 780a428, alternating 3 rounds, 4 workers, local files, memory catalog, fresh Table per run so every manifest is parsed:
- 600 manifests × 100 entries: 346–430 → 179–207 ms
- 300 manifests × 2000 entries: 2302–2447 → 1550–1750 ms
So close to 2x when manifests are small, less when each holds many entries. LGTM.
| let (metadata, mut entries) = self | ||
| .runtime | ||
| .cpu() | ||
| .spawn(async move { Manifest::try_from_avro_bytes(&bytes, Some(&location)) }) |
There was a problem hiding this comment.
Only the parse moves here, and the gain shrinks as manifests get larger (numbers in the review body), so the per-entry work after the parse is probably what's left on the calling task.
| .await | ||
| // A `JoinError` (the parse panicked, or the cpu runtime shut down) | ||
| // names no manifest; a parse error already does. | ||
| .map_err(|err| err.with_context("manifest", &manifest_file.manifest_path))??; |
There was a problem hiding this comment.
A panicking parse used to unwind through the planning task and now comes back as Unexpected with the manifest path. That seems like the better behavior, and test_read_names_manifest_when_parse_is_cancelled covers the same JoinError path.
laskoviymishka
left a comment
There was a problem hiding this comment.
Thanks @CTTY for keeping the storage fetch off cpu() and for the reader-level cancellation test. As I read it, only the Avro parse moves, the public API is unchanged, and the shared-runtime case can't deadlock because plan_files already awaits cpu() spawns the same way. The two inline notes are about doc wording.
Table::manifest_reader and ManifestReader::read now say the parse runs on the cpu runtime and fails with ErrorKind::Unexpected once that runtime has shut down. The scan-level cancellation test credits ObjectCache's wrapper for the manifest path, and points to the reader-level test that pins the reader's own context.
Which issue does this PR close?
What changes are included in this PR?
plan_filesfetched manifests concurrently but parsed them one after another on the single task that drives the fetches. On a 601-manifest table about 80% of planning time wasManifest::try_from_avro_byteson one core while the other workers idled.ManifestReadernow takes the table'sRuntimeand runstry_from_avro_bytesonruntime.cpu(); the fetch stays on the calling task.ObjectCache::get_manifestpasses the runtime through, so up toconcurrency_limit_manifest_filesparses run in parallel. The transaction and drop-table paths go through the same reader and pick this up too.Error handling is unchanged from the caller's view: a parse that panics or is cancelled surfaces as
ErrorKind::Unexpectedwith the manifest path, and the plan stream ends with that error exactly as it does for a read error.Two choices worth a reviewer's eye:
cpu().spawnrather thanspawn_blocking, so parses stay within the worker count the embedding engine sizedcpu()with. With a single shared runtime (Runtime::new/try_current), parses now share workers with IO; this matches what the entry stages inplan_filesalready do.cpu()would run storage IO on a runtime an engine may build without an IO driver.No public API change (
make check-public-apiis clean); new items arepub(crate).Measurements
plan_filesmedian, 4-worker runtime, TPC-Hlineitemwritten by pyiceberg, local files:Are these changes tested?
Existing scan, object-cache and manifest tests cover the happy path. New tests:
scan::tests::test_plan_files_reports_{corrupt,missing}_manifest_as_error: a bad manifest fails the plan stream withErrorKind::Unexpectedand the manifest path, under a timeout so a swallowed error shows up as a failure rather than a hang.scan::tests::test_plan_files_reports_cancelled_manifest_parse_as_error: a parse spawned on a shut-down cpu runtime fails the same way, exercising theJoinErrorpath a panic would take.spec::manifest::reader::tests::test_read_names_manifest_when_parse_is_cancelled: the reader itself attaches the manifest path to aJoinError, which carries none on its own.cargo test -p iceberg --lib,cargo clippy --all-targets --all-features -D warnings,cargo fmt --checkandmake check-public-apiare clean.AI Disclosure
Drafted with AI assistance (Kiro), then reviewed, tested and benchmarked by me.