Repository navigation
Cherry pick #107097 to 26.3: Do not hold mutex across ZooKeeper reads in access entities refresh - #107276
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]>
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]>
…-from-zk Do not hold mutex across ZooKeeper reads in access entities refresh
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.
Original pull-request #107097
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job