Repository navigation
Cap max_threads in the DistributedQueryTest gtest - #113597
PedroTadim merged 2 commits into
Conversation
`DistributedQueryTest.ShuffleHashJoin` left `max_threads` unset, so it resolved to auto (the runner's core count). Each distributed task runs its own pipeline, so that value is multiplied by the task count: the test creates 875 threads under TSAN where 101 suffice, and it twice consumed the whole remaining budget of the `Unit tests (tsan)` job and was killed at the 45-minute mark. Because gtest writes its JSON report only after the binary exits, a kill loses the result of every test in the run, not just this one: on the two failing runs the arm reported no pass count at all while the asan_ubsan and msan arms reported 19531 and 19527 passing tests. The failure mode is a timeout rather than a deadlock. Over 30 days this test's tsan duration has a median of 176 ms but a maximum, among runs that PASSED, of 1445260 ms (24.1 minutes); the asan_ubsan and msan arms never exceed 736 ms. Thread population is what drives that tail: TSAN has 255 usable thread slots (kThreadSlotCount in tsan_defs.h), and once every slot reaches its last epoch the runtime performs a stop-the-world DoReset which locks all 256 slot mutexes. In both failing runs 35 threads, including the main thread, were parked in __tsan::SlotAttachAndLock, and the binary had created roughly 16880 threads by the time this test ran. Setting `max_threads` to 1 removes the pipelines whose width is derived from it. Measured over one run with a breakpoint on every PipelineExecutor pool creation, 8 of the 14 task pipelines drop from 192 threads to 1; the other 6 are 4 wide on both arms, because their width comes from the 4 shuffle buckets rather than from `max_threads` and QueryPlan::setMaxThreads is never called on a task pipeline. Threads created per run fall from 875 to 101 under TSAN and from 523 to 104 in a debug build. This does not weaken what the test covers. The number of stages, tasks, exchanges and buckets is unchanged, both the Persisted and Streaming exchange kinds still run, the test still runs on every sanitizer arm with no no-tsan tag, and each task still gets its own thread from startTask, so cross-task concurrency is preserved: the count of DistQueryTask threads is 14 before and after. `max_threads = 1` for the same reason is already the convention for this subsystem in 04628_distributed_plan_local_execution_merge_no_deadlock.sql. Validation. Interleaved A/B, 20 runs per arm in a debug build: 20/20 pass on both arms, and the ranges do not overlap, 246-1055 ms before against 48-322 ms after. Interleaved A/B under TSAN, 6 runs per arm: 12/12 pass, 0 ThreadSanitizer warnings on either arm, 914-1773 ms before against 153-420 ms after. Whole binary under TSAN with the job's own gdb launcher, excluding the S3 suites that need a MinIO endpoint unavailable locally: 19470 tests ran on both arms with 0 failures and 0 errors, in 1747 s before and 1732 s after, and this test itself went from 0.932 s to 0.1 s.
Follow-up to the previous commit on this branch, which set `max_threads = 1`. That value fixes the thread fan-out but also collapses the parallel hash join to a single shard, silencing the cross-shard code paths that a test named `ShuffleHashJoin` exists to cover. `max_threads` reaches `ConcurrentHashJoin`'s shard count on the task side: `JoinStepLogical::buildPhysicalJoin` passes `optimization_settings.max_threads` into `JoinAlgorithmParams` (`JoinStepLogical.cpp:1476`), `chooseJoinAlgorithm` forwards it as `slots_` (`PlannerJoins.cpp:1246-1251`), and the constructor computes `slots = toPowerOfTwo(min(slots_, 256))` (`ConcurrentHashJoin.cpp:189`). Four loops of the form `for (i = 1; i < slots; ++i)` therefore stop iterating at one shard: `:236` shares one `StoredColumnsIndex` across shards, `:783` merges the two-level map buckets, `:826` merges the per-shard right-side nullmaps into shard 0, and `:862` copies the common used-flags back. Measured by printing `slots` and each loop's iteration count, one run per arm, three binaries differing only in this line: max_threads slots iterations per loop auto (192) 256 2040 4 4 24 1 1 0 4 also matches the shuffle bucket count, which is already the width of the six task pipelines whose width does not derive from `max_threads`, so all fourteen pipelines end up four wide. The hang fix is preserved: thread creations per run under TSAN are 875-879 before and 112-115 after, `DistributedQueryTest.*` passes with no ThreadSanitizer warnings, and the whole binary under the job's own launcher runs 19470 tests with no failures in 1696 s, this test taking 0.093 s. Two claims from the previous commit message are corrected here. The pre-fix width of 192 is the host's physical core count, because `getNumberOfCPUCoresToUseImpl` replaces `hardware_concurrency` with `physical_concurrency` above 64 logical cores (`getNumberOfCPUCoresToUse.cpp:170-183`); pinning the process with `taskset` does not change it, since `sysconf(_SC_NPROCESSORS_ONLN)` returns the same value with and without the pin. And the interleaved debug durations overlap at the edges rather than being strictly separated, so that result is stated as a median ratio; only the TSAN ranges are non-overlapping.
Internal second-model review (click to expand)Two independent reviews of this change: my own cold read of the resulting code, and a
Also checked and unchanged: the change is 3 lines in one test file, no production source; the Session id: cron:clickhouse-review-slot-55:20260805-235300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20260805-223800 |
|
cc @davenger, could you review this? The gtest left |
|
Workflow [PR], commit [4f8dec4] Summary: ✅
AI ReviewSummaryThis PR caps Final Verdict
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 3/3 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units185 translation units recompiled, 583 s compile time in total, 71 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. Translation units without a recent master baseline:
Symbol sizes
|
CI finish ledger - 4f8dec4No failure to own on this head. All 175 check-runs completed with 151 CIDB agrees for this commit: 0 rows with Session id: cron:our-pr-ci-monitor:20260807-213000 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
DistributedQueryTest.ShuffleHashJoinleftmax_threadsunset, so it resolved to auto, which ona large host is the physical core count (192 here). Every distributed task runs its own pipeline,
so that value gets multiplied by the task count. It twice consumed the remaining budget of the
Unit tests (tsan)job and was killed at the 45-minute mark. Because gtest writes its JSON reportonly after the binary exits, a kill loses the whole run's result: on those two runs the tsan arm
reported no pass count at all, while asan_ubsan and msan reported 19531 and 19527 passing tests.
The failure is a timeout, not a deadlock. Over 30 days this test's tsan duration has a median of
176 ms but a maximum among passing runs of 1445260 ms (24.1 min); asan_ubsan and msan never
exceed 736 ms. Thread population drives that tail: TSAN has 255 thread slots and does a stop-the-
world
DoResetonce they are exhausted. In both failing runs 35 threads including the main threadwere parked in
__tsan::SlotAttachAndLock.Setting
max_threads = 4bounds the pipelines whose width derives from it: measured at everyinitializeExecutioncall, all 14 task pipelines become 4 wide instead of 8 at 192 plus 6 at 4,and threads created per run fall from ~877 to ~113 under TSAN.
4 rather than 1, because
max_threadsalso sizesConcurrentHashJoin's shard count on the taskside, and at 1 that collapses to a single shard, so the cross-shard merge paths a test named
ShuffleHashJoinshould cover stop executing. Measured shard-loop iterations per run: 2040 atauto, 24 at 4, 0 at 1. Coverage is otherwise unchanged: same stages, tasks, exchanges and bucket
count, both exchange kinds run, no
no-tsantag, and each task keeps its ownstartTaskthread(14
DistQueryTaskthreads either way).Validation
All arms pinned to 32 CPUs, which is required on the host used here because
unit_tests_dbmsotherwise exhausts
RLIMIT_MEMLOCKcreating one io_uring per CPU. Interleaved A/B so box loadcannot explain the difference.
ConcurrentHashJoinshardsDistQueryTask(TSAN)The TSAN ranges are non-overlapping; the debug ranges overlap at the edges (7 of 20 before at
or below the after maximum), so the debug result is given as a median ratio.
The whole-binary runs exclude the
ReadBufferFromS3Test/*S3*suites, which abort identicallyon both arms without a local MinIO endpoint. In both whole-binary runs the gtest JSON report
was written, which is the artifact a killed run loses.
Residuals, unchanged by this PR:
PipelineExecutor's upscaling drain loop(
PipelineExecutor.cpp:429-434) has no cancellation check or backoff and held 15-17 spinningthreads in the failing runs; and
startTaskspawns an unpooledstd::threadper task, so slotpressure still scales with fan-out. This reduces this test's contribution to TSAN slot
pressure; it does not make the binary immune.
Version info
26.8.1.1002(included in26.8and later)