Skip to content

Fix Kafka temporary partition lock loop iterating unshuffled list - #102757

Closed
SAY-5 wants to merge 1 commit into
ClickHouse:masterfrom
SAY-5:fix-kafka-temporary-locks-shuffle
Closed

SAY-5 wants to merge 1 commit into
ClickHouse:masterfrom
SAY-5:fix-kafka-temporary-locks-shuffle

Conversation

@SAY-5

@SAY-5 SAY-5 commented Apr 15, 2026

Copy link
Copy Markdown

Closes #102049.

src/Storages/Kafka/KeeperHandlingConsumer.cpp::lockTemporaryLocksLocked builds a shuffled copy of available_topic_partitions to randomize the order in which replicas race for temporary locks:

auto available_topic_partitions_copy = available_topic_partitions;
pcg64 generator(randomSeed());
std::shuffle(available_topic_partitions_copy.begin(), available_topic_partitions_copy.end(), generator);

for (const auto & tp : available_topic_partitions)   // <-- iterates the *original*
{
    ...
}

The for-each loop iterates available_topic_partitions (the untouched original) instead of available_topic_partitions_copy, so the std::shuffle is dead code and every replica walks the same deterministic order each round — biasing all replicas toward the same partitions and increasing lock contention.

Iterate available_topic_partitions_copy so the shuffle actually has an effect.

Verified against current master.

src/Storages/Kafka/KeeperHandlingConsumer.cpp::lockTemporaryLocksLocked
constructs a shuffled copy of available_topic_partitions to randomize the
order in which replicas attempt temporary partition locks, but the
subsequent for-each loop iterates the original (unshuffled) container —
the std::shuffle call is dead code and every replica walks the partition
list in identical deterministic order. This biases all replicas toward
the same partitions, harms fairness and increases lock contention.

Iterate available_topic_partitions_copy in the loop so the shuffle
actually takes effect.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


SAY-5 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

github-merge-queue Bot pushed a commit that referenced this pull request Jun 29, 2026
In KeeperHandlingConsumer::lockTemporaryLocksLocked the code shuffled a copy
(available_topic_partitions_copy) but then iterated the original, unshuffled
available_topic_partitions, so the std::shuffle had no effect and every replica
walked partitions in the same order, biasing lock acquisition.

Closes: #102049
Related: #102757

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
In KeeperHandlingConsumer::lockTemporaryLocksLocked the code shuffled a copy
(available_topic_partitions_copy) but then iterated the original, unshuffled
available_topic_partitions, so the std::shuffle had no effect and every replica
walked partitions in the same order, biasing lock acquisition.

Closes: ClickHouse#102049
Related: ClickHouse#102757

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

lockTemporaryLocksLocked iterates unshuffled original instead of shuffled copy, making shuffle dead code

3 participants