Skip to content

Keeper: fix Create2 response serialization - #108909

Merged
antonio2368 merged 14 commits into
ClickHouse:masterfrom
unintended:fix_create2_method
Jul 9, 2026
Merged

antonio2368 merged 14 commits into
ClickHouse:masterfrom
unintended:fix_create2_method

Conversation

@unintended

@unintended unintended commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Keeper: fix Create2 response serialization

Create2 (OpNum::Create2) is the ZooKeeper opcode that returns the created node's Stat as part of the create response. It was already implemented in Keeper, but two issues prevented it from working correctly:

1. dataLength missing from the create response stat.
In preprocess for create requests, the Coordination::Stat passed to prepareCreateNode had dataLength left at zero. The Create2 response was therefore always returning dataLength = 0 regardless of the actual node data size.

2. ZooKeeperCreate2Response missing readImpl.
ZooKeeperCreate2Response overrode writeImpl (writing path_created + zstat) but not readImpl, so it inherited ZooKeeperCreateResponse::readImpl, which only reads path_created. Any code path that reads a Create2 response (e.g. a ClickHouse ZooKeeper client receiving a response from Keeper) would silently leave zstat uninitialized. This is now made symmetric.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry:

Fixed ClickHouse Keeper returning incorrect dataLength = 0 in the Stat of Create2 responses, and missing deserialization of zstat in ZooKeeperCreate2Response::readImpl.

Version info

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

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6eb18ab]

Summary: ✅


AI Review

Summary

This PR now scopes back to the two Create2 serialization bugs it describes: KeeperStorage populates Stat.dataLength before building the Create2 response, and ZooKeeperCreate2Response now deserializes the returned Stat instead of reading only path_created. The added tests cover both the storage-side dataLength regression and the wire-level readImpl/writeImpl round trip, and I did not find remaining correctness or contract issues in the current five-file diff.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 30, 2026
Comment thread src/Coordination/KeeperStorage.cpp Resolved
unintended and others added 3 commits July 2, 2026 10:26
…ngth stat

- `src/Common/ZooKeeper/tests/gtest_zookeeper.cpp`: add `Create2ResponseWireRoundTrip`
  test that serializes a `ZooKeeperCreate2Response` via `writeImpl`, deserializes via
  `readImpl`, and asserts the `path_created` and all `Stat` fields (including
  `dataLength`) survive the round trip.

- `src/Coordination/tests/gtest_coordination_storage.cpp`: add
  `TestCreate2ResponseDataLength` test that runs a `Create2` request through
  `KeeperStorage::preprocessRequest` / `processRequest` and asserts the returned
  `ZooKeeperCreate2Response::zstat.dataLength` equals the size of the data written to
  the node.
`WriteBufferFromOwnString` is defined in `IO/WriteBufferFromString.h`,
not in a separate `IO/WriteBufferFromOwnString.h` header that does not
exist. The wrong include caused a fatal compile error in the
`build_arm_tidy` CI job.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
`CREATE_WITH_STATS` is now enabled by default, so it appears in the
`enabled_feature_flags` column of `system.zookeeper_connection`.
Update the reference for `02735_system_zookeeper_connection` accordingly.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Comment thread src/Coordination/KeeperContext.cpp Resolved
unintended and others added 2 commits July 3, 2026 11:36
`CREATE_WITH_STATS` is now enabled by default in real Keeper
(`KeeperContext`). `TestKeeper` must advertise the same set of
capabilities so that code using `isFeatureEnabled` against a
`TestKeeper` instance sees consistent behaviour.

`TestKeeperCreateRequest` already populates all `Stat` fields
internally; the only missing piece was the feature-flag advertisement.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Now that `CREATE_WITH_STATS` is advertised by `TestKeeper`,
it must deliver the promised semantics.

- Add `Create2Response` to `IKeeper.h` (inherits `CreateResponse`,
  adds `Stat stat`) so callers can retrieve the node stat after
  a create without going through the ZooKeeper wire layer.

- `TestKeeperCreateRequest::process` already computed all `Stat`
  fields for the newly created node; now it returns them in a
  `Create2Response` instead of the bare `CreateResponse`.

- `TestKeeperCreateRequest::createResponse` (used for the error-fill
  path in multi-transactions) likewise returns `Create2Response`.

- Add `TestKeeperTest/Create2ResponseHasStatInMulti` regression test
  that verifies the response type and that `dataLength` is correct
  when a `CreateRequest` with `include_stats = true` is issued
  through the `multi` path.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Comment thread src/Common/ZooKeeper/TestKeeper.cpp Resolved
…nclude_ttl

Previously TestKeeperCreateRequest::process always returned Create2Response,
exposing stat fields even for plain CreateRequest (include_stats == false).
This violated the real Keeper contract where ZooKeeperCreateRequest::makeResponse
returns a bare CreateResponse unless the opcode is Create2 or CreateTTL.

Now the condition mirrors production:
- include_stats || include_ttl  →  Create2Response (stat populated from container)
- otherwise                     →  plain CreateResponse

createResponse (used for the failed-subrequest placeholder in multi
transactions) follows the same condition.

Extend the regression test to assert both sides of the contract:
a plain create sub-request must NOT be dynamic_cast-able to Create2Response.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Comment thread src/Common/ZooKeeper/TestKeeper.cpp Resolved
unintended and others added 2 commits July 5, 2026 19:17
…on duplicate

When not_exists=true and the node already exists, TestKeeperCreateRequest::process
returns error=ZOK but leaves path_created empty (the node was not created).
The previous stat-lookup guarded on error==ZOK, so container.at("") threw.

Guard on !path_created.empty() instead: stat is only meaningful when a node
was actually inserted into the container.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
`TestKeeperCreateRequest::process` was treating `not_exists=true` as a
blanket "always succeed on duplicate" flag regardless of the effective
opcode, so a `Create2` request on an already-existing node returned
`ZOK` instead of `ZNODEEXISTS`.

Real Keeper (and `ZooKeeperCreateRequest::getOpNum`) have a clear
precedence: `Create2` and `CreateTTL` win over `CreateIfNotExists`,
meaning a duplicate is always rejected with `ZNODEEXISTS` when
`include_stats` or `include_ttl` is set.

Apply the same precedence in `TestKeeper`:

```cpp
if (not_exists && !include_stats && !include_ttl)
    base_response.error = Error::ZOK;   // pure CreateIfNotExists
else
    base_response.error = Error::ZNODEEXISTS;
```

Also add `Create2DuplicateReturnsZNodeExists` regression test that
exercises both `not_exists=false` and `not_exists=true` with
`include_stats=true`, and the `include_ttl=true` variant.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Comment thread src/Common/ZooKeeper/TestKeeper.cpp Resolved
Comment thread src/Common/ZooKeeper/TestKeeper.cpp Resolved
@CLAassistant

CLAassistant commented Jul 7, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Comment thread src/Common/ZooKeeper/TestKeeper.cpp Resolved
Addresses the two remaining TestKeeper divergences flagged by the AI review on
the PR:

- A duplicate `CreateIfNotExists` (`not_exists`, no stats/ttl) now reports the
  requested path in `path_created`, matching `KeeperStorage::process`. Previously
  it returned `ZOK` with an empty `path_created`, so consumers reading
  `CreateResponse::path_created` (e.g. from `multi`) behaved differently under
  `implementation=testkeeper` than against real Keeper.

- TTL creates now undergo the same preflight validation as
  `KeeperStorage::preprocess`: creating under a TTL parent, combining `include_ttl`
  with an ephemeral node, and a non-positive or out-of-range `ttl` are all
  rejected with `ZBADARGUMENTS`. Previously TestKeeper accepted TTL creates that
  real Keeper rejects. The TTL bound `MAX_TESTKEEPER_TTL_MS` mirrors
  `MAX_KEEPER_TTL_MS`; it is duplicated in the Common/ZooKeeper layer because
  TestKeeper must not depend on the Coordination layer.

Adds two focused regressions in `gtest_testkeeper.cpp`: duplicate
`CreateIfNotExists` asserts `path_created == path`, and invalid TTL requests
return `ZBADARGUMENTS` without creating a node (while a valid TTL create at the
maximum bound succeeds).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@unintended
unintended force-pushed the fix_create2_method branch from f88925c to 794419d Compare July 7, 2026 17:30
`ZooKeeperCreateRequest::getOpNum` maps `not_exists` to `CreateIfNotExists`,
and `KeeperStorage::preprocess` rejects a sequential `CreateIfNotExists` with
`ZBADARGUMENTS` before the node-existence lookup. TestKeeper had no such check,
so a request built via
`zkutil::makeCreateRequest(path, data, CreateMode::PersistentSequential, /*ignore_if_exists=*/ true)`
fell through to the normal create path (or the duplicate `ZOK` branch) under
`implementation=testkeeper` instead of failing closed, letting tests pass
against TestKeeper where real Keeper would reject the same multi request.

Restructure `TestKeeperCreateRequest::process` to mirror the check order of
`KeeperStorage::preprocess` (parent existence/kind, then `is_sequential` with
`not_exists`, then node existence, then TTL validation) so the same request
yields the same error code, and add a focused regression test.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Comment thread src/Common/ZooKeeper/TestKeeper.cpp Resolved
…efix

KeeperStorage::preprocess appends the sequence suffix to path_created
before checking whether the node already exists, so a legal
CreateMode::PersistentSequential create still succeeds even when the
unsuffixed prefix is already present. TestKeeper's duplicate check still
probed container.contains(path) (the raw prefix), so a sequential create
on a prefix that already existed as a literal node wrongly returned
ZNODEEXISTS instead of creating the suffixed path like real Keeper.

Move the existence check to the final path_created (after appending the
sequence suffix), and add a focused regression test for the
prefix-collision case.
Don't enable `CREATE_WITH_STATS` by default and drop the `TestKeeper`
work that existed only to keep it consistent with real Keeper once
default-enabled (feature-flag advertisement, `Create2Response` support,
and the duplicate/TTL/sequential-create contract fixes). Keep the
`ZooKeeperCreate2Response::readImpl` fix and the `stat.dataLength`
fix in `KeeperStorage::preprocess`, which are the actual serialization
bugs this PR is about.
@unintended unintended changed the title Keeper: enable Create2 by default and fix Create2 response serialization Keeper: fix Create2 response serialization Jul 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.90% 85.90% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 78.00% 78.10% +0.10%

Changed lines: Changed C/C++ lines covered: 48/48 (100.00%) · Uncovered code

Full report · Diff report

@antonio2368
antonio2368 added this pull request to the merge queue Jul 9, 2026
Merged via the queue into ClickHouse:master with commit 9ed1f98 Jul 9, 2026
175 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 9, 2026
@unintended
unintended deleted the fix_create2_method branch July 9, 2026 12:54
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

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default 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.

5 participants