From 9b02ccc448e45c35f28153cf9cb0d935b70f5c7c Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Fri, 6 Nov 2026 14:02:24 +0900 Subject: [PATCH 01/16] feat: cache immutable manifest content per table Keep bounded content-cache state on Table and share it with manifest readers, scans, and compatible refresh and commit handles. Configure independent table budgets from catalog and table properties while keeping FileIO and ReaderOptions unchanged. Reuse weighted LruCache for binary content and bounded normalized aliases. Skip empty and oversized files, validate lengths before storage access, and close Arrow size probes on every path while preserving errors. --- mkdocs/docs/file-io.md | 28 + src/iceberg/CMakeLists.txt | 1 + src/iceberg/arrow/arrow_io.cc | 27 +- .../catalog/memory/in_memory_catalog.cc | 22 +- src/iceberg/catalog/rest/rest_catalog.cc | 8 +- src/iceberg/catalog/rest/rest_file_io.cc | 3 + src/iceberg/catalog/rest/rest_table.cc | 8 +- src/iceberg/catalog/sql/sql_catalog.cc | 13 +- src/iceberg/content_cache.cc | 304 ++++++++ src/iceberg/content_cache_internal.h | 97 +++ src/iceberg/data_table_scan.cc | 11 +- src/iceberg/incremental_append_scan.cc | 6 +- src/iceberg/incremental_changelog_scan.cc | 10 +- src/iceberg/table.cc | 62 +- src/iceberg/table.h | 31 +- src/iceberg/table_scan.cc | 1 + src/iceberg/table_scan.h | 3 + src/iceberg/test/CMakeLists.txt | 1 + src/iceberg/test/arrow_io_test.cc | 140 ++++ src/iceberg/test/cache_test.cc | 111 +++ src/iceberg/test/fast_append_test.cc | 1 + src/iceberg/test/manifest_reader_test.cc | 154 ++++ src/iceberg/test/metadata_cache_test.cc | 668 ++++++++++++++++++ src/iceberg/test/transaction_test.cc | 23 + src/iceberg/transaction.cc | 5 +- src/iceberg/update/cherry_pick_operation.cc | 4 +- src/iceberg/update/expire_snapshots.cc | 10 +- src/iceberg/update/fast_append.cc | 4 +- src/iceberg/update/merging_snapshot_update.cc | 6 +- src/iceberg/update/overwrite_files.cc | 16 +- src/iceberg/update/replace_partitions.cc | 2 +- src/iceberg/update/rewrite_files.cc | 2 +- src/iceberg/update/row_delta.cc | 2 +- src/iceberg/update/snapshot_update.cc | 3 +- src/iceberg/util/cache_internal.h | 108 ++- 35 files changed, 1811 insertions(+), 84 deletions(-) create mode 100644 src/iceberg/content_cache.cc create mode 100644 src/iceberg/content_cache_internal.h create mode 100644 src/iceberg/test/metadata_cache_test.cc diff --git a/mkdocs/docs/file-io.md b/mkdocs/docs/file-io.md index 62f998b1fc..823035c24f 100644 --- a/mkdocs/docs/file-io.md +++ b/mkdocs/docs/file-io.md @@ -21,6 +21,34 @@ `FileIO` reads, writes, and deletes Iceberg data and metadata files. +## Manifest content cache + +Manifest content caching is owned by each loaded `Table`. Tables sharing a `FileIO` +have independent caches and byte budgets. The storage `FileIO` remains usable for +uncached data-file and table-metadata JSON reads. + +| Property | Default | Description | +|---|---|---| +| `io.manifest.cache-enabled` | `false` | Cache immutable manifest-list and manifest bytes across scans of a table | +| `io.manifest.cache.max-total-bytes` | `104857600` | Maximum combined content retained by one table's cache | +| `io.manifest.cache.max-content-length` | `8388608` | Maximum file size eligible for caching | + +Catalog I/O properties provide defaults. Table properties override them. Direct +`Table::Make` calls can supply defaults through the trailing `io_properties` +argument; properties exposed by the supplied FileIO are also used as defaults. + +Scans and manifest maintenance share the table's cache. Refreshes and transactions +preserve cached bytes when the table UUID, cache limits, and storage context are +compatible. Independently loaded table handles start with separate caches. + +`Table::ClearManifestCache()` releases retained entries. Active input handles and +streams can keep their bytes alive after clearing or eviction; those bytes and +concurrent population buffers are outside the retention budget. Normalized path +aliases use a separate index retaining at most 64 KiB of alias strings per table +and do not pin content. Empty files and +files above either byte limit bypass the cache. Time-based expiration is not +supported, so `io.manifest.cache.expiration-interval-ms` is ignored. + ## Built-in implementations Call `iceberg::arrow::RegisterAll()` to register the Arrow-backed FileIO diff --git a/src/iceberg/CMakeLists.txt b/src/iceberg/CMakeLists.txt index 0065b838b1..e71c2016af 100644 --- a/src/iceberg/CMakeLists.txt +++ b/src/iceberg/CMakeLists.txt @@ -26,6 +26,7 @@ set(ICEBERG_SOURCES catalog/memory/in_memory_catalog.cc catalog/session_catalog.cc catalog/session_context.cc + content_cache.cc data_table_scan.cc delete_file_index.cc deletes/dv_util.cc diff --git a/src/iceberg/arrow/arrow_io.cc b/src/iceberg/arrow/arrow_io.cc index 998f8c5ce8..f9bab7b808 100644 --- a/src/iceberg/arrow/arrow_io.cc +++ b/src/iceberg/arrow/arrow_io.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -428,24 +429,41 @@ class ArrowInputFile : public InputFile { } ::arrow::fs::FileInfo file_info(path_, ::arrow::fs::FileType::File); ICEBERG_ARROW_ASSIGN_OR_RETURN(auto input, fs_->OpenInputFile(file_info)); - ICEBERG_ARROW_ASSIGN_OR_RETURN(auto size, input->GetSize()); + auto size_result = input->GetSize(); + auto close_status = input->Close(); + if (!size_result.ok() && !close_status.ok()) { + auto status = size_result.status().WithMessage( + size_result.status().message(), + "; additionally failed to close file: ", close_status.ToString()); + ICEBERG_ARROW_RETURN_NOT_OK(status); + } + ICEBERG_ARROW_ASSIGN_OR_RETURN(auto size, std::move(size_result)); + ICEBERG_ARROW_RETURN_NOT_OK(close_status); + file_size_ = size; return size; } Result> Open() override { + ICEBERG_ASSIGN_OR_RAISE(auto input, OpenArrowFile()); + return std::make_unique(std::move(input)); + } + + Result> OpenArrowFile() { ::arrow::fs::FileInfo file_info(path_, ::arrow::fs::FileType::File); if (file_size_.has_value()) { file_info.set_size(*file_size_); } ICEBERG_ARROW_ASSIGN_OR_RETURN(auto input, fs_->OpenInputFile(file_info)); - return std::make_unique(std::move(input)); + return input; } private: std::shared_ptr<::arrow::fs::FileSystem> fs_; std::string location_; std::string path_; - std::optional file_size_; + // Set by Size() so a later Open() can skip the size lookup. Not synchronized: like + // other InputFile handles, this one is not shared across threads. + mutable std::optional file_size_; }; class ArrowOutputFile : public OutputFile { @@ -530,6 +548,9 @@ Result> OpenArrowInputStream( } else { ICEBERG_ASSIGN_OR_RAISE(input_file, io->NewInputFile(path)); } + if (auto* arrow_input = dynamic_cast(input_file.get())) { + return arrow_input->OpenArrowFile(); + } ICEBERG_ASSIGN_OR_RAISE(size, input_file->Size()); if (size < 0) { return Invalid("Invalid negative file size {} for {}", size, path); diff --git a/src/iceberg/catalog/memory/in_memory_catalog.cc b/src/iceberg/catalog/memory/in_memory_catalog.cc index e7229bef30..1a9fdfc465 100644 --- a/src/iceberg/catalog/memory/in_memory_catalog.cc +++ b/src/iceberg/catalog/memory/in_memory_catalog.cc @@ -23,6 +23,7 @@ #include #include "iceberg/catalog/catalog_util.h" +#include "iceberg/content_cache_internal.h" #include "iceberg/file_io.h" #include "iceberg/metrics/metrics_reporters.h" #include "iceberg/table.h" @@ -343,6 +344,8 @@ Result> InMemoryCatalog::Make( const std::string& name, const std::shared_ptr& file_io, const std::string& warehouse_location, const std::unordered_map& properties) { + ICEBERG_PRECHECK(file_io != nullptr, "InMemoryCatalog requires a non-null FileIO"); + ICEBERG_RETURN_UNEXPECTED(internal::ContentCacheOptions::FromProperties(properties)); std::shared_ptr reporter; auto it = properties.find(std::string(kMetricsReporterImpl)); if (it != properties.end() && !it->second.empty() && @@ -445,7 +448,8 @@ Result> InMemoryCatalog::CreateTable( root_namespace_->UpdateTableMetadataLocation(identifier, metadata_file_location)); return Table::Make(identifier, std::move(table_metadata), std::move(metadata_file_location), file_io_, shared_from_this(), - CatalogUtil::FullTableName(name(), identifier), reporter_); + CatalogUtil::FullTableName(name(), identifier), reporter_, + /*labels=*/nullptr, properties_); } Result> InMemoryCatalog::UpdateTable( @@ -499,7 +503,8 @@ Result> InMemoryCatalog::UpdateTable( return Table::Make(identifier, std::move(updated), std::move(new_metadata_location), file_io_, shared_from_this(), - CatalogUtil::FullTableName(name(), identifier), reporter_); + CatalogUtil::FullTableName(name(), identifier), reporter_, + /*labels=*/nullptr, properties_); } Result> InMemoryCatalog::StageCreateTable( @@ -519,10 +524,10 @@ Result> InMemoryCatalog::StageCreateTable( auto table_metadata, TableMetadata::Make(*schema, *spec, *order, base_location, properties)); ICEBERG_ASSIGN_OR_RAISE( - auto table, - StagedTable::Make(identifier, std::move(table_metadata), "", file_io_, - shared_from_this(), - CatalogUtil::FullTableName(name(), identifier), reporter_)); + auto table, StagedTable::Make(identifier, std::move(table_metadata), "", file_io_, + shared_from_this(), + CatalogUtil::FullTableName(name(), identifier), + reporter_, properties_)); return Transaction::Make(std::move(table), TransactionKind::kCreate); } @@ -603,7 +608,8 @@ Result> InMemoryCatalog::LoadTable( TableMetadataUtil::Read(*file_io_, metadata_location)); return Table::Make(identifier, std::move(metadata), std::move(metadata_location), file_io_, shared_from_this(), - CatalogUtil::FullTableName(name(), identifier), reporter_); + CatalogUtil::FullTableName(name(), identifier), reporter_, + /*labels=*/nullptr, properties_); } Result> InMemoryCatalog::RegisterTable( @@ -626,7 +632,7 @@ Result> InMemoryCatalog::RegisterTable( } return Table::Make(identifier, std::move(metadata), metadata_file_location, file_io_, shared_from_this(), CatalogUtil::FullTableName(name(), identifier), - reporter_); + reporter_, /*labels=*/nullptr, properties_); } } // namespace iceberg diff --git a/src/iceberg/catalog/rest/rest_catalog.cc b/src/iceberg/catalog/rest/rest_catalog.cc index a6ba928e50..54c0d6446f 100644 --- a/src/iceberg/catalog/rest/rest_catalog.cc +++ b/src/iceberg/catalog/rest/rest_catalog.cc @@ -927,12 +927,14 @@ Result> RestCatalog::StageCreateTable( auto table_catalog = std::make_shared( shared_from_this(), context, identifier, table_config, std::move(table_session), table_io); + auto manifest_properties = config_.configs(); + for (const auto& [key, value] : table_config) manifest_properties[key] = value; ICEBERG_ASSIGN_OR_RAISE( auto staged_table, StagedTable::Make(identifier, std::move(result.metadata), std::move(result.metadata_location), std::move(table_io), std::move(table_catalog), RestTableName(name_, identifier), - std::move(reporter))); + std::move(reporter), manifest_properties)); return Transaction::Make(std::move(staged_table), TransactionKind::kCreate); } @@ -1078,6 +1080,8 @@ Result> RestCatalog::MakeTable( std::shared_ptr table_catalog, std::shared_ptr reporter, const std::unordered_map& table_config, std::shared_ptr table_session, std::unique_ptr labels) { + auto manifest_properties = config_.configs(); + for (const auto& [key, value] : table_config) manifest_properties[key] = value; // Table config overrides client config. ICEBERG_ASSIGN_OR_RAISE(auto server_mode, RestCatalogProperties::ScanPlanningModeFrom(table_config)); @@ -1131,7 +1135,7 @@ Result> RestCatalog::MakeTable( return Table::Make(identifier, std::move(metadata), std::move(metadata_location), std::move(table_io), std::move(table_catalog), RestTableName(name_, identifier), std::move(reporter), - std::move(labels)); + std::move(labels), manifest_properties); } } // namespace iceberg::rest diff --git a/src/iceberg/catalog/rest/rest_file_io.cc b/src/iceberg/catalog/rest/rest_file_io.cc index 4cd102704b..f7f7aa6410 100644 --- a/src/iceberg/catalog/rest/rest_file_io.cc +++ b/src/iceberg/catalog/rest/rest_file_io.cc @@ -25,6 +25,7 @@ #include #include "iceberg/catalog/rest/types.h" +#include "iceberg/content_cache_internal.h" #include "iceberg/file_io.h" #include "iceberg/file_io_registry.h" #include "iceberg/logging/log_macros.h" @@ -48,6 +49,8 @@ std::unordered_map MergeFileIOProperties( } // namespace Result> MakeCatalogFileIO(const RestCatalogProperties& config) { + ICEBERG_RETURN_UNEXPECTED( + internal::ContentCacheOptions::FromProperties(config.configs())); const std::string io_impl = config.Get(RestCatalogProperties::kIOImpl); if (io_impl.empty()) { return std::make_unique(config.configs()); diff --git a/src/iceberg/catalog/rest/rest_table.cc b/src/iceberg/catalog/rest/rest_table.cc index f70bd9e926..c12909817d 100644 --- a/src/iceberg/catalog/rest/rest_table.cc +++ b/src/iceberg/catalog/rest/rest_table.cc @@ -48,13 +48,19 @@ Result> RestTable::Make( std::shared_ptr catalog, std::string full_name, std::shared_ptr reporter, RestScanContext rest_context, std::unique_ptr labels) { + ICEBERG_PRECHECK(io != nullptr, "FileIO cannot be null"); if (metadata == nullptr) { return InvalidArgument("Metadata cannot be null"); } - return std::shared_ptr(new RestTable( + auto table = std::shared_ptr(new RestTable( std::move(identifier), std::move(metadata), std::move(metadata_location), std::move(io), std::move(catalog), std::move(full_name), std::move(reporter), std::move(rest_context), std::move(labels))); + auto properties = table->rest_context_->catalog_config; + for (const auto& [key, value] : table->rest_context_->table_config) + properties[key] = value; + ICEBERG_RETURN_UNEXPECTED(table->InitializeManifestCache(properties)); + return table; } Result> RestTable::NewScan() const { diff --git a/src/iceberg/catalog/sql/sql_catalog.cc b/src/iceberg/catalog/sql/sql_catalog.cc index 169612f49b..78866843fb 100644 --- a/src/iceberg/catalog/sql/sql_catalog.cc +++ b/src/iceberg/catalog/sql/sql_catalog.cc @@ -25,6 +25,7 @@ #include "iceberg/catalog/catalog_util.h" #include "iceberg/catalog/sql/config.h" +#include "iceberg/content_cache_internal.h" #include "iceberg/file_io.h" #include "iceberg/metrics/metrics_reporters.h" #include "iceberg/table.h" @@ -148,6 +149,7 @@ Result> SqlCatalog::Make( if (file_io == nullptr) { return InvalidArgument("SqlCatalog requires a non-null FileIO"); } + ICEBERG_RETURN_UNEXPECTED(internal::ContentCacheOptions::FromProperties(config.props)); ICEBERG_RETURN_UNEXPECTED(store->Initialize()); std::shared_ptr reporter; @@ -385,7 +387,7 @@ Result> SqlCatalog::LoadTableFrom( TableMetadataUtil::Read(*file_io_, metadata_location)); return Table::Make(identifier, std::move(metadata), metadata_location, file_io_, shared_from_this(), CatalogUtil::FullTableName(name(), identifier), - reporter_); + reporter_, /*labels=*/nullptr, config_.props); } Result> SqlCatalog::LoadTable(const TableIdentifier& identifier) { @@ -424,7 +426,7 @@ Result> SqlCatalog::CreateTable( return Table::Make(identifier, std::move(metadata), metadata_location, file_io_, shared_from_this(), CatalogUtil::FullTableName(name(), identifier), - reporter_); + reporter_, /*labels=*/nullptr, config_.props); } Result> SqlCatalog::UpdateTable( @@ -492,7 +494,7 @@ Result> SqlCatalog::UpdateTable( return Table::Make(identifier, std::move(updated), new_metadata_location, file_io_, shared_from_this(), CatalogUtil::FullTableName(name(), identifier), - reporter_); + reporter_, /*labels=*/nullptr, config_.props); } Result> SqlCatalog::StageCreateTable( @@ -520,7 +522,8 @@ Result> SqlCatalog::StageCreateTable( ICEBERG_ASSIGN_OR_RAISE( auto table, StagedTable::Make(identifier, std::move(metadata), "", file_io_, shared_from_this(), - CatalogUtil::FullTableName(name(), identifier), reporter_)); + CatalogUtil::FullTableName(name(), identifier), reporter_, + config_.props)); return Transaction::Make(std::move(table), TransactionKind::kCreate); } @@ -601,7 +604,7 @@ Result> SqlCatalog::RegisterTable( return Table::Make(identifier, std::move(metadata), metadata_file_location, file_io_, shared_from_this(), CatalogUtil::FullTableName(name(), identifier), - reporter_); + reporter_, /*labels=*/nullptr, config_.props); } // -------------------------------------------------------------------------- diff --git a/src/iceberg/content_cache.cc b/src/iceberg/content_cache.cc new file mode 100644 index 0000000000..bb4f52e1f9 --- /dev/null +++ b/src/iceberg/content_cache.cc @@ -0,0 +1,304 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include +#include +#include + +#include "iceberg/content_cache_internal.h" +#include "iceberg/util/macros.h" +#include "iceberg/util/string_util.h" + +namespace iceberg::internal { +namespace { + +template +Result ParseNumberProperty( + const std::unordered_map& properties, std::string_view key, + T fallback) { + auto it = properties.find(std::string(key)); + if (it == properties.end()) return fallback; + auto result = StringUtils::ParseNumber(it->second); + if (!result) + return InvalidArgument("Invalid numeric value '{}' for property '{}': {}", it->second, + key, result.error().message); + return result; +} + +Status FinishWithCloseStatus(Status operation_status, Status close_status) { + if (!operation_status.has_value()) { + auto error = operation_status.error(); + if (!close_status.has_value()) { + error.message += "; additionally failed to close stream: "; + error.message += close_status.error().message; + } + return ::iceberg::unexpected(std::move(error)); + } + return close_status; +} + +Result> ReadInputFile(InputFile& input_file, int64_t read_size, + std::string_view file_location) { + if (read_size < 0) { + return Invalid("Invalid negative file size {} for {}", read_size, file_location); + } + if (static_cast(read_size) > + static_cast(std::numeric_limits::max())) { + return Invalid("File size {} exceeds size_t max for {}", read_size, file_location); + } + + auto size = static_cast(read_size); + std::vector content(size); + ICEBERG_ASSIGN_OR_RAISE(auto stream, input_file.Open()); + Status read_status = {}; + if (size > 0) { + read_status = stream->ReadFully(/*position=*/0, content); + } + ICEBERG_RETURN_UNEXPECTED( + FinishWithCloseStatus(std::move(read_status), stream->Close())); + return content; +} + +class CachedSeekableInputStream : public SeekableInputStream { + public: + explicit CachedSeekableInputStream(MetadataContent content) + : content_(std::move(content)) {} + + Result Position() const override { return position_; } + + Status Seek(int64_t position) override { + ICEBERG_PRECHECK(!closed_, "Input stream is closed"); + ICEBERG_PRECHECK(position >= 0, "Position must not be negative: {}", position); + ICEBERG_PRECHECK(static_cast(position) <= content_->size(), + "Position {} exceeds file size {}", position, content_->size()); + position_ = position; + return {}; + } + + Result Read(std::span out) override { + ICEBERG_PRECHECK(!closed_, "Input stream is closed"); + auto position = static_cast(position_); + auto bytes_to_read = std::min(out.size(), content_->size() - position); + if (bytes_to_read > 0) { + std::memcpy(out.data(), content_->data() + position, bytes_to_read); + position_ += static_cast(bytes_to_read); + } + return static_cast(bytes_to_read); + } + + Status ReadFully(int64_t position, std::span out) override { + ICEBERG_PRECHECK(!closed_, "Input stream is closed"); + ICEBERG_PRECHECK(position >= 0, "Position must not be negative: {}", position); + ICEBERG_PRECHECK(static_cast(position) <= content_->size(), + "Position {} exceeds file size {}", position, content_->size()); + auto offset = static_cast(position); + ICEBERG_PRECHECK(out.size() <= content_->size() - offset, + "Read out of bounds: offset {} + length {} exceeds file size {}", + position, out.size(), content_->size()); + if (!out.empty()) { + std::memcpy(out.data(), content_->data() + offset, out.size()); + } + return {}; + } + + Status Close() override { + closed_ = true; + return {}; + } + + private: + MetadataContent content_; + int64_t position_ = 0; + bool closed_ = false; +}; + +class CachedInputFile : public InputFile { + public: + CachedInputFile(std::unique_ptr input, std::shared_ptr cache, + int64_t size) + : input_(std::move(input)), + cache_(std::move(cache)), + location_(input_->location()), + size_(size) {} + CachedInputFile(std::string location, MetadataContent content) + : location_(std::move(location)), + size_(static_cast(content->size())), + content_(std::move(content)) {} + std::string_view location() const override { return location_; } + Result Size() const override { return size_; } + Result> Open() override { + if (content_) return std::make_unique(content_); + if (auto cached = cache_->Find(location_)) + return std::make_unique(std::move(cached)); + auto loaded = ReadInputFile(*input_, size_, location_); + if (!loaded) { + auto error = std::move(loaded).error(); + if (error.kind != ErrorKind::kIOError) return unexpected(std::move(error)); + auto fallback = input_->Open(); + if (!fallback) { + error.message += "; fallback open failed: " + fallback.error().message; + return unexpected(std::move(error)); + } + return fallback; + } + auto content = + std::make_shared>(std::move(loaded).value()); + cache_->Insert(location_, content); + return std::make_unique(std::move(content)); + } + + private: + std::unique_ptr input_; + std::shared_ptr cache_; + std::string location_; + int64_t size_; + MetadataContent content_; +}; + +// Only manifest readers receive this adapter. Output and deletion operations are +// forwarded unchanged, allowing manifest maintenance to use the same read context. +class ManifestFileIO : public FileIO { + public: + ManifestFileIO(std::shared_ptr io, std::shared_ptr cache) + : io_(std::move(io)), cache_(std::move(cache)) {} + Result> NewInputFile(std::string location) override { + return cache_->NewInputFile(io_, std::move(location)); + } + Result> NewInputFile(std::string location, + size_t length) override { + return cache_->NewInputFile(io_, std::move(location), length); + } + Result> NewOutputFile(std::string location) override { + return io_->NewOutputFile(std::move(location)); + } + Status DeleteFile(const std::string& location) override { + return io_->DeleteFile(location); + } + Status DeleteFiles(const std::vector& locations) override { + return io_->DeleteFiles(locations); + } + const std::unordered_map& properties() const override { + return io_->properties(); + } + + private: + std::shared_ptr io_; + std::shared_ptr cache_; +}; + +} // namespace + +Result ContentCacheOptions::FromProperties( + const std::unordered_map& properties) { + ContentCacheOptions options; + if (auto it = properties.find(std::string(kEnabled)); it != properties.end()) + options.enabled = StringUtils::ParseBoolean(it->second); + ICEBERG_ASSIGN_OR_RAISE( + options.max_total_bytes, + ParseNumberProperty(properties, kMaxTotalBytes, options.max_total_bytes)); + ICEBERG_ASSIGN_OR_RAISE( + options.max_content_length, + ParseNumberProperty(properties, kMaxContentLength, options.max_content_length)); + if (options.enabled) { + ICEBERG_PRECHECK( + options.max_total_bytes > 0 && + std::cmp_less_equal(options.max_total_bytes, + std::numeric_limits::max()), + "Manifest cache maximum total bytes must be positive and fit in int64_t"); + ICEBERG_PRECHECK( + options.max_content_length > 0 && + std::cmp_less_equal(options.max_content_length, + std::numeric_limits::max()), + "Manifest cache maximum content length must be positive and fit in int64_t"); + } + return options; +} + +ContentCache::ContentCache(ContentCacheOptions options) + : options_(options), + lru_(static_cast(options.max_total_bytes)), + aliases_( + static_cast(std::min(options.max_total_bytes, 64 * 1024))) {} +std::shared_ptr ContentCache::Wrap(std::shared_ptr io) { + return std::make_shared(std::move(io), shared_from_this()); +} +MetadataContent ContentCache::Find(std::string_view location, + std::string* canonical_location) { + std::lock_guard lock(mutex_); + if (auto* content = lru_.Find(location)) { + if (canonical_location) *canonical_location = location; + return *content; + } + if (auto* canonical = aliases_.Find(location)) { + if (auto* content = lru_.Find(*canonical)) { + if (canonical_location) *canonical_location = *canonical; + return *content; + } + } + return nullptr; +} +void ContentCache::Insert(const std::string& location, MetadataContent content) { + // Empty/corrupt metadata must not accumulate unbounded zero-weight entries. + if (content->empty()) return; + std::lock_guard lock(mutex_); + lru_.Replace(location, std::move(content)); +} +void ContentCache::Clear() { + std::lock_guard lock(mutex_); + lru_.Clear(); + aliases_.Clear(); +} +Result> ContentCache::NewInputFile( + const std::shared_ptr& io, std::string location, + std::optional length) { + if (length && std::cmp_greater(*length, std::numeric_limits::max())) + return InvalidArgument("File length {} exceeds int64_t max", *length); + // A hit needs neither storage resolution nor working remote credentials. + std::string canonical_location; + if (auto content = Find(location, &canonical_location)) + return std::make_unique(std::move(canonical_location), + std::move(content)); + std::unique_ptr input; + if (length) { + ICEBERG_ASSIGN_OR_RAISE(input, io->NewInputFile(location, *length)); + } else { + ICEBERG_ASSIGN_OR_RAISE(input, io->NewInputFile(location)); + } + if (input->location() != location) { + std::lock_guard lock(mutex_); + aliases_.Replace(std::move(location), std::string(input->location())); + } + if (auto content = Find(input->location())) + return std::make_unique(std::string(input->location()), + std::move(content)); + int64_t size; + if (length) + size = static_cast(*length); + else { + ICEBERG_ASSIGN_OR_RAISE(size, input->Size()); + } + ICEBERG_PRECHECK(size >= 0, "Invalid negative file size {}", size); + if (size == 0 || std::cmp_greater(size, options_.max_content_length) || + std::cmp_greater(size, options_.max_total_bytes)) + return input; + return std::make_unique(std::move(input), shared_from_this(), size); +} + +} // namespace iceberg::internal diff --git a/src/iceberg/content_cache_internal.h b/src/iceberg/content_cache_internal.h new file mode 100644 index 0000000000..5ec39bb7fa --- /dev/null +++ b/src/iceberg/content_cache_internal.h @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "iceberg/file_io.h" +#include "iceberg/util/cache_internal.h" +#include "iceberg/util/string_util.h" + +namespace iceberg::internal { + +// Cache limits apply independently to each loaded table. Expiration is not supported. +struct ICEBERG_EXPORT ContentCacheOptions { + inline static constexpr std::string_view kEnabled = "io.manifest.cache-enabled"; + inline static constexpr std::string_view kMaxTotalBytes = + "io.manifest.cache.max-total-bytes"; + inline static constexpr std::string_view kMaxContentLength = + "io.manifest.cache.max-content-length"; + bool enabled = false; + size_t max_total_bytes = 100 * 1024 * 1024; + size_t max_content_length = 8 * 1024 * 1024; + friend bool operator==(const ContentCacheOptions&, + const ContentCacheOptions&) = default; + static Result FromProperties( + const std::unordered_map& properties); +}; + +using MetadataContent = std::shared_ptr>; +struct MetadataContentWeigher { + int64_t operator()(const std::string&, const MetadataContent& content) const { + return static_cast(content->size()); + } +}; + +// Bound the bookkeeping for normalized aliases separately from retained content. +struct LocationAliasWeigher { + int64_t operator()(const std::string& alias, const std::string& canonical) const { + constexpr size_t kMaxAliasPart = 65537; + return static_cast(std::min(alias.size(), kMaxAliasPart) + + std::min(canonical.size(), kMaxAliasPart)); + } +}; + +// A table owns this cache; readers share it through a metadata-only FileIO adapter. +// Storage FileIOs remain independent of the cache and its configuration. +class ICEBERG_EXPORT ContentCache : public std::enable_shared_from_this { + public: + explicit ContentCache(ContentCacheOptions options); + const ContentCacheOptions& options() const { return options_; } + std::shared_ptr Wrap(std::shared_ptr io); + Result> NewInputFile( + const std::shared_ptr& io, std::string location, + std::optional length = std::nullopt); + // Optionally return the canonical location represented by a cache hit. + MetadataContent Find(std::string_view location, + std::string* canonical_location = nullptr); + void Insert(const std::string& location, MetadataContent content); + void Clear(); + + private: + const ContentCacheOptions options_; + std::mutex mutex_; + LruCache + lru_; + // Alias strings do not pin content and are capped at 64 KiB in total. + LruCache + aliases_; +}; + +} // namespace iceberg::internal diff --git a/src/iceberg/data_table_scan.cc b/src/iceberg/data_table_scan.cc index 5eea6088e9..6db6aa7183 100644 --- a/src/iceberg/data_table_scan.cc +++ b/src/iceberg/data_table_scan.cc @@ -165,8 +165,12 @@ Result DataTableScan::PlanFilesStream() const { ICEBERG_ASSIGN_OR_RAISE(auto specs_by_id, metadata_cache.GetPartitionSpecsById()); SnapshotReader snapshot_reader(snapshot.get()); - ICEBERG_ASSIGN_OR_RAISE(auto data_manifests, snapshot_reader.DataManifests(io_)); - ICEBERG_ASSIGN_OR_RAISE(auto delete_manifests, snapshot_reader.DeleteManifests(io_)); + ICEBERG_ASSIGN_OR_RAISE( + auto data_manifests, + snapshot_reader.DataManifests(context_.manifest_io ? context_.manifest_io : io_)); + ICEBERG_ASSIGN_OR_RAISE( + auto delete_manifests, + snapshot_reader.DeleteManifests(context_.manifest_io ? context_.manifest_io : io_)); if (scan_metrics) { scan_metrics->total_data_manifests->Increment( @@ -184,7 +188,8 @@ Result DataTableScan::PlanFilesStream() const { ICEBERG_ASSIGN_OR_RAISE( auto manifest_group, - ManifestGroup::Make(io_, schema_, specs_by_id, std::move(owned_data_manifests), + ManifestGroup::Make(context_.manifest_io ? context_.manifest_io : io_, schema_, + specs_by_id, std::move(owned_data_manifests), std::move(owned_delete_manifests))); manifest_group->CaseSensitive(context_.case_sensitive) .Select(ScanColumns()) diff --git a/src/iceberg/incremental_append_scan.cc b/src/iceberg/incremental_append_scan.cc index cf01fe8949..189d374eb9 100644 --- a/src/iceberg/incremental_append_scan.cc +++ b/src/iceberg/incremental_append_scan.cc @@ -75,7 +75,9 @@ Result>> IncrementalAppendScan::PlanFi std::unordered_set data_manifests; for (const auto& snapshot : append_snapshots) { SnapshotReader snapshot_reader(snapshot.get()); - ICEBERG_ASSIGN_OR_RAISE(auto manifests, snapshot_reader.DataManifests(io_)); + ICEBERG_ASSIGN_OR_RAISE( + auto manifests, + snapshot_reader.DataManifests(context_.manifest_io ? context_.manifest_io : io_)); std::ranges::copy_if( manifests, std::inserter(data_manifests, data_manifests.end()), [&snapshot_ids](const ManifestFile& manifest) { @@ -93,7 +95,7 @@ Result>> IncrementalAppendScan::PlanFi ICEBERG_ASSIGN_OR_RAISE( auto manifest_group, ManifestGroup::Make( - io_, schema_, specs_by_id, + context_.manifest_io ? context_.manifest_io : io_, schema_, specs_by_id, std::vector(data_manifests.begin(), data_manifests.end()), {})); manifest_group->CaseSensitive(context_.case_sensitive) diff --git a/src/iceberg/incremental_changelog_scan.cc b/src/iceberg/incremental_changelog_scan.cc index 5a1dbba111..3f4dba6a04 100644 --- a/src/iceberg/incremental_changelog_scan.cc +++ b/src/iceberg/incremental_changelog_scan.cc @@ -67,7 +67,8 @@ IncrementalChangelogScan::PlanFiles(std::optional from_snapshot_id_excl if (!operation.has_value() || operation.value() != DataOperation::kReplace) { auto snapshot_reader = std::make_unique(snapshot.get()); ICEBERG_ASSIGN_OR_RAISE(auto delete_manifests, - snapshot_reader->DeleteManifests(io_)); + snapshot_reader->DeleteManifests( + context_.manifest_io ? context_.manifest_io : io_)); if (!delete_manifests.empty()) { return NotSupported( "Delete files are currently not supported in changelog scans"); @@ -93,7 +94,9 @@ IncrementalChangelogScan::PlanFiles(std::optional from_snapshot_id_excl std::vector data_manifests; std::unordered_set seen_manifest_paths; for (const auto& snapshot : changelog_snapshots) { - ICEBERG_ASSIGN_OR_RAISE(auto manifests, snapshot.second->DataManifests(io_)); + ICEBERG_ASSIGN_OR_RAISE(auto manifests, + snapshot.second->DataManifests( + context_.manifest_io ? context_.manifest_io : io_)); for (auto& manifest : manifests) { if (manifest.added_snapshot_id.has_value() && snapshot_ids.contains(manifest.added_snapshot_id.value()) && @@ -111,7 +114,8 @@ IncrementalChangelogScan::PlanFiles(std::optional from_snapshot_id_excl ICEBERG_ASSIGN_OR_RAISE( auto manifest_group, - ManifestGroup::Make(io_, schema_, specs_by_id, std::move(data_manifests), + ManifestGroup::Make(context_.manifest_io ? context_.manifest_io : io_, schema_, + specs_by_id, std::move(data_manifests), /*delete_manifests=*/{})); manifest_group->CaseSensitive(context_.case_sensitive) diff --git a/src/iceberg/table.cc b/src/iceberg/table.cc index a2bf3fada2..15ff8ce5ad 100644 --- a/src/iceberg/table.cc +++ b/src/iceberg/table.cc @@ -22,7 +22,9 @@ #include #include "iceberg/catalog.h" +#include "iceberg/content_cache_internal.h" #include "iceberg/data_table_scan.h" +#include "iceberg/file_io.h" #include "iceberg/incremental_append_scan.h" #include "iceberg/incremental_changelog_scan.h" #include "iceberg/inspect/metadata_table.h" @@ -63,7 +65,8 @@ Result> Table::Make( TableIdentifier identifier, std::shared_ptr metadata, std::string metadata_location, std::shared_ptr io, std::shared_ptr catalog, std::string full_name, - std::shared_ptr reporter, std::unique_ptr labels) { + std::shared_ptr reporter, std::unique_ptr labels, + const std::unordered_map& io_properties) { if (metadata == nullptr) [[unlikely]] { return InvalidArgument("Metadata cannot be null"); } @@ -76,10 +79,12 @@ Result> Table::Make( if (catalog == nullptr) [[unlikely]] { return InvalidArgument("Catalog cannot be null"); } - return std::shared_ptr(new Table(std::move(identifier), std::move(metadata), - std::move(metadata_location), std::move(io), - std::move(catalog), std::move(full_name), - std::move(reporter), std::move(labels))); + auto table = std::shared_ptr
( + new Table(std::move(identifier), std::move(metadata), std::move(metadata_location), + std::move(io), std::move(catalog), std::move(full_name), + std::move(reporter), std::move(labels))); + ICEBERG_RETURN_UNEXPECTED(table->InitializeManifestCache(io_properties)); + return table; } Table::~Table() = default; @@ -93,16 +98,52 @@ Table::Table(TableIdentifier identifier, std::shared_ptr metadata metadata_(std::move(metadata)), metadata_location_(std::move(metadata_location)), io_(std::move(io)), + manifest_io_(io_), catalog_(std::move(catalog)), reporter_(std::move(reporter)), metadata_cache_(std::make_unique(metadata_.get())), labels_(labels && !labels->empty() ? std::move(labels) : nullptr) {} +Status Table::InitializeManifestCache( + const std::unordered_map& io_properties) { + auto properties = io_->properties(); + for (const auto& [key, value] : io_properties) properties[key] = value; + for (const auto& [key, value] : metadata_->properties.configs()) + properties[key] = value; + ICEBERG_ASSIGN_OR_RAISE(auto options, + internal::ContentCacheOptions::FromProperties(properties)); + if (options.enabled) { + content_cache_ = std::make_shared(options); + manifest_io_ = content_cache_->Wrap(io_); + } + return {}; +} + +void Table::ClearManifestCache() { + if (content_cache_) content_cache_->Clear(); +} + +void Table::ReuseManifestCache(const Table& source) { + if (!content_cache_ || !source.content_cache_ || uuid() != source.uuid() || + content_cache_->options() != source.content_cache_->options()) + return; + // A replaced storage context with no comparable properties may represent a different + // endpoint or filesystem. Only preserve bytes when its identity is known to match. + if (io_ != source.io_ && + (io_->properties().empty() || io_->properties() != source.io_->properties())) + return; + content_cache_ = source.content_cache_; + manifest_io_ = content_cache_->Wrap(io_); +} + const std::string& Table::uuid() const { return metadata_->table_uuid; } Status Table::Refresh() { ICEBERG_ASSIGN_OR_RAISE(auto refreshed_table, catalog_->LoadTable(identifier_)); if (metadata_location_ != refreshed_table->metadata_file_location()) { + refreshed_table->ReuseManifestCache(*this); + content_cache_ = std::move(refreshed_table->content_cache_); + manifest_io_ = std::move(refreshed_table->manifest_io_); metadata_ = std::move(refreshed_table->metadata_); metadata_location_ = std::string(refreshed_table->metadata_file_location()); io_ = std::move(refreshed_table->io_); @@ -310,7 +351,8 @@ Result> StagedTable::Make( TableIdentifier identifier, std::shared_ptr metadata, std::string metadata_location, std::shared_ptr io, std::shared_ptr catalog, std::string full_name, - std::shared_ptr reporter) { + std::shared_ptr reporter, + const std::unordered_map& io_properties) { if (metadata == nullptr) [[unlikely]] { return InvalidArgument("Metadata cannot be null"); } @@ -320,10 +362,12 @@ Result> StagedTable::Make( if (catalog == nullptr) [[unlikely]] { return InvalidArgument("Catalog cannot be null"); } - return std::shared_ptr( + auto table = std::shared_ptr( new StagedTable(std::move(identifier), std::move(metadata), std::move(metadata_location), std::move(io), std::move(catalog), std::move(full_name), std::move(reporter), /*labels=*/nullptr)); + ICEBERG_RETURN_UNEXPECTED(table->InitializeManifestCache(io_properties)); + return table; } StagedTable::~StagedTable() = default; @@ -341,10 +385,12 @@ Result> StaticTable::Make( if (io == nullptr) [[unlikely]] { return InvalidArgument("FileIO cannot be null"); } - return std::shared_ptr( + auto table = std::shared_ptr( new StaticTable(std::move(identifier), std::move(metadata), std::move(metadata_location), std::move(io), /*catalog=*/nullptr, std::move(full_name), /*reporter=*/nullptr, /*labels=*/nullptr)); + ICEBERG_RETURN_UNEXPECTED(table->InitializeManifestCache()); + return table; } StaticTable::~StaticTable() = default; diff --git a/src/iceberg/table.h b/src/iceberg/table.h index 5f53f999a3..1bf0331bed 100644 --- a/src/iceberg/table.h +++ b/src/iceberg/table.h @@ -38,6 +38,10 @@ namespace iceberg { +namespace internal { +class ContentCache; +} + /// \brief Represents an Iceberg table /// /// Safe to read from several threads, but not while one of them commits through it @@ -55,12 +59,15 @@ class ICEBERG_EXPORT Table : public std::enable_shared_from_this
{ /// \param[in] reporter Optional metrics reporter for this table. Defaults to nullptr /// (noop). /// \param[in] labels Labels for this table, or nullptr when it has none. + /// \param[in] io_properties Catalog I/O defaults for the table manifest cache. + /// Table properties override these defaults; the budget is per loaded table. static Result> Make( TableIdentifier identifier, std::shared_ptr metadata, std::string metadata_location, std::shared_ptr io, std::shared_ptr catalog, std::string full_name = "", std::shared_ptr reporter = nullptr, - std::unique_ptr labels = nullptr); + std::unique_ptr labels = nullptr, + const std::unordered_map& io_properties = {}); virtual ~Table(); @@ -127,6 +134,16 @@ class ICEBERG_EXPORT Table : public std::enable_shared_from_this
{ /// \brief Returns a FileIO to read and write table data and metadata files const std::shared_ptr& io() const; + /// \brief I/O context for immutable manifest lists and manifests only. + /// Each loaded table has its own optional content cache, shared by its readers. + const std::shared_ptr& manifest_io() const { return manifest_io_; } + + /// \brief Return whether this table caches manifest content. + bool ManifestCacheEnabled() const { return content_cache_ != nullptr; } + + /// \brief Release manifest content retained by this table. + void ClearManifestCache(); + /// \brief Returns the current metadata for this table const std::shared_ptr& metadata() const; @@ -234,6 +251,9 @@ class ICEBERG_EXPORT Table : public std::enable_shared_from_this
{ Result> Inspect(MetadataTableKind kind) const; protected: + Status InitializeManifestCache( + const std::unordered_map& io_properties = {}); + Table(TableIdentifier identifier, std::shared_ptr metadata, std::string metadata_location, std::shared_ptr io, std::shared_ptr catalog, std::string full_name, @@ -245,10 +265,16 @@ class ICEBERG_EXPORT Table : public std::enable_shared_from_this
{ std::shared_ptr metadata_; std::string metadata_location_; std::shared_ptr io_; + std::shared_ptr content_cache_; + std::shared_ptr manifest_io_; std::shared_ptr catalog_; std::shared_ptr reporter_; std::unique_ptr metadata_cache_; const std::unique_ptr labels_; + + private: + friend class Transaction; + void ReuseManifestCache(const Table& source); }; /// \brief A table created by stage-create and not yet committed. @@ -258,7 +284,8 @@ class ICEBERG_EXPORT StagedTable final : public Table { TableIdentifier identifier, std::shared_ptr metadata, std::string metadata_location, std::shared_ptr io, std::shared_ptr catalog, std::string full_name = "", - std::shared_ptr reporter = nullptr); + std::shared_ptr reporter = nullptr, + const std::unordered_map& io_properties = {}); ~StagedTable() override; diff --git a/src/iceberg/table_scan.cc b/src/iceberg/table_scan.cc index a29ee00434..35a146cff8 100644 --- a/src/iceberg/table_scan.cc +++ b/src/iceberg/table_scan.cc @@ -335,6 +335,7 @@ Result>> TableScanBuilder:: auto builder = std::unique_ptr>(new TableScanBuilder( table.metadata(), table.io(), table.full_name(), table.reporter())); + builder->context_.manifest_io = table.manifest_io(); return builder; } diff --git a/src/iceberg/table_scan.h b/src/iceberg/table_scan.h index 3781d88cf2..777a9e3de1 100644 --- a/src/iceberg/table_scan.h +++ b/src/iceberg/table_scan.h @@ -316,6 +316,9 @@ struct ICEBERG_EXPORT TableScanContext { std::string table_name; std::shared_ptr metrics_reporter; + // Table-owned manifest content cache, independent from data-file I/O. + std::shared_ptr manifest_io; + // Validate the context parameters to see if they have conflicts. [[nodiscard]] Status Validate() const; }; diff --git a/src/iceberg/test/CMakeLists.txt b/src/iceberg/test/CMakeLists.txt index a6f3d8ba12..c037d55317 100644 --- a/src/iceberg/test/CMakeLists.txt +++ b/src/iceberg/test/CMakeLists.txt @@ -180,6 +180,7 @@ add_iceberg_test(util_test lazy_test.cc location_util_test.cc math_util_internal_test.cc + metadata_cache_test.cc executor_util_test.cc roaring_position_bitmap_test.cc position_delete_index_test.cc diff --git a/src/iceberg/test/arrow_io_test.cc b/src/iceberg/test/arrow_io_test.cc index 7bc9ebba53..3ed0e39ba4 100644 --- a/src/iceberg/test/arrow_io_test.cc +++ b/src/iceberg/test/arrow_io_test.cc @@ -24,16 +24,23 @@ #include #include +#include #include +#include #include #include #include #include "iceberg/arrow/arrow_io_internal.h" #include "iceberg/arrow/arrow_register.h" +#include "iceberg/content_cache_internal.h" +#include "iceberg/file_io.h" #include "iceberg/file_io_registry.h" #include "iceberg/resolving_file_io.h" +#include "iceberg/table.h" +#include "iceberg/table_metadata.h" #include "iceberg/test/matchers.h" +#include "iceberg/test/mock_catalog.h" #include "iceberg/test/std_io.h" #include "iceberg/test/temp_file_test_base.h" @@ -50,6 +57,74 @@ struct CloseState { bool closed = false; }; +class TrackingLocalFileSystem : public ::arrow::fs::LocalFileSystem { + public: + using ::arrow::fs::LocalFileSystem::OpenInputFile; + + ::arrow::Result> OpenInputFile( + const ::arrow::fs::FileInfo& info) override { + ARROW_ASSIGN_OR_RAISE(auto input, + ::arrow::fs::LocalFileSystem::OpenInputFile(info.path())); + opened_inputs.emplace_back(input); + return input; + } + + // Strong references keep every opened file alive, so tests can tell an explicit + // Close() apart from the file simply going out of scope. + std::vector> opened_inputs; +}; + +class SizeFailureArrowFile : public ::arrow::io::RandomAccessFile { + public: + explicit SizeFailureArrowFile(bool fail_close) : fail_close_(fail_close) {} + ::arrow::Result GetSize() override { + return ::arrow::Status::IOError("size failed"); + } + ::arrow::Status Close() override { + ++close_calls; + closed_ = true; + return fail_close_ ? ::arrow::Status::IOError("close failed") : ::arrow::Status::OK(); + } + bool closed() const override { return closed_; } + ::arrow::Result Tell() const override { return 0; } + ::arrow::Status Seek(int64_t) override { return ::arrow::Status::OK(); } + ::arrow::Result Read(int64_t, void*) override { return 0; } + ::arrow::Result> Read(int64_t) override { + return ::arrow::Buffer::FromString(""); + } + int close_calls = 0; + + private: + bool fail_close_; + bool closed_ = false; +}; + +class SizeFailureFileSystem : public ::arrow::fs::LocalFileSystem { + public: + using ::arrow::fs::LocalFileSystem::OpenInputFile; + explicit SizeFailureFileSystem(bool fail_close) + : input(std::make_shared(fail_close)) {} + ::arrow::Result> OpenInputFile( + const ::arrow::fs::FileInfo&) override { + return input; + } + std::shared_ptr input; +}; + +TEST(ArrowFileIOTest, SizeClosesTemporaryInputOnFailure) { + for (bool fail_close : {false, true}) { + SCOPED_TRACE(fail_close); + auto fs = std::make_shared(fail_close); + auto io = std::make_shared(fs); + ICEBERG_UNWRAP_OR_FAIL(auto input, io->NewInputFile("manifest.avro")); + auto result = input->Size(); + EXPECT_THAT(result, HasErrorMessage("size failed")); + if (fail_close) EXPECT_THAT(result, HasErrorMessage("close failed")); + EXPECT_TRUE(fs->input->closed()); + EXPECT_EQ(fs->input->close_calls, 1); + } +} + class ReadFailureInputStream : public SeekableInputStream { public: explicit ReadFailureInputStream(std::shared_ptr state) @@ -356,6 +431,51 @@ TEST_F(LocalFileIOTest, ReadWriteFile) { EXPECT_THAT(read_res, HasValue(::testing::Eq("hello world"))); } +TEST_F(LocalFileIOTest, SizeReleasesTemporaryArrowFile) { + auto fs = std::make_shared(); + auto file_io = std::make_shared(fs); + ASSERT_THAT(file_io->WriteFile(temp_filepath_, "hello"), IsOk()); + ICEBERG_UNWRAP_OR_FAIL(auto input_file, file_io->NewInputFile(temp_filepath_)); + + EXPECT_THAT(input_file->Size(), HasValue(::testing::Eq(5))); + ASSERT_EQ(fs->opened_inputs.size(), 1); + EXPECT_TRUE(fs->opened_inputs.front()->closed()); + + EXPECT_THAT(input_file->Size(), HasValue(::testing::Eq(5))); + EXPECT_EQ(fs->opened_inputs.size(), 1); + ICEBERG_UNWRAP_OR_FAIL(auto stream, input_file->Open()); + ASSERT_EQ(fs->opened_inputs.size(), 2); + EXPECT_FALSE(fs->opened_inputs.back()->closed()); + std::array data; + ASSERT_THAT(stream->ReadFully(0, data), IsOk()); + EXPECT_EQ(std::string(reinterpret_cast(data.data()), data.size()), + "hello"); + ASSERT_THAT(stream->Close(), IsOk()); + EXPECT_TRUE(fs->opened_inputs.back()->closed()); +} + +TEST_F(LocalFileIOTest, CachedInputCreationReleasesTemporaryArrowFile) { + auto fs = std::make_shared(); + auto file_io = std::make_shared(fs); + ASSERT_THAT(file_io->WriteFile(temp_filepath_, "hello"), IsOk()); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + Table::Make({.ns = {}, .name = "test"}, std::make_shared(), + "metadata.json", file_io, std::make_shared(), "", nullptr, + nullptr, + {{std::string(internal::ContentCacheOptions::kEnabled), "true"}})); + + ICEBERG_UNWRAP_OR_FAIL(auto input_file, + table->manifest_io()->NewInputFile(temp_filepath_)); + ASSERT_EQ(fs->opened_inputs.size(), 1); + EXPECT_TRUE(fs->opened_inputs.front()->closed()); + // Opening loads the content into the cache and closes the underlying file. + ICEBERG_UNWRAP_OR_FAIL(auto stream, input_file->Open()); + ASSERT_EQ(fs->opened_inputs.size(), 2); + EXPECT_TRUE(fs->opened_inputs.back()->closed()); + ASSERT_THAT(stream->Close(), IsOk()); +} + TEST_F(LocalFileIOTest, DeleteFile) { auto write_res = file_io_->WriteFile(temp_filepath_, "hello world"); EXPECT_THAT(write_res, IsOk()); @@ -446,6 +566,26 @@ TEST(ArrowFileIOTest, OutputStoredLengthAfterClose) { EXPECT_THAT(output->StoredLength(), HasValue(::testing::Eq(3))); } +TEST(ArrowFileIOTest, OversizedCachedInputRetainsNativeArrowStream) { + std::shared_ptr file_io = arrow::ArrowFileSystemFileIO::MakeMockFileIO(); + ASSERT_THAT(file_io->WriteFile("input", "manifest"), IsOk()); + + ICEBERG_UNWRAP_OR_FAIL(auto uncached, arrow::OpenArrowInputStream(file_io, "input")); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + Table::Make( + {.ns = {}, .name = "test"}, std::make_shared(), "metadata.json", + file_io, std::make_shared(), "", nullptr, nullptr, + {{std::string(internal::ContentCacheOptions::kEnabled), "true"}, + {std::string(internal::ContentCacheOptions::kMaxContentLength), "4"}})); + ICEBERG_UNWRAP_OR_FAIL(auto oversized, + arrow::OpenArrowInputStream(table->manifest_io(), "input")); + + const auto& oversized_stream = *oversized; + const auto& uncached_stream = *uncached; + EXPECT_TRUE(typeid(oversized_stream) == typeid(uncached_stream)); +} + TEST_F(LocalFileIOTest, ResolvesForeignSchemeToUnderlyingPath) { ASSERT_THAT(file_io_->WriteFile(temp_filepath_, "hello world"), IsOk()); diff --git a/src/iceberg/test/cache_test.cc b/src/iceberg/test/cache_test.cc index fbc29ab9c4..c406febdd6 100644 --- a/src/iceberg/test/cache_test.cc +++ b/src/iceberg/test/cache_test.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include @@ -43,6 +44,116 @@ TEST(LruCacheTest, Eviction) { EXPECT_EQ(*cache.Find(1), "one"); ASSERT_NE(cache.Find(3), nullptr); EXPECT_EQ(*cache.Find(3), "three"); + EXPECT_EQ(cache.size(), 2); + EXPECT_EQ(cache.total_weight(), 2); +} + +struct StringLengthWeigher { + int64_t operator()(int32_t /*key*/, const std::string& value) const { + return static_cast(value.size()); + } +}; + +using WeightedCache = LruCache; + +TEST(LruCacheTest, WeightedEviction) { + WeightedCache cache(10); + + EXPECT_TRUE(cache.Replace(1, "aaaa").first); + EXPECT_TRUE(cache.Replace(2, "bbb").first); + ASSERT_NE(cache.Find(1), nullptr); + EXPECT_EQ(cache.total_weight(), 7); + + // 7 + 6 exceeds 10, so the least recently used entry 2 is evicted. + auto [inserted, value] = cache.Replace(3, "cccccc"); + EXPECT_TRUE(inserted); + ASSERT_NE(value, nullptr); + EXPECT_EQ(*value, "cccccc"); + EXPECT_EQ(cache.Find(2), nullptr); + ASSERT_NE(cache.Find(1), nullptr); + EXPECT_EQ(*cache.Find(1), "aaaa"); + EXPECT_EQ(cache.size(), 2); + EXPECT_EQ(cache.total_weight(), 10); +} + +TEST(LruCacheTest, WeightedEvictionRemovesMultipleEntries) { + WeightedCache cache(10); + cache.Replace(1, "aaa"); + cache.Replace(2, "bbb"); + cache.Replace(3, "ccc"); + + // Making room for 7 requires evicting the two least recently used entries. + ASSERT_NE(cache.Replace(4, "ddddddd").second, nullptr); + EXPECT_EQ(cache.Find(1), nullptr); + EXPECT_EQ(cache.Find(2), nullptr); + EXPECT_NE(cache.Find(3), nullptr); + EXPECT_NE(cache.Find(4), nullptr); + EXPECT_EQ(cache.size(), 2); + EXPECT_EQ(cache.total_weight(), 10); +} + +TEST(LruCacheTest, OversizedValueIsNotCached) { + WeightedCache cache(5); + cache.Replace(1, "aa"); + + auto [inserted, value] = cache.Replace(2, "bbbbbb"); + EXPECT_FALSE(inserted); + EXPECT_EQ(value, nullptr); + EXPECT_EQ(cache.Find(2), nullptr); + // Rejecting the oversized value must not evict anything. + ASSERT_NE(cache.Find(1), nullptr); + EXPECT_EQ(cache.size(), 1); + EXPECT_EQ(cache.total_weight(), 2); + + // A value exactly at the maximum weight fits. + ASSERT_NE(cache.Replace(3, "ccccc").second, nullptr); + EXPECT_EQ(cache.Find(1), nullptr); + EXPECT_EQ(cache.total_weight(), 5); +} + +TEST(LruCacheTest, ReplaceReweighsEntry) { + WeightedCache cache(10); + cache.Replace(1, "aaa"); + cache.Replace(2, "bbb"); + + // Growing entry 2 from 3 to 7 still fits next to entry 1. + EXPECT_FALSE(cache.Replace(2, "bbbbbbb").first); + EXPECT_EQ(cache.size(), 2); + EXPECT_EQ(cache.total_weight(), 10); + + // Growing entry 1 to 8 makes it most recently used and evicts entry 2. + auto [inserted, value] = cache.Replace(1, "aaaaaaaa"); + EXPECT_FALSE(inserted); + ASSERT_NE(value, nullptr); + EXPECT_EQ(*value, "aaaaaaaa"); + EXPECT_EQ(cache.Find(2), nullptr); + EXPECT_EQ(cache.size(), 1); + EXPECT_EQ(cache.total_weight(), 8); + + // Shrinking releases weight. + cache.Replace(1, "a"); + EXPECT_EQ(cache.total_weight(), 1); + + // Replacing with an oversized value removes the existing entry. + std::tie(inserted, value) = cache.Replace(1, std::string(11, 'a')); + EXPECT_FALSE(inserted); + EXPECT_EQ(value, nullptr); + EXPECT_EQ(cache.Find(1), nullptr); + EXPECT_EQ(cache.size(), 0); + EXPECT_EQ(cache.total_weight(), 0); +} + +TEST(LruCacheTest, ClearResetsWeight) { + WeightedCache cache(10); + cache.Replace(1, "aaaa"); + cache.Replace(2, "bbbb"); + + cache.Clear(); + EXPECT_EQ(cache.size(), 0); + EXPECT_EQ(cache.total_weight(), 0); + + ASSERT_NE(cache.Replace(3, std::string(10, 'c')).second, nullptr); + EXPECT_EQ(cache.total_weight(), 10); } TEST(MemoizeLruTest, Caches) { diff --git a/src/iceberg/test/fast_append_test.cc b/src/iceberg/test/fast_append_test.cc index 491f977f34..fda41a0e55 100644 --- a/src/iceberg/test/fast_append_test.cc +++ b/src/iceberg/test/fast_append_test.cc @@ -33,6 +33,7 @@ #include #include +#include #include #include diff --git a/src/iceberg/test/manifest_reader_test.cc b/src/iceberg/test/manifest_reader_test.cc index 6a201bd569..311398f1ff 100644 --- a/src/iceberg/test/manifest_reader_test.cc +++ b/src/iceberg/test/manifest_reader_test.cc @@ -24,16 +24,22 @@ #include #include #include +#include #include #include #include +#include #include #include +#include "iceberg/arrow/arrow_io_internal.h" #include "iceberg/arrow/arrow_io_util.h" #include "iceberg/avro/avro_register.h" +#include "iceberg/content_cache_internal.h" +#include "iceberg/data_table_scan.h" #include "iceberg/expression/expressions.h" +#include "iceberg/file_io.h" #include "iceberg/file_writer.h" #include "iceberg/manifest/manifest_entry.h" #include "iceberg/manifest/manifest_list.h" @@ -41,12 +47,36 @@ #include "iceberg/partition_spec.h" #include "iceberg/schema.h" #include "iceberg/schema_internal.h" +#include "iceberg/table.h" +#include "iceberg/table_metadata.h" +#include "iceberg/table_scan.h" #include "iceberg/test/matchers.h" +#include "iceberg/test/mock_catalog.h" #include "iceberg/transform.h" #include "iceberg/type.h" namespace iceberg { +namespace { + +// Counts how often each path is opened for reading. FileSystem::OpenInputFile(FileInfo) +// forwards to the path overload, so every open is counted exactly once. +class CountingMockFileSystem : public ::arrow::fs::internal::MockFileSystem { + public: + using ::arrow::fs::internal::MockFileSystem::MockFileSystem; + using ::arrow::fs::internal::MockFileSystem::OpenInputFile; + + ::arrow::Result> OpenInputFile( + const std::string& path) override { + ++open_counts[path]; + return ::arrow::fs::internal::MockFileSystem::OpenInputFile(path); + } + + std::unordered_map open_counts; +}; + +} // namespace + class TestManifestReader : public testing::TestWithParam { protected: void SetUp() override { @@ -765,6 +795,130 @@ TEST(ManifestReaderStaticTest, WithStatsColumnsHandlesEmptyAndSelectAll) { testing::ElementsAre(Schema::kAllColumns)); } +TEST_P(TestManifestReader, ReadsManifestListAndManifestFromCachedContent) { + const auto version = GetParam(); + auto fs = std::make_shared(std::chrono::system_clock::now()); + file_io_ = std::make_shared(fs); + auto table_metadata = std::make_shared(); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + Table::Make({.ns = {}, .name = "test"}, table_metadata, "metadata.json", file_io_, + std::make_shared(), "", nullptr, nullptr, + {{std::string(internal::ContentCacheOptions::kEnabled), "true"}})); + + std::vector entries; + entries.push_back(MakeEntry( + ManifestStatus::kAdded, /*snapshot_id=*/1000L, + MakeDataFile("/path/to/data-a.parquet", PartitionValues({Literal::Int(0)})))); + entries.push_back(MakeEntry( + ManifestStatus::kAdded, /*snapshot_id=*/1000L, + MakeDataFile("/path/to/data-b.parquet", PartitionValues({Literal::Int(1)})))); + auto written_manifest = WriteManifest(version, /*snapshot_id=*/1000L, entries); + + const std::string manifest_list_path = std::format("snap-{}.avro", MakeManifestPath()); + ICEBERG_UNWRAP_OR_FAIL( + auto list_writer, + ManifestListWriter::MakeWriter(version, /*snapshot_id=*/1000L, + /*parent_snapshot_id=*/std::nullopt, + manifest_list_path, file_io_, + /*sequence_number=*/1, /*first_row_id=*/0L)); + ASSERT_THAT(list_writer->Add(written_manifest), IsOk()); + ASSERT_THAT(list_writer->Close(), IsOk()); + + auto read_manifest_list = [&]() -> std::vector { + auto reader = ManifestListReader::Make(manifest_list_path, table->manifest_io()); + EXPECT_THAT(reader, IsOk()); + if (!reader.has_value()) { + return {}; + } + auto files = reader.value()->Files(); + EXPECT_THAT(files, IsOk()); + return files.has_value() ? std::move(files).value() : std::vector{}; + }; + auto read_data_file_paths = [&](const ManifestFile& manifest) { + std::vector paths; + auto reader = ManifestReader::Make(manifest, table->manifest_io(), schema_, spec_); + EXPECT_THAT(reader, IsOk()); + if (!reader.has_value()) { + return paths; + } + auto read_entries = reader.value()->Entries(); + EXPECT_THAT(read_entries, IsOk()); + if (read_entries.has_value()) { + for (const auto& entry : read_entries.value()) { + EXPECT_EQ(entry.status, ManifestStatus::kAdded); + EXPECT_EQ(entry.snapshot_id, 1000L); + paths.push_back(entry.data_file->file_path); + } + } + return paths; + }; + const std::vector expected_paths = {"/path/to/data-a.parquet", + "/path/to/data-b.parquet"}; + + // First read: both files are loaded from the file system into the cache. + auto first_files = read_manifest_list(); + ASSERT_EQ(first_files.size(), 1); + EXPECT_EQ(first_files[0].manifest_path, written_manifest.manifest_path); + EXPECT_EQ(first_files[0].added_snapshot_id, 1000L); + EXPECT_EQ(read_data_file_paths(first_files[0]), expected_paths); + const int list_opens = fs->open_counts[manifest_list_path]; + const int manifest_opens = fs->open_counts[written_manifest.manifest_path]; + EXPECT_GT(list_opens, 0); + EXPECT_GT(manifest_opens, 0); + + // Second read: the same rows are decoded from cached content without opening either + // file again. + auto second_files = read_manifest_list(); + ASSERT_EQ(second_files.size(), 1); + EXPECT_EQ(second_files[0], first_files[0]); + EXPECT_EQ(read_data_file_paths(second_files[0]), expected_paths); + EXPECT_EQ(fs->open_counts[manifest_list_path], list_opens); + EXPECT_EQ(fs->open_counts[written_manifest.manifest_path], manifest_opens); + + // The public scan builder must carry this same table-owned cache into both + // manifest-list and manifest readers. Each builder captures its own read context. + table_metadata->format_version = version; + table_metadata->schemas = {schema_}; + table_metadata->current_schema_id = schema_->schema_id(); + table_metadata->partition_specs = {spec_}; + table_metadata->default_spec_id = spec_->spec_id(); + table_metadata->current_snapshot_id = 1000; + table_metadata->snapshots = {std::make_shared(Snapshot{ + .snapshot_id = 1000, + .sequence_number = 1, + .manifest_list = manifest_list_path, + .summary = {{"operation", "append"}}, + .schema_id = schema_->schema_id(), + })}; + for (int iteration = 0; iteration < 2; ++iteration) { + ICEBERG_UNWRAP_OR_FAIL(auto builder, table->NewScan()); + ICEBERG_UNWRAP_OR_FAIL(auto scan, builder->Build()); + EXPECT_EQ(scan->io(), file_io_); + ICEBERG_UNWRAP_OR_FAIL(auto tasks, scan->PlanFiles()); + ASSERT_EQ(tasks.size(), 2); + EXPECT_EQ(tasks[0]->data_file()->file_path, expected_paths[0]); + EXPECT_EQ(tasks[1]->data_file()->file_path, expected_paths[1]); + } + EXPECT_EQ(fs->open_counts[manifest_list_path], list_opens); + EXPECT_EQ(fs->open_counts[written_manifest.manifest_path], manifest_opens); + + ICEBERG_UNWRAP_OR_FAIL(auto captured_builder, table->NewScan()); + table.reset(); + ICEBERG_UNWRAP_OR_FAIL(auto captured_scan, captured_builder->Build()); + ICEBERG_UNWRAP_OR_FAIL(auto captured_tasks, captured_scan->PlanFiles()); + EXPECT_EQ(captured_tasks.size(), 2); + EXPECT_EQ(fs->open_counts[manifest_list_path], list_opens); + EXPECT_EQ(fs->open_counts[written_manifest.manifest_path], manifest_opens); + + // Control: without caching, reading the manifest opens it again. + ICEBERG_UNWRAP_OR_FAIL( + table, Table::Make({.ns = {}, .name = "test"}, table_metadata, "metadata.json", + file_io_, std::make_shared())); + EXPECT_EQ(read_data_file_paths(second_files[0]), expected_paths); + EXPECT_GT(fs->open_counts[written_manifest.manifest_path], manifest_opens); +} + INSTANTIATE_TEST_SUITE_P(ManifestReaderVersions, TestManifestReader, testing::Values(1, 2, 3)); diff --git a/src/iceberg/test/metadata_cache_test.cc b/src/iceberg/test/metadata_cache_test.cc new file mode 100644 index 0000000000..d98bf16299 --- /dev/null +++ b/src/iceberg/test/metadata_cache_test.cc @@ -0,0 +1,668 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "iceberg/content_cache_internal.h" +#include "iceberg/file_io.h" +#include "iceberg/file_io_registry.h" +#include "iceberg/file_reader.h" +#include "iceberg/schema.h" +#include "iceberg/table.h" +#include "iceberg/table_metadata.h" +#include "iceberg/test/matchers.h" +#include "iceberg/test/mock_catalog.h" +#include "iceberg/test/mock_io.h" +#include "iceberg/util/macros.h" + +namespace iceberg { +namespace { +using internal::ContentCacheOptions; + +class CountingInputStream : public SeekableInputStream { + public: + CountingInputStream(std::unique_ptr stream, int* read_count) + : stream_(std::move(stream)), read_count_(read_count) {} + + Result Position() const override { return stream_->Position(); } + Status Seek(int64_t position) override { return stream_->Seek(position); } + + Result Read(std::span out) override { + ++*read_count_; + return stream_->Read(out); + } + + Status ReadFully(int64_t position, std::span out) override { + ++*read_count_; + return stream_->ReadFully(position, out); + } + + Status Close() override { return stream_->Close(); } + + private: + std::unique_ptr stream_; + int* read_count_; +}; + +class CountingInputFile : public InputFile { + public: + CountingInputFile(std::unique_ptr input_file, int* open_count, + int* size_count, int* read_count, int* failing_opens) + : input_file_(std::move(input_file)), + open_count_(open_count), + size_count_(size_count), + read_count_(read_count), + failing_opens_(failing_opens) {} + + std::string_view location() const override { return input_file_->location(); } + + Result Size() const override { + ++*size_count_; + return input_file_->Size(); + } + + Result> Open() override { + ++*open_count_; + if (*failing_opens_ > 0) { + --*failing_opens_; + return NotFound("Simulated open failure"); + } + ICEBERG_ASSIGN_OR_RAISE(auto stream, input_file_->Open()); + return std::make_unique(std::move(stream), read_count_); + } + + private: + std::unique_ptr input_file_; + int* open_count_; + int* size_count_; + int* read_count_; + int* failing_opens_; +}; + +class CountingMockFileIO : public MockFileIO { + public: + Result> NewInputFile(std::string file_location) override { + ICEBERG_ASSIGN_OR_RAISE(auto input_file, + MockFileIO::NewInputFile(std::move(file_location))); + return std::make_unique(std::move(input_file), &open_count, + &size_count, &read_count, &failing_opens); + } + + int open_count = 0; + int size_count = 0; + int read_count = 0; + // Number of upcoming Open calls that fail with kNotFound. + int failing_opens = 0; +}; + +// Create independently configured tables sharing the same storage FileIO. +Result> MakeCachedTable( + FileIO& io, const std::unordered_map& properties = {}, + std::string metadata_location = "metadata.json") { + auto metadata = std::make_shared(); + metadata->table_uuid = "cache-test-table"; + auto storage = std::shared_ptr(&io, [](FileIO*) {}); + return Table::Make({.ns = {}, .name = "test"}, std::move(metadata), + std::move(metadata_location), std::move(storage), + std::make_shared(), "", nullptr, nullptr, properties); +} + +// Read a whole immutable file through a table's manifest I/O context. +Result ReadCached(FileIO& file_io, const std::string& location) { + ICEBERG_ASSIGN_OR_RAISE(auto input_file, file_io.NewInputFile(location)); + ICEBERG_ASSIGN_OR_RAISE(auto size, input_file->Size()); + ICEBERG_ASSIGN_OR_RAISE(auto stream, input_file->Open()); + std::string content(static_cast(size), '\0'); + ICEBERG_RETURN_UNEXPECTED( + stream->ReadFully(/*position=*/0, std::as_writable_bytes(std::span(content)))); + ICEBERG_RETURN_UNEXPECTED(stream->Close()); + return content; +} + +class NormalizingMockFileIO : public CountingMockFileIO { + public: + Result> NewInputFile(std::string file_location) override { + constexpr std::string_view kAliasPrefix = "alias://"; + if (file_location.starts_with(kAliasPrefix)) { + file_location.erase(0, kAliasPrefix.size()); + } + return CountingMockFileIO::NewInputFile(std::move(file_location)); + } +}; + +struct FailingOpenState { + int open_count = 0; + Error first_error; + Error fallback_error; +}; + +class FailingOpenInputFile : public InputFile { + public: + explicit FailingOpenInputFile(std::shared_ptr state) + : state_(std::move(state)) {} + + std::string_view location() const override { return "manifest.avro"; } + Result Size() const override { return 8; } + + Result> Open() override { + ++state_->open_count; + if (state_->open_count == 1) { + return unexpected(state_->first_error); + } + return unexpected(state_->fallback_error); + } + + private: + std::shared_ptr state_; +}; + +class FailingOpenFileIO : public FileIO { + public: + explicit FailingOpenFileIO(std::shared_ptr state) + : state_(std::move(state)) {} + + Result> NewInputFile( + std::string /*file_location*/) override { + return std::make_unique(state_); + } + + private: + std::shared_ptr state_; +}; + +TEST(MetadataCacheTest, TableReusesCachedContentAcrossInputFiles) { + CountingMockFileIO file_io; + file_io.AddFile("manifest.avro", "manifest"); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(file_io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); + + auto first_file = table->manifest_io()->NewInputFile("manifest.avro"); + ASSERT_THAT(first_file, IsOk()); + auto first_stream = first_file.value()->Open(); + ASSERT_THAT(first_stream, IsOk()); + ASSERT_THAT(first_stream.value()->Close(), IsOk()); + + auto second_file = table->manifest_io()->NewInputFile("manifest.avro"); + ASSERT_THAT(second_file, IsOk()); + auto second_stream = second_file.value()->Open(); + ASSERT_THAT(second_stream, IsOk()); + ASSERT_THAT(second_stream.value()->Close(), IsOk()); + + EXPECT_EQ(file_io.open_count, 1); +} + +TEST(MetadataCacheTest, TableUsesNormalizedLocationForCacheHits) { + NormalizingMockFileIO file_io; + file_io.AddFile("manifest.avro", "manifest"); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(file_io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); + + for (const auto* location : + {"alias://manifest.avro", "alias://manifest.avro", "manifest.avro"}) { + ICEBERG_UNWRAP_OR_FAIL(auto input_file, table->manifest_io()->NewInputFile(location)); + EXPECT_EQ(input_file->location(), "manifest.avro"); + ICEBERG_UNWRAP_OR_FAIL(auto stream, input_file->Open()); + ASSERT_THAT(stream->Close(), IsOk()); + } + + EXPECT_EQ(file_io.size_count, 1); + EXPECT_EQ(file_io.open_count, 1); +} + +TEST(MetadataCacheTest, CachedInputFileDoesNotRetryNonIOErrors) { + auto state = std::make_shared(FailingOpenState{ + .first_error = {.kind = ErrorKind::kNotFound, .message = "missing"}, + .fallback_error = {.kind = ErrorKind::kIOError, .message = "unexpected retry"}, + }); + FailingOpenFileIO file_io(state); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(file_io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); + ICEBERG_UNWRAP_OR_FAIL(auto input_file, + table->manifest_io()->NewInputFile("manifest.avro")); + + EXPECT_THAT(input_file->Open(), IsError(ErrorKind::kNotFound)); + EXPECT_EQ(state->open_count, 1); +} + +TEST(MetadataCacheTest, CachedInputFilePreservesReadAheadAndFallbackErrors) { + auto state = std::make_shared(FailingOpenState{ + .first_error = {.kind = ErrorKind::kIOError, .message = "read-ahead failed"}, + .fallback_error = {.kind = ErrorKind::kIOError, .message = "open failed"}, + }); + FailingOpenFileIO file_io(state); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(file_io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); + ICEBERG_UNWRAP_OR_FAIL(auto input_file, + table->manifest_io()->NewInputFile("manifest.avro")); + + auto result = input_file->Open(); + EXPECT_THAT(result, IsError(ErrorKind::kIOError)); + EXPECT_THAT(result, HasErrorMessage("read-ahead failed")); + EXPECT_THAT(result, HasErrorMessage("fallback open failed: open failed")); + EXPECT_EQ(state->open_count, 2); +} + +TEST(MetadataCacheTest, TableDoesNotCacheLoadFailures) { + CountingMockFileIO file_io; + file_io.AddFile("manifest.avro", "manifest"); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(file_io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); + file_io.failing_opens = 1; + + EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + IsError(ErrorKind::kNotFound)); + EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + HasValue(::testing::Eq("manifest"))); + EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + HasValue(::testing::Eq("manifest"))); + // The failed open, then the successful load. The last read comes from the cache. + EXPECT_EQ(file_io.open_count, 2); +} + +TEST(MetadataCacheTest, TableReadsUncacheableFilesDirectly) { + for (auto limit : + {ContentCacheOptions::kMaxTotalBytes, ContentCacheOptions::kMaxContentLength}) { + SCOPED_TRACE(limit); + CountingMockFileIO file_io; + file_io.AddFile("manifest.avro", "manifest"); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(file_io, {{std::string(ContentCacheOptions::kEnabled), "true"}, + {std::string(limit), "4"}})); + + // The 8-byte file can never be cached, so opening it must not load the whole file + // into memory first. + ICEBERG_UNWRAP_OR_FAIL(auto input_file, + table->manifest_io()->NewInputFile("manifest.avro")); + ICEBERG_UNWRAP_OR_FAIL(auto stream, input_file->Open()); + EXPECT_EQ(file_io.read_count, 0); + ASSERT_THAT(stream->Close(), IsOk()); + + EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + HasValue(::testing::Eq("manifest"))); + EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + HasValue(::testing::Eq("manifest"))); + EXPECT_EQ(file_io.open_count, 3); + } +} + +TEST(MetadataCacheTest, TableEvictsLeastRecentlyUsedContentByBytes) { + CountingMockFileIO file_io; + file_io.AddFile("a", "aa"); + file_io.AddFile("b", "bbb"); + file_io.AddFile("c", "cccc"); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(file_io, + {{std::string(ContentCacheOptions::kEnabled), "true"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "6"}})); + + // Return the number of underlying opens after reading `location`. + auto read = [&file_io, &table](const std::string& location) { + EXPECT_THAT(ReadCached(*table->manifest_io(), location), IsOk()); + return file_io.open_count; + }; + EXPECT_EQ(read("a"), 1); + EXPECT_EQ(read("b"), 2); + EXPECT_EQ(read("a"), 2); // Cached, and now the most recently used. + EXPECT_EQ(read("c"), 3); // 2 + 3 + 4 bytes exceed 6, so "b" is evicted. + EXPECT_EQ(read("a"), 3); + EXPECT_EQ(read("c"), 3); + EXPECT_EQ(read("b"), 4); +} + +TEST(MetadataCacheTest, TableClearsCachedContent) { + CountingMockFileIO file_io; + file_io.AddFile("snap.avro", "manifest-list"); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(file_io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); + ASSERT_THAT(ReadCached(*table->manifest_io(), "snap.avro"), IsOk()); + + table->ClearManifestCache(); + + ASSERT_THAT(ReadCached(*table->manifest_io(), "snap.avro"), IsOk()); + ASSERT_THAT(ReadCached(*table->manifest_io(), "snap.avro"), IsOk()); + EXPECT_EQ(file_io.open_count, 2); +} + +TEST(MetadataCacheTest, TableConcurrentReadsReturnFileContent) { + // CountingMockFileIO's counters are not thread-safe, so use the plain mock. + MockFileIO file_io; + constexpr int kLocations = 8; + std::vector contents; + for (int id = 0; id < kLocations; ++id) { + contents.emplace_back(static_cast(id % 4 + 1), static_cast('a' + id)); + file_io.AddFile(std::to_string(id), contents.back()); + } + // The files add up to 20 bytes, so reads keep evicting from the 16-byte cache. + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(file_io, + {{std::string(ContentCacheOptions::kEnabled), "true"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "16"}})); + + constexpr int kThreads = 4; + constexpr int kIterations = 1000; + std::atomic mismatch = false; + std::vector> workers; + for (int thread = 0; thread < kThreads; ++thread) { + workers.push_back(std::async(std::launch::async, [&, thread] { + for (int i = 0; i < kIterations; ++i) { + const int id = (thread + i) % kLocations; + auto content = ReadCached(*table->manifest_io(), std::to_string(id)); + if (!content.has_value() || content.value() != contents[id]) { + mismatch.store(true, std::memory_order_relaxed); + } + } + })); + } + for (auto& worker : workers) { + worker.get(); + } + + EXPECT_FALSE(mismatch.load(std::memory_order_relaxed)); +} + +TEST(MetadataCacheTest, ParsesJavaCompatibleProperties) { + std::unordered_map properties = { + {std::string(ContentCacheOptions::kEnabled), "true"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "200"}, + {std::string(ContentCacheOptions::kMaxContentLength), "50"}, + }; + + auto options = ContentCacheOptions::FromProperties(properties); + + ASSERT_THAT(options, IsOk()); + EXPECT_TRUE(options->enabled); + EXPECT_EQ(options->max_total_bytes, 200); + EXPECT_EQ(options->max_content_length, 50); +} + +TEST(MetadataCacheTest, ParsesEnabledPropertyIgnoringCase) { + for (const auto* value : {"true", "TRUE", "True", "tRuE"}) { + SCOPED_TRACE(value); + ICEBERG_UNWRAP_OR_FAIL(auto options, + ContentCacheOptions::FromProperties( + {{std::string(ContentCacheOptions::kEnabled), value}})); + EXPECT_TRUE(options.enabled); + } +} + +TEST(MetadataCacheTest, TreatsOtherEnabledPropertyValuesAsFalseLikeJava) { + for (const auto* value : + {"false", "FALSE", "False", "fAlSe", "", "yes", "1", "ture", " true "}) { + SCOPED_TRACE(value); + ICEBERG_UNWRAP_OR_FAIL(auto options, + ContentCacheOptions::FromProperties( + {{std::string(ContentCacheOptions::kEnabled), value}})); + EXPECT_FALSE(options.enabled); + } +} + +TEST(MetadataCacheTest, UsesJavaCompatibleDefaults) { + auto options = ContentCacheOptions::FromProperties( + {{std::string(ContentCacheOptions::kEnabled), "true"}}); + + ASSERT_THAT(options, IsOk()); + EXPECT_TRUE(options->enabled); + EXPECT_EQ(options->max_total_bytes, 100 * 1024 * 1024); + EXPECT_EQ(options->max_content_length, 8 * 1024 * 1024); +} + +TEST(MetadataCacheTest, InvalidNumericPropertyErrorIncludesPropertyName) { + auto options = ContentCacheOptions::FromProperties( + {{std::string(ContentCacheOptions::kEnabled), "true"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "invalid"}}); + + EXPECT_THAT(options, HasErrorMessage(std::string(ContentCacheOptions::kMaxTotalBytes))); +} + +TEST(MetadataCacheTest, ParsesTuningPropertiesWhenDisabled) { + auto options = ContentCacheOptions::FromProperties( + {{std::string(ContentCacheOptions::kEnabled), "false"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "1024"}}); + + ASSERT_THAT(options, IsOk()); + EXPECT_FALSE(options->enabled); + EXPECT_EQ(options->max_total_bytes, 1024); +} + +TEST(MetadataCacheTest, RejectsInvalidNumericPropertyWhenDisabled) { + auto options = ContentCacheOptions::FromProperties( + {{std::string(ContentCacheOptions::kEnabled), "false"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "invalid"}}); + + EXPECT_THAT(options, HasErrorMessage(std::string(ContentCacheOptions::kMaxTotalBytes))); +} + +TEST(MetadataCacheTest, ReaderOptionsKeepsPositionalAggregateInitialization) { + auto io = std::make_shared(); + auto schema = std::make_shared(std::vector{}); + ReaderOptions options{"manifest.avro", {}, {}, io, schema}; + EXPECT_EQ(options.path, "manifest.avro"); + EXPECT_EQ(options.io, io); + EXPECT_EQ(options.projection, schema); +} + +class LengthTrackingFileIO : public MockFileIO { + public: + using MockFileIO::NewInputFile; + int length_calls = 0; + Result> NewInputFile(std::string location, + size_t length) override { + ++length_calls; + return MockFileIO::NewInputFile(std::move(location), length); + } +}; + +TEST(MetadataCacheTest, RejectsOversizedLengthBeforeCallingStorage) { + if constexpr (sizeof(size_t) <= sizeof(int32_t)) return; + LengthTrackingFileIO io; + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); + EXPECT_THAT(table->manifest_io()->NewInputFile("manifest.avro", + std::numeric_limits::max()), + IsError(ErrorKind::kInvalidArgument)); + EXPECT_EQ(io.length_calls, 0); +} + +TEST(MetadataCacheTest, DisabledTableUsesStorageDirectly) { + CountingMockFileIO io; + io.AddFile("manifest.avro", "manifest"); + ICEBERG_UNWRAP_OR_FAIL(auto table, MakeCachedTable(io)); + EXPECT_FALSE(table->ManifestCacheEnabled()); + EXPECT_EQ(table->manifest_io(), table->io()); + ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + EXPECT_EQ(io.open_count, 2); +} + +TEST(MetadataCacheTest, TablesSharingStorageHaveIndependentCachesAndLimits) { + CountingMockFileIO io; + io.AddFile("manifest.avro", "manifest"); + ICEBERG_UNWRAP_OR_FAIL( + auto first, + MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "16"}})); + ICEBERG_UNWRAP_OR_FAIL( + auto second, + MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "4"}})); + ASSERT_THAT(ReadCached(*first->manifest_io(), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*second->manifest_io(), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*first->manifest_io(), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*second->manifest_io(), "manifest.avro"), IsOk()); + EXPECT_EQ(io.open_count, 3); + // Ordinary FileIO input remains independent of either table's cache. + ASSERT_THAT(ReadCached(io, "manifest.avro"), IsOk()); + EXPECT_EQ(io.open_count, 4); +} + +TEST(MetadataCacheTest, CompatibleRefreshPreservesCachedContent) { + CountingMockFileIO io; + io.AddFile("manifest.avro", "manifest"); + std::unordered_map props = { + {std::string(ContentCacheOptions::kEnabled), "true"}}; + ICEBERG_UNWRAP_OR_FAIL(auto table, MakeCachedTable(io, props)); + ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + ICEBERG_UNWRAP_OR_FAIL(auto refreshed, MakeCachedTable(io, props, "metadata-2.json")); + auto catalog = std::dynamic_pointer_cast(table->catalog()); + EXPECT_CALL(*catalog, LoadTable(::testing::_)).WillOnce(::testing::Return(refreshed)); + ASSERT_THAT(table->Refresh(), IsOk()); + ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + EXPECT_EQ(io.open_count, 1); + table->ClearManifestCache(); + ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + EXPECT_EQ(io.open_count, 2); +} + +TEST(MetadataCacheTest, RefreshWithChangedLimitsReplacesCache) { + CountingMockFileIO io; + io.AddFile("manifest.avro", "manifest"); + std::unordered_map props = { + {std::string(ContentCacheOptions::kEnabled), "true"}}; + ICEBERG_UNWRAP_OR_FAIL(auto table, MakeCachedTable(io, props)); + ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + props[std::string(ContentCacheOptions::kMaxTotalBytes)] = "4"; + ICEBERG_UNWRAP_OR_FAIL(auto refreshed, MakeCachedTable(io, props, "metadata-2.json")); + auto catalog = std::dynamic_pointer_cast(table->catalog()); + EXPECT_CALL(*catalog, LoadTable(::testing::_)).WillOnce(::testing::Return(refreshed)); + ASSERT_THAT(table->Refresh(), IsOk()); + ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + EXPECT_EQ(io.open_count, 3); +} + +TEST(MetadataCacheTest, RefreshWithDifferentStorageDiscardsCachedBytes) { + CountingMockFileIO first_io; + CountingMockFileIO second_io; + first_io.AddFile("manifest.avro", "first"); + second_io.AddFile("manifest.avro", "second"); + std::unordered_map props = { + {std::string(ContentCacheOptions::kEnabled), "true"}}; + ICEBERG_UNWRAP_OR_FAIL(auto table, MakeCachedTable(first_io, props)); + ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + HasValue(::testing::Eq("first"))); + ICEBERG_UNWRAP_OR_FAIL(auto refreshed, + MakeCachedTable(second_io, props, "metadata-2.json")); + auto catalog = std::dynamic_pointer_cast(table->catalog()); + EXPECT_CALL(*catalog, LoadTable(::testing::_)).WillOnce(::testing::Return(refreshed)); + ASSERT_THAT(table->Refresh(), IsOk()); + EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + HasValue(::testing::Eq("second"))); +} + +TEST(MetadataCacheTest, RejectsInvalidCacheLimitsAtTableCreation) { + for (const auto key : + {ContentCacheOptions::kMaxTotalBytes, ContentCacheOptions::kMaxContentLength}) { + for (const auto value : {"0", "-1", "9223372036854775808", "invalid"}) { + CountingMockFileIO io; + EXPECT_THAT( + MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}, + {std::string(key), value}}), + IsError(ErrorKind::kInvalidArgument)); + EXPECT_THAT( + MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}}), + IsOk()); + } + } +} + +TEST(MetadataCacheTest, EmptyFilesAreNotRetained) { + CountingMockFileIO io; + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "1"}})); + for (int i = 0; i < 100; ++i) { + io.AddFile(std::to_string(i), ""); + ASSERT_THAT(ReadCached(*table->manifest_io(), std::to_string(i)), IsOk()); + } + ASSERT_THAT(ReadCached(*table->manifest_io(), "0"), IsOk()); + EXPECT_EQ(io.open_count, 101); +} + +class UnavailableFileIO : public CountingMockFileIO { + public: + bool unavailable = false; + Result> NewInputFile(std::string location) override { + if (unavailable) return IOError("storage resolution unavailable"); + return CountingMockFileIO::NewInputFile(std::move(location)); + } +}; + +class UnavailableNormalizingFileIO : public NormalizingMockFileIO { + public: + bool unavailable = false; + Result> NewInputFile(std::string location) override { + if (unavailable) return IOError("storage resolution unavailable"); + return NormalizingMockFileIO::NewInputFile(std::move(location)); + } +}; + +TEST(MetadataCacheTest, NormalizedAliasHitDoesNotResolveStorage) { + UnavailableNormalizingFileIO io; + io.AddFile("manifest.avro", "manifest"); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); + ASSERT_THAT(ReadCached(*table->manifest_io(), "alias://manifest.avro"), IsOk()); + io.unavailable = true; + EXPECT_THAT(ReadCached(*table->manifest_io(), "alias://manifest.avro"), + HasValue(::testing::Eq("manifest"))); + EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + HasValue(::testing::Eq("manifest"))); + EXPECT_EQ(io.open_count, 1); +} + +TEST(MetadataCacheTest, HitDoesNotResolveStorage) { + UnavailableFileIO io; + io.AddFile("manifest.avro", "manifest"); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); + ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + io.unavailable = true; + EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + HasValue(::testing::Eq("manifest"))); +} + +} // namespace +} // namespace iceberg diff --git a/src/iceberg/test/transaction_test.cc b/src/iceberg/test/transaction_test.cc index 50491e96d8..be3e199427 100644 --- a/src/iceberg/test/transaction_test.cc +++ b/src/iceberg/test/transaction_test.cc @@ -95,6 +95,29 @@ TEST_F(TransactionTest, BaseMetadataSurvivesTableRefresh) { ASSERT_FALSE(original_base.expired()); } +TEST_F(TransactionTest, CommitPreservesTableManifestContentCache) { + ICEBERG_UNWRAP_OR_FAIL(auto enable_txn, table_->NewTransaction()); + ICEBERG_UNWRAP_OR_FAIL(auto enable, enable_txn->NewUpdateProperties()); + enable->Set("io.manifest.cache-enabled", "true"); + ASSERT_THAT(enable->Commit(), IsOk()); + ICEBERG_UNWRAP_OR_FAIL(table_, enable_txn->Commit()); + ASSERT_TRUE(table_->ManifestCacheEnabled()); + + const auto manifest_path = table_location_ + "/metadata/cached-manifest.avro"; + ASSERT_THAT(file_io_->WriteFile(manifest_path, "manifest bytes"), IsOk()); + ASSERT_THAT(table_->manifest_io()->ReadFile(manifest_path, std::nullopt), + HasValue(::testing::Eq("manifest bytes"))); + + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); + ICEBERG_UNWRAP_OR_FAIL(auto update, txn->NewUpdateProperties()); + update->Set("txn.property", "txn.value"); + ASSERT_THAT(update->Commit(), IsOk()); + ICEBERG_UNWRAP_OR_FAIL(auto committed, txn->Commit()); + ASSERT_THAT(file_io_->DeleteFile(manifest_path), IsOk()); + EXPECT_THAT(committed->manifest_io()->ReadFile(manifest_path, std::nullopt), + HasValue(::testing::Eq("manifest bytes"))); +} + TEST_F(TransactionTest, MultipleUpdatesInTransaction) { ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); diff --git a/src/iceberg/transaction.cc b/src/iceberg/transaction.cc index 2509c5ae2c..c19b84d3f3 100644 --- a/src/iceberg/transaction.cc +++ b/src/iceberg/transaction.cc @@ -600,6 +600,7 @@ Result> Transaction::Commit() { return commit_result; } + commit_result.value()->ReuseManifestCache(*ctx_->table); ctx_->table = std::move(commit_result.value()); state_ = TransactionState::kCommitted; FinalizeUpdates(*ctx_->table->metadata()); @@ -649,8 +650,8 @@ void Transaction::FinalizeUpdates(const TableMetadata& committed) noexcept { } ICEBERG_ASSIGN_OR_RAISE( committed_files, - CommittedTransactionFiles(committed, ctx_->table->io(), std::move(snapshot_ids), - std::move(content_paths))); + CommittedTransactionFiles(committed, ctx_->table->manifest_io(), + std::move(snapshot_ids), std::move(content_paths))); return {}; }); // Skip staging cleanup on discovery failure to protect committed files. diff --git a/src/iceberg/update/cherry_pick_operation.cc b/src/iceberg/update/cherry_pick_operation.cc index d0a330760f..3856c98da6 100644 --- a/src/iceberg/update/cherry_pick_operation.cc +++ b/src/iceberg/update/cherry_pick_operation.cc @@ -241,7 +241,7 @@ CherryPickOperation& CherryPickOperation::Cherrypick(int64_t snapshot_id) { replaced_partitions_.emplace(); } - auto io = ctx_->table->io(); + auto io = ctx_->table->manifest_io(); ICEBERG_BUILDER_ASSIGN_OR_RETURN( auto changes, ReadSnapshotChanges(*cherrypick_snapshot_, io, current)); @@ -306,7 +306,7 @@ Status CherryPickOperation::ValidateReplacedPartitions(const TableMetadata& meta // Walk back from the current snapshot to the picked snapshot's parent and // reject any file added into a partition this pick replaces. - auto io = ctx_->table->io(); + auto io = ctx_->table->manifest_io(); ICEBERG_ASSIGN_OR_RAISE(auto snapshots, SnapshotUtil::AncestorsBetween( meta, meta.current_snapshot_id, parent_id)); // TODO: Parallelize added-file validation using the planning executor. diff --git a/src/iceberg/update/expire_snapshots.cc b/src/iceberg/update/expire_snapshots.cc index fd14308e88..8805f6d151 100644 --- a/src/iceberg/update/expire_snapshots.cc +++ b/src/iceberg/update/expire_snapshots.cc @@ -978,8 +978,8 @@ Result ExpireSnapshots::Apply() { std::unordered_set schema_ids; ICEBERG_ASSIGN_OR_RAISE(auto snapshot, base.SnapshotById(snapshot_id)); SnapshotReader snapshot_reader(snapshot.get()); - ICEBERG_ASSIGN_OR_RAISE(auto manifests, - snapshot_reader.Manifests(ctx_->table->io())); + ICEBERG_ASSIGN_OR_RAISE( + auto manifests, snapshot_reader.Manifests(ctx_->table->manifest_io())); for (const auto& manifest : manifests) { spec_ids.insert(manifest.partition_spec_id); } @@ -1038,12 +1038,12 @@ Status ExpireSnapshots::Finalize(const TableMetadata& metadata_after_expiration) !HasNonMainSnapshots(metadata_after_expiration); if (can_use_incremental) { - return IncrementalFileCleanup(ctx_->table->io(), delete_func_, delete_executor_, - plan_executor_) + return IncrementalFileCleanup(ctx_->table->manifest_io(), delete_func_, + delete_executor_, plan_executor_) .CleanFiles(metadata_before_expiration, metadata_after_expiration, cleanup_level_); } - return ReachableFileCleanup(ctx_->table->io(), delete_func_, delete_executor_, + return ReachableFileCleanup(ctx_->table->manifest_io(), delete_func_, delete_executor_, plan_executor_) .CleanFiles(metadata_before_expiration, metadata_after_expiration, cleanup_level_); } diff --git a/src/iceberg/update/fast_append.cc b/src/iceberg/update/fast_append.cc index 60c63e57b6..f1c177a47d 100644 --- a/src/iceberg/update/fast_append.cc +++ b/src/iceberg/update/fast_append.cc @@ -129,7 +129,7 @@ Result> FastAppend::Apply( if (snapshot != nullptr) { auto snapshot_reader = SnapshotReader(snapshot.get()); ICEBERG_ASSIGN_OR_RAISE(auto snapshot_manifests, - snapshot_reader.Manifests(ctx_->table->io())); + snapshot_reader.Manifests(ctx_->table->manifest_io())); manifests.insert(manifests.end(), snapshot_manifests.begin(), snapshot_manifests.end()); } @@ -186,7 +186,7 @@ Result FastAppend::CopyManifest(const ManifestFile& manifest) { SnapshotSummaryBuilder summary; ICEBERG_ASSIGN_OR_RAISE( auto copied, - CopyAppendManifest(manifest, ctx_->table->io(), schema, spec, snapshot_id, + CopyAppendManifest(manifest, ctx_->table->manifest_io(), schema, spec, snapshot_id, new_manifest_path, current.format_version, &summary)); copied_manifests_summary_.Merge(summary); return copied; diff --git a/src/iceberg/update/merging_snapshot_update.cc b/src/iceberg/update/merging_snapshot_update.cc index 2260be336f..caf424d70a 100644 --- a/src/iceberg/update/merging_snapshot_update.cc +++ b/src/iceberg/update/merging_snapshot_update.cc @@ -409,7 +409,7 @@ MergingSnapshotUpdate::MergingSnapshotUpdate(std::string table_name, : SnapshotUpdate(std::move(ctx)), table_name_(std::move(table_name)), delete_expression_(Expressions::AlwaysFalse()) { - auto file_io = ctx_->table->io(); + auto file_io = ctx_->table->manifest_io(); auto data_filter_manager = ManifestFilterManager::Make( ManifestContent::kData, file_io, [this](const std::string& location) { RegisterStagedFile(location); @@ -689,8 +689,8 @@ Result MergingSnapshotUpdate::CopyManifest(const ManifestFile& man SnapshotSummaryBuilder summary; ICEBERG_ASSIGN_OR_RAISE( auto copied, - CopyAppendManifest(manifest, ctx_->table->io(), schema, spec, SnapshotId(), path, - current.format_version, &summary)); + CopyAppendManifest(manifest, ctx_->table->manifest_io(), schema, spec, SnapshotId(), + path, current.format_version, &summary)); copied_manifests_summary_.Merge(summary); return copied; } diff --git a/src/iceberg/update/overwrite_files.cc b/src/iceberg/update/overwrite_files.cc index cba6e028cd..90b90a144b 100644 --- a/src/iceberg/update/overwrite_files.cc +++ b/src/iceberg/update/overwrite_files.cc @@ -215,25 +215,25 @@ Status OverwriteFiles::Validate(const TableMetadata& current_metadata, if (validate_new_data_files_) { ICEBERG_RETURN_UNEXPECTED(ValidateAddedDataFiles( current_metadata, starting_snapshot_id_, DataConflictDetectionFilter(), snapshot, - ctx_->table->io(), IsCaseSensitive())); + ctx_->table->manifest_io(), IsCaseSensitive())); } if (validate_new_deletes_) { if (row_filter != nullptr && row_filter != Expressions::AlwaysFalse()) { auto filter = conflict_detection_filter_ != nullptr ? conflict_detection_filter_ : row_filter; - ICEBERG_RETURN_UNEXPECTED( - ValidateNoNewDeleteFiles(current_metadata, starting_snapshot_id_, filter, - snapshot, ctx_->table->io(), IsCaseSensitive())); - ICEBERG_RETURN_UNEXPECTED( - ValidateDeletedDataFiles(current_metadata, starting_snapshot_id_, filter, - snapshot, ctx_->table->io(), IsCaseSensitive())); + ICEBERG_RETURN_UNEXPECTED(ValidateNoNewDeleteFiles( + current_metadata, starting_snapshot_id_, filter, snapshot, + ctx_->table->manifest_io(), IsCaseSensitive())); + ICEBERG_RETURN_UNEXPECTED(ValidateDeletedDataFiles( + current_metadata, starting_snapshot_id_, filter, snapshot, + ctx_->table->manifest_io(), IsCaseSensitive())); } if (!deleted_data_files_.empty()) { ICEBERG_RETURN_UNEXPECTED(ValidateNoNewDeletesForDataFiles( current_metadata, starting_snapshot_id_, conflict_detection_filter_, - deleted_data_files_, snapshot, ctx_->table->io(), IsCaseSensitive())); + deleted_data_files_, snapshot, ctx_->table->manifest_io(), IsCaseSensitive())); } } diff --git a/src/iceberg/update/replace_partitions.cc b/src/iceberg/update/replace_partitions.cc index 4487751bf1..8ad35cc4b6 100644 --- a/src/iceberg/update/replace_partitions.cc +++ b/src/iceberg/update/replace_partitions.cc @@ -113,7 +113,7 @@ Status ReplacePartitions::Validate(const TableMetadata& current_metadata, return {}; } - auto io = ctx_->table->io(); + auto io = ctx_->table->manifest_io(); if (validate_conflicting_data_) { if (data_spec->IsUnpartitioned()) { ICEBERG_RETURN_UNEXPECTED(ValidateAddedDataFiles( diff --git a/src/iceberg/update/rewrite_files.cc b/src/iceberg/update/rewrite_files.cc index 3c743c01d8..9df62f5536 100644 --- a/src/iceberg/update/rewrite_files.cc +++ b/src/iceberg/update/rewrite_files.cc @@ -172,7 +172,7 @@ Status RewriteFiles::Validate(const TableMetadata& current_metadata, if (!replaced_data_files_.empty()) { // If there are replaced data files, there cannot be any new row-level deletes // for those data files. - auto io = ctx_->table->io(); + auto io = ctx_->table->manifest_io(); ICEBERG_RETURN_UNEXPECTED(MergingSnapshotUpdate::ValidateNoNewDeletesForDataFiles( current_metadata, starting_snapshot_id_, replaced_data_files_, snapshot, std::move(io), HasDataSequenceNumber())); diff --git a/src/iceberg/update/row_delta.cc b/src/iceberg/update/row_delta.cc index c58b5233df..deac1a2dcf 100644 --- a/src/iceberg/update/row_delta.cc +++ b/src/iceberg/update/row_delta.cc @@ -139,7 +139,7 @@ Status RowDelta::Validate(const TableMetadata& current_metadata, starting_snapshot_id_.value(), snapshot->snapshot_id); } - auto io = ctx_->table->io(); + auto io = ctx_->table->manifest_io(); if (!referenced_data_files_.empty()) { ICEBERG_RETURN_UNEXPECTED(MergingSnapshotUpdate::ValidateDataFilesExist( current_metadata, starting_snapshot_id_, referenced_data_files_, diff --git a/src/iceberg/update/snapshot_update.cc b/src/iceberg/update/snapshot_update.cc index 5eccdc6f59..d2ffb1d814 100644 --- a/src/iceberg/update/snapshot_update.cc +++ b/src/iceberg/update/snapshot_update.cc @@ -344,7 +344,8 @@ Result SnapshotUpdate::Apply() { continue; } metadata_tasks.Submit([&manifest, this]() -> Status { - ICEBERG_ASSIGN_OR_RAISE(manifest, AddMetadata(manifest, ctx_->table->io(), base())); + ICEBERG_ASSIGN_OR_RAISE(manifest, + AddMetadata(manifest, ctx_->table->manifest_io(), base())); return {}; }); } diff --git a/src/iceberg/util/cache_internal.h b/src/iceberg/util/cache_internal.h index d82a93b80b..4aded5a618 100644 --- a/src/iceberg/util/cache_internal.h +++ b/src/iceberg/util/cache_internal.h @@ -25,6 +25,7 @@ /// \file iceberg/util/cache_internal.h /// \brief Provide internal LRU cache and memoization helpers. +#include #include #include #include @@ -37,13 +38,31 @@ namespace iceberg::internal { +// Gives every entry a weight of one, so that the maximum weight of a cache is the +// maximum number of entries. +struct UnitWeigher { + template + constexpr int64_t operator()(const Key& /*key*/, + const Value& /*value*/) const noexcept { + return 1; + } +}; + // A LRU (Least recently used) replacement cache -template +// +// The combined weight of the cached entries never exceeds `max_weight`; least recently +// used entries are evicted first to make room. `Weigher` computes the weight of an entry +// from its key and value when the entry is inserted or replaced, e.g. its size in bytes. +// Weights must be non-negative. With the default UnitWeigher, `max_weight` is the +// maximum number of entries. +template , typename KeyEqual = std::equal_to> class LruCache { public: - explicit LruCache(int32_t capacity) : capacity_(capacity) { - // The map size can temporarily exceed the cache capacity, see Replace() - map_.reserve(capacity_ + 1); + explicit LruCache(int64_t max_weight, Weigher weigher = Weigher{}) + : max_weight_(max_weight), weigher_(std::move(weigher)) { + ICEBERG_DCHECK(max_weight_ >= 0, "Cache maximum weight must be non-negative"); + ReserveForUnitWeight(); } LruCache(const LruCache&) = delete; @@ -54,15 +73,23 @@ class LruCache { void Clear() { items_.clear(); map_.clear(); + total_weight_ = 0; // The C++ spec doesn't tell whether map_.clear() will shrink the map capacity - map_.reserve(capacity_ + 1); + ReserveForUnitWeight(); } + // Number of retained entries. int32_t size() const { ICEBERG_DCHECK(items_.size() == map_.size(), "Cache item and map sizes differ"); return static_cast(items_.size()); } + // Combined weight of retained entries. + int64_t total_weight() const { return total_weight_; } + + // Maximum combined weight allowed in this cache. + int64_t max_weight() const { return max_weight_; } + template Value* Find(K&& key) { const auto it = map_.find(key); @@ -76,29 +103,47 @@ class LruCache { return &list_it->value; } + // Insert or update the value for `key`, mark it as most recently used, and evict least + // recently used entries until the cache is within its maximum weight again. + // + // Returns whether a new entry was added, and a pointer to the cached value. A value + // heavier than the maximum weight is not cached: any existing entry for `key` is + // removed, other entries are kept, and {false, nullptr} is returned. template std::pair Replace(K&& key, V&& value) { // Try to insert temporary iterator auto [it, inserted] = map_.emplace(std::forward(key), ListIt{}); + ListIt list_it; if (inserted) { // Inserted => push item at front of the list, and update iterator items_.emplace_front(&it->first, std::forward(value)); - it->second = items_.begin(); - // Did we exceed the cache capacity? If so, remove least recently used item - if (static_cast(items_.size()) > capacity_) { - const bool erased = map_.erase(*items_.back().key); - ICEBERG_DCHECK(erased, "Failed to erase least recently used cache item"); - static_cast(erased); - items_.pop_back(); - } - return {true, &it->second->value}; + list_it = items_.begin(); + it->second = list_it; + } else { + // Already exists => move item at front of the list, and update value + list_it = it->second; + items_.splice(items_.begin(), items_, list_it); + list_it->value = std::forward(value); + total_weight_ -= list_it->weight; + list_it->weight = 0; } - // Already exists => move item at front of the list, and update value - auto list_it = it->second; - items_.splice(items_.begin(), items_, list_it); - list_it->value = std::forward(value); - return {false, &list_it->value}; + const int64_t weight = weigher_(it->first, list_it->value); + ICEBERG_DCHECK(weight >= 0, "Cache entry weight must be non-negative"); + if (weight > max_weight_) { + items_.erase(list_it); + map_.erase(it); + return {false, nullptr}; + } + + // The item is at the front of the list and not yet counted in total_weight_, and + // `max_weight_ - weight` is non-negative, so eviction stops before reaching it. + while (total_weight_ > max_weight_ - weight) { + EvictLeastRecentlyUsed(); + } + list_it->weight = weight; + total_weight_ += weight; + return {inserted, &list_it->value}; } private: @@ -106,14 +151,35 @@ class LruCache { // Pointer to the key inside the unordered_map const Key* key; Value value; + int64_t weight = 0; }; using List = std::list; using ListIt = typename List::iterator; - const int32_t capacity_; + void EvictLeastRecentlyUsed() { + ICEBERG_DCHECK(!items_.empty(), "Cannot evict from an empty cache"); + const Item& item = items_.back(); + total_weight_ -= item.weight; + const bool erased = map_.erase(*item.key); + ICEBERG_DCHECK(erased, "Failed to erase least recently used cache item"); + static_cast(erased); + items_.pop_back(); + } + + void ReserveForUnitWeight() { + // Only with unit weights does the maximum weight bound the number of entries. The + // map size can temporarily exceed it by one, see Replace(). + if constexpr (std::is_same_v) { + map_.reserve(static_cast(max_weight_) + 1); + } + } + + const int64_t max_weight_; + Weigher weigher_; + int64_t total_weight_ = 0; // In most to least recently used order std::list items_; - std::unordered_map map_; + std::unordered_map map_; }; template From 1812a51e6f792b364a83abcfaa0f83cd07fbc0e0 Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Fri, 6 Nov 2026 23:25:05 +0800 Subject: [PATCH 02/16] docs: remove manifest cache changes from FileIO guide --- mkdocs/docs/file-io.md | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/mkdocs/docs/file-io.md b/mkdocs/docs/file-io.md index 823035c24f..62f998b1fc 100644 --- a/mkdocs/docs/file-io.md +++ b/mkdocs/docs/file-io.md @@ -21,34 +21,6 @@ `FileIO` reads, writes, and deletes Iceberg data and metadata files. -## Manifest content cache - -Manifest content caching is owned by each loaded `Table`. Tables sharing a `FileIO` -have independent caches and byte budgets. The storage `FileIO` remains usable for -uncached data-file and table-metadata JSON reads. - -| Property | Default | Description | -|---|---|---| -| `io.manifest.cache-enabled` | `false` | Cache immutable manifest-list and manifest bytes across scans of a table | -| `io.manifest.cache.max-total-bytes` | `104857600` | Maximum combined content retained by one table's cache | -| `io.manifest.cache.max-content-length` | `8388608` | Maximum file size eligible for caching | - -Catalog I/O properties provide defaults. Table properties override them. Direct -`Table::Make` calls can supply defaults through the trailing `io_properties` -argument; properties exposed by the supplied FileIO are also used as defaults. - -Scans and manifest maintenance share the table's cache. Refreshes and transactions -preserve cached bytes when the table UUID, cache limits, and storage context are -compatible. Independently loaded table handles start with separate caches. - -`Table::ClearManifestCache()` releases retained entries. Active input handles and -streams can keep their bytes alive after clearing or eviction; those bytes and -concurrent population buffers are outside the retention budget. Normalized path -aliases use a separate index retaining at most 64 KiB of alias strings per table -and do not pin content. Empty files and -files above either byte limit bypass the cache. Time-based expiration is not -supported, so `io.manifest.cache.expiration-interval-ms` is ignored. - ## Built-in implementations Call `iceberg::arrow::RegisterAll()` to register the Arrow-backed FileIO From 69da20336a733aa014c49362b102ea8bb1d02639 Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Fri, 6 Nov 2026 23:50:38 +0800 Subject: [PATCH 03/16] refactor: align content cache source and header names --- src/iceberg/CMakeLists.txt | 2 +- src/iceberg/{content_cache.cc => content_cache_internal.cc} | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) rename src/iceberg/{content_cache.cc => content_cache_internal.cc} (99%) diff --git a/src/iceberg/CMakeLists.txt b/src/iceberg/CMakeLists.txt index e71c2016af..15168659e2 100644 --- a/src/iceberg/CMakeLists.txt +++ b/src/iceberg/CMakeLists.txt @@ -26,7 +26,7 @@ set(ICEBERG_SOURCES catalog/memory/in_memory_catalog.cc catalog/session_catalog.cc catalog/session_context.cc - content_cache.cc + content_cache_internal.cc data_table_scan.cc delete_file_index.cc deletes/dv_util.cc diff --git a/src/iceberg/content_cache.cc b/src/iceberg/content_cache_internal.cc similarity index 99% rename from src/iceberg/content_cache.cc rename to src/iceberg/content_cache_internal.cc index bb4f52e1f9..05f417c1af 100644 --- a/src/iceberg/content_cache.cc +++ b/src/iceberg/content_cache_internal.cc @@ -17,12 +17,13 @@ * under the License. */ +#include "iceberg/content_cache_internal.h" + #include #include #include #include -#include "iceberg/content_cache_internal.h" #include "iceberg/util/macros.h" #include "iceberg/util/string_util.h" From 182ce447707ad1ab1f18674725fe84be15cb353e Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Thu, 12 Nov 2026 17:29:33 +0800 Subject: [PATCH 04/16] style: fix manifest cache lint warnings Add braces required by clang-tidy and retain the intentional positional ReaderOptions compatibility check with a targeted suppression. Co-authored-by: Codex --- src/iceberg/catalog/rest/rest_table.cc | 3 ++- src/iceberg/content_cache_internal.cc | 25 ++++++++++++++++--------- src/iceberg/table.cc | 9 ++++++--- src/iceberg/test/metadata_cache_test.cc | 2 ++ 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/iceberg/catalog/rest/rest_table.cc b/src/iceberg/catalog/rest/rest_table.cc index c12909817d..4bacc2ee52 100644 --- a/src/iceberg/catalog/rest/rest_table.cc +++ b/src/iceberg/catalog/rest/rest_table.cc @@ -57,8 +57,9 @@ Result> RestTable::Make( std::move(io), std::move(catalog), std::move(full_name), std::move(reporter), std::move(rest_context), std::move(labels))); auto properties = table->rest_context_->catalog_config; - for (const auto& [key, value] : table->rest_context_->table_config) + for (const auto& [key, value] : table->rest_context_->table_config) { properties[key] = value; + } ICEBERG_RETURN_UNEXPECTED(table->InitializeManifestCache(properties)); return table; } diff --git a/src/iceberg/content_cache_internal.cc b/src/iceberg/content_cache_internal.cc index 05f417c1af..3f3fa0f5b0 100644 --- a/src/iceberg/content_cache_internal.cc +++ b/src/iceberg/content_cache_internal.cc @@ -37,9 +37,10 @@ Result ParseNumberProperty( auto it = properties.find(std::string(key)); if (it == properties.end()) return fallback; auto result = StringUtils::ParseNumber(it->second); - if (!result) + if (!result) { return InvalidArgument("Invalid numeric value '{}' for property '{}': {}", it->second, key, result.error().message); + } return result; } @@ -146,8 +147,9 @@ class CachedInputFile : public InputFile { Result Size() const override { return size_; } Result> Open() override { if (content_) return std::make_unique(content_); - if (auto cached = cache_->Find(location_)) + if (auto cached = cache_->Find(location_)) { return std::make_unique(std::move(cached)); + } auto loaded = ReadInputFile(*input_, size_, location_); if (!loaded) { auto error = std::move(loaded).error(); @@ -209,8 +211,9 @@ class ManifestFileIO : public FileIO { Result ContentCacheOptions::FromProperties( const std::unordered_map& properties) { ContentCacheOptions options; - if (auto it = properties.find(std::string(kEnabled)); it != properties.end()) + if (auto it = properties.find(std::string(kEnabled)); it != properties.end()) { options.enabled = StringUtils::ParseBoolean(it->second); + } ICEBERG_ASSIGN_OR_RAISE( options.max_total_bytes, ParseNumberProperty(properties, kMaxTotalBytes, options.max_total_bytes)); @@ -269,13 +272,15 @@ void ContentCache::Clear() { Result> ContentCache::NewInputFile( const std::shared_ptr& io, std::string location, std::optional length) { - if (length && std::cmp_greater(*length, std::numeric_limits::max())) + if (length && std::cmp_greater(*length, std::numeric_limits::max())) { return InvalidArgument("File length {} exceeds int64_t max", *length); + } // A hit needs neither storage resolution nor working remote credentials. std::string canonical_location; - if (auto content = Find(location, &canonical_location)) + if (auto content = Find(location, &canonical_location)) { return std::make_unique(std::move(canonical_location), std::move(content)); + } std::unique_ptr input; if (length) { ICEBERG_ASSIGN_OR_RAISE(input, io->NewInputFile(location, *length)); @@ -286,19 +291,21 @@ Result> ContentCache::NewInputFile( std::lock_guard lock(mutex_); aliases_.Replace(std::move(location), std::string(input->location())); } - if (auto content = Find(input->location())) + if (auto content = Find(input->location())) { return std::make_unique(std::string(input->location()), std::move(content)); + } int64_t size; - if (length) + if (length) { size = static_cast(*length); - else { + } else { ICEBERG_ASSIGN_OR_RAISE(size, input->Size()); } ICEBERG_PRECHECK(size >= 0, "Invalid negative file size {}", size); if (size == 0 || std::cmp_greater(size, options_.max_content_length) || - std::cmp_greater(size, options_.max_total_bytes)) + std::cmp_greater(size, options_.max_total_bytes)) { return input; + } return std::make_unique(std::move(input), shared_from_this(), size); } diff --git a/src/iceberg/table.cc b/src/iceberg/table.cc index 15ff8ce5ad..b654050889 100644 --- a/src/iceberg/table.cc +++ b/src/iceberg/table.cc @@ -108,8 +108,9 @@ Status Table::InitializeManifestCache( const std::unordered_map& io_properties) { auto properties = io_->properties(); for (const auto& [key, value] : io_properties) properties[key] = value; - for (const auto& [key, value] : metadata_->properties.configs()) + for (const auto& [key, value] : metadata_->properties.configs()) { properties[key] = value; + } ICEBERG_ASSIGN_OR_RAISE(auto options, internal::ContentCacheOptions::FromProperties(properties)); if (options.enabled) { @@ -125,13 +126,15 @@ void Table::ClearManifestCache() { void Table::ReuseManifestCache(const Table& source) { if (!content_cache_ || !source.content_cache_ || uuid() != source.uuid() || - content_cache_->options() != source.content_cache_->options()) + content_cache_->options() != source.content_cache_->options()) { return; + } // A replaced storage context with no comparable properties may represent a different // endpoint or filesystem. Only preserve bytes when its identity is known to match. if (io_ != source.io_ && - (io_->properties().empty() || io_->properties() != source.io_->properties())) + (io_->properties().empty() || io_->properties() != source.io_->properties())) { return; + } content_cache_ = source.content_cache_; manifest_io_ = content_cache_->Wrap(io_); } diff --git a/src/iceberg/test/metadata_cache_test.cc b/src/iceberg/test/metadata_cache_test.cc index d98bf16299..f286b270e6 100644 --- a/src/iceberg/test/metadata_cache_test.cc +++ b/src/iceberg/test/metadata_cache_test.cc @@ -474,6 +474,8 @@ TEST(MetadataCacheTest, RejectsInvalidNumericPropertyWhenDisabled) { TEST(MetadataCacheTest, ReaderOptionsKeepsPositionalAggregateInitialization) { auto io = std::make_shared(); auto schema = std::make_shared(std::vector{}); + // Verify positional initialization remains compatible with the existing API. + // NOLINTNEXTLINE(modernize-use-designated-initializers) ReaderOptions options{"manifest.avro", {}, {}, io, schema}; EXPECT_EQ(options.path, "manifest.avro"); EXPECT_EQ(options.io, io); From 8ff9a61efeed200864004ace956fb0135d238b3e Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Wed, 18 Nov 2026 23:32:31 +0800 Subject: [PATCH 05/16] fix: make the manifest cache safe during commits and cleanup Treat cache configuration as a best-effort read optimization, simplify its configuration and I/O plumbing, and invalidate deleted manifests without warming the cache during expiration cleanup. Co-authored-by: Codex --- src/iceberg/arrow/arrow_io.cc | 25 +-- .../catalog/memory/in_memory_catalog.cc | 21 +-- src/iceberg/catalog/rest/rest_catalog.cc | 8 +- src/iceberg/catalog/rest/rest_file_io.cc | 3 - src/iceberg/catalog/rest/rest_table.cc | 5 - src/iceberg/catalog/sql/sql_catalog.cc | 13 +- src/iceberg/content_cache_internal.cc | 106 ++++++------- src/iceberg/content_cache_internal.h | 23 +-- src/iceberg/data_table_scan.cc | 19 +-- src/iceberg/file_io.cc | 21 +-- src/iceberg/incremental_append_scan.cc | 6 +- src/iceberg/incremental_changelog_scan.cc | 9 +- src/iceberg/table.cc | 42 +++-- src/iceberg/table.h | 25 ++- src/iceberg/table_scan.cc | 2 +- src/iceberg/table_scan.h | 4 + src/iceberg/test/arrow_io_test.cc | 55 +++---- src/iceberg/test/fast_append_test.cc | 23 +++ src/iceberg/test/manifest_reader_test.cc | 14 +- src/iceberg/test/metadata_cache_test.cc | 146 ++++++++++-------- src/iceberg/test/transaction_test.cc | 4 +- src/iceberg/transaction.cc | 2 +- src/iceberg/update/cherry_pick_operation.cc | 4 +- src/iceberg/update/expire_snapshots.cc | 37 +++-- src/iceberg/update/fast_append.cc | 13 +- src/iceberg/update/merging_snapshot_update.cc | 6 +- src/iceberg/update/overwrite_files.cc | 9 +- src/iceberg/update/replace_partitions.cc | 2 +- src/iceberg/update/rewrite_files.cc | 2 +- src/iceberg/update/row_delta.cc | 2 +- src/iceberg/update/snapshot_update.cc | 5 +- src/iceberg/util/cache_internal.h | 11 ++ src/iceberg/util/error_util_internal.h | 14 ++ 33 files changed, 348 insertions(+), 333 deletions(-) diff --git a/src/iceberg/arrow/arrow_io.cc b/src/iceberg/arrow/arrow_io.cc index f9bab7b808..0e423eb387 100644 --- a/src/iceberg/arrow/arrow_io.cc +++ b/src/iceberg/arrow/arrow_io.cc @@ -427,8 +427,19 @@ class ArrowInputFile : public InputFile { if (file_size_.has_value()) { return *file_size_; } - ::arrow::fs::FileInfo file_info(path_, ::arrow::fs::FileType::File); - ICEBERG_ARROW_ASSIGN_OR_RETURN(auto input, fs_->OpenInputFile(file_info)); + ICEBERG_ARROW_ASSIGN_OR_RETURN(auto info, fs_->GetFileInfo(path_)); + if (info.type() == ::arrow::fs::FileType::NotFound) { + return IOError("File does not exist: {}", location_); + } + if (info.type() != ::arrow::fs::FileType::File) { + return IOError("Not a regular file: {}", location_); + } + if (info.size() != ::arrow::fs::kNoSize) { + file_size_ = info.size(); + return *file_size_; + } + // Some filesystems report a file without a size. + ICEBERG_ARROW_ASSIGN_OR_RETURN(auto input, fs_->OpenInputFile(info)); auto size_result = input->GetSize(); auto close_status = input->Close(); if (!size_result.ok() && !close_status.ok()) { @@ -444,17 +455,12 @@ class ArrowInputFile : public InputFile { } Result> Open() override { - ICEBERG_ASSIGN_OR_RAISE(auto input, OpenArrowFile()); - return std::make_unique(std::move(input)); - } - - Result> OpenArrowFile() { ::arrow::fs::FileInfo file_info(path_, ::arrow::fs::FileType::File); if (file_size_.has_value()) { file_info.set_size(*file_size_); } ICEBERG_ARROW_ASSIGN_OR_RETURN(auto input, fs_->OpenInputFile(file_info)); - return input; + return std::make_unique(std::move(input)); } private: @@ -548,9 +554,6 @@ Result> OpenArrowInputStream( } else { ICEBERG_ASSIGN_OR_RAISE(input_file, io->NewInputFile(path)); } - if (auto* arrow_input = dynamic_cast(input_file.get())) { - return arrow_input->OpenArrowFile(); - } ICEBERG_ASSIGN_OR_RAISE(size, input_file->Size()); if (size < 0) { return Invalid("Invalid negative file size {} for {}", size, path); diff --git a/src/iceberg/catalog/memory/in_memory_catalog.cc b/src/iceberg/catalog/memory/in_memory_catalog.cc index 1a9fdfc465..5ca951d033 100644 --- a/src/iceberg/catalog/memory/in_memory_catalog.cc +++ b/src/iceberg/catalog/memory/in_memory_catalog.cc @@ -23,7 +23,6 @@ #include #include "iceberg/catalog/catalog_util.h" -#include "iceberg/content_cache_internal.h" #include "iceberg/file_io.h" #include "iceberg/metrics/metrics_reporters.h" #include "iceberg/table.h" @@ -345,7 +344,6 @@ Result> InMemoryCatalog::Make( const std::string& warehouse_location, const std::unordered_map& properties) { ICEBERG_PRECHECK(file_io != nullptr, "InMemoryCatalog requires a non-null FileIO"); - ICEBERG_RETURN_UNEXPECTED(internal::ContentCacheOptions::FromProperties(properties)); std::shared_ptr reporter; auto it = properties.find(std::string(kMetricsReporterImpl)); if (it != properties.end() && !it->second.empty() && @@ -448,8 +446,7 @@ Result> InMemoryCatalog::CreateTable( root_namespace_->UpdateTableMetadataLocation(identifier, metadata_file_location)); return Table::Make(identifier, std::move(table_metadata), std::move(metadata_file_location), file_io_, shared_from_this(), - CatalogUtil::FullTableName(name(), identifier), reporter_, - /*labels=*/nullptr, properties_); + CatalogUtil::FullTableName(name(), identifier), reporter_); } Result> InMemoryCatalog::UpdateTable( @@ -503,8 +500,7 @@ Result> InMemoryCatalog::UpdateTable( return Table::Make(identifier, std::move(updated), std::move(new_metadata_location), file_io_, shared_from_this(), - CatalogUtil::FullTableName(name(), identifier), reporter_, - /*labels=*/nullptr, properties_); + CatalogUtil::FullTableName(name(), identifier), reporter_); } Result> InMemoryCatalog::StageCreateTable( @@ -524,10 +520,10 @@ Result> InMemoryCatalog::StageCreateTable( auto table_metadata, TableMetadata::Make(*schema, *spec, *order, base_location, properties)); ICEBERG_ASSIGN_OR_RAISE( - auto table, StagedTable::Make(identifier, std::move(table_metadata), "", file_io_, - shared_from_this(), - CatalogUtil::FullTableName(name(), identifier), - reporter_, properties_)); + auto table, + StagedTable::Make(identifier, std::move(table_metadata), "", file_io_, + shared_from_this(), + CatalogUtil::FullTableName(name(), identifier), reporter_)); return Transaction::Make(std::move(table), TransactionKind::kCreate); } @@ -608,8 +604,7 @@ Result> InMemoryCatalog::LoadTable( TableMetadataUtil::Read(*file_io_, metadata_location)); return Table::Make(identifier, std::move(metadata), std::move(metadata_location), file_io_, shared_from_this(), - CatalogUtil::FullTableName(name(), identifier), reporter_, - /*labels=*/nullptr, properties_); + CatalogUtil::FullTableName(name(), identifier), reporter_); } Result> InMemoryCatalog::RegisterTable( @@ -632,7 +627,7 @@ Result> InMemoryCatalog::RegisterTable( } return Table::Make(identifier, std::move(metadata), metadata_file_location, file_io_, shared_from_this(), CatalogUtil::FullTableName(name(), identifier), - reporter_, /*labels=*/nullptr, properties_); + reporter_); } } // namespace iceberg diff --git a/src/iceberg/catalog/rest/rest_catalog.cc b/src/iceberg/catalog/rest/rest_catalog.cc index 54c0d6446f..a6ba928e50 100644 --- a/src/iceberg/catalog/rest/rest_catalog.cc +++ b/src/iceberg/catalog/rest/rest_catalog.cc @@ -927,14 +927,12 @@ Result> RestCatalog::StageCreateTable( auto table_catalog = std::make_shared( shared_from_this(), context, identifier, table_config, std::move(table_session), table_io); - auto manifest_properties = config_.configs(); - for (const auto& [key, value] : table_config) manifest_properties[key] = value; ICEBERG_ASSIGN_OR_RAISE( auto staged_table, StagedTable::Make(identifier, std::move(result.metadata), std::move(result.metadata_location), std::move(table_io), std::move(table_catalog), RestTableName(name_, identifier), - std::move(reporter), manifest_properties)); + std::move(reporter))); return Transaction::Make(std::move(staged_table), TransactionKind::kCreate); } @@ -1080,8 +1078,6 @@ Result> RestCatalog::MakeTable( std::shared_ptr table_catalog, std::shared_ptr reporter, const std::unordered_map& table_config, std::shared_ptr table_session, std::unique_ptr labels) { - auto manifest_properties = config_.configs(); - for (const auto& [key, value] : table_config) manifest_properties[key] = value; // Table config overrides client config. ICEBERG_ASSIGN_OR_RAISE(auto server_mode, RestCatalogProperties::ScanPlanningModeFrom(table_config)); @@ -1135,7 +1131,7 @@ Result> RestCatalog::MakeTable( return Table::Make(identifier, std::move(metadata), std::move(metadata_location), std::move(table_io), std::move(table_catalog), RestTableName(name_, identifier), std::move(reporter), - std::move(labels), manifest_properties); + std::move(labels)); } } // namespace iceberg::rest diff --git a/src/iceberg/catalog/rest/rest_file_io.cc b/src/iceberg/catalog/rest/rest_file_io.cc index f7f7aa6410..4cd102704b 100644 --- a/src/iceberg/catalog/rest/rest_file_io.cc +++ b/src/iceberg/catalog/rest/rest_file_io.cc @@ -25,7 +25,6 @@ #include #include "iceberg/catalog/rest/types.h" -#include "iceberg/content_cache_internal.h" #include "iceberg/file_io.h" #include "iceberg/file_io_registry.h" #include "iceberg/logging/log_macros.h" @@ -49,8 +48,6 @@ std::unordered_map MergeFileIOProperties( } // namespace Result> MakeCatalogFileIO(const RestCatalogProperties& config) { - ICEBERG_RETURN_UNEXPECTED( - internal::ContentCacheOptions::FromProperties(config.configs())); const std::string io_impl = config.Get(RestCatalogProperties::kIOImpl); if (io_impl.empty()) { return std::make_unique(config.configs()); diff --git a/src/iceberg/catalog/rest/rest_table.cc b/src/iceberg/catalog/rest/rest_table.cc index 4bacc2ee52..33bebf568b 100644 --- a/src/iceberg/catalog/rest/rest_table.cc +++ b/src/iceberg/catalog/rest/rest_table.cc @@ -56,11 +56,6 @@ Result> RestTable::Make( std::move(identifier), std::move(metadata), std::move(metadata_location), std::move(io), std::move(catalog), std::move(full_name), std::move(reporter), std::move(rest_context), std::move(labels))); - auto properties = table->rest_context_->catalog_config; - for (const auto& [key, value] : table->rest_context_->table_config) { - properties[key] = value; - } - ICEBERG_RETURN_UNEXPECTED(table->InitializeManifestCache(properties)); return table; } diff --git a/src/iceberg/catalog/sql/sql_catalog.cc b/src/iceberg/catalog/sql/sql_catalog.cc index 78866843fb..169612f49b 100644 --- a/src/iceberg/catalog/sql/sql_catalog.cc +++ b/src/iceberg/catalog/sql/sql_catalog.cc @@ -25,7 +25,6 @@ #include "iceberg/catalog/catalog_util.h" #include "iceberg/catalog/sql/config.h" -#include "iceberg/content_cache_internal.h" #include "iceberg/file_io.h" #include "iceberg/metrics/metrics_reporters.h" #include "iceberg/table.h" @@ -149,7 +148,6 @@ Result> SqlCatalog::Make( if (file_io == nullptr) { return InvalidArgument("SqlCatalog requires a non-null FileIO"); } - ICEBERG_RETURN_UNEXPECTED(internal::ContentCacheOptions::FromProperties(config.props)); ICEBERG_RETURN_UNEXPECTED(store->Initialize()); std::shared_ptr reporter; @@ -387,7 +385,7 @@ Result> SqlCatalog::LoadTableFrom( TableMetadataUtil::Read(*file_io_, metadata_location)); return Table::Make(identifier, std::move(metadata), metadata_location, file_io_, shared_from_this(), CatalogUtil::FullTableName(name(), identifier), - reporter_, /*labels=*/nullptr, config_.props); + reporter_); } Result> SqlCatalog::LoadTable(const TableIdentifier& identifier) { @@ -426,7 +424,7 @@ Result> SqlCatalog::CreateTable( return Table::Make(identifier, std::move(metadata), metadata_location, file_io_, shared_from_this(), CatalogUtil::FullTableName(name(), identifier), - reporter_, /*labels=*/nullptr, config_.props); + reporter_); } Result> SqlCatalog::UpdateTable( @@ -494,7 +492,7 @@ Result> SqlCatalog::UpdateTable( return Table::Make(identifier, std::move(updated), new_metadata_location, file_io_, shared_from_this(), CatalogUtil::FullTableName(name(), identifier), - reporter_, /*labels=*/nullptr, config_.props); + reporter_); } Result> SqlCatalog::StageCreateTable( @@ -522,8 +520,7 @@ Result> SqlCatalog::StageCreateTable( ICEBERG_ASSIGN_OR_RAISE( auto table, StagedTable::Make(identifier, std::move(metadata), "", file_io_, shared_from_this(), - CatalogUtil::FullTableName(name(), identifier), reporter_, - config_.props)); + CatalogUtil::FullTableName(name(), identifier), reporter_)); return Transaction::Make(std::move(table), TransactionKind::kCreate); } @@ -604,7 +601,7 @@ Result> SqlCatalog::RegisterTable( return Table::Make(identifier, std::move(metadata), metadata_file_location, file_io_, shared_from_this(), CatalogUtil::FullTableName(name(), identifier), - reporter_, /*labels=*/nullptr, config_.props); + reporter_); } // -------------------------------------------------------------------------- diff --git a/src/iceberg/content_cache_internal.cc b/src/iceberg/content_cache_internal.cc index 3f3fa0f5b0..a248bebf91 100644 --- a/src/iceberg/content_cache_internal.cc +++ b/src/iceberg/content_cache_internal.cc @@ -24,6 +24,7 @@ #include #include +#include "iceberg/util/error_util_internal.h" #include "iceberg/util/macros.h" #include "iceberg/util/string_util.h" @@ -44,18 +45,6 @@ Result ParseNumberProperty( return result; } -Status FinishWithCloseStatus(Status operation_status, Status close_status) { - if (!operation_status.has_value()) { - auto error = operation_status.error(); - if (!close_status.has_value()) { - error.message += "; additionally failed to close stream: "; - error.message += close_status.error().message; - } - return ::iceberg::unexpected(std::move(error)); - } - return close_status; -} - Result> ReadInputFile(InputFile& input_file, int64_t read_size, std::string_view file_location) { if (read_size < 0) { @@ -147,7 +136,8 @@ class CachedInputFile : public InputFile { Result Size() const override { return size_; } Result> Open() override { if (content_) return std::make_unique(content_); - if (auto cached = cache_->Find(location_)) { + uint64_t generation; + if (auto cached = cache_->Find(location_, &generation)) { return std::make_unique(std::move(cached)); } auto loaded = ReadInputFile(*input_, size_, location_); @@ -163,7 +153,7 @@ class CachedInputFile : public InputFile { } auto content = std::make_shared>(std::move(loaded).value()); - cache_->Insert(location_, content); + cache_->Insert(location_, content, generation); return std::make_unique(std::move(content)); } @@ -192,10 +182,17 @@ class ManifestFileIO : public FileIO { return io_->NewOutputFile(std::move(location)); } Status DeleteFile(const std::string& location) override { - return io_->DeleteFile(location); + auto status = io_->DeleteFile(location); + cache_->Invalidate(location); + return status; } Status DeleteFiles(const std::vector& locations) override { - return io_->DeleteFiles(locations); + auto status = io_->DeleteFiles(locations); + // A failed bulk delete can still remove some files. + for (const auto& location : locations) { + cache_->Invalidate(location); + } + return status; } const std::unordered_map& properties() const override { return io_->properties(); @@ -214,61 +211,64 @@ Result ContentCacheOptions::FromProperties( if (auto it = properties.find(std::string(kEnabled)); it != properties.end()) { options.enabled = StringUtils::ParseBoolean(it->second); } + if (!options.enabled) { + return options; + } ICEBERG_ASSIGN_OR_RAISE( options.max_total_bytes, ParseNumberProperty(properties, kMaxTotalBytes, options.max_total_bytes)); ICEBERG_ASSIGN_OR_RAISE( options.max_content_length, ParseNumberProperty(properties, kMaxContentLength, options.max_content_length)); - if (options.enabled) { - ICEBERG_PRECHECK( - options.max_total_bytes > 0 && - std::cmp_less_equal(options.max_total_bytes, - std::numeric_limits::max()), - "Manifest cache maximum total bytes must be positive and fit in int64_t"); - ICEBERG_PRECHECK( - options.max_content_length > 0 && - std::cmp_less_equal(options.max_content_length, - std::numeric_limits::max()), - "Manifest cache maximum content length must be positive and fit in int64_t"); - } + ICEBERG_PRECHECK( + options.max_total_bytes > 0 && + std::cmp_less_equal(options.max_total_bytes, + std::numeric_limits::max()), + "Manifest cache maximum total bytes must be positive and fit in int64_t"); + ICEBERG_PRECHECK( + options.max_content_length > 0 && + std::cmp_less_equal(options.max_content_length, + std::numeric_limits::max()), + "Manifest cache maximum content length must be positive and fit in int64_t"); return options; } ContentCache::ContentCache(ContentCacheOptions options) - : options_(options), - lru_(static_cast(options.max_total_bytes)), - aliases_( - static_cast(std::min(options.max_total_bytes, 64 * 1024))) {} + : options_(options), lru_(static_cast(options.max_total_bytes)) {} + std::shared_ptr ContentCache::Wrap(std::shared_ptr io) { return std::make_shared(std::move(io), shared_from_this()); } -MetadataContent ContentCache::Find(std::string_view location, - std::string* canonical_location) { + +MetadataContent ContentCache::Find(std::string_view location, uint64_t* generation) { std::lock_guard lock(mutex_); - if (auto* content = lru_.Find(location)) { - if (canonical_location) *canonical_location = location; - return *content; - } - if (auto* canonical = aliases_.Find(location)) { - if (auto* content = lru_.Find(*canonical)) { - if (canonical_location) *canonical_location = *canonical; - return *content; - } - } - return nullptr; + if (generation) *generation = generation_; + auto* content = lru_.Find(location); + return content ? *content : nullptr; } -void ContentCache::Insert(const std::string& location, MetadataContent content) { + +void ContentCache::Insert(const std::string& location, MetadataContent content, + uint64_t generation) { // Empty/corrupt metadata must not accumulate unbounded zero-weight entries. if (content->empty()) return; std::lock_guard lock(mutex_); - lru_.Replace(location, std::move(content)); + if (generation == generation_) { + lru_.Replace(location, std::move(content)); + } +} + +void ContentCache::Invalidate(std::string_view location) { + std::lock_guard lock(mutex_); + lru_.Erase(location); + ++generation_; } + void ContentCache::Clear() { std::lock_guard lock(mutex_); lru_.Clear(); - aliases_.Clear(); + ++generation_; } + Result> ContentCache::NewInputFile( const std::shared_ptr& io, std::string location, std::optional length) { @@ -276,10 +276,8 @@ Result> ContentCache::NewInputFile( return InvalidArgument("File length {} exceeds int64_t max", *length); } // A hit needs neither storage resolution nor working remote credentials. - std::string canonical_location; - if (auto content = Find(location, &canonical_location)) { - return std::make_unique(std::move(canonical_location), - std::move(content)); + if (auto content = Find(location)) { + return std::make_unique(std::move(location), std::move(content)); } std::unique_ptr input; if (length) { @@ -287,10 +285,6 @@ Result> ContentCache::NewInputFile( } else { ICEBERG_ASSIGN_OR_RAISE(input, io->NewInputFile(location)); } - if (input->location() != location) { - std::lock_guard lock(mutex_); - aliases_.Replace(std::move(location), std::string(input->location())); - } if (auto content = Find(input->location())) { return std::make_unique(std::string(input->location()), std::move(content)); diff --git a/src/iceberg/content_cache_internal.h b/src/iceberg/content_cache_internal.h index 5ec39bb7fa..eeaab14f40 100644 --- a/src/iceberg/content_cache_internal.h +++ b/src/iceberg/content_cache_internal.h @@ -19,7 +19,6 @@ #pragma once -#include #include #include #include @@ -59,15 +58,6 @@ struct MetadataContentWeigher { } }; -// Bound the bookkeeping for normalized aliases separately from retained content. -struct LocationAliasWeigher { - int64_t operator()(const std::string& alias, const std::string& canonical) const { - constexpr size_t kMaxAliasPart = 65537; - return static_cast(std::min(alias.size(), kMaxAliasPart) + - std::min(canonical.size(), kMaxAliasPart)); - } -}; - // A table owns this cache; readers share it through a metadata-only FileIO adapter. // Storage FileIOs remain independent of the cache and its configuration. class ICEBERG_EXPORT ContentCache : public std::enable_shared_from_this { @@ -78,10 +68,11 @@ class ICEBERG_EXPORT ContentCache : public std::enable_shared_from_this> NewInputFile( const std::shared_ptr& io, std::string location, std::optional length = std::nullopt); - // Optionally return the canonical location represented by a cache hit. - MetadataContent Find(std::string_view location, - std::string* canonical_location = nullptr); - void Insert(const std::string& location, MetadataContent content); + // A load that overlaps invalidation may serve its own reader but cannot repopulate + // the cache. Capture the generation under the same lock as the lookup. + MetadataContent Find(std::string_view location, uint64_t* generation = nullptr); + void Insert(const std::string& location, MetadataContent content, uint64_t generation); + void Invalidate(std::string_view location); void Clear(); private: @@ -89,9 +80,7 @@ class ICEBERG_EXPORT ContentCache : public std::enable_shared_from_this lru_; - // Alias strings do not pin content and are capped at 64 KiB in total. - LruCache - aliases_; + uint64_t generation_ = 0; }; } // namespace iceberg::internal diff --git a/src/iceberg/data_table_scan.cc b/src/iceberg/data_table_scan.cc index 6db6aa7183..df92cb3215 100644 --- a/src/iceberg/data_table_scan.cc +++ b/src/iceberg/data_table_scan.cc @@ -165,12 +165,10 @@ Result DataTableScan::PlanFilesStream() const { ICEBERG_ASSIGN_OR_RAISE(auto specs_by_id, metadata_cache.GetPartitionSpecsById()); SnapshotReader snapshot_reader(snapshot.get()); - ICEBERG_ASSIGN_OR_RAISE( - auto data_manifests, - snapshot_reader.DataManifests(context_.manifest_io ? context_.manifest_io : io_)); - ICEBERG_ASSIGN_OR_RAISE( - auto delete_manifests, - snapshot_reader.DeleteManifests(context_.manifest_io ? context_.manifest_io : io_)); + ICEBERG_ASSIGN_OR_RAISE(auto data_manifests, + snapshot_reader.DataManifests(manifest_io())); + ICEBERG_ASSIGN_OR_RAISE(auto delete_manifests, + snapshot_reader.DeleteManifests(manifest_io())); if (scan_metrics) { scan_metrics->total_data_manifests->Increment( @@ -186,11 +184,10 @@ Result DataTableScan::PlanFilesStream() const { std::make_move_iterator(delete_manifests.begin()), std::make_move_iterator(delete_manifests.end())); - ICEBERG_ASSIGN_OR_RAISE( - auto manifest_group, - ManifestGroup::Make(context_.manifest_io ? context_.manifest_io : io_, schema_, - specs_by_id, std::move(owned_data_manifests), - std::move(owned_delete_manifests))); + ICEBERG_ASSIGN_OR_RAISE(auto manifest_group, + ManifestGroup::Make(manifest_io(), schema_, specs_by_id, + std::move(owned_data_manifests), + std::move(owned_delete_manifests))); manifest_group->CaseSensitive(context_.case_sensitive) .Select(ScanColumns()) .FilterData(filter()) diff --git a/src/iceberg/file_io.cc b/src/iceberg/file_io.cc index 543c13b03a..827c9b325b 100644 --- a/src/iceberg/file_io.cc +++ b/src/iceberg/file_io.cc @@ -22,26 +22,11 @@ #include #include +#include "iceberg/util/error_util_internal.h" #include "iceberg/util/macros.h" namespace iceberg { -namespace { - -Status FinishWithCloseStatus(Status operation_status, Status close_status) { - if (!operation_status.has_value()) { - auto error = operation_status.error(); - if (!close_status.has_value()) { - error.message += "; additionally failed to close stream: "; - error.message += close_status.error().message; - } - return ::iceberg::unexpected(std::move(error)); - } - return close_status; -} - -} // namespace - Result> FileIO::NewInputFile(std::string file_location) { return NotImplemented("NewInputFile not implemented for {}", file_location); } @@ -82,7 +67,7 @@ Result FileIO::ReadFile(const std::string& file_location, read_status = stream->ReadFully(/*position=*/0, bytes); } ICEBERG_RETURN_UNEXPECTED( - FinishWithCloseStatus(std::move(read_status), stream->Close())); + internal::FinishWithCloseStatus(std::move(read_status), stream->Close())); return content; } @@ -97,7 +82,7 @@ Status FileIO::WriteFile(const std::string& file_location, std::string_view cont if (status.has_value()) { status = stream->Flush(); } - return FinishWithCloseStatus(std::move(status), stream->Close()); + return internal::FinishWithCloseStatus(std::move(status), stream->Close()); } Status FileIO::DeleteFiles(const std::vector& file_locations) { diff --git a/src/iceberg/incremental_append_scan.cc b/src/iceberg/incremental_append_scan.cc index 189d374eb9..201ecc41e0 100644 --- a/src/iceberg/incremental_append_scan.cc +++ b/src/iceberg/incremental_append_scan.cc @@ -75,9 +75,7 @@ Result>> IncrementalAppendScan::PlanFi std::unordered_set data_manifests; for (const auto& snapshot : append_snapshots) { SnapshotReader snapshot_reader(snapshot.get()); - ICEBERG_ASSIGN_OR_RAISE( - auto manifests, - snapshot_reader.DataManifests(context_.manifest_io ? context_.manifest_io : io_)); + ICEBERG_ASSIGN_OR_RAISE(auto manifests, snapshot_reader.DataManifests(manifest_io())); std::ranges::copy_if( manifests, std::inserter(data_manifests, data_manifests.end()), [&snapshot_ids](const ManifestFile& manifest) { @@ -95,7 +93,7 @@ Result>> IncrementalAppendScan::PlanFi ICEBERG_ASSIGN_OR_RAISE( auto manifest_group, ManifestGroup::Make( - context_.manifest_io ? context_.manifest_io : io_, schema_, specs_by_id, + manifest_io(), schema_, specs_by_id, std::vector(data_manifests.begin(), data_manifests.end()), {})); manifest_group->CaseSensitive(context_.case_sensitive) diff --git a/src/iceberg/incremental_changelog_scan.cc b/src/iceberg/incremental_changelog_scan.cc index 3f4dba6a04..198bb86172 100644 --- a/src/iceberg/incremental_changelog_scan.cc +++ b/src/iceberg/incremental_changelog_scan.cc @@ -67,8 +67,7 @@ IncrementalChangelogScan::PlanFiles(std::optional from_snapshot_id_excl if (!operation.has_value() || operation.value() != DataOperation::kReplace) { auto snapshot_reader = std::make_unique(snapshot.get()); ICEBERG_ASSIGN_OR_RAISE(auto delete_manifests, - snapshot_reader->DeleteManifests( - context_.manifest_io ? context_.manifest_io : io_)); + snapshot_reader->DeleteManifests(manifest_io())); if (!delete_manifests.empty()) { return NotSupported( "Delete files are currently not supported in changelog scans"); @@ -95,8 +94,7 @@ IncrementalChangelogScan::PlanFiles(std::optional from_snapshot_id_excl std::unordered_set seen_manifest_paths; for (const auto& snapshot : changelog_snapshots) { ICEBERG_ASSIGN_OR_RAISE(auto manifests, - snapshot.second->DataManifests( - context_.manifest_io ? context_.manifest_io : io_)); + snapshot.second->DataManifests(manifest_io())); for (auto& manifest : manifests) { if (manifest.added_snapshot_id.has_value() && snapshot_ids.contains(manifest.added_snapshot_id.value()) && @@ -114,8 +112,7 @@ IncrementalChangelogScan::PlanFiles(std::optional from_snapshot_id_excl ICEBERG_ASSIGN_OR_RAISE( auto manifest_group, - ManifestGroup::Make(context_.manifest_io ? context_.manifest_io : io_, schema_, - specs_by_id, std::move(data_manifests), + ManifestGroup::Make(manifest_io(), schema_, specs_by_id, std::move(data_manifests), /*delete_manifests=*/{})); manifest_group->CaseSensitive(context_.case_sensitive) diff --git a/src/iceberg/table.cc b/src/iceberg/table.cc index b654050889..f70d7d9def 100644 --- a/src/iceberg/table.cc +++ b/src/iceberg/table.cc @@ -30,6 +30,7 @@ #include "iceberg/inspect/metadata_table.h" #include "iceberg/labels.h" #include "iceberg/location_provider.h" +#include "iceberg/logging/log_macros.h" #include "iceberg/partition_spec.h" #include "iceberg/result.h" #include "iceberg/schema.h" @@ -65,8 +66,7 @@ Result> Table::Make( TableIdentifier identifier, std::shared_ptr metadata, std::string metadata_location, std::shared_ptr io, std::shared_ptr catalog, std::string full_name, - std::shared_ptr reporter, std::unique_ptr labels, - const std::unordered_map& io_properties) { + std::shared_ptr reporter, std::unique_ptr labels) { if (metadata == nullptr) [[unlikely]] { return InvalidArgument("Metadata cannot be null"); } @@ -83,7 +83,6 @@ Result> Table::Make( new Table(std::move(identifier), std::move(metadata), std::move(metadata_location), std::move(io), std::move(catalog), std::move(full_name), std::move(reporter), std::move(labels))); - ICEBERG_RETURN_UNEXPECTED(table->InitializeManifestCache(io_properties)); return table; } @@ -102,22 +101,36 @@ Table::Table(TableIdentifier identifier, std::shared_ptr metadata catalog_(std::move(catalog)), reporter_(std::move(reporter)), metadata_cache_(std::make_unique(metadata_.get())), - labels_(labels && !labels->empty() ? std::move(labels) : nullptr) {} + labels_(labels && !labels->empty() ? std::move(labels) : nullptr) { + InitializeManifestCache(); +} -Status Table::InitializeManifestCache( - const std::unordered_map& io_properties) { +void Table::InitializeManifestCache() { auto properties = io_->properties(); - for (const auto& [key, value] : io_properties) properties[key] = value; for (const auto& [key, value] : metadata_->properties.configs()) { properties[key] = value; } - ICEBERG_ASSIGN_OR_RAISE(auto options, - internal::ContentCacheOptions::FromProperties(properties)); - if (options.enabled) { - content_cache_ = std::make_shared(options); + auto options = internal::ContentCacheOptions::FromProperties(properties); + if (!options.has_value()) { + // Table construction can happen after the catalog has committed new metadata. + ICEBERG_LOG_WARN("Manifest cache disabled for table {}: {}", full_name_, + options.error().message); + return; + } + if (options->enabled) { + content_cache_ = std::make_shared(*options); manifest_io_ = content_cache_->Wrap(io_); } - return {}; +} + +const std::shared_ptr& internal::ManifestIO(const Table& table) { + return table.manifest_io_; +} + +void internal::InvalidateManifestCache(const Table& table, std::string_view location) { + if (table.content_cache_) { + table.content_cache_->Invalidate(location); + } } void Table::ClearManifestCache() { @@ -354,8 +367,7 @@ Result> StagedTable::Make( TableIdentifier identifier, std::shared_ptr metadata, std::string metadata_location, std::shared_ptr io, std::shared_ptr catalog, std::string full_name, - std::shared_ptr reporter, - const std::unordered_map& io_properties) { + std::shared_ptr reporter) { if (metadata == nullptr) [[unlikely]] { return InvalidArgument("Metadata cannot be null"); } @@ -369,7 +381,6 @@ Result> StagedTable::Make( new StagedTable(std::move(identifier), std::move(metadata), std::move(metadata_location), std::move(io), std::move(catalog), std::move(full_name), std::move(reporter), /*labels=*/nullptr)); - ICEBERG_RETURN_UNEXPECTED(table->InitializeManifestCache(io_properties)); return table; } @@ -392,7 +403,6 @@ Result> StaticTable::Make( new StaticTable(std::move(identifier), std::move(metadata), std::move(metadata_location), std::move(io), /*catalog=*/nullptr, std::move(full_name), /*reporter=*/nullptr, /*labels=*/nullptr)); - ICEBERG_RETURN_UNEXPECTED(table->InitializeManifestCache()); return table; } diff --git a/src/iceberg/table.h b/src/iceberg/table.h index 1bf0331bed..db21e9f74f 100644 --- a/src/iceberg/table.h +++ b/src/iceberg/table.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -40,7 +41,10 @@ namespace iceberg { namespace internal { class ContentCache; -} +// Used by snapshot readers and writers for immutable manifests and manifest lists. +ICEBERG_EXPORT const std::shared_ptr& ManifestIO(const Table& table); +void InvalidateManifestCache(const Table& table, std::string_view location); +} // namespace internal /// \brief Represents an Iceberg table /// @@ -59,15 +63,12 @@ class ICEBERG_EXPORT Table : public std::enable_shared_from_this
{ /// \param[in] reporter Optional metrics reporter for this table. Defaults to nullptr /// (noop). /// \param[in] labels Labels for this table, or nullptr when it has none. - /// \param[in] io_properties Catalog I/O defaults for the table manifest cache. - /// Table properties override these defaults; the budget is per loaded table. static Result> Make( TableIdentifier identifier, std::shared_ptr metadata, std::string metadata_location, std::shared_ptr io, std::shared_ptr catalog, std::string full_name = "", std::shared_ptr reporter = nullptr, - std::unique_ptr labels = nullptr, - const std::unordered_map& io_properties = {}); + std::unique_ptr labels = nullptr); virtual ~Table(); @@ -134,10 +135,6 @@ class ICEBERG_EXPORT Table : public std::enable_shared_from_this
{ /// \brief Returns a FileIO to read and write table data and metadata files const std::shared_ptr& io() const; - /// \brief I/O context for immutable manifest lists and manifests only. - /// Each loaded table has its own optional content cache, shared by its readers. - const std::shared_ptr& manifest_io() const { return manifest_io_; } - /// \brief Return whether this table caches manifest content. bool ManifestCacheEnabled() const { return content_cache_ != nullptr; } @@ -251,9 +248,6 @@ class ICEBERG_EXPORT Table : public std::enable_shared_from_this
{ Result> Inspect(MetadataTableKind kind) const; protected: - Status InitializeManifestCache( - const std::unordered_map& io_properties = {}); - Table(TableIdentifier identifier, std::shared_ptr metadata, std::string metadata_location, std::shared_ptr io, std::shared_ptr catalog, std::string full_name, @@ -274,6 +268,10 @@ class ICEBERG_EXPORT Table : public std::enable_shared_from_this
{ private: friend class Transaction; + friend const std::shared_ptr& internal::ManifestIO(const Table& table); + friend void internal::InvalidateManifestCache(const Table& table, + std::string_view location); + void InitializeManifestCache(); void ReuseManifestCache(const Table& source); }; @@ -284,8 +282,7 @@ class ICEBERG_EXPORT StagedTable final : public Table { TableIdentifier identifier, std::shared_ptr metadata, std::string metadata_location, std::shared_ptr io, std::shared_ptr catalog, std::string full_name = "", - std::shared_ptr reporter = nullptr, - const std::unordered_map& io_properties = {}); + std::shared_ptr reporter = nullptr); ~StagedTable() override; diff --git a/src/iceberg/table_scan.cc b/src/iceberg/table_scan.cc index 35a146cff8..f04227ae1f 100644 --- a/src/iceberg/table_scan.cc +++ b/src/iceberg/table_scan.cc @@ -335,7 +335,7 @@ Result>> TableScanBuilder:: auto builder = std::unique_ptr>(new TableScanBuilder( table.metadata(), table.io(), table.full_name(), table.reporter())); - builder->context_.manifest_io = table.manifest_io(); + builder->context_.manifest_io = internal::ManifestIO(table); return builder; } diff --git a/src/iceberg/table_scan.h b/src/iceberg/table_scan.h index 777a9e3de1..c6fa37b50c 100644 --- a/src/iceberg/table_scan.h +++ b/src/iceberg/table_scan.h @@ -541,6 +541,10 @@ class ICEBERG_EXPORT TableScan { const std::shared_ptr metadata_; const std::shared_ptr schema_; + const std::shared_ptr& manifest_io() const { + return context_.manifest_io ? context_.manifest_io : io_; + } + const std::shared_ptr io_; const internal::TableScanContext context_; mutable std::shared_ptr projected_schema_; diff --git a/src/iceberg/test/arrow_io_test.cc b/src/iceberg/test/arrow_io_test.cc index 3ed0e39ba4..b9a9813cc5 100644 --- a/src/iceberg/test/arrow_io_test.cc +++ b/src/iceberg/test/arrow_io_test.cc @@ -104,6 +104,10 @@ class SizeFailureFileSystem : public ::arrow::fs::LocalFileSystem { using ::arrow::fs::LocalFileSystem::OpenInputFile; explicit SizeFailureFileSystem(bool fail_close) : input(std::make_shared(fail_close)) {} + using ::arrow::fs::LocalFileSystem::GetFileInfo; + ::arrow::Result<::arrow::fs::FileInfo> GetFileInfo(const std::string& path) override { + return ::arrow::fs::FileInfo(path, ::arrow::fs::FileType::File); + } ::arrow::Result> OpenInputFile( const ::arrow::fs::FileInfo&) override { return input; @@ -421,7 +425,7 @@ class LocalFileIOTest : public TempFileTestBase { TEST_F(LocalFileIOTest, ReadWriteFile) { auto read_res = file_io_->ReadFile(temp_filepath_, std::nullopt); EXPECT_THAT(read_res, IsError(ErrorKind::kIOError)); - EXPECT_THAT(read_res, HasErrorMessage("Failed to open local file")); + EXPECT_THAT(read_res, HasErrorMessage("File does not exist")); auto write_res = file_io_->WriteFile(temp_filepath_, "hello world"); EXPECT_THAT(write_res, IsOk()); @@ -431,20 +435,18 @@ TEST_F(LocalFileIOTest, ReadWriteFile) { EXPECT_THAT(read_res, HasValue(::testing::Eq("hello world"))); } -TEST_F(LocalFileIOTest, SizeReleasesTemporaryArrowFile) { +TEST_F(LocalFileIOTest, SizeUsesFileInfoWithoutOpeningStream) { auto fs = std::make_shared(); auto file_io = std::make_shared(fs); ASSERT_THAT(file_io->WriteFile(temp_filepath_, "hello"), IsOk()); ICEBERG_UNWRAP_OR_FAIL(auto input_file, file_io->NewInputFile(temp_filepath_)); EXPECT_THAT(input_file->Size(), HasValue(::testing::Eq(5))); - ASSERT_EQ(fs->opened_inputs.size(), 1); - EXPECT_TRUE(fs->opened_inputs.front()->closed()); - + EXPECT_TRUE(fs->opened_inputs.empty()); EXPECT_THAT(input_file->Size(), HasValue(::testing::Eq(5))); - EXPECT_EQ(fs->opened_inputs.size(), 1); + EXPECT_TRUE(fs->opened_inputs.empty()); ICEBERG_UNWRAP_OR_FAIL(auto stream, input_file->Open()); - ASSERT_EQ(fs->opened_inputs.size(), 2); + ASSERT_EQ(fs->opened_inputs.size(), 1); EXPECT_FALSE(fs->opened_inputs.back()->closed()); std::array data; ASSERT_THAT(stream->ReadFully(0, data), IsOk()); @@ -454,24 +456,23 @@ TEST_F(LocalFileIOTest, SizeReleasesTemporaryArrowFile) { EXPECT_TRUE(fs->opened_inputs.back()->closed()); } -TEST_F(LocalFileIOTest, CachedInputCreationReleasesTemporaryArrowFile) { +TEST_F(LocalFileIOTest, CachedInputCreationUsesFileInfoWithoutOpeningStream) { auto fs = std::make_shared(); auto file_io = std::make_shared(fs); ASSERT_THAT(file_io->WriteFile(temp_filepath_, "hello"), IsOk()); + auto metadata = std::make_shared(); + metadata->properties = TableProperties::FromMap( + {{std::string(internal::ContentCacheOptions::kEnabled), "true"}}); ICEBERG_UNWRAP_OR_FAIL( - auto table, - Table::Make({.ns = {}, .name = "test"}, std::make_shared(), - "metadata.json", file_io, std::make_shared(), "", nullptr, - nullptr, - {{std::string(internal::ContentCacheOptions::kEnabled), "true"}})); + auto table, Table::Make({.ns = {}, .name = "test"}, std::move(metadata), + "metadata.json", file_io, std::make_shared())); ICEBERG_UNWRAP_OR_FAIL(auto input_file, - table->manifest_io()->NewInputFile(temp_filepath_)); - ASSERT_EQ(fs->opened_inputs.size(), 1); - EXPECT_TRUE(fs->opened_inputs.front()->closed()); + internal::ManifestIO(*table)->NewInputFile(temp_filepath_)); + EXPECT_TRUE(fs->opened_inputs.empty()); // Opening loads the content into the cache and closes the underlying file. ICEBERG_UNWRAP_OR_FAIL(auto stream, input_file->Open()); - ASSERT_EQ(fs->opened_inputs.size(), 2); + ASSERT_EQ(fs->opened_inputs.size(), 1); EXPECT_TRUE(fs->opened_inputs.back()->closed()); ASSERT_THAT(stream->Close(), IsOk()); } @@ -566,26 +567,6 @@ TEST(ArrowFileIOTest, OutputStoredLengthAfterClose) { EXPECT_THAT(output->StoredLength(), HasValue(::testing::Eq(3))); } -TEST(ArrowFileIOTest, OversizedCachedInputRetainsNativeArrowStream) { - std::shared_ptr file_io = arrow::ArrowFileSystemFileIO::MakeMockFileIO(); - ASSERT_THAT(file_io->WriteFile("input", "manifest"), IsOk()); - - ICEBERG_UNWRAP_OR_FAIL(auto uncached, arrow::OpenArrowInputStream(file_io, "input")); - ICEBERG_UNWRAP_OR_FAIL( - auto table, - Table::Make( - {.ns = {}, .name = "test"}, std::make_shared(), "metadata.json", - file_io, std::make_shared(), "", nullptr, nullptr, - {{std::string(internal::ContentCacheOptions::kEnabled), "true"}, - {std::string(internal::ContentCacheOptions::kMaxContentLength), "4"}})); - ICEBERG_UNWRAP_OR_FAIL(auto oversized, - arrow::OpenArrowInputStream(table->manifest_io(), "input")); - - const auto& oversized_stream = *oversized; - const auto& uncached_stream = *uncached; - EXPECT_TRUE(typeid(oversized_stream) == typeid(uncached_stream)); -} - TEST_F(LocalFileIOTest, ResolvesForeignSchemeToUnderlyingPath) { ASSERT_THAT(file_io_->WriteFile(temp_filepath_, "hello world"), IsOk()); diff --git a/src/iceberg/test/fast_append_test.cc b/src/iceberg/test/fast_append_test.cc index fda41a0e55..dfc4034932 100644 --- a/src/iceberg/test/fast_append_test.cc +++ b/src/iceberg/test/fast_append_test.cc @@ -340,6 +340,29 @@ TEST_F(FastAppendTest, AppendNullFile) { EXPECT_THAT(table_->current_snapshot(), HasErrorMessage("No current snapshot")); } +TEST_F(FastAppendTest, InvalidManifestCacheSettingsDoNotDeleteCommittedFiles) { + ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); + ICEBERG_UNWRAP_OR_FAIL(auto append, txn->NewFastAppend()); + append->AppendFile(file_a_); + ASSERT_THAT(append->Commit(), IsOk()); + + ICEBERG_UNWRAP_OR_FAIL(auto update, txn->NewUpdateProperties()); + update->Set("io.manifest.cache-enabled", "true"); + update->Set("io.manifest.cache.max-total-bytes", "invalid"); + ASSERT_THAT(update->Commit(), IsOk()); + + ICEBERG_UNWRAP_OR_FAIL(auto committed, txn->Commit()); + EXPECT_FALSE(committed->ManifestCacheEnabled()); + ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); + ICEBERG_UNWRAP_OR_FAIL(auto snapshot, reloaded->current_snapshot()); + SnapshotReader reader(snapshot.get()); + ICEBERG_UNWRAP_OR_FAIL(auto manifests, reader.Manifests(file_io_)); + ASSERT_FALSE(manifests.empty()); + for (const auto& manifest : manifests) { + EXPECT_THAT(file_io_->ReadFile(manifest.manifest_path, std::nullopt), IsOk()); + } +} + TEST_F(FastAppendTest, AbortIgnoresCleanupDeleteFailure) { ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); ICEBERG_UNWRAP_OR_FAIL(auto fast_append, txn->NewFastAppend()); diff --git a/src/iceberg/test/manifest_reader_test.cc b/src/iceberg/test/manifest_reader_test.cc index 311398f1ff..1a8616d5ba 100644 --- a/src/iceberg/test/manifest_reader_test.cc +++ b/src/iceberg/test/manifest_reader_test.cc @@ -800,11 +800,11 @@ TEST_P(TestManifestReader, ReadsManifestListAndManifestFromCachedContent) { auto fs = std::make_shared(std::chrono::system_clock::now()); file_io_ = std::make_shared(fs); auto table_metadata = std::make_shared(); + table_metadata->properties = TableProperties::FromMap( + {{std::string(internal::ContentCacheOptions::kEnabled), "true"}}); ICEBERG_UNWRAP_OR_FAIL( - auto table, - Table::Make({.ns = {}, .name = "test"}, table_metadata, "metadata.json", file_io_, - std::make_shared(), "", nullptr, nullptr, - {{std::string(internal::ContentCacheOptions::kEnabled), "true"}})); + auto table, Table::Make({.ns = {}, .name = "test"}, table_metadata, "metadata.json", + file_io_, std::make_shared())); std::vector entries; entries.push_back(MakeEntry( @@ -826,7 +826,8 @@ TEST_P(TestManifestReader, ReadsManifestListAndManifestFromCachedContent) { ASSERT_THAT(list_writer->Close(), IsOk()); auto read_manifest_list = [&]() -> std::vector { - auto reader = ManifestListReader::Make(manifest_list_path, table->manifest_io()); + auto reader = + ManifestListReader::Make(manifest_list_path, internal::ManifestIO(*table)); EXPECT_THAT(reader, IsOk()); if (!reader.has_value()) { return {}; @@ -837,7 +838,8 @@ TEST_P(TestManifestReader, ReadsManifestListAndManifestFromCachedContent) { }; auto read_data_file_paths = [&](const ManifestFile& manifest) { std::vector paths; - auto reader = ManifestReader::Make(manifest, table->manifest_io(), schema_, spec_); + auto reader = + ManifestReader::Make(manifest, internal::ManifestIO(*table), schema_, spec_); EXPECT_THAT(reader, IsOk()); if (!reader.has_value()) { return paths; diff --git a/src/iceberg/test/metadata_cache_test.cc b/src/iceberg/test/metadata_cache_test.cc index f286b270e6..41370da3a8 100644 --- a/src/iceberg/test/metadata_cache_test.cc +++ b/src/iceberg/test/metadata_cache_test.cc @@ -131,10 +131,11 @@ Result> MakeCachedTable( std::string metadata_location = "metadata.json") { auto metadata = std::make_shared(); metadata->table_uuid = "cache-test-table"; + metadata->properties = TableProperties::FromMap(properties); auto storage = std::shared_ptr(&io, [](FileIO*) {}); return Table::Make({.ns = {}, .name = "test"}, std::move(metadata), std::move(metadata_location), std::move(storage), - std::make_shared(), "", nullptr, nullptr, properties); + std::make_shared()); } // Read a whole immutable file through a table's manifest I/O context. @@ -207,13 +208,13 @@ TEST(MetadataCacheTest, TableReusesCachedContentAcrossInputFiles) { auto table, MakeCachedTable(file_io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); - auto first_file = table->manifest_io()->NewInputFile("manifest.avro"); + auto first_file = internal::ManifestIO(*table)->NewInputFile("manifest.avro"); ASSERT_THAT(first_file, IsOk()); auto first_stream = first_file.value()->Open(); ASSERT_THAT(first_stream, IsOk()); ASSERT_THAT(first_stream.value()->Close(), IsOk()); - auto second_file = table->manifest_io()->NewInputFile("manifest.avro"); + auto second_file = internal::ManifestIO(*table)->NewInputFile("manifest.avro"); ASSERT_THAT(second_file, IsOk()); auto second_stream = second_file.value()->Open(); ASSERT_THAT(second_stream, IsOk()); @@ -231,7 +232,8 @@ TEST(MetadataCacheTest, TableUsesNormalizedLocationForCacheHits) { for (const auto* location : {"alias://manifest.avro", "alias://manifest.avro", "manifest.avro"}) { - ICEBERG_UNWRAP_OR_FAIL(auto input_file, table->manifest_io()->NewInputFile(location)); + ICEBERG_UNWRAP_OR_FAIL(auto input_file, + internal::ManifestIO(*table)->NewInputFile(location)); EXPECT_EQ(input_file->location(), "manifest.avro"); ICEBERG_UNWRAP_OR_FAIL(auto stream, input_file->Open()); ASSERT_THAT(stream->Close(), IsOk()); @@ -251,7 +253,7 @@ TEST(MetadataCacheTest, CachedInputFileDoesNotRetryNonIOErrors) { auto table, MakeCachedTable(file_io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); ICEBERG_UNWRAP_OR_FAIL(auto input_file, - table->manifest_io()->NewInputFile("manifest.avro")); + internal::ManifestIO(*table)->NewInputFile("manifest.avro")); EXPECT_THAT(input_file->Open(), IsError(ErrorKind::kNotFound)); EXPECT_EQ(state->open_count, 1); @@ -267,7 +269,7 @@ TEST(MetadataCacheTest, CachedInputFilePreservesReadAheadAndFallbackErrors) { auto table, MakeCachedTable(file_io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); ICEBERG_UNWRAP_OR_FAIL(auto input_file, - table->manifest_io()->NewInputFile("manifest.avro")); + internal::ManifestIO(*table)->NewInputFile("manifest.avro")); auto result = input_file->Open(); EXPECT_THAT(result, IsError(ErrorKind::kIOError)); @@ -284,11 +286,11 @@ TEST(MetadataCacheTest, TableDoesNotCacheLoadFailures) { MakeCachedTable(file_io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); file_io.failing_opens = 1; - EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + EXPECT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), IsError(ErrorKind::kNotFound)); - EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + EXPECT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), HasValue(::testing::Eq("manifest"))); - EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + EXPECT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), HasValue(::testing::Eq("manifest"))); // The failed open, then the successful load. The last read comes from the cache. EXPECT_EQ(file_io.open_count, 2); @@ -308,14 +310,14 @@ TEST(MetadataCacheTest, TableReadsUncacheableFilesDirectly) { // The 8-byte file can never be cached, so opening it must not load the whole file // into memory first. ICEBERG_UNWRAP_OR_FAIL(auto input_file, - table->manifest_io()->NewInputFile("manifest.avro")); + internal::ManifestIO(*table)->NewInputFile("manifest.avro")); ICEBERG_UNWRAP_OR_FAIL(auto stream, input_file->Open()); EXPECT_EQ(file_io.read_count, 0); ASSERT_THAT(stream->Close(), IsOk()); - EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + EXPECT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), HasValue(::testing::Eq("manifest"))); - EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + EXPECT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), HasValue(::testing::Eq("manifest"))); EXPECT_EQ(file_io.open_count, 3); } @@ -334,7 +336,7 @@ TEST(MetadataCacheTest, TableEvictsLeastRecentlyUsedContentByBytes) { // Return the number of underlying opens after reading `location`. auto read = [&file_io, &table](const std::string& location) { - EXPECT_THAT(ReadCached(*table->manifest_io(), location), IsOk()); + EXPECT_THAT(ReadCached(*internal::ManifestIO(*table), location), IsOk()); return file_io.open_count; }; EXPECT_EQ(read("a"), 1); @@ -352,15 +354,40 @@ TEST(MetadataCacheTest, TableClearsCachedContent) { ICEBERG_UNWRAP_OR_FAIL( auto table, MakeCachedTable(file_io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); - ASSERT_THAT(ReadCached(*table->manifest_io(), "snap.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "snap.avro"), IsOk()); table->ClearManifestCache(); - ASSERT_THAT(ReadCached(*table->manifest_io(), "snap.avro"), IsOk()); - ASSERT_THAT(ReadCached(*table->manifest_io(), "snap.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "snap.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "snap.avro"), IsOk()); EXPECT_EQ(file_io.open_count, 2); } +TEST(MetadataCacheTest, ManifestDeletesInvalidateOnlyTheAttemptedLocations) { + CountingMockFileIO io; + io.AddFile("single.avro", "old"); + io.AddFile("batch.avro", "old"); + io.AddFile("retained.avro", "retained"); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); + auto manifest_io = internal::ManifestIO(*table); + for (const auto* name : {"single.avro", "batch.avro", "retained.avro"}) { + ASSERT_THAT(ReadCached(*manifest_io, name), IsOk()); + } + EXPECT_CALL(io, DeleteFile("single.avro")).WillOnce(::testing::Return(Status{})); + EXPECT_CALL(io, DeleteFile("batch.avro")).WillOnce(::testing::Return(Status{})); + ASSERT_THAT(manifest_io->DeleteFile("single.avro"), IsOk()); + ASSERT_THAT(manifest_io->DeleteFiles({"batch.avro"}), IsOk()); + for (const auto* name : {"single.avro", "batch.avro"}) { + io.AddFile(name, "new"); + EXPECT_THAT(ReadCached(*manifest_io, name), HasValue(::testing::Eq("new"))); + } + EXPECT_THAT(ReadCached(*manifest_io, "retained.avro"), + HasValue(::testing::Eq("retained"))); + EXPECT_EQ(io.open_count, 5); +} + TEST(MetadataCacheTest, TableConcurrentReadsReturnFileContent) { // CountingMockFileIO's counters are not thread-safe, so use the plain mock. MockFileIO file_io; @@ -385,7 +412,7 @@ TEST(MetadataCacheTest, TableConcurrentReadsReturnFileContent) { workers.push_back(std::async(std::launch::async, [&, thread] { for (int i = 0; i < kIterations; ++i) { const int id = (thread + i) % kLocations; - auto content = ReadCached(*table->manifest_io(), std::to_string(id)); + auto content = ReadCached(*internal::ManifestIO(*table), std::to_string(id)); if (!content.has_value() || content.value() != contents[id]) { mismatch.store(true, std::memory_order_relaxed); } @@ -453,22 +480,12 @@ TEST(MetadataCacheTest, InvalidNumericPropertyErrorIncludesPropertyName) { EXPECT_THAT(options, HasErrorMessage(std::string(ContentCacheOptions::kMaxTotalBytes))); } -TEST(MetadataCacheTest, ParsesTuningPropertiesWhenDisabled) { - auto options = ContentCacheOptions::FromProperties( - {{std::string(ContentCacheOptions::kEnabled), "false"}, - {std::string(ContentCacheOptions::kMaxTotalBytes), "1024"}}); - - ASSERT_THAT(options, IsOk()); - EXPECT_FALSE(options->enabled); - EXPECT_EQ(options->max_total_bytes, 1024); -} - -TEST(MetadataCacheTest, RejectsInvalidNumericPropertyWhenDisabled) { - auto options = ContentCacheOptions::FromProperties( - {{std::string(ContentCacheOptions::kEnabled), "false"}, - {std::string(ContentCacheOptions::kMaxTotalBytes), "invalid"}}); - - EXPECT_THAT(options, HasErrorMessage(std::string(ContentCacheOptions::kMaxTotalBytes))); +TEST(MetadataCacheTest, IgnoresInvalidTuningPropertiesWhenDisabled) { + ICEBERG_UNWRAP_OR_FAIL( + auto options, ContentCacheOptions::FromProperties( + {{std::string(ContentCacheOptions::kEnabled), "false"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "invalid"}})); + EXPECT_FALSE(options.enabled); } TEST(MetadataCacheTest, ReaderOptionsKeepsPositionalAggregateInitialization) { @@ -499,8 +516,8 @@ TEST(MetadataCacheTest, RejectsOversizedLengthBeforeCallingStorage) { ICEBERG_UNWRAP_OR_FAIL( auto table, MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); - EXPECT_THAT(table->manifest_io()->NewInputFile("manifest.avro", - std::numeric_limits::max()), + EXPECT_THAT(internal::ManifestIO(*table)->NewInputFile( + "manifest.avro", std::numeric_limits::max()), IsError(ErrorKind::kInvalidArgument)); EXPECT_EQ(io.length_calls, 0); } @@ -510,9 +527,9 @@ TEST(MetadataCacheTest, DisabledTableUsesStorageDirectly) { io.AddFile("manifest.avro", "manifest"); ICEBERG_UNWRAP_OR_FAIL(auto table, MakeCachedTable(io)); EXPECT_FALSE(table->ManifestCacheEnabled()); - EXPECT_EQ(table->manifest_io(), table->io()); - ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); - ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + EXPECT_EQ(internal::ManifestIO(*table), table->io()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), IsOk()); EXPECT_EQ(io.open_count, 2); } @@ -527,10 +544,10 @@ TEST(MetadataCacheTest, TablesSharingStorageHaveIndependentCachesAndLimits) { auto second, MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}, {std::string(ContentCacheOptions::kMaxTotalBytes), "4"}})); - ASSERT_THAT(ReadCached(*first->manifest_io(), "manifest.avro"), IsOk()); - ASSERT_THAT(ReadCached(*second->manifest_io(), "manifest.avro"), IsOk()); - ASSERT_THAT(ReadCached(*first->manifest_io(), "manifest.avro"), IsOk()); - ASSERT_THAT(ReadCached(*second->manifest_io(), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*first), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*second), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*first), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*second), "manifest.avro"), IsOk()); EXPECT_EQ(io.open_count, 3); // Ordinary FileIO input remains independent of either table's cache. ASSERT_THAT(ReadCached(io, "manifest.avro"), IsOk()); @@ -543,15 +560,15 @@ TEST(MetadataCacheTest, CompatibleRefreshPreservesCachedContent) { std::unordered_map props = { {std::string(ContentCacheOptions::kEnabled), "true"}}; ICEBERG_UNWRAP_OR_FAIL(auto table, MakeCachedTable(io, props)); - ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), IsOk()); ICEBERG_UNWRAP_OR_FAIL(auto refreshed, MakeCachedTable(io, props, "metadata-2.json")); auto catalog = std::dynamic_pointer_cast(table->catalog()); EXPECT_CALL(*catalog, LoadTable(::testing::_)).WillOnce(::testing::Return(refreshed)); ASSERT_THAT(table->Refresh(), IsOk()); - ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), IsOk()); EXPECT_EQ(io.open_count, 1); table->ClearManifestCache(); - ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), IsOk()); EXPECT_EQ(io.open_count, 2); } @@ -561,14 +578,14 @@ TEST(MetadataCacheTest, RefreshWithChangedLimitsReplacesCache) { std::unordered_map props = { {std::string(ContentCacheOptions::kEnabled), "true"}}; ICEBERG_UNWRAP_OR_FAIL(auto table, MakeCachedTable(io, props)); - ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), IsOk()); props[std::string(ContentCacheOptions::kMaxTotalBytes)] = "4"; ICEBERG_UNWRAP_OR_FAIL(auto refreshed, MakeCachedTable(io, props, "metadata-2.json")); auto catalog = std::dynamic_pointer_cast(table->catalog()); EXPECT_CALL(*catalog, LoadTable(::testing::_)).WillOnce(::testing::Return(refreshed)); ASSERT_THAT(table->Refresh(), IsOk()); - ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); - ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), IsOk()); EXPECT_EQ(io.open_count, 3); } @@ -580,29 +597,28 @@ TEST(MetadataCacheTest, RefreshWithDifferentStorageDiscardsCachedBytes) { std::unordered_map props = { {std::string(ContentCacheOptions::kEnabled), "true"}}; ICEBERG_UNWRAP_OR_FAIL(auto table, MakeCachedTable(first_io, props)); - ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), HasValue(::testing::Eq("first"))); ICEBERG_UNWRAP_OR_FAIL(auto refreshed, MakeCachedTable(second_io, props, "metadata-2.json")); auto catalog = std::dynamic_pointer_cast(table->catalog()); EXPECT_CALL(*catalog, LoadTable(::testing::_)).WillOnce(::testing::Return(refreshed)); ASSERT_THAT(table->Refresh(), IsOk()); - EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + EXPECT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), HasValue(::testing::Eq("second"))); } -TEST(MetadataCacheTest, RejectsInvalidCacheLimitsAtTableCreation) { +TEST(MetadataCacheTest, InvalidCacheLimitsFallBackToStorage) { for (const auto key : {ContentCacheOptions::kMaxTotalBytes, ContentCacheOptions::kMaxContentLength}) { for (const auto value : {"0", "-1", "9223372036854775808", "invalid"}) { CountingMockFileIO io; - EXPECT_THAT( + ICEBERG_UNWRAP_OR_FAIL( + auto table, MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}, - {std::string(key), value}}), - IsError(ErrorKind::kInvalidArgument)); - EXPECT_THAT( - MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}}), - IsOk()); + {std::string(key), value}})); + EXPECT_FALSE(table->ManifestCacheEnabled()); + EXPECT_EQ(internal::ManifestIO(*table), table->io()); } } } @@ -615,9 +631,9 @@ TEST(MetadataCacheTest, EmptyFilesAreNotRetained) { {std::string(ContentCacheOptions::kMaxTotalBytes), "1"}})); for (int i = 0; i < 100; ++i) { io.AddFile(std::to_string(i), ""); - ASSERT_THAT(ReadCached(*table->manifest_io(), std::to_string(i)), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), std::to_string(i)), IsOk()); } - ASSERT_THAT(ReadCached(*table->manifest_io(), "0"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "0"), IsOk()); EXPECT_EQ(io.open_count, 101); } @@ -639,17 +655,15 @@ class UnavailableNormalizingFileIO : public NormalizingMockFileIO { } }; -TEST(MetadataCacheTest, NormalizedAliasHitDoesNotResolveStorage) { +TEST(MetadataCacheTest, CanonicalLocationHitDoesNotResolveStorage) { UnavailableNormalizingFileIO io; io.AddFile("manifest.avro", "manifest"); ICEBERG_UNWRAP_OR_FAIL( auto table, MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); - ASSERT_THAT(ReadCached(*table->manifest_io(), "alias://manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "alias://manifest.avro"), IsOk()); io.unavailable = true; - EXPECT_THAT(ReadCached(*table->manifest_io(), "alias://manifest.avro"), - HasValue(::testing::Eq("manifest"))); - EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + EXPECT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), HasValue(::testing::Eq("manifest"))); EXPECT_EQ(io.open_count, 1); } @@ -660,9 +674,9 @@ TEST(MetadataCacheTest, HitDoesNotResolveStorage) { ICEBERG_UNWRAP_OR_FAIL( auto table, MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); - ASSERT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), IsOk()); io.unavailable = true; - EXPECT_THAT(ReadCached(*table->manifest_io(), "manifest.avro"), + EXPECT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), HasValue(::testing::Eq("manifest"))); } diff --git a/src/iceberg/test/transaction_test.cc b/src/iceberg/test/transaction_test.cc index be3e199427..c10a6ad7ec 100644 --- a/src/iceberg/test/transaction_test.cc +++ b/src/iceberg/test/transaction_test.cc @@ -105,7 +105,7 @@ TEST_F(TransactionTest, CommitPreservesTableManifestContentCache) { const auto manifest_path = table_location_ + "/metadata/cached-manifest.avro"; ASSERT_THAT(file_io_->WriteFile(manifest_path, "manifest bytes"), IsOk()); - ASSERT_THAT(table_->manifest_io()->ReadFile(manifest_path, std::nullopt), + ASSERT_THAT(internal::ManifestIO(*table_)->ReadFile(manifest_path, std::nullopt), HasValue(::testing::Eq("manifest bytes"))); ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); @@ -114,7 +114,7 @@ TEST_F(TransactionTest, CommitPreservesTableManifestContentCache) { ASSERT_THAT(update->Commit(), IsOk()); ICEBERG_UNWRAP_OR_FAIL(auto committed, txn->Commit()); ASSERT_THAT(file_io_->DeleteFile(manifest_path), IsOk()); - EXPECT_THAT(committed->manifest_io()->ReadFile(manifest_path, std::nullopt), + EXPECT_THAT(internal::ManifestIO(*committed)->ReadFile(manifest_path, std::nullopt), HasValue(::testing::Eq("manifest bytes"))); } diff --git a/src/iceberg/transaction.cc b/src/iceberg/transaction.cc index c19b84d3f3..148de92fa7 100644 --- a/src/iceberg/transaction.cc +++ b/src/iceberg/transaction.cc @@ -650,7 +650,7 @@ void Transaction::FinalizeUpdates(const TableMetadata& committed) noexcept { } ICEBERG_ASSIGN_OR_RAISE( committed_files, - CommittedTransactionFiles(committed, ctx_->table->manifest_io(), + CommittedTransactionFiles(committed, internal::ManifestIO(*ctx_->table), std::move(snapshot_ids), std::move(content_paths))); return {}; }); diff --git a/src/iceberg/update/cherry_pick_operation.cc b/src/iceberg/update/cherry_pick_operation.cc index 3856c98da6..b9313028f9 100644 --- a/src/iceberg/update/cherry_pick_operation.cc +++ b/src/iceberg/update/cherry_pick_operation.cc @@ -241,7 +241,7 @@ CherryPickOperation& CherryPickOperation::Cherrypick(int64_t snapshot_id) { replaced_partitions_.emplace(); } - auto io = ctx_->table->manifest_io(); + auto io = internal::ManifestIO(*ctx_->table); ICEBERG_BUILDER_ASSIGN_OR_RETURN( auto changes, ReadSnapshotChanges(*cherrypick_snapshot_, io, current)); @@ -306,7 +306,7 @@ Status CherryPickOperation::ValidateReplacedPartitions(const TableMetadata& meta // Walk back from the current snapshot to the picked snapshot's parent and // reject any file added into a partition this pick replaces. - auto io = ctx_->table->manifest_io(); + auto io = internal::ManifestIO(*ctx_->table); ICEBERG_ASSIGN_OR_RAISE(auto snapshots, SnapshotUtil::AncestorsBetween( meta, meta.current_snapshot_id, parent_id)); // TODO: Parallelize added-file validation using the planning executor. diff --git a/src/iceberg/update/expire_snapshots.cc b/src/iceberg/update/expire_snapshots.cc index 8805f6d151..90dfd28230 100644 --- a/src/iceberg/update/expire_snapshots.cc +++ b/src/iceberg/update/expire_snapshots.cc @@ -65,10 +65,11 @@ Result> MakeManifestReader( /// \brief Abstract strategy for cleaning up files after snapshot expiration. class FileCleanupStrategy { public: - FileCleanupStrategy(std::shared_ptr file_io, + FileCleanupStrategy(const Table& table, std::function delete_func, OptionalExecutor delete_executor, OptionalExecutor plan_executor) - : file_io_(std::move(file_io)), + : table_(table), + file_io_(table.io()), delete_func_(std::move(delete_func)), delete_executor_(std::move(delete_executor)), plan_executor_(std::move(plan_executor)) {} @@ -104,7 +105,8 @@ class FileCleanupStrategy { } /// \brief Best-effort delete with bounded retry. - void DeleteFiles(const std::unordered_set& paths) { + void DeleteFiles(const std::unordered_set& paths, + bool invalidate_manifest_cache = false) { if (paths.empty()) { return; } @@ -119,9 +121,17 @@ class FileCleanupStrategy { if (auto status = std::move(group).Run(); !status) { ICEBERG_LOG_WARN("Expiration deletion failed: {}", status.error().message); } - return; + } else { + DeleteWithCallback(paths); } + if (invalidate_manifest_cache) { + for (const auto& path : paths) { + internal::InvalidateManifestCache(table_, path); + } + } + } + void DeleteWithCallback(const std::unordered_set& paths) { TaskGroup> group(kDeleteRetryConfig); group.SetExecutor(delete_executor_); for (const auto& path : paths) { @@ -178,6 +188,7 @@ class FileCleanupStrategy { return stats_files_to_delete; } + const Table& table_; std::shared_ptr file_io_; std::function delete_func_; OptionalExecutor delete_executor_; @@ -248,12 +259,13 @@ class ReachableFileCleanup : public FileCleanupStrategy { } // Deleting manifest files - DeleteFiles(ManifestPaths(manifests_to_delete)); + DeleteFiles(ManifestPaths(manifests_to_delete), + /*invalidate_manifest_cache=*/true); } } // Deleting manifest-list files - DeleteFiles(manifest_lists_to_delete); + DeleteFiles(manifest_lists_to_delete, /*invalidate_manifest_cache=*/true); // Deleting statistics files if (HasAnyStatisticsFiles(metadata_before_expiration)) { @@ -593,10 +605,10 @@ class IncrementalFileCleanup : public FileCleanupStrategy { } // Deleting manifest files - DeleteFiles(manifests_to_delete); + DeleteFiles(manifests_to_delete, /*invalidate_manifest_cache=*/true); // Deleting manifest-list files - DeleteFiles(manifest_lists_to_delete); + DeleteFiles(manifest_lists_to_delete, /*invalidate_manifest_cache=*/true); // Deleting statistics files if (HasAnyStatisticsFiles(metadata_before_expiration)) { @@ -979,7 +991,8 @@ Result ExpireSnapshots::Apply() { ICEBERG_ASSIGN_OR_RAISE(auto snapshot, base.SnapshotById(snapshot_id)); SnapshotReader snapshot_reader(snapshot.get()); ICEBERG_ASSIGN_OR_RAISE( - auto manifests, snapshot_reader.Manifests(ctx_->table->manifest_io())); + auto manifests, + snapshot_reader.Manifests(internal::ManifestIO(*ctx_->table))); for (const auto& manifest : manifests) { spec_ids.insert(manifest.partition_spec_id); } @@ -1038,12 +1051,12 @@ Status ExpireSnapshots::Finalize(const TableMetadata& metadata_after_expiration) !HasNonMainSnapshots(metadata_after_expiration); if (can_use_incremental) { - return IncrementalFileCleanup(ctx_->table->manifest_io(), delete_func_, - delete_executor_, plan_executor_) + return IncrementalFileCleanup(*ctx_->table, delete_func_, delete_executor_, + plan_executor_) .CleanFiles(metadata_before_expiration, metadata_after_expiration, cleanup_level_); } - return ReachableFileCleanup(ctx_->table->manifest_io(), delete_func_, delete_executor_, + return ReachableFileCleanup(*ctx_->table, delete_func_, delete_executor_, plan_executor_) .CleanFiles(metadata_before_expiration, metadata_after_expiration, cleanup_level_); } diff --git a/src/iceberg/update/fast_append.cc b/src/iceberg/update/fast_append.cc index f1c177a47d..6f05742559 100644 --- a/src/iceberg/update/fast_append.cc +++ b/src/iceberg/update/fast_append.cc @@ -128,8 +128,9 @@ Result> FastAppend::Apply( // Add all manifests from the snapshot if (snapshot != nullptr) { auto snapshot_reader = SnapshotReader(snapshot.get()); - ICEBERG_ASSIGN_OR_RAISE(auto snapshot_manifests, - snapshot_reader.Manifests(ctx_->table->manifest_io())); + ICEBERG_ASSIGN_OR_RAISE( + auto snapshot_manifests, + snapshot_reader.Manifests(internal::ManifestIO(*ctx_->table))); manifests.insert(manifests.end(), snapshot_manifests.begin(), snapshot_manifests.end()); } @@ -184,10 +185,10 @@ Result FastAppend::CopyManifest(const ManifestFile& manifest) { // A failed copy must not leave partial counts in the cached summary. SnapshotSummaryBuilder summary; - ICEBERG_ASSIGN_OR_RAISE( - auto copied, - CopyAppendManifest(manifest, ctx_->table->manifest_io(), schema, spec, snapshot_id, - new_manifest_path, current.format_version, &summary)); + ICEBERG_ASSIGN_OR_RAISE(auto copied, + CopyAppendManifest(manifest, internal::ManifestIO(*ctx_->table), + schema, spec, snapshot_id, new_manifest_path, + current.format_version, &summary)); copied_manifests_summary_.Merge(summary); return copied; } diff --git a/src/iceberg/update/merging_snapshot_update.cc b/src/iceberg/update/merging_snapshot_update.cc index caf424d70a..dd4f3b4f2f 100644 --- a/src/iceberg/update/merging_snapshot_update.cc +++ b/src/iceberg/update/merging_snapshot_update.cc @@ -409,7 +409,7 @@ MergingSnapshotUpdate::MergingSnapshotUpdate(std::string table_name, : SnapshotUpdate(std::move(ctx)), table_name_(std::move(table_name)), delete_expression_(Expressions::AlwaysFalse()) { - auto file_io = ctx_->table->manifest_io(); + auto file_io = internal::ManifestIO(*ctx_->table); auto data_filter_manager = ManifestFilterManager::Make( ManifestContent::kData, file_io, [this](const std::string& location) { RegisterStagedFile(location); @@ -689,8 +689,8 @@ Result MergingSnapshotUpdate::CopyManifest(const ManifestFile& man SnapshotSummaryBuilder summary; ICEBERG_ASSIGN_OR_RAISE( auto copied, - CopyAppendManifest(manifest, ctx_->table->manifest_io(), schema, spec, SnapshotId(), - path, current.format_version, &summary)); + CopyAppendManifest(manifest, internal::ManifestIO(*ctx_->table), schema, spec, + SnapshotId(), path, current.format_version, &summary)); copied_manifests_summary_.Merge(summary); return copied; } diff --git a/src/iceberg/update/overwrite_files.cc b/src/iceberg/update/overwrite_files.cc index 90b90a144b..8993863e89 100644 --- a/src/iceberg/update/overwrite_files.cc +++ b/src/iceberg/update/overwrite_files.cc @@ -215,7 +215,7 @@ Status OverwriteFiles::Validate(const TableMetadata& current_metadata, if (validate_new_data_files_) { ICEBERG_RETURN_UNEXPECTED(ValidateAddedDataFiles( current_metadata, starting_snapshot_id_, DataConflictDetectionFilter(), snapshot, - ctx_->table->manifest_io(), IsCaseSensitive())); + internal::ManifestIO(*ctx_->table), IsCaseSensitive())); } if (validate_new_deletes_) { @@ -224,16 +224,17 @@ Status OverwriteFiles::Validate(const TableMetadata& current_metadata, conflict_detection_filter_ != nullptr ? conflict_detection_filter_ : row_filter; ICEBERG_RETURN_UNEXPECTED(ValidateNoNewDeleteFiles( current_metadata, starting_snapshot_id_, filter, snapshot, - ctx_->table->manifest_io(), IsCaseSensitive())); + internal::ManifestIO(*ctx_->table), IsCaseSensitive())); ICEBERG_RETURN_UNEXPECTED(ValidateDeletedDataFiles( current_metadata, starting_snapshot_id_, filter, snapshot, - ctx_->table->manifest_io(), IsCaseSensitive())); + internal::ManifestIO(*ctx_->table), IsCaseSensitive())); } if (!deleted_data_files_.empty()) { ICEBERG_RETURN_UNEXPECTED(ValidateNoNewDeletesForDataFiles( current_metadata, starting_snapshot_id_, conflict_detection_filter_, - deleted_data_files_, snapshot, ctx_->table->manifest_io(), IsCaseSensitive())); + deleted_data_files_, snapshot, internal::ManifestIO(*ctx_->table), + IsCaseSensitive())); } } diff --git a/src/iceberg/update/replace_partitions.cc b/src/iceberg/update/replace_partitions.cc index 8ad35cc4b6..007d48e419 100644 --- a/src/iceberg/update/replace_partitions.cc +++ b/src/iceberg/update/replace_partitions.cc @@ -113,7 +113,7 @@ Status ReplacePartitions::Validate(const TableMetadata& current_metadata, return {}; } - auto io = ctx_->table->manifest_io(); + auto io = internal::ManifestIO(*ctx_->table); if (validate_conflicting_data_) { if (data_spec->IsUnpartitioned()) { ICEBERG_RETURN_UNEXPECTED(ValidateAddedDataFiles( diff --git a/src/iceberg/update/rewrite_files.cc b/src/iceberg/update/rewrite_files.cc index 9df62f5536..45c9f61802 100644 --- a/src/iceberg/update/rewrite_files.cc +++ b/src/iceberg/update/rewrite_files.cc @@ -172,7 +172,7 @@ Status RewriteFiles::Validate(const TableMetadata& current_metadata, if (!replaced_data_files_.empty()) { // If there are replaced data files, there cannot be any new row-level deletes // for those data files. - auto io = ctx_->table->manifest_io(); + auto io = internal::ManifestIO(*ctx_->table); ICEBERG_RETURN_UNEXPECTED(MergingSnapshotUpdate::ValidateNoNewDeletesForDataFiles( current_metadata, starting_snapshot_id_, replaced_data_files_, snapshot, std::move(io), HasDataSequenceNumber())); diff --git a/src/iceberg/update/row_delta.cc b/src/iceberg/update/row_delta.cc index deac1a2dcf..04de06c24f 100644 --- a/src/iceberg/update/row_delta.cc +++ b/src/iceberg/update/row_delta.cc @@ -139,7 +139,7 @@ Status RowDelta::Validate(const TableMetadata& current_metadata, starting_snapshot_id_.value(), snapshot->snapshot_id); } - auto io = ctx_->table->manifest_io(); + auto io = internal::ManifestIO(*ctx_->table); if (!referenced_data_files_.empty()) { ICEBERG_RETURN_UNEXPECTED(MergingSnapshotUpdate::ValidateDataFilesExist( current_metadata, starting_snapshot_id_, referenced_data_files_, diff --git a/src/iceberg/update/snapshot_update.cc b/src/iceberg/update/snapshot_update.cc index d2ffb1d814..4e8a7151e2 100644 --- a/src/iceberg/update/snapshot_update.cc +++ b/src/iceberg/update/snapshot_update.cc @@ -344,8 +344,8 @@ Result SnapshotUpdate::Apply() { continue; } metadata_tasks.Submit([&manifest, this]() -> Status { - ICEBERG_ASSIGN_OR_RAISE(manifest, - AddMetadata(manifest, ctx_->table->manifest_io(), base())); + ICEBERG_ASSIGN_OR_RAISE( + manifest, AddMetadata(manifest, internal::ManifestIO(*ctx_->table), base())); return {}; }); } @@ -517,6 +517,7 @@ void SnapshotUpdate::RegisterStagedFile(const std::string& path, bool is_content Status SnapshotUpdate::DeleteStagedFile(const std::string& path) noexcept { try { auto result = delete_func_ ? delete_func_(path) : ctx_->table->io()->DeleteFile(path); + internal::InvalidateManifestCache(*ctx_->table, path); if (!result && result.error().kind != ErrorKind::kNotFound) { ICEBERG_LOG_WARN("Cannot clean staged file {}: {}", path, result.error().message); return {}; diff --git a/src/iceberg/util/cache_internal.h b/src/iceberg/util/cache_internal.h index 4aded5a618..30d0ec2397 100644 --- a/src/iceberg/util/cache_internal.h +++ b/src/iceberg/util/cache_internal.h @@ -103,6 +103,17 @@ class LruCache { return &list_it->value; } + template + void Erase(const K& key) { + const auto it = map_.find(key); + if (it == map_.end()) { + return; + } + total_weight_ -= it->second->weight; + items_.erase(it->second); + map_.erase(it); + } + // Insert or update the value for `key`, mark it as most recently used, and evict least // recently used entries until the cache is within its maximum weight again. // diff --git a/src/iceberg/util/error_util_internal.h b/src/iceberg/util/error_util_internal.h index 8ecfd3ce15..b6a4280e56 100644 --- a/src/iceberg/util/error_util_internal.h +++ b/src/iceberg/util/error_util_internal.h @@ -21,11 +21,25 @@ #include #include +#include #include "iceberg/logging/log_macros.h" +#include "iceberg/result.h" namespace iceberg::internal { +inline Status FinishWithCloseStatus(Status operation_status, Status close_status) { + if (operation_status.has_value()) { + return close_status; + } + auto error = std::move(operation_status).error(); + if (!close_status.has_value()) { + error.message += "; additionally failed to close stream: "; + error.message += close_status.error().message; + } + return unexpected(std::move(error)); +} + template void LogAndIgnoreFailure(std::string_view name, Action&& action) noexcept { try { From 2cd135ef6a28bb6d26fd62727ab1bf8ec666e413 Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Thu, 19 Nov 2026 11:15:51 +0800 Subject: [PATCH 06/16] test(s3): expect the Arrow file size lookup error Co-authored-by: Codex --- src/iceberg/test/arrow_s3_file_io_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/iceberg/test/arrow_s3_file_io_test.cc b/src/iceberg/test/arrow_s3_file_io_test.cc index f7aa6ce685..3422e4a39c 100644 --- a/src/iceberg/test/arrow_s3_file_io_test.cc +++ b/src/iceberg/test/arrow_s3_file_io_test.cc @@ -1300,7 +1300,7 @@ TEST_F(ArrowS3FileIOTest, DeleteFilesAttemptsEveryFile) { 2); EXPECT_THAT(io->ReadFile(allowed, std::nullopt), ::testing::AllOf(IsError(ErrorKind::kIOError), - HasErrorMessage("Path does not exist"))); + HasErrorMessage("File does not exist: " + allowed))); // The denied files remain: Arrow fails to delete a missing object. ASSERT_THAT(credentialed->SetStorageCredentials({}), IsOk()); From 9b019884a314a09aafbd60a2b435bba431470b0e Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Fri, 20 Nov 2026 18:03:09 +0900 Subject: [PATCH 07/16] fix: bound manifest caches and preserve credential rotation --- mkdocs/docs/library.md | 33 ++++ src/iceberg/content_cache_internal.cc | 74 ++++++- src/iceberg/content_cache_internal.h | 15 +- src/iceberg/table.cc | 64 ++++-- src/iceberg/table.h | 6 + src/iceberg/test/arrow_io_test.cc | 6 +- src/iceberg/test/fast_append_test.cc | 9 +- src/iceberg/test/manifest_reader_test.cc | 9 +- src/iceberg/test/metadata_cache_test.cc | 239 ++++++++++++++++++++++- src/iceberg/test/mock_io.h | 12 ++ src/iceberg/test/transaction_test.cc | 8 +- src/iceberg/test/update_test_base.h | 5 +- src/iceberg/util/cache_internal.h | 12 +- 13 files changed, 443 insertions(+), 49 deletions(-) diff --git a/mkdocs/docs/library.md b/mkdocs/docs/library.md index 6b769425ad..5d33de5e8b 100644 --- a/mkdocs/docs/library.md +++ b/mkdocs/docs/library.md @@ -79,3 +79,36 @@ follows: `iceberg::avro::RegisterAll()`. - Parquet readers and writers are registered by `iceberg::parquet::RegisterAll()`. + +## Manifest content caching + +Tables can cache immutable manifest and manifest-list bytes. Cache settings come +exclusively from `FileIO::properties()`; table metadata properties do not enable +or override the cache. REST catalogs supply their merged catalog and table I/O +configuration through the table's `FileIO`. Applications that inject a `FileIO` +must expose the desired settings through its `properties()` method. + +| Property | Default | +|---|---| +| `io.manifest.cache-enabled` | `false` | +| `io.manifest.cache.max-total-bytes` | `104857600` (100 MiB) | +| `io.manifest.cache.max-content-length` | `8388608` (8 MiB) | + +Independent table loads have separate caches and byte budgets. A process-wide +LRU registry retains at most eight cache states by default. Change this global +limit with `Table::SetManifestCacheLimit(max_caches)`, passing a positive number. +Lowering the limit immediately evicts the least recently used cache states. +The global count is configured separately from the per-table FileIO settings so +different FileIO instances cannot overwrite one another's global limit. + +Per-table handles and manifest I/O adapters retain only weak references to cache +states. Cache states are created on first use, and an evicted table recreates an +empty cache on its next read. Active readers and input files can still hold bytes +after eviction, and concurrent load buffers are outside the retained byte budget. +Call `Table::ClearManifestCache()` to release a table's retained content explicitly. + +Compatible refreshes and committed table handles share their source table's cache. +S3 access keys, secrets, session tokens, and token expiry can rotate without losing +warm content when the remaining storage configuration matches. Changes to cache +limits or storage configuration, such as an endpoint or region, start a new cache. +Data files and table metadata JSON continue to use storage I/O directly. diff --git a/src/iceberg/content_cache_internal.cc b/src/iceberg/content_cache_internal.cc index a248bebf91..10b5d36363 100644 --- a/src/iceberg/content_cache_internal.cc +++ b/src/iceberg/content_cache_internal.cc @@ -29,8 +29,62 @@ #include "iceberg/util/string_util.h" namespace iceberg::internal { + +struct ContentCacheState { + explicit ContentCacheState(size_t max_total_bytes) + : lru(static_cast(max_total_bytes)) {} + + LruCache + lru; +}; + +// Owns cache states, never their per-table handles or storage FileIOs. Callers hold +// the handle's mutex before accessing a state; the registry mutex only protects the +// global LRU. Eviction releases content without calling back into a table handle. +class ContentCacheRegistry { + public: + std::shared_ptr Get(const ContentCache* owner, + const ContentCacheOptions& options, + std::weak_ptr& weak_state) { + std::lock_guard lock(mutex_); + if (auto* state = caches_.Find(owner)) { + return *state; + } + auto state = weak_state.lock(); + if (!state) { + state = std::make_shared(options.max_total_bytes); + } + caches_.Replace(owner, state); + weak_state = state; + return state; + } + + void Erase(const ContentCache* owner) { + std::lock_guard lock(mutex_); + caches_.Erase(owner); + } + + int32_t SetCapacity(int32_t max_caches) { + ICEBERG_DCHECK(max_caches > 0, "Global manifest cache limit must be positive"); + std::lock_guard lock(mutex_); + auto previous = static_cast(caches_.max_weight()); + caches_.Resize(max_caches); + return previous; + } + + private: + std::mutex mutex_; + LruCache> caches_{8}; +}; + namespace { +std::shared_ptr GlobalContentCacheRegistry() { + // Handles keep the registry alive even when destroyed during static teardown. + static auto registry = std::make_shared(); + return registry; +} + template Result ParseNumberProperty( const std::unordered_map& properties, std::string_view key, @@ -234,7 +288,13 @@ Result ContentCacheOptions::FromProperties( } ContentCache::ContentCache(ContentCacheOptions options) - : options_(options), lru_(static_cast(options.max_total_bytes)) {} + : options_(options), registry_(GlobalContentCacheRegistry()) {} + +ContentCache::~ContentCache() { registry_->Erase(this); } + +int32_t ContentCache::SetGlobalCapacity(int32_t max_caches) { + return GlobalContentCacheRegistry()->SetCapacity(max_caches); +} std::shared_ptr ContentCache::Wrap(std::shared_ptr io) { return std::make_shared(std::move(io), shared_from_this()); @@ -243,7 +303,8 @@ std::shared_ptr ContentCache::Wrap(std::shared_ptr io) { MetadataContent ContentCache::Find(std::string_view location, uint64_t* generation) { std::lock_guard lock(mutex_); if (generation) *generation = generation_; - auto* content = lru_.Find(location); + auto state = registry_->Get(this, options_, state_); + auto* content = state->lru.Find(location); return content ? *content : nullptr; } @@ -253,19 +314,22 @@ void ContentCache::Insert(const std::string& location, MetadataContent content, if (content->empty()) return; std::lock_guard lock(mutex_); if (generation == generation_) { - lru_.Replace(location, std::move(content)); + registry_->Get(this, options_, state_)->lru.Replace(location, std::move(content)); } } void ContentCache::Invalidate(std::string_view location) { std::lock_guard lock(mutex_); - lru_.Erase(location); + if (auto state = state_.lock()) { + state->lru.Erase(location); + } ++generation_; } void ContentCache::Clear() { std::lock_guard lock(mutex_); - lru_.Clear(); + registry_->Erase(this); + state_.reset(); ++generation_; } diff --git a/src/iceberg/content_cache_internal.h b/src/iceberg/content_cache_internal.h index eeaab14f40..69d31bc726 100644 --- a/src/iceberg/content_cache_internal.h +++ b/src/iceberg/content_cache_internal.h @@ -58,11 +58,18 @@ struct MetadataContentWeigher { } }; -// A table owns this cache; readers share it through a metadata-only FileIO adapter. -// Storage FileIOs remain independent of the cache and its configuration. +struct ContentCacheState; +class ContentCacheRegistry; + +// A lightweight per-table handle with a weak reference to cached content. A global +// LRU registry retains at most eight cache states by default. Readers share the +// handle through a manifest-only FileIO adapter without pinning its cache state. class ICEBERG_EXPORT ContentCache : public std::enable_shared_from_this { public: explicit ContentCache(ContentCacheOptions options); + ~ContentCache(); + // Return the previous process-wide capacity. The new capacity must be positive. + static int32_t SetGlobalCapacity(int32_t max_caches); const ContentCacheOptions& options() const { return options_; } std::shared_ptr Wrap(std::shared_ptr io); Result> NewInputFile( @@ -77,9 +84,9 @@ class ICEBERG_EXPORT ContentCache : public std::enable_shared_from_this registry_; std::mutex mutex_; - LruCache - lru_; + std::weak_ptr state_; uint64_t generation_ = 0; }; diff --git a/src/iceberg/table.cc b/src/iceberg/table.cc index f70d7d9def..1e75793340 100644 --- a/src/iceberg/table.cc +++ b/src/iceberg/table.cc @@ -20,6 +20,9 @@ #include "iceberg/table.h" #include +#include +#include +#include #include "iceberg/catalog.h" #include "iceberg/content_cache_internal.h" @@ -61,6 +64,32 @@ #include "iceberg/util/macros.h" namespace iceberg { +namespace { + +std::unordered_map StorageProperties(const FileIO& io) { + auto properties = io.properties(); + // Vended S3 credentials can rotate without changing the underlying storage. + // Cache options are checked separately and do not identify a storage context. + constexpr std::string_view kNonStorageProperties[] = { + "s3.access-key-id", + "s3.secret-access-key", + "s3.session-token", + "s3.session-token-expires-at-ms", + internal::ContentCacheOptions::kEnabled, + internal::ContentCacheOptions::kMaxTotalBytes, + internal::ContentCacheOptions::kMaxContentLength}; + for (const auto key : kNonStorageProperties) { + properties.erase(std::string(key)); + } + return properties; +} + +bool HasS3Credentials(const FileIO& io) { + return io.properties().contains("s3.access-key-id") && + io.properties().contains("s3.secret-access-key"); +} + +} // namespace Result> Table::Make( TableIdentifier identifier, std::shared_ptr metadata, @@ -106,11 +135,7 @@ Table::Table(TableIdentifier identifier, std::shared_ptr metadata } void Table::InitializeManifestCache() { - auto properties = io_->properties(); - for (const auto& [key, value] : metadata_->properties.configs()) { - properties[key] = value; - } - auto options = internal::ContentCacheOptions::FromProperties(properties); + auto options = internal::ContentCacheOptions::FromProperties(io_->properties()); if (!options.has_value()) { // Table construction can happen after the catalog has committed new metadata. ICEBERG_LOG_WARN("Manifest cache disabled for table {}: {}", full_name_, @@ -137,16 +162,26 @@ void Table::ClearManifestCache() { if (content_cache_) content_cache_->Clear(); } +Status Table::SetManifestCacheLimit(int32_t max_caches) { + ICEBERG_PRECHECK(max_caches > 0, "Global manifest cache limit must be positive"); + internal::ContentCache::SetGlobalCapacity(max_caches); + return {}; +} + void Table::ReuseManifestCache(const Table& source) { if (!content_cache_ || !source.content_cache_ || uuid() != source.uuid() || content_cache_->options() != source.content_cache_->options()) { return; } - // A replaced storage context with no comparable properties may represent a different - // endpoint or filesystem. Only preserve bytes when its identity is known to match. - if (io_ != source.io_ && - (io_->properties().empty() || io_->properties() != source.io_->properties())) { - return; + // Without comparable settings or S3 credentials, a replacement may represent a + // different opaque filesystem. S3 credentials alone use the default S3 endpoint. + if (io_ != source.io_) { + auto properties = StorageProperties(*io_); + if (properties != StorageProperties(*source.io_) || + (properties.empty() && !HasS3Credentials(*io_) && + !HasS3Credentials(*source.io_))) { + return; + } } content_cache_ = source.content_cache_; manifest_io_ = content_cache_->Wrap(io_); @@ -156,13 +191,14 @@ const std::string& Table::uuid() const { return metadata_->table_uuid; } Status Table::Refresh() { ICEBERG_ASSIGN_OR_RAISE(auto refreshed_table, catalog_->LoadTable(identifier_)); + refreshed_table->ReuseManifestCache(*this); + // Credentials and I/O configuration can change even when metadata is unchanged. + content_cache_ = std::move(refreshed_table->content_cache_); + manifest_io_ = std::move(refreshed_table->manifest_io_); + io_ = std::move(refreshed_table->io_); if (metadata_location_ != refreshed_table->metadata_file_location()) { - refreshed_table->ReuseManifestCache(*this); - content_cache_ = std::move(refreshed_table->content_cache_); - manifest_io_ = std::move(refreshed_table->manifest_io_); metadata_ = std::move(refreshed_table->metadata_); metadata_location_ = std::string(refreshed_table->metadata_file_location()); - io_ = std::move(refreshed_table->io_); metadata_cache_ = std::make_unique(metadata_.get()); } return {}; diff --git a/src/iceberg/table.h b/src/iceberg/table.h index db21e9f74f..8765c2a524 100644 --- a/src/iceberg/table.h +++ b/src/iceberg/table.h @@ -136,11 +136,17 @@ class ICEBERG_EXPORT Table : public std::enable_shared_from_this
{ const std::shared_ptr& io() const; /// \brief Return whether this table caches manifest content. + /// Cache settings come exclusively from io()->properties(). bool ManifestCacheEnabled() const { return content_cache_ != nullptr; } /// \brief Release manifest content retained by this table. void ClearManifestCache(); + /// \brief Set the process-wide maximum number of retained manifest caches. + /// Defaults to 8. The limit must be positive; lowering it immediately evicts the + /// least recently used caches. Active readers may retain bytes after eviction. + static Status SetManifestCacheLimit(int32_t max_caches); + /// \brief Returns the current metadata for this table const std::shared_ptr& metadata() const; diff --git a/src/iceberg/test/arrow_io_test.cc b/src/iceberg/test/arrow_io_test.cc index b9a9813cc5..6180b18d81 100644 --- a/src/iceberg/test/arrow_io_test.cc +++ b/src/iceberg/test/arrow_io_test.cc @@ -41,6 +41,7 @@ #include "iceberg/table_metadata.h" #include "iceberg/test/matchers.h" #include "iceberg/test/mock_catalog.h" +#include "iceberg/test/mock_io.h" #include "iceberg/test/std_io.h" #include "iceberg/test/temp_file_test_base.h" @@ -458,11 +459,10 @@ TEST_F(LocalFileIOTest, SizeUsesFileInfoWithoutOpeningStream) { TEST_F(LocalFileIOTest, CachedInputCreationUsesFileInfoWithoutOpeningStream) { auto fs = std::make_shared(); - auto file_io = std::make_shared(fs); + auto file_io = std::make_shared>(fs); + file_io->config = {{std::string(internal::ContentCacheOptions::kEnabled), "true"}}; ASSERT_THAT(file_io->WriteFile(temp_filepath_, "hello"), IsOk()); auto metadata = std::make_shared(); - metadata->properties = TableProperties::FromMap( - {{std::string(internal::ContentCacheOptions::kEnabled), "true"}}); ICEBERG_UNWRAP_OR_FAIL( auto table, Table::Make({.ns = {}, .name = "test"}, std::move(metadata), "metadata.json", file_io, std::make_shared())); diff --git a/src/iceberg/test/fast_append_test.cc b/src/iceberg/test/fast_append_test.cc index dfc4034932..0d2b20489b 100644 --- a/src/iceberg/test/fast_append_test.cc +++ b/src/iceberg/test/fast_append_test.cc @@ -341,16 +341,15 @@ TEST_F(FastAppendTest, AppendNullFile) { } TEST_F(FastAppendTest, InvalidManifestCacheSettingsDoNotDeleteCommittedFiles) { + auto& io = static_cast&>(*file_io_); + io.config = {{"io.manifest.cache-enabled", "true"}, + {"io.manifest.cache.max-total-bytes", "invalid"}}; + ICEBERG_UNWRAP_OR_FAIL(table_, catalog_->LoadTable(table_ident_)); ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); ICEBERG_UNWRAP_OR_FAIL(auto append, txn->NewFastAppend()); append->AppendFile(file_a_); ASSERT_THAT(append->Commit(), IsOk()); - ICEBERG_UNWRAP_OR_FAIL(auto update, txn->NewUpdateProperties()); - update->Set("io.manifest.cache-enabled", "true"); - update->Set("io.manifest.cache.max-total-bytes", "invalid"); - ASSERT_THAT(update->Commit(), IsOk()); - ICEBERG_UNWRAP_OR_FAIL(auto committed, txn->Commit()); EXPECT_FALSE(committed->ManifestCacheEnabled()); ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); diff --git a/src/iceberg/test/manifest_reader_test.cc b/src/iceberg/test/manifest_reader_test.cc index 1a8616d5ba..211798bd8d 100644 --- a/src/iceberg/test/manifest_reader_test.cc +++ b/src/iceberg/test/manifest_reader_test.cc @@ -52,6 +52,7 @@ #include "iceberg/table_scan.h" #include "iceberg/test/matchers.h" #include "iceberg/test/mock_catalog.h" +#include "iceberg/test/mock_io.h" #include "iceberg/transform.h" #include "iceberg/type.h" @@ -798,10 +799,12 @@ TEST(ManifestReaderStaticTest, WithStatsColumnsHandlesEmptyAndSelectAll) { TEST_P(TestManifestReader, ReadsManifestListAndManifestFromCachedContent) { const auto version = GetParam(); auto fs = std::make_shared(std::chrono::system_clock::now()); - file_io_ = std::make_shared(fs); + auto configured_io = + std::make_shared>(fs); + configured_io->config = { + {std::string(internal::ContentCacheOptions::kEnabled), "true"}}; + file_io_ = std::move(configured_io); auto table_metadata = std::make_shared(); - table_metadata->properties = TableProperties::FromMap( - {{std::string(internal::ContentCacheOptions::kEnabled), "true"}}); ICEBERG_UNWRAP_OR_FAIL( auto table, Table::Make({.ns = {}, .name = "test"}, table_metadata, "metadata.json", file_io_, std::make_shared())); diff --git a/src/iceberg/test/metadata_cache_test.cc b/src/iceberg/test/metadata_cache_test.cc index 41370da3a8..40e30d666a 100644 --- a/src/iceberg/test/metadata_cache_test.cc +++ b/src/iceberg/test/metadata_cache_test.cc @@ -49,6 +49,16 @@ namespace iceberg { namespace { using internal::ContentCacheOptions; +class ScopedGlobalCacheLimit { + public: + explicit ScopedGlobalCacheLimit(int32_t limit) + : previous_(internal::ContentCache::SetGlobalCapacity(limit)) {} + ~ScopedGlobalCacheLimit() { internal::ContentCache::SetGlobalCapacity(previous_); } + + private: + int32_t previous_; +}; + class CountingInputStream : public SeekableInputStream { public: CountingInputStream(std::unique_ptr stream, int* read_count) @@ -109,7 +119,7 @@ class CountingInputFile : public InputFile { int* failing_opens_; }; -class CountingMockFileIO : public MockFileIO { +class CountingMockFileIO : public FileIOWithProperties { public: Result> NewInputFile(std::string file_location) override { ICEBERG_ASSIGN_OR_RAISE(auto input_file, @@ -125,13 +135,16 @@ class CountingMockFileIO : public MockFileIO { int failing_opens = 0; }; -// Create independently configured tables sharing the same storage FileIO. +// Configure storage before creating an independently cached table. +template Result> MakeCachedTable( - FileIO& io, const std::unordered_map& properties = {}, - std::string metadata_location = "metadata.json") { + IO& io, const std::unordered_map& properties = {}, + std::string metadata_location = "metadata.json", + const std::unordered_map& table_properties = {}) { + io.config = properties; auto metadata = std::make_shared(); metadata->table_uuid = "cache-test-table"; - metadata->properties = TableProperties::FromMap(properties); + metadata->properties = TableProperties::FromMap(table_properties); auto storage = std::shared_ptr(&io, [](FileIO*) {}); return Table::Make({.ns = {}, .name = "test"}, std::move(metadata), std::move(metadata_location), std::move(storage), @@ -187,7 +200,7 @@ class FailingOpenInputFile : public InputFile { std::shared_ptr state_; }; -class FailingOpenFileIO : public FileIO { +class FailingOpenFileIO : public FileIOWithProperties { public: explicit FailingOpenFileIO(std::shared_ptr state) : state_(std::move(state)) {} @@ -390,7 +403,7 @@ TEST(MetadataCacheTest, ManifestDeletesInvalidateOnlyTheAttemptedLocations) { TEST(MetadataCacheTest, TableConcurrentReadsReturnFileContent) { // CountingMockFileIO's counters are not thread-safe, so use the plain mock. - MockFileIO file_io; + FileIOWithProperties file_io; constexpr int kLocations = 8; std::vector contents; for (int id = 0; id < kLocations; ++id) { @@ -499,7 +512,7 @@ TEST(MetadataCacheTest, ReaderOptionsKeepsPositionalAggregateInitialization) { EXPECT_EQ(options.projection, schema); } -class LengthTrackingFileIO : public MockFileIO { +class LengthTrackingFileIO : public FileIOWithProperties { public: using MockFileIO::NewInputFile; int length_calls = 0; @@ -533,6 +546,35 @@ TEST(MetadataCacheTest, DisabledTableUsesStorageDirectly) { EXPECT_EQ(io.open_count, 2); } +TEST(MetadataCacheTest, TablePropertiesDoNotEnableManifestCache) { + CountingMockFileIO io; + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(io, {}, "metadata.json", + {{std::string(ContentCacheOptions::kEnabled), "true"}})); + EXPECT_FALSE(table->ManifestCacheEnabled()); + EXPECT_EQ(internal::ManifestIO(*table), table->io()); +} + +TEST(MetadataCacheTest, TablePropertiesDoNotOverrideFileIOCacheSettings) { + CountingMockFileIO io; + io.AddFile("manifest.avro", "manifest"); + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(io, + {{std::string(ContentCacheOptions::kEnabled), "true"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "16"}, + {std::string(ContentCacheOptions::kMaxContentLength), "16"}}, + "metadata.json", + {{std::string(ContentCacheOptions::kEnabled), "false"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "invalid"}, + {std::string(ContentCacheOptions::kMaxContentLength), "1"}})); + EXPECT_TRUE(table->ManifestCacheEnabled()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), IsOk()); + EXPECT_EQ(io.open_count, 1); +} + TEST(MetadataCacheTest, TablesSharingStorageHaveIndependentCachesAndLimits) { CountingMockFileIO io; io.AddFile("manifest.avro", "manifest"); @@ -554,6 +596,121 @@ TEST(MetadataCacheTest, TablesSharingStorageHaveIndependentCachesAndLimits) { EXPECT_EQ(io.open_count, 4); } +TEST(MetadataCacheTest, GlobalLimitEvictsLeastRecentlyUsedCacheWithLiveTableHandles) { + ScopedGlobalCacheLimit limit(2); + CountingMockFileIO io; + io.AddFile("manifest.avro", "manifest"); + std::unordered_map props = { + {std::string(ContentCacheOptions::kEnabled), "true"}}; + ICEBERG_UNWRAP_OR_FAIL(auto first, MakeCachedTable(io, props)); + ICEBERG_UNWRAP_OR_FAIL(auto second, MakeCachedTable(io, props)); + ICEBERG_UNWRAP_OR_FAIL(auto third, MakeCachedTable(io, props)); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*first), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*second), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*first), "manifest.avro"), IsOk()); + EXPECT_EQ(io.open_count, 2); + // Touching the first cache makes the second the next victim. The adapter must + // retain only a weak reference to its cached content, even while the table lives. + auto second_adapter = internal::ManifestIO(*second); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*third), "manifest.avro"), IsOk()); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*first), "manifest.avro"), IsOk()); + EXPECT_EQ(io.open_count, 3); + ASSERT_THAT(ReadCached(*second_adapter, "manifest.avro"), IsOk()); + EXPECT_EQ(io.open_count, 4); +} + +TEST(MetadataCacheTest, GlobalEvictionReleasesBytesAndReadersKeepTheirContent) { + ScopedGlobalCacheLimit limit(1); + auto first = std::make_shared(ContentCacheOptions{}); + auto content = std::make_shared>(8); + std::weak_ptr> weak_content = content; + first->Insert("manifest.avro", std::move(content), 0); + CountingMockFileIO io; + auto adapter = first->Wrap(std::shared_ptr(&io, [](FileIO*) {})); + ICEBERG_UNWRAP_OR_FAIL(auto file, adapter->NewInputFile("manifest.avro")); + ICEBERG_UNWRAP_OR_FAIL(auto reader, file->Open()); + file.reset(); + auto second = std::make_shared(ContentCacheOptions{}); + second->Insert("other.avro", std::make_shared>(8), 0); + EXPECT_FALSE(first->Find("manifest.avro")); + // Only the active reader still owns these bytes after global eviction. + EXPECT_FALSE(weak_content.expired()); + std::array out; + ASSERT_THAT(reader->ReadFully(0, out), IsOk()); + EXPECT_EQ(out, (std::array{})); + reader.reset(); + EXPECT_TRUE(weak_content.expired()); +} + +TEST(MetadataCacheTest, LoweringGlobalLimitImmediatelyReleasesOldCacheContent) { + ScopedGlobalCacheLimit limit(2); + auto first = std::make_shared(ContentCacheOptions{}); + auto second = std::make_shared(ContentCacheOptions{}); + auto content = std::make_shared>(8); + std::weak_ptr> weak_content = content; + first->Insert("first.avro", std::move(content), 0); + second->Insert("second.avro", std::make_shared>(8), 0); + ASSERT_THAT(Table::SetManifestCacheLimit(1), IsOk()); + EXPECT_TRUE(weak_content.expired()); + EXPECT_TRUE(second->Find("second.avro")); +} + +TEST(MetadataCacheTest, RejectsNonPositiveGlobalLimits) { + EXPECT_THAT(Table::SetManifestCacheLimit(0), IsError(ErrorKind::kInvalidArgument)); + EXPECT_THAT(Table::SetManifestCacheLimit(-1), IsError(ErrorKind::kInvalidArgument)); +} + +TEST(MetadataCacheTest, DestroyingLastCacheHandleReleasesRetainedBytes) { + auto cache = std::make_shared(ContentCacheOptions{}); + auto content = std::make_shared>(8); + std::weak_ptr> weak_content = content; + cache->Insert("manifest.avro", std::move(content), 0); + ASSERT_FALSE(weak_content.expired()); + cache.reset(); + EXPECT_TRUE(weak_content.expired()); +} + +TEST(MetadataCacheTest, InvalidatedLoadCannotPopulateRecreatedCacheState) { + ScopedGlobalCacheLimit limit(1); + auto cache = std::make_shared(ContentCacheOptions{}); + uint64_t generation; + ASSERT_FALSE(cache->Find("manifest.avro", &generation)); + auto other = std::make_shared(ContentCacheOptions{}); + other->Insert("other.avro", std::make_shared>(8), 0); + cache->Invalidate("manifest.avro"); + cache->Insert("manifest.avro", std::make_shared>(8), + generation); + EXPECT_FALSE(cache->Find("manifest.avro")); +} + +TEST(MetadataCacheTest, ConcurrentReadsSurviveGlobalEviction) { + ScopedGlobalCacheLimit limit(2); + FileIOWithProperties io; + io.AddFile("manifest.avro", "manifest"); + std::vector> tables; + for (int i = 0; i < 4; ++i) { + ICEBERG_UNWRAP_OR_FAIL( + auto table, + MakeCachedTable(io, {{std::string(ContentCacheOptions::kEnabled), "true"}})); + tables.push_back(std::move(table)); + } + std::barrier start(4); + std::vector> readers; + for (const auto& table : tables) { + readers.push_back(std::async(std::launch::async, [&start, table] { + start.arrive_and_wait(); + for (int i = 0; i < 1000; ++i) { + auto content = ReadCached(*internal::ManifestIO(*table), "manifest.avro"); + if (!content || *content != "manifest") return false; + } + return true; + })); + } + for (auto& reader : readers) { + EXPECT_TRUE(reader.get()); + } +} + TEST(MetadataCacheTest, CompatibleRefreshPreservesCachedContent) { CountingMockFileIO io; io.AddFile("manifest.avro", "manifest"); @@ -608,6 +765,72 @@ TEST(MetadataCacheTest, RefreshWithDifferentStorageDiscardsCachedBytes) { HasValue(::testing::Eq("second"))); } +TEST(MetadataCacheTest, RefreshWithRotatedCredentialsPreservesCacheAndUsesNewStorage) { + for (const auto* metadata_location : {"metadata.json", "metadata-2.json"}) { + SCOPED_TRACE(metadata_location); + for (bool explicit_endpoint : {false, true}) { + SCOPED_TRACE(explicit_endpoint); + CountingMockFileIO first_io; + CountingMockFileIO second_io; + first_io.AddFile("manifest.avro", "cached"); + second_io.AddFile("new-manifest.avro", "new manifest"); + std::unordered_map props = { + {std::string(ContentCacheOptions::kEnabled), "true"}, + {"s3.access-key-id", "old-key"}, + {"s3.secret-access-key", "old-secret"}, + {"s3.session-token", "old-token"}, + {"s3.session-token-expires-at-ms", "1000"}}; + if (explicit_endpoint) { + props["s3.endpoint"] = "https://storage.example"; + } + ICEBERG_UNWRAP_OR_FAIL(auto table, MakeCachedTable(first_io, props)); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), IsOk()); + + props["s3.access-key-id"] = "new-key"; + props["s3.secret-access-key"] = "new-secret"; + props["s3.session-token"] = "new-token"; + props["s3.session-token-expires-at-ms"] = "2000"; + ICEBERG_UNWRAP_OR_FAIL(auto refreshed, + MakeCachedTable(second_io, props, metadata_location)); + auto catalog = std::dynamic_pointer_cast(table->catalog()); + EXPECT_CALL(*catalog, LoadTable(::testing::_)) + .WillOnce(::testing::Return(refreshed)); + ASSERT_THAT(table->Refresh(), IsOk()); + EXPECT_EQ(table->io().get(), &second_io); + EXPECT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), + HasValue(::testing::Eq("cached"))); + EXPECT_EQ(first_io.open_count, 1); + EXPECT_EQ(second_io.open_count, 0); + EXPECT_THAT(ReadCached(*internal::ManifestIO(*table), "new-manifest.avro"), + HasValue(::testing::Eq("new manifest"))); + EXPECT_EQ(second_io.open_count, 1); + } + } +} + +TEST(MetadataCacheTest, RefreshWithChangedStoragePropertiesDiscardsCachedBytes) { + for (const auto* key : {"s3.endpoint", "client.region", "io-impl"}) { + SCOPED_TRACE(key); + CountingMockFileIO first_io; + CountingMockFileIO second_io; + first_io.AddFile("manifest.avro", "first"); + second_io.AddFile("manifest.avro", "second"); + std::unordered_map props = { + {std::string(ContentCacheOptions::kEnabled), "true"}, {key, "old"}}; + ICEBERG_UNWRAP_OR_FAIL(auto table, MakeCachedTable(first_io, props)); + ASSERT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), IsOk()); + props[key] = "new"; + ICEBERG_UNWRAP_OR_FAIL(auto refreshed, + MakeCachedTable(second_io, props, "metadata-2.json")); + auto catalog = std::dynamic_pointer_cast(table->catalog()); + EXPECT_CALL(*catalog, LoadTable(::testing::_)).WillOnce(::testing::Return(refreshed)); + ASSERT_THAT(table->Refresh(), IsOk()); + EXPECT_THAT(ReadCached(*internal::ManifestIO(*table), "manifest.avro"), + HasValue(::testing::Eq("second"))); + EXPECT_EQ(second_io.open_count, 1); + } +} + TEST(MetadataCacheTest, InvalidCacheLimitsFallBackToStorage) { for (const auto key : {ContentCacheOptions::kMaxTotalBytes, ContentCacheOptions::kMaxContentLength}) { diff --git a/src/iceberg/test/mock_io.h b/src/iceberg/test/mock_io.h index 63abb6ea6d..c69ed50ceb 100644 --- a/src/iceberg/test/mock_io.h +++ b/src/iceberg/test/mock_io.h @@ -42,6 +42,18 @@ namespace iceberg { +template +class FileIOWithProperties : public Base { + public: + using Base::Base; + + const std::unordered_map& properties() const override { + return config; + } + + std::unordered_map config; +}; + class MockFileIO : public FileIO { public: MockFileIO() = default; diff --git a/src/iceberg/test/transaction_test.cc b/src/iceberg/test/transaction_test.cc index c10a6ad7ec..82b8647810 100644 --- a/src/iceberg/test/transaction_test.cc +++ b/src/iceberg/test/transaction_test.cc @@ -96,11 +96,9 @@ TEST_F(TransactionTest, BaseMetadataSurvivesTableRefresh) { } TEST_F(TransactionTest, CommitPreservesTableManifestContentCache) { - ICEBERG_UNWRAP_OR_FAIL(auto enable_txn, table_->NewTransaction()); - ICEBERG_UNWRAP_OR_FAIL(auto enable, enable_txn->NewUpdateProperties()); - enable->Set("io.manifest.cache-enabled", "true"); - ASSERT_THAT(enable->Commit(), IsOk()); - ICEBERG_UNWRAP_OR_FAIL(table_, enable_txn->Commit()); + auto& io = static_cast&>(*file_io_); + io.config["io.manifest.cache-enabled"] = "true"; + ICEBERG_UNWRAP_OR_FAIL(table_, catalog_->LoadTable(table_ident_)); ASSERT_TRUE(table_->ManifestCacheEnabled()); const auto manifest_path = table_location_ + "/metadata/cached-manifest.avro"; diff --git a/src/iceberg/test/update_test_base.h b/src/iceberg/test/update_test_base.h index fe8c9705fa..144f41ffa3 100644 --- a/src/iceberg/test/update_test_base.h +++ b/src/iceberg/test/update_test_base.h @@ -41,6 +41,7 @@ #include "iceberg/table_metadata.h" #include "iceberg/test/matchers.h" #include "iceberg/test/mock_catalog.h" +#include "iceberg/test/mock_io.h" #include "iceberg/test/test_resource.h" #include "iceberg/util/uuid.h" @@ -62,7 +63,9 @@ class UpdateTestBase : public ::testing::Test { /// \brief Initialize file IO and create necessary directories. void InitializeFileIO() { - file_io_ = arrow::ArrowFileSystemFileIO::MakeMockFileIO(); + auto mock_io = arrow::ArrowFileSystemFileIO::MakeMockFileIO(); + file_io_ = std::make_shared>( + static_cast(*mock_io).fs()); ICEBERG_UNWRAP_OR_FAIL(catalog_, InMemoryCatalog::Make("test_catalog", file_io_, "/warehouse/", /*properties=*/{})); diff --git a/src/iceberg/util/cache_internal.h b/src/iceberg/util/cache_internal.h index 30d0ec2397..45638143c9 100644 --- a/src/iceberg/util/cache_internal.h +++ b/src/iceberg/util/cache_internal.h @@ -90,6 +90,16 @@ class LruCache { // Maximum combined weight allowed in this cache. int64_t max_weight() const { return max_weight_; } + // 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"); + max_weight_ = max_weight; + while (total_weight_ > max_weight_) { + EvictLeastRecentlyUsed(); + } + ReserveForUnitWeight(); + } + template Value* Find(K&& key) { const auto it = map_.find(key); @@ -185,7 +195,7 @@ class LruCache { } } - const int64_t max_weight_; + int64_t max_weight_; Weigher weigher_; int64_t total_weight_ = 0; // In most to least recently used order From b448a524701c867e25b3ed282fa801a33a7ca16c Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Fri, 20 Nov 2026 23:58:49 +0800 Subject: [PATCH 08/16] fix: allow configuring the manifest cache limit at startup Co-authored-by: Codex --- mkdocs/docs/library.md | 16 +++++++----- src/iceberg/content_cache_internal.cc | 23 +++++++++++++++-- src/iceberg/table.h | 8 +++--- src/iceberg/test/metadata_cache_test.cc | 34 +++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 11 deletions(-) diff --git a/mkdocs/docs/library.md b/mkdocs/docs/library.md index 5d33de5e8b..a68d1eb418 100644 --- a/mkdocs/docs/library.md +++ b/mkdocs/docs/library.md @@ -82,7 +82,7 @@ follows: ## Manifest content caching -Tables can cache immutable manifest and manifest-list bytes. Cache settings come +Tables can cache immutable manifest and manifest-list bytes. Per-table settings come exclusively from `FileIO::properties()`; table metadata properties do not enable or override the cache. REST catalogs supply their merged catalog and table I/O configuration through the table's `FileIO`. Applications that inject a `FileIO` @@ -95,11 +95,15 @@ must expose the desired settings through its `properties()` method. | `io.manifest.cache.max-content-length` | `8388608` (8 MiB) | Independent table loads have separate caches and byte budgets. A process-wide -LRU registry retains at most eight cache states by default. Change this global -limit with `Table::SetManifestCacheLimit(max_caches)`, passing a positive number. -Lowering the limit immediately evicts the least recently used cache states. -The global count is configured separately from the per-table FileIO settings so -different FileIO instances cannot overwrite one another's global limit. +LRU registry retains at most eight cache states by default. To change the limit +at launch, set `ICEBERG_IO_MANIFEST_CACHE_LIMIT` to a positive integer that fits +in `int32_t`. For example, `ICEBERG_IO_MANIFEST_CACHE_LIMIT=16` allows up to 16 +retained caches per process. The registry reads this setting once when it +initializes and uses the default if the setting is missing or invalid. + +`Table::SetManifestCacheLimit(max_caches)` overrides the initial limit for the +entire process. Lowering the limit immediately evicts the least recently used +cache states. Per-table handles and manifest I/O adapters retain only weak references to cache states. Cache states are created on first use, and an evicted table recreates an diff --git a/src/iceberg/content_cache_internal.cc b/src/iceberg/content_cache_internal.cc index 10b5d36363..8fc4cac6d6 100644 --- a/src/iceberg/content_cache_internal.cc +++ b/src/iceberg/content_cache_internal.cc @@ -20,10 +20,12 @@ #include "iceberg/content_cache_internal.h" #include +#include #include #include #include +#include "iceberg/logging/log_macros.h" #include "iceberg/util/error_util_internal.h" #include "iceberg/util/macros.h" #include "iceberg/util/string_util.h" @@ -43,6 +45,8 @@ struct ContentCacheState { // global LRU. Eviction releases content without calling back into a table handle. class ContentCacheRegistry { public: + explicit ContentCacheRegistry(int32_t capacity) : caches_(capacity) {} + std::shared_ptr Get(const ContentCache* owner, const ContentCacheOptions& options, std::weak_ptr& weak_state) { @@ -74,14 +78,29 @@ class ContentCacheRegistry { private: std::mutex mutex_; - LruCache> caches_{8}; + LruCache> caches_; }; namespace { +int32_t InitialGlobalCacheCapacity() { + constexpr int32_t kDefaultCapacity = 8; + constexpr const char* kLimitEnv = "ICEBERG_IO_MANIFEST_CACHE_LIMIT"; + if (const char* configured_limit = std::getenv(kLimitEnv)) { + auto limit = StringUtils::ParseNumber(configured_limit); + if (limit && *limit > 0) { + return *limit; + } + ICEBERG_LOG_WARN("{} must be a positive integer that fits in int32_t. Using {}.", + kLimitEnv, kDefaultCapacity); + } + return kDefaultCapacity; +} + std::shared_ptr GlobalContentCacheRegistry() { // Handles keep the registry alive even when destroyed during static teardown. - static auto registry = std::make_shared(); + static auto registry = + std::make_shared(InitialGlobalCacheCapacity()); return registry; } diff --git a/src/iceberg/table.h b/src/iceberg/table.h index 8765c2a524..74c7221251 100644 --- a/src/iceberg/table.h +++ b/src/iceberg/table.h @@ -136,15 +136,17 @@ class ICEBERG_EXPORT Table : public std::enable_shared_from_this
{ const std::shared_ptr& io() const; /// \brief Return whether this table caches manifest content. - /// Cache settings come exclusively from io()->properties(). + /// Per-table cache settings come exclusively from io()->properties(). bool ManifestCacheEnabled() const { return content_cache_ != nullptr; } /// \brief Release manifest content retained by this table. void ClearManifestCache(); /// \brief Set the process-wide maximum number of retained manifest caches. - /// Defaults to 8. The limit must be positive; lowering it immediately evicts the - /// least recently used caches. Active readers may retain bytes after eviction. + /// The initial limit defaults to 8 and can be set at launch through + /// ICEBERG_IO_MANIFEST_CACHE_LIMIT. This setter overrides that value. The limit + /// must be positive. Lowering it immediately evicts the least recently used + /// caches. Active readers may retain bytes after eviction. static Status SetManifestCacheLimit(int32_t max_caches); /// \brief Returns the current metadata for this table diff --git a/src/iceberg/test/metadata_cache_test.cc b/src/iceberg/test/metadata_cache_test.cc index 40e30d666a..57c19f1014 100644 --- a/src/iceberg/test/metadata_cache_test.cc +++ b/src/iceberg/test/metadata_cache_test.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -49,6 +50,39 @@ namespace iceberg { namespace { using internal::ContentCacheOptions; +#ifdef GTEST_HAS_DEATH_TEST +int SetManifestCacheLimitEnv(const char* value) { + constexpr const char* kLimitEnv = "ICEBERG_IO_MANIFEST_CACHE_LIMIT"; +# ifdef _WIN32 + return _putenv_s(kLimitEnv, value); +# else + return setenv(kLimitEnv, value, /*overwrite=*/1); +# endif +} + +TEST(MetadataCacheDeathTest, InitializesGlobalLimitFromEnvironment) { + // Start with a new registry for every environment setting. + GTEST_FLAG_SET(death_test_style, "threadsafe"); + for (const auto& [value, expected_limit] : + {std::pair{"2", 2}, std::pair{"0", 8}, std::pair{"-1", 8}, std::pair{"2x", 8}, + std::pair{"2147483648", 8}}) { + SCOPED_TRACE(value); + EXPECT_EXIT( + { + if (SetManifestCacheLimitEnv(value) != 0) { + std::_Exit(EXIT_FAILURE); + } + const auto initial_limit = internal::ContentCache::SetGlobalCapacity(3); + const auto overridden_limit = internal::ContentCache::SetGlobalCapacity(4); + std::_Exit(initial_limit == expected_limit && overridden_limit == 3 + ? EXIT_SUCCESS + : EXIT_FAILURE); + }, + ::testing::ExitedWithCode(EXIT_SUCCESS), ""); + } +} +#endif + class ScopedGlobalCacheLimit { public: explicit ScopedGlobalCacheLimit(int32_t limit) From 2ca730b3ce04e28b148ee45057cd54294f909174 Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Mon, 23 Nov 2026 19:27:14 +0800 Subject: [PATCH 09/16] style: use std::array for manifest cache property keys Co-authored-by: Codex --- src/iceberg/table.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/iceberg/table.cc b/src/iceberg/table.cc index 1e75793340..180683bc47 100644 --- a/src/iceberg/table.cc +++ b/src/iceberg/table.cc @@ -19,6 +19,7 @@ #include "iceberg/table.h" +#include #include #include #include @@ -70,7 +71,7 @@ std::unordered_map StorageProperties(const FileIO& io) auto properties = io.properties(); // Vended S3 credentials can rotate without changing the underlying storage. // Cache options are checked separately and do not identify a storage context. - constexpr std::string_view kNonStorageProperties[] = { + constexpr std::array kNonStorageProperties = { "s3.access-key-id", "s3.secret-access-key", "s3.session-token", From ef627f9f4cdffcf872f0622e735e00d560d4eeaa Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Wed, 25 Nov 2026 20:19:26 +0800 Subject: [PATCH 10/16] feat(util): support weighted LRU eviction Track entry weights, support eviction on resize, and grow storage on demand. Co-authored-by: Codex --- src/iceberg/test/cache_test.cc | 150 ++++++++++++++++++++++++++++++ src/iceberg/util/cache_internal.h | 133 +++++++++++++++++++++----- 2 files changed, 258 insertions(+), 25 deletions(-) diff --git a/src/iceberg/test/cache_test.cc b/src/iceberg/test/cache_test.cc index fbc29ab9c4..37af5a6fdf 100644 --- a/src/iceberg/test/cache_test.cc +++ b/src/iceberg/test/cache_test.cc @@ -19,8 +19,11 @@ #include #include +#include #include +#include #include +#include #include @@ -43,6 +46,145 @@ TEST(LruCacheTest, Eviction) { EXPECT_EQ(*cache.Find(1), "one"); ASSERT_NE(cache.Find(3), nullptr); EXPECT_EQ(*cache.Find(3), "three"); + EXPECT_EQ(cache.size(), 2); + EXPECT_EQ(cache.total_weight(), 2); +} + +struct StringLengthWeigher { + int64_t operator()(int32_t /*key*/, const std::string& value) const { + return static_cast(value.size()); + } +}; + +using WeightedCache = LruCache; + +TEST(LruCacheTest, LargeLimitDoesNotReserveStorage) { + constexpr auto kLargeLimit = std::numeric_limits::max(); + LruCache cache(kLargeLimit); + ASSERT_NE(cache.Replace(1, "one").second, nullptr); + + cache.Resize(1); + cache.Resize(kLargeLimit); + EXPECT_EQ(cache.max_weight(), kLargeLimit); + EXPECT_EQ(cache.size(), 1); + cache.Clear(); + ASSERT_NE(cache.Replace(2, "two").second, nullptr); + EXPECT_EQ(cache.size(), 1); +} + +TEST(LruCacheTest, WeightedEviction) { + WeightedCache cache(10); + + EXPECT_TRUE(cache.Replace(1, "aaaa").first); + EXPECT_TRUE(cache.Replace(2, "bbb").first); + ASSERT_NE(cache.Find(1), nullptr); + EXPECT_EQ(cache.total_weight(), 7); + + // 7 + 6 exceeds 10, so the least recently used entry 2 is evicted. + auto [inserted, value] = cache.Replace(3, "cccccc"); + EXPECT_TRUE(inserted); + ASSERT_NE(value, nullptr); + EXPECT_EQ(*value, "cccccc"); + EXPECT_EQ(cache.Find(2), nullptr); + ASSERT_NE(cache.Find(1), nullptr); + EXPECT_EQ(*cache.Find(1), "aaaa"); + EXPECT_EQ(cache.size(), 2); + EXPECT_EQ(cache.total_weight(), 10); +} + +TEST(LruCacheTest, WeightedEvictionRemovesMultipleEntries) { + WeightedCache cache(10); + cache.Replace(1, "aaa"); + cache.Replace(2, "bbb"); + cache.Replace(3, "ccc"); + + // Making room for 7 requires evicting the two least recently used entries. + ASSERT_NE(cache.Replace(4, "ddddddd").second, nullptr); + EXPECT_EQ(cache.Find(1), nullptr); + EXPECT_EQ(cache.Find(2), nullptr); + EXPECT_NE(cache.Find(3), nullptr); + EXPECT_NE(cache.Find(4), nullptr); + EXPECT_EQ(cache.size(), 2); + EXPECT_EQ(cache.total_weight(), 10); +} + +TEST(LruCacheTest, OversizedValueIsNotCached) { + WeightedCache cache(5); + cache.Replace(1, "aa"); + + auto [inserted, value] = cache.Replace(2, "bbbbbb"); + EXPECT_FALSE(inserted); + EXPECT_EQ(value, nullptr); + EXPECT_EQ(cache.Find(2), nullptr); + // Rejecting the oversized value must not evict anything. + ASSERT_NE(cache.Find(1), nullptr); + EXPECT_EQ(cache.size(), 1); + EXPECT_EQ(cache.total_weight(), 2); + + // A value exactly at the maximum weight fits. + ASSERT_NE(cache.Replace(3, "ccccc").second, nullptr); + EXPECT_EQ(cache.Find(1), nullptr); + EXPECT_EQ(cache.total_weight(), 5); +} + +TEST(LruCacheTest, ReplaceReweighsEntry) { + WeightedCache cache(10); + cache.Replace(1, "aaa"); + cache.Replace(2, "bbb"); + + // Growing entry 2 from 3 to 7 still fits next to entry 1. + EXPECT_FALSE(cache.Replace(2, "bbbbbbb").first); + EXPECT_EQ(cache.size(), 2); + EXPECT_EQ(cache.total_weight(), 10); + + // Growing entry 1 to 8 makes it most recently used and evicts entry 2. + auto [inserted, value] = cache.Replace(1, "aaaaaaaa"); + EXPECT_FALSE(inserted); + ASSERT_NE(value, nullptr); + EXPECT_EQ(*value, "aaaaaaaa"); + EXPECT_EQ(cache.Find(2), nullptr); + EXPECT_EQ(cache.size(), 1); + EXPECT_EQ(cache.total_weight(), 8); + + // Shrinking releases weight. + cache.Replace(1, "a"); + EXPECT_EQ(cache.total_weight(), 1); + + // Replacing with an oversized value removes the existing entry. + std::tie(inserted, value) = cache.Replace(1, std::string(11, 'a')); + EXPECT_FALSE(inserted); + EXPECT_EQ(value, nullptr); + EXPECT_EQ(cache.Find(1), nullptr); + EXPECT_EQ(cache.size(), 0); + EXPECT_EQ(cache.total_weight(), 0); +} + +TEST(LruCacheTest, ClearResetsWeight) { + WeightedCache cache(10); + cache.Replace(1, "aaaa"); + cache.Replace(2, "bbbb"); + + cache.Clear(); + EXPECT_EQ(cache.size(), 0); + EXPECT_EQ(cache.total_weight(), 0); + + ASSERT_NE(cache.Replace(3, std::string(10, 'c')).second, nullptr); + EXPECT_EQ(cache.total_weight(), 10); +} + +TEST(LruCacheTest, ShrinkingLimitEvictsByWeightInLruOrder) { + WeightedCache cache(10); + cache.Replace(1, "aaa"); + cache.Replace(2, "bbbb"); + cache.Replace(3, "ccc"); + ASSERT_NE(cache.Find(1), nullptr); + + cache.Resize(3); + EXPECT_EQ(cache.max_weight(), 3); + EXPECT_EQ(cache.total_weight(), 3); + EXPECT_EQ(cache.Find(2), nullptr); + EXPECT_EQ(cache.Find(3), nullptr); + EXPECT_NE(cache.Find(1), nullptr); } TEST(MemoizeLruTest, Caches) { @@ -64,6 +206,14 @@ TEST(MemoizeLruTest, Caches) { EXPECT_EQ(calls, 4); } +TEST(MemoizeLruTest, RejectsLookupWithZeroCapacity) { + auto identity = [](int32_t key) { return key; }; + auto memoized = MemoizeLru(identity, 0); + auto memoized_thread_unsafe = MemoizeLruThreadUnsafe(identity, 0); + EXPECT_THROW(memoized(1), std::invalid_argument); + EXPECT_THROW(memoized_thread_unsafe(1), std::invalid_argument); +} + TEST(MemoizeLruTest, MovesKey) { auto memoized = MemoizeLru([](const std::unique_ptr& key) { return *key; }, 2); EXPECT_EQ(memoized(std::make_unique(1)), 1); diff --git a/src/iceberg/util/cache_internal.h b/src/iceberg/util/cache_internal.h index d82a93b80b..9ff53d6d30 100644 --- a/src/iceberg/util/cache_internal.h +++ b/src/iceberg/util/cache_internal.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -37,13 +38,30 @@ namespace iceberg::internal { +// Gives every entry a weight of one, so that the maximum weight of a cache is the +// maximum number of entries. +struct UnitWeigher { + template + constexpr int64_t operator()(const Key& /*key*/, + const Value& /*value*/) const noexcept { + return 1; + } +}; + // A LRU (Least recently used) replacement cache -template +// +// The combined weight of the cached entries never exceeds `max_weight`; least recently +// used entries are evicted first to make room. `Weigher` computes the weight of an entry +// from its key and value when the entry is inserted or replaced, e.g. its size in bytes. +// Weights must be non-negative. With the default UnitWeigher, `max_weight` is the +// maximum number of entries. Storage grows as entries are inserted. +template , typename KeyEqual = std::equal_to> class LruCache { public: - explicit LruCache(int32_t capacity) : capacity_(capacity) { - // The map size can temporarily exceed the cache capacity, see Replace() - map_.reserve(capacity_ + 1); + explicit LruCache(int64_t max_weight, Weigher weigher = Weigher{}) + : max_weight_(max_weight), weigher_(std::move(weigher)) { + ICEBERG_DCHECK(max_weight_ >= 0, "Cache maximum weight must be non-negative"); } LruCache(const LruCache&) = delete; @@ -54,15 +72,30 @@ class LruCache { void Clear() { items_.clear(); map_.clear(); - // The C++ spec doesn't tell whether map_.clear() will shrink the map capacity - map_.reserve(capacity_ + 1); + total_weight_ = 0; } + // Number of retained entries. int32_t size() const { ICEBERG_DCHECK(items_.size() == map_.size(), "Cache item and map sizes differ"); return static_cast(items_.size()); } + // Combined weight of retained entries. + int64_t total_weight() const { return total_weight_; } + + // Maximum combined weight allowed in this cache. + int64_t max_weight() const { return max_weight_; } + + // 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"); + max_weight_ = max_weight; + while (total_weight_ > max_weight_) { + EvictLeastRecentlyUsed(); + } + } + template Value* Find(K&& key) { const auto it = map_.find(key); @@ -76,29 +109,58 @@ class LruCache { return &list_it->value; } + template + void Erase(const K& key) { + const auto it = map_.find(key); + if (it == map_.end()) { + return; + } + total_weight_ -= it->second->weight; + items_.erase(it->second); + map_.erase(it); + } + + // Insert or update the value for `key`, mark it as most recently used, and evict least + // recently used entries until the cache is within its maximum weight again. + // + // Returns whether a new entry was added, and a pointer to the cached value. A value + // heavier than the maximum weight is not cached: any existing entry for `key` is + // removed, other entries are kept, and {false, nullptr} is returned. template std::pair Replace(K&& key, V&& value) { // Try to insert temporary iterator auto [it, inserted] = map_.emplace(std::forward(key), ListIt{}); + ListIt list_it; if (inserted) { // Inserted => push item at front of the list, and update iterator items_.emplace_front(&it->first, std::forward(value)); - it->second = items_.begin(); - // Did we exceed the cache capacity? If so, remove least recently used item - if (static_cast(items_.size()) > capacity_) { - const bool erased = map_.erase(*items_.back().key); - ICEBERG_DCHECK(erased, "Failed to erase least recently used cache item"); - static_cast(erased); - items_.pop_back(); - } - return {true, &it->second->value}; + list_it = items_.begin(); + it->second = list_it; + } else { + // Already exists => move item at front of the list, and update value + list_it = it->second; + items_.splice(items_.begin(), items_, list_it); + list_it->value = std::forward(value); + total_weight_ -= list_it->weight; + list_it->weight = 0; } - // Already exists => move item at front of the list, and update value - auto list_it = it->second; - items_.splice(items_.begin(), items_, list_it); - list_it->value = std::forward(value); - return {false, &list_it->value}; + const int64_t weight = weigher_(it->first, list_it->value); + ICEBERG_DCHECK(weight >= 0, "Cache entry weight must be non-negative"); + if (weight > max_weight_) { + items_.erase(list_it); + map_.erase(it); + return {false, nullptr}; + } + + // The item is at the front of the list and not yet counted in total_weight_, and + // `max_weight_ - weight` is non-negative, so eviction stops before reaching it. + while (total_weight_ > max_weight_ - weight) { + EvictLeastRecentlyUsed(); + } + list_it->weight = weight; + total_weight_ += weight; + return {inserted, &list_it->value}; } private: @@ -106,14 +168,27 @@ class LruCache { // Pointer to the key inside the unordered_map const Key* key; Value value; + int64_t weight = 0; }; using List = std::list; using ListIt = typename List::iterator; - const int32_t capacity_; + void EvictLeastRecentlyUsed() { + ICEBERG_DCHECK(!items_.empty(), "Cannot evict from an empty cache"); + const Item& item = items_.back(); + total_weight_ -= item.weight; + const bool erased = map_.erase(*item.key); + ICEBERG_DCHECK(erased, "Failed to erase least recently used cache item"); + static_cast(erased); + items_.pop_back(); + } + + int64_t max_weight_; + Weigher weigher_; + int64_t total_weight_ = 0; // In most to least recently used order std::list items_; - std::unordered_map map_; + std::unordered_map map_; }; template @@ -135,7 +210,11 @@ struct ThreadSafeMemoizer { lock.unlock(); Value value = func_(key); lock.lock(); - return *cache_.Replace(std::forward(key), std::move(value)).second; + const auto* cached = cache_.Replace(std::forward(key), std::move(value)).second; + if (cached == nullptr) { + throw std::invalid_argument("Memoization cache capacity must be positive"); + } + return *cached; } private: @@ -158,7 +237,11 @@ struct ThreadUnsafeMemoizer { return *value_ptr; } Value value = func_(key); - return *cache_.Replace(std::forward(key), std::move(value)).second; + const auto* cached = cache_.Replace(std::forward(key), std::move(value)).second; + if (cached == nullptr) { + throw std::invalid_argument("Memoization cache capacity must be positive"); + } + return *cached; } private: @@ -184,7 +267,7 @@ auto Memoize(Func&& func, int32_t cache_capacity) { return Memoizer(std::forward(func), cache_capacity); } -// Apply a LRU memoization cache to a callable. +// Apply a LRU memoization cache to a callable. Calls require positive capacity. template auto MemoizeLru(Func&& func, int32_t cache_capacity) { return Memoize(std::forward(func), cache_capacity); From 57dac0022d1da2f6f4c2228d619291bd5a4f1ea5 Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Wed, 25 Nov 2026 20:22:21 +0800 Subject: [PATCH 11/16] fix(util): allocate LRU storage on demand Sync the weighted LRU implementation and tests with the standalone patch in #1174. Co-authored-by: Codex --- src/iceberg/test/cache_test.cc | 39 +++++++++++++++++++++++++++++++ src/iceberg/util/cache_internal.h | 30 +++++++++++------------- 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/iceberg/test/cache_test.cc b/src/iceberg/test/cache_test.cc index c406febdd6..37af5a6fdf 100644 --- a/src/iceberg/test/cache_test.cc +++ b/src/iceberg/test/cache_test.cc @@ -19,7 +19,9 @@ #include #include +#include #include +#include #include #include @@ -56,6 +58,20 @@ struct StringLengthWeigher { using WeightedCache = LruCache; +TEST(LruCacheTest, LargeLimitDoesNotReserveStorage) { + constexpr auto kLargeLimit = std::numeric_limits::max(); + LruCache cache(kLargeLimit); + ASSERT_NE(cache.Replace(1, "one").second, nullptr); + + cache.Resize(1); + cache.Resize(kLargeLimit); + EXPECT_EQ(cache.max_weight(), kLargeLimit); + EXPECT_EQ(cache.size(), 1); + cache.Clear(); + ASSERT_NE(cache.Replace(2, "two").second, nullptr); + EXPECT_EQ(cache.size(), 1); +} + TEST(LruCacheTest, WeightedEviction) { WeightedCache cache(10); @@ -156,6 +172,21 @@ TEST(LruCacheTest, ClearResetsWeight) { EXPECT_EQ(cache.total_weight(), 10); } +TEST(LruCacheTest, ShrinkingLimitEvictsByWeightInLruOrder) { + WeightedCache cache(10); + cache.Replace(1, "aaa"); + cache.Replace(2, "bbbb"); + cache.Replace(3, "ccc"); + ASSERT_NE(cache.Find(1), nullptr); + + cache.Resize(3); + EXPECT_EQ(cache.max_weight(), 3); + EXPECT_EQ(cache.total_weight(), 3); + EXPECT_EQ(cache.Find(2), nullptr); + EXPECT_EQ(cache.Find(3), nullptr); + EXPECT_NE(cache.Find(1), nullptr); +} + TEST(MemoizeLruTest, Caches) { std::atomic calls{0}; auto memoized = MemoizeLru( @@ -175,6 +206,14 @@ TEST(MemoizeLruTest, Caches) { EXPECT_EQ(calls, 4); } +TEST(MemoizeLruTest, RejectsLookupWithZeroCapacity) { + auto identity = [](int32_t key) { return key; }; + auto memoized = MemoizeLru(identity, 0); + auto memoized_thread_unsafe = MemoizeLruThreadUnsafe(identity, 0); + EXPECT_THROW(memoized(1), std::invalid_argument); + EXPECT_THROW(memoized_thread_unsafe(1), std::invalid_argument); +} + TEST(MemoizeLruTest, MovesKey) { auto memoized = MemoizeLru([](const std::unique_ptr& key) { return *key; }, 2); EXPECT_EQ(memoized(std::make_unique(1)), 1); diff --git a/src/iceberg/util/cache_internal.h b/src/iceberg/util/cache_internal.h index 45638143c9..9ff53d6d30 100644 --- a/src/iceberg/util/cache_internal.h +++ b/src/iceberg/util/cache_internal.h @@ -25,11 +25,11 @@ /// \file iceberg/util/cache_internal.h /// \brief Provide internal LRU cache and memoization helpers. -#include #include #include #include #include +#include #include #include #include @@ -54,7 +54,7 @@ struct UnitWeigher { // used entries are evicted first to make room. `Weigher` computes the weight of an entry // from its key and value when the entry is inserted or replaced, e.g. its size in bytes. // Weights must be non-negative. With the default UnitWeigher, `max_weight` is the -// maximum number of entries. +// maximum number of entries. Storage grows as entries are inserted. template , typename KeyEqual = std::equal_to> class LruCache { @@ -62,7 +62,6 @@ class LruCache { explicit LruCache(int64_t max_weight, Weigher weigher = Weigher{}) : max_weight_(max_weight), weigher_(std::move(weigher)) { ICEBERG_DCHECK(max_weight_ >= 0, "Cache maximum weight must be non-negative"); - ReserveForUnitWeight(); } LruCache(const LruCache&) = delete; @@ -74,8 +73,6 @@ class LruCache { items_.clear(); map_.clear(); total_weight_ = 0; - // The C++ spec doesn't tell whether map_.clear() will shrink the map capacity - ReserveForUnitWeight(); } // Number of retained entries. @@ -97,7 +94,6 @@ class LruCache { while (total_weight_ > max_weight_) { EvictLeastRecentlyUsed(); } - ReserveForUnitWeight(); } template @@ -187,14 +183,6 @@ class LruCache { items_.pop_back(); } - void ReserveForUnitWeight() { - // Only with unit weights does the maximum weight bound the number of entries. The - // map size can temporarily exceed it by one, see Replace(). - if constexpr (std::is_same_v) { - map_.reserve(static_cast(max_weight_) + 1); - } - } - int64_t max_weight_; Weigher weigher_; int64_t total_weight_ = 0; @@ -222,7 +210,11 @@ struct ThreadSafeMemoizer { lock.unlock(); Value value = func_(key); lock.lock(); - return *cache_.Replace(std::forward(key), std::move(value)).second; + const auto* cached = cache_.Replace(std::forward(key), std::move(value)).second; + if (cached == nullptr) { + throw std::invalid_argument("Memoization cache capacity must be positive"); + } + return *cached; } private: @@ -245,7 +237,11 @@ struct ThreadUnsafeMemoizer { return *value_ptr; } Value value = func_(key); - return *cache_.Replace(std::forward(key), std::move(value)).second; + const auto* cached = cache_.Replace(std::forward(key), std::move(value)).second; + if (cached == nullptr) { + throw std::invalid_argument("Memoization cache capacity must be positive"); + } + return *cached; } private: @@ -271,7 +267,7 @@ auto Memoize(Func&& func, int32_t cache_capacity) { return Memoizer(std::forward(func), cache_capacity); } -// Apply a LRU memoization cache to a callable. +// Apply a LRU memoization cache to a callable. Calls require positive capacity. template auto MemoizeLru(Func&& func, int32_t cache_capacity) { return Memoize(std::forward(func), cache_capacity); From 94195fd73034b9e1a85dbd81cf5086ed5c888c51 Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Wed, 25 Nov 2026 22:14:13 +0800 Subject: [PATCH 12/16] fix(util): preserve LRU weights across cache updates Co-authored-by: Codex --- src/iceberg/test/cache_test.cc | 31 ++++++++++++++++++ src/iceberg/util/cache_internal.h | 54 +++++++++++++++---------------- 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/iceberg/test/cache_test.cc b/src/iceberg/test/cache_test.cc index 37af5a6fdf..dc72d5128a 100644 --- a/src/iceberg/test/cache_test.cc +++ b/src/iceberg/test/cache_test.cc @@ -18,6 +18,7 @@ */ #include +#include #include #include #include @@ -82,6 +83,8 @@ TEST(LruCacheTest, WeightedEviction) { // 7 + 6 exceeds 10, so the least recently used entry 2 is evicted. auto [inserted, value] = cache.Replace(3, "cccccc"); + static_assert(std::same_as); + static_assert(std::same_as); EXPECT_TRUE(inserted); ASSERT_NE(value, nullptr); EXPECT_EQ(*value, "cccccc"); @@ -159,6 +162,34 @@ TEST(LruCacheTest, ReplaceReweighsEntry) { EXPECT_EQ(cache.total_weight(), 0); } +TEST(LruCacheTest, ThrowingWeigherPreservesEntriesWeightAndRecency) { + auto weigher = [](int32_t, const std::string& value) { + if (value.empty()) { + throw std::invalid_argument("empty value"); + } + return static_cast(value.size()); + }; + LruCache cache(4, weigher); + cache.Replace(1, "aa"); + cache.Replace(2, "bb"); + + EXPECT_THROW(cache.Replace(3, ""), std::invalid_argument); + EXPECT_EQ(cache.Find(3), nullptr); + EXPECT_THROW(cache.Replace(2, ""), std::invalid_argument); + const auto* original = cache.Find(2); + ASSERT_NE(original, nullptr); + EXPECT_EQ(*original, "bb"); + EXPECT_THROW(cache.Replace(1, ""), std::invalid_argument); + EXPECT_EQ(cache.size(), 2); + EXPECT_EQ(cache.total_weight(), 4); + + // The failed replacement of key 1 must leave it least recently used. + EXPECT_TRUE(cache.Replace(3, "cc").first); + EXPECT_EQ(cache.Find(1), nullptr); + EXPECT_NE(cache.Find(2), nullptr); + EXPECT_EQ(cache.total_weight(), 4); +} + TEST(LruCacheTest, ClearResetsWeight) { WeightedCache cache(10); cache.Replace(1, "aaaa"); diff --git a/src/iceberg/util/cache_internal.h b/src/iceberg/util/cache_internal.h index 9ff53d6d30..21397a19bf 100644 --- a/src/iceberg/util/cache_internal.h +++ b/src/iceberg/util/cache_internal.h @@ -97,7 +97,7 @@ class LruCache { } template - Value* Find(K&& key) { + const Value* Find(K&& key) { const auto it = map_.find(key); if (it == map_.end()) { return nullptr; @@ -126,41 +126,41 @@ class LruCache { // Returns whether a new entry was added, and a pointer to the cached value. A value // heavier than the maximum weight is not cached: any existing entry for `key` is // removed, other entries are kept, and {false, nullptr} is returned. + // Use Replace to change a cached value. A successful replacement invalidates the + // previous value pointer. If weighing throws, the cache retains its previous state. template - std::pair Replace(K&& key, V&& value) { - // Try to insert temporary iterator - auto [it, inserted] = map_.emplace(std::forward(key), ListIt{}); - ListIt list_it; - if (inserted) { - // Inserted => push item at front of the list, and update iterator - items_.emplace_front(&it->first, std::forward(value)); - list_it = items_.begin(); - it->second = list_it; - } else { - // Already exists => move item at front of the list, and update value - list_it = it->second; - items_.splice(items_.begin(), items_, list_it); - list_it->value = std::forward(value); - total_weight_ -= list_it->weight; - list_it->weight = 0; + std::pair Replace(K&& key, V&& value) { + List candidate; + auto& item = candidate.emplace_front(nullptr, std::forward(value)); + auto [it, inserted] = map_.try_emplace(std::forward(key)); + item.key = &it->first; + try { + item.weight = weigher_(*item.key, std::as_const(item.value)); + } catch (...) { + if (inserted) { + map_.erase(it); + } + throw; } + ICEBERG_DCHECK(item.weight >= 0, "Cache entry weight must be non-negative"); - const int64_t weight = weigher_(it->first, list_it->value); - ICEBERG_DCHECK(weight >= 0, "Cache entry weight must be non-negative"); - if (weight > max_weight_) { - items_.erase(list_it); + if (!inserted) { + total_weight_ -= it->second->weight; + items_.erase(it->second); + } + if (item.weight > max_weight_) { map_.erase(it); return {false, nullptr}; } - // The item is at the front of the list and not yet counted in total_weight_, and - // `max_weight_ - weight` is non-negative, so eviction stops before reaching it. - while (total_weight_ > max_weight_ - weight) { + items_.splice(items_.begin(), candidate); + it->second = items_.begin(); + // Subtract from the limit before adding the new weight to avoid overflow. + while (total_weight_ > max_weight_ - item.weight) { EvictLeastRecentlyUsed(); } - list_it->weight = weight; - total_weight_ += weight; - return {inserted, &list_it->value}; + total_weight_ += item.weight; + return {inserted, &item.value}; } private: From 01ccbf117f94d94579bf19f94d20a9685ec071bf Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Wed, 25 Nov 2026 22:19:11 +0800 Subject: [PATCH 13/16] feat(io): add a bounded cache for immutable file content Co-authored-by: Codex --- src/iceberg/CMakeLists.txt | 1 + src/iceberg/content_cache_internal.cc | 408 ++++++++++++++++++++++++ src/iceberg/content_cache_internal.h | 93 ++++++ src/iceberg/file_io.cc | 21 +- src/iceberg/test/CMakeLists.txt | 1 + src/iceberg/test/metadata_cache_test.cc | 292 +++++++++++++++++ src/iceberg/util/error_util_internal.h | 14 + 7 files changed, 812 insertions(+), 18 deletions(-) create mode 100644 src/iceberg/content_cache_internal.cc create mode 100644 src/iceberg/content_cache_internal.h create mode 100644 src/iceberg/test/metadata_cache_test.cc diff --git a/src/iceberg/CMakeLists.txt b/src/iceberg/CMakeLists.txt index d459020197..1bd4afec3c 100644 --- a/src/iceberg/CMakeLists.txt +++ b/src/iceberg/CMakeLists.txt @@ -26,6 +26,7 @@ set(ICEBERG_SOURCES catalog/memory/in_memory_catalog.cc catalog/session_catalog.cc catalog/session_context.cc + content_cache_internal.cc data_table_scan.cc delete_file_index.cc deletes/dv_util.cc diff --git a/src/iceberg/content_cache_internal.cc b/src/iceberg/content_cache_internal.cc new file mode 100644 index 0000000000..e9ca69edcd --- /dev/null +++ b/src/iceberg/content_cache_internal.cc @@ -0,0 +1,408 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "iceberg/content_cache_internal.h" + +#include +#include +#include +#include +#include + +#include "iceberg/logging/log_macros.h" +#include "iceberg/util/error_util_internal.h" +#include "iceberg/util/macros.h" +#include "iceberg/util/string_util.h" + +namespace iceberg::internal { + +struct ContentCacheState { + explicit ContentCacheState(size_t max_total_bytes) + : lru(static_cast(max_total_bytes)) {} + + LruCache + lru; +}; + +// Owns cache states, never their per-table handles or storage FileIOs. Callers hold +// the handle's mutex before accessing a state; the registry mutex only protects the +// global LRU. Eviction releases content without calling back into a table handle. +class ContentCacheRegistry { + public: + explicit ContentCacheRegistry(int32_t capacity) : caches_(capacity) {} + + std::shared_ptr Find(const ContentCache* owner) { + std::lock_guard lock(mutex_); + const auto* state = caches_.Find(owner); + return state ? *state : nullptr; + } + + std::shared_ptr GetOrCreate( + const ContentCache* owner, const ContentCacheOptions& options, + std::weak_ptr& weak_state) { + std::lock_guard lock(mutex_); + if (auto* state = caches_.Find(owner)) { + return *state; + } + auto state = weak_state.lock(); + if (!state) { + state = std::make_shared(options.max_total_bytes); + } + caches_.Replace(owner, state); + weak_state = state; + return state; + } + + void Erase(const ContentCache* owner) { + std::lock_guard lock(mutex_); + caches_.Erase(owner); + } + + int32_t SetCapacity(int32_t max_caches) { + ICEBERG_DCHECK(max_caches > 0, "Global manifest cache limit must be positive"); + std::lock_guard lock(mutex_); + auto previous = static_cast(caches_.max_weight()); + caches_.Resize(max_caches); + return previous; + } + + private: + std::mutex mutex_; + LruCache> caches_; +}; + +namespace { + +int32_t InitialGlobalCacheCapacity() { + constexpr int32_t kDefaultCapacity = 8; + constexpr const char* kLimitEnv = "ICEBERG_IO_MANIFEST_CACHE_LIMIT"; + if (const char* configured_limit = std::getenv(kLimitEnv)) { + auto limit = StringUtils::ParseNumber(configured_limit); + if (limit && *limit > 0) { + return *limit; + } + ICEBERG_LOG_WARN("{} must be a positive integer that fits in int32_t. Using {}.", + kLimitEnv, kDefaultCapacity); + } + return kDefaultCapacity; +} + +std::shared_ptr GlobalContentCacheRegistry() { + // Handles keep the registry alive even when destroyed during static teardown. + static auto registry = + std::make_shared(InitialGlobalCacheCapacity()); + return registry; +} + +template +Result ParseNumberProperty( + const std::unordered_map& properties, std::string_view key, + T fallback) { + auto it = properties.find(std::string(key)); + if (it == properties.end()) return fallback; + auto result = StringUtils::ParseNumber(it->second); + if (!result) { + return InvalidArgument("Invalid numeric value '{}' for property '{}': {}", it->second, + key, result.error().message); + } + return result; +} + +Result> ReadInputFile(InputFile& input_file, int64_t read_size, + std::string_view file_location) { + if (read_size < 0) { + return Invalid("Invalid negative file size {} for {}", read_size, file_location); + } + if (std::cmp_greater(read_size, std::numeric_limits::max())) { + return Invalid("File size {} exceeds size_t max for {}", read_size, file_location); + } + + auto size = static_cast(read_size); + std::vector content(size); + ICEBERG_ASSIGN_OR_RAISE(auto stream, input_file.Open()); + Status read_status = {}; + if (size > 0) { + read_status = stream->ReadFully(/*position=*/0, content); + } + ICEBERG_RETURN_UNEXPECTED( + FinishWithCloseStatus(std::move(read_status), stream->Close())); + return content; +} + +class CachedSeekableInputStream : public SeekableInputStream { + public: + explicit CachedSeekableInputStream(MetadataContent content) + : content_(std::move(content)) {} + + Result Position() const override { return position_; } + + Status Seek(int64_t position) override { + ICEBERG_PRECHECK(!closed_, "Input stream is closed"); + ICEBERG_PRECHECK(position >= 0, "Position must not be negative: {}", position); + ICEBERG_PRECHECK(std::cmp_less_equal(position, content_->size()), + "Position {} exceeds file size {}", position, content_->size()); + position_ = position; + return {}; + } + + Result Read(std::span out) override { + ICEBERG_PRECHECK(!closed_, "Input stream is closed"); + auto position = static_cast(position_); + auto bytes_to_read = std::min(out.size(), content_->size() - position); + if (bytes_to_read > 0) { + std::memcpy(out.data(), content_->data() + position, bytes_to_read); + position_ += static_cast(bytes_to_read); + } + return static_cast(bytes_to_read); + } + + Status ReadFully(int64_t position, std::span out) override { + ICEBERG_PRECHECK(!closed_, "Input stream is closed"); + ICEBERG_PRECHECK(position >= 0, "Position must not be negative: {}", position); + ICEBERG_PRECHECK(std::cmp_less_equal(position, content_->size()), + "Position {} exceeds file size {}", position, content_->size()); + auto offset = static_cast(position); + ICEBERG_PRECHECK(out.size() <= content_->size() - offset, + "Read out of bounds: offset {} + length {} exceeds file size {}", + position, out.size(), content_->size()); + if (!out.empty()) { + std::memcpy(out.data(), content_->data() + offset, out.size()); + } + return {}; + } + + Status Close() override { + closed_ = true; + return {}; + } + + private: + MetadataContent content_; + int64_t position_ = 0; + bool closed_ = false; +}; + +class CachedInputFile : public InputFile { + public: + CachedInputFile(std::unique_ptr input, std::shared_ptr cache, + int64_t size) + : input_(std::move(input)), + cache_(std::move(cache)), + location_(input_->location()), + size_(size) {} + CachedInputFile(std::string location, MetadataContent content) + : location_(std::move(location)), + size_(static_cast(content->size())), + content_(std::move(content)) {} + std::string_view location() const override { return location_; } + Result Size() const override { return size_; } + Result> Open() override { + if (content_) { + return std::make_unique(content_); + } + uint64_t generation; + if (auto cached = cache_->Find(location_, &generation)) { + return std::make_unique(std::move(cached)); + } + auto loaded = ReadInputFile(*input_, size_, location_); + if (!loaded) { + auto error = std::move(loaded).error(); + if (error.kind != ErrorKind::kIOError) return unexpected(std::move(error)); + auto fallback = input_->Open(); + if (!fallback) { + error.message += "; fallback open failed: " + fallback.error().message; + return unexpected(std::move(error)); + } + return fallback; + } + auto content = + std::make_shared>(std::move(loaded).value()); + cache_->Insert(location_, content, generation); + return std::make_unique(std::move(content)); + } + + private: + std::unique_ptr input_; + std::shared_ptr cache_; + std::string location_; + int64_t size_; + MetadataContent content_; +}; + +// Only manifest readers receive this adapter. Output and deletion operations are +// forwarded unchanged, allowing manifest maintenance to use the same read context. +class ManifestFileIO : public FileIO { + public: + ManifestFileIO(std::shared_ptr io, std::shared_ptr cache) + : io_(std::move(io)), cache_(std::move(cache)) {} + Result> NewInputFile(std::string location) override { + return cache_->NewInputFile(io_, std::move(location)); + } + Result> NewInputFile(std::string location, + size_t length) override { + return cache_->NewInputFile(io_, std::move(location), length); + } + Result ReadFile(const std::string& location, + std::optional length) override { + // A requested byte count can cover only a prefix of an immutable file. + if (length) { + return io_->ReadFile(location, length); + } + return FileIO::ReadFile(location, std::nullopt); + } + Result> NewOutputFile(std::string location) override { + return io_->NewOutputFile(std::move(location)); + } + Status DeleteFile(const std::string& location) override { + auto status = io_->DeleteFile(location); + cache_->Invalidate(location); + return status; + } + Status DeleteFiles(const std::vector& locations) override { + auto status = io_->DeleteFiles(locations); + // A failed bulk delete can still remove some files. + for (const auto& location : locations) { + cache_->Invalidate(location); + } + return status; + } + const std::unordered_map& properties() const override { + return io_->properties(); + } + + private: + std::shared_ptr io_; + std::shared_ptr cache_; +}; + +} // namespace + +Result ContentCacheOptions::FromProperties( + const std::unordered_map& properties) { + ContentCacheOptions options; + if (auto it = properties.find(std::string(kEnabled)); it != properties.end()) { + options.enabled = StringUtils::ParseBoolean(it->second); + } + if (!options.enabled) { + return options; + } + ICEBERG_ASSIGN_OR_RAISE( + options.max_total_bytes, + ParseNumberProperty(properties, kMaxTotalBytes, options.max_total_bytes)); + ICEBERG_ASSIGN_OR_RAISE( + options.max_content_length, + ParseNumberProperty(properties, kMaxContentLength, options.max_content_length)); + ICEBERG_PRECHECK( + options.max_total_bytes > 0 && + std::cmp_less_equal(options.max_total_bytes, + std::numeric_limits::max()), + "Manifest cache maximum total bytes must be positive and fit in int64_t"); + ICEBERG_PRECHECK( + options.max_content_length > 0 && + std::cmp_less_equal(options.max_content_length, + std::numeric_limits::max()), + "Manifest cache maximum content length must be positive and fit in int64_t"); + return options; +} + +ContentCache::ContentCache(ContentCacheOptions options) + : options_(options), registry_(GlobalContentCacheRegistry()) {} + +ContentCache::~ContentCache() { registry_->Erase(this); } + +int32_t ContentCache::SetGlobalCapacity(int32_t max_caches) { + return GlobalContentCacheRegistry()->SetCapacity(max_caches); +} + +std::shared_ptr ContentCache::Wrap(std::shared_ptr io) { + return std::make_shared(std::move(io), shared_from_this()); +} + +MetadataContent ContentCache::Find(std::string_view location, uint64_t* generation) { + std::lock_guard lock(mutex_); + if (generation) *generation = generation_; + auto state = registry_->Find(this); + const auto* content = state ? state->lru.Find(location) : nullptr; + return content ? *content : nullptr; +} + +void ContentCache::Insert(const std::string& location, MetadataContent content, + uint64_t generation) { + // Empty/corrupt metadata must not accumulate unbounded zero-weight entries. + if (content->empty() || content->size() > options_.max_content_length || + content->size() > options_.max_total_bytes) { + return; + } + std::lock_guard lock(mutex_); + if (generation == generation_) { + registry_->GetOrCreate(this, options_, state_) + ->lru.Replace(location, std::move(content)); + } +} + +void ContentCache::Invalidate(std::string_view location) { + std::lock_guard lock(mutex_); + if (auto state = state_.lock()) { + state->lru.Erase(location); + } + ++generation_; +} + +void ContentCache::Clear() { + std::lock_guard lock(mutex_); + registry_->Erase(this); + state_.reset(); + ++generation_; +} + +Result> ContentCache::NewInputFile( + const std::shared_ptr& io, std::string location, + std::optional length) { + if (length && std::cmp_greater(*length, std::numeric_limits::max())) { + return InvalidArgument("File length {} exceeds int64_t max", *length); + } + // A hit needs neither storage resolution nor working remote credentials. + if (auto content = Find(location)) { + return std::make_unique(std::move(location), std::move(content)); + } + std::unique_ptr input; + if (length) { + ICEBERG_ASSIGN_OR_RAISE(input, io->NewInputFile(location, *length)); + } else { + ICEBERG_ASSIGN_OR_RAISE(input, io->NewInputFile(location)); + } + if (auto content = Find(input->location())) { + return std::make_unique(std::string(input->location()), + std::move(content)); + } + int64_t size; + if (length) { + size = static_cast(*length); + } else { + ICEBERG_ASSIGN_OR_RAISE(size, input->Size()); + } + ICEBERG_PRECHECK(size >= 0, "Invalid negative file size {}", size); + if (size == 0 || std::cmp_greater(size, options_.max_content_length) || + std::cmp_greater(size, options_.max_total_bytes)) { + return input; + } + return std::make_unique(std::move(input), shared_from_this(), size); +} + +} // namespace iceberg::internal diff --git a/src/iceberg/content_cache_internal.h b/src/iceberg/content_cache_internal.h new file mode 100644 index 0000000000..69d31bc726 --- /dev/null +++ b/src/iceberg/content_cache_internal.h @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "iceberg/file_io.h" +#include "iceberg/util/cache_internal.h" +#include "iceberg/util/string_util.h" + +namespace iceberg::internal { + +// Cache limits apply independently to each loaded table. Expiration is not supported. +struct ICEBERG_EXPORT ContentCacheOptions { + inline static constexpr std::string_view kEnabled = "io.manifest.cache-enabled"; + inline static constexpr std::string_view kMaxTotalBytes = + "io.manifest.cache.max-total-bytes"; + inline static constexpr std::string_view kMaxContentLength = + "io.manifest.cache.max-content-length"; + bool enabled = false; + size_t max_total_bytes = 100 * 1024 * 1024; + size_t max_content_length = 8 * 1024 * 1024; + friend bool operator==(const ContentCacheOptions&, + const ContentCacheOptions&) = default; + static Result FromProperties( + const std::unordered_map& properties); +}; + +using MetadataContent = std::shared_ptr>; +struct MetadataContentWeigher { + int64_t operator()(const std::string&, const MetadataContent& content) const { + return static_cast(content->size()); + } +}; + +struct ContentCacheState; +class ContentCacheRegistry; + +// A lightweight per-table handle with a weak reference to cached content. A global +// LRU registry retains at most eight cache states by default. Readers share the +// handle through a manifest-only FileIO adapter without pinning its cache state. +class ICEBERG_EXPORT ContentCache : public std::enable_shared_from_this { + public: + explicit ContentCache(ContentCacheOptions options); + ~ContentCache(); + // Return the previous process-wide capacity. The new capacity must be positive. + static int32_t SetGlobalCapacity(int32_t max_caches); + const ContentCacheOptions& options() const { return options_; } + std::shared_ptr Wrap(std::shared_ptr io); + Result> NewInputFile( + const std::shared_ptr& io, std::string location, + std::optional length = std::nullopt); + // A load that overlaps invalidation may serve its own reader but cannot repopulate + // the cache. Capture the generation under the same lock as the lookup. + MetadataContent Find(std::string_view location, uint64_t* generation = nullptr); + void Insert(const std::string& location, MetadataContent content, uint64_t generation); + void Invalidate(std::string_view location); + void Clear(); + + private: + const ContentCacheOptions options_; + const std::shared_ptr registry_; + std::mutex mutex_; + std::weak_ptr state_; + uint64_t generation_ = 0; +}; + +} // namespace iceberg::internal diff --git a/src/iceberg/file_io.cc b/src/iceberg/file_io.cc index 543c13b03a..827c9b325b 100644 --- a/src/iceberg/file_io.cc +++ b/src/iceberg/file_io.cc @@ -22,26 +22,11 @@ #include #include +#include "iceberg/util/error_util_internal.h" #include "iceberg/util/macros.h" namespace iceberg { -namespace { - -Status FinishWithCloseStatus(Status operation_status, Status close_status) { - if (!operation_status.has_value()) { - auto error = operation_status.error(); - if (!close_status.has_value()) { - error.message += "; additionally failed to close stream: "; - error.message += close_status.error().message; - } - return ::iceberg::unexpected(std::move(error)); - } - return close_status; -} - -} // namespace - Result> FileIO::NewInputFile(std::string file_location) { return NotImplemented("NewInputFile not implemented for {}", file_location); } @@ -82,7 +67,7 @@ Result FileIO::ReadFile(const std::string& file_location, read_status = stream->ReadFully(/*position=*/0, bytes); } ICEBERG_RETURN_UNEXPECTED( - FinishWithCloseStatus(std::move(read_status), stream->Close())); + internal::FinishWithCloseStatus(std::move(read_status), stream->Close())); return content; } @@ -97,7 +82,7 @@ Status FileIO::WriteFile(const std::string& file_location, std::string_view cont if (status.has_value()) { status = stream->Flush(); } - return FinishWithCloseStatus(std::move(status), stream->Close()); + return internal::FinishWithCloseStatus(std::move(status), stream->Close()); } Status FileIO::DeleteFiles(const std::vector& file_locations) { diff --git a/src/iceberg/test/CMakeLists.txt b/src/iceberg/test/CMakeLists.txt index 70c31275d0..09dbc28c3f 100644 --- a/src/iceberg/test/CMakeLists.txt +++ b/src/iceberg/test/CMakeLists.txt @@ -180,6 +180,7 @@ add_iceberg_test(util_test lazy_test.cc location_util_test.cc math_util_internal_test.cc + metadata_cache_test.cc executor_util_test.cc roaring_position_bitmap_test.cc position_delete_index_test.cc diff --git a/src/iceberg/test/metadata_cache_test.cc b/src/iceberg/test/metadata_cache_test.cc new file mode 100644 index 0000000000..b68d337133 --- /dev/null +++ b/src/iceberg/test/metadata_cache_test.cc @@ -0,0 +1,292 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "iceberg/content_cache_internal.h" +#include "iceberg/file_io.h" +#include "iceberg/test/matchers.h" +#include "iceberg/test/mock_io.h" +#include "iceberg/util/macros.h" + +namespace iceberg { +namespace { +using internal::ContentCacheOptions; + +#ifdef GTEST_HAS_DEATH_TEST +int SetManifestCacheLimitEnv(const char* value) { + constexpr const char* kLimitEnv = "ICEBERG_IO_MANIFEST_CACHE_LIMIT"; +# ifdef _WIN32 + return _putenv_s(kLimitEnv, value); +# else + return setenv(kLimitEnv, value, /*overwrite=*/1); +# endif +} + +TEST(MetadataCacheDeathTest, InitializesGlobalLimitFromEnvironment) { + // Start with a new registry for every environment setting. + GTEST_FLAG_SET(death_test_style, "threadsafe"); + for (const auto& [value, expected_limit] : + {std::pair{"2", 2}, std::pair{"0", 8}, std::pair{"-1", 8}, std::pair{"2x", 8}, + std::pair{"2147483648", 8}}) { + SCOPED_TRACE(value); + EXPECT_EXIT( + { + if (SetManifestCacheLimitEnv(value) != 0) { + std::_Exit(EXIT_FAILURE); + } + const auto initial_limit = internal::ContentCache::SetGlobalCapacity(3); + const auto overridden_limit = internal::ContentCache::SetGlobalCapacity(4); + std::_Exit(initial_limit == expected_limit && overridden_limit == 3 + ? EXIT_SUCCESS + : EXIT_FAILURE); + }, + ::testing::ExitedWithCode(EXIT_SUCCESS), ""); + } +} +#endif + +class ScopedGlobalCacheLimit { + public: + explicit ScopedGlobalCacheLimit(int32_t limit) + : previous_(internal::ContentCache::SetGlobalCapacity(limit)) {} + ~ScopedGlobalCacheLimit() { internal::ContentCache::SetGlobalCapacity(previous_); } + + private: + int32_t previous_; +}; + +struct FailingOpenState { + int open_count = 0; + Error first_error; + Error fallback_error; +}; + +class FailingOpenInputFile : public InputFile { + public: + explicit FailingOpenInputFile(std::shared_ptr state) + : state_(std::move(state)) {} + + std::string_view location() const override { return "manifest.avro"; } + Result Size() const override { return 8; } + + Result> Open() override { + ++state_->open_count; + if (state_->open_count == 1) { + return unexpected(state_->first_error); + } + return unexpected(state_->fallback_error); + } + + private: + std::shared_ptr state_; +}; + +class FailingOpenFileIO : public FileIO { + public: + explicit FailingOpenFileIO(std::shared_ptr state) + : state_(std::move(state)) {} + + Result> NewInputFile( + std::string /*file_location*/) override { + return std::make_unique(state_); + } + + private: + std::shared_ptr state_; +}; + +TEST(MetadataCacheTest, CachedInputFileDoesNotRetryNonIOErrors) { + auto state = std::make_shared(FailingOpenState{ + .first_error = {.kind = ErrorKind::kNotFound, .message = "missing"}, + .fallback_error = {.kind = ErrorKind::kIOError, .message = "unexpected retry"}, + }); + auto cache = std::make_shared(ContentCacheOptions{}); + auto adapter = cache->Wrap(std::make_shared(state)); + ICEBERG_UNWRAP_OR_FAIL(auto input_file, adapter->NewInputFile("manifest.avro")); + + EXPECT_THAT(input_file->Open(), IsError(ErrorKind::kNotFound)); + EXPECT_EQ(state->open_count, 1); +} + +TEST(MetadataCacheTest, CachedInputFilePreservesReadAheadAndFallbackErrors) { + auto state = std::make_shared(FailingOpenState{ + .first_error = {.kind = ErrorKind::kIOError, .message = "read-ahead failed"}, + .fallback_error = {.kind = ErrorKind::kIOError, .message = "open failed"}, + }); + auto cache = std::make_shared(ContentCacheOptions{}); + auto adapter = cache->Wrap(std::make_shared(state)); + ICEBERG_UNWRAP_OR_FAIL(auto input_file, adapter->NewInputFile("manifest.avro")); + + auto result = input_file->Open(); + EXPECT_THAT(result, IsError(ErrorKind::kIOError)); + EXPECT_THAT(result, HasErrorMessage("read-ahead failed")); + EXPECT_THAT(result, HasErrorMessage("fallback open failed: open failed")); + EXPECT_EQ(state->open_count, 2); +} + +TEST(MetadataCacheTest, ParsesJavaCompatibleProperties) { + std::unordered_map properties = { + {std::string(ContentCacheOptions::kEnabled), "true"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "200"}, + {std::string(ContentCacheOptions::kMaxContentLength), "50"}, + }; + + auto options = ContentCacheOptions::FromProperties(properties); + + ASSERT_THAT(options, IsOk()); + EXPECT_TRUE(options->enabled); + EXPECT_EQ(options->max_total_bytes, 200); + EXPECT_EQ(options->max_content_length, 50); +} + +TEST(MetadataCacheTest, ParsesEnabledPropertyIgnoringCase) { + for (const auto* value : {"true", "TRUE", "True", "tRuE"}) { + SCOPED_TRACE(value); + ICEBERG_UNWRAP_OR_FAIL(auto options, + ContentCacheOptions::FromProperties( + {{std::string(ContentCacheOptions::kEnabled), value}})); + EXPECT_TRUE(options.enabled); + } +} + +TEST(MetadataCacheTest, TreatsOtherEnabledPropertyValuesAsFalseLikeJava) { + for (const auto* value : + {"false", "FALSE", "False", "fAlSe", "", "yes", "1", "ture", " true "}) { + SCOPED_TRACE(value); + ICEBERG_UNWRAP_OR_FAIL(auto options, + ContentCacheOptions::FromProperties( + {{std::string(ContentCacheOptions::kEnabled), value}})); + EXPECT_FALSE(options.enabled); + } +} + +TEST(MetadataCacheTest, UsesJavaCompatibleDefaults) { + auto options = ContentCacheOptions::FromProperties( + {{std::string(ContentCacheOptions::kEnabled), "true"}}); + + ASSERT_THAT(options, IsOk()); + EXPECT_TRUE(options->enabled); + EXPECT_EQ(options->max_total_bytes, 100 * 1024 * 1024); + EXPECT_EQ(options->max_content_length, 8 * 1024 * 1024); +} + +TEST(MetadataCacheTest, InvalidNumericPropertyErrorIncludesPropertyName) { + auto options = ContentCacheOptions::FromProperties( + {{std::string(ContentCacheOptions::kEnabled), "true"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "invalid"}}); + + EXPECT_THAT(options, HasErrorMessage(std::string(ContentCacheOptions::kMaxTotalBytes))); +} + +TEST(MetadataCacheTest, IgnoresInvalidTuningPropertiesWhenDisabled) { + ICEBERG_UNWRAP_OR_FAIL( + auto options, ContentCacheOptions::FromProperties( + {{std::string(ContentCacheOptions::kEnabled), "false"}, + {std::string(ContentCacheOptions::kMaxTotalBytes), "invalid"}})); + EXPECT_FALSE(options.enabled); +} + +TEST(MetadataCacheTest, PrefixReadDoesNotPopulateWholeFileCache) { + auto storage = std::make_shared(); + storage->AddFile("manifest.avro", "manifest"); + auto cache = std::make_shared(ContentCacheOptions{}); + auto adapter = cache->Wrap(storage); + + EXPECT_CALL(*storage, ReadFile("manifest.avro", std::optional{4})) + .WillOnce(::testing::Return(std::string("mani"))); + EXPECT_THAT(adapter->ReadFile("manifest.avro", 4), HasValue(::testing::Eq("mani"))); + EXPECT_FALSE(cache->Find("manifest.avro")); + EXPECT_THAT(adapter->ReadFile("manifest.avro", std::nullopt), + HasValue(::testing::Eq("manifest"))); +} + +TEST(MetadataCacheTest, OversizedReadDoesNotDisplaceAWarmCache) { + ScopedGlobalCacheLimit limit(1); + auto warm = std::make_shared(ContentCacheOptions{}); + warm->Insert("warm.avro", std::make_shared>(1), 0); + + auto storage = std::make_shared(); + storage->AddFile("large.avro", "large"); + auto cold = std::make_shared( + ContentCacheOptions{.max_content_length = 4}); + EXPECT_THAT(cold->Wrap(storage)->ReadFile("large.avro", std::nullopt), + HasValue(::testing::Eq("large"))); + EXPECT_FALSE(cold->Find("large.avro")); + EXPECT_TRUE(warm->Find("warm.avro")); +} + +TEST(MetadataCacheTest, GlobalEvictionReleasesBytesAndReadersKeepTheirContent) { + ScopedGlobalCacheLimit limit(1); + auto first = std::make_shared(ContentCacheOptions{}); + auto content = std::make_shared>(8); + std::weak_ptr> weak_content = content; + first->Insert("manifest.avro", std::move(content), 0); + auto adapter = first->Wrap(std::make_shared()); + ICEBERG_UNWRAP_OR_FAIL(auto file, adapter->NewInputFile("manifest.avro")); + ICEBERG_UNWRAP_OR_FAIL(auto reader, file->Open()); + file.reset(); + auto second = std::make_shared(ContentCacheOptions{}); + second->Insert("other.avro", std::make_shared>(8), 0); + EXPECT_FALSE(first->Find("manifest.avro")); + // Only the active reader still owns these bytes after global eviction. + EXPECT_FALSE(weak_content.expired()); + std::array out; + ASSERT_THAT(reader->ReadFully(0, out), IsOk()); + EXPECT_EQ(out, (std::array{})); + reader.reset(); + EXPECT_TRUE(weak_content.expired()); +} + +TEST(MetadataCacheTest, DestroyingLastCacheHandleReleasesRetainedBytes) { + auto cache = std::make_shared(ContentCacheOptions{}); + auto content = std::make_shared>(8); + std::weak_ptr> weak_content = content; + cache->Insert("manifest.avro", std::move(content), 0); + ASSERT_FALSE(weak_content.expired()); + cache.reset(); + EXPECT_TRUE(weak_content.expired()); +} + +TEST(MetadataCacheTest, InvalidatedLoadCannotPopulateRecreatedCacheState) { + ScopedGlobalCacheLimit limit(1); + auto cache = std::make_shared(ContentCacheOptions{}); + uint64_t generation; + ASSERT_FALSE(cache->Find("manifest.avro", &generation)); + auto other = std::make_shared(ContentCacheOptions{}); + other->Insert("other.avro", std::make_shared>(8), 0); + cache->Invalidate("manifest.avro"); + cache->Insert("manifest.avro", std::make_shared>(8), + generation); + EXPECT_FALSE(cache->Find("manifest.avro")); +} + +} // namespace +} // namespace iceberg diff --git a/src/iceberg/util/error_util_internal.h b/src/iceberg/util/error_util_internal.h index 8ecfd3ce15..b6a4280e56 100644 --- a/src/iceberg/util/error_util_internal.h +++ b/src/iceberg/util/error_util_internal.h @@ -21,11 +21,25 @@ #include #include +#include #include "iceberg/logging/log_macros.h" +#include "iceberg/result.h" namespace iceberg::internal { +inline Status FinishWithCloseStatus(Status operation_status, Status close_status) { + if (operation_status.has_value()) { + return close_status; + } + auto error = std::move(operation_status).error(); + if (!close_status.has_value()) { + error.message += "; additionally failed to close stream: "; + error.message += close_status.error().message; + } + return unexpected(std::move(error)); +} + template void LogAndIgnoreFailure(std::string_view name, Action&& action) noexcept { try { From f0e17f0187718d12f398e013c45dfe7fc1b9807a Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Wed, 25 Nov 2026 22:41:06 +0800 Subject: [PATCH 14/16] fix(io): invalidate canonical content after storage deletes 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 --- src/iceberg/CMakeLists.txt | 2 +- ...ent_cache_internal.cc => content_cache.cc} | 151 +++++++++--------- src/iceberg/content_cache_internal.h | 38 ++--- src/iceberg/test/metadata_cache_test.cc | 117 ++++++++------ src/iceberg/test/mock_io.h | 2 + src/iceberg/util/cache_internal.h | 6 +- src/iceberg/util/error_util_internal.h | 2 +- src/iceberg/util/property_util.cc | 13 ++ src/iceberg/util/property_util.h | 6 + 9 files changed, 186 insertions(+), 151 deletions(-) rename src/iceberg/{content_cache_internal.cc => content_cache.cc} (78%) diff --git a/src/iceberg/CMakeLists.txt b/src/iceberg/CMakeLists.txt index 1bd4afec3c..531aa735b1 100644 --- a/src/iceberg/CMakeLists.txt +++ b/src/iceberg/CMakeLists.txt @@ -26,7 +26,7 @@ set(ICEBERG_SOURCES catalog/memory/in_memory_catalog.cc catalog/session_catalog.cc catalog/session_context.cc - content_cache_internal.cc + content_cache.cc data_table_scan.cc delete_file_index.cc deletes/dv_util.cc diff --git a/src/iceberg/content_cache_internal.cc b/src/iceberg/content_cache.cc similarity index 78% rename from src/iceberg/content_cache_internal.cc rename to src/iceberg/content_cache.cc index e9ca69edcd..00f122320a 100644 --- a/src/iceberg/content_cache_internal.cc +++ b/src/iceberg/content_cache.cc @@ -17,21 +17,26 @@ * under the License. */ -#include "iceberg/content_cache_internal.h" - #include -#include #include #include #include -#include "iceberg/logging/log_macros.h" +#include "iceberg/content_cache_internal.h" +#include "iceberg/util/cache_internal.h" #include "iceberg/util/error_util_internal.h" #include "iceberg/util/macros.h" +#include "iceberg/util/property_util.h" #include "iceberg/util/string_util.h" namespace iceberg::internal { +struct MetadataContentWeigher { + int64_t operator()(const std::string&, const MetadataContent& content) const { + return static_cast(content->size()); + } +}; + struct ContentCacheState { explicit ContentCacheState(size_t max_total_bytes) : lru(static_cast(max_total_bytes)) {} @@ -89,41 +94,12 @@ class ContentCacheRegistry { namespace { -int32_t InitialGlobalCacheCapacity() { - constexpr int32_t kDefaultCapacity = 8; - constexpr const char* kLimitEnv = "ICEBERG_IO_MANIFEST_CACHE_LIMIT"; - if (const char* configured_limit = std::getenv(kLimitEnv)) { - auto limit = StringUtils::ParseNumber(configured_limit); - if (limit && *limit > 0) { - return *limit; - } - ICEBERG_LOG_WARN("{} must be a positive integer that fits in int32_t. Using {}.", - kLimitEnv, kDefaultCapacity); - } - return kDefaultCapacity; -} - std::shared_ptr GlobalContentCacheRegistry() { // Handles keep the registry alive even when destroyed during static teardown. - static auto registry = - std::make_shared(InitialGlobalCacheCapacity()); + static auto registry = std::make_shared(8); return registry; } -template -Result ParseNumberProperty( - const std::unordered_map& properties, std::string_view key, - T fallback) { - auto it = properties.find(std::string(key)); - if (it == properties.end()) return fallback; - auto result = StringUtils::ParseNumber(it->second); - if (!result) { - return InvalidArgument("Invalid numeric value '{}' for property '{}': {}", it->second, - key, result.error().message); - } - return result; -} - Result> ReadInputFile(InputFile& input_file, int64_t read_size, std::string_view file_location) { if (read_size < 0) { @@ -201,11 +177,12 @@ class CachedSeekableInputStream : public SeekableInputStream { class CachedInputFile : public InputFile { public: CachedInputFile(std::unique_ptr input, std::shared_ptr cache, - int64_t size) + int64_t size, uint64_t generation) : input_(std::move(input)), cache_(std::move(cache)), location_(input_->location()), - size_(size) {} + size_(size), + generation_(generation) {} CachedInputFile(std::string location, MetadataContent content) : location_(std::move(location)), size_(static_cast(content->size())), @@ -216,8 +193,7 @@ class CachedInputFile : public InputFile { if (content_) { return std::make_unique(content_); } - uint64_t generation; - if (auto cached = cache_->Find(location_, &generation)) { + if (auto cached = cache_->Find(location_)) { return std::make_unique(std::move(cached)); } auto loaded = ReadInputFile(*input_, size_, location_); @@ -233,7 +209,7 @@ class CachedInputFile : public InputFile { } auto content = std::make_shared>(std::move(loaded).value()); - cache_->Insert(location_, content, generation); + cache_->Insert(location_, content, generation_); return std::make_unique(std::move(content)); } @@ -242,11 +218,12 @@ class CachedInputFile : public InputFile { std::shared_ptr cache_; std::string location_; int64_t size_; + uint64_t generation_ = 0; MetadataContent content_; }; -// Only manifest readers receive this adapter. Output and deletion operations are -// forwarded unchanged, allowing manifest maintenance to use the same read context. +// Manifest maintenance creates outputs on storage. Cleanup deletes through storage +// and invalidates the cache after each delete attempt. class ManifestFileIO : public FileIO { public: ManifestFileIO(std::shared_ptr io, std::shared_ptr cache) @@ -269,18 +246,11 @@ class ManifestFileIO : public FileIO { Result> NewOutputFile(std::string location) override { return io_->NewOutputFile(std::move(location)); } - Status DeleteFile(const std::string& location) override { - auto status = io_->DeleteFile(location); - cache_->Invalidate(location); - return status; + Status DeleteFile(const std::string& /*location*/) override { + return NotSupported("Delete files through the storage FileIO"); } - Status DeleteFiles(const std::vector& locations) override { - auto status = io_->DeleteFiles(locations); - // A failed bulk delete can still remove some files. - for (const auto& location : locations) { - cache_->Invalidate(location); - } - return status; + Status DeleteFiles(const std::vector& /*locations*/) override { + return NotSupported("Delete files through the storage FileIO"); } const std::unordered_map& properties() const override { return io_->properties(); @@ -296,28 +266,24 @@ class ManifestFileIO : public FileIO { Result ContentCacheOptions::FromProperties( const std::unordered_map& properties) { ContentCacheOptions options; - if (auto it = properties.find(std::string(kEnabled)); it != properties.end()) { - options.enabled = StringUtils::ParseBoolean(it->second); - } + const auto& enabled = ContentCacheProperties::kEnabled; + options.enabled = + PropertyUtil::PropertyAsBoolean(properties, enabled.key(), enabled.value()); if (!options.enabled) { return options; } - ICEBERG_ASSIGN_OR_RAISE( - options.max_total_bytes, - ParseNumberProperty(properties, kMaxTotalBytes, options.max_total_bytes)); - ICEBERG_ASSIGN_OR_RAISE( - options.max_content_length, - ParseNumberProperty(properties, kMaxContentLength, options.max_content_length)); - ICEBERG_PRECHECK( - options.max_total_bytes > 0 && - std::cmp_less_equal(options.max_total_bytes, - std::numeric_limits::max()), - "Manifest cache maximum total bytes must be positive and fit in int64_t"); - ICEBERG_PRECHECK( - options.max_content_length > 0 && - std::cmp_less_equal(options.max_content_length, - std::numeric_limits::max()), - "Manifest cache maximum content length must be positive and fit in int64_t"); + const auto read_limit = [&properties](const auto& entry) -> Result { + ICEBERG_ASSIGN_OR_RAISE(auto limit, PropertyUtil::PropertyAsInt64( + properties, entry.key(), entry.value())); + ICEBERG_PRECHECK(limit > 0 && std::in_range(limit), + "Property '{}' must be positive and fit in size_t: {}", entry.key(), + limit); + return static_cast(limit); + }; + ICEBERG_ASSIGN_OR_RAISE(options.max_total_bytes, + read_limit(ContentCacheProperties::kMaxTotalBytes)); + ICEBERG_ASSIGN_OR_RAISE(options.max_content_length, + read_limit(ContentCacheProperties::kMaxContentLength)); return options; } @@ -342,7 +308,7 @@ MetadataContent ContentCache::Find(std::string_view location, uint64_t* generati return content ? *content : nullptr; } -void ContentCache::Insert(const std::string& location, MetadataContent content, +void ContentCache::Insert(std::string_view location, MetadataContent content, uint64_t generation) { // Empty/corrupt metadata must not accumulate unbounded zero-weight entries. if (content->empty() || content->size() > options_.max_content_length || @@ -352,16 +318,43 @@ void ContentCache::Insert(const std::string& location, MetadataContent content, std::lock_guard lock(mutex_); if (generation == generation_) { registry_->GetOrCreate(this, options_, state_) - ->lru.Replace(location, std::move(content)); + ->lru.Replace(std::string(location), std::move(content)); } } -void ContentCache::Invalidate(std::string_view location) { +void ContentCache::Invalidate(FileIO& storage, std::string_view location) { + { + std::lock_guard lock(mutex_); + auto state = state_.lock(); + if (!state || state->lru.size() == 0 || state->lru.Erase(location)) { + ++generation_; + return; + } + } + + // A storage implementation may normalize this path. Resolve it outside the + // cache lock, then invalidate loads that overlapped the resolution. + std::unique_ptr input; + try { + if (auto resolved = storage.NewInputFile(std::string(location))) { + input = std::move(*resolved); + } + } catch (...) { + // Failure to resolve an alias must not turn a completed delete into a failure. + Clear(); + return; + } + if (!input) { + // A store may no longer be able to resolve an alias after deletion. + Clear(); + return; + } + std::lock_guard lock(mutex_); + ++generation_; if (auto state = state_.lock()) { - state->lru.Erase(location); + state->lru.Erase(input->location()); } - ++generation_; } void ContentCache::Clear() { @@ -378,7 +371,8 @@ Result> ContentCache::NewInputFile( return InvalidArgument("File length {} exceeds int64_t max", *length); } // A hit needs neither storage resolution nor working remote credentials. - if (auto content = Find(location)) { + uint64_t generation; + if (auto content = Find(location, &generation)) { return std::make_unique(std::move(location), std::move(content)); } std::unique_ptr input; @@ -402,7 +396,8 @@ Result> ContentCache::NewInputFile( std::cmp_greater(size, options_.max_total_bytes)) { return input; } - return std::make_unique(std::move(input), shared_from_this(), size); + return std::make_unique(std::move(input), shared_from_this(), size, + generation); } } // namespace iceberg::internal diff --git a/src/iceberg/content_cache_internal.h b/src/iceberg/content_cache_internal.h index 69d31bc726..8a4d256b10 100644 --- a/src/iceberg/content_cache_internal.h +++ b/src/iceberg/content_cache_internal.h @@ -30,21 +30,26 @@ #include #include "iceberg/file_io.h" -#include "iceberg/util/cache_internal.h" -#include "iceberg/util/string_util.h" +#include "iceberg/util/config.h" namespace iceberg::internal { -// Cache limits apply independently to each loaded table. Expiration is not supported. +class ContentCacheProperties : public ConfigBase { + public: + inline static const Entry kEnabled{"io.manifest.cache-enabled", false}; + inline static const Entry kMaxTotalBytes{"io.manifest.cache.max-total-bytes", + 100 * 1024 * 1024}; + inline static const Entry kMaxContentLength{ + "io.manifest.cache.max-content-length", 8 * 1024 * 1024}; +}; + +// Retain only validated cache settings, independent of the storage credentials. struct ICEBERG_EXPORT ContentCacheOptions { - inline static constexpr std::string_view kEnabled = "io.manifest.cache-enabled"; - inline static constexpr std::string_view kMaxTotalBytes = - "io.manifest.cache.max-total-bytes"; - inline static constexpr std::string_view kMaxContentLength = - "io.manifest.cache.max-content-length"; - bool enabled = false; - size_t max_total_bytes = 100 * 1024 * 1024; - size_t max_content_length = 8 * 1024 * 1024; + bool enabled = ContentCacheProperties::kEnabled.value(); + size_t max_total_bytes = + static_cast(ContentCacheProperties::kMaxTotalBytes.value()); + size_t max_content_length = + static_cast(ContentCacheProperties::kMaxContentLength.value()); friend bool operator==(const ContentCacheOptions&, const ContentCacheOptions&) = default; static Result FromProperties( @@ -52,12 +57,6 @@ struct ICEBERG_EXPORT ContentCacheOptions { }; using MetadataContent = std::shared_ptr>; -struct MetadataContentWeigher { - int64_t operator()(const std::string&, const MetadataContent& content) const { - return static_cast(content->size()); - } -}; - struct ContentCacheState; class ContentCacheRegistry; @@ -78,8 +77,9 @@ class ICEBERG_EXPORT ContentCache : public std::enable_shared_from_this #include -#include #include #include #include #include -#include #include #include @@ -39,39 +37,7 @@ namespace iceberg { namespace { using internal::ContentCacheOptions; - -#ifdef GTEST_HAS_DEATH_TEST -int SetManifestCacheLimitEnv(const char* value) { - constexpr const char* kLimitEnv = "ICEBERG_IO_MANIFEST_CACHE_LIMIT"; -# ifdef _WIN32 - return _putenv_s(kLimitEnv, value); -# else - return setenv(kLimitEnv, value, /*overwrite=*/1); -# endif -} - -TEST(MetadataCacheDeathTest, InitializesGlobalLimitFromEnvironment) { - // Start with a new registry for every environment setting. - GTEST_FLAG_SET(death_test_style, "threadsafe"); - for (const auto& [value, expected_limit] : - {std::pair{"2", 2}, std::pair{"0", 8}, std::pair{"-1", 8}, std::pair{"2x", 8}, - std::pair{"2147483648", 8}}) { - SCOPED_TRACE(value); - EXPECT_EXIT( - { - if (SetManifestCacheLimitEnv(value) != 0) { - std::_Exit(EXIT_FAILURE); - } - const auto initial_limit = internal::ContentCache::SetGlobalCapacity(3); - const auto overridden_limit = internal::ContentCache::SetGlobalCapacity(4); - std::_Exit(initial_limit == expected_limit && overridden_limit == 3 - ? EXIT_SUCCESS - : EXIT_FAILURE); - }, - ::testing::ExitedWithCode(EXIT_SUCCESS), ""); - } -} -#endif +using internal::ContentCacheProperties; class ScopedGlobalCacheLimit { public: @@ -153,13 +119,12 @@ TEST(MetadataCacheTest, CachedInputFilePreservesReadAheadAndFallbackErrors) { } TEST(MetadataCacheTest, ParsesJavaCompatibleProperties) { - std::unordered_map properties = { - {std::string(ContentCacheOptions::kEnabled), "true"}, - {std::string(ContentCacheOptions::kMaxTotalBytes), "200"}, - {std::string(ContentCacheOptions::kMaxContentLength), "50"}, - }; + ContentCacheProperties properties; + properties.Set(ContentCacheProperties::kEnabled, true) + .Set(ContentCacheProperties::kMaxTotalBytes, int64_t{200}) + .Set(ContentCacheProperties::kMaxContentLength, int64_t{50}); - auto options = ContentCacheOptions::FromProperties(properties); + auto options = ContentCacheOptions::FromProperties(properties.configs()); ASSERT_THAT(options, IsOk()); EXPECT_TRUE(options->enabled); @@ -172,7 +137,7 @@ TEST(MetadataCacheTest, ParsesEnabledPropertyIgnoringCase) { SCOPED_TRACE(value); ICEBERG_UNWRAP_OR_FAIL(auto options, ContentCacheOptions::FromProperties( - {{std::string(ContentCacheOptions::kEnabled), value}})); + {{ContentCacheProperties::kEnabled.key(), value}})); EXPECT_TRUE(options.enabled); } } @@ -183,14 +148,14 @@ TEST(MetadataCacheTest, TreatsOtherEnabledPropertyValuesAsFalseLikeJava) { SCOPED_TRACE(value); ICEBERG_UNWRAP_OR_FAIL(auto options, ContentCacheOptions::FromProperties( - {{std::string(ContentCacheOptions::kEnabled), value}})); + {{ContentCacheProperties::kEnabled.key(), value}})); EXPECT_FALSE(options.enabled); } } TEST(MetadataCacheTest, UsesJavaCompatibleDefaults) { auto options = ContentCacheOptions::FromProperties( - {{std::string(ContentCacheOptions::kEnabled), "true"}}); + {{ContentCacheProperties::kEnabled.key(), "true"}}); ASSERT_THAT(options, IsOk()); EXPECT_TRUE(options->enabled); @@ -200,20 +165,71 @@ TEST(MetadataCacheTest, UsesJavaCompatibleDefaults) { TEST(MetadataCacheTest, InvalidNumericPropertyErrorIncludesPropertyName) { auto options = ContentCacheOptions::FromProperties( - {{std::string(ContentCacheOptions::kEnabled), "true"}, - {std::string(ContentCacheOptions::kMaxTotalBytes), "invalid"}}); + {{ContentCacheProperties::kEnabled.key(), "true"}, + {ContentCacheProperties::kMaxTotalBytes.key(), "invalid"}}); - EXPECT_THAT(options, HasErrorMessage(std::string(ContentCacheOptions::kMaxTotalBytes))); + EXPECT_THAT(options, HasErrorMessage(ContentCacheProperties::kMaxTotalBytes.key())); } TEST(MetadataCacheTest, IgnoresInvalidTuningPropertiesWhenDisabled) { ICEBERG_UNWRAP_OR_FAIL( auto options, ContentCacheOptions::FromProperties( - {{std::string(ContentCacheOptions::kEnabled), "false"}, - {std::string(ContentCacheOptions::kMaxTotalBytes), "invalid"}})); + {{ContentCacheProperties::kEnabled.key(), "false"}, + {ContentCacheProperties::kMaxTotalBytes.key(), "invalid"}})); EXPECT_FALSE(options.enabled); } +TEST(MetadataCacheTest, AdapterRejectsDeletes) { + auto storage = std::make_shared(); + auto cache = std::make_shared(ContentCacheOptions{}); + auto adapter = cache->Wrap(storage); + EXPECT_CALL(*storage, DeleteFile).Times(0); + EXPECT_THAT(adapter->DeleteFile("manifest.avro"), IsError(ErrorKind::kNotSupported)); + EXPECT_THAT(adapter->DeleteFiles({"manifest.avro"}), IsError(ErrorKind::kNotSupported)); + EXPECT_THAT(adapter->DeleteFiles({}), IsError(ErrorKind::kNotSupported)); +} + +class AliasedMockFileIO : public MockFileIO { + public: + Result> NewInputFile(std::string location) override { + if (location == "alias") location = "manifest.avro"; + return MockFileIO::NewInputFile(std::move(location)); + } +}; + +TEST(MetadataCacheTest, StorageDeleteThroughAliasInvalidatesCanonicalContent) { + for (bool recreate : {false, true}) { + SCOPED_TRACE(recreate); + auto storage = std::make_shared(); + storage->AddFile("manifest.avro", "old"); + auto cache = std::make_shared(ContentCacheOptions{}); + auto adapter = cache->Wrap(storage); + ICEBERG_UNWRAP_OR_FAIL(auto old_file, adapter->NewInputFile("alias")); + ASSERT_THAT(adapter->ReadFile("alias", std::nullopt), HasValue(::testing::Eq("old"))); + ASSERT_TRUE(cache->Find("manifest.avro")); + cache->Insert("unrelated", std::make_shared>(1), 0); + + EXPECT_CALL(*storage, DeleteFile("alias")).WillOnce([&] { + storage->RemoveFile("manifest.avro"); + return Status{}; + }); + ASSERT_THAT(storage->DeleteFile("alias"), IsOk()); + if (recreate) storage->AddFile("manifest.avro", "new"); + cache->Invalidate(*storage, "alias"); + + // An input handle obtained before deletion must not repopulate the cache. + ICEBERG_UNWRAP_OR_FAIL(auto old_stream, old_file->Open()); + ASSERT_THAT(old_stream->Close(), IsOk()); + auto read = adapter->ReadFile("manifest.avro", std::nullopt); + if (recreate) { + EXPECT_THAT(read, HasValue(::testing::Eq("new"))); + EXPECT_TRUE(cache->Find("unrelated")); + } else { + EXPECT_THAT(read, IsError(ErrorKind::kNotFound)); + } + } +} + TEST(MetadataCacheTest, PrefixReadDoesNotPopulateWholeFileCache) { auto storage = std::make_shared(); storage->AddFile("manifest.avro", "manifest"); @@ -282,7 +298,8 @@ TEST(MetadataCacheTest, InvalidatedLoadCannotPopulateRecreatedCacheState) { ASSERT_FALSE(cache->Find("manifest.avro", &generation)); auto other = std::make_shared(ContentCacheOptions{}); other->Insert("other.avro", std::make_shared>(8), 0); - cache->Invalidate("manifest.avro"); + MockFileIO storage; + cache->Invalidate(storage, "manifest.avro"); cache->Insert("manifest.avro", std::make_shared>(8), generation); EXPECT_FALSE(cache->Find("manifest.avro")); diff --git a/src/iceberg/test/mock_io.h b/src/iceberg/test/mock_io.h index 63abb6ea6d..45641563c0 100644 --- a/src/iceberg/test/mock_io.h +++ b/src/iceberg/test/mock_io.h @@ -84,6 +84,8 @@ class MockFileIO : public FileIO { std::as_bytes(std::span(data.data(), data.size()))); } + void RemoveFile(const std::string& file_location) { files_->erase(file_location); } + std::vector& FileData(const std::string& file_location) { return *GetOrCreateFile(file_location); } diff --git a/src/iceberg/util/cache_internal.h b/src/iceberg/util/cache_internal.h index 21397a19bf..2bd0df70e2 100644 --- a/src/iceberg/util/cache_internal.h +++ b/src/iceberg/util/cache_internal.h @@ -109,15 +109,17 @@ class LruCache { return &list_it->value; } + // Return whether a cached entry was removed. template - void Erase(const K& key) { + bool Erase(const K& key) { const auto it = map_.find(key); if (it == map_.end()) { - return; + return false; } total_weight_ -= it->second->weight; items_.erase(it->second); map_.erase(it); + return true; } // Insert or update the value for `key`, mark it as most recently used, and evict least diff --git a/src/iceberg/util/error_util_internal.h b/src/iceberg/util/error_util_internal.h index b6a4280e56..5295142658 100644 --- a/src/iceberg/util/error_util_internal.h +++ b/src/iceberg/util/error_util_internal.h @@ -34,7 +34,7 @@ inline Status FinishWithCloseStatus(Status operation_status, Status close_status } auto error = std::move(operation_status).error(); if (!close_status.has_value()) { - error.message += "; additionally failed to close stream: "; + error.message += "; close also failed: "; error.message += close_status.error().message; } return unexpected(std::move(error)); diff --git a/src/iceberg/util/property_util.cc b/src/iceberg/util/property_util.cc index 7fbd23d444..d385c64436 100644 --- a/src/iceberg/util/property_util.cc +++ b/src/iceberg/util/property_util.cc @@ -47,6 +47,19 @@ Status PropertyUtil::ValidateCommitProperties( return {}; } +Result PropertyUtil::PropertyAsInt64( + const std::unordered_map& properties, std::string_view key, + int64_t default_value) { + auto it = properties.find(std::string(key)); + if (it == properties.end()) return default_value; + auto result = StringUtils::ParseNumber(it->second); + if (!result) { + return InvalidArgument("Invalid numeric value '{}' for property '{}': {}", it->second, + key, result.error().message); + } + return result; +} + bool PropertyUtil::PropertyAsBoolean( const std::unordered_map& properties, std::string_view key, bool default_value) { diff --git a/src/iceberg/util/property_util.h b/src/iceberg/util/property_util.h index 72bbf9f047..83fd8de4c9 100644 --- a/src/iceberg/util/property_util.h +++ b/src/iceberg/util/property_util.h @@ -22,6 +22,7 @@ /// \file iceberg/util/property_util.h /// \brief Provide property conversion helpers. +#include #include #include #include @@ -37,6 +38,11 @@ class ICEBERG_EXPORT PropertyUtil { static Status ValidateCommitProperties( const std::unordered_map& properties); + /// \brief Read a signed 64-bit property, returning the default when absent. + static Result PropertyAsInt64( + const std::unordered_map& properties, + std::string_view key, int64_t default_value); + /// \brief Read a boolean property from a property map. /// /// Mirrors Java's PropertyUtil.propertyAsBoolean: the value is parsed with From dd0c9b9925e0baff9b4d8d1f8f2805e68f34de8c Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Wed, 25 Nov 2026 23:08:47 +0800 Subject: [PATCH 15/16] refactor(io): track manifest caches with weak references Co-authored-by: Codex --- src/iceberg/content_cache.cc | 104 ++++++++++++++---------- src/iceberg/content_cache_internal.h | 9 +- src/iceberg/test/metadata_cache_test.cc | 17 ++++ 3 files changed, 82 insertions(+), 48 deletions(-) diff --git a/src/iceberg/content_cache.cc b/src/iceberg/content_cache.cc index 00f122320a..71a904edef 100644 --- a/src/iceberg/content_cache.cc +++ b/src/iceberg/content_cache.cc @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "iceberg/content_cache_internal.h" @@ -37,59 +38,75 @@ struct MetadataContentWeigher { } }; -struct ContentCacheState { - explicit ContentCacheState(size_t max_total_bytes) - : lru(static_cast(max_total_bytes)) {} +using CachedFiles = LruCache; - LruCache - lru; +// Owned by the table's cache handle. Only the registry accesses these fields under +// its mutex. A position is valid while files is non-null. +struct ContentCacheRegistration { + using Entries = std::list>; + Entries::iterator position; + std::shared_ptr files; }; -// Owns cache states, never their per-table handles or storage FileIOs. Callers hold -// the handle's mutex before accessing a state; the registry mutex only protects the -// global LRU. Eviction releases content without calling back into a table handle. class ContentCacheRegistry { public: - explicit ContentCacheRegistry(int32_t capacity) : caches_(capacity) {} + explicit ContentCacheRegistry(int32_t capacity) : capacity_(capacity) {} - std::shared_ptr Find(const ContentCache* owner) { + // Pin the files for the calling operation. Their entries are protected by the + // per-table mutex, so accessing bytes never requires holding the registry lock. + std::shared_ptr Find(const ContentCacheRegistration& entry, + bool touch = true) { std::lock_guard lock(mutex_); - const auto* state = caches_.Find(owner); - return state ? *state : nullptr; + if (touch && entry.files) { + entries_.splice(entries_.begin(), entries_, entry.position); + } + return entry.files; } - std::shared_ptr GetOrCreate( - const ContentCache* owner, const ContentCacheOptions& options, - std::weak_ptr& weak_state) { + std::shared_ptr GetOrCreate( + const std::shared_ptr& entry, size_t max_total_bytes) { std::lock_guard lock(mutex_); - if (auto* state = caches_.Find(owner)) { - return *state; - } - auto state = weak_state.lock(); - if (!state) { - state = std::make_shared(options.max_total_bytes); + if (entry->files) { + entries_.splice(entries_.begin(), entries_, entry->position); + } else { + auto files = std::make_shared(static_cast(max_total_bytes)); + entry->position = entries_.emplace(entries_.begin(), entry); + entry->files = std::move(files); + TrimToCapacity(); } - caches_.Replace(owner, state); - weak_state = state; - return state; + return entry->files; } - void Erase(const ContentCache* owner) { + void Erase(ContentCacheRegistration& entry) { std::lock_guard lock(mutex_); - caches_.Erase(owner); + if (entry.files) { + entries_.erase(entry.position); + entry.files.reset(); + } } - int32_t SetCapacity(int32_t max_caches) { - ICEBERG_DCHECK(max_caches > 0, "Global manifest cache limit must be positive"); + int32_t SetCapacity(int32_t capacity) { + ICEBERG_DCHECK(capacity > 0, "Global manifest cache limit must be positive"); std::lock_guard lock(mutex_); - auto previous = static_cast(caches_.max_weight()); - caches_.Resize(max_caches); + const auto previous = std::exchange(capacity_, capacity); + TrimToCapacity(); return previous; } private: + void TrimToCapacity() { + while (std::cmp_greater(entries_.size(), capacity_)) { + if (auto entry = entries_.back().lock()) { + entry->files.reset(); + } + entries_.pop_back(); + } + } + std::mutex mutex_; - LruCache> caches_; + int32_t capacity_; + ContentCacheRegistration::Entries entries_; }; namespace { @@ -288,9 +305,11 @@ Result ContentCacheOptions::FromProperties( } ContentCache::ContentCache(ContentCacheOptions options) - : options_(options), registry_(GlobalContentCacheRegistry()) {} + : options_(options), + registry_(GlobalContentCacheRegistry()), + registration_(std::make_shared()) {} -ContentCache::~ContentCache() { registry_->Erase(this); } +ContentCache::~ContentCache() { registry_->Erase(*registration_); } int32_t ContentCache::SetGlobalCapacity(int32_t max_caches) { return GlobalContentCacheRegistry()->SetCapacity(max_caches); @@ -303,8 +322,8 @@ std::shared_ptr ContentCache::Wrap(std::shared_ptr io) { MetadataContent ContentCache::Find(std::string_view location, uint64_t* generation) { std::lock_guard lock(mutex_); if (generation) *generation = generation_; - auto state = registry_->Find(this); - const auto* content = state ? state->lru.Find(location) : nullptr; + auto files = registry_->Find(*registration_); + const auto* content = files ? files->Find(location) : nullptr; return content ? *content : nullptr; } @@ -317,16 +336,16 @@ void ContentCache::Insert(std::string_view location, MetadataContent content, } std::lock_guard lock(mutex_); if (generation == generation_) { - registry_->GetOrCreate(this, options_, state_) - ->lru.Replace(std::string(location), std::move(content)); + registry_->GetOrCreate(registration_, options_.max_total_bytes) + ->Replace(std::string(location), std::move(content)); } } void ContentCache::Invalidate(FileIO& storage, std::string_view location) { { std::lock_guard lock(mutex_); - auto state = state_.lock(); - if (!state || state->lru.size() == 0 || state->lru.Erase(location)) { + auto files = registry_->Find(*registration_, /*touch=*/false); + if (!files || files->size() == 0 || files->Erase(location)) { ++generation_; return; } @@ -352,15 +371,14 @@ void ContentCache::Invalidate(FileIO& storage, std::string_view location) { std::lock_guard lock(mutex_); ++generation_; - if (auto state = state_.lock()) { - state->lru.Erase(input->location()); + if (auto files = registry_->Find(*registration_, /*touch=*/false)) { + files->Erase(input->location()); } } void ContentCache::Clear() { std::lock_guard lock(mutex_); - registry_->Erase(this); - state_.reset(); + registry_->Erase(*registration_); ++generation_; } diff --git a/src/iceberg/content_cache_internal.h b/src/iceberg/content_cache_internal.h index 8a4d256b10..04d5328a5a 100644 --- a/src/iceberg/content_cache_internal.h +++ b/src/iceberg/content_cache_internal.h @@ -57,12 +57,11 @@ struct ICEBERG_EXPORT ContentCacheOptions { }; using MetadataContent = std::shared_ptr>; -struct ContentCacheState; +struct ContentCacheRegistration; class ContentCacheRegistry; -// A lightweight per-table handle with a weak reference to cached content. A global -// LRU registry retains at most eight cache states by default. Readers share the -// handle through a manifest-only FileIO adapter without pinning its cache state. +// The per-table handle owns its cached files. The process-wide registry tracks +// active caches through weak references and can evict their contents at capacity. class ICEBERG_EXPORT ContentCache : public std::enable_shared_from_this { public: explicit ContentCache(ContentCacheOptions options); @@ -86,7 +85,7 @@ class ICEBERG_EXPORT ContentCache : public std::enable_shared_from_this registry_; std::mutex mutex_; - std::weak_ptr state_; + const std::shared_ptr registration_; uint64_t generation_ = 0; }; diff --git a/src/iceberg/test/metadata_cache_test.cc b/src/iceberg/test/metadata_cache_test.cc index 6ea38504c2..99accd5955 100644 --- a/src/iceberg/test/metadata_cache_test.cc +++ b/src/iceberg/test/metadata_cache_test.cc @@ -291,6 +291,23 @@ TEST(MetadataCacheTest, DestroyingLastCacheHandleReleasesRetainedBytes) { EXPECT_TRUE(weak_content.expired()); } +TEST(MetadataCacheTest, DestroyedCacheFreesItsSlotWithoutEvictingALiveCache) { + ScopedGlobalCacheLimit limit(2); + auto first = std::make_shared(ContentCacheOptions{}); + first->Insert("first", std::make_shared>(1), 0); + std::weak_ptr weak_cache; + { + auto second = std::make_shared(ContentCacheOptions{}); + weak_cache = second; + second->Insert("second", std::make_shared>(1), 0); + } + ASSERT_TRUE(weak_cache.expired()); + auto third = std::make_shared(ContentCacheOptions{}); + third->Insert("third", std::make_shared>(1), 0); + EXPECT_TRUE(first->Find("first")); + EXPECT_TRUE(third->Find("third")); +} + TEST(MetadataCacheTest, InvalidatedLoadCannotPopulateRecreatedCacheState) { ScopedGlobalCacheLimit limit(1); auto cache = std::make_shared(ContentCacheOptions{}); From a4d92036a7de7330255f4427a697957f0cc23611 Mon Sep 17 00:00:00 2001 From: Manu Zhang Date: Wed, 25 Nov 2026 23:59:45 +0800 Subject: [PATCH 16/16] fix(io): invalidate cached aliases and release empty cache slots Co-authored-by: Codex --- src/iceberg/arrow/arrow_io.cc | 13 +++++- src/iceberg/content_cache.cc | 54 ++++++++++++++++--------- src/iceberg/content_cache_internal.h | 11 ++++- src/iceberg/test/arrow_io_test.cc | 46 +++++++++++++++++++++ src/iceberg/test/cache_test.cc | 14 +++++++ src/iceberg/test/metadata_cache_test.cc | 31 +++++++++++++- src/iceberg/test/mock_io.h | 4 +- src/iceberg/util/cache_internal.h | 15 +++++++ 8 files changed, 166 insertions(+), 22 deletions(-) diff --git a/src/iceberg/arrow/arrow_io.cc b/src/iceberg/arrow/arrow_io.cc index 998f8c5ce8..54968c419e 100644 --- a/src/iceberg/arrow/arrow_io.cc +++ b/src/iceberg/arrow/arrow_io.cc @@ -35,6 +35,7 @@ #include "iceberg/arrow/arrow_io_internal.h" #include "iceberg/arrow/arrow_io_util.h" #include "iceberg/arrow/arrow_status_internal.h" +#include "iceberg/content_cache_internal.h" #include "iceberg/util/macros.h" namespace iceberg::arrow { @@ -411,7 +412,7 @@ class ArrowPositionOutputStream : public PositionOutputStream { int64_t closed_position_ = 0; }; -class ArrowInputFile : public InputFile { +class ArrowInputFile : public iceberg::internal::InputFileWithStoragePath { public: ArrowInputFile(std::shared_ptr<::arrow::fs::FileSystem> fs, std::string location, std::string path, std::optional file_size) @@ -421,6 +422,16 @@ class ArrowInputFile : public InputFile { file_size_(file_size) {} std::string_view location() const override { return location_; } + std::optional 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; + // Ignore boundary slashes for invalidation, where extra matches are harmless. + std::string_view key(path_); + const auto first = key.find_first_not_of('/'); + if (first == std::string_view::npos) return std::nullopt; + return key.substr(first, key.find_last_not_of('/') - first + 1); + } Result Size() const override { if (file_size_.has_value()) { diff --git a/src/iceberg/content_cache.cc b/src/iceberg/content_cache.cc index 71a904edef..1b9dd838ea 100644 --- a/src/iceberg/content_cache.cc +++ b/src/iceberg/content_cache.cc @@ -32,14 +32,19 @@ namespace iceberg::internal { +struct CachedFile { + MetadataContent content; + std::optional storage_path; +}; + struct MetadataContentWeigher { - int64_t operator()(const std::string&, const MetadataContent& content) const { - return static_cast(content->size()); + int64_t operator()(const std::string&, const CachedFile& file) const { + return static_cast(file.content->size()); } }; -using CachedFiles = LruCache; +using CachedFiles = + LruCache; // Owned by the table's cache handle. Only the registry accesses these fields under // its mutex. A position is valid while files is non-null. @@ -111,6 +116,13 @@ class ContentCacheRegistry { namespace { +std::optional StoragePath(const InputFile& input) { + if (const auto* file = dynamic_cast(&input)) { + return file->storage_path(); + } + return std::nullopt; +} + std::shared_ptr GlobalContentCacheRegistry() { // Handles keep the registry alive even when destroyed during static teardown. static auto registry = std::make_shared(8); @@ -226,7 +238,7 @@ class CachedInputFile : public InputFile { } auto content = std::make_shared>(std::move(loaded).value()); - cache_->Insert(location_, content, generation_); + cache_->Insert(location_, content, generation_, StoragePath(*input_)); return std::make_unique(std::move(content)); } @@ -323,12 +335,13 @@ MetadataContent ContentCache::Find(std::string_view location, uint64_t* generati std::lock_guard lock(mutex_); if (generation) *generation = generation_; auto files = registry_->Find(*registration_); - const auto* content = files ? files->Find(location) : nullptr; - return content ? *content : nullptr; + const auto* file = files ? files->Find(location) : nullptr; + return file ? file->content : nullptr; } void ContentCache::Insert(std::string_view location, MetadataContent content, - uint64_t generation) { + uint64_t generation, + std::optional storage_path) { // Empty/corrupt metadata must not accumulate unbounded zero-weight entries. if (content->empty() || content->size() > options_.max_content_length || content->size() > options_.max_total_bytes) { @@ -337,7 +350,9 @@ void ContentCache::Insert(std::string_view location, MetadataContent content, std::lock_guard lock(mutex_); if (generation == generation_) { registry_->GetOrCreate(registration_, options_.max_total_bytes) - ->Replace(std::string(location), std::move(content)); + ->Replace(std::string(location), + CachedFile{.content = std::move(content), + .storage_path = std::optional(storage_path)}); } } @@ -345,26 +360,26 @@ void ContentCache::Invalidate(FileIO& storage, std::string_view location) { { std::lock_guard lock(mutex_); auto files = registry_->Find(*registration_, /*touch=*/false); - if (!files || files->size() == 0 || files->Erase(location)) { + if (!files) { ++generation_; return; } } - // A storage implementation may normalize this path. Resolve it outside the - // cache lock, then invalidate loads that overlapped the resolution. - std::unique_ptr input; + // A URI may have several cached spellings, even when the deleted spelling + // itself was cached. Ask storage for the path it actually uses. + std::optional path; try { - if (auto resolved = storage.NewInputFile(std::string(location))) { - input = std::move(*resolved); + if (auto input = storage.NewInputFile(std::string(location)); input && *input) { + path = StoragePath(**input); } } catch (...) { // Failure to resolve an alias must not turn a completed delete into a failure. Clear(); return; } - if (!input) { - // A store may no longer be able to resolve an alias after deletion. + if (!path) { + // The public InputFile location may simply echo the requested spelling. Clear(); return; } @@ -372,7 +387,10 @@ void ContentCache::Invalidate(FileIO& storage, std::string_view location) { std::lock_guard lock(mutex_); ++generation_; if (auto files = registry_->Find(*registration_, /*touch=*/false)) { - files->Erase(input->location()); + files->EraseIf([&](const CachedFile& file) { + return !file.storage_path || *file.storage_path == *path; + }); + if (files->size() == 0) registry_->Erase(*registration_); } } diff --git a/src/iceberg/content_cache_internal.h b/src/iceberg/content_cache_internal.h index 04d5328a5a..3bb1ffb585 100644 --- a/src/iceberg/content_cache_internal.h +++ b/src/iceberg/content_cache_internal.h @@ -57,6 +57,14 @@ struct ICEBERG_EXPORT ContentCacheOptions { }; using MetadataContent = std::shared_ptr>; + +// Storage handles can identify aliases for cache invalidation without changing +// the caller-visible location. The key must be the same for aliases of a file. +class ICEBERG_EXPORT InputFileWithStoragePath : public InputFile { + public: + virtual std::optional storage_path() const = 0; +}; + struct ContentCacheRegistration; class ContentCacheRegistry; @@ -76,7 +84,8 @@ class ICEBERG_EXPORT ContentCache : public std::enable_shared_from_this storage_path = std::nullopt); // Invalidate a deleted storage path, including its canonical cache key. void Invalidate(FileIO& storage, std::string_view location); void Clear(); diff --git a/src/iceberg/test/arrow_io_test.cc b/src/iceberg/test/arrow_io_test.cc index 7bc9ebba53..02b913c51f 100644 --- a/src/iceberg/test/arrow_io_test.cc +++ b/src/iceberg/test/arrow_io_test.cc @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -31,6 +32,7 @@ #include "iceberg/arrow/arrow_io_internal.h" #include "iceberg/arrow/arrow_register.h" +#include "iceberg/content_cache_internal.h" #include "iceberg/file_io_registry.h" #include "iceberg/resolving_file_io.h" #include "iceberg/test/matchers.h" @@ -368,6 +370,50 @@ TEST_F(LocalFileIOTest, DeleteFile) { EXPECT_THAT(del_res, HasErrorMessage("Cannot delete file")); } +TEST(ArrowFileIOTest, AliasDeleteInvalidatesEveryCachedSpelling) { + std::shared_ptr storage = arrow::ArrowFileSystemFileIO::MakeMockFileIO(); + const std::string canonical = "s3://manifest.avro"; + const std::string alias = "s3a://manifest.avro"; + const std::string unrelated = "s3://other.avro"; + for (bool warm_alias : {false, true}) { + SCOPED_TRACE(warm_alias); + auto cache = + std::make_shared(internal::ContentCacheOptions{}); + auto adapter = cache->Wrap(storage); + ASSERT_THAT(storage->WriteFile(canonical, "old"), IsOk()); + ASSERT_THAT(storage->WriteFile(unrelated, "keep"), IsOk()); + ASSERT_THAT(adapter->ReadFile(unrelated, std::nullopt), + HasValue(::testing::Eq("keep"))); + ASSERT_THAT(adapter->ReadFile(canonical, std::nullopt), + HasValue(::testing::Eq("old"))); + if (warm_alias) { + ASSERT_THAT(adapter->ReadFile(alias, std::nullopt), HasValue(::testing::Eq("old"))); + } + ASSERT_THAT(storage->DeleteFile(alias), IsOk()); + cache->Invalidate(*storage, alias); + EXPECT_TRUE(cache->Find(unrelated)); + ASSERT_THAT(storage->WriteFile(canonical, "new"), IsOk()); + EXPECT_THAT(adapter->ReadFile(canonical, std::nullopt), + HasValue(::testing::Eq("new"))); + EXPECT_THAT(adapter->ReadFile(alias, std::nullopt), HasValue(::testing::Eq("new"))); + } +} + +TEST_F(LocalFileIOTest, AliasDeleteClearsCachedLocalPaths) { + auto cache = std::make_shared(internal::ContentCacheOptions{}); + auto adapter = cache->Wrap(file_io_); + const std::filesystem::path path(temp_filepath_); + const auto alias = (path.parent_path() / "." / path.filename()).string(); + ASSERT_THAT(file_io_->WriteFile(temp_filepath_, "old"), IsOk()); + ASSERT_THAT(adapter->ReadFile(temp_filepath_, std::nullopt), + HasValue(::testing::Eq("old"))); + ASSERT_THAT(file_io_->DeleteFile(alias), IsOk()); + cache->Invalidate(*file_io_, alias); + ASSERT_THAT(file_io_->WriteFile(temp_filepath_, "new"), IsOk()); + EXPECT_THAT(adapter->ReadFile(temp_filepath_, std::nullopt), + HasValue(::testing::Eq("new"))); +} + TEST_F(LocalFileIOTest, DeleteFiles) { auto first_path = CreateNewTempFilePath(); auto second_path = CreateNewTempFilePath(); diff --git a/src/iceberg/test/cache_test.cc b/src/iceberg/test/cache_test.cc index dc72d5128a..199b691ce2 100644 --- a/src/iceberg/test/cache_test.cc +++ b/src/iceberg/test/cache_test.cc @@ -190,6 +190,20 @@ TEST(LruCacheTest, ThrowingWeigherPreservesEntriesWeightAndRecency) { EXPECT_EQ(cache.total_weight(), 4); } +TEST(LruCacheTest, EraseIfPreservesSurvivorWeightAndRecency) { + WeightedCache cache(8); + cache.Replace(1, "aa"); + cache.Replace(2, "x"); + cache.Replace(3, "ccc"); + cache.Replace(4, "x"); + cache.EraseIf([](const std::string& value) { return value == "x"; }); + EXPECT_EQ(cache.size(), 2); + EXPECT_EQ(cache.total_weight(), 5); + cache.Resize(3); + EXPECT_EQ(cache.Find(1), nullptr); + EXPECT_NE(cache.Find(3), nullptr); +} + TEST(LruCacheTest, ClearResetsWeight) { WeightedCache cache(10); cache.Replace(1, "aaaa"); diff --git a/src/iceberg/test/metadata_cache_test.cc b/src/iceberg/test/metadata_cache_test.cc index 99accd5955..8292ec123e 100644 --- a/src/iceberg/test/metadata_cache_test.cc +++ b/src/iceberg/test/metadata_cache_test.cc @@ -207,7 +207,8 @@ TEST(MetadataCacheTest, StorageDeleteThroughAliasInvalidatesCanonicalContent) { ICEBERG_UNWRAP_OR_FAIL(auto old_file, adapter->NewInputFile("alias")); ASSERT_THAT(adapter->ReadFile("alias", std::nullopt), HasValue(::testing::Eq("old"))); ASSERT_TRUE(cache->Find("manifest.avro")); - cache->Insert("unrelated", std::make_shared>(1), 0); + storage->AddFile("unrelated", "keep"); + ASSERT_THAT(adapter->ReadFile("unrelated", std::nullopt), IsOk()); EXPECT_CALL(*storage, DeleteFile("alias")).WillOnce([&] { storage->RemoveFile("manifest.avro"); @@ -308,6 +309,34 @@ TEST(MetadataCacheTest, DestroyedCacheFreesItsSlotWithoutEvictingALiveCache) { EXPECT_TRUE(third->Find("third")); } +TEST(MetadataCacheTest, InvalidatingLastFileFreesItsGlobalSlot) { + ScopedGlobalCacheLimit limit(2); + auto first = std::make_shared(ContentCacheOptions{}); + auto second = std::make_shared(ContentCacheOptions{}); + auto third = std::make_shared(ContentCacheOptions{}); + const auto bytes = std::make_shared>(1); + first->Insert("first", bytes, 0); + second->Insert("second", bytes, 0, "second"); + MockFileIO storage; + storage.AddFile("second", "replacement"); + second->Invalidate(storage, "second"); + third->Insert("third", bytes, 0); + EXPECT_TRUE(first->Find("first")); + EXPECT_FALSE(second->Find("second")); + EXPECT_TRUE(third->Find("third")); +} + +TEST(MetadataCacheTest, InvalidationClearsAliasesWhenStorageOnlyReportsTheRequest) { + FailingOpenFileIO storage(std::make_shared()); + auto cache = std::make_shared(ContentCacheOptions{}); + const auto bytes = std::make_shared>(1); + cache->Insert("manifest.avro", bytes, 0); + cache->Insert("alias", bytes, 0); + cache->Invalidate(storage, "manifest.avro"); + EXPECT_FALSE(cache->Find("manifest.avro")); + EXPECT_FALSE(cache->Find("alias")); +} + TEST(MetadataCacheTest, InvalidatedLoadCannotPopulateRecreatedCacheState) { ScopedGlobalCacheLimit limit(1); auto cache = std::make_shared(ContentCacheOptions{}); diff --git a/src/iceberg/test/mock_io.h b/src/iceberg/test/mock_io.h index 45641563c0..df99b04e4c 100644 --- a/src/iceberg/test/mock_io.h +++ b/src/iceberg/test/mock_io.h @@ -36,6 +36,7 @@ #include #include +#include "iceberg/content_cache_internal.h" #include "iceberg/file_io.h" #include "iceberg/storage_credential.h" #include "iceberg/util/macros.h" @@ -189,13 +190,14 @@ class MockFileIO : public FileIO { bool closed_ = false; }; - class InMemoryInputFile : public InputFile { + class InMemoryInputFile : public internal::InputFileWithStoragePath { public: InMemoryInputFile(std::string location, std::shared_ptr> data, std::optional length) : location_(std::move(location)), data_(std::move(data)), length_(length) {} std::string_view location() const override { return location_; } + std::optional storage_path() const override { return location_; } Result Size() const override { return length_.value_or(static_cast(data_->size())); diff --git a/src/iceberg/util/cache_internal.h b/src/iceberg/util/cache_internal.h index 2bd0df70e2..a4b2843a06 100644 --- a/src/iceberg/util/cache_internal.h +++ b/src/iceberg/util/cache_internal.h @@ -122,6 +122,21 @@ class LruCache { return true; } + // Remove matching entries without changing the eviction order of the survivors. + template + void EraseIf(Predicate matches) { + for (auto it = map_.begin(); it != map_.end();) { + const auto item = it->second; + if (matches(std::as_const(item->value))) { + total_weight_ -= item->weight; + items_.erase(item); + it = map_.erase(it); + } else { + ++it; + } + } + } + // Insert or update the value for `key`, mark it as most recently used, and evict least // recently used entries until the cache is within its maximum weight again. //