Skip to content

Commit dde77c6

Browse files
committed
[fix](io) Align reader cache with stream ownership
1 parent 721924f commit dde77c6

12 files changed

Lines changed: 193 additions & 264 deletions

‎be/src/common/config.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ DEFINE_mInt64(file_cache_background_block_lru_update_queue_max_size, "500000");
12721272
DEFINE_mBool(enable_file_cache_async_touch_on_get_or_set, "false");
12731273
DEFINE_mBool(enable_reader_dryrun_when_download_file_cache, "true");
12741274
DEFINE_mBool(enable_file_scanner_v2_reader_local_cache, "true");
1275-
DEFINE_mInt64(file_scanner_v2_reader_local_cache_size, "67108864"); // 64MB per query
1275+
DEFINE_mInt64(file_scanner_v2_reader_local_cache_size, "67108864"); // 64MB per scanner
12761276
DEFINE_mInt64(file_cache_background_monitor_interval_ms, "5000");
12771277
DEFINE_mInt64(file_cache_background_ttl_gc_interval_ms, "180000");
12781278
DEFINE_mInt64(file_cache_background_ttl_info_update_interval_ms, "180000");

‎be/src/common/config.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,10 +1310,10 @@ DECLARE_mInt64(file_cache_background_block_lru_update_qps_limit);
13101310
DECLARE_mInt64(file_cache_background_block_lru_update_queue_max_size);
13111311
DECLARE_mBool(enable_file_cache_async_touch_on_get_or_set);
13121312
DECLARE_mBool(enable_reader_dryrun_when_download_file_cache);
1313-
// Cache File Scanner V2 file-cache blocks in query-shared memory. File Scanner V1 and internal
1313+
// Cache File Scanner V2 file-cache blocks in reader-local memory. File Scanner V1 and internal
13141314
// table readers never opt in to this cache.
13151315
DECLARE_mBool(enable_file_scanner_v2_reader_local_cache);
1316-
// Maximum reader-local cache bytes shared by all File Scanner V2 readers in one query.
1316+
// Maximum reader-local cache bytes managed by one File Scanner V2 scanner.
13171317
DECLARE_mInt64(file_scanner_v2_reader_local_cache_size);
13181318
DECLARE_mInt64(file_cache_background_monitor_interval_ms);
13191319
DECLARE_mInt64(file_cache_background_ttl_gc_interval_ms);

‎be/src/exec/scan/file_scanner_v2.cpp‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#include "format_v2/table_reader.h"
6868
#include "format_v2/wal/wal_table_reader.h"
6969
#include "io/cache/block_file_cache_profile.h"
70+
#include "io/cache/cached_remote_file_reader.h"
7071
#include "io/fs/file_meta_cache.h"
7172
#include "io/io_common.h"
7273
#include "runtime/descriptors.h"
@@ -965,10 +966,10 @@ Status FileScannerV2::_to_file_format(TFileFormatType::type format_type,
965966
Status FileScannerV2::_init_io_ctx() {
966967
_io_ctx = create_file_scan_io_context(_state);
967968
if (config::enable_file_scanner_v2_reader_local_cache) {
968-
if (auto* query_ctx = _state->get_query_ctx(); query_ctx != nullptr) {
969-
_io_ctx->reader_local_cache =
970-
query_ctx->get_or_create_file_scanner_v2_reader_local_cache();
971-
}
969+
const size_t capacity = cast_set<size_t>(
970+
std::max<int64_t>(0, config::file_scanner_v2_reader_local_cache_size));
971+
_io_ctx->reader_local_cache = std::make_shared<io::FileScannerV2ReaderLocalCache>(
972+
capacity, _state->query_mem_tracker());
972973
}
973974
return Status::OK();
974975
}

‎be/src/exec/scan/file_scanner_v2.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ class FileScannerV2 final : public Scanner {
105105
bool TEST_has_reader_local_cache() const {
106106
return _io_ctx != nullptr && _io_ctx->reader_local_cache != nullptr;
107107
}
108+
const void* TEST_reader_local_cache() const {
109+
return _io_ctx != nullptr ? _io_ctx->reader_local_cache.get() : nullptr;
110+
}
108111
#endif
109112

110113
FileScannerV2(RuntimeState* state, FileScanLocalState* parent, int64_t limit,

‎be/src/io/cache/cached_remote_file_reader.cpp‎

Lines changed: 53 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
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

123122
FileScannerV2ReaderLocalCache::~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-
143132
std::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-
395347
void 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
462414
Status 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

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

Comments
 (0)