Repository navigation
Conversation
A compressed block is counted once however many times it is loaded, so the counted ranges of each file are kept until the query ends. A read that skips many granules leaves one range per run of granules read, per column, per part: 48 bytes each in the `std::map`, about 250 bytes per file, so a sparse scan of a billion rows and a hundred columns needs about 300 MB. The new setting `automatic_parallel_replicas_max_memory_for_input_statistics` (256 MiB by default) limits that per query. The memory is accounted in the statistics block every reads of the query share, once per reader per step. Past the limit the reads stop counting and free their ranges, and the cache remembers that the input was not measured: the next run declines parallel replicas from it, counted in `AutoParallelReplicasRejectedByUnmeasuredInput`, without collecting the statistics again until the rows read drift. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The limit was checked when a reader committed its batch, after the maps of all its streams had grown, and a file looked up once counting stopped still got an entry. Now each stream's `take` is checked beforehand against what it can add at most, a node per range, and a file is added only within the limit: past it `getCountedRanges` gives out an empty `CountedRanges` that is never counted into. The entries readers already point to stay, emptied by the next `count`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`modernize-use-nodiscard` flags them in every translation unit that includes the header. The PR changed the header, so the clang-tidy cache no longer hid them. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=127586&sha=141c98b0e47f6cb31f8858d2d53c64949e2e624a&name_0=PR #127586 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…er instead of their memory The check before a map grew compared the committed total, so concurrent readers could all pass it and then commit past the limit together, and the ranges pending in the buffers were outside the limit. Now the limit is `automatic_parallel_replicas_max_counted_ranges`, a number of ranges (about 48 bytes each) where a counted file takes a unit too, by default 4 million. A reader reserves what a stream's ranges can add at most before taking them, and releases the rest when it commits its batch; a reservation that does not fit stops the counting. Once it stopped, the readers detach their sinks and free the pending ranges, and a reader enabled later attaches none. The buffers extend the last pending range with a block that continues it or that it covers already, so reading on and loading a block again do not grow the pending ranges. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The compact tables of `05443` and `05452` are created from their wide counterparts, keeping only the settings that make them compact. The assertions of `05452` and `05838` take their aliases from `WITH` instead of a subquery. Comments that repeated the accounting are shortened, and the comment on the columns cache in `05443` says why it is still disabled: a reader writing to it reads the values without the string filter. `Readers::countCompressedBytesRead` had one caller and is inlined into it, and `RuntimeDataflowStatistics.h` no longer includes headers for the removed input sampling. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…to fix-stats-for-string-filters
# Conflicts: # src/Processors/QueryPlan/Optimizations/RuntimeDataflowStatistics.h # src/Processors/QueryPlan/Optimizations/considerEnablingParallelReplicas.cpp
# Conflicts: # src/Compression/CachedCompressedReadBuffer.cpp # src/Compression/CompressedReadBufferBase.h # src/Compression/CompressedReadBufferFromFile.cpp # src/Processors/QueryPlan/Optimizations/RuntimeDataflowStatistics.h # src/Storages/MergeTree/IMergeTreeReader.h # src/Storages/MergeTree/MergeTreeReaderCompactSingleBuffer.cpp # src/Storages/MergeTree/MergeTreeReaderStream.cpp # src/Storages/MergeTree/MergeTreeReaderStream.h # src/Storages/MergeTree/MergeTreeReaderWide.cpp
Revert before this PR is retargeted to master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [4a331bd] Summary: ❌
AI ReviewSummaryThis PR limits the compressed-block ranges retained while automatic parallel replicas measure query input, remembers an abandoned measurement, and replaces the range maps with vectors merged in bulk. Reviewed Findings
Tests
Final VerdictStatus: Bound pending and registered allocation growth before it happens, and add focused coverage for exhaustion after collection starts. LLVM Coverage ReportMeasured on commit 4a331bd.
Changed lines: Changed C/C++ lines covered: 282/311 (90.68%) · Uncovered code |
The workflow edit in 8e9803d failed the `Check Workflows` job, which regenerates the workflows from `ci/workflows`. Set the base branches in the source instead. Revert before this PR is retargeted to master. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=133777&sha=latest&name_0=PR Related: #133777 Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # src/Storages/MergeTree/MergeTreeReaderStream.cpp # src/Storages/MergeTree/MergeTreeReaderWide.cpp
…arallel_replicas_max_counted_ranges` to 10M A `std::map` node takes 48 bytes for the 16 bytes of a range; `absl::btree_map` keeps a range in about 18-23 bytes with about 15 times fewer allocations. In a benchmark of `ReadRanges::add` on one core: - 10k ranges per file: 5-50% slower than `std::map` (both in cache); - 500k ranges per file: up to 2.4x faster (random order: 535 -> 218 ns per range). The range that goes on from where the last one ended, the common case, is now extended without a lookup: 9.8 -> 2.3 ns per range with `std::map`, 25.6 -> 8.2 ns with the B-tree. The limit is raised to keep about the memory it allowed before, ~200 MB. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n a flat hash map With 1M files of ~105-character paths, the `std::unordered_map<String, ReadRanges>` took ~261 bytes and 3 allocations per file; `absl::flat_hash_map<UInt128, std::unique_ptr<ReadRanges>>` takes 137-167 bytes and 2 allocations, and the lookup is a bit faster (405 -> 381 ns per file at 1M files, 102 -> 74 ns at 10k). Co-Authored-By: Claude Opus 5.5 <[email protected]>
… keeping them in a B-tree A range is appended, or extends the last one in place; the appended ranges are sorted and merged into the others once they are as many, or before the vector would grow. Their bytes are counted when they are merged, and the ranges left unmerged when the registry is destroyed, which is before the statistics are recorded since it holds their block. In a benchmark of the same workloads on one core, per range: - 10k ranges per file: 2-10x faster than the B-tree (e.g. gaps, 16 streams: 94 -> 28 ns); - 500k ranges per file: 2.5-6x faster (random order: 212 -> 77 ns); - 16 bytes kept, 25-39 at the peak, against 18-22 for the B-tree. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes121 object files changed (+53.36 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units732 translation units recompiled, 4981 s compile time in total, 732 of them have a recent master baseline. |
…the limit, and the arguments of a unit test `setReadRangesRegistry` takes `ranges_read_mutex` under the lock of the streams, and `registerReadRanges` took the lock of the streams under `ranges_read_mutex` to drop the sinks once the counting stopped: TSan reported a lock-order inversion. The sinks are now dropped with `ranges_read_mutex` released. The test of the undecided queries passed its key as `max_counted_ranges_`, the argument inserted before it. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=133777&sha=latest&name_0=PR Related: #133777 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ge a file 8 units of it On ClickBench (`hits`, one part of 100M rows), the most a query takes of the limit is 3,511 units (q23, where the filter leaves 3,462 separate ranges), and most take 15-330. 1M leaves room for about 300 times that while keeping the ranges within 16-32 MB. A file takes about as much memory as 8 ranges, so it now takes 8 units instead of 1. The description of the setting no longer explains how the ranges are kept. Also from the review: the files looked up once the counting stopped share one `not_registered`, which `registerRanges` no longer adds ranges to, and the reservation is checked not to underflow. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ty the ranges hold, and free them all when the counting stops The units given back when ranges merged were by the size of the vector, which keeps its capacity: a file whose ranges collapsed returned its budget while still holding the memory, and over many files the memory could grow past the limit. The limit is now charged by what the capacity grows by after each registration, and nothing goes back on a merge; a query can go over by one vector growth at most. When the counting stopped, only the file at hand was freed, and the ranges of the others stayed until the registry was destroyed. Now each registry frees the ranges of all its files once it sees the stop, keeping the files the readers refer to, and frees the file a registration after the stop is given. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
|
||
| -- The first counted file takes more units than the limit. | ||
| SELECT key % 10 AS k, max(value) FROM t_autopr_input_ranges GROUP BY k FORMAT Null | ||
| SETTINGS automatic_parallel_replicas_max_counted_ranges = 1, log_comment = 'autopr_input_ranges_collect'; |
There was a problem hiding this comment.
[Major] Cover range-capacity exhaustion after collection starts
With automatic_parallel_replicas_max_counted_ranges = 1, getReadRanges rejects the first file because units_per_file is 8 and returns not_registered. Consequently this test never adds a range to the registry or reaches the capacity-growth check in ReadRangesRegistry::registerRanges; deleting reserve(grown) would leave it passing. The existing input-byte tests use the default budget and do not exercise exhaustion either.
Please add a focused case whose budget admits the measured files and some ranges, then exhaust it with disjoint compressed ranges. Check that the query completes and the next execution reports AutoParallelReplicasRejectedByUnmeasuredInput, with an unlimited-budget control that successfully measures the same read. This would protect the range-storage limit that is the main behavior of this change.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The memory automatic parallel replicas use to measure the input bytes of a query is limited by the new setting
automatic_parallel_replicas_max_counted_ranges. Past it the query is not measured, and the statistics remember that, so automatic parallel replicas are not enabled for it.Stacked on #127586, which measures the input bytes as the compressed blocks the readers load, each block once. The ranges registered for that in the
ReadRangesRegistryare kept per file for the whole read, so a read over many files, or with a sparse filter, could keep many of them.The ranges and files of all the reads of a query are now limited by
automatic_parallel_replicas_max_counted_ranges(1'000'000 by default, 0 means no limit; a file takes 8 units, about the memory of 8 ranges). Each batch of ranges is reserved from a query-wide atomic counter before it is kept, so the counter is updated once perregisterReadRanges, not once per range. Past the limit the readers stop collecting the ranges and free what they registered, and the statistics are cached withinput_not_measured, so the next run does not collect them again:considerEnablingParallelReplicasdeclines and countsAutoParallelReplicasRejectedByUnmeasuredInput.The registry also takes less memory and time than in #127586:
std::mapnode, and adding one is 2-10x faster than with a B-tree, which was tried too (absl::btree_map: 18-22 bytes, but slower thanstd::mapuntil the ranges no longer fit in the cache).sipHash128of the path in anabsl::flat_hash_map, not by the path in astd::unordered_map: 137-167 bytes per file instead of 261 at 1M files, and slightly faster lookups.registerReadRangesis timed inRuntimeDataflowStatisticsInputNanoseconds, not only the merge of the ranges.The default comes from ClickBench: on
hits(one part of 100M rows) the most a query takes is 3,511 units (q23, whose filter leaves 3,462 separate ranges), and most take 15-330. 1M leaves room for about 300 times that, with the ranges within 16-32 MB.Workflow [PR]
Sync PR [sync-upstream/pr/133777]