Repository navigation
fix(scan): allocate snapshot buffers from the scan memory pool - #588
realAaronWu wants to merge 5 commits into
Conversation
Generated-by: OpenAI Codex (GPT-6)
Generated-by: OpenAI Codex (GPT-6)
Generated-by: OpenAI Codex (GPT-6)
|
@lxy-9602 Could you approve the GitHub Actions workflows for the revision that addresses your review? All 33 configured Linux CPU CTest targets passed on |
| std::string branch_; | ||
| SnapshotLoader snapshot_loader_; | ||
| std::shared_ptr<MemoryPool> pool_; | ||
| }; |
There was a problem hiding this comment.
I think if we’re going to overhaul this part thoroughly, the SchemaManager likely has similar issues too. Is there any plan to adjust it as well? Also, do the various operations—read/write/commit/scan—follow the same logic? Will these be adjusted together as part of the future plan?
There was a problem hiding this comment.
SchemaManager also reads JSON through ReadFile(), and we’re considering whether historical schema loading needs a follow-up. The first Sirius read profile scans a bound snapshot on a local append-only Parquet table. It doesn’t push data predicates into native planning, so it doesn’t reach the historical schema lookup used for file-statistics pruning.
For this PR, the demonstrated bypass is the numbered snapshot input buffer in the shared data-scan path. We’ll revisit historical schema accounting when a supported read profile reaches that lookup and we can reproduce a budget bypass through the public scan entry point. Broader planning budgets are tracked in #435.
| const std::string& path); | ||
| static Result<Snapshot> FromPath(const std::shared_ptr<FileSystem>& fs, const std::string& path, | ||
| MemoryPool* pool = nullptr); | ||
|
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Removed the new default arguments. The existing Snapshot::FromPath(fs, path) and manager constructors keep their original behavior. The pooled overload requires a MemoryPool&, and the data-scan factory requires an explicit pool.
| json = Bytes(static_cast<size_t>(allocation_size), pool); | ||
| } catch (const std::bad_alloc&) { | ||
| return Status::OutOfMemory("cannot allocate ", allocation_size, " bytes for snapshot ", | ||
| path); |
There was a problem hiding this comment.
Could you clarify why we need the additional try-catch here? If we follow this pattern, it seems like we might end up wrapping the entire end-to-end flow with try-catch for any memory allocation, which may be overkill—so I’m wondering about the rationale.
There was a problem hiding this comment.
MemoryPool::Malloc() throws std::bad_alloc when it refuses the request. This catch wraps only the snapshot buffer allocation so CreatePlan() can return OutOfMemory before reading the payload. Reads and parsing keep their existing error handling.
| } | ||
| json.data()[length] = '\0'; | ||
| } | ||
| Snapshot snapshot; |
There was a problem hiding this comment.
Could we use doc.Parse(json.data(), json.size()) and rename the helper to FromJsonView()? This would remove the extra byte allocation, manual NUL terminator, and size() - 1, while keeping the embedded-NUL validation.
There was a problem hiding this comment.
FromJsonView() now parses exactly the file length, with no extra byte or manual terminator. It uses a bounded MemoryStream because this RapidJSON version’s Parse(data, size) consumes a leading UTF-8 BOM, which the original parser rejects. The embedded-NUL check remains.
| Snapshot snapshot; | ||
| PAIMON_RETURN_NOT_OK(RapidJsonUtil::FromNullTerminatedJson( | ||
| std::string_view(json.data(), json.size() - 1), &snapshot, path)); | ||
| return snapshot; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 CreatePlan(); this snapshot buffer is allocated before Sirius gets a plan or batch it can admit.
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 ReadFile() outside the pool, before planning later failed. This demonstrates a budget bypass on the actual read path. The tested cap and padded size are stress inputs. We have no production size or concurrency measurements.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Generated-by: OpenAI Codex (GPT-6)
Purpose
Closes #587.
When a caller supplies a custom memory pool, the shared data scan requests the numbered filesystem snapshot input buffer from that pool before reading. If the pool refuses, planning returns
OutOfMemory. This also applies when the audit log or read-optimized system table delegates to the same data scan. Schema loading, standalone snapshot readers, and other planning allocations are outside this change.Issue #587 reproduces the budget gap with one scan, a 256 KiB capped pool, and a valid 270,337 B snapshot padded with JSON whitespace.
Tests
The
CreatePlan()regression exercises refusal before reading, exact-size allocation, stream closure, empty and malformed input, a short read, retry, and the default filesystem read path. The JSON-view regression checks length boundaries and the existing BOM behavior.API and Format
The public C++ API and persistent formats are unchanged. The default pool uses the existing virtual
ReadFile()route. With a custom pool, the numbered snapshot is read viaOpen()into the pool-backed buffer.Documentation
No documentation changes.
Generative AI tooling
Generated-by: OpenAI Codex (GPT-6)