matanper opened a new issue, #71027:
URL: https://github.com/apache/doris/issues/71027

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/doris/issues?q=is%3Aissue) and found no 
similar issues.
   
   ### Version
   
   master (7dd131e514c), also branch-4.1 (since #66479).
   
   - `be/src/runtime/query_context.cpp:132-136`: the per-query 
`RemoteScanCacheWriteLimiter` is built only when `query_options.query_type == 
TQueryType::SELECT`.
   - `be/src/io/cache/cached_remote_file_reader.cpp:1676-1702`: 
`prefetch_range()` copies the caller's `IOContext` and then sets 
`remote_scan_cache_write_limiter = nullptr`.
   
   ### What's Wrong?
   
   1. `INSERT INTO ... SELECT` runs its fragments with `TQueryType::LOAD`, so 
`QueryContext` never builds the limiter for it, even when the session sets 
`file_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()` in `cached_remote_file_reader.cpp` is 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 ... SELECT` copy 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 the `BlockFileCache` mutex, while 
`remote_scan_no_write_file_cache_query_total` stays at 0.
   
   2. `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 has `file_cache_query_limit_bytes = 
0` or has used up its budget. It is only reached with 
`enable_query_segment_file_cache_prefetch = true`.
   
   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_bytes` applies to the OLAP scan of `INSERT INTO 
... SELECT` as it does to a `SELECT`. Loads that only read external files 
(stream, broker, routine load) are unaffected: file scans attach the limiter 
only for `SELECT` (`file_scan_io_context.h`), and OLAP readers attach it only 
for `READER_QUERY`.
   - 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 its range counts against 
a limiter that still has budget.
   
   ### How to Reproduce?
   
   1. A cloud-mode cluster with `enable_file_cache = true` on the BEs.
   2. Create `t1` with enough data that its segments are not in the cache (or 
clear the cache), and an empty `t2` with the same schema.
   3. Run:
      ```sql
      SET file_cache_query_limit_bytes = 0;
      INSERT INTO t2 SELECT * FROM t1;
      ```
   4. 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_total` stays unchanged.
   5. Run `SELECT * FROM t1` with the same setting after clearing the cache: no 
cache writes, as expected.
   
   ### Anything Else?
   
   Proposed fix:
   - `query_context.cpp`: build the limiter for `TQueryType::LOAD` as well as 
`SELECT`.
   - `prefetch_range()`: return early when 
`use_remote_only_on_cache_miss(io_ctx)` is true; otherwise charge the range 
with `try_admit_cache_write()` before submitting, and skip it when refused.
   
   ### Are you willing to submit PR?
   
   - [x] Yes I am willing to submit a PR! We have a patch with BE unit tests 
and can open a PR.
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to