Skip to content

More KeeperStorage refactoring - #108583

Merged
al13n321 merged 34 commits into
masterfrom
kref
Jul 20, 2026
Merged

al13n321 merged 34 commits into
masterfrom
kref

Conversation

@al13n321

@al13n321 al13n321 commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Things needed to integrate new LSM-tree-based storage (#107261) while keeping the current in-memory implementation also available as an option. A lot of moving code around. Can be reviewed one commit at a time (at least the initial 11 commits), each commit does a different thing and leaves everything in working state. (Don't trust the AI-generated long commit messages though, they might be outdated, just the titles are correct.) (This could be a stack of 11 PRs, but stacking PRs on github is way too painful for that.)

As usual, the integration refactoring was way harder and took longer than the actual meaningful work of implementing the LSM tree etc :/

Sequence of commits:

  • First, 3 small behavior fixes: some requests were missing some corner case checks. E.g. you could have a Multi request containing a Check that checks the contents of a system node, which may be different on different servers (e.g. if they're running different versions); then some servers would reject the request and others would accept - inconsistency. So Check on system nodes is not allowed now.
  • Moved NodeStats + acl_id + num_children into a struct in KeeperCommon, so (1) it can be reused between KeeperMemNode and the new storage, (2) acl_id and num_children don't need to be awkwardly passed around separately, this made no sense, they clearly belong in NodeStats.
  • (Removed some dead code, should_lock_storage.)
  • Made snapshot reader and writer not look at KeeperStorage internals. Will also come in handy for Improvements to keeper-utils analyze-snapshot #106270
  • Made getStorageStats just lock the mutex and look at sizes of data structures directly, instead of laboriously updating atomics after each time these data structures changed size.
  • Moved Delta structs to somewhere visible outside KeeperStorage. Deltas won't be templated/abstracted, it'll still be one std::variant, but it will include both the old storage's and the new storage's stuff. So the base KeeperStorage would know about both storage implementations' delta types (e.g. to get node stats out of Delta to put into response). A bit weird, but all alternatives I could think of were way worse (e.g. making Delta a template, but adding some common accessor methods to it to e.g. get node stats to put into response).
  • (A little cleanup for how KeeperStorage::prepare* methods are organized.)
  • Remove an obsolete compatibility behavior when loading snapshots (ignore_system_path_on_startup), which suddenly becomes more inconvenient with the snapshot deserialization refactoring. Support for IDisk in Keeper #50098 (comment)
  • Finally do the main thing: pull KeeperStorage asunder into 3 parts: (1) KeeperNodesStorage interface (that will have 2 implementations) for storing the bulk of the data, (2) KeeperStorage base class with things like ACLMap, ephemeral node set, watches, deltas, (3) KeeperStorageImpl, templated by KeeperNodesStorage implementation, has all the request logic (all the preprocess, process, processLocal, processWatches). (The template is probably not necessary, I kinda sunken cost fallacied into keeping it, may make sense to experiment with removing it later. At least the template is only in KeeperStorageImpl, it doesn't contaminate KeeperStateMachine, KeeperSnapshotManager, etc, not like rocksdb storage did.)
  • Got rid of templates in tests, they're using virtual methods instead. And tests that don't use enable_compression parameter are not parametrized by it anymore (not instantiated twice).

Once this and #107261 are merged, a smaller third PR will finally integrate the two.

Version info

  • Merged into: 26.7.1.1257 (included in 26.7 and later)

al13n321 and others added 11 commits June 25, 2026 23:28
Three client-visible behavior changes, separated from the storage
refactoring that follows:
 * `Set` on `/` now returns `ZBADARGUMENTS` (previously it was allowed
   and updated the root node).
 * `Check`/`CheckNotExists`/`CheckStat` on a path inside the internal
   `/keeper` system subtree now returns `ZBADARGUMENTS`. `Check` is
   allowed inside `Multi`, which must be deterministic, but internal
   nodes may depend on Keeper version and configuration.
 * Filtered `List` (`EPHEMERAL_ONLY`/`PERSISTENT_ONLY`) now checks each
   reported child's Read ACL even when neither stat nor data is
   requested, because exposing a child's `is_ephemeral` flag is also
   secret. Previously child ACLs were only checked when stat or data
   was requested.

The `List` handler body is reorganized into the same name-only vs.
name+stat/data branching that the later refactoring uses, but still
implemented with `container`/`getChildren` lookups so that the
refactoring commit becomes a pure switch to the nodes-storage
abstraction.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Step 1 of decoupling the Coordination gtests from KeeperMemoryStorage
internals: replace the typed-test fixture (TestParam<TStorage, compress>)
with a value-parametrized one. TestParam is now a plain struct with a
non-constexpr enable_compression field, CoordinationTest derives from
testing::TestWithParam<TestParam>, all TYPED_TEST become TEST_P, and a
single INSTANTIATE_TEST_SUITE_P lives in gtest_coordination_common.cpp.

A makeKeeperContext helper centralizes KeeperContext creation so a future
TestParam field selecting the KeeperStorage implementation can be applied
in one place.

Storage usages temporarily point at KeeperMemoryStorage; subsequent
commits replace them with KeeperStorage::create and virtual-method access.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@al13n321
al13n321 requested a review from antonio2368 June 26, 2026 07:34
@clickhouse-gh

clickhouse-gh Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [79188d6]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_tsan, 3/6) FAIL
test_replicated_database/test.py::test_replicated_table_structure_alter FAIL cidb, issue

AI Review

Summary

This PR splits Keeper storage into a node-storage layer plus shared request logic and rewires snapshot/stat plumbing around that refactor. Most earlier review points are fixed in the current head, but I still see two correctness blockers in the final code: snapshot serialization still consults live TTL/container bookkeeping instead of the frozen snapshot state, and the new uncommitted-node bookkeeping still relies on std::unordered_map iterator stability across rehash. I would request changes. The remaining issues already have review threads, so I did not post duplicate inline comments.

Findings

❌ Blockers

  • [src/Coordination/KeeperMemNodesStorage.h:166] [dismissed by author -- https://github.com/More KeeperStorage refactoring #108583#discussion_r3556053620] The new uncommitted-node bookkeeping still stores UncommittedNodesMap::iterator handles in side structures (uncommitted_zxid_to_nodes, visitUncommittedRecursive, prepareRemoveEphemeralNodes) while later code can still emplace into uncommitted_nodes. A normal Remove path already does this (KeeperStorageImpl.cpp:511-527 takes parent_node_ref, then getUncommittedNode(zk_request.path) can insert and rehash before prepareUpdateNodeStat uses the saved ref), and the recursive/close paths queue these refs for even longer. That is undefined behavior in core Keeper mutation/cleanup paths; please switch these handles to stable keys or use a container with guaranteed iterator stability.
  • [src/Coordination/KeeperSnapshotManager.cpp:178] [dismissed by author -- https://github.com/More KeeperStorage refactoring #108583#discussion_r3556136754] Snapshot serialization still decides V8/V9 compatibility from live storage->ttl_paths / storage->container_paths even though node_stream was frozen earlier in KeeperStorageSnapshot construction (KeeperSnapshotManager.cpp:300-332). A TTL/container created or deleted after beginWritingSnapshot can therefore make an actually compatible frozen snapshot fail, or let an incompatible frozen snapshot serialize without the required TTL/container metadata. Please capture TTL/container presence from the frozen snapshot state and use only that immutable metadata during serialization.
Final Verdict

Status: ⚠️ Request changes

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jun 26, 2026
@al13n321 al13n321 mentioned this pull request Jun 26, 2026
Comment thread src/Coordination/KeeperStorageImpl.cpp Resolved
Comment thread src/Coordination/KeeperStorageImpl.cpp Resolved
al13n321 added a commit that referenced this pull request Jun 26, 2026
Style check (cpp): the split left `ProfileEvents`/`ErrorCodes` extern
declarations out of sync with their usage. Remove the unused
`ProfileEvents::Keeper*Request`/`*ElapsedMicroseconds` declarations from
`KeeperStorage.cpp` (they are used in `KeeperStorageImpl.cpp` now), remove the
unused `ErrorCodes::LOGICAL_ERROR` from `KeeperCommon.cpp`, and add the missing
`ErrorCodes::LOGICAL_ERROR` declaration to `KeeperMemNodesStorage.cpp`.

Build (arm_tidy): the new virtual interfaces declared default arguments on
virtual/override methods, which clang-tidy's `google-default-arguments`
prohibits. Move the defaults to non-virtual wrappers that forward to no-default
virtual `*Impl` methods (`preprocessRequest`, `getCommittedNodeSimple`,
`getUncommittedNodeSimple`, `updateCommittedNode`), and drop the unused default
argument on `processLocalRequests` (all callers already pass it explicitly).

CI: #108583

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread src/Coordination/KeeperStorageImpl.cpp Resolved
al13n321 added a commit that referenced this pull request Jun 26, 2026
Replace the non-virtual NVI wrappers added in the previous commit with plain
virtual methods that have no default arguments, and update every caller to pass
`nullptr`/`std::nullopt`/`true`/etc. explicitly. This still satisfies clang-tidy's
`google-default-arguments` (no defaults on virtual/override methods) without the
extra wrapper indirection.

Affects `preprocessRequest`, `getCommittedNodeSimple`, `getUncommittedNodeSimple`,
and `updateCommittedNode`.

CI: #108583

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
al13n321 added a commit that referenced this pull request Jun 26, 2026
Style check (cpp): the split left `ProfileEvents`/`ErrorCodes` extern
declarations out of sync with their usage. Remove the unused
`ProfileEvents::Keeper*Request`/`*ElapsedMicroseconds` declarations from
`KeeperStorage.cpp` (they are used in `KeeperStorageImpl.cpp` now), remove the
unused `ErrorCodes::LOGICAL_ERROR` from `KeeperCommon.cpp`, and add the missing
`ErrorCodes::LOGICAL_ERROR` declaration to `KeeperMemNodesStorage.cpp`.

Build (arm_tidy): the new virtual interfaces declared default arguments on
virtual/override methods, which clang-tidy's `google-default-arguments`
prohibits. Move the defaults to non-virtual wrappers that forward to no-default
virtual `*Impl` methods (`preprocessRequest`, `getCommittedNodeSimple`,
`getUncommittedNodeSimple`, `updateCommittedNode`), and drop the unused default
argument on `processLocalRequests` (all callers already pass it explicitly).

CI: #108583

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…le judged them to be too error-prone for everyone everywhere.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread src/Coordination/KeeperStorage.cpp Resolved
Comment thread src/Coordination/KeeperStorageImpl.cpp Resolved
Comment thread src/Coordination/KeeperStorageImpl.cpp Resolved
@antonio2368 antonio2368 self-assigned this Jul 9, 2026
Comment thread src/Coordination/KeeperStorageImpl.cpp Resolved

@antonio2368 antonio2368 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for deleting my code.
I like the changes.

Comment on lines +122 to +127
static constexpr uint64_t NUM_FLAGS = 3;
static constexpr uint64_t EPHEMERAL = 1ul << 63;
static constexpr uint64_t TTL = 1ul << 62;
static constexpr uint64_t CONTAINER = 1ul << 61;
static constexpr uint64_t FLAGS_MASK = EPHEMERAL | TTL | CONTAINER;
static_assert(FLAGS_MASK == ~(~0ul >> NUM_FLAGS));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bitfields?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with manual packing because manual packing would be needed for serialization anyway, and deserialization may be slightly faster if it just assigns one uint64_t without unpacking bitfields.

{
KEEPER_STORAGE_LOCK_EXCLUSIVE(lock);
return storage->getSessionWithEphemeralNodesCount();
return storage->getStorageStats();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exclusive lock is problematic as this is used by mntr which is used for polling readiness.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How frequent is that? Once a second?

I'll see if I can change it to shared lock. IIRC the only problems were watch sets (would need to add yet another mutex for them) and uncommitted state (would need to either add yet another mutex or report uncommitted state stats through a different mechanism, e.g. CurrentMetrics).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, all I had to do was grab process_and_responses_lock. Did that, switched to shared lock.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's once a second.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But with this lock you still are contending with process/preprocess?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, once a second the monitoring thread will wait for the current process/processLocal/preprocess call to complete and lock the mutex for a very short time. Is that a problem? Does mntr need to be available if something is broken and a request is stuck?

(With exclusive rwlock lock there was a problem that it prevents new processLocal-s from starting while waiting for the running preprocess to finish; so preprocess latency would sometimes be added to read latency. But with process_and_responses_lock no similar problem exists, I think? It's mostly a one-sided wait, delaying only the mntr thread? Assuming getStorageStats() is fast, which it seems to be, it's just a few size()s and such.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(If you just mean "mutex contention slow", doesn't that apply at more like 10k locks/s scale, not 1?)

@antonio2368 antonio2368 Jul 14, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do use it to report readiness so mntr not being available means the pod will be reported as unhealthy.
If request gets stuck I guess we can pretend this is a legit case of reporting Keeper as unhealthy.
I'm more worried if it can affect latency.
After I changed it to atomic I also changed how we count certain watches metrics which were really heavy so maybe it's okay to run it with a mutex again as all operations seem simple.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In old old code we iterated the sessions_and_watchers map.

Comment thread src/Coordination/KeeperStorageImpl.cpp Resolved
@al13n321
al13n321 enabled auto-merge July 17, 2026 01:22
@clickhouse-gh

clickhouse-gh Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 78.20% 78.20% +0.00%

Changed lines: Changed C/C++ lines covered: 3768/4253 (88.60%) · Uncovered code

Full report · Diff report

@al13n321
al13n321 added this pull request to the merge queue Jul 20, 2026
Merged via the queue into master with commit d4c749d Jul 20, 2026
176 of 178 checks passed
@al13n321
al13n321 deleted the kref branch July 20, 2026 23:57
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 21, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
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.

3 participants