Skip to content

Cherry pick #107097 to 26.4: Do not hold mutex across ZooKeeper reads in access entities refresh - #107278

Merged
robot-ch-test-poll merged 4 commits into
backport/26.4/107097from
cherrypick/26.4/107097
Jun 11, 2026
Merged

robot-ch-test-poll merged 4 commits into
backport/26.4/107097from
cherrypick/26.4/107097

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

azat and others added 4 commits June 11, 2026 00:43
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
@robot-ch-test-poll robot-ch-test-poll added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Jun 11, 2026
@robot-ch-test-poll
robot-ch-test-poll merged commit dd97836 into backport/26.4/107097 Jun 11, 2026
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.4/107097 branch June 11, 2026 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants