Repository navigation
perf(scalar): build one decode plan per index scan, not one per batch - #36
rerun-rmack wants to merge 7 commits into
Conversation
52a2d77 to
e478c7e
Compare
|
My main concern was switching to entire file reader, hence probed Claude for the risk. It seems there is one. I'm also wondering given that Lance team is quite responsive to issues, if we should (in parallel) open an issue describing the findings and the proposed fix, to see if there's more things we're missing. Proposed fix (sketch): |
|
Addressed some of the review feedback in a new commit (clamp batch_size, keep shared-store merges paced) and separate PR #37 for backpressure accounting (leak is real, but it's capped at the io_buffer_size and the store/scheduler are per-operation so it doesn't persist.) My Claude says some things we don't need to worry about:
Recommended next steps:
|
|
Pushed a commit that bounds this. Two bits of your feedback I did not take, and one thing you should know before trusting the numbers. Capping the number of concurrently-open segment streams. A k-way sort-preserving merge needs a live cursor on every input, and nesting the merge nodes doesn't reduce leaf concurrency, so bounding it properly means multi-pass merging with materialized intermediates (an extra full write and read per level). It also wouldn't help on its own, since the prefetch starts when the streams are constructed. Splitting the budget gets the same memory ceiling for none of that. Windowing instead of whole-file. The premise checks out ( What isn't measured. The A/B runs don't cover your scenario. Every index in that fixture had exactly one segment, so N was 1 throughout and the multiplicative term never ran. There's also no in-flight-bytes instrumentation in those runs, and the fixture is local disk (256 MiB budget) rather than object storage (2 GiB). The new bound is reasoned, not measured, and the underlying budget is soft in any case (a request is admitted regardless of remaining bytes when nothing of lower priority is in flight). Both caveats are in the doc comments. Separately, on #37: it turns out upstream already fixed that in lance-format#7638 back in July and just never backported it to v8.0. So it's a backport rather than a new finding, and release-9.0.0 already has it (plus the lite scheduler half, which ours doesn't do). Nothing to send upstream. |
This is my remaining concern - we already tuned this knob for the use of the Scanner i.e. the query path. In other words - it's still hard for me to reason how this PR will impact a stack under load, will the query path be impacted, will the memory blow up, etc. Not sure what's the best way to test this. My humble honest take is - let's see if we can merge deferral and if it's good enough. Cut an issue to Lance team today and explain what we're facing and share this proposed fix ( + the concerns) and let's see what they say. If you think I'm being overly paranoid on this one, let me know. We've just been bitten by these knobs and OOMs to many times that I'm concerned without a proper exhaustive stack test, this is a risk. |
|
I'm still not sure I share your concern with memory issues. This reduced
compaction from needing 250GB of memory (and OOMing) down to 60GB and
succeeding in 1/20th the time. That seems like a useful optimization.
…On Tue, Aug 18, 2026 at 3:43 AM Zeljko Mihaljcic ***@***.***> wrote:
*zehiko* left a comment (rerun-io/lance#36)
<#36 (comment)>
Also made LanceIndexStore honour LANCE_DEFAULT_IO_BUFFER_SIZE, which turns
out to already exist and to already be documented as applying to this exact
constructor, index stores just never consulted it. So there is a knob now.
This is my remaining concern - we already tuned this knob for the use of
the Scanner i.e. the query path. In other words - it's still hard for me to
reason how this PR will impact a stack under load, will the query path be
impacted, will the memory blow up, etc. Not sure what's the best way to
test this.
My humble honest take is - let's see if we can merge deferral and if it's
good enough. Cut an issue to Lance team today and explain what we're facing
and share this proposed fix ( + the concerns) and let's see what they say.
If you think I'm being overly paranoid on this one, let me know. We've
just been bitten by these knobs and OOMs to many times that I'm concerned
without a proper exhaustive stack test, this is a risk.
—
Reply to this email directly, view it on GitHub
<#36?email_source=notifications&email_token=B4VTWGEHLO6QRTN5JIPPZCL5KQCJNA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMZSGUYTIMJYHAY2M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#issuecomment-5325141881>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/B4VTWGGE7APQXJQGFMTR4LL5KQCJNAVCNFSNUABFKJSXA33TNF2G64TZHM4DSNBRHE4DOMRQHNEXG43VMU5TKMJWGU3DKNBZHAYKC5QC>
.
Triage notifications, keep track of coding agent tasks and review pull
requests on the go with GitHub Mobile for iOS
<https://github.com/notifications/mobile/ios/B4VTWGASGGCRTZBA7GZOUFL5KQCJNA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMZSGUYTIMJYHAY2M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KUZTPN52GK4S7NFXXG>
and Android
<https://github.com/notifications/mobile/android/B4VTWGHJQY6OIJWVQUHHIED5KQCJNA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMZSGUYTIMJYHAY2M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2K4ZTPN52GK4S7MFXGI4TPNFSA>.
Download it today!
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
|
Anyhow, I'll have Claude try another iteration on these and we can keep it
parked for now. If we end up in a situation where maintenance is taking
hours again we can temporarily deploy a build with this to get us caught up.
…On Tue, Aug 18, 2026 at 5:28 AM Ryan Mack ***@***.***> wrote:
I'm still not sure I share your concern with memory issues. This reduced
compaction from needing 250GB of memory (and OOMing) down to 60GB and
succeeding in 1/20th the time. That seems like a useful optimization.
On Tue, Aug 18, 2026 at 3:43 AM Zeljko Mihaljcic ***@***.***>
wrote:
> *zehiko* left a comment (rerun-io/lance#36)
> <#36 (comment)>
>
> Also made LanceIndexStore honour LANCE_DEFAULT_IO_BUFFER_SIZE, which
> turns out to already exist and to already be documented as applying to this
> exact constructor, index stores just never consulted it. So there is a knob
> now.
>
> This is my remaining concern - we already tuned this knob for the use of
> the Scanner i.e. the query path. In other words - it's still hard for me to
> reason how this PR will impact a stack under load, will the query path be
> impacted, will the memory blow up, etc. Not sure what's the best way to
> test this.
>
> My humble honest take is - let's see if we can merge deferral and if it's
> good enough. Cut an issue to Lance team today and explain what we're facing
> and share this proposed fix ( + the concerns) and let's see what they say.
>
> If you think I'm being overly paranoid on this one, let me know. We've
> just been bitten by these knobs and OOMs to many times that I'm concerned
> without a proper exhaustive stack test, this is a risk.
>
> —
> Reply to this email directly, view it on GitHub
> <#36?email_source=notifications&email_token=B4VTWGEHLO6QRTN5JIPPZCL5KQCJNA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMZSGUYTIMJYHAY2M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#issuecomment-5325141881>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/B4VTWGGE7APQXJQGFMTR4LL5KQCJNAVCNFSNUABFKJSXA33TNF2G64TZHM4DSNBRHE4DOMRQHNEXG43VMU5TKMJWGU3DKNBZHAYKC5QC>
> .
> Triage notifications, keep track of coding agent tasks and review pull
> requests on the go with GitHub Mobile for iOS
> <https://github.com/notifications/mobile/ios/B4VTWGASGGCRTZBA7GZOUFL5KQCJNA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMZSGUYTIMJYHAY2M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KUZTPN52GK4S7NFXXG>
> and Android
> <https://github.com/notifications/mobile/android/B4VTWGHJQY6OIJWVQUHHIED5KQCJNA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKMZSGUYTIMJYHAY2M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2K4ZTPN52GK4S7MFXGI4TPNFSA>.
> Download it today!
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>
|
|
You're right, and it's worse than you framed it. Pushed 6bd6c56 backing it out.
And the direction was backwards. We set that var to Lance's own default (2 GiB) rather than leaving it unset, so instead of letting us shrink the budget it would have raised every index store from 32 MiB x LANCE_IO_THREADS (256 MiB for us) to 2 GiB, query path included. Latent in practice, since a query-path index open does point reads and won't fill 256 MiB let alone 2 GiB, but that's a reasoning argument and your objection is precisely that you shouldn't have to take one on faith. Worth noting the knob you're thinking of is a different one. The scanner is tuned by Now back to Also retracting something I said earlier in this thread: I claimed the index store's scheduler "is never shared with a live search". Per-construction is right, but the scanner builds these stores too, so that was too strong. Happy to file the upstream issue today either way. Agreed it's worth their read before we carry this. |
Scanning a scalar index end to end went through `IndexReaderStream`, which issues an independent `read_record_batch` per 4096-row batch. Each of those builds a fresh `DecodeBatchScheduler`, and `DecodeBatchScheduler::try_new` runs `initialize()` — which for the structural encoding initializes *every page in the column*, with no reference to the rows being requested. So a full scan costs O(batches x pages) metadata reads instead of O(pages). Each read is one small chunk-metadata buffer, and consecutive pages' buffers sit megabytes apart, so the scheduler's coalescer (gap tolerance = block_size, 4 KiB on local FS) cannot merge them: every one becomes its own pread. Adds `IndexReader::whole_file_stream`, which returns a single-decode-plan stream or `None`, and takes it in the btree scan paths. Readers without a fast path keep the old per-batch behaviour via the default. Measured on the new test's fixture (10k rows, 157 batches): 315 read IOPs before, 4 after. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Three changes from review feedback. 1. `batch_size` is read from the index file's own metadata, so a `u64` that is a multiple of 2^32 truncated to a `rows_per_batch` of 0 in the `as u32` cast — an empty stream that silently retrained an empty index and reported success. Clamp instead. Regression test verified to fail without the clamp. 2. `merge_range_partitioned_lookups` and `merge_pages` build one stream per partition against a *shared* store, and construct all of them before the consumer polls any. A whole-file plan per partition would let partitions the consumer is not draining occupy the scheduler's byte budget — credited back only when a batch is polled — and stall the partition being drained. Both keep the demand-paced `buffered(1)` path; only the three single-stream sites use `stream_index_file`. 3. The IOP test used 10,000 rows, below `DEFAULT_INLINE_SCHEDULING_THRESHOLD` (16 Ki), so it exercised inline scheduling rather than the spawned path this change actually affects. Raised to 40,000 rows. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Now that `data_stream()` is a whole-file plan, what it keeps in flight is capped by the store's scheduler budget rather than by readahead. `merge_segments` opens a store per source segment and starts a stream on each, so the merge was sitting on N of those budgets at once (and none of it is visible to the DataFusion memory pool). Adds `IndexStore::with_io_buffer_size` (default no-op) and has `merge_segments` split one scan's budget across the segments it actually reads. The rescope happens before the stream is awaited, because that is where prefetch starts, not at `execute_plan`. Two limits, documented rather than left to be discovered. The 32 MiB floor keeps a whole page in flight per source, so the aggregate is max(one scan's budget, sources * 32 MiB) and not a constant (past `io_parallelism` sources the floor wins and it grows again). And the budget is soft: a request is admitted regardless of remaining bytes when nothing of lower priority is in flight, which is what keeps things moving at any budget. Also has `LanceIndexStore` honour `LANCE_DEFAULT_IO_BUFFER_SIZE`. Its doc comment asks paths with a computed default to consult it, but index stores never did, so `LANCE_IO_THREADS` was the only way to shrink the budget and that changes concurrency too. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Yesterday's commit had `LanceIndexStore::with_format_version` consult `LANCE_DEFAULT_IO_BUFFER_SIZE`. That constructor is not maintenance-only: `open_scalar_index` builds one per scalar index a scan opens, so this put a knob tuned for one workload in charge of the other. It also went the wrong way in practice. We set that var to Lance's own default (2 GiB) rather than leaving it unset, so instead of letting a deployment shrink the budget it raised every index store from 32 MiB x LANCE_IO_THREADS (256 MiB for us) to 2 GiB, query path included. Back to `max_bandwidth`, with a comment saying why. The merge-site bound stays: `merge_segments` still splits one scan's budget across its sources through `with_io_buffer_size`, which is explicit and scoped to the caller that needs it. If we do want a knob later it should be its own index-store variable, not one the scanner already owns. Side effect: `merge_io_buffer_size` no longer reads the environment, so its test is deterministic regardless of how the shell is configured. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
6bd6c56 to
c65cdb1
Compare
The review objection to this PR is that a whole-file plan's outstanding bytes are bounded only by the scheduler budget, and that nothing reports that number. The second half was true: `IoQueueState` tracks `bytes_avail` but exposes nothing, so every measurement so far has been whole-process RSS, which cannot separate scheduler prefetch from decode buffers from the index merge's own sort. Adds a high-water mark of bytes debited and not yet credited, plus a count of requests admitted through the `priority <= min_in_flight` path despite the budget being exhausted. That second number matters: it is exactly where the budget is soft rather than hard, and until now the only evidence it happens at all was a debug log. Surfaced through `ScanScheduler::backpressure_stats()` alongside the existing `stats()`, and `LanceIndexStore::scheduler()` so a caller can reach it. The lite scheduler reports zeroes; it accounts for reservations with its own RAII type rather than this queue. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Three tests, one per objection raised in review. The budget is respected. A whole-file scan of a 40k-row index is run at a 1 MiB and a 32 MiB budget and must hold no more than budget + max_iop_size in flight. The slack is deliberate: the budget is soft, because `can_deliver` admits a request whose priority is at or below everything in flight even when the budget is exhausted, and that is what guarantees forward progress. Measured on this fixture: 480 KB in flight against 1 MiB, zero bypass admissions. The budget survives an abandoned scan. A whole-file plan has far more outstanding when dropped than a 4096-row plan did, so a leak in the credit path is proportionally worse here. The scan is dropped after 1, 32 and 256 batches and a subsequent full scan on the same store must complete within 30 s. The query path cannot reach the whole-file scan. The index is loaded through a store whose readers panic if asked for a whole-file stream, then searched with equality, range and IS NULL. This is a structural assertion, not an inspection: if someone later routes a search through a whole-file plan it fails here. Each test carries its own positive control, because all three would otherwise pass vacuously. The budget test asserts the gauge actually moved; the query-path test first asserts that a maintenance scan through the same store *does* trip the guard, proving the guard is consulted at all. Both controls were checked to fire before being folded in. Not covered: the drop test cannot be shown to fail without reverting the credit-on-drop fix it guards, so treat it as a regression guard rather than a demonstration. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Every earlier measurement ran with a single source, so the budget split in merge_segments had never executed with the multiplier it exists for. This test runs merges of 2, 4 and 8 segments through stores that record the rescoped scheduler each source is handed, then checks: - each source got exactly one store, budgeted at 1/N of a scan (the 32 MiB floor engages at N=8 locally) - every rescoped scheduler carried traffic and stayed within its budget plus one split request, with zero priority bypasses - the original stores' schedulers saw no merge traffic at all, so no read path escapes the split - the merged index still contains every row from all N sources Passes at LANCE_IO_THREADS=8 and 64 (256 MiB and 2 GiB scan budgets). Peaks print under --nocapture so reviewers see numbers, not asserts.
|
|
resolving in favour of #63 |
Problem
Scanning a scalar index end to end goes through
IndexReaderStream, which issues an independentread_record_batchper 4096-row batch. Each builds a freshDecodeBatchScheduler, andtry_newrunsinitialize()— which inStructuralPrimitiveFieldScheduler::initializeiterates every page in the column, ignoring the rows actually requested. A full scan therefore costs O(batches × pages) metadata reads. Each is one small chunk-metadata buffer, and consecutive pages' buffers sit megabytes apart, so the coalescer (gap tolerance =block_size) can never merge them — onepreadeach.Fix
Adds
IndexReader::whole_file_stream(batch_size, readahead) -> Option<stream>, implemented for the v2FileReaderas a singleread_stream_projectedoverRangeFull, and uses it in the btree scan paths. Readers with no fast path returnNoneand keep the existing per-batch behaviour.Measurements
Two btree index files scanned end to end through both paths in one binary — large: 21.3 GB / 5,505 pages, 20,000 of 234,855 batches; small: 1.1 GB / 698 pages, full scan:
With the cache disabled the per-batch path degrades to 214 s and 835 s. The one-plan path is identical at every cache size — it does not depend on the cache holding, which is the main argument for it.
In per-batch mode
IOPs/batchequals the file's page count exactly (698 and 2,755) — that is the mechanism, measured. Both paths return byte-identical output (checksummed).Testing
cargo test -p lance-index --lib— 638 pass. The new test asserts an IOP bound and was verified to fail without the fix (315 vs a bound of 32).Draft
whole_file_streamis implemented only for the v2FileReader; the multi-fileLazyRangedIndexReaderstill takes the per-batch path.