justinli500 commented on code in PR #49855:
URL: https://github.com/apache/arrow/pull/49855#discussion_r4774423964


##########
cpp/src/arrow/io/memory_test.cc:
##########
@@ -886,36 +925,247 @@ TEST(RangeReadCache, LazyWithPrefetching) {
   ASSERT_EQ(0, file->read_count());
 
   ASSERT_OK_AND_ASSIGN(auto buf, cache.Read({8, 2}));
-  AssertBufferEqual(*buf, "ij");
+  ASSERT_TRUE(buf);
+  AssertBufferEqual(**buf, "ij");
   // Read {8, 2} and prefetch {20, 2}
   ASSERT_EQ(2, file->read_count());
 
   ASSERT_OK_AND_ASSIGN(buf, cache.Read({20, 2}));
-  AssertBufferEqual(*buf, "uv");
+  ASSERT_TRUE(buf);
+  AssertBufferEqual(**buf, "uv");
   // Read count remains 2 as the range {20, 2} has already been prefetched
   ASSERT_EQ(2, file->read_count());
 
   ASSERT_OK_AND_ASSIGN(buf, cache.Read({1, 1}));
-  AssertBufferEqual(*buf, "b");
+  ASSERT_TRUE(buf);
+  AssertBufferEqual(**buf, "b");
   // Read {1, 3} and prefetch {5, 2}
   ASSERT_EQ(4, file->read_count());
 
   ASSERT_OK_AND_ASSIGN(buf, cache.Read({3, 1}));
-  AssertBufferEqual(*buf, "d");
+  ASSERT_TRUE(buf);
+  AssertBufferEqual(**buf, "d");
   // Already prefetched
   ASSERT_EQ(4, file->read_count());
 
   // Requested ranges are still cached
   ASSERT_OK_AND_ASSIGN(buf, cache.Read({5, 1}));
-  AssertBufferEqual(*buf, "f");
+  ASSERT_TRUE(buf);
+  AssertBufferEqual(**buf, "f");
   // Already prefetched
   ASSERT_EQ(4, file->read_count());
 
   // Non-cached ranges
-  ASSERT_RAISES(Invalid, cache.Read({20, 3}));
-  ASSERT_RAISES(Invalid, cache.Read({19, 3}));
-  ASSERT_RAISES(Invalid, cache.Read({0, 3}));
-  ASSERT_RAISES(Invalid, cache.Read({25, 2}));
+  ASSERT_OK_AND_EQ(std::nullopt, cache.Read({20, 3}));
+  ASSERT_OK_AND_EQ(std::nullopt, cache.Read({19, 3}));
+  ASSERT_OK_AND_EQ(std::nullopt, cache.Read({0, 3}));
+  ASSERT_OK_AND_EQ(std::nullopt, cache.Read({25, 2}));
+}
+
+TEST(RangeReadCache, EvictEntriesBefore) {
+  // GH-39808: entries cached by PreBuffer()-style code need to be evictable so
+  // memory stays bounded while iterating a large Parquet file.
+  std::string data = "abcdefghijklmnopqrstuvwxyz";
+
+  for (auto lazy : std::vector<bool>{false, true}) {
+    SCOPED_TRACE(lazy);
+    CacheOptions options = CacheOptions::Defaults();
+    options.hole_size_limit = 0;  // disable coalescing: one entry per range
+    options.range_size_limit = 10;
+    options.lazy = lazy;
+
+    auto file = std::make_shared<BufferReader>(std::make_shared<Buffer>(data));
+    internal::ReadRangeCache cache(file, {}, options);
+
+    // Entries: [1,3), [10,14), [20,22).
+    ASSERT_OK(cache.Cache({{1, 2}, {10, 4}, {20, 2}}));
+    ASSERT_OK_AND_ASSIGN(auto buf, cache.Read({10, 4}));
+    ASSERT_TRUE(buf);
+    AssertBufferEqual(**buf, "klmn");
+
+    // An offset that splits no entry frees nothing.
+    cache.EvictEntriesBefore(0);
+    cache.EvictEntriesBefore(2);  // [1,3) extends past 2 -> retained
+    ASSERT_OK_AND_ASSIGN(buf, cache.Read({1, 2}));
+    ASSERT_TRUE(buf);
+
+    // end_offset == an entry's end frees exactly that entry ([1,3) ends at 3).
+    cache.EvictEntriesBefore(3);
+    ASSERT_OK_AND_EQ(std::nullopt, cache.Read({1, 2}));
+    ASSERT_OK_AND_ASSIGN(buf, cache.Read({10, 4}));  // others intact
+    ASSERT_TRUE(buf);
+    AssertBufferEqual(**buf, "klmn");
+
+    // An offset inside an entry leaves it in place.
+    cache.EvictEntriesBefore(12);  // [10,14) straddles 12
+    ASSERT_OK_AND_ASSIGN(buf, cache.Read({10, 4}));
+    ASSERT_TRUE(buf);
+
+    // A wide offset frees every remaining entry in one call.
+    cache.EvictEntriesBefore(100);
+    ASSERT_OK_AND_EQ(std::nullopt, cache.Read({10, 4}));
+    ASSERT_OK_AND_EQ(std::nullopt, cache.Read({20, 2}));
+    AssertBufferEqual(**buf, "klmn");
+
+    // Empty cache is a safe no-op.
+    cache.EvictEntriesBefore(100);
+  }
+}
+
+TEST(RangeReadCache, EvictAndRecache) {
+  for (bool lazy : {false, true}) {
+    SCOPED_TRACE(lazy);
+    auto options = CacheOptions::Defaults();
+    options.hole_size_limit = 0;
+    options.range_size_limit = 1;
+    options.lazy = lazy;
+    options.prefetch_limit = 1;
+    auto file = std::make_shared<CountingBufferReader>(
+        Buffer::FromString("abcdefghijklmnopqrstuvwxyz"));
+    internal::ReadRangeCache cache(file, {}, options);
+    ASSERT_OK(cache.Cache({{1, 2}, {10, 2}, {20, 2}}));
+
+    cache.EvictEntriesBefore(3);
+    ASSERT_FINISHES_OK(cache.Wait());
+    const int64_t expected_reads = lazy ? 2 : 3;
+    ASSERT_EQ(file->read_count(), expected_reads);
+    ASSERT_OK_AND_EQ(std::nullopt, cache.Read({1, 2}));
+    ASSERT_FINISHES_AND_RAISES(Invalid, cache.WaitFor({{1, 2}}));
+
+    ASSERT_OK(cache.Cache({{1, 2}}));
+    ASSERT_FINISHES_OK(cache.WaitFor({{1, 2}}));
+    ASSERT_OK_AND_ASSIGN(auto buffer, cache.Read({1, 2}));
+    ASSERT_TRUE(buffer);
+    AssertBufferEqual(**buffer, "bc");
+    ASSERT_EQ(file->read_count(), expected_reads + 1);
+
+    cache.EvictEntriesBefore(12);
+    ASSERT_FINISHES_OK(cache.Wait());
+    ASSERT_OK_AND_EQ(std::nullopt, cache.Read({1, 2}));
+    ASSERT_OK_AND_EQ(std::nullopt, cache.Read({10, 2}));
+    ASSERT_OK_AND_ASSIGN(buffer, cache.Read({20, 2}));
+    ASSERT_TRUE(buffer);
+    AssertBufferEqual(**buffer, "uv");
+    ASSERT_EQ(file->read_count(), expected_reads + 1);
+
+    cache.EvictEntriesBefore(100);
+    ASSERT_FINISHES_OK(cache.Wait());
+    ASSERT_OK(cache.Cache({{5, 2}}));
+    ASSERT_FINISHES_OK(cache.Wait());
+    ASSERT_OK_AND_ASSIGN(buffer, cache.Read({5, 2}));
+    ASSERT_TRUE(buffer);
+    AssertBufferEqual(**buffer, "fg");
+    ASSERT_EQ(file->read_count(), expected_reads + 2);
+  }
+}
+
+TEST(RangeReadCache, ConcurrentReadAndEvict) {
+#ifndef ARROW_ENABLE_THREADING
+  GTEST_SKIP() << "Test requires threading support";
+#endif
+  // GH-39808: the Parquet dataset scanner calls EvictEntriesBefore from the
+  // thread-pool continuation that runs after a row group is decoded, while
+  // other threads may still be calling Read() for column chunks of other
+  // in-flight row groups. Exercise that pattern explicitly by slamming the
+  // cache with parallel Read()s interleaved with Evict()s and make sure we
+  // don't hit UB (iterator invalidation, torn reads, etc.).
+  constexpr int kNumRanges = 64;
+  constexpr int kRangeSize = 64;
+  constexpr int kIterations = 50;
+  std::string data(kNumRanges * kRangeSize, 'x');
+
+  for (auto lazy : std::vector<bool>{false, true}) {
+    SCOPED_TRACE(lazy);
+    CacheOptions options = CacheOptions::Defaults();
+    // No coalescing: each range is its own entry so we can evict them
+    // individually without fighting the coalescing heuristic.
+    options.hole_size_limit = 0;
+    options.range_size_limit = kRangeSize;
+    options.lazy = lazy;
+
+    auto file = std::make_shared<BufferReader>(std::make_shared<Buffer>(data));
+    internal::ReadRangeCache cache(file, {}, options);
+
+    std::vector<ReadRange> ranges;
+    ranges.reserve(kNumRanges);
+    for (int i = 0; i < kNumRanges; ++i) {
+      ranges.push_back({static_cast<int64_t>(i * kRangeSize), kRangeSize});
+    }
+    ASSERT_OK(cache.Cache(ranges));
+
+    // Half of the threads repeatedly read the upper half of the ranges.
+    // The other half repeatedly evict and re-cache the lower half. Under
+    // the old code this would race on the shared `entries` vector.
+    std::atomic<bool> stop{false};
+    std::atomic<int> failures{0};
+
+    auto reader_fn = [&]() {

Review Comment:
   Good catch - I changed it so that the evictor now waits for reader progress 
before each eviction, so that it can't finish before a reader runs. 



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