This is an automated email from the ASF dual-hosted git repository.
JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git
The following commit(s) were added to refs/heads/master by this push:
new af01a0e236 [python] Open one remote stream for concurrent cached reads
(#10895)
af01a0e236 is described below
commit af01a0e23609a0e13d480fb184f9d48516766009
Author: Wenchao Wu <[email protected]>
AuthorDate: Sun Nov 1 22:53:29 2026 +0800
[python] Open one remote stream for concurrent cached reads (#10895)
---
.../pypaimon/filesystem/caching_file_io.py | 9 +++-
.../pypaimon/tests/caching_file_io_test.py | 48 ++++++++++++++++++++++
2 files changed, 56 insertions(+), 1 deletion(-)
diff --git a/paimon-python/pypaimon/filesystem/caching_file_io.py
b/paimon-python/pypaimon/filesystem/caching_file_io.py
index d60d8c2ecd..f968f1b3d8 100644
--- a/paimon-python/pypaimon/filesystem/caching_file_io.py
+++ b/paimon-python/pypaimon/filesystem/caching_file_io.py
@@ -274,6 +274,7 @@ class CachingInputStream(io.RawIOBase):
self._cache = cache
self._pos = 0
self._io_lock = threading.Lock()
+ self._open_lock = threading.Lock()
self._remote_supports_pread = None
def readable(self):
@@ -398,8 +399,14 @@ class CachingInputStream(io.RawIOBase):
return bytes(buf)
def _get_remote_stream(self):
+ # read_at is documented as thread-safe, so concurrent misses must not
+ # each open their own stream: only the first one is retained and the
+ # rest would leak. Kept separate from _io_lock, which serializes seek
+ # and read on an already open stream.
if self._stream is None:
- self._stream = self._file_io.new_input_stream(self._file_path)
+ with self._open_lock:
+ if self._stream is None:
+ self._stream =
self._file_io.new_input_stream(self._file_path)
return self._stream
def close(self):
diff --git a/paimon-python/pypaimon/tests/caching_file_io_test.py
b/paimon-python/pypaimon/tests/caching_file_io_test.py
index ae5ff07044..6b09c71c2a 100644
--- a/paimon-python/pypaimon/tests/caching_file_io_test.py
+++ b/paimon-python/pypaimon/tests/caching_file_io_test.py
@@ -26,6 +26,7 @@ import pickle
import shutil
import tempfile
import threading
+import time
import tracemalloc
import unittest
from concurrent.futures import ThreadPoolExecutor
@@ -279,6 +280,53 @@ class CachingInputStreamTest(unittest.TestCase):
self.assertEqual(data, result)
file_io2.new_input_stream.assert_not_called()
+ def test_concurrent_read_at_opens_one_remote_stream(self):
+ # read_at is documented as thread-safe and is called concurrently by
+ # the parallel readers, so a cold stream must be opened exactly once.
+ data = bytes(i % 251 for i in range(200000))
+ opened = []
+ lock = threading.Lock()
+
+ class _SeekOnlyStream(io.BytesIO):
+ def fileno(self):
+ raise OSError("no fileno")
+
+ file_io = MagicMock()
+ file_io.get_file_size.side_effect = lambda p: len(data)
+
+ def open_stream(path):
+ # Hold the opener briefly so concurrent misses overlap.
+ time.sleep(0.02)
+ stream = _SeekOnlyStream(data)
+ with lock:
+ opened.append(stream)
+ return stream
+
+ file_io.new_input_stream.side_effect = open_stream
+
+ cache = LocalMemoryCacheManager(2 ** 63 - 1, block_size=4096)
+ stream = CachingInputStream(file_io, "test.index", cache)
+ errors = []
+
+ def reader(offset):
+ try:
+ self.assertEqual(data[offset:offset + 100],
stream.read_at(100, offset))
+ except Exception as error: # noqa: B902 - surfaced through errors
+ errors.append(error)
+
+ threads = [threading.Thread(target=reader, args=(i * 7000,)) for i in
range(16)]
+ for thread in threads:
+ thread.start()
+ for thread in threads:
+ thread.join()
+
+ self.assertEqual([], errors)
+ self.assertEqual(1, len(opened))
+
+ # Closing the stream must leave no remote stream behind.
+ stream.close()
+ self.assertTrue(all(opened_stream.closed for opened_stream in opened))
+
def test_read_empty(self):
cache = LocalDiskCacheManager(self.cache_dir, 2 ** 63 - 1,
block_size=8)
stream = CachingInputStream(self._make_file_io(b""), "test", cache)