Skip to content

Reduce lock-contention from getTotalByteCount in ConcurrentHashJoin - #108938

Merged
m-selmi merged 2 commits into
ClickHouse:masterfrom
m-selmi:reduce-lock-contention-in-concurrent-hash-join
Jul 3, 2026
Merged

m-selmi merged 2 commits into
ClickHouse:masterfrom
m-selmi:reduce-lock-contention-in-concurrent-hash-join

Conversation

@m-selmi

@m-selmi m-selmi commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

getTotalByteCount (and getTotalRowCount) in ConcurrentHashJoin loop over all HashJoin instances and lock them to extract the total. This leads to high lock contention when such a function is called frequently (this is the case for SpillingHashJoin that checks the byte limit on each block).
The contention can add a lot of overhead and make queries flaky.

For query 9 from TPC-H at sf100 for 7 queries runtime (warm) fluctuates between 2690ms and 2966ms (getTotalByteCount taking between 164ms and 347ms).
image
image

This change records the current totals of each hash join instance during insert while the lock is already held and makes getTotalByteCount (and getTotalRowCount) lock-free.

TPC-H q9 runtime improves to 2.4~2.5ms
image

Changelog category (leave one):

  • Performance Improvement

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

Reduce the overhead coming from lock-contention in parallel_hash join algorithm.

Version info

  • Merged into: 26.7.1.474 (included in 26.7 and later)

@m-selmi m-selmi added the can be tested Allows running workflows for external contributors label Jun 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [84e78c8]

Summary: ✅


AI Review

Summary

This PR replaces locked aggregation of ConcurrentHashJoin totals with per-slot atomic snapshots to reduce contention in getTotalByteCount and getTotalRowCount. The direction is reasonable for read-only diagnostics, but the same totals are used as a control input for SpillingHashJoin, where stale snapshots can skip the pre-insert spill decision while another slot is already growing.

Findings

⚠️ Majors

  • [src/Interpreters/ConcurrentHashJoin.cpp:470] SpillingHashJoin relies on ConcurrentHashJoin::getTotalByteCount as the pre-insert max_bytes_before_external_join gate, but the new counter is only written after the slot insert returns. A caller can therefore read the previous snapshot while another thread has already grown a slot under its mutex, skip switchToGraceHashJoin, and keep building in memory even though the old locked implementation would have waited for that slot and observed the completed insert before making the spill decision.
    Suggested fix: keep an exact locking total path for the spill threshold decision, or account for in-flight/reserved bytes before mutating the slot so the pre-insert gate cannot observe a stale lower total.
Tests
  • ⚠️ Please add a focused concurrent SpillingHashJoin(ConcurrentHashJoin) regression or stress test that proves in-flight slot growth cannot bypass the half-threshold spill trigger for max_bytes_before_external_join.
Final Verdict

Status: ⚠️ Request changes

Minimum required action: fix the spill-threshold accounting so SpillingHashJoin does not make its pre-insert switch decision from stale ConcurrentHashJoin snapshots.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jun 30, 2026
std::lock_guard lock(hash_join->mutex);
res += hash_join->data->getTotalByteCount();
}
res += hash_join->total_bytes.load(std::memory_order_relaxed);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

SpillingHashJoin still uses ConcurrentHashJoin::getTotalByteCount as the pre-insert spill gate, but this now returns only the last completed snapshot for each slot. While another thread holds a slot mutex inside ConcurrentHashJoin::addBlockToJoin, that slot's inner HashJoin may already have grown but total_bytes is not stored until after the insert returns, so this load can undercount exactly when the wrapper decides whether to switch to GraceHashJoin. The old implementation waited on the slot mutex and observed the completed insert before making that decision. With max_bytes_before_external_join, a concurrent inserter can miss the half-threshold trigger and later promote the join in memory if the final total stays below the full cap. Please keep an exact/locking path for the spill decision or account for in-flight bytes before inserting.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Couldn't this still happen with the old implementation? Multiple threads could perform ConcurrentHashJoin::getTotalByteCount, see a pre-insert counters, then start inserting concurrently?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, the old code could still miss a group of callers that all completed the pre-insert check before any of them started inserting. The difference is that the old getTotalByteCount then blocked on any slot already being mutated and observed that completed insert before deciding whether to switch; with the cached value, a caller can start its check after another thread has already grown a slot and still read the previous snapshot until that insert returns and stores total_bytes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

wouldn't the worst case be the same? before we missed blocks that did not yet start insert, now we miss blocks that have not yet finished insert, but the same blocks could be in either state

@clickhouse-gh

clickhouse-gh Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 1ea20e3a-12e0-479a-b24e-a9229184fa33
  • MIRAI run: 8572fcdc-c48d-4b9e-a975-3d70e316752b
  • PR check IDs:
    • clickbench_528675_1782847025
    • clickbench_528681_1782847025
    • clickbench_528691_1782847025
    • tpch_adapted_1_official_528698_1782847025
    • tpch_adapted_1_official_528712_1782847025
    • tpch_adapted_1_official_528727_1782847025

@clickhouse-gh

clickhouse-gh Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.50% 85.50% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 77.80% 77.70% -0.10%

Changed lines: Changed C/C++ lines covered: 25/26 (96.15%) · Uncovered code

Full report · Diff report

@m-selmi
m-selmi marked this pull request as ready for review July 1, 2026 08:09
@antaljanosbenjamin antaljanosbenjamin self-assigned this Jul 1, 2026

@antaljanosbenjamin antaljanosbenjamin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this should be fine. In a perfect world temporary overshoot of memory is not acceptable, but as we already had the chance to overshoot I think it would be a mistake to block this PR because of that.

@m-selmi
m-selmi added this pull request to the merge queue Jul 3, 2026
Merged via the queue into ClickHouse:master with commit c575978 Jul 3, 2026
342 of 344 checks passed
@m-selmi
m-selmi deleted the reduce-lock-contention-in-concurrent-hash-join branch July 3, 2026 10:06
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 3, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ion-in-concurrent-hash-join

Reduce lock-contention from getTotalByteCount in ConcurrentHashJoin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors 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.

3 participants