Skip to content

Remove current_task_last_mark from MergeTree readers - #113584

Merged
CurtizJ merged 2 commits into
ClickHouse:masterfrom
CurtizJ:remove-current-task-last-mark
Aug 7, 2026
Merged

CurtizJ merged 2 commits into
ClickHouse:masterfrom
CurtizJ:remove-current-task-last-mark

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Aug 5, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed multiplied read requests on remote disks when a step of the readers chain reads over fragmented mark ranges, e.g., ranges filtered by primary or skip indexes.

Version info

  • Merged into: 26.8.1.976 (included in 26.8 and later)

The parameter is redundant since commit fbacf3a, which keeps
`IMergeTreeReader::all_mark_ranges` in sync with the current task's
mark ranges on the reader-reuse path (`MergeTreeReadPoolBase::createTask`).
At every call site the passed value was equal to
`getLastMark(reader->all_mark_ranges)`, so the right bound of ranged
read requests on remote disks is now derived inside the reader from its
own `all_mark_ranges` (cached in the `last_mark_to_read` member).

This also fixes multiplied S3 read requests when a step of the readers
chain reads over fragmented mark ranges: `continueReadingChain` computed
the bound from the ranges started by the current read block only, not
from the whole task, so with fragmented task ranges (e.g. a skip index
applied at index analysis time plus `PREWHERE`) the bound advanced on
every read block, and every advance cancelled the buffer's prefetch and
issued a new small read request.

Patch readers are now refreshed with their per-task mark ranges on the
reader-reuse path (`MergeTreeReadTask::Readers::updateAllMarkRanges`),
because their right bound now also comes from their own
`all_mark_ranges`.

Related: ClickHouse#112296
Related: ClickHouse#112296 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [12fc5d7]

Summary: ❌

job_name test_name status info comment
Stress test (arm_asan_ubsan, s3) FAIL
Logical error: 'std::exception. Code: 1001, type: std::out_of_range, e.what() = vector (version 26.8.1.1), Stack trace: (STID: 2508-3fb1) FAIL cidb, issue ISSUE CREATED

AI Review

Summary

This PR removes the explicit current_task_last_mark plumbing from MergeTree readers, derives the remote-read right bound from each reader's own task ranges, updates reused patch readers accordingly, and adds a stateless s3_disk regression test for fragmented PREWHERE ranges. I did not find an actionable correctness, compatibility, or regression issue in the current code, and the existing PR discussion appears addressed by the latest diff.

Final Verdict

✅ No blocking issues found.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.80% +0.10%

Changed lines: Changed C/C++ lines covered: 161/174 (92.53%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Aug 5, 2026
@CurtizJ
CurtizJ marked this pull request as draft August 5, 2026 23:09
const auto & rows_per_granule = result.rows_per_granule;
const auto & started_ranges = result.started_ranges;

size_t current_task_last_mark = ReadResult::getLastMark(started_ranges);

@CurtizJ CurtizJ Aug 5, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This takes only the last mark of the currently started ranges, not the last mark of the whole read task.

If the read task contains, e.g., ranges [0; 8), [10; 16), on read of the second range, it could cause a call of

void ReadBufferFromRemoteFSGather::setReadUntilPosition(size_t position)

This causes a reset of the current read buffer and a GET request from object storage on the next read.

I think it's more correct to set the last mark as a last mark (all_mark_ranges in IMergTreeReader) of the read task, and allow ReadBuffers to decide when to start a new request in seek depending on the setting remote_read_min_bytes_for_seek.

@CurtizJ CurtizJ changed the title Remove current_task_last_mark from MergeTree readers Remove current_task_last_mark from MergeTree readers Aug 5, 2026
Comment on lines -97 to -101
/// 3. current_task_last_mark argument in readRows() (which is used only for reading from remote fs to make precise
/// ranged read requests) is different from current reader's IMergeTreeReader::all_mark_ranges.back().end because
/// the same reader can be reused between read tasks - if the new task mark ranges correspond to the same part we last
/// read, so we cannot rely on all_mark_ranges and pass actual current_task_last_mark. But here we can do prefetch for begin
/// of range only once so there is no such problem.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is outdated, because all_mark_ranges are always updated in updateAllMarkRanges when we reuse the reader between read tasks.

The 600000-row INSERT with index_granularity = 16 and 512-byte
compression blocks on `s3_disk` timed out on sanitizer builds in the
flaky check. Shrink the dataset 10x and lower `max_block_size` to 64 to
keep the number of read blocks (and therefore the number of read
requests issued by the broken code) high: measured 99 vs 7 requests
with the assertion threshold of 20.

Also pin `enable_parallel_replicas = 0` in the EXPLAIN query: with
parallel replicas forced by the ParallelReplicas jobs, the local plan is
embedded into a boxed subplan and the `Granules:` line gets a border
prefix, which broke the reference.

CI report: ClickHouse#113584

Co-Authored-By: Claude Fable 5 <[email protected]>
@CurtizJ
CurtizJ marked this pull request as ready for review August 6, 2026 09:32
@kssenii kssenii self-assigned this Aug 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 36 queries analysed

This is a read-path refactor: it caches the last mark to read and bounds remote-filesystem ranged read requests for fragmented mark ranges (PREWHERE plus skip indexes), leaving the query-execution compute path untouched. Both flagged queries (Q28, Q32) are already not_sure from mixed test signals, and CPU time is essentially flat for both (15297 vs 15420 ms, 18447 vs 18397 ms), so the ~6% wall-clock moves read as I/O run-to-run variance rather than a compute regression. No consistent directional signal here; nothing to act on.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 28 not_sure 1036 1096 +5.9% 0.0002 io: This PR only reshapes how the remote-read right bound is computed in the MergeTree reader; CPU is flat here (change_factor +5.9%), so the mixed test signals read as I/O variance rather than a real compute regression.
⚠️ 32 not_sure 1308 1388 +6.1% 0.0234 io: CPU time is essentially unchanged (change_factor +6.1%), and this read-path bound refactor doesn't touch Q32's compute, so the borderline wall-clock move looks like I/O run-to-run variance.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 7a2168c0-8bc2-411f-bcd3-a4881ecad82f
  • MIRAI run: 7bade89b-4750-452e-918c-4afd22b41a69
  • PR check IDs:
    • clickbench_159524_1786103830
    • clickbench_159530_1786103830
    • clickbench_159541_1786103830
    • tpch_adapted_1_official_159558_1786103830
    • tpch_adapted_1_official_159567_1786103830
    • tpch_adapted_1_official_159595_1786103830

@CurtizJ
CurtizJ added this pull request to the merge queue Aug 7, 2026
Merged via the queue into ClickHouse:master with commit a7cd483 Aug 7, 2026
178 of 180 checks passed
@CurtizJ
CurtizJ deleted the remove-current-task-last-mark branch August 7, 2026 16:54
CurtizJ added a commit to CurtizJ/ClickHouse that referenced this pull request Aug 7, 2026
Test `04812_prewhere_fragmented_ranges_s3_read_requests` now runs the same
`PREWHERE` query twice: with the mark ranges of a read task fragmented by
index analysis, and with them fragmented by the ranges refiner in read pools
(`use_indexes_refiner_in_read_pools`). Both cases assert that the number of
S3 read requests stays low; the refiner case also asserts that the refiner
did drop marks, so it cannot silently degrade into the analysis path.

Checked against the release build of the last commit without the fix for read
fragmentation (`2de0d16b63b`): the analysis case issues 99 S3 read requests
and the refiner case 189 (with 3375 marks dropped by the refiner), versus 7
and 3 with the fix.

Test `04654_skip_indexes_refiner_remote_disk` is removed. It asserted the
shrink-only refinement of ranges of parts on remote disks, which was dropped
in `c8fcfdd09cc` because the right bound of ranged read requests now spans
the whole read task.

Build report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112296&sha=2de0d16b63bf36975d10da3068a0d9fc025c7825&name_0=PR&name_1=Build+%28amd_release%29
Related: ClickHouse#112296
Related: ClickHouse#113584

Co-Authored-By: Claude Opus 5 <[email protected]>
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants