Skip to content

Do not list every key node when dropping a KeeperMap table - #129440

Merged
Blargian merged 3 commits into
ClickHouse:masterfrom
groeneai:keepermap-drop-flat-data-removal
Nov 9, 2026
Merged

Blargian merged 3 commits into
ClickHouse:masterfrom
groeneai:keepermap-drop-flat-data-removal

Conversation

@groeneai

@groeneai groeneai commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #129287

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

DROP TABLE of a KeeperMap table no longer sends a Keeper List request for every key before removing it, and removes the keys of a table with fewer than 1000 keys in the same Keeper request as its metadata when Keeper supports RemoveRecursive. Dropping a table with 200000 keys is more than 10 times faster, and so is a server shutdown that waits for such a drop.

Description

StorageKeeperMap::dropTableData removed the keys with ZooKeeper::removeChildrenRecursive, which calls getChildren on every key node. KeeperMap keys are leaf nodes, so dropping N keys cost N empty List round trips.

Server shutdown waits for a KeeperMap drop in the background drop task: in La Casa Del Dolor (amd_tsan) on #129287 the server was force-killed after its 30 s stop window while the drop was still in removeChildrenRecursive -> getChildren.

The drop now removes data/ with a RemoveRecursive operation inside the multi request that removes the table metadata, when Keeper supports it. Without that support, or when data/ has more than 1000 nodes (the operation's limit), the keys are first removed with the flat tryRemoveChildrenRecursive path that TRUNCATE already uses (one List of data/, then batched removes), and data/ is removed with the metadata as before. So an interrupted drop still leaves data/ in place, which the leftover cleanup of older servers needs on the next CREATE.

Measured on a debug build with the embedded Keeper, 3 runs, before -> after (200000 keys unless noted):

  • DROP TABLE ... SYNC: 133.6 / 141.4 / 176.0 s -> 11.2 / 11.1 / 11.1 s; per-key List requests 200000 -> 0.
  • SIGTERM right after a non-SYNC DROP, until the server exits: 194.3 / 161.5 / 153.8 s -> 13.4 / 14.4 / 14.1 s.
  • 999 keys, DROP TABLE ... SYNC: 956-999 ms -> 24-31 ms.

The test drops a 999-key and a 1000-key table (the two sides of the limit) and counts requests in system.zookeeper_log: master sends one List per key and no RemoveRecursive; with the change, one RemoveRecursive per table in the same multi request as the metadata removal, no per-key List, and no per-key request at all for the small table.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/129440]

Version info

  • Merged into: 26.11.1.1768-master (included in 26.11 and later)

`StorageKeeperMap::dropTableData` removed the keys with
`ZooKeeper::removeChildrenRecursive`, which calls `getChildren` on every
child before removing it. KeeperMap keys are always leaf nodes
(`data/<url-safe base64 key>`), so a drop of N keys cost N empty `List`
round trips plus N/100 multi requests, serialized on one session.

Use `tryRemoveChildrenRecursive(path, /*probably_flat=*/ true)`, which
TRUNCATE already uses for the same `data/` node: one `List` of `data/`,
then removals in batches of 100. If a batch fails with a user error it
falls back to per-node removal and removes a non-empty child recursively;
hardware errors still throw. The final multi that removes `data/` and the
metadata still reports leftovers as before.

The drop runs in the background drop task, and server shutdown waits for
it, so a large KeeperMap drop in flight held shutdown: La Casa Del Dolor
(amd_tsan) force-killed the server after 30 s while a dropped table's
keys were still being walked, ~1000 batches of 100 into the drop (Keeper
commit log of PR ClickHouse#129287's run).

Measured on a debug build with the embedded Keeper, 200000 keys:
DROP TABLE ... SYNC 133.6/141.4/176.0 s -> 11.3/11.3/11.3 s;
SIGTERM right after a non-SYNC DROP until exit 194.3/161.5/153.8 s ->
13.8/14.4/13.9 s; per-key List requests 200000 -> 0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (nits only, none blocking).

# Sev Finding Verdict Evidence / action
1 💡 A missing data/ node would now surface as the final multi's LOGICAL_ERROR instead of ZNONODE from getChildren DISAGREE Unreachable: data/ is created in the same multi as metadata/ (StorageKeeperMap.cpp:651-655) and removed only by the final multi (:1088-1093), and dropTableData always runs after the multi that creates metadata/dropped/lock
2 💡 With Shared Catalog drop() returns before dropTableData, so the test would see no removes DISAGREE The shared-catalog run already skips no-replicated-database tests (tests/clickhouse-test:3656)

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261105-054300

@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05508_keeper_map_drop_does_not_list_keys on master prints 1000 1000 (1000 per-key List requests for 1000 keys) on every run. Cost on demand: a 200000-key DROP TABLE ... SYNC takes 133.6-176.0 s, SIGTERM during that drop 153.8-194.3 s until exit (debug build, embedded Keeper).
b Root cause explained? StorageKeeperMap::dropTableData used ZooKeeper::removeChildrenRecursive, which calls getChildren on every child before removing it. KeeperMap keys are leaf nodes, so N keys cost N empty List round trips; the drop runs in the background drop task and server shutdown waits for it.
c Fix matches root cause? Yes: the call site now uses tryRemoveChildrenRecursive(path, /*probably_flat=*/ true), the call TRUNCATE already makes on the same node. No timeout, skip or shutdown special case.
d Test intent preserved / new tests added? New SQL test: 1000 removes, 0 per-key Lists, root node gone. Reddens on master (1000 1000) and on a mutant without the removal (0 0, root left).
e Both directions demonstrated? Yes, same rig: master FAIL, fix OK; 50/50 randomized runs OK; all 20 existing KeeperMap stateless tests pass on both binaries.
f Fix is general across code paths? Every caller of dropTableData goes through the changed line: DROP, leftover cleanup on CREATE, dropTableFromZooKeeper. TRUNCATE was already flat. No other removeChildrenRecursive caller outside ZooKeeper.cpp.
g Fix generalizes across inputs (params/datatypes/wrappers)? Key nodes are url-safe base64 of the serialized key for every key type and writer, so the layout is type-independent; a nested node (only via a crafted backup) is still removed by the primitive's fallback. Checked 0, 1, 2, 100, 101 (batch edge), 1000 and 200000 keys.
h Backward compatible? (maintainer-approved exception only) Yes: no setting, no Keeper layout or metadata change; the Keeper writes are the same removes.
i Invariants and contracts preserved? dropTableData still runs only after the multi that creates metadata/dropped/lock, so data/ exists when it runs; the final multi and its error handling are unchanged; non-user Keeper errors still throw.

Session id: cron:clickhouse-impl-slot-7:20261105-043600

@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author

cc @PedroTadim @Michicosun, could you review this and add the can be tested label so CI runs? DROP TABLE of a KeeperMap table removed its keys with removeChildrenRecursive, one empty List per key node, and that drop held server shutdown in La Casa Del Dolor until the force kill; it now uses the flat tryRemoveChildrenRecursive path that TRUNCATE already takes (200000 keys: about 140 s -> 11 s).

@CLAassistant

CLAassistant commented Nov 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@clickhouse-gh

clickhouse-gh Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [39d5550]

Summary: ✅


AI Review

Summary

KeeperMap drops now try a bounded RemoveRecursive operation inside the metadata transaction and use flat, batched key removal when the operation is unsupported or exceeds its limit. At commit 39d555004dcdc637a636f5265a866634dcd7c04b, the previously reported interrupted-drop compatibility issue is fixed: data/ remains present until the metadata transaction commits. No unresolved findings remain. The boundary regression test and reported before/after measurements support the change.

Missing context / blind spots
  • ⚠️ No local server binary or build was available for execution. The current-head KeeperMap stateless and integration CI results were still pending when reviewed; completed results would close this runtime-verification gap.
Final Verdict

✅ Approve — no new actionable findings. The prior bot-authored compatibility thread has been resolved.

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements can be tested Allows running workflows for external contributors comp-keeper ClickHouse Keeper (Raft-based coordination layer; ZooKeeper replacement) and ZooKeeper client lib... labels Nov 5, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 39d555004 with master 4d3a2ee79 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 18 s compile time in total, 7 of them have a recent master baseline.

Job report

@PedroTadim PedroTadim self-assigned this Nov 5, 2026
@antonio2368

Copy link
Copy Markdown
Member

@groeneai why not use tryRemoveRecursive?

Comment thread src/Storages/StorageKeeperMap.cpp Outdated
throw zkutil::KeeperException(Coordination::Error::ZOPERATIONTIMEOUT, "Manually triggered operation timeout");
});
zookeeper->removeChildrenRecursive(zk_data_path_);
zookeeper->tryRemoveChildrenRecursive(zk_data_path_, /*probably_flat=*/ true);

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.

@groeneai are you sure this won't ever leak nodes? Check if other places also need this improvement such as truncating a KeeperMap table?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes. It lists data/ once and removes every listed child in multis of 100 (ZooKeeper.cpp:1126-1198). If a multi fails, the children of that batch are removed one by one: ZNONODE is skipped, ZNOTEMPTY (a key with children, which KeeperMap does not create: keys are base64url leaves) removes that child's subtree, and any other error throws, as the old code did. A node still there afterwards, e.g. one created after the listing, is caught by the final multi, which removes data/ together with the metadata (StorageKeeperMap.cpp:1091): a non-empty data/ fails it with ZNOTEMPTY (:1114), the dropped marker stays, and the next table created on that path removes the leftovers (:597, :637). That part is unchanged; the old removeChildrenRecursive also listed data/ only once. The test checks that the table's root node is gone after the drop.

Other places: TRUNCATE already makes this exact call on the same node (StorageKeeperMap.cpp:994), I took it from there. Every drop path (drop(), dropTableFromZooKeeper, the leftover cleanup in the constructor) goes through the line changed here, and DELETE removes the matching keys by path (:1801) without a listing. Outside ZooKeeper.cpp, nothing calls removeChildrenRecursive after this PR.

@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author

why not use tryRemoveRecursive?

With the default remove_nodes_limit = 1000 it does not help here: Keeper answers ZNOTEMPTY when the subtree, root included, has more nodes than the limit (KeeperNodesStorage.h:156-158), and the client then falls back to tryRemoveChildrenRecursive(path) with probably_flat = false (ZooKeeper.cpp:1214-1216, :1259), the same per-key List walk this PR removes. With Apache ZooKeeper the flag is never set, so it is always that walk. It also removes data/ itself, which the final multi removes together with the metadata (StorageKeeperMap.cpp:1091; a missing data/ there is the LOGICAL_ERROR at :1112).

With a limit that covers the table it is much faster, though. Local debug server, embedded Keeper, request counts from Keeper's system.events, wall time of the client call:

keys removal Keeper requests time
2000 RemoveRecursive, limit 1000 ZNOTEMPTY, then 2001 List + 20 multi 2.0 s
200000 RemoveRecursive, limit 300000 1 RemoveRecursive 0.67-0.71 s
200000 this PR 1 List + 2000 multi 11.1-11.2 s

So I can switch dropTableData to remove data/ with a RemoveRecursive op inside the final multi (atomic with the metadata) when the flag is on, and keep this flat path for ZooKeeper. Which limit do you prefer: UINT32_MAX (any table in one request), or a fixed cap like the 10000 in ObjectStorageQueueMetadata.cpp:1029, with the flat path above it?

@antonio2368

Copy link
Copy Markdown
Member

@groeneai it's fine if it fallbacks to non RemoveRecursive option but it's worth trying in case the table is small.
If you need probably_flat = true you can just extend the signature.

Per review: `dropTableData` now removes `data/` with `tryRemoveRecursive`, so
a table whose `data/` subtree fits in `remove_nodes_limit` (1000 nodes, root
included) is removed with one `RemoveRecursive` request. Bigger tables, or a
Keeper without the `REMOVE_RECURSIVE` feature flag, fall back to the flat
`tryRemoveChildrenRecursive` path from the previous commit: `tryRemoveRecursive`
gets a `probably_flat` argument that it passes to the fallback.

`data/` is now removed before the final multi rather than in it, so the multi
no longer removes `data/`; the removal of the root node still fails with
`ZNOTEMPTY` while anything is left under it, keeping the leftover detection.
Any other error of the removal is thrown, as before.
A drop interrupted between the two steps leaves `data/` already gone, which
the leftover cleanup on the next `CREATE` handles (checked by hand: it removes
the remaining nodes and creates the table).

Debug build, embedded Keeper: dropping 999 keys 80-88 ms -> 28-32 ms;
200000 keys (fallback) 11.29 s -> 10.95 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author

it's fine if it fallbacks to non RemoveRecursive option but it's worth trying in case the table is small.

Done in a153247: dropTableData calls tryRemoveRecursive(data/, 1000, /*probably_flat=*/ true), and tryRemoveRecursive passes the new probably_flat argument to its tryRemoveChildrenRecursive fallback. Since data/ is gone after that call, the final multi no longer removes it: removing the root still fails with ZNOTEMPTY while anything is left, other errors of the removal throw as before, and a drop interrupted between the two steps is finished by the leftover cleanup on the next CREATE (checked by hand).

The test now drops 999 keys (one RemoveRecursive, no per-key request) and 1000 keys (over the limit, flat fallback, no per-key List). Dropping 999 keys takes 956-999 ms on master and 35-37 ms now (debug build, embedded Keeper).

Comment thread src/Storages/StorageKeeperMap.cpp Resolved
The previous commit removed data/ with a standalone RemoveRecursive
request before the multi that removes the table metadata. A drop
interrupted between the two leaves metadata/dropped without data/, and
the leftover cleanup of servers without this change starts with
removeChildrenRecursive(data/), which throws ZNONODE, so a CREATE on such
a server fails on every retry (measured with a master binary sharing the
Keeper: "No node, path .../data"; same code on 26.3, 26.8, 26.9, 26.10).

Put RemoveRecursive of data/ into the metadata multi instead. When Keeper
does not support it, or the multi fails with ZNOTEMPTY (the 1000-node
limit), remove the keys with the flat tryRemoveChildrenRecursive path
first and remove data/ with a plain Remove in the multi, as before. An
interrupted drop now always leaves data/ in place. The tryRemoveRecursive
signature change is no longer needed and is reverted.

The test now requires the RemoveRecursive to be in the same multi request
as the removal of metadata/, and no removal of data/ in any other request.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@PedroTadim

Copy link
Copy Markdown
Member

@antonio2368, you finish the review. The PR became too large for my threshold 😄

@antonio2368
antonio2368 added this pull request to the merge queue Nov 9, 2026
@Blargian
Blargian removed this pull request from the merge queue due to a manual request Nov 9, 2026
@Blargian
Blargian added this pull request to the merge queue Nov 9, 2026
Merged via the queue into ClickHouse:master with commit 98f37ee Nov 9, 2026
186 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 Nov 9, 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 comp-keeper ClickHouse Keeper (Raft-based coordination layer; ZooKeeper replacement) and ZooKeeper client lib... pr-performance Pull request with some performance improvements 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.

6 participants