Skip to content

fix(cache-moka): bound the caches by bytes instead of entry count - #3164

Open
jackylee-ch wants to merge 2 commits into
apache:mainfrom
jackylee-ch:fix-cache-moka-byte-bounded
Open

fix(cache-moka): bound the caches by bytes instead of entry count#3164
jackylee-ch wants to merge 2 commits into
apache:mainfrom
jackylee-ch:fix-cache-moka-byte-bounded

Conversation

@jackylee-ch

Copy link
Copy Markdown

Which issue does this PR close?

None — filed directly. Related to but distinct from #1720, which is about the accuracy of the weigher
iceberg::io::ObjectCache already has; this crate has no weigher at all.

What changes are included in this PR?

DEFAULT_CACHE_SIZE_BYTES (32MiB) was passed straight to moka::sync::Cache::new. Without a
weigher moka treats max_capacity as a number of entries, so both caches admitted 33_554_432
manifests rather than 32MiB of them — the documented budget never bound.

Build them through Cache::builder().weigher(..).max_capacity(..), using the same
size_of_val weigher as iceberg::io::ObjectCache. No public signature changes.

Are these changes tested?

Yes — test_cache_weighs_entries_by_size_not_count inserts two 512-byte values and asserts
weighted_size() == 1024; on the parent commit it is 2, the entry count.
cargo test --release -p iceberg-cache-moka → 2 passed.

AI Disclosure

Written with AI assistance (Claude Code); I reviewed the change and ran the tests above.
Worth flagging: size_of_val on Arc<Manifest> counts the struct, not the heap its entries own, so
the cap still under-counts — that is exactly what #1720 tracks. This PR only makes the budget a byte
budget; improving the weight function belongs with that issue.

DEFAULT_CACHE_SIZE_BYTES went straight to Cache::new, but without a weigher
moka reads max_capacity as a number of entries, so the 32MiB budget admitted
33_554_432 manifests. Add the weigher iceberg::io::ObjectCache already uses.
Copilot AI lite review requested due to automatic review settings September 7, 2026 08:16

Copilot AI 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.

🟡 Changes recommended

The new weigher currently uses a truncating as u32 cast that can under-weigh large values on overflow, weakening the intended byte-budget bound.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR fixes the iceberg-cache-moka integration to ensure the default Moka caches are bounded by a byte budget (as documented) rather than by entry count, by introducing a weigher and switching construction to Cache::builder().max_capacity(..).

Changes:

  • Add byte_bounded_cache helper that applies a size_of_val-based Moka weigher and enforces max_capacity as bytes.
  • Update MokaObjectCacheProvider::new() to build both default caches via byte_bounded_cache.
  • Add unit tests validating the cache’s weighted sizing behavior and default provider capacity configuration.
File summaries
File Description
crates/integrations/cache-moka/src/lib.rs Switch default caches to byte-weighted Moka caches and add tests verifying weight semantics and capacity.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

fn byte_bounded_cache<V>(max_capacity_bytes: u64) -> moka::sync::Cache<String, Arc<V>>
where V: Send + Sync + 'static {
moka::sync::Cache::builder()
.weigher(|_, value: &Arc<V>| size_of_val(value.as_ref()) as u32)
Review feedback: a truncating cast could wrap for a hypothetical value larger
than u32::MAX. Unreachable for Manifest (72 bytes) and ManifestList (24 bytes),
but the helper is generic, so saturate instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants