Skip to content

feat(session): allow resizing cache capacity at runtime - #10657

Merged
Xuanwo merged 4 commits into
lance-format:mainfrom
hello-peter-tang:feat/10137-session-cache-capacity
Nov 12, 2026
Merged

Xuanwo merged 4 commits into
lance-format:mainfrom
hello-peter-tang:feat/10137-session-cache-capacity

Conversation

@hello-peter-tang

@hello-peter-tang hello-peter-tang commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Exposes the caches' capacity as a runtime knob through the session API, so an application that manages several sessions can bound their combined memory without dropping or recreating them:

  • CacheBackend::set_capacity (default: NotSupported), implemented by QuickCacheBackend.
  • LanceCache::set_capacity.
  • Session::set_index_cache_capacity and Session::set_metadata_cache_capacity, in Rust and Python (lance.Session).

Shrinking evicts down to the new budget immediately: the quick cache trims its shards, and a cache in priority-admission mode trims its priority tier (lowest priority, then oldest). Priority-tier evictions are reported through the same size-removal counters as ordinary cache evictions. Growing only raises the budget; evicted entries reload on demand. Clones of a session share the cache, so every handle observes the new capacity.

Backends that cannot resize after construction (such as moka) return NotSupported / ValueError rather than silently ignoring the request.

Resizing also has to keep byte accounting honest. LanceCache skips the deep-size traversal while a backend reports itself size-blind, so an insertion that started on a disabled cache can reach admission after the cache grew. QuickCacheBackend therefore refuses any insertion whose size is zero — the value was never measured — instead of charging it its key footprint alone. The check runs before admission and never consults the current capacity, so a resize racing the insertion cannot slip an unmeasured value through. Callers still get their value; it is simply not retained, and a disabled cache keeps reporting the attempt as a rejected write.

Closes #10137.

Test plan

Validated on the integrated tree after merging main (12ca831a):

  • cargo test --profile ci -p lance-core --lib cache:: — new tests cover shrink eviction and reported limits, priority-tier trimming (including exactly one size-removal and its byte weight), PriorityEntries budget trimming, an insertion racing a capacity growth, and unmeasured insertions being refused at any capacity.
  • cargo test --profile ci -p lance --lib session:: — new tests resize both caches, observe the change through a session clone, and report unsupported backends.
  • cargo clippy --profile ci --all --tests --benches -- -D warnings, cargo fmt --all -- --check.
  • make install && make build from python/, then uv run pytest python/tests/test_session.py — new tests evict resident entries by shrinking to zero, re-admit them after growing, and reject unsupported backends.
  • uv run make lint from python/.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions github-actions Bot added A-python Python bindings A-docs Documentation enhancement New feature or request labels Nov 3, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 3, 2026
@hello-peter-tang
hello-peter-tang force-pushed the feat/10137-session-cache-capacity branch from 4dd684a to b457a3e Compare November 4, 2026 00:37
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 4, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Nov 4, 2026
@hello-peter-tang
hello-peter-tang force-pushed the feat/10137-session-cache-capacity branch from b457a3e to a288076 Compare November 4, 2026 04:15
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Nov 5, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. and removed K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Nov 5, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Nov 5, 2026
Expose `Session::set_index_cache_capacity` and
`Session::set_metadata_cache_capacity`, plus their Python equivalents, so
an application managing several sessions can bound their combined memory
without dropping or recreating them.

`QuickCacheBackend` now tracks its capacity atomically and trims both the
ordinary and priority tiers when shrunk; backends that cannot resize (such
as moka) report `NotSupported`.

Closes lance-format#10137
@hello-peter-tang
hello-peter-tang force-pushed the feat/10137-session-cache-capacity branch from a3ee069 to dcd0cd9 Compare November 8, 2026 06:04
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 8, 2026
@hello-peter-tang

Copy link
Copy Markdown
Contributor Author

Rebased onto main (3bb1550f4) and reworked the sizing interaction with the cache changes that landed in the meantime (notably #10419 get_immediate / prepared keys, #10772, #10404).

One behaviour change worth calling out: my previous version made QuickCacheBackend::uses_entry_sizes() return true unconditionally, which would have reverted #9522's fix for zero-capacity caches. The rebase now keeps that fast path and instead rejects an insertion whose size was never measured once the capacity is positive:

  • uses_entry_sizes() stays capacity > 0, so a disabled cache still skips the deep-size traversal.
  • QuickCacheBackend refuses an insertion that arrives with no measured size while its capacity is positive. Callers still get their value; it just is not retained. This closes the race where an insertion started while disabled is admitted after a capacity growth and is charged its key footprint only, which the grown budget could never reclaim.
  • The regression test races an oversized insert against a growth and then inserts the same value again, so it also shows the rejection is about the missing size rather than the resize.

Verification on this branch:

  • cargo test -p lance-core --lib cache:: -> 144 passed, including cache::tests::backends_that_ignore_entry_sizes_skip_the_traversal (unchanged from perf(core): skip the entry deep-size traversal for size-blind backends #9522) and the three resize tests.
  • cargo test -p lance --lib session::tests::set_cache_capacity -> 2 passed.
  • cargo check -p lance-core -p lance --tests, cargo clippy -p lance-core -p lance --tests -- -D warnings, cargo fmt --all -- --check all clean.

The diff is now limited to set_capacity in the backend trait, the quick backend resize path, the session/Python surface and docs.

lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Nov 8, 2026
`LanceCache` skips the deep-size traversal while a backend reports itself
size-blind, which lets a disabled cache insert without paying for a
measurement it would discard. Resizing the capacity at runtime breaks that
premise: an insertion that started while the cache was disabled can reach
admission after the growth, and it would then be charged its key footprint
alone, so the grown budget can never reclaim the payload it holds.

Keep the traversal skipped while the cache is disabled, and have the quick
backend refuse any insertion whose size was never measured. The check runs
after the write is recorded but before admission, so it never consults the
current capacity: an insertion racing a growth observes whatever capacity is
current at that instant, and a zero size proves the value was not measured
either way. Callers still get their value; it is simply not retained, and a
disabled cache keeps reporting the attempt as a rejected write.

Two tests cover it. One races an oversized insertion against a capacity
growth and then re-inserts the same value to show the rejection is about the
missing size rather than the resize. The other drives the backend directly at
zero and non-zero capacity through both the insert and load paths, so a
pre-insertion check that trusted the capacity it observed would fail.
@hello-peter-tang
hello-peter-tang force-pushed the feat/10137-session-cache-capacity branch from dcd0cd9 to 1df1af1 Compare November 8, 2026 08:06
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Nov 8, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 8, 2026
@hello-peter-tang

Copy link
Copy Markdown
Contributor Author

Follow-up on the gate request-changes: the grow-from-zero accounting failure is fixed in 1df1af13c.

admits_measured_size no longer reads the capacity at all. It rejects any insertion whose size is zero, which is exactly the set LanceCache leaves unmeasured while the backend reports itself size-blind, so the outcome no longer depends on the capacity the check happens to observe. The check now runs after record_write and before admission: the disabled-cache fast path (no deep-size traversal) is unchanged, a disabled cache still reports the attempt as a rejected write, and the caller still gets its value — it is just not retained.

Coverage:

  • growing_disabled_cache_does_not_retain_unmeasured_entry — races an oversized insertion against set_capacity, then re-inserts the same value with a size to show the rejection is about the missing size, not the resize.
  • unmeasured_insertions_are_never_retained (new) — drives the backend directly at capacity 0 and 4 MiB through both insert_tagged and get_or_insert_tagged. A check that trusted the observed capacity would retain the value at capacity 0, so this pins the exact race the gate reported.

Local verification: cargo test -p lance-core --lib cache:: 145 passed, cargo test -p lance --lib session:: 33 passed, cargo fmt --all -- --check, cargo clippy -p lance-core --all-targets -- -D warnings.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Nov 8, 2026
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 12, 2026

@lance-gatekeeper lance-gatekeeper Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Gate recommendation: approve.

Runtime cache resizing preserves resident byte accounting and the disabled-cache sizing shortcut after integration with main. No merge-blocking issues or significant residual risks found.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 12, 2026

@Xuanwo Xuanwo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved for squash merge at 76ab9a8. All 46 check runs are terminal (45 success; Arrow ordering compatibility skipped because its detector found no dependency, fixture, or exercised-crate change), all 14 workflow runs are successful, and the format-spec vote gate reports success. The latest Gatekeeper review on this head recommends approval, and the three prior discussion threads are resolved. The session cache capacity API, priority-eviction accounting, and disabled-cache sizing shortcut match the accepted scope.

@Xuanwo
Xuanwo merged commit d4d0d3b into lance-format:main Nov 12, 2026
47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-docs Documentation A-python Python bindings enhancement New feature or request K-approved Latest Gatekeeper recommendation permits acceptance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(feat): expose set_capacity of the cache to the session API

2 participants