SteNicholas commented on code in PR #513:
URL: https://github.com/apache/paimon-cpp/pull/513#discussion_r4559181327


##########
src/paimon/common/reader/warmup_file_batch_reader.cpp:
##########
@@ -0,0 +1,152 @@
+/*
+ * 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 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 "paimon/common/reader/warmup_file_batch_reader.h"
+
+#include <type_traits>
+#include <utility>
+
+#include "paimon/common/metrics/metrics_impl.h"
+#include "paimon/common/reader/reader_utils.h"
+
+namespace paimon {
+
+WarmupFileBatchReader::WarmupFileBatchReader(std::shared_ptr<PrefetchFileBatchReader>
 reader,
+                                             std::shared_ptr<FileReadAhead> 
read_ahead,
+                                             WarmupLevel level)
+    : reader_(std::move(reader)), read_ahead_(std::move(read_ahead)), 
level_(level) {}
+
+WarmupFileBatchReader::~WarmupFileBatchReader() {
+    ResetReadState();
+    read_ahead_->ReleaseBuffers();
+}
+
+void WarmupFileBatchReader::ResetReadState() {
+    prepared_ = false;
+    batch_returned_ = false;
+    read_status_ = Status::OK();
+}
+
+void WarmupFileBatchReader::Prepare() {
+    prepared_ = true;
+}
+
+void WarmupFileBatchReader::Warmup() {
+    if (!prepared_ || closed_ || level_ != WarmupLevel::RAW) {
+        return;
+    }
+    (void)read_ahead_->Warmup(reader_.get());
+}
+
+template <typename Batch>
+Result<Batch> WarmupFileBatchReader::ReadOneBatch() {
+    if (closed_) {
+        return Status::Invalid("cannot read a closed warmup reader");
+    }
+    PAIMON_RETURN_NOT_OK(read_status_);
+    batch_returned_ = false;
+    Result<Batch> result = [&]() -> Result<Batch> {
+        PAIMON_RETURN_NOT_OK(read_ahead_->Warmup(reader_.get()));
+        // Preserve the format reader's mapping for the API used to consume 
this batch.
+        Result<Batch> read = [&]() {
+            if constexpr (std::is_same_v<Batch, ReadBatch>) {
+                return reader_->NextBatch();
+            } else {
+                return reader_->NextBatchWithBitmap();
+            }
+        }();
+        PAIMON_ASSIGN_OR_RAISE(Batch batch, std::move(read));
+        Status status = read_ahead_->GetStatus();
+        if (!status.ok()) {
+            if constexpr (std::is_same_v<Batch, ReadBatch>) {
+                ReaderUtils::ReleaseReadBatch(std::move(batch));
+            } else {
+                ReaderUtils::ReleaseReadBatch(std::move(batch.first));
+            }
+            return status;
+        }
+        return batch;
+    }();
+    if (result.ok() && !BatchReader::IsEofBatch(result.value())) {
+        batch_returned_ = true;
+    } else if (!result.ok()) {
+        read_status_ = result.status();
+    }
+    return result;
+}
+
+Result<BatchReader::ReadBatchWithBitmap> 
WarmupFileBatchReader::NextBatchWithBitmap() {
+    return ReadOneBatch<ReadBatchWithBitmap>();
+}
+
+Result<BatchReader::ReadBatch> WarmupFileBatchReader::NextBatch() {
+    return ReadOneBatch<ReadBatch>();
+}
+
+Status WarmupFileBatchReader::SetReadSchema(::ArrowSchema* schema,
+                                            const std::shared_ptr<Predicate>& 
predicate,
+                                            const 
std::optional<RoaringBitmap32>& bitmap) {
+    ResetReadState();
+    if (closed_) {
+        return Status::Invalid("cannot configure a closed warmup reader");
+    }
+    read_ahead_->Reset();
+    read_status_ = reader_->SetReadSchema(schema, predicate, bitmap);
+    PAIMON_RETURN_NOT_OK(read_status_);
+    Prepare();
+    return Status::OK();
+}
+
+std::shared_ptr<Metrics> WarmupFileBatchReader::GetReaderMetrics() const {
+    auto metrics = std::make_shared<MetricsImpl>();
+    metrics->Merge(reader_->GetReaderMetrics());
+    metrics->Merge(read_ahead_->GetMetrics());
+    metrics->SetGauge(PrefetchMetrics::ENABLED, 0.0);
+    metrics->SetGauge(PrefetchMetrics::PARALLELISM, 1.0);
+    return metrics;
+}
+
+Result<std::unique_ptr<::ArrowSchema>> WarmupFileBatchReader::GetFileSchema() 
const {
+    return reader_->GetFileSchema();
+}
+
+Result<uint64_t> WarmupFileBatchReader::GetPreviousBatchFileRowId(uint64_t 
batch_row_id) const {
+    if (!batch_returned_) {
+        return Status::Invalid("no previous batch returned by warmup reader");
+    }
+    return reader_->GetPreviousBatchFileRowId(batch_row_id);
+}
+
+Result<uint64_t> WarmupFileBatchReader::GetNumberOfRows() const {
+    return reader_->GetNumberOfRows();
+}
+
+bool WarmupFileBatchReader::SupportPreciseBitmapSelection() const {
+    return reader_->SupportPreciseBitmapSelection();
+}
+
+void WarmupFileBatchReader::Close() {
+    if (closed_) {
+        return;
+    }
+    ResetReadState();
+    closed_ = true;
+    read_ahead_->ReleaseBuffers();

Review Comment:
   **`Close()` leaves the cache’s file descriptor open.** With a synchronous 
local Parquet reader, I counted two descriptors for the file after open, one 
after `Close()`, and zero after destroying the reader. `ReleaseBuffers()` 
drains the I/O, but `ReadAheadCache` and `FileBlockCache` still own the 
dedicated stream. `ConcatKeyValueRecordReader` keeps its closed children, so a 
multi-file run can retain one descriptor per finished file. Please close the 
cache stream after draining its fetches and preserve the counters for 
`GetReaderMetrics()`. A regression test should check the descriptors while the 
closed reader is still alive.



##########
src/paimon/format/parquet/parquet_reader_builder.h:
##########
@@ -81,6 +82,14 @@ class ParquetReaderBuilder : public ReaderBuilder {
             return Status::Invalid("Parquet reader memory pool is nullptr");
         }
         try {
+            std::optional<ReadHints> hints = hints_;
+            if (auto cache_stream = 
std::dynamic_pointer_cast<CacheInputStream>(path)) {
+                PAIMON_ASSIGN_OR_RAISE(bool cache_active, 
cache_stream->EnableCache());

Review Comment:
   **RAW warmup fetches small Parquet files’ payload bytes a second time.** 
With default synchronous settings and the checked-in 
`data-multi-row-groups.parquet` fixture, the footer read fetched `[0, 7620)` 
into the block cache. That covers the entire file and survives 
`SetReadSchema()`. Before the first batch, `Warmup()` still issued a storage 
read for `[4, 4781)`, all 4,777 bytes of which were already resident. Please 
reuse fully resident blocks when publishing data ranges. A counting-stream test 
can assert that warming this file issues no second request.



##########
src/paimon/common/reader/data_file_reader_factory.cpp:
##########
@@ -69,7 +70,9 @@ Result<std::unique_ptr<ReaderBuilder>> 
DataFileReaderFactory::CreateReaderBuilde
     // off when the shared read-ahead cache is reading ahead.
     ReadHints read_hints;
     read_hints.prefetch_enabled = read_options.prefetch_enabled;
-    read_hints.read_ahead_cache_enabled = 
read_options.read_ahead_cache_enabled;
+    read_hints.read_ahead_cache_enabled = 
read_options.read_ahead_cache_enabled &&
+                                          read_options.prefetch_enabled &&

Review Comment:
   **A range-capable format plugin receives a false cache hint on synchronous 
reads.** I registered a custom `PrefetchFileBatchReader` format and opened it 
with the default options. Its `WithReadHints()` received 
`read_ahead_cache_enabled == false`, then `Warmup()` issued one shared-cache 
fetch. Parquet corrects its own copy inside `Build()`, but a plugin using the 
documented public hint can leave its own buffering enabled alongside the shared 
cache. Please make the active cache state available to capable builders when 
they choose their buffering, and cover `CreateReaderBuilder()` followed by 
`Open()` in the plugin test.



##########
src/paimon/format/parquet/parquet_reader_builder.h:
##########
@@ -92,14 +101,15 @@ class ParquetReaderBuilder : public ReaderBuilder {
             auto input_stream = std::make_shared<ParquetInputStream>(path, 
file_length, arrow_pool_,
                                                                      pool_, 
cache_, file_uri);
             auto storage_read_bytes = input_stream->StorageReadBytes();
-            PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<::parquet::FileMetaData> 
file_metadata,
-                                   GetCachedParquetMetadata(input_stream, 
file_uri, arrow_pool_));
+            PAIMON_ASSIGN_OR_RAISE(
+                std::shared_ptr<::parquet::FileMetaData> file_metadata,
+                GetCachedParquetMetadata(input_stream, file_uri, arrow_pool_, 
hints));
             if (file_metadata) {
                 input_stream->SetPageIndexRanges(*file_metadata);
             }
             return ParquetFileBatchReader::Create(
                 std::move(input_stream), options_, batch_size_, 
std::move(file_metadata),
-                std::move(storage_read_bytes), arrow_pool_, hints_);
+                std::move(storage_read_bytes), arrow_pool_, hints);

Review Comment:
   **`parquet.read.storage-read-bytes` counts cache hits and misses physical 
warmup I/O on synchronous scans.** On the local Parquet fixture, RAW warmup 
read 4,777 bytes while this counter stayed at 7,620. The first batch then added 
303 bytes to the counter while the filesystem read zero. `storage_read_bytes` 
comes from the Arrow adapter above `CacheInputStream`, so it measures bytes 
delivered to Parquet. Please count successful reads on the backing streams for 
this metric. A regression test can compare the increments after warmup and 
after a cache-hit batch.



##########
src/paimon/common/utils/read_ahead_cache.cpp:
##########
@@ -225,10 +247,58 @@ void ReadAheadCache::PreBuffer(uint64_t offset) {
     DispatchFetches(fetches);
 }
 
+void ReadAheadCache::SetBoundedRetention(bool enabled) {
+    std::unique_lock<std::shared_mutex> lock(rw_mutex_);
+    bounded_retention_ = enabled;
+}
+
+bool ReadAheadCache::MakeRoomLocked(uint64_t size, uint64_t window_begin, 
uint64_t window_end) {
+    const uint64_t limit = config_.GetPreBufferLimit();
+    if (size > limit) {
+        return false;
+    }
+    for (size_t visited = 0; visited < ranges_.size(); ++visited) {

Review Comment:
   **A full budget can turn every cache miss into a scan of every registered 
range.** When a pending fetch or a callback pins the only resident buffer, 
`MakeRoomLocked()` visits even unpublished entries under the exclusive lock and 
frees nothing. In a local debug benchmark with one pinned 4 KiB buffer, 256 
identical-size misses took 2.5 ms with 1,000 ranges, 20 ms with 8,000, and 154 
ms with 64,000. Each run issued just one fetch. Please restrict eviction to 
resident candidates so a reader can fall back promptly while those buffers are 
pinned.



-- 
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