Skip to content

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

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

manuzhang wants to merge 21 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 cache for immutable file content. #875 uses it for manifest files and lists when a table enables caching.

  • The LRU accepts an explicit weight on insertion or replacement. File entries use their byte size and memoized entries default to one. Storage grows as entries arrive. A negative entry weight or resize limit returns InvalidArgument before the cache changes. Original LRU allocation review.
  • Each cache binds to one FileIO and keys immutable file content by location. Storage namespace review.
  • The adapter forwards a length hint to storage and uses the returned size. On a cold open, it independently verifies a size that echoes the hint before publishing those bytes as a whole file. Prefix reads go directly to storage. Empty and oversized files reuse their discovered size and bypass read-ahead. Concurrent fills with the same generation and length share the retained buffer. Hinted read review.
  • Tables keep separate byte caches. Adapters bound to the same storage FileIO share invalidation for overwrites and nonempty delete batches, including partially successful batches. Each peer discards invalidated entries on its next cache access. Deleting through one adapter and recreating the path through another invalidates all cached spellings. Creating a fresh manifest preserves warm entries in nested adapters, and reads that overlap the write cannot publish partial bytes. An explicit table-cache clear affects that table and adapters layered on it.
  • The process-wide LRU tracks active table caches through weak references. A cache acquires a slot on its first insertion. Successful file hits and insertions refresh its position. Clearing the cache or destroying its last handle releases the slot. Eviction detaches the cached files under the registry mutex and releases them after unlocking. Open readers retain their bytes. The default of eight table caches matches Java’s default count for FileIO caches. ContentCache::SetGlobalCapacity adjusts the limit immediately and reports the supplied value when it rejects a nonpositive limit. feat: cache immutable manifest content per table #875 exposes this as Table::SetManifestCacheLimit. Global eviction review.
  • Share stream-close handling with FileIO so a close error preserves an earlier read or write error.

Scope

Changes to S3 FileIO and automatic detection of endpoint or prefix rerouting are out of scope for this PR. The bound FileIO must keep each cached location in the same storage namespace for the lifetime of the cache. Credential rotation can reuse the cache when the location still identifies the same immutable object. If a routing change makes the same location refer to a different store, callers must use a fresh cache before reading from that store. Reusing the old cache can serve bytes from the previous store.

Validation

All 473 tests in util_test and 118 in arrow_test passed locally. The delete-and-recreate and nested fresh-write regressions both failed on the previous revision. An earlier local run passed all 44 S3 tests against loopback MinIO using a separate S3-enabled Arrow 23.0.1 build and matching AWS SDK. Regression coverage includes the InvalidArgument error and property name for malformed numeric settings, identical locations on distinct backends, invalid storage-reported sizes, cold short hints, nested wrappers, alias overwrites, partial writes and deletes, concurrent insertion, and eviction order. An earlier release-mode run passed all 15 LRU and memoization tests with -DNDEBUG. The move regression reproduced a stale weight on the previous revision and now checks that both move construction and move assignment leave the source cache empty and reusable. All repository pre-commit hooks passed.

Track entry weights, support eviction on resize, and grow storage on demand.

Co-authored-by: Codex <codex@openai.com>
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 <codex@openai.com>
manuzhang and others added 2 commits November 25, 2026 22:14
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>
@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 and others added 2 commits November 25, 2026 22:41
Use typed cache properties and keep the process capacity configurable through the runtime setter. Reject deletes on the cache adapter and keep older input handles from restoring invalidated bytes.

Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>
@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 Outdated
}
int64_t size;
if (length) {
size = static_cast<int64_t>(*length);

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.

FileIO can ignore this length hint. A short hint then stores a prefix as the whole file, so a later unhinted read returns truncated content. Please pass the hint to the backend and use the returned InputFile's Size() for admission and read-ahead.

Comment thread src/iceberg/content_cache.cc Outdated
if (content_) {
return std::make_unique<CachedSeekableInputStream>(content_);
}
if (auto cached = cache_->Find(location_)) {

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.

This handle fixes Size() at construction, while Open() can use a later cache entry. After invalidation and a warm read of a differently sized replacement, this lookup can return the new bytes with the old size. Please require a matching generation and length on the second lookup and cover opening the old handle after the replacement is warm.

Comment thread src/iceberg/util/cache_internal.h Outdated
auto& item = candidate.emplace_front(nullptr, std::forward<V>(value));
auto [it, inserted] = map_.try_emplace(std::forward<K>(key));
item.key = &it->first;
try {

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.

Both callers already know the weight: one per memoized entry or the file size in bytes. Could Replace() take the weight directly, defaulting to 1, so we can remove the throwing callback and rollback path?

Comment thread src/iceberg/util/cache_internal.h Outdated

// Apply a new limit immediately, evicting least recently used entries as needed.
void Resize(int64_t max_weight) {
ICEBERG_DCHECK(max_weight >= 0, "Cache maximum weight must be non-negative");

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.

A negative limit can drive eviction past the last entry in release builds because this DCHECK is compiled out. Can we validate the limit at the configuration boundary, or return InvalidArgument from Resize() if it accepts unchecked input?

Comment thread src/iceberg/content_cache.cc Outdated
Result<std::unique_ptr<OutputFile>> NewOutputFile(std::string location) override {
return io_->NewOutputFile(std::move(location));
}
Status DeleteFile(const std::string& /*location*/) override {

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.

Could we give manifest cleanup a single operation that deletes through storage and invalidates the cache? Today every cleanup caller has to remember both steps.

Comment thread src/iceberg/content_cache.cc Outdated
return GlobalContentCacheRegistry()->SetCapacity(max_caches);
}

std::shared_ptr<FileIO> ContentCache::Wrap(std::shared_ptr<FileIO> io) {

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.

A cache can be bound to several FileIOs, and a hit is keyed only by location before storage resolution. Could we make the same-storage-namespace requirement explicit at this binding point? Otherwise a rebind to a different backend can serve bytes from the previous one.

Comment thread src/iceberg/content_cache.cc Outdated
void TrimToCapacity() {
while (std::cmp_greater(entries_.size(), capacity_)) {
if (auto entry = entries_.back().lock()) {
entry->files.reset();

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.

This reset can destroy an entire table's LRU under the registry mutex, blocking lookups for other tables. Could we detach the evicted state under the lock and release it after unlocking?

Comment thread src/iceberg/arrow/arrow_io.cc Outdated
std::optional<std::string_view> storage_path() const override {
const auto type = fs_->type_name();
// Local paths can have symlinks, and a custom filesystem defines its own aliases.
if (type != "s3" && type != "mock") return std::nullopt;

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.

This looks too hacky to me...

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.

Could we clear the table’s manifest cache once per cleanup batch? That would handle all path aliases without adding an InputFile subtype.

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.

2 participants