realAaronWu commented on code in PR #588:
URL: https://github.com/apache/paimon-cpp/pull/588#discussion_r4667986573


##########
src/paimon/core/snapshot.cpp:
##########
@@ -323,11 +332,42 @@ void Snapshot::FromJson(const rapidjson::Value& obj) 
noexcept(false) {
         RapidJsonUtil::DeserializeKeyValue<std::optional<int64_t>>(obj, 
FIELD_NEXT_ROW_ID);
 }
 
-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));
+Result<Snapshot> Snapshot::FromPath(const std::shared_ptr<FileSystem>& fs, 
const std::string& path,
+                                    MemoryPool* pool) {
+    if (pool == nullptr || pool == GetDefaultPool().get()) {
+        std::string json_str;
+        PAIMON_RETURN_NOT_OK(fs->ReadFile(path, &json_str));
+        return Snapshot::FromJsonString(json_str);
+    }
+
+    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"));
+        const uint64_t allocation_size = static_cast<uint64_t>(length) + 1;
+        PAIMON_RETURN_NOT_OK(ValidateValueInRange<size_t>(allocation_size, 
"snapshot buffer size"));
+        try {
+            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);
+        }
+        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));
+        }
+        json.data()[length] = '\0';
+    }
+    Snapshot snapshot;
+    PAIMON_RETURN_NOT_OK(RapidJsonUtil::FromNullTerminatedJson(
+        std::string_view(json.data(), json.size() - 1), &snapshot, path));
     return snapshot;

Review Comment:
   The motivating workload is [Sirius’s Paimon read 
integration](https://github.com/sirius-db/sirius/issues/2181). 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](https://github.com/apache/paimon-cpp/issues/587), 
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.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to