Skip to content

Keeper: index uncommitted nodes by parent so RemoveRecursive preprocessing does not scan all uncommitted nodes - #117778

Merged
davenger merged 3 commits into
masterfrom
keeper-remove-recursive-uncommitted-index
Sep 4, 2026
Merged

davenger merged 3 commits into
masterfrom
keeper-remove-recursive-uncommitted-index

Conversation

@davenger

@davenger davenger commented Sep 2, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

Keeper: preprocessing of a RemoveRecursive request no longer scans all uncommitted nodes. With a large uncommitted backlog, many concurrent RemoveRecursive requests could saturate the request processing thread and raise request latency above the client operation timeout.

Problem

KeeperMemNodesStorage::visitUncommittedRecursive collected the uncommitted children of the removed subtree by iterating the whole uncommitted_nodes map once per RemoveRecursive request. The cost of one request was therefore proportional to the number of uncommitted nodes in flight, not to the size of the removed subtree.

Normally the uncommitted set is small. It becomes large when Multi requests with tens of thousands of subrequests are in flight or when the commit backlog grows to hundreds of log entries. In that state a workload that sends several thousand small RemoveRecursive requests per second makes each of them scan tens of thousands of entries. On followers this work happens in pre_commit on the Raft append path, so their acknowledgements slow down and the leader's commit rate collapses. The effect feeds itself: slower preprocessing grows the backlog, and a larger backlog makes each request slower. The preprocessing thread was observed at close to 100 percent utilization while CPU, memory and disk on the Keeper nodes were far from their limits, and request latency reached tens of seconds with sessions expiring on the client operation timeout.

The scan has existed since RemoveRecursive was added (#69332, 2024-09). #86789 (2025-09) reduced it from one pass per visited node to one pass per request, which is the form fixed here.

Fix

KeeperMemNodesStorage keeps a new index uncommitted_children_by_parent, a map from a parent path to the set of uncommitted_nodes iterators of its children. Two helpers, addUncommittedNode and eraseUncommittedNode, are now the only code that inserts into or erases from uncommitted_nodes; they replace the five former direct call sites and keep the index in sync, including cleanup after commit and after rollback. visitUncommittedRecursive looks the uncommitted children of each visited node up by that node's path instead of scanning, and sorts them by path so the visit order stays deterministic across replicas.

The cost of a RemoveRecursive request is now proportional to the subtree it removes. KeeperLSMTNodesStorage already lists uncommitted children per node and is not changed.

Tests

Two tests in gtest_coordination_storage.cpp, parametrized over both storages.

TestRemoveRecursivePreprocessWithUncommittedBacklog preprocesses one Multi of 50 RemoveRecursive requests on small committed subtrees, first with no backlog and then with 100,000 uncommitted Create requests in flight, commits everything and checks the result. It asserts that the second preprocessing takes at most ten times the first or 20 ms. RelWithDebInfo measurements:

storage no backlog 100,000 uncommitted nodes
Mem, before this change 257 us 443,709 us
Mem, with this change 125 us 212 us
LSMT, before 547 us 599 us
LSMT, after 85 us 118 us

TestRemoveRecursiveSeesUncommittedChildrenUntilRollback checks that an uncommitted child still counts toward remove_nodes_limit and stops counting after the request that created it is rolled back.

The full *Coordination* unit test suite passes.


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

Version info

  • Merged into: 26.9.1.669 (included in 26.9 and later)
  • Backported to: 26.8.3.30

…cessing does not scan all uncommitted nodes

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e05536b]

Summary: ✅


AI Review

Summary

This PR replaces the KeeperMemNodesStorage scan over all uncommitted nodes during RemoveRecursive preprocessing with a parent-to-children index, and adds focused Coordination tests for backlog behavior and rollback visibility. I did not find a code-level correctness, safety, or performance regression in the current patch.

Missing context / blind spots
  • ⚠️ The workspace has no existing build artifacts, so I could not run the Coordination unit tests locally. A passing unit_tests_dbms --gtest_filter='*Coordination*' (or the corresponding CI job) on the PR head would close this gap.
  • ⚠️ From this environment I could only inspect the currently published Praktika build-profile-diff report, not a full CI run for the PR head.
Final Verdict

No blocking issues found in the current diff.

LLVM Coverage Report

Measured on commit e05536b.

Metric Baseline Current Δ
Lines 88.70% 88.70% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 81.00% 81.00% +0.00%

Changed lines: Changed C/C++ lines covered: 191/193 (98.96%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Sep 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 41 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: ac9ac244-d4f3-4de1-ae23-8ffcc2550b81
  • MIRAI run: 9e0b0bb9-d402-4683-9c1f-88a0f60a0969
  • PR check IDs:
    • clickbench_1114318_1788489330
    • clickbench_1114415_1788489334
    • clickbench_1114523_1788489355
    • tpch_adapted_1_official_1114640_1788489359
    • tpch_adapted_1_official_1114751_1788489367
    • tpch_adapted_1_official_1114884_1788489418

@clickhouse-gh

clickhouse-gh Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing e05536b04 with master dfde23932 (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

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

Job report

@davenger
davenger requested a review from antonio2368 September 3, 2026 06:49
@antonio2368 antonio2368 self-assigned this Sep 3, 2026
@davenger
davenger added this pull request to the merge queue Sep 4, 2026
Merged via the queue into master with commit b633813 Sep 4, 2026
175 checks passed
@davenger
davenger deleted the keeper-remove-recursive-uncommitted-index branch September 4, 2026 05:56
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 4, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 4, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 4, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 4, 2026
…o `RemoveRecursive` preprocessing does not scan all uncommitted nodes
clickhouse-gh Bot added a commit that referenced this pull request Sep 4, 2026
Backport #117778 to 26.8: Keeper: index uncommitted nodes by parent so `RemoveRecursive` preprocessing does not scan all uncommitted nodes
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 28, 2026
As suggested in review, exclude the timing assertion of
TestRemoveRecursivePreprocessWithUncommittedBacklog from ASan, TSan and MSan
builds instead of switching its clock, and restore the wall clock.

Both failures of the bound so far were one-off stalls of about 80-110 ms in
Unit tests (tsan), with a normal baseline and the /LSMT arm of the same process
unaffected. A thread CPU clock removes host preemption, but still counts any
sanitizer runtime work done on the measured thread.

The bound keeps running in Unit tests (amd_llvm_coverage), the unit-test job
built without a sanitizer, on PRs and master. There /Mem measures 121-139 us
without backlog and 221-264 us with it, against the 20 ms floor. With the
pre-ClickHouse#117778 storage code restored (0ddbd00 and 4f8f8f5
reverse-applied to KeeperMemNodesStorage only) the test still fails in a
non-sanitizer Debug build, at 318624 us against a 142 us baseline. The rest of
the test, including its functional assertions, still runs under sanitizers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
pull Bot pushed a commit to Mattlk13/ClickHouse that referenced this pull request Sep 29, 2026
…caling guard

Storage/CoordinationTest.TestRemoveRecursivePreprocessWithUncommittedBacklog/Mem
failed once in Unit tests (tsan) at 108883 us against its
max(base_us * 10, 20000) allowance, the first failure since the guard landed in
ClickHouse#117778 seventeen days earlier.

The product code is not at fault. Seven sibling Unit tests (tsan) runs that
morning measured 813-944 us to 1046-1232 us, a ratio of 1.17x to 1.53x, and the
/LSMT arm inside the failing process itself measured 1.2x while its neighbouring
tests took the same milliseconds as in a passing run: one sample moved and
nothing else did. The by-parent index added by ClickHouse#117778 still covers the
exercised path, and nothing reachable from preprocessRequest here is linear in
the uncommitted set. The observed rate is one failure in 27022 rows over 30 days.

The assertion bounds the wall time the host granted a single unrepeated ~1 ms
window, while the property it guards is the work the algorithm performs. A stall
the algorithm did not cause, such as thread preemption on a loaded runner, a
ThreadSanitizer stop-the-world shadow flush or kernel reclaim, adds its full
duration to a wall-clock sample and can never subtract from it, so roughly
107 ms of stall crossed the 20 ms floor.

Constructing the two Stopwatch objects with CLOCK_THREAD_CPUTIME_ID makes the
guard bound the quantity it actually claims. The allowance, the EXPECT_LE
message, the subtree sets, the 100000-node backlog and every functional
assertion are unchanged. Raising the threshold was rejected: surviving a 107 ms
stall would need a floor above 110 ms, and this batch cost hundreds of
milliseconds before ClickHouse#117778, so such a floor would leave the guard almost no
detection margin.

Validated in a Debug build. Reverse-applying 0ddbd00 and 4f8f8f5 on
KeeperMemNodesStorage only makes the fixed test fail at 323253 us of CPU time,
16x over the floor, so the guard still catches the regression it exists for. A
150 ms sleep_for injected into the measured window fails on wall clock
(150451 us) and passes on the new clock (587 us), while a 150 ms CPU spin fails
on both, so the new clock filters the observed failure class without going
blind. Fifty consecutive runs are 100/100 green with backlog samples of
233-840 us, against 236-495 us on wall clock before the change, and the maximum
base_us of 565 us keeps base_us * 10 below the 20 ms floor, so the effective
bound is unchanged.

Related: ClickHouse#117778

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-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants