Skip to content

[Bug](file-cache) file_cache_query_limit_bytes is ignored by INSERT INTO ... SELECT and by prefetch_range #71027

Backlog

Description

@matanper

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-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. Callers include segment reads with enable_query_segment_file_cache_prefetch = true and 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_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, because file scans attach the limiter only for SELECT (file_scan_io_context.h:30). That same check means INSERT 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?

  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:
    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 keep the limiter alive for the prefetch task (e.g. the query holds it through a shared_ptr the 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

Activity

  1. Doris-Breakwater commented on Nov 25, 2026

    @Doris-Breakwater
    Collaborator

    Breakwater-GitHub-Analysis-Slot: slot_b7b06c4e91cd

    Source inspection confirms both ways of bypassing the scan cache-write budget at master 7dd131e514c and on branch-4.1 at 1330a0785d1. 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 backport

    • Native INSERT INTO … SELECT. The FE ships the session limit in the query options and sets the insert's query type to LOAD. The BE only constructs the limiter for SELECT, even when the limit is explicitly zero. The native source still uses READER_QUERY, so BetaRowsetReader takes the query-reader path but gets a null limiter. Cold source reads are therefore eligible for ordinary read-through caching. Extending limiter construction to LOAD addresses this path. The external-file scan helper still gates attachment on SELECT, so please include an external-file load as a regression control. FE insert · BE limiter · Native scan · External scan
    • Cache-populating prefetch. prefetch_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 caller

    Please open the PR with the BE tests and include two cold-source checks. First compare SELECT and INSERT … SELECT with segment prefetch off. Then exercise a standalone SELECT with 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_total only 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions