diff --git a/src/paimon/common/utils/rapidjson_util.h b/src/paimon/common/utils/rapidjson_util.h index bac1b9ba0..77e72a3c6 100644 --- a/src/paimon/common/utils/rapidjson_util.h +++ b/src/paimon/common/utils/rapidjson_util.h @@ -34,6 +34,7 @@ #include "rapidjson/allocators.h" #include "rapidjson/document.h" #include "rapidjson/encodings.h" +#include "rapidjson/memorystream.h" #include "rapidjson/prettywriter.h" #include "rapidjson/rapidjson.h" #include "rapidjson/stringbuffer.h" @@ -89,18 +90,34 @@ class RapidJsonUtil { if (!FromJson(json_str, &doc)) { return Status::Invalid("deserialize failed: ", json_str); } - try { - obj->FromJson(doc); - } catch (const std::invalid_argument& e) { - return Status::Invalid("deserialize failed, possibly type incompatible: ", - e.what()); - } catch (...) { - return Status::Invalid("deserialize failed, reason unknown: ", json_str); - } + return DeserializeObject(doc, json_str, obj); } return Status::OK(); } + // T must have FromJson(). The view need not be NUL-terminated. + template + static Status FromJsonView(std::string_view json, std::string_view diagnostic, T* obj) { + if (!obj) { + return Status::Invalid("deserialize failed: obj is nullptr"); + } + if (json.empty()) { + return Status::Invalid("deserialize failed: ", diagnostic); + } + if (json.find('\0') != std::string_view::npos) { + return Status::Invalid("deserialize failed: JSON contains an unescaped NUL byte"); + } + // Parse(data, size) uses an encoded stream that consumes a leading BOM. + // Keep the same JSON syntax as FromJsonString's UTF-8 string stream. + rapidjson::MemoryStream stream(json.data(), json.size()); + rapidjson::Document doc; + doc.ParseStream(stream); + if (doc.HasParseError()) { + return Status::Invalid("deserialize failed: ", diagnostic); + } + return DeserializeObject(doc, diagnostic, obj); + } + // if T is std::nullopt, will use rapid_json null_value template static rapidjson::Value SerializeValue(const T& obj, @@ -126,6 +143,19 @@ class RapidJsonUtil { static Status CheckNoDuplicateMembers(const rapidjson::Value& value); private: + template + static Status DeserializeObject(const rapidjson::Document& doc, std::string_view diagnostic, + T* obj) { + try { + obj->FromJson(doc); + } catch (const std::invalid_argument& e) { + return Status::Invalid("deserialize failed, possibly type incompatible: ", e.what()); + } catch (...) { + return Status::Invalid("deserialize failed, reason unknown: ", diagnostic); + } + return Status::OK(); + } + static inline bool ToJson(const rapidjson::Value& value, std::string* json_str) { assert(json_str); rapidjson::StringBuffer buffer; diff --git a/src/paimon/common/utils/rapidjson_util_test.cpp b/src/paimon/common/utils/rapidjson_util_test.cpp index 2a2774392..e89e7337a 100644 --- a/src/paimon/common/utils/rapidjson_util_test.cpp +++ b/src/paimon/common/utils/rapidjson_util_test.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "gtest/gtest.h" @@ -152,6 +153,25 @@ TEST(RapidJsonUtilTest, TestRejectUnescapedNul) { } } +TEST(RapidJsonUtilTest, TestFromJsonViewUsesExactLength) { + struct SnapshotField { + void FromJson(const rapidjson::Value& doc) { + id = RapidJsonUtil::DeserializeKeyValue(doc, "id"); + } + int64_t id = 0; + } value; + constexpr std::string_view json = R"({"id":17}trailing)"; + ASSERT_OK(RapidJsonUtil::FromJsonView(json.substr(0, 9), "snapshot", &value)); + EXPECT_EQ(17, value.id); + ASSERT_NOK_WITH_MSG(RapidJsonUtil::FromJsonView(json, "snapshot", &value), "snapshot"); + + const std::string bom_json = + "\xEF\xBB\xBF" + R"({"id":17})"; + ASSERT_NOK_WITH_MSG(RapidJsonUtil::FromJsonString(bom_json, &value), "deserialize failed"); + ASSERT_NOK_WITH_MSG(RapidJsonUtil::FromJsonView(bom_json, "snapshot", &value), "snapshot"); +} + TEST(RapidJsonUtilTest, TestSerializeStringWithEmbeddedNul) { rapidjson::Document doc; const std::string text("name\0secret", 11); diff --git a/src/paimon/core/snapshot.cpp b/src/paimon/core/snapshot.cpp index 6b27c8fa1..87d56bbd1 100644 --- a/src/paimon/core/snapshot.cpp +++ b/src/paimon/core/snapshot.cpp @@ -19,14 +19,23 @@ #include "paimon/core/snapshot.h" #include +#include +#include +#include #include #include +#include #include +#include "fmt/format.h" +#include "paimon/common/utils/math.h" #include "paimon/common/utils/rapidjson_util.h" +#include "paimon/common/utils/scope_guard.h" #include "paimon/common/utils/uuid.h" #include "paimon/fs/file_system.h" #include "paimon/macros.h" +#include "paimon/memory/bytes.h" +#include "paimon/memory/memory_pool.h" #include "paimon/result.h" #include "paimon/status.h" #include "rapidjson/allocators.h" @@ -327,7 +336,40 @@ Result Snapshot::FromPath(const std::shared_ptr& fs, const std::string& path) { std::string json_str; PAIMON_RETURN_NOT_OK(fs->ReadFile(path, &json_str)); - PAIMON_ASSIGN_OR_RAISE(Snapshot snapshot, Snapshot::FromJsonString(json_str)); + return Snapshot::FromJsonString(json_str); +} + +Result Snapshot::FromPath(const std::shared_ptr& fs, const std::string& path, + MemoryPool& pool) { + Bytes json; + { + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr input, fs->Open(path)); + if (input == nullptr) { + return Status::Invalid("input stream is not suppose to be a null pointer, path: ", + path); + } + ScopeGuard close([&input]() { (void)input->Close(); }); + PAIMON_ASSIGN_OR_RAISE(int64_t length, input->Length()); + PAIMON_RETURN_NOT_OK(ValidateValueNonNegative(length, "read length")); + if (length == 0) { + return Status::Invalid("empty snapshot: ", path); + } + PAIMON_RETURN_NOT_OK(ValidateValueInRange(length, "snapshot buffer size")); + // MemoryPool::Malloc throws on refusal. Report it through the snapshot Result. + try { + json = Bytes(static_cast(length), &pool); + } catch (const std::bad_alloc&) { + return Status::OutOfMemory("cannot allocate ", length, " bytes for snapshot ", path); + } + PAIMON_ASSIGN_OR_RAISE(int64_t read_length, input->Read(json.data(), length)); + if (read_length != length) { + return Status::IOError(fmt::format("path {}, expect read len {}, actual read len {}", + path, length, read_length)); + } + } + Snapshot snapshot; + PAIMON_RETURN_NOT_OK( + RapidJsonUtil::FromJsonView(std::string_view(json.data(), json.size()), path, &snapshot)); return snapshot; } diff --git a/src/paimon/core/snapshot.h b/src/paimon/core/snapshot.h index 31adc6b13..91191f35c 100644 --- a/src/paimon/core/snapshot.h +++ b/src/paimon/core/snapshot.h @@ -244,6 +244,8 @@ class Snapshot : public Jsonizable { static Result FromPath(const std::shared_ptr& fs, const std::string& path); + static Result FromPath(const std::shared_ptr& fs, const std::string& path, + MemoryPool& pool); private: // version of snapshot diff --git a/src/paimon/core/table/append_only_file_store_table.cpp b/src/paimon/core/table/append_only_file_store_table.cpp index b8e3906f6..5da959a36 100644 --- a/src/paimon/core/table/append_only_file_store_table.cpp +++ b/src/paimon/core/table/append_only_file_store_table.cpp @@ -158,16 +158,17 @@ Result> AppendOnlyFileStoreTable::NewFileStoreSca const ScanContext* context) const { // TODO(liancheng.lsz): support fallback branch in scan std::shared_ptr table_schema = checked_pointer_cast(schema_); + const auto snapshot_manager = GetSnapshotManagerForScan(memory_pool); std::unique_ptr scan; if (core_options.DataEvolutionEnabled()) { PAIMON_ASSIGN_OR_RAISE( - scan, DataEvolutionFileStoreScan::Create(GetSnapshotManager(), GetSchemaManager(), + scan, DataEvolutionFileStoreScan::Create(snapshot_manager, GetSchemaManager(), manifest_list, manifest_file, table_schema, arrow_schema, context->GetScanFilters(), core_options, executor, memory_pool)); } else { PAIMON_ASSIGN_OR_RAISE( - scan, AppendOnlyFileStoreScan::Create(GetSnapshotManager(), GetSchemaManager(), + scan, AppendOnlyFileStoreScan::Create(snapshot_manager, GetSchemaManager(), manifest_list, manifest_file, table_schema, arrow_schema, context->GetScanFilters(), core_options, executor, memory_pool)); diff --git a/src/paimon/core/table/file_store_table.h b/src/paimon/core/table/file_store_table.h index e9e34adf6..5e13ba80f 100644 --- a/src/paimon/core/table/file_store_table.h +++ b/src/paimon/core/table/file_store_table.h @@ -80,6 +80,11 @@ class FileStoreTable : public Table { return std::make_shared(file_system_, root_path_, CurrentBranch()); } + std::shared_ptr GetSnapshotManagerForScan( + const std::shared_ptr& pool) const { + return std::make_shared(file_system_, root_path_, CurrentBranch(), pool); + } + std::string Name() const override { return identifier_.GetObjectName(); } diff --git a/src/paimon/core/table/primary_key_file_store_table.cpp b/src/paimon/core/table/primary_key_file_store_table.cpp index 54b31e585..b3dc72bd3 100644 --- a/src/paimon/core/table/primary_key_file_store_table.cpp +++ b/src/paimon/core/table/primary_key_file_store_table.cpp @@ -233,10 +233,10 @@ Result> PrimaryKeyFileStoreTable::NewFileStoreSca std::shared_ptr table_schema = checked_pointer_cast(schema_); std::unique_ptr scan; PAIMON_ASSIGN_OR_RAISE( - scan, KeyValueFileStoreScan::Create(GetSnapshotManager(), GetSchemaManager(), manifest_list, - manifest_file, table_schema, arrow_schema, - context->GetScanFilters(), core_options, executor, - memory_pool)); + scan, KeyValueFileStoreScan::Create(GetSnapshotManagerForScan(memory_pool), + GetSchemaManager(), manifest_list, manifest_file, + table_schema, arrow_schema, context->GetScanFilters(), + core_options, executor, memory_pool)); return WithTablePath(std::move(scan), root_path_); } diff --git a/src/paimon/core/table/source/scan_exception_safety_test.cpp b/src/paimon/core/table/source/scan_exception_safety_test.cpp index 246ab10f7..c08df9825 100644 --- a/src/paimon/core/table/source/scan_exception_safety_test.cpp +++ b/src/paimon/core/table/source/scan_exception_safety_test.cpp @@ -16,6 +16,7 @@ * limitations under the License. */ +#include #include #include #include @@ -35,9 +36,11 @@ #include "paimon/common/utils/arrow/status_utils.h" #include "paimon/executor.h" #include "paimon/fs/local/local_file_system.h" +#include "paimon/memory/bytes.h" #include "paimon/memory/memory_pool.h" #include "paimon/scan_context.h" #include "paimon/table/source/table_scan.h" +#include "paimon/testing/utils/counting_reader_file_system.h" #include "paimon/testing/utils/testharness.h" namespace paimon::test { @@ -55,15 +58,18 @@ class InlinePlanningExecutor final : public Executor { class PlanningFaultPool final : public MemoryPool { public: + explicit PlanningFaultPool(uint64_t capacity = std::numeric_limits::max()) + : capacity_(capacity) {} + void* Malloc(uint64_t size, uint64_t alignment) override { std::lock_guard lock(mutex_); - Check(); + Check(size); ++malloc_calls; return upstream_->Malloc(size, alignment); } void* Realloc(void* pointer, size_t old_size, size_t new_size, uint64_t alignment) override { std::lock_guard lock(mutex_); - Check(); + Check(new_size - std::min(old_size, new_size)); ++realloc_calls; return upstream_->Realloc(pointer, old_size, new_size, alignment); } @@ -86,14 +92,20 @@ class PlanningFaultPool final : public MemoryPool { std::atomic malloc_calls{0}; std::atomic realloc_calls{0}; std::atomic rejected{false}; + std::atomic refused_bytes{0}; private: - void Check() { + void Check(uint64_t additional_bytes) { if (calls.fetch_add(1) == fail_at.load()) { rejected = true; throw std::bad_alloc(); } + if (additional_bytes > capacity_ - upstream_->CurrentUsage()) { + refused_bytes = additional_bytes; + throw std::bad_alloc(); + } } + const uint64_t capacity_; std::unique_ptr upstream_ = GetMemoryPool(); std::mutex mutex_; }; @@ -108,6 +120,7 @@ struct PlanningInput { std::shared_ptr cache; std::optional bucket; std::shared_ptr file_system; + std::optional scan_mode; }; Result> OrdinaryScan( @@ -118,8 +131,10 @@ Result> OrdinaryScan( std::string schema{std::istreambuf_iterator(schema_file), std::istreambuf_iterator()}; ScanContextBuilder builder(input.path); - builder.SetTableSchema(schema).WithMemoryPool(pool).WithExecutor(executor).AddOption( - "scan.snapshot-id", input.snapshot); + builder.SetTableSchema(schema).WithExecutor(executor).AddOption("scan.snapshot-id", + input.snapshot); + if (pool) builder.WithMemoryPool(pool); + if (input.scan_mode) builder.AddOption("scan.mode", *input.scan_mode); if (input.cache) { builder.WithCache(input.cache) .AddOption("scan.manifest-entry-cache.max-snapshots", "8") @@ -291,6 +306,106 @@ TEST(ScanExceptionSafetyTest, RetainedSplitRemainsUsableWithCallerOwnedPool) { EXPECT_EQ(pool->CurrentUsage(), 0); } +TEST(ScanExceptionSafetyTest, SnapshotBufferUsesScanPoolBeforeReading) { + // Leave headroom for the fixture's manifest readers on the successful path. + constexpr size_t kPoolCapacity = 8 * 1024 * 1024; + auto temp_dir = UniqueTestDirectory::Create(); + ASSERT_NE(temp_dir, nullptr); + PlanningInput input; + input.path = temp_dir->Str(); + ASSERT_TRUE(TestUtil::CopyDirectory(PlanningFixture(), input.path)); + const std::string snapshot_path = input.path + "/snapshot/snapshot-1"; + auto fs = std::make_shared("/snapshot/snapshot-1"); + input.file_system = fs; + input.scan_mode = "from-snapshot-full"; + std::string snapshot_json; + ASSERT_OK(fs->ReadFile(snapshot_path, &snapshot_json)); + auto pool = std::make_shared(kPoolCapacity); + auto executor = std::make_shared(); + + std::string oversized_snapshot = snapshot_json; + // The file fits in an empty pool, but other query memory has already been admitted. + oversized_snapshot.resize(kPoolCapacity / 2 + 1, ' '); + ASSERT_OK(fs->WriteFile(snapshot_path, oversized_snapshot, /*overwrite=*/true)); + { + Bytes existing_buffer(kPoolCapacity / 2, pool.get()); + ASSERT_OK_AND_ASSIGN(auto scan, OrdinaryScan(pool, executor, input)); + const auto usage_before = pool->CurrentUsage(); + const auto opens_before = fs->counts->opens.load(); + const auto closes_before = fs->counts->closes.load(); + const auto reads_before = fs->counts->reads.load(); + auto plan = scan->CreatePlan(); + EXPECT_FALSE(plan.ok()); + EXPECT_TRUE(plan.status().IsOutOfMemory()) << plan.status(); + EXPECT_EQ(pool->refused_bytes.load(), oversized_snapshot.size()); + EXPECT_LE(pool->MaxMemoryUsage(), kPoolCapacity); + EXPECT_EQ(fs->counts->opens.load(), opens_before + 1); + EXPECT_EQ(fs->counts->closes.load(), closes_before + 1); + EXPECT_EQ(fs->counts->reads.load(), reads_before); + EXPECT_EQ(fs->counts->live.load(), 0); + EXPECT_EQ(pool->CurrentUsage(), usage_before); + } + EXPECT_EQ(pool->CurrentUsage(), 0); + + // An empty file has no input bytes to charge and is invalid JSON. + ASSERT_OK(fs->WriteFile(snapshot_path, "", /*overwrite=*/true)); + { + ASSERT_OK_AND_ASSIGN(auto scan, OrdinaryScan(pool, executor, input)); + const auto usage_before = pool->CurrentUsage(); + auto plan = scan->CreatePlan(); + EXPECT_TRUE(plan.status().IsInvalid()) << plan.status(); + EXPECT_EQ(fs->counts->live.load(), 0); + EXPECT_EQ(pool->CurrentUsage(), usage_before); + } + EXPECT_EQ(pool->CurrentUsage(), 0); + + ASSERT_OK(fs->WriteFile(snapshot_path, snapshot_json + '\0', /*overwrite=*/true)); + { + ASSERT_OK_AND_ASSIGN(auto scan, OrdinaryScan(pool, executor, input)); + const auto usage_before = pool->CurrentUsage(); + auto plan = scan->CreatePlan(); + EXPECT_TRUE(plan.status().IsInvalid()) << plan.status(); + EXPECT_EQ(fs->counts->live.load(), 0); + EXPECT_EQ(pool->CurrentUsage(), usage_before); + } + EXPECT_EQ(pool->CurrentUsage(), 0); + + ASSERT_OK(fs->WriteFile(snapshot_path, snapshot_json, /*overwrite=*/true)); + fs->sequential_read_limit = static_cast(snapshot_json.size()) - 1; + { + ASSERT_OK_AND_ASSIGN(auto scan, OrdinaryScan(pool, executor, input)); + const auto usage_before = pool->CurrentUsage(); + const auto closes_before = fs->counts->closes.load(); + auto plan = scan->CreatePlan(); + EXPECT_TRUE(plan.status().IsIOError()) << plan.status(); + ASSERT_NOK_WITH_MSG(plan.status(), "expect read len"); + EXPECT_EQ(fs->counts->closes.load(), closes_before + 1); + EXPECT_EQ(fs->counts->live.load(), 0); + EXPECT_EQ(pool->CurrentUsage(), usage_before); + } + EXPECT_EQ(pool->CurrentUsage(), 0); + + fs->sequential_read_limit.reset(); + const auto reads_before = fs->counts->reads.load(); + const auto read_file_calls_before = fs->counts->read_file_calls.load(); + { + ASSERT_OK_AND_ASSIGN(auto scan, OrdinaryScan(pool, executor, input)); + ASSERT_OK_AND_ASSIGN(auto plan, scan->CreatePlan()); + EXPECT_EQ(plan->SnapshotId(), 1); + EXPECT_FALSE(plan->Splits().empty()); + } + EXPECT_EQ(fs->counts->reads.load(), reads_before + 1); + EXPECT_EQ(fs->counts->read_file_calls.load(), read_file_calls_before); + EXPECT_EQ(fs->counts->live.load(), 0); + EXPECT_EQ(pool->CurrentUsage(), 0); + + ASSERT_OK_AND_ASSIGN(auto default_scan, OrdinaryScan(nullptr, executor, input)); + ASSERT_OK_AND_ASSIGN(auto default_plan, default_scan->CreatePlan()); + EXPECT_EQ(default_plan->SnapshotId(), 1); + EXPECT_FALSE(default_plan->Splits().empty()); + EXPECT_EQ(fs->counts->read_file_calls.load(), read_file_calls_before + 1); +} + TEST(ScanExceptionSafetyTest, RefusedArrowReallocationPreservesExistingBuffer) { auto pool = std::make_shared(); auto arrow_pool = GetArrowPool(pool); diff --git a/src/paimon/core/utils/snapshot_manager.cpp b/src/paimon/core/utils/snapshot_manager.cpp index 3e6c85eaa..f0ce484f5 100644 --- a/src/paimon/core/utils/snapshot_manager.cpp +++ b/src/paimon/core/utils/snapshot_manager.cpp @@ -31,6 +31,7 @@ #include "paimon/core/utils/branch_manager.h" #include "paimon/core/utils/file_utils.h" #include "paimon/fs/file_system.h" +#include "paimon/memory/memory_pool.h" #include "paimon/result.h" namespace paimon { @@ -43,6 +44,15 @@ SnapshotManager::SnapshotManager(const std::shared_ptr& fs, const std::string& root_path, const std::string& branch) : fs_(fs), root_path_(root_path), branch_(BranchManager::NormalizeBranch(branch)) {} +SnapshotManager::SnapshotManager(const std::shared_ptr& fs, + const std::string& root_path, const std::string& branch, + const std::shared_ptr& pool) + : SnapshotManager(fs, root_path, branch) { + if (pool && pool != GetDefaultPool()) { + pool_ = pool; + } +} + SnapshotManager::~SnapshotManager() = default; const std::shared_ptr& SnapshotManager::Fs() const { @@ -124,7 +134,8 @@ bool SnapshotManager::ExpiredSinceBoundaryWasRead(int64_t id) const { } Result SnapshotManager::LoadSnapshot(int64_t snapshot_id) const { - return Snapshot::FromPath(fs_, SnapshotPath(snapshot_id)); + const std::string path = SnapshotPath(snapshot_id); + return pool_ ? Snapshot::FromPath(fs_, path, *pool_) : Snapshot::FromPath(fs_, path); } void SnapshotManager::SetSnapshotLoader(SnapshotLoader loader) { diff --git a/src/paimon/core/utils/snapshot_manager.h b/src/paimon/core/utils/snapshot_manager.h index 5b346fe91..9a7358888 100644 --- a/src/paimon/core/utils/snapshot_manager.h +++ b/src/paimon/core/utils/snapshot_manager.h @@ -49,6 +49,8 @@ class SnapshotManager { SnapshotManager(const std::shared_ptr& fs, const std::string& root_path); SnapshotManager(const std::shared_ptr& fs, const std::string& root_path, const std::string& branch); + SnapshotManager(const std::shared_ptr& fs, const std::string& root_path, + const std::string& branch, const std::shared_ptr& pool); ~SnapshotManager(); /// Sets the loader for `LatestSnapshot()` and `LatestSnapshotId()`. @@ -126,6 +128,7 @@ class SnapshotManager { std::string root_path_; std::string branch_; SnapshotLoader snapshot_loader_; + std::shared_ptr pool_; }; } // namespace paimon diff --git a/src/paimon/testing/utils/counting_reader_file_system.h b/src/paimon/testing/utils/counting_reader_file_system.h index e93daae76..1dbd1dd83 100644 --- a/src/paimon/testing/utils/counting_reader_file_system.h +++ b/src/paimon/testing/utils/counting_reader_file_system.h @@ -19,10 +19,12 @@ #pragma once +#include #include #include #include #include +#include #include #include @@ -35,16 +37,25 @@ class CountingReaderFileSystem : public LocalFileSystem { public: struct Counts { std::atomic opens = 0; + std::atomic closes = 0; std::atomic live = 0; std::atomic peak = 0; std::atomic reads = 0; std::atomic bytes = 0; + std::atomic read_file_calls = 0; }; explicit CountingReaderFileSystem(std::string suffix) : suffix_(std::move(suffix)) {} using LocalFileSystem::Open; + Status ReadFile(const std::string& path, std::string* content) override { + if (StringUtils::EndsWith(path, suffix_)) { + counts->read_file_calls++; + } + return LocalFileSystem::ReadFile(path, content); + } + Result> Open(const std::string& path) const override { if (!StringUtils::EndsWith(path, suffix_)) { return LocalFileSystem::Open(path); @@ -54,17 +65,21 @@ class CountingReaderFileSystem : public LocalFileSystem { PAIMON_RETURN_NOT_OK(before_open(path)); } PAIMON_ASSIGN_OR_RAISE(std::unique_ptr input, LocalFileSystem::Open(path)); - return std::make_unique(std::move(input), counts); + return std::make_unique(std::move(input), counts, sequential_read_limit); } std::shared_ptr counts = std::make_shared(); std::function before_open; + std::optional sequential_read_limit; private: class CountedInput : public InputStream { public: - CountedInput(std::unique_ptr input, std::shared_ptr counts) - : input_(std::move(input)), counts_(std::move(counts)) { + CountedInput(std::unique_ptr input, std::shared_ptr counts, + std::optional sequential_read_limit) + : input_(std::move(input)), + counts_(std::move(counts)), + sequential_read_limit_(sequential_read_limit) { const uint64_t live = ++counts_->live; uint64_t peak = counts_->peak.load(); while (peak < live && !counts_->peak.compare_exchange_weak(peak, live)) { @@ -74,6 +89,7 @@ class CountingReaderFileSystem : public LocalFileSystem { counts_->live--; } Status Close() override { + counts_->closes++; return input_->Close(); } Status Seek(int64_t offset, SeekOrigin origin) override { @@ -84,6 +100,9 @@ class CountingReaderFileSystem : public LocalFileSystem { } Result Read(char* buffer, int64_t size) override { counts_->reads++; + if (sequential_read_limit_) { + size = std::min(size, *sequential_read_limit_); + } PAIMON_ASSIGN_OR_RAISE(int64_t bytes, input_->Read(buffer, size)); counts_->bytes += bytes; return bytes; @@ -116,6 +135,7 @@ class CountingReaderFileSystem : public LocalFileSystem { private: std::unique_ptr input_; std::shared_ptr counts_; + std::optional sequential_read_limit_; }; std::string suffix_;