Repository navigation
Conversation
ConcurrentHashJoin enforces the global max_rows_in_join / max_bytes_in_join limit at the end of addBlockToJoin by summing per-instance snapshots via getTotalRowCount / getTotalByteCount. PR ClickHouse#108938 made those snapshots lock-free atomics written and read with memory_order_relaxed, removing the per-instance mutex whose lock/unlock previously provided the acquire/release synchronization between a build thread's snapshot store and another thread's summation in the limit check. With relaxed ordering there is no happens-before, so on weak memory models (ARM) the check can observe stale-low totals, compute a sum below the limit, and skip the SET_SIZE_LIMIT_EXCEEDED throw. On x86 (TSO) the stores are effectively ordered, so the failure is arm-only. Publish the snapshots with memory_order_release and sum them with memory_order_acquire, restoring the exact happens-before the removed mutex provided. This is stlr/ldar on ARM and negligible on x86, far cheaper than the lock contention ClickHouse#108938 removed. Closes: ClickHouse#109400 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-1:20260704-182600 |
|
cc @vdimir @KochetovNicolai @m-selmi for review. Restores the acquire/release synchronization on the ConcurrentHashJoin total-count snapshots that #108938's relaxed atomics dropped, fixing the arm-only global JOIN limit miss (#109400). |
|
Workflow [PR], commit [28d4507] Summary: ❌
AI ReviewSummaryThis PR tries to fix the Findings❌ Blockers
Final Verdict
|
| size_t res = 0; | ||
| for (const auto & hash_join : hash_joins) | ||
| res += hash_join->total_rows.load(std::memory_order_relaxed); | ||
| res += hash_join->total_rows.load(std::memory_order_acquire); |
There was a problem hiding this comment.
memory_order_acquire here still does not restore the pre-#108938 correctness guarantee for the global limit check. Before that PR, getTotalRowCount / getTotalByteCount took hash_join->mutex, so the summation blocked until each slot's active writer finished and then read the current HashJoin totals. With the snapshot atomics, the load can still observe the previous snapshot value even after another thread has already published a newer one, so two concurrent builders can still both miss each other's final slot update and return success while the combined total is already over the limit.
A concrete interleaving is:
- slot 0's last writer stores
1000, slot 1's last writer stores1000 - thread A's final check sees
{slot0 = 500, slot1 = 1000} - thread B's final check sees
{slot0 = 1000, slot1 = 500}
Both calls pass even though the true total is 2000, so #109400 can still reproduce. To actually close the regression, the final global check needs a read path that waits for or verifies the latest publication from every slot before accepting the sum (for example by keeping the mutex-based summation for the limit check, or by adding a versioned stable-snapshot protocol), not just acquire/release ordering.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 25/26 (96.15%) · Uncovered code |
|
closed in favor of #109488 |
Closes: #109400
Related: #108938
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the global
max_rows_in_join/max_bytes_in_joinlimit not being enforced for theparallel_hashjoin algorithm on ARM, where a query withjoin_overflow_mode = 'throw'could silently succeed instead of raisingSET_SIZE_LIMIT_EXCEEDED.Description
ConcurrentHashJoinenforces the globalmax_rows_in_join/max_bytes_in_joinlimit at the end ofaddBlockToJoinby summing per-instance snapshots viagetTotalRowCount/getTotalByteCount. Forparallel_hashthe right side is split across buckets, so no single instance reaches the limit; only the sum does, so enforcement depends entirely on that summation reading the committed totals.#108938 made those snapshots lock-free atomics written and read with
memory_order_relaxed. The previous code took each instance'smutexin the getters, and it was that lock/unlock which provided the acquire/release synchronization between a build thread's snapshot store and another thread's summation in the limit check. With relaxed ordering there is no happens-before, so on weak memory models (ARM) the check can observe stale-low totals, compute a sum below the limit, and skip thethrow. On x86 (TSO) the stores are effectively ordered, so the failure is arm-only.Fix: publish the snapshots with
memory_order_releaseand sum them withmemory_order_acquire, restoring the exact happens-before the removedmutexprovided. This isstlr/ldaron ARM and negligible on x86, far cheaper than the lock contention #108938 removed. The performance win of #108938 is preserved (still lock-free).Regression coverage already exists:
04061_spilling_hash_join_overflow_limitstest 3 (parallel_hash+max_rows_in_join+join_overflow_mode='throw'), which fails onarm_binaryon master and passes with this change.CI failure (master, arm_binary): https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=e2368f7c094207fee27d342463ae98a6977d79bf&name_0=MasterCI&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29