Repository navigation
Description
Search before asking
- I had searched in the issues and found no similar issues.
Version
master (7dd131e), also branch-4.1 (since #66479).
be/src/runtime/query_context.cpp:132-136: the per-queryRemoteScanCacheWriteLimiteris built only whenquery_options.query_type == TQueryType::SELECT.be/src/io/cache/cached_remote_file_reader.cpp:1676-1702:prefetch_range()copies the caller'sIOContextand then setsremote_scan_cache_write_limiter = nullptr.
What's Wrong?
-
INSERT INTO ... SELECTruns its fragments withTQueryType::LOAD, soQueryContextnever builds the limiter for it, even when the session setsfile_cache_query_limit_bytes. The OLAP scan of the SELECT side (olap_scanner.cpp,beta_rowset_reader.cpp) then attaches a null limiter,use_remote_only_on_cache_miss()incached_remote_file_reader.cppis false, and every remote miss is inserted into the file cache (or queued for an async cache write). The limit is silently ignored.On a large
INSERT INTO ... SELECTcopy of cold data through a much smaller file cache, this makes the copy churn the whole cache: the hit rate is near zero, cache writes run at hundreds of MB/s per BE, and hundreds of scanner and compaction threads per BE wait on theBlockFileCachemutex, whileremote_scan_no_write_file_cache_query_totalstays at 0. -
CachedRemoteFileReader::prefetch_range()drops the limiter (the prefetch task may outlive the query), so a prefetched range is read through and written into the cache even when the query hasfile_cache_query_limit_bytes = 0or has used up its budget. Callers include segment reads withenable_query_segment_file_cache_prefetch = trueand the format_v2 Parquet scanner, which prefetches every column chunk of a selected row group.
VARIANT sub-column readers are not affected: HierarchicalDataIterator::init() and SparseColumnMergeIterator::init() pass the caller's ColumnIteratorOptions (and so its IOContext and limiter) to every sub-iterator, and the external column-meta readers copy the source IOContext.
What You Expected?
file_cache_query_limit_bytesapplies to the OLAP scan ofINSERT INTO ... SELECTas it does to aSELECT. Loads that only read external files (stream, broker, routine load) are unaffected, because file scans attach the limiter only forSELECT(file_scan_io_context.h:30). That same check meansINSERT INTO <olap table> SELECT ... FROM <external catalog>still ignores the limit; covering it would need the file-scan check to distinguish INSERT ... SELECT from other loads.- A prefetch does not write what the query's own reads would not write: it is skipped when the query is remote-only on miss, and otherwise each block it actually writes on a miss is charged to the query's budget, exactly as a normal read is.
How to Reproduce?
- A cloud-mode cluster with
enable_file_cache = trueon the BEs. - Create
t1with enough data that its segments are not in the cache (or clear the cache), and an emptyt2with the same schema. - Run:
SET file_cache_query_limit_bytes = 0; INSERT INTO t2 SELECT * FROM t1;
- On a BE, compare bvars before and after (
curl http://<be>:<webserver_port>/vars):- the file-cache write bvars (for example the cache's written-bytes counters) grow by roughly the size of
t1; remote_scan_no_write_file_cache_query_totalstays unchanged.
- the file-cache write bvars (for example the cache's written-bytes counters) grow by roughly the size of
- Run
SELECT * FROM t1with the same setting after clearing the cache: no cache writes, as expected.
Anything Else?
Proposed fix:
query_context.cpp: build the limiter forTQueryType::LOADas well asSELECT.prefetch_range(): return early whenuse_remote_only_on_cache_miss(io_ctx)is true; otherwise keep the limiter alive for the prefetch task (e.g. the query holds it through ashared_ptrthe task also references) so the normal per-missed-block charging applies. Charging the whole range up front is wrong: it also charges bytes already in the cache, double-charges bytes the query reads itself, and one refused charge would flip the whole query to remote-only.
Are you willing to submit PR?
- Yes I am willing to submit a PR! We have a patch with BE unit tests and can open a PR.
Code of Conduct
- I agree to follow this project's Code of Conduct
Activity
Doris-Breakwater commentedon Nov 25, 2026
Breakwater-GitHub-Analysis-Slot: slot_b7b06c4e91cd
Source inspection confirms both ways of bypassing the scan cache-write budget at master
7dd131e514cand on branch-4.1 at1330a0785d1. Both paths are also present in the branch-4.1 backport commit. The reported write rate and mutex contention still need runtime evidence. 4.1 backportINSERT INTO … SELECT. The FE ships the session limit in the query options and sets the insert's query type toLOAD. The BE only constructs the limiter forSELECT, even when the limit is explicitly zero. The native source still usesREADER_QUERY, soBetaRowsetReadertakes the query-reader path but gets a null limiter. Cold source reads are therefore eligible for ordinary read-through caching. Extending limiter construction toLOADaddresses this path. The external-file scan helper still gates attachment onSELECT, so please include an external-file load as a regression control. FE insert · BE limiter · Native scan · External scanprefetch_range()queues a dry-run read that forces synchronous cache population and clears the borrowed limiter pointer. Check the caller's no-write state before submitting and keep any limiter used by the worker alive for the duration of the task. For positive budgets, charging raw range length can undercount block-aligned misses and overcharge hits or overlapping requests. A refused admission also permanently switches the limiter to remote-only mode, so an oversized speculative request can force later demand reads into no-write mode while budget remains. Please test accounting against newly admitted cache blocks. The segment flag gates the native query caller. Native Parquet also calls this helper and is worth covering if the patch changes the shared path. Prefetch worker · Block admission · Parquet callerPlease open the PR with the BE tests and include two cold-source checks. First compare
SELECTandINSERT … SELECTwith segment prefetch off. Then exercise a standaloneSELECTwith segment prefetch on, using zero and a small positive limit. Please include the exact FE and BE builds, effective cache and prefetch settings, and source-segment cache writes or newly populated blocks before and after each run. Account for destination-writer cache fills and background activity when using BE-wide deltas. A BE thread dump from the same interval would help verify the mutex contention. Dry-run prefetch skips the normal read-profile update, so use cache-block state or another write-side measurement to verify it.remote_scan_no_write_file_cache_query_totalonly counts a transition on rejected admission. A zero limit starts in remote-only mode and leaves that counter unchanged even when it works. Read statistics · Counter semantics