Skip to content

KeeperStorage refactoring - #106412

Merged
al13n321 merged 13 commits into
masterfrom
kstor
Jun 19, 2026
Merged

al13n321 merged 13 commits into
masterfrom
kstor

Conversation

@al13n321

@al13n321 al13n321 commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Partly to improve performance, partly to prepare for trying out a different storage implementation or two.


KeeperStorage's request preprocessing was split into 3 steps:

  • First it does map lookup for the affected znodes to get their ACL info to check auth.
  • Then it does the same map lookup to do most of the request logic and produce Deltas.
  • Then it does the same map lookup a third time to apply the Deltas.

This PR merges them all into one step with one node lookup.

checkAuth and applyDelta are removed and their logic is moved in into preprocess/preprocessLocal. Uncommitted state mutations are moved into a group of functions like prepareCreateNode, prepareUpdateNodeStat, etc, to make it easier to try a different underlying storage implementation later (I have specific plans for that).


When applying snapshot received from leader, KeeperServer reapplied Deltas instead of re-preprocessing requests. It's more efficient, but (1) requires deltas to be transferable between KeeperStorage instances (I'd like to try a KeeperStorage implementation that doesn't have that property), (2) requires code for applying deltas separately from producing them (which this PR removes as mentioned above). This PR changes it to preprocess requests instead, reusing code with preprocessing uncommitted requests on startup.


preprocess/process for read requests was weird: it did some small fraction of error checks in preprocess instead of just leaving preprocess no-op and making process and processLocal the same. This added a bunch of code for seemingly no gain. This PR removes preprocess for read requests, removes processImpl, and makes read requests' process call processLocal.

Version info

  • Merged into: 26.6.1.1012

@clickhouse-gh

clickhouse-gh Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e847b06]

Summary: ✅


AI Review

Summary

This PR refactors KeeperStorage request preprocessing so authorization, delta production, and delta application share one path, and changes snapshot apply/startup recovery to re-preprocess log tails instead of transferring deltas. The current head fixes most earlier review issues, but two release-build fail-closed checks are still missing in delta commit paths, so I cannot approve it yet.

Findings

❌ Blockers

  • [src/Coordination/KeeperStorage.cpp:1236] commit must verify that update/data/ACL deltas still match the committed node before mutating it in release builds. The current guards for UpdateNodeStatDelta, UpdateNodeDataDelta, and SetACLDelta are chassert only, so a stale delta would apply over mismatched committed state outside debug/sanitizer builds; the SetACLDelta path can also decrement the wrong old ACL usage and corrupt the ACL map. Keep the cheap runtime version/ACL checks and call onStorageInconsistency before any mutation when they fail.
  • [src/Coordination/KeeperStorage.cpp:1392] RemoveNodeDelta has the same fail-open behavior: removeNode checks the expected version only with chassert, then deletes the node and updates ACL/ephemeral state in release builds. A stale remove delta after a recreated or modified node should fail closed instead of deleting current state. Add a runtime version check here, or have the caller verify the version before calling removeNode.
Final Verdict

Status: ❌ Block

Minimum required action: restore release-build consistency checks for update/data/ACL/remove delta application before merging.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jun 4, 2026
Comment thread src/Coordination/KeeperStateMachine.cpp Resolved
Comment thread src/Coordination/KeeperStateMachine.cpp Resolved
Comment thread src/Coordination/KeeperStateMachine.cpp Resolved
Comment thread src/Coordination/KeeperStorage.cpp Resolved
Comment thread src/Coordination/KeeperStorage.cpp Resolved
Comment thread src/Coordination/KeeperStateMachine.cpp Resolved
Comment thread src/Coordination/KeeperStorage.cpp Resolved
Comment thread src/Coordination/KeeperStorage.cpp Resolved
Comment thread src/Coordination/KeeperStateMachine.cpp Resolved
Comment thread src/Coordination/KeeperStorage.cpp Resolved
@alexey-milovidov alexey-milovidov mentioned this pull request Jun 11, 2026
1 task
@al13n321
al13n321 marked this pull request as ready for review June 11, 2026 22:21
@antonio2368 antonio2368 self-assigned this Jun 15, 2026
Comment thread src/Coordination/ACLMap.cpp Resolved
Comment thread src/Coordination/ACLMap.cpp Resolved
Comment thread src/Coordination/ACLMap.cpp Resolved
Comment thread src/Coordination/KeeperServer.cpp Resolved
Comment thread src/Coordination/KeeperStorage.cpp Resolved
Comment thread src/Coordination/KeeperStorage.cpp Resolved
Comment thread src/Coordination/ACLMap.cpp Resolved
Comment thread src/Coordination/KeeperStateMachine.cpp Resolved
@al13n321
al13n321 enabled auto-merge June 16, 2026 17:45
if (operation.version != -1 && operation.version != node_it->value.stats.version)
onStorageInconsistency("Node to be updated has invalid version");
if constexpr (std::same_as<DeltaType, UpdateNodeStatDelta>)
chassert(operation.old_stats.version == node_it->value.stats.version);

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.

commit still needs to fail closed in release builds when a preprocessed delta no longer matches committed state. chassert is compiled out outside debug/sanitizer builds, so a stale UpdateNodeStatDelta or UpdateNodeDataDelta would now keep applying over the mismatched node; the SetACLDelta branch below also decrements operation.old_acl_id even if the node currently has a different ACL, corrupting the ACL usage map. This used to call onStorageInconsistency for the version mismatch. Please keep a release check here and in the SetACLDelta branch before mutating the node; the heavier data comparison can stay debug-only if needed.

Comment thread src/Coordination/KeeperStorage.cpp Resolved
@@ -1635,8 +1389,7 @@ bool KeeperStorage<Container>::removeNode(const std::string & path, int32_t vers
if (node_it == container.end())
return false;

if (version != -1 && version != node_it->value.stats.version)
return false;
chassert(version == node_it->value.stats.version);

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.

RemoveNodeDelta needs the same release-build fail-closed check as the update and ACL branches. Before this PR, removeNode returned false when the committed version no longer matched the preprocessed remove; now the only version guard is this chassert, which is compiled out in release builds. If a stale remove delta reaches commit after the node was changed or recreated, release builds will delete the current node and decrement its ACL/ephemeral state instead of reporting storage inconsistency. Please keep a runtime version check here, or in the caller, before mutating the node.

@clickhouse-gh

clickhouse-gh Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.20% 85.30% +0.10%
Functions 92.50% 92.60% +0.10%
Branches 77.50% 77.50% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 1069/1239 (86.28%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 25 line(s) · Uncovered code

Full report · Diff report

@al13n321
al13n321 added this pull request to the merge queue Jun 19, 2026
Merged via the queue into master with commit c064fad Jun 19, 2026
166 checks passed
@al13n321
al13n321 deleted the kstor branch June 19, 2026 01:11
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 19, 2026
@SaltTan

SaltTan commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

I have ancient ZooKeeper data with some of znodes created in 2016 and then migrated to Keeper by clickhouse-keeper-converter.
When I upgraded Keeper to 26.6 my ClickHouse couldn't connect and generated millions of exceptions trying to establish a session...
#117603

qiuyanjun888 pushed a commit to qiuyanjun888/ClickHouse that referenced this pull request Sep 15, 2026
…id carries it

Keeper returned ZNOAUTH for every request on a node whose acl_id is nonzero but
whose ACL list resolves to empty. ZNOAUTH is in isHardwareError, so the client
tore down the shared Keeper session and every replicated table reconnect-looped;
the reporter of ClickHouse#117603 measured 11,493,573 KEEPER_EXCEPTION in 58 minutes on a
cluster upgraded from 26.5 to 26.6, with no Keeper authentication configured.

ClickHouse#106412 rewrote checkACL to take an ACLId instead of a path and, in doing so,
replaced the semantic test

    const auto node_acls = getNodeACLs(...);
    if (node_acls.empty())
        return true;

with the representational

    if (acl_id == 0)
        return true;

Those two agree only while a nonzero id implies a non-empty list. ACLMap::
convertACLs, the only way a running Keeper assigns an id, maintains that by
returning 0 for an empty list, and its three production callers are the only
assignment sites. ACLMap::addMapping does not maintain it: it stores whatever
(id, acls) pair it is handed, and deserializeACLMap passes a ZooKeeper
snapshot's ACL cache through verbatim. Such an entry then round-trips through
every later Keeper snapshot via getMapping, and its usage count keeps
removeUnusedACLs from collecting it.

Restoring the emptiness test after the id is resolved reinstates the pre-26.6
behaviour at the predicate where it was lost, so all fourteen checkACL call
sites, checkCommittedACL and KeeperStateMachine recover at once. The acl_id == 0
fast path is kept: convertNumber is an out-of-line call returning ACLs by value
and the zero case is the common one on a per-request predicate.

The alternative of normalizing at load time (mapping an empty list down to id 0
and rewriting referencing nodes) was rejected: it hides the trigger rather than
fixing the semantics, silently rewrites an operator's persisted ACL ids and the
acl_id column of system.keeper_storage, and needs three coordinated sites
because dropping the mapping alone makes the per-node addUsage throw.

The change cannot widen access. A non-empty list the session does not satisfy is
still refused, and no live Keeper can mint a nonzero-empty id to exploit it. An
empty ACL list already means unrestricted elsewhere: fixupACL drops a
world:anyone entry whose permissions are ACL::All, so an ordinary create with
the wide-open ACL also ends up at id 0. Nothing is mutated, so there is no
digest impact (acl_id is not hashed by KeeperNodeStats::calculateDigest), no
snapshot format change and no setting change.

The regression test builds the state through ACLMap::addMapping, which is the
surface deserialization uses; it is unreachable from the client API, since
convertACLs can never produce it, so no SQL test can express it. It also asserts
that a nonzero id whose non-empty ACL list the session cannot satisfy is still
refused, which fails if the fix is widened past emptiness.

Co-Authored-By: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants