Repository navigation
perf(iceberg): a Parquet page header is read from the window already fetched - #2365
Conversation
…fetched Parquet's page reader calls `ChunkReader::get_read` once per page to read the page header, then reads the body through `get_bytes`. `RangeBackedChunkReader::get_read` fetched `min(8 MiB, file_size - start)` on every call and took a cached window only when it held all 8 MiB past `start`. A page at least a few hundred KiB past the start of the last window therefore fetched a fresh 8 MiB, so a page-by-page read of a column chunk moved about 8 MiB per page from storage. On a file of 1 MiB pages that is about 8x the column's bytes, one GET per page. `get_read` now serves the newest cached window that covers `start` and holds at least 1 MiB past it, or runs to the end of the file. Only when none does is a fresh 8 MiB window fetched. A page-by-page read now costs about one fetch per 7 MiB of column. The reader `get_read` returns also runs to the end of the file, as the trait describes, fetching past its window only if read that far, so a header longer than a window's remainder is still read whole (the old 8 MiB cursor stopped short). Tests count the ranges a mock store is asked for: pages served from one window, a fresh window near a window's end, a window that reaches the end of the file, a read past the first window, and an Arrow decode of a 19 MB file of 64 KiB pages, which took 43 fetches and 363 MB before and 5 fetches and 29 MB now, with the same rows. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The reader `get_read` returns ran on to the end of the file. No valid file needs more than the old cursor's 8 MiB from the offset: a served window has at least 1 MiB left past it and a fresh one has 8 MiB. A corrupt page header that declares a long field, though, has parquet copy that length through the reader, and it would have pulled uncached 8 MiB windows to the end of the file before the header was refused. The reader now stops at `start + GET_READ_WINDOW`, as the old cursor did, and still fetches what a served window lacks of that reach, in one range. An offset past the end of the file is now an EOF error, as parquet's `Bytes` reader gives, rather than a fetch of an inverted range or a slice past the end of the fetched bytes. Tests: a `get_read` reader, served or fresh, returns exactly 8 MiB from its offset, the served one with one fetch for what its window lacked; an offset past the end is an EOF error with no fetch; ten 1.75 MiB pages, two of whose bodies run past their window's edge, take exactly four fetches (ten 8 MiB fetches before); and the Arrow end-to-end test now pins its five fetches. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ndow branch No textual conflicts. #2315 gave `decode_batches_arrow` a `row_group_subset` argument, so the ranged-vs-in-memory decode test in `chunk_reader.rs` passes `None` (the whole file) for it in both calls. Co-Authored-By: Claude Opus 5.5 <[email protected]>
bplatz
left a comment
There was a problem hiding this comment.
Approving. The fix checks out against parquet 58's page reader, and the fetch-count tests pin it well. A few small comments inline, none blocking.
One follow-up I couldn't anchor inline: the row-group-parallel path in decode_large_file (send_parquet.rs) builds a separate RangeBackedChunkReader per task, plus one for the footer, each with its own cache. A task's last window reads up to 8 MiB into the next task's row groups, and worst-case memory is tasks × ~528 MiB. Not from this PR (which makes the cache fill much more slowly); a cache shared across the tasks would probably cover both.
| /// about one fetch per window. Requiring a cached window to hold a whole | ||
| /// `GET_READ_WINDOW` past the offset instead meant a fresh 8 MiB fetch for | ||
| /// nearly every page: 8 MiB moved per 1 MiB page. |
There was a problem hiding this comment.
Nit: this sentence describes the old behaviour. It's already in the PR body and goes stale once this merges, so I'd drop it. Same for the "before" figures in the test docs (L647–648, L847–848, L888–889).
There was a problem hiding this comment.
Agreed, dropped in 612e1f6. Along with the get_read sentence and the three "before" figures in the test docs, I also took out the other comparisons to the reader this replaces that I found on the same pass (the module doc's and RangeRead's "as the old 8 MiB cursor did", and the same phrase in get_read_reads_up_to_its_reach_and_no_further's doc), since they'd go stale the same way. The history stays in the PR description.
| check.bytes_fetched(), | ||
| check.fetches().len() | ||
| ); | ||
| // The footer, then a window per ~7 MiB of the two column chunks. |
There was a problem hiding this comment.
1.2M rows is over parquet's default limit of 1,048,576 rows per row group, so this file has two row groups (four column chunks), not the two chunks this comment describes. The == 5 still holds, but it depends on that default. Suggest setting .set_max_row_group_row_count(Some(..)) in the writer props and updating the comment.
There was a problem hiding this comment.
You're right, thanks for catching this. Parquet 58.4's DEFAULT_MAX_ROW_GROUP_ROW_COUNT is 1,048,576, so the file had two row groups and the five-fetch count only held by way of that default. 3f7ed50 sets .set_max_row_group_row_count(Some(ROWS as usize)) and rewords the comment, and the test now also asserts num_row_groups() == 1, so the comment can't drift from the file again (I checked that dropping the setter fails it with left: 2). With one row group it's still five fetches.
| /// remainder still reads whole, while a corrupt header that declares a longer | ||
| /// field than that runs into the end of the reader, as it did before, instead | ||
| /// of pulling the rest of the file through it. | ||
| pub struct RangeRead<S: SendIcebergStorage + 'static> { |
There was a problem hiding this comment.
Nit: RangeRead is a new public type without Debug. RangeBackedChunkReader has a hand-written one, so one here would match.
There was a problem hiding this comment.
Added in b8f8bef, matching RangeBackedChunkReader's hand-written one: path, bytes still buffered, next and limit, with finish_non_exhaustive() for the storage and runtime it leaves out. One small knock-on: with Debug in place, clippy's err_expect started flagging the EOF test's .err().expect(..) (it only fires once T: Debug), so that's expect_err now, in the same commit.
| const GET_READ_WINDOW: u64 = 8 * 1024 * 1024; | ||
|
|
||
| /// The least a cached window must hold past a `get_read` offset to serve it, | ||
| /// unless it runs to the end of the file. A page header needs a few bytes; the | ||
| /// rest is room for the page bodies that follow, so a window this close to its | ||
| /// end is passed over for a fresh one rather than served and soon missed. | ||
| const GET_READ_MIN_TAIL: u64 = 1024 * 1024; |
There was a problem hiding this comment.
Follow-up idea, not for this PR: a window doesn't know where column chunks end. A narrow projection over a wide file still pulls 8 MiB for each projected chunk smaller than that, and a chunk whose last pages fall in a window's last MiB triggers a fresh 8 MiB fetch. Prefetching each row group's projected chunk ranges, as parquet's async reader does, would fix that. The min-tail rule would need another look then, since a chunk's end isn't EOF.
There was a problem hiding this comment.
Agreed, @bplatz, I think that's the right shape. A window has no idea where a column chunk ends, so a narrow projection over a wide file pays 8 MiB for each small projected chunk, and a chunk whose last pages land in a window's final MiB buys a whole fresh window just for them. Planning each row group's fetches from the footer's projected chunk ranges, the way parquet's async reader does, would fix both.
I'm not planning it in this PR, though. It's a change to the fetch planner rather than to how a page header is read, and it brings its own question about the min-tail rule: once a chunk's end, not EOF, bounds a fetch, that rule is answering a different question and probably wants re-deriving rather than carrying over. It seems worth doing if remote Iceberg scans stay a hot path, and I'd rather it get its own before/after numbers than ride along here.
…ot what it replaced
The get_read doc, RangeRead's doc, the module doc and the fetch-count
test docs compared against the reader this PR replaced ("the old 8 MiB
cursor", "before" figures). The PR description carries that history;
in the code it goes stale once the change lands.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
RangeRead is public and RangeBackedChunkReader already has a hand-written Debug; this one matches it, showing the path, the bytes still buffered, and the reader's next offset and limit. With Debug in place the EOF test uses expect_err, which clippy's err_expect now asks for. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Its 1.2M rows ran past parquet's default of 1,048,576 rows per row group, so the file had two row groups (four column chunks), not the two column chunks its comment describes; the five-fetch count held, but only by way of that default. The writer now sets max_row_group_row_count, and the test asserts the file has one row group. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Partially addresses #2363: this fixes the first of its three items, the 8 MiB fetch per Parquet page header. #2366 takes the third (file reads that outlive their scan), and the second (a LIMIT bounding how much of each file a scan reads) stays open on the issue.
RangeBackedChunkReaderreads every Iceberg data file over 64 MiB, and parquet's page reader calls itsget_read(start)once per page, just to read the page header (the body then comes throughget_bytes).get_readfetchedmin(8 MiB, file_size - start)every time and only took a cached window when that window held all 8 MiB paststart. So once the reader was a couple hundred KiB into a window, every page header fetched a fresh 8 MiB, and a column chunk of 1 MiB pages moved about 8 MiB per page from storage. Claude found this in the synthetic repro described on the issue; I've traced it againstSerializedPageReader::get_next_pagein parquet 58.What changes
All of it is in
fluree-db-iceberg/src/io/chunk_reader.rs.get_readis now served from the newest cached window that coversstartand still holds at least 1 MiB past it (GET_READ_MIN_TAIL), or that runs to the end of the file. Only when none does is a fresh 8 MiB window (GET_READ_WINDOW) fetched. The 1 MiB isn't for the header itself (a few bytes, a few KiB with statistics); it's room for the page bodies that follow, so we don't serve a header from a window we'd miss on right afterwards. A page-by-page read now costs about one fetch per 7 MiB of column.get_readreturns (RangeRead, replacingCursor<Bytes>) reads up to 8 MiB from its offset, the same reach the old cursor had, and fetches whatever part of that a served window lacks only if it's read that far. With a 1 MiB minimum tail I didn't want a page header longer than a window's remainder to turn into a decode error where it used to be fine. That continuation is one uncached range, and no valid file should reach it.mainit hit the end of the 8 MiB cursor and errored.1d3b1381cputs the 8 MiB bound back.Bytesreader gives, rather than a fetch of an inverted range (or, onmain, a wrapped subtraction).fetch_rangepulls the sync-to-async storage read out offetch_bytes, so both paths share it.Query results don't change, only how many bytes the reader asks storage for. Memory per open file doesn't change either: the cache still holds up to 64 windows of up to 8 MiB plus alignment (about 528 MiB), but those windows are now mostly distinct and fill about 8x more slowly, where on
maina page-by-page read filled it with overlapping copies.A page longer than what its header's window has left (Iceberg's default page size is 1 MiB, and real pages vary) still costs one extra GET at that window's edge: the body is fetched on its own, and the next header then starts a fresh window that re-reads the overlap. It's pinned in a test below. A 2 MiB
GET_READ_MIN_TAILwould make that rarer at the cost of starting fresh windows sooner, and I don't think either trade is clearly better without real page-size data, so I've left it at 1 MiB.Measured
Same box, same synthetic tables,
main(467f0da4d) against this branch ata6fc12339(1d3b1381conly changes what a reader does past a served window or past the end of the file, which no valid file reaches, so I didn't re-run the box for it): LocalStack as S3 with 20 ms injected on its egress, the server (dev-fast, default features) pinned to 16 vCPUs on a c7i.8xlarge, a fresh server and cache per build. Each cell is cold / warm (the same query run twice).LIMIT 5, 8 files of ~136 MB (int64 keys)mainLIMIT 5, 8 files of ~166 MB (string keys, 45 columns)mainSUMover one int32 column of the first tablemainLIMIT 5over 576 files of ~12 MB (under 64 MiB, so not this reader)mainmainrun again at the end as a drift check: 21.7 / 21.9 s on the first query, and theSUMat 63.2 / 61.1 s with 259 / 260 CPU-s. So theSUM's CPU swings quite a bit run to run (it's aggregation-bound once the bytes are in), and I'd read its wall-time win as roughly 15-30 s rather than the exact figure; its bytes and GETs are exact. The 576-file row moves fewer bytes inside the query window only because more of its abandoned reads land after the response (#2363's third item, not this PR).Results: the
SUMand aCOUNTcontrol are identical on every build. The unorderedLIMIT 5answers vary from run to run onmainas well (which file finishes first), so for those I checked that every row returned, in every run on every build, is a row of its table.Tests
The new unit tests count the ranges a mock store is asked for:
page_by_page_read_fetches_each_window_once: 16 MiB of 64 KiB pages, read header-then-body, take 3 fetches (64 fetches and about 520 MiB before).a_page_longer_than_its_windows_remainder_costs_one_extra_fetch: ten 1.75 MiB pages, two of whose bodies run past their window's edge, take exactly four fetches: a window, that body, a fresh window, the next such body (ten 8 MiB fetches before).get_read_near_a_window_end_fetches_a_fresh_window: a window with exactly 1 MiB left serves; 1 byte further, a fresh window is fetched from the offset.get_read_serves_a_window_that_runs_to_eof: a window that reaches the end of the file serves any offset in it, including the end itself.get_read_reads_up_to_its_reach_and_no_further: aget_readreader returns exactly 8 MiB from its offset whether it was served (with one fetch for what its window lacked) or fresh.get_read_past_the_end_of_the_file_is_an_eof_error: an EOF error, and no fetch.small_page_parquet_decodes_with_about_one_pass_over_the_file: an Arrow decode of a 19 MB file of 64 KiB pages, in one row group, through the range-backed reader returns the same rows as decoding it from memory, in exactly 5 fetches and under 2x the file's bytes (29 MB; 43 fetches and 363 MB before).With
get_readput back the way it was, the fetch-count tests fail; the reach test fails with the reader left uncapped, and the EOF test with the past-the-end check taken out. The end-of-file one passes either way: it pins the end-of-file exception to the 1 MiB rule rather than a regression.Verification
On a c7i.8xlarge, at
a6fc12339:cargo fmt --all -- --check,cargo clippy --all --all-features --all-targets --locked -- -D warningsandcargo check --workspace --all-targets --locked(CI's flags): clean.cargo test -p fluree-db-iceberg --all-features: 510 passed, 0 failed (9 ignored).cargo test -p fluree-db-api --all-features: 99 test binaries, 5,687 passed, 0 failed (137 ignored).At
1d3b1381c, which only toucheschunk_reader.rs:cargo fmt --all -- --check,cargo clippy -p fluree-db-iceberg --all-features --all-targets --locked -- -D warnings, andcargo test -p fluree-db-iceberg --features aws(512 passed, 0 failed), plus CI.After #2315 landed, the branch merges
main(d56ed5467) rather than being rebased. Nothing conflicted in git, but #2315 gavedecode_batches_arrowarow_group_subsetargument, sosmall_page_parquet_decodes_with_about_one_pass_over_the_filenow passesNone(the whole file) for it in both of its decodes; without that the test module didn't compile on the merge. On a Mac atd56ed5467:cargo fmt --all -- --checkandcargo clippy -p fluree-db-iceberg --all-targets --all-features -- -D warningsare clean, andcargo test -p fluree-db-iceberg --all-featurespasses 533, fails 0 (9 ignored); CI covers the rest.testsuite-sparql/isn't touched.One thing worth flagging for embedders:
RangeBackedChunkReaderis public, so itsChunkReader::Tchanging fromCursor<Bytes>toRangeRead<S>is visible. I'd be surprised if anything names that associated type, but it's there.After review
Brian's notes are in
612e1f698(the docs say what the window does rather than what it replaced),b8f8bef08(DebugforRangeRead, andexpect_errin the EOF test now that it can be) and3f7ed50e7(small_page_parquet_decodes_with_about_one_pass_over_the_filewrites one row group, and asserts it, rather than relying on parquet's 1,048,576-row default; still 5 fetches), on top of a merge ofmain(f5056f289). At3f7ed50e7, on a Mac:cargo fmt -p fluree-db-iceberg -- --checkandcargo clippy -p fluree-db-iceberg --features aws --all-targets -- -D warningsare clean and the 11chunk_readertests pass; CI is green.🤖 Generated with Claude Code