Repository navigation
Do not hold mutex across ZooKeeper reads in access entities refresh - #107097
Merged
azat merged 2 commits intoJun 11, 2026
Merged
Conversation
ZooKeeperReplicator::refreshEntities held `mutex` across one synchronous ZooKeeper round trip per access entity. The same mutex is taken by findEntity, which sits on the hot path of authentication (AccessControl::authenticate -> MultipleAccessStorage::findImpl) and of Context::getAccess during query analysis. A full refresh (all=true) runs on every session expiry and on config-driven session recreation; when Keeper round trips are slow (e.g. right after a Keeper rolling restart, when every component reconnects through the freshly created shared session), the refresh holds the mutex for N * latency, during which the server accepts no new connections and query analysis stalls. Observed in production as a 693-second hold that blocked all queries for the whole window. Read the entities without the lock and take `mutex` only around the memory_storage updates. Writers are unaffected: entity CRUD already reads from ZooKeeper before locking (refreshEntity). The one semantic change is that an entity mutated locally while a full reload is reading the snapshot may be transiently overwritten by setAll with the older snapshot; the per-entity and children watches registered by the reads re-deliver such changes immediately afterwards, which is the same eventual-consistency behavior remote mutations always had (previously the local writer would instead block on the mutex for the whole refresh). refreshEntityNoLock is inlined into its only remaining use via refreshEntity and removed. Co-Authored-By: Claude Fable 5 <[email protected]>
Contributor
|
Workflow [PR], commit [b8971ac] Summary: ❌
AI ReviewSummaryThis PR moves slow Final VerdictStatus: ✅ Approve |
The previous commit moved the ZooKeeper reads of `refreshEntities` out of `mutex`, which allowed a publish based on an older ZooKeeper read to overwrite a publish based on a newer one: a full refresh (all=true) reads a user, a concurrent REVOKE writes ZooKeeper and publishes the updated user via `refreshEntity`, and then `setAll` overwrites it with the pre-REVOKE snapshot. Until the watch registered by the stale read fires, authentication and `Context::getAccess` can observe grants that REVOKE has already reported removed. The same read->publish inversion existed for two concurrent `refreshEntity` calls on the same entity, for `removeAllExcept` racing a concurrent `insertEntity` (this one predates the previous commit: the children list was always read outside `mutex`), and for `removeEntity` racing a full refresh (a dropped user resurrected by a stale snapshot, with repair delayed until the children watch fires because the per-entity watch only reacts to `CHANGED`). Instead of gating only `setAll` on a generation counter (which would leave the other three races open), restore the invariant the code had while everything ran under `mutex` -- every "read from ZooKeeper, then publish into `memory_storage`" sequence is atomic with respect to every other one -- but on a dedicated `refresh_mutex`. It is held across `refreshEntities` (both modes, including the children read), `refreshEntity`, and the cache removal of `removeEntity`. Since the sequences are totally ordered and ZooKeeper reads through a session are monotonic, a later read always observes state at least as new as any earlier published one, so the last publish wins with the newest state; in particular, once a local write has published its own refresh, no stale snapshot can be published over it. `mutex` now only guards `memory_storage` and is held for in-memory operations only, so the hot path (`findEntity` in authentication and query analysis) still never waits for ZooKeeper. Writers (RBAC DDL) and the watching thread serialize with full refreshes exactly as they did before the previous commit (back then they serialized on `mutex`). The one remaining stale window is cross-session: a full refresh on a freshly created session can read a lagging follower and miss a write committed through the old session after the new session's sync; it also predates the previous commit and self-heals when the follower applies the missed transaction and fires the read's watch. Lock order: cached_zookeeper_mutex -> refresh_mutex -> mutex. Co-Authored-By: Claude Fable 5 <[email protected]>
pufit
approved these changes
Jun 11, 2026
azat
enabled auto-merge
June 11, 2026 16:46
Contributor
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 32/32 (100.00%) | Lost baseline coverage: none · Uncovered code |
Merged
via the queue into
ClickHouse:master
with commit Jun 11, 2026
48658ff
325 of 328 checks passed
robot-ch-test-poll
added a commit
that referenced
this pull request
Jun 11, 2026
Cherry pick #107097 to 25.8: Do not hold mutex across ZooKeeper reads in access entities refresh
robot-clickhouse
added a commit
that referenced
this pull request
Jun 11, 2026
… access entities refresh
robot-ch-test-poll
added a commit
that referenced
this pull request
Jun 11, 2026
Cherry pick #107097 to 26.3: Do not hold mutex across ZooKeeper reads in access entities refresh
robot-clickhouse
added a commit
that referenced
this pull request
Jun 11, 2026
… access entities refresh
robot-ch-test-poll
added a commit
that referenced
this pull request
Jun 11, 2026
Cherry pick #107097 to 26.4: Do not hold mutex across ZooKeeper reads in access entities refresh
robot-clickhouse
added a commit
that referenced
this pull request
Jun 11, 2026
… access entities refresh
robot-ch-test-poll
added a commit
that referenced
this pull request
Jun 11, 2026
Cherry pick #107097 to 26.5: Do not hold mutex across ZooKeeper reads in access entities refresh
robot-clickhouse
added a commit
that referenced
this pull request
Jun 11, 2026
… access entities refresh
azat
added a commit
that referenced
this pull request
Jun 12, 2026
Backport #107097 to 26.4: Do not hold mutex across ZooKeeper reads in access entities refresh
azat
added a commit
that referenced
this pull request
Jun 12, 2026
Backport #107097 to 26.5: Do not hold mutex across ZooKeeper reads in access entities refresh
azat
added a commit
that referenced
this pull request
Jun 12, 2026
Backport #107097 to 26.3: Do not hold mutex across ZooKeeper reads in access entities refresh
azat
added a commit
that referenced
this pull request
Jun 12, 2026
Backport #107097 to 25.8: Do not hold mutex across ZooKeeper reads in access entities refresh
pull Bot
pushed a commit
to admariner/ClickHouse
that referenced
this pull request
Jun 18, 2026
Each per-entity cache recomputes every live "enabled" set on every entity change, under the `mutex` the `ContextAccess` build path takes. Notifications arrive one per changed entity, so a batch changing N entities does N full O(enabled sets * entities) rebuilds, degenerating to O(enabled sets * entities^2) when a refresh changes everything (e.g. after a Keeper session is recreated, ClickHouse#107097). In one installation with ~50K row policies and a few hundred enabled sets a single rebuild is tens of millions of ops; repeated per changed policy it held the lock for minutes, blocking session creation and query analysis. `sendNotifications` already drains the whole queue (an entire refresh) in one call, so add a batch-finished hook (`subscribeForBatchFinished`) invoked once after the queue is drained. Each per-entity handler now only updates its `all_*` map and sets a dirty flag; the rebuild runs once per batch if dirty. This drops a batch to O(enabled sets * entities). The rebuild still completes within the same `sendNotifications` call, so state is current once it returns, and single-DDL notifications still recompute exactly once. `RoleCache` subscribes on first `getEnabledRoles`, not in its ctor, because `changes_notifier` is constructed after the caches. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Slach
added a commit
to Slach/ClickHouse
that referenced
this pull request
Jul 14, 2026
…esolution The fix for the UNKNOWN_ROLE race was already merged into master via ClickHouse#107097 (refresh_mutex + scoped mutex locks at publish points). The merge of master into this branch (db83926) incorrectly kept the older approach's outer std::lock_guard lock{mutex} before getChildrenWatch on top of it, producing a double lock of mutex in refreshEntities and failing the build with -Wshadow and -Wthread-safety-analysis: https://github.com/ClickHouse/ClickHouse/actions/runs/29334767283/job/87091850817 Remove the leftover hunk so ZooKeeperReplicator.cpp matches master; this PR now only adds the integration test. Co-Authored-By: Claude Fable 5 <[email protected]>
1 task
Slach
added a commit
to Slach/ClickHouse
that referenced
this pull request
Jul 15, 2026
The fix itself was merged via ClickHouse#107097, so this PR is test-only now and Bugfix validation no longer applies. Co-Authored-By: Claude Fable 5 <[email protected]>
1 task done
zeekay
pushed a commit
to hanzoai/datastore
that referenced
this pull request
Sep 11, 2026
…r reads in access entities refresh
zeekay
pushed a commit
to hanzoai/datastore
that referenced
this pull request
Sep 11, 2026
…7097 Backport ClickHouse#107097 to 26.4: Do not hold mutex across ZooKeeper reads in access entities refresh
zeekay
pushed a commit
to hanzoai/datastore
that referenced
this pull request
Sep 11, 2026
…esolution The fix for the UNKNOWN_ROLE race was already merged into master via ClickHouse#107097 (refresh_mutex + scoped mutex locks at publish points). The merge of master into this branch (db83926) incorrectly kept the older approach's outer std::lock_guard lock{mutex} before getChildrenWatch on top of it, producing a double lock of mutex in refreshEntities and failing the build with -Wshadow and -Wthread-safety-analysis: https://github.com/ClickHouse/ClickHouse/actions/runs/29334767283/job/87091850817 Remove the leftover hunk so ZooKeeperReplicator.cpp matches master; this PR now only adds the integration test. Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay
pushed a commit
to hanzoai/datastore
that referenced
this pull request
Sep 11, 2026
The fix itself was merged via ClickHouse#107097, so this PR is test-only now and Bugfix validation no longer applies. Co-Authored-By: Claude Fable 5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Do not hold mutex across ZooKeeper reads in access entities refresh
ZooKeeperReplicator::refreshEntities held
mutexacross one synchronous ZooKeeper round trip per access entity. The same mutex is taken by findEntity, which sits on the hot path of authentication (AccessControl::authenticate -> MultipleAccessStorage::findImpl) and of Context::getAccess during query analysis. A full refresh (all=true) runs on every session expiry and on config-driven session recreation; when Keeper round trips are slow (e.g. right after a Keeper rolling restart, when every component reconnects through the freshly created shared session), the refresh holds the mutex for N * latency, during which the server accepts no new connections and query analysis stalls. Observed in production as a 693-second hold that blocked all queries for the whole window.Version info
26.6.1.68926.5.3.22,26.4.5.29,26.3.14.23,25.8.25.22