Skip to content

feat(io): add a weighted cache for immutable file content - #1174

Open
manuzhang wants to merge 2 commits into
apache:mainfrom
manuzhang:codex/weighted-lru-cache
Open

manuzhang wants to merge 2 commits into
apache:mainfrom
manuzhang:codex/weighted-lru-cache

Conversation

@manuzhang

@manuzhang manuzhang commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Summary

Add an internal ContentCache for repeated reads of immutable files. A cache hit serves the complete file from memory. #875 integrates this cache with table manifest and manifest-list readers.

Cache behavior

  • The weighted LRU charges file entries by byte size and memoized entries one unit each. Manifest caching is opt-in through io.manifest.cache-enabled. The default limits are 100 MiB per table cache, 8 MiB per file, and eight active table caches per process. Readers that already hold cached bytes can finish after eviction.
  • On a miss, the adapter forwards any length hint to storage. If storage echoes the hint, the adapter independently checks the full length before caching. Explicit prefix reads bypass the cache, as do empty and oversized files. After invalidation or during a write, a storage-backed handle checks the opened stream against its recorded length so it cannot silently return a prefix of a longer replacement.
  • Before a wrapped create, overwrite, whole-file write, or nonempty delete, the adapter invalidates all caches sharing the underlying FileIO. Even a fresh create can reuse an alias for a file deleted outside the adapter, so this also evicts unrelated entries. Reads overlapping the mutation cannot populate the cache. Whole-file writes delegate to the backend’s WriteFile override.
  • Each cache binds to one FileIO and keys entries by location. Credential rotation can reuse the cache while each location identifies the same immutable object. An endpoint or prefix change that redirects a location to another store requires a fresh cache.

Validation

On 85b1f27e, all 513 util_test tests, 121 arrow_test tests, and pre-commit hooks passed locally. The 42 metadata-cache tests also passed across 30 consecutive runs. Three regressions reproduced on a4c0715b and passed on the updated head. They cover opening a cold handle after a longer replacement, recreating an externally deleted path, and writing through a backend with a whole-file override. GitHub skipped all 10 hosted checks for this head.

manuzhang added a commit to manuzhang/iceberg-cpp that referenced this pull request Nov 25, 2026
Sync the weighted LRU implementation and tests with the standalone patch in apache#1174.

Co-authored-by: Codex <[email protected]>
@manuzhang manuzhang changed the title feat(util): support weighted LRU eviction feat(io): add a weighted cache for immutable file content Nov 25, 2026
@manuzhang
manuzhang marked this pull request as ready for review November 25, 2026 15:27
@manuzhang
manuzhang requested review from wgtmac and zhjwpku November 25, 2026 15:27
Comment thread src/iceberg/content_cache.cc Resolved
Comment thread src/iceberg/content_cache.cc Resolved
Comment thread src/iceberg/util/cache_internal.h Resolved
Comment thread src/iceberg/util/cache_internal.h Resolved
Comment thread src/iceberg/content_cache.cc Resolved
Comment thread src/iceberg/content_cache.cc Resolved
Comment thread src/iceberg/content_cache.cc Resolved
Comment thread src/iceberg/arrow/arrow_io.cc Resolved

@timothyw553 timothyw553 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.

overall looks good, left a few comments on a delete/recreate race, fallback stream wrapping, and test coverage (Read/Seek and write cleanup). also one question on the enabled flag.

Comment thread src/iceberg/content_cache.cc Outdated

struct ClearAfterDelete {
ContentCache& cache;
~ClearAfterDelete() { cache.InvalidateStorage(); }

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.

ClearAfterDelete only invalidates in the destructor after DeleteFile returns. what if storage deletes the file and another adapter recreates it at the same path before DeleteFile finishes? readers could still get the old cached bytes in that window. could we track it as an active write and invalidate up front too, plus add a test for that overlap?

 struct ClearAfterDelete {
+  explicit ClearAfterDelete(ContentCache& owner) : cache(owner) {
+    std::lock_guard lock(cache.storage_.mutex);
+    ++cache.storage_.active_writes;
+    ++cache.storage_.invalidations;
+    cache.SyncGenerationLocked();
+  }
+
   ContentCache& cache;
-  ~ClearAfterDelete() { cache.InvalidateStorage(); }
+  ~ClearAfterDelete() {
+    std::lock_guard lock(cache.storage_.mutex);
+    --cache.storage_.active_writes;
+    ++cache.storage_.invalidations;
+    cache.SyncGenerationLocked();
+  }
 };

Comment thread src/iceberg/content_cache.cc Resolved
Comment thread src/iceberg/test/metadata_cache_test.cc Resolved
return input_ ? input_->Position() : Result<int64_t>(position_);
}

Status Seek(int64_t position) override {

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.

looks like the tests never call Seek, and the Read calls in metadata_cache_test.cc only hit wrapped storage streams rather than the in-memory content_ branch. since avro decoding uses both, could we add tests on an actual cached stream for sequential Read, Seek (backward, eof, invalid positions), ReadFully bounds, and reads after Close()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Tests added.

Comment thread src/iceberg/content_cache_internal.h Resolved
Add a per-table FileIO adapter that retains complete immutable files within a byte budget and limits the number of active caches across tables. Honor input handle sizes on fallback and invalidate cached data across adapters sharing storage when files are overwritten or deleted.
@manuzhang
manuzhang force-pushed the codex/weighted-lru-cache branch from 73cc1a9 to a4c0715 Compare December 1, 2026 09:43
Invalidate caches sharing a FileIO before creating or overwriting files, including paths removed outside the adapter. Dispatch whole-file writes to the backend and verify the stream extent when a cold handle overlaps a mutation.

Co-authored-by: Codex <[email protected]>
@manuzhang
manuzhang marked this pull request as ready for review December 1, 2026 16:06
@manuzhang
manuzhang requested review from timothyw553 and wgtmac and a balanced review from Copilot December 1, 2026 16:06

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.

🟢 Approval recommended

No active review issues are blocking the immutable-file content cache and FileIO adapter changes.

No open issues 🎉

What changed in this PR

Adds an internal immutable-file content cache and FileIO adapter, providing the foundation for manifest and manifest-list caching in #875.

Changes:

  • Extends the LRU cache with weighted entries, immediate resizing, and byte-budget eviction.
  • Adds bounded content caching with length validation, process-wide cache limits, and coordinated invalidation during storage mutations.
  • Extracts shared I/O helpers and adds regression coverage for caching, eviction, aliases, and overlapping reads and writes.
File Description
src/​iceberg/​util/​property_util.h Declares signed 64-bit property parsing.
src/​iceberg/​util/​property_util.cc Implements parsing with property-specific errors.
src/​iceberg/​util/​error_util_internal.h Shares operation and stream-close error handling.
src/​iceberg/​util/​cache_internal.h Adds weighted LRU eviction and updates memoizers.
src/​iceberg/​test/​mock_io.h Supports simulated external file deletion.
src/​iceberg/​test/​metadata_cache_test.cc Covers cache behavior, mutation safety, and lifetime.
src/​iceberg/​test/​CMakeLists.txt Registers content-cache tests.
src/​iceberg/​test/​cache_test.cc Tests weighted eviction, resizing, and move behavior.
src/​iceberg/​test/​arrow_io_test.cc Adds Arrow-backed cache and invalidation regressions.
src/​iceberg/​file_io.cc Extracts reusable reading logic and shares close handling.
src/​iceberg/​file_io_internal.h Declares the internal whole-file read helper.
src/​iceberg/​content_cache.cc Implements caching, registry limits, and I/O adapters.
src/​iceberg/​content_cache_internal.h Defines cache configuration and internal interfaces.
src/​iceberg/​CMakeLists.txt Includes the cache implementation in the library build.

Review effort: Balanced

This branch has not been deployed

No deployments
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.

4 participants