Repository navigation
fix(scan): allocate snapshot buffers from the scan memory pool #588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2fba071
dfefccc
bcf77e5
5e9d017
2bf362a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,14 +19,23 @@ | |
| #include "paimon/core/snapshot.h" | ||
|
|
||
| #include <cassert> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <new> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <utility> | ||
|
|
||
| #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> Snapshot::FromPath(const std::shared_ptr<FileSystem>& 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> Snapshot::FromPath(const std::shared_ptr<FileSystem>& fs, const std::string& path, | ||
| MemoryPool& pool) { | ||
| Bytes json; | ||
| { | ||
| PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<InputStream> 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<size_t>(length, "snapshot buffer size")); | ||
| // MemoryPool::Malloc throws on refusal. Report it through the snapshot Result. | ||
| try { | ||
| json = Bytes(static_cast<size_t>(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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, @realAaronWu In PR #584, your measurements showed schema JSON sizes of roughly 0.7–7.7 KB. Snapshot JSON appears even smaller—all 372 snapshots in this repository are under 1 KB—and its input buffer is short-lived. Propagating the memory pool consistently through all snapshot-loading paths would touch several components. Could you share the motivating workload, expected snapshot sizes, concurrency, and memory budgets? It would help us understand whether this addresses a measurable memory issue or a requirement to account for every allocation, and assess the implementation cost accordingly.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I measured the 372 numbered snapshot fixtures on the target base. The median is 572 B, p95 is 651 B, and the maximum is 716 B. The motivating case is one embedded database scan with a capped pool. In the controlled reproduction, padding the snapshot to 270,337 B with JSON whitespace let a 270,338 B input allocation bypass the 256 KiB pool on the old path. I don’t have production size or concurrency measurements, or evidence of meaningful RSS savings for ordinary snapshots. The case demonstrates an admission gap for this buffer. Would you prefer to keep this narrowly scoped to the data-scan path, or defer it to the broader planning controls? Reproduction
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The motivating workload is Sirius’s Paimon read integration. Our first target is a local append-only Parquet scan of an already-bound snapshot. Sirius supplies a capped pool to In the #587 reproduction, we ran one scan with a 256 KiB pool and padded a valid 459 B snapshot to 270,337 B with JSON whitespace. The allocation trace recorded a successful 270,338 B heap allocation in The proposed change routes only the numbered snapshot input buffer for data scans through the existing caller pool. The regression verifies refusal before reading, stream closure, and successful retry. This closes the specific gap we measured while keeping write, commit, and standalone metadata paths outside the change.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After internal discussion, our understanding is that both #588 and #592 aim to make memory usage during scan planning more tightly bounded. Although 256 KiB is a stress-test quota rather than a production requirement, manifest limits and accounting for JSON buffers alone would still leave many allocations outside the budget. Fully enforcing such a limit across the planning path could require intrusive changes and create a substantial maintenance burden. We suggest either exploring a scan-limit design aligned with Java Paimon to mitigate planning memory pressure, or maintaining downstream patches for your integration’s stricter requirements. Based on the workload evidence shared so far, it is not yet clear that precise memory-budget enforcement is a broadly applicable requirement that justifies the added code complexity and maintenance burden.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for discussing the scope and maintenance cost. We tested a Sirius-side alternative with unmodified Paimon C++. Sirius checks the numbered snapshot size, manifest references, and Zstandard decoded block bounds before entering planning. For our fixed local append-only fixture, healthy reads and retry returned the expected rows. The oversized snapshot, decode amplification, manifest fanout, and unknown decoded-size cases were refused before the SDK ran. We will carry this forward on the Sirius side and close this PR. Sirius validation draft |
||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -244,6 +244,8 @@ class Snapshot : public Jsonizable<Snapshot> { | |
|
|
||
| static Result<Snapshot> FromPath(const std::shared_ptr<FileSystem>& fs, | ||
| const std::string& path); | ||
| static Result<Snapshot> FromPath(const std::shared_ptr<FileSystem>& fs, const std::string& path, | ||
| MemoryPool& pool); | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We added default parameters in the official code, so other places can compile even if they don’t call the updated APIs—it's easy to miss necessary changes. However, if we remove the default parameters, we’d likely need to update most areas related to snapshots. @lucasfang What do you think?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the new default arguments. The existing |
||
| private: | ||
| // version of snapshot | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use
doc.Parse(json.data(), json.size())and rename the helper toFromJsonView()? This would remove the extra byte allocation, manual NUL terminator, andsize() - 1, while keeping the embedded-NUL validation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FromJsonView()now parses exactly the file length, with no extra byte or manual terminator. It uses a boundedMemoryStreambecause this RapidJSON version’sParse(data, size)consumes a leading UTF-8 BOM, which the original parser rejects. The embedded-NUL check remains.