Skip to content

Reduce Lock Contention on Shared Context Mutex - #120355

Merged
Michicosun merged 7 commits into
masterfrom
reduce_lock_contention
Sep 18, 2026
Merged

Michicosun merged 7 commits into
masterfrom
reduce_lock_contention

Conversation

@Michicosun

@Michicosun Michicosun commented Sep 16, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Observed as a bottleneck in the ci-logs cluster.


Workflow [PR]
Sync PR [sync-upstream/pr/120355]

Version info

  • Merged into: 26.10.1.67-master (included in 26.10 and later)
  • Backported to: 26.9.19.10, 26.8.27.10, 26.7.31.7

@clickhouse-gh

clickhouse-gh Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6d8f519]

Summary: ✅


AI Review

Summary

This PR memoizes ZooKeeper system-log lookups and lazily initializes the shared CompressionCodecSelector to reduce Context lock contention. The Context.cpp lifetime fix now looks correct and CI is green, but the ZooKeeperImpl.cpp change still regresses Keeper logging for injected or late-bound log sinks, so I would not merge it as-is.

Missing context / blind spots
  • ⚠️ The title promises lower shared-context lock contention, but the PR does not include a before/after profile or focused benchmark, and the performance comparison jobs were skipped because the PR is not labeled pr-performance. A small contention benchmark or profile delta would close that evidence gap.
Findings
❌ Blockers
  • [src/Common/ZooKeeper/ZooKeeperImpl.cpp:2294-2312, src/Common/ZooKeeper/ZooKeeper.cpp:1541] [dismissed by author -- https://github.com/Reduce Lock Contention on Shared Context Mutex #120355#discussion_r4028795252] resolveSystemLogs still breaks the old lazy/injected logging contract. startNewSession and the constructor preserve zk_log / aggregated_zookeeper_log, but when a global Context exists before initializeSystemLogs() finishes, line 2294 returns false before lines 2300-2303 ever consult those injected pointers, so Keeper clients with constructor-supplied log sinks lose logging exactly in the window where they were supposed to stay independent of global system_logs. Separately, if the first lookup happens before any global context exists, line 2312 caches Resolved with null members and the impl never rechecks once the global context appears. Suggested fix: return already-supplied log pointers before consulting the global context, and keep the state Unresolved when there is no global context yet so a later call can bind the real system logs.
Tests
  • ⚠️ Existing CI stayed green, but there is no targeted regression test for a Coordination::ZooKeeper created with constructor-supplied logs before system_logs are initialized, or for the first log lookup before a global Context exists. Either case would catch the surviving resolveSystemLogs contract break.
Final Verdict

❌ Changes requested. One blocker from the earlier review is still present in the current code.

LLVM Coverage Report

Measured on commit 6d8f519.

Metric Baseline Current Δ
Lines 88.50% 88.50% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 80.70% 80.70% +0.00%

Changed lines: Changed C/C++ lines covered: 41/44 (93.18%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 16, 2026
Comment thread src/Interpreters/Context.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-keeper ClickHouse Keeper (Raft-based coordination layer; ZooKeeper replacement) and ZooKeeper client lib... label Sep 16, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6d8f51911 with master bdd39260f (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

1267 translation units recompiled, 7525 s compile time in total, 1267 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×0.89 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost -994.3 s (-12%) in total before that adjustment.

Translation unit Master PR Δ vs median
src/Functions/FunctionsConversion_reg.cpp 40.0 s 24.3 s -11.2 s (-32%)
src/Processors/QueryPlan/ReadFromMergeTree.cpp 34.8 s 23.0 s -7.8 s (-25%)

Job report

@azat azat self-assigned this Sep 16, 2026
Comment thread src/Interpreters/Context.cpp Resolved
Comment thread src/Common/ZooKeeper/ZooKeeperImpl.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This sync PR changes ZooKeeper system-log resolution (getZooKeeperLog/getAggregatedZooKeeperLog now go through a lazy resolveSystemLogs state machine) and switches the compression-codec selector to a callOnce init — server-startup and Keeper-logging plumbing, none of which sits on TPC-H Q1's scan-and-aggregate execution path. The lone flagged item, a -23.1% "improvement" on Q1, cannot plausibly be attributed to this diff, and the query is intrinsically noisy, so it was downgraded to not_sure as likely variance. No other queries were flagged and no regressions are attributable to this change.

clickbench

🟢 No significant changes

tpch_adapted_1_official

⚠️ 1 inconclusive

Flagged queries (1 of 22)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 1 not_sure 65 50 -23.1% <0.0001 This PR only touches ZooKeeper system-log resolution and compression-codec selector init — neither is on TPC-H Q1's scan/aggregate hot path. The query is intrinsically noisy, so this -23.1% reads as run-to-run variance, not a PR effect.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

Debug info
  • StressHouse run: bb751079-c8b8-47d5-a65b-c0503c32e2a1
  • MIRAI run: c23dbda5-4d07-4143-9207-e005bc8df04c
  • PR check IDs:
    • clickbench_641073_1789688112
    • clickbench_641079_1789688112
    • tpch_adapted_1_official_641085_1789688112
    • tpch_adapted_1_official_641096_1789688112
    • tpch_adapted_1_official_641131_1789688112

@Michicosun
Michicosun added this pull request to the merge queue Sep 18, 2026
Merged via the queue into master with commit 83a6d57 Sep 18, 2026
349 of 350 checks passed
@Michicosun
Michicosun deleted the reduce_lock_contention branch September 18, 2026 02:40
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 18, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 14, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 14, 2026
Backport #120355 to 26.8: Reduce Lock Contention on Shared Context Mutex
clickhouse-gh Bot added a commit that referenced this pull request Oct 14, 2026
Backport #120355 to 26.9: Reduce Lock Contention on Shared Context Mutex
clickhouse-gh Bot added a commit that referenced this pull request Oct 14, 2026
Backport #120355 to 26.7: Reduce Lock Contention on Shared Context Mutex
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-keeper ClickHouse Keeper (Raft-based coordination layer; ZooKeeper replacement) and ZooKeeper client lib... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo v26.4-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants