Repository navigation
Balance the buckets of a single Parquet file read by several sources in parallel - #130740
alexey-milovidov merged 5 commits into
Conversation
A single local Parquet file is split into up to 4 * max_num_streams round-robin buckets, returned largest first (by the larger of the row share and the uncompressed-byte share), and the sources take the next bucket from an atomic counter in SplitFile instead of reading one fixed bucket each, like they take the next file of a multi-file read. WIP for the follow-up to ClickHouse#125738 (row groups of different sizes leave the source with the heaviest static bucket finishing last). Benchmarks show the 4x factor costs short scans at high thread counts (serialized per-bucket startup latency), so the design goes back to planning. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A source that takes several buckets of a split Parquet file re-opened the file for every bucket. In clickhouse-local, where storage_file_read_method = mmap by default, that is an mmap + munmap of the whole file per bucket, and these serialize on the process mmap_lock with each other and with the page faults of all query threads: with 4 buckets per stream, short scans of hits.parquet got slower with the thread count (Q1 1.28x at 96 threads), while with pread the same build was neutral or faster. The source now creates its read buffer for the first bucket it takes and keeps it until the queue is empty. The exists / stat / version-token checks still run for every bucket, so a file removed or modified during the read still fails with FILE_CHANGED_DURING_READ. The Parquet reader of a bucket reads the buffer only by offset (readBigAt, or seek before every read), and the formats of the previous bucket are destroyed before the next one is created. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The row groups alternate 1.5M and 0.5M rows, so with round-robin buckets every bucket of an even index gets only the large row groups. Measured with clickhouse-local (fresh process, 3 runs): the queue makes both queries 0.75x / 0.76x at max_threads = 8 and 0.66x / 0.70x at max_threads = 12. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tearing down the page tables of a mapping costs time per page touched, and the munmap calls of one process run one after another (each takes the process memory map lock exclusively), while MADV_DONTNEED of different mappings runs in parallel. With the sources of a split Parquet file balanced by the queue, all of them finish at the same time in clickhouse-local (storage_file_read_method = mmap by default), and their teardowns of the whole-file mapping queue up at the end of the query. Microbenchmark on the page-cached hits.parquet (N threads copy disjoint 1 MiB chunks, then all drop their mapping at once), ms: | threads, touched | own munmap each | MADV_DONTNEED, then munmap | |---|---|---| | 16, 1.7 GB | 36-59 | 4-6 | | 16, 6 GB | 180-190 | 13 | | 64, 1.7 GB | 63-67 | 11-15 | | 96, 1.7 GB | 63-74 | 17-18 | ClickBench Q21 on hits.parquet, 16 threads, bpftrace of the whole-file mappings: the query waited 25-50 ms after the last source stopped reading (sum of munmap durations 207-401 ms); with this change 5-6 ms (17 madvise calls in parallel, munmap < 1 ms in total). The mappings are PROT_READ and MAP_PRIVATE and are never written, so they have no private pages; the result of madvise is ignored, munmap still does the teardown if it fails. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop three comments that restate the code below them, shorten one to a line, and keep the existing IBucketSplitter doc's wrapping. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings) plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261111-095100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261111-085000 |
|
Workflow [PR], commit [ad6aaf2] Summary: ✅
AI ReviewSummaryReviewed #130740 at Missing context / blind spots
Final Verdict✅ Approve. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units22 translation units recompiled, 201 s compile time in total, 22 of them have a recent master baseline. |
|
On aarch64 (4-core Graviton3, 4 threads), this PR makes several full scans of the single
Of the other queries over 100 ms, only Q25 is slower (1.007 [1.001, 1.014]). Queries under 100 ms move by 1–5 ms in both directions: Q1 1.067, Q7 1.080 and Q36 1.050 are slower; Q19 0.921 and Q42 0.951 are faster (all significant). The Setup: Release builds with identical flags (clang 21, no LTO) of ad6aaf2 and its merge base c3124c7, 4-core Graviton3 with 30 GiB and Linux 7.0, a fresh |
f908404
Related: #125738
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Faster parallel reading of a single local Parquet file whose row groups differ in size: the readers take the next part of the file from a shared queue, largest first, instead of each reading a fixed round-robin share of the row groups. Threads that finish reading memory-mapped files at the same time (e.g. in
clickhouse-local, which reads files withmmapby default) no longer wait for each other to release the mappings.Description
Follow-up to #125738, as asked in #125738 (comment). There, row group
iof the file goes to bucketi % N, one bucket per source, so with row groups of different sizes the heaviest bucket is the wall time (hits.parquet: 1.19x the mean rows at 16 streams, 1.6x at 64), and CPU-bound scans got 2-6% slower (measurement).4 * max_threadsround-robin buckets, ordered largest first, and the sources take the next bucket from a shared counter. A static largest-first assignment was measured to slow down the URL queries, because rows and bytes per row group are anti-correlated. Round-robin buckets keep the row groups that survive min/max pruning spread.MMappedFileDescriptor::finishcallsmadvise(MADV_DONTNEED)beforemunmap. The balanced sources drop their whole-file mappings at the same time, and concurrentmunmapcalls tear down the page tables one after another (16 threads: 36-59 ms; withMADV_DONTNEEDfirst: 4-6 ms).Validation:
clickhouse localon x86,hits.parquet,max_threads = max_parsing_threads = T, ClickBench queries (0-based), paired geomean of PR / master. T=16 (10 rounds): Q8 0.962, Q16 0.968, Q18 0.962, Q28 0.960, Q32 0.942, Q20 0.901, Q21 0.879, Q27 0.909, all 43 queries 0.969. T=64: Q20 0.684, Q21 0.695, Q22 0.669. Q10 is 1.13 at T=16 because one thread's aggregation table crossesadaptive_aggregator_freeze_threshold_bytes(with it set to 0: 0.953). Q39 (CounterID = 62) is 1.07-1.09 at T=96. Results are identical (mmap and pread). The new performance test: 0.75 / 0.69 atmax_threads = 8.Workflow [PR]
Sync PR [sync-upstream/pr/130740]
Version info
26.11.1.1977-master(included in26.11and later)