Skip to content

Do not pre-allocate memory for the declared array size when reading ZooKeeper protocol arrays - #126914

Merged
antonio2368 merged 2 commits into
masterfrom
fix-keeper-oversized-array-prealloc
Oct 22, 2026
Merged

antonio2368 merged 2 commits into
masterfrom
fix-keeper-oversized-array-prealloc

Conversation

@antonio2368

@antonio2368 antonio2368 commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

The generic ZooKeeper protocol array reader (read(std::vector<T> &, ReadBuffer &) in ZooKeeperIO.h) resized the vector to the element count declared in the request before reading any element. A small request (for example a Create with a large declared ACL count) caused Keeper to allocate and zero-initialize up to several GB of memory, even when the request had no matching data.

The reader now grows the vector incrementally, with at most 4096 elements reserved up front. Allocated memory now follows the bytes that are actually read. Truncated input throws an exception on the first missing element after a small allocation.

A unit test in gtest_coordination.cpp covers this case.

Changelog category (leave one):

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

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

Keeper no longer pre-allocates memory for the array size declared in a request (for example the ACL list in Create) before it reads the array elements. Before this fix, a small malformed request could cause a large memory allocation.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.519-master (included in 26.11 and later)
  • Backported to: 26.10.2.78, 26.9.25.15, 26.8.34.13, 26.7.37.12, 26.3.60.2

The generic ZooKeeper array reader resized a `std::vector<T>` to the
declared element count before reading any element. A tiny `Create`
request declaring a huge ACL count forced a multi-GB zero-initialized
allocation held while the socket stayed open.

Grow the vector incrementally instead, reserving at most 4096 elements,
so allocated memory tracks bytes actually read. Truncated input now
throws on the first missing element after a bounded allocation.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [35e9ff8]

Summary: ✅


AI Review

Summary

The PR bounds initial allocation when decoding ZooKeeper arrays and appends elements only after successfully reading them. The revised regression test directly checks the allocation bound, addressing the previous finding. No unresolved findings remain.

Missing context / blind spots
  • ⚠️ Unit-test and unit-test bugfix-validation jobs for 35e9ff86c7fb895e4d9e559937314d6fec0b99cc are pending. No local C++ tests were run because this checkout has no configured build directory. The pending jobs will provide execution evidence for CreateRequestDoesNotPreallocateOversizedACLs.
Final Verdict

Status: ✅ Approve. No additional code changes requested; unit-test CI results remain pending.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 20, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-keeper ClickHouse Keeper (Raft-based coordination layer; ZooKeeper replacement) and ZooKeeper client lib... label Oct 20, 2026
The truncated-input case only checked that reading throws, which the old
`resize`-based reader also did after allocating the full declared count,
so the test passed with the bug restored on a machine with enough memory.

Name the reservation cap `MAX_ARRAY_PREALLOCATION`, declare twice that many
ACLs in the test, and assert that `acls.capacity` stays within the cap after
the exception.

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

clickhouse-gh Bot commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 35e9ff86c with master 074719710 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 5ba211c4d; 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

567 translation units recompiled, 4928 s compile time in total, 356 of them have a recent master baseline.

Translation units without a recent master baseline:

  • src/Databases/DatabaseReplicated.cpp: 12.0 s
  • src/Coordination/KeeperStateMachine.cpp: 9.3 s
  • src/Coordination/Changelog.cpp: 9.2 s
  • src/Common/ZooKeeper/ZooKeeperImpl.cpp: 9.0 s
  • programs/keeper-bench/LogRunner.cpp: 9.0 s
  • programs/keeper-utils/KeeperUtils.cpp: 8.8 s
  • src/Interpreters/FileCache/FileCache.cpp: 8.7 s
  • src/Coordination/KeeperDispatcher.cpp: 8.7 s
  • src/Databases/DatabaseOnDisk.cpp: 8.6 s
  • src/Storages/StorageStripeLog.cpp: 8.6 s
  • src/Backups/BackupsWorker.cpp: 8.5 s
  • src/Backups/RestorerFromBackup.cpp: 8.5 s
  • src/Storages/Kafka/StorageKafkaUtils.cpp: 8.4 s
  • src/Interpreters/DDLWorker.cpp: 8.3 s
  • src/Databases/DatabaseAtomic.cpp: 8.3 s
  • src/Storages/PostgreSQL/PostgreSQLReplicationHandler.cpp: 8.2 s
  • src/Common/ZooKeeper/ZooKeeper.cpp: 8.2 s
  • src/Backups/BackupCoordinationOnCluster.cpp: 8.1 s
  • src/Coordination/KeeperServer.cpp: 8.0 s
  • src/Analyzer/Resolve/IdentifierResolver.cpp: 7.9 s
  • ... and 96 more

Job report

@antonio2368
antonio2368 added this pull request to the merge queue Oct 22, 2026
@antonio2368 antonio2368 added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 22, 2026
Merged via the queue into master with commit 7ecdd26 Oct 22, 2026
179 checks passed
@antonio2368
antonio2368 deleted the fix-keeper-oversized-array-prealloc branch October 22, 2026 08:47
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 22, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 22, 2026
antonio2368 added a commit that referenced this pull request Oct 22, 2026
antonio2368 added a commit that referenced this pull request Oct 22, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Oct 22, 2026
Cherry pick #126914 to 26.3: Do not pre-allocate memory for the declared array size when reading ZooKeeper protocol arrays
robot-clickhouse added a commit that referenced this pull request Oct 22, 2026
… array size when reading ZooKeeper protocol arrays
robot-clickhouse-ci-2 added a commit that referenced this pull request Oct 22, 2026
Cherry pick #126914 to 26.7: Do not pre-allocate memory for the declared array size when reading ZooKeeper protocol arrays
robot-clickhouse added a commit that referenced this pull request Oct 22, 2026
… array size when reading ZooKeeper protocol arrays
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 22, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 22, 2026
Backport #126914 to 26.10: Do not pre-allocate memory for the declared array size when reading ZooKeeper protocol arrays
clickhouse-gh Bot added a commit that referenced this pull request Oct 22, 2026
Backport #126914 to 26.9: Do not pre-allocate memory for the declared array size when reading ZooKeeper protocol arrays
clickhouse-gh Bot added a commit that referenced this pull request Oct 22, 2026
Backport #126914 to 26.8: Do not pre-allocate memory for the declared array size when reading ZooKeeper protocol arrays
clickhouse-gh Bot added a commit that referenced this pull request Oct 22, 2026
Backport #126914 to 26.7: Do not pre-allocate memory for the declared array size when reading ZooKeeper protocol arrays
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 22, 2026
…e declared array size when reading ZooKeeper protocol arrays
antonio2368 added a commit that referenced this pull request Oct 23, 2026
Backport #126914 to 26.3: Do not pre-allocate memory for the declared array size when reading ZooKeeper protocol arrays
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-keeper ClickHouse Keeper (Raft-based coordination layer; ZooKeeper replacement) and ZooKeeper client lib... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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