3636#include < memory>
3737#include < mutex>
3838#include < optional>
39- #include < ranges>
4039#include < thread>
4140#include < vector>
4241
@@ -121,60 +120,40 @@ FileScannerV2ReaderLocalCache::FileScannerV2ReaderLocalCache(
121120 _memory_tracker(std::make_shared<doris::MemTracker>(" FileScannerV2ReaderLocalCache" )) {}
122121
123122FileScannerV2ReaderLocalCache::~FileScannerV2ReaderLocalCache () {
124- std::map<FileKey, std::shared_ptr<FileScannerV2ReaderLocalFileCache>> files;
125- {
126- std::lock_guard lock (_registry_mutex);
127- files.swap (_files);
128- }
129- for (const auto & file : files | std::views::values) {
123+ auto files = _file_caches ();
124+ for (const auto & file : files) {
130125 file->_drain (this );
131126 }
132- files.clear ();
133127 std::lock_guard lock (_budget_mutex);
134128 DORIS_CHECK (_memory_bytes == 0 );
135129 DORIS_CHECK (_reserved_bytes == 0 );
136130}
137131
138- FileScannerV2ReaderLocalCache::FileKey FileScannerV2ReaderLocalCache::_make_file_key (
139- const UInt128Wrapper& file_key) {
140- return {.file_key_high = file_key.high (), .file_key_low = file_key.low ()};
141- }
142-
143132std::shared_ptr<FileScannerV2ReaderLocalFileCache>
144- FileScannerV2ReaderLocalCache::get_or_create_file_cache ( const UInt128Wrapper& file_key ) {
133+ FileScannerV2ReaderLocalCache::create_file_cache ( ) {
145134 if (_capacity == 0 ) {
146135 return nullptr ;
147136 }
148- const FileKey key = _make_file_key (file_key);
149- std::shared_ptr<FileScannerV2ReaderLocalFileCache> retired_file;
150137 std::shared_ptr<FileScannerV2ReaderLocalFileCache> file_cache;
138+ try {
139+ file_cache = std::shared_ptr<FileScannerV2ReaderLocalFileCache>(
140+ new FileScannerV2ReaderLocalFileCache (shared_from_this ()));
141+ } catch (const doris::Exception&) {
142+ return nullptr ;
143+ } catch (const std::bad_alloc&) {
144+ return nullptr ;
145+ }
151146 {
152147 std::lock_guard lock (_registry_mutex);
153- if (const auto it = _files.find (key); it != _files.end ()) {
154- return it->second ;
155- }
156- const size_t registry_limit =
157- std::max<size_t >(16 , _capacity / FILE_CACHE_REGISTRY_GRANULARITY );
158- if (_files.size () >= registry_limit) {
159- const auto idle = std::ranges::find_if (
160- _files, [](const auto & item) { return item.second .use_count () == 1 ; });
161- if (idle != _files.end ()) {
162- retired_file = std::move (idle->second );
163- _files.erase (idle);
164- }
165- }
166148 try {
167- file_cache = std::shared_ptr<FileScannerV2ReaderLocalFileCache>(
168- new FileScannerV2ReaderLocalFileCache (shared_from_this ()));
169- _files.emplace (key, file_cache);
149+ std::erase_if (_files, [](const auto & file) { return file.expired (); });
150+ _files.emplace_back (file_cache);
170151 } catch (const doris::Exception&) {
171152 return nullptr ;
172153 } catch (const std::bad_alloc&) {
173154 return nullptr ;
174155 }
175156 }
176- // Retiring outside the registry lock avoids coupling file cleanup to new-reader admission.
177- retired_file.reset ();
178157 return file_cache;
179158}
180159
@@ -205,20 +184,12 @@ bool FileScannerV2ReaderLocalCache::_reserve(size_t bytes,
205184 if (_try_reserve (bytes)) {
206185 return true ;
207186 }
208- auto files = _file_caches ();
209- // Reservation is a cold-path operation. Cross-file eviction keeps one query budget without
210- // putting a query-global mutex on every cache hit.
211- for (int pass = 0 ; pass < 2 ; ++pass) {
212- for (const auto & file : files) {
213- if ((pass == 0 ) != (file.get () == requester)) {
214- continue ;
215- }
216- while (file->_evict_one ()) {
217- ++*evicted;
218- if (_try_reserve (bytes)) {
219- return true ;
220- }
221- }
187+ // A stream may recycle its own cold blocks, but it must never evict another stream's hot
188+ // block map. StarRocks gets the same isolation from CacheInputStream::_block_map ownership.
189+ while (requester->_evict_one ()) {
190+ ++*evicted;
191+ if (_try_reserve (bytes)) {
192+ return true ;
222193 }
223194 }
224195 return false ;
@@ -254,8 +225,10 @@ FileScannerV2ReaderLocalCache::_file_caches() const {
254225 std::vector<std::shared_ptr<FileScannerV2ReaderLocalFileCache>> files;
255226 std::lock_guard lock (_registry_mutex);
256227 files.reserve (_files.size ());
257- for (const auto & file : _files | std::views::values) {
258- files.push_back (file);
228+ for (const auto & file : _files) {
229+ if (auto live_file = file.lock (); live_file != nullptr ) {
230+ files.push_back (std::move (live_file));
231+ }
259232 }
260233 return files;
261234}
@@ -301,7 +274,6 @@ void FileScannerV2ReaderLocalFileCache::_drain(FileScannerV2ReaderLocalCache* ow
301274 }
302275 _entries.clear ();
303276 _lru.clear ();
304- _admission_probes.clear ();
305277 };
306278 try {
307279 std::optional<SwitchThreadMemTrackerLimiter> switch_query_tracker;
@@ -372,26 +344,6 @@ bool FileScannerV2ReaderLocalFileCache::_evict_one() {
372344 return true ;
373345}
374346
375- bool FileScannerV2ReaderLocalFileCache::_should_admit_locked (size_t block_offset, size_t block_size,
376- size_t requested_bytes) {
377- if (requested_bytes >= (block_size + DIRECT_ADMISSION_RATIO - 1 ) / DIRECT_ADMISSION_RATIO ) {
378- _admission_probes.erase (block_offset);
379- return true ;
380- }
381- if (_admission_probes.erase (block_offset) != 0 ) {
382- return true ;
383- }
384- if (_admission_probes.size () >= MAX_ADMISSION_PROBES ) {
385- _admission_probes.erase (_admission_probes.begin ());
386- }
387- try {
388- _admission_probes.emplace (block_offset);
389- } catch (...) {
390- return false ;
391- }
392- return false ;
393- }
394-
395347void FileScannerV2ReaderLocalFileCache::_abort_load (size_t block_offset,
396348 const std::shared_ptr<Entry>& entry) {
397349 size_t reserved_bytes = 0 ;
@@ -462,7 +414,6 @@ bool FileScannerV2ReaderLocalFileCache::read_if_present(size_t block_offset, siz
462414Status FileScannerV2ReaderLocalFileCache::get_or_load (size_t block_offset, size_t block_size,
463415 const FileBlockSPtr& file_block,
464416 size_t file_block_offset,
465- size_t requested_bytes,
466417 LookupResult* lookup) {
467418 DORIS_CHECK (lookup != nullptr );
468419 *lookup = {};
@@ -476,10 +427,6 @@ Status FileScannerV2ReaderLocalFileCache::get_or_load(size_t block_offset, size_
476427 std::unique_lock lock (_mutex);
477428 const auto it = _entries.find (block_offset);
478429 if (it == _entries.end ()) {
479- if (!_should_admit_locked (block_offset, block_size, requested_bytes)) {
480- lookup->admission_rejected = true ;
481- return Status::OK ();
482- }
483430 try {
484431 entry = std::make_shared<Entry>();
485432 } catch (const doris::Exception&) {
@@ -612,8 +559,8 @@ CachedRemoteFileReader::CachedRemoteFileReader(FileReaderSPtr remote_file_reader
612559 _init_external_table_cache (opts);
613560 }
614561 if (_enable_reader_local_cache) {
615- // Bind once per immutable file identity so hot reads never visit the query-wide registry.
616- _reader_local_file_cache = _reader_local_cache->get_or_create_file_cache (_cache_hash );
562+ // The block map follows this physical reader rather than surviving in a query registry.
563+ _reader_local_file_cache = _reader_local_cache->create_file_cache ( );
617564 _enable_reader_local_cache = _reader_local_file_cache != nullptr ;
618565 }
619566}
@@ -1394,9 +1341,8 @@ Status CachedRemoteFileReader::_read_local_block(const FileBlockSPtr& block, siz
13941341
13951342 const size_t buffer_size = buffer_end - buffer_offset;
13961343 FileScannerV2ReaderLocalFileCache::LookupResult lookup;
1397- RETURN_IF_ERROR (_reader_local_file_cache->get_or_load (buffer_offset, buffer_size, block,
1398- buffer_offset - block->range ().left ,
1399- copy_size, &lookup));
1344+ RETURN_IF_ERROR (_reader_local_file_cache->get_or_load (
1345+ buffer_offset, buffer_size, block, buffer_offset - block->range ().left , &lookup));
14001346 stats.num_reader_local_cache_evict += cast_set<int64_t >(lookup.evicted );
14011347 stats.num_reader_local_cache_admission_reject += lookup.admission_rejected ? 1 : 0 ;
14021348 stats.reader_local_cache_fill_timer += lookup.fill_time ;
@@ -1469,17 +1415,13 @@ Status CachedRemoteFileReader::_read_remote_blocks_into_cache(
14691415 const std::vector<FileBlockSPtr>& empty_blocks, size_t offset, size_t bytes_req,
14701416 size_t already_read, Slice result, bool is_dryrun, ReadStatistics& stats,
14711417 SourceReadBreakdown& source_read_breakdown, const IOContext* io_ctx,
1472- size_t & indirect_read_bytes, size_t & empty_start, size_t & empty_end,
1473- PeerFetchedBlockSet& peer_fetched_blocks) {
1474- empty_start = 0 ;
1475- empty_end = 0 ;
1476- peer_fetched_blocks.clear ();
1418+ size_t & indirect_read_bytes, PeerFetchedBlockSet& fetched_blocks) {
14771419 if (empty_blocks.empty ()) {
14781420 return Status::OK ();
14791421 }
14801422
1481- empty_start = empty_blocks.front ()->range ().left ;
1482- empty_end = empty_blocks.back ()->range ().right ;
1423+ const size_t empty_start = empty_blocks.front ()->range ().left ;
1424+ const size_t empty_end = empty_blocks.back ()->range ().right ;
14831425 const size_t span_read_size = empty_end - empty_start + 1 ;
14841426 const auto peer_fetch_layout = build_peer_fetch_layout (empty_blocks, size ());
14851427 std::unique_ptr<char []> buffer;
@@ -1491,10 +1433,6 @@ Status CachedRemoteFileReader::_read_remote_blocks_into_cache(
14911433 std::vector<std::vector<const PeerFetchChunk*>> peer_chunks_by_block;
14921434 if (stats.from_peer_cache ) {
14931435 // Peer returns sparse payloads; remember the exact sparse blocks that were filled.
1494- peer_fetched_blocks.reserve (empty_blocks.size ());
1495- for (const auto & block : empty_blocks) {
1496- peer_fetched_blocks.insert (block.get ());
1497- }
14981436 peer_chunks_by_block.resize (empty_blocks.size ());
14991437 for (const auto & chunk : peer_result.chunks ) {
15001438 DCHECK_LT (chunk.block_index , empty_blocks.size ());
@@ -1503,8 +1441,10 @@ Status CachedRemoteFileReader::_read_remote_blocks_into_cache(
15031441 }
15041442
15051443 SCOPED_CONCURRENCY_COUNT (ConcurrencyStatsManager::instance ().cached_remote_reader_write_back );
1444+ fetched_blocks.reserve (fetched_blocks.size () + empty_blocks.size ());
15061445 for (size_t idx = 0 ; idx < empty_blocks.size (); ++idx) {
15071446 auto & block = empty_blocks[idx];
1447+ fetched_blocks.insert (block.get ());
15081448 if (block->state () == FileBlock::State::SKIP_CACHE ) {
15091449 continue ;
15101450 }
@@ -1566,8 +1506,7 @@ Status CachedRemoteFileReader::_read_remote_blocks_into_cache(
15661506
15671507Status CachedRemoteFileReader::_read_remaining_blocks_from_cache (
15681508 const FileBlocksHolder& holder, size_t offset, size_t bytes_req, Slice result,
1569- bool is_dryrun, size_t empty_start, size_t empty_end,
1570- const PeerFetchedBlockSet& peer_fetched_blocks, ReadStatistics& stats,
1509+ bool is_dryrun, const PeerFetchedBlockSet& fetched_blocks, ReadStatistics& stats,
15711510 SourceReadBreakdown& source_read_breakdown, size_t & indirect_read_bytes, size_t * bytes_read,
15721511 const IOContext* io_ctx) {
15731512 size_t current_offset = offset + *bytes_read;
@@ -1586,14 +1525,7 @@ Status CachedRemoteFileReader::_read_remaining_blocks_from_cache(
15861525
15871526 size_t read_size =
15881527 end_offset > right ? right - current_offset + 1 : end_offset - current_offset + 1 ;
1589- if (!peer_fetched_blocks.empty () && contains_file_block (peer_fetched_blocks, block)) {
1590- // For sparse peer reads, skip only blocks fetched from peer. Other blocks inside the
1591- // enclosing span may still come from local cache.
1592- *bytes_read += read_size;
1593- current_offset = right + 1 ;
1594- continue ;
1595- }
1596- if (peer_fetched_blocks.empty () && empty_start <= left && right <= empty_end) {
1528+ if (contains_file_block (fetched_blocks, block)) {
15971529 *bytes_read += read_size;
15981530 current_offset = right + 1 ;
15991531 continue ;
@@ -1816,17 +1748,27 @@ Status CachedRemoteFileReader::_read_from_indirect_cache(size_t offset, Slice re
18161748 stats.cache_get_or_set_timer += sw.elapsed_time ();
18171749
18181750 auto empty_blocks = _collect_remote_read_blocks (holder, stats);
1819- size_t empty_start = 0 ;
1820- size_t empty_end = 0 ;
1821- PeerFetchedBlockSet peer_fetched_blocks;
1822- RETURN_IF_ERROR (_read_remote_blocks_into_cache (empty_blocks, offset, bytes_req, already_read,
1823- result, is_dryrun, stats, source_read_breakdown,
1824- io_ctx, indirect_read_bytes, empty_start,
1825- empty_end, peer_fetched_blocks));
1751+ PeerFetchedBlockSet fetched_blocks;
1752+ size_t run_start = 0 ;
1753+ for (size_t index = 1 ; index <= empty_blocks.size (); ++index) {
1754+ const bool end_of_run =
1755+ index == empty_blocks.size () ||
1756+ empty_blocks[index - 1 ]->range ().right + 1 != empty_blocks[index]->range ().left ;
1757+ if (!end_of_run) {
1758+ continue ;
1759+ }
1760+ // A cache hit is a hard merge boundary. Reading across it would redownload resident data
1761+ // and violate the cache-aware miss coalescing invariant used by StarRocks.
1762+ std::vector<FileBlockSPtr> contiguous_misses (empty_blocks.begin () + run_start,
1763+ empty_blocks.begin () + index);
1764+ RETURN_IF_ERROR (_read_remote_blocks_into_cache (
1765+ contiguous_misses, offset, bytes_req, already_read, result, is_dryrun, stats,
1766+ source_read_breakdown, io_ctx, indirect_read_bytes, fetched_blocks));
1767+ run_start = index;
1768+ }
18261769 *bytes_read = already_read;
18271770 RETURN_IF_ERROR (_read_remaining_blocks_from_cache (holder, offset, bytes_req, result, is_dryrun,
1828- empty_start, empty_end, peer_fetched_blocks,
1829- stats, source_read_breakdown,
1771+ fetched_blocks, stats, source_read_breakdown,
18301772 indirect_read_bytes, bytes_read, io_ctx));
18311773 g_read_cache_indirect_bytes << indirect_read_bytes;
18321774 g_read_cache_indirect_total_bytes << *bytes_read;
0 commit comments