Skip to content

Fix Kafka temporary partition locking iterating the unshuffled list - #108033

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-kafka-shuffle-temporary-locks
Jun 29, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-kafka-shuffle-temporary-locks

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 20, 2026 •

Copy link
Copy Markdown
Member

In KeeperHandlingConsumer::lockTemporaryLocksLocked, the code builds a copy of the available topic partitions and shuffles it (available_topic_partitions_copy), but then iterates the original, unshuffled available_topic_partitions. The shuffle result was therefore unused, so every replica walked the partitions in the same deterministic order each round, biasing all replicas toward the same partitions and increasing temporary-lock contention. This iterates the shuffled copy, as the surrounding std::shuffle clearly intends.

Testing: this is not covered by a targeted automated test because it concerns the randomized fairness of temporary-lock distribution across replicas, which is not deterministically observable; the existing Kafka integration tests continue to cover functional correctness. The fix is a one-line correction that makes the already-present std::shuffle take effect.

Note on the changelog category: although this corrects code that did not behave as written, its only user-visible effect is fairer distribution of temporary partition locks across replicas and reduced lock contention - query results were never affected. It is therefore categorized as an Improvement rather than a Bug Fix, which also matches the fact that the randomized-fairness behavior cannot be covered by a deterministic regression test.

This revives the change from the closed PR #102757 (closed for process reasons) and rebases it onto current master.

Closes: #102049
Related: #102757

Changelog category (leave one):

  • Improvement

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

The Keeper-based Kafka consumer now randomizes the order in which it acquires temporary partition locks across replicas, improving fairness of partition distribution and reducing lock contention. Previously the intended std::shuffle had no effect because the iteration used the unshuffled list.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

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

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]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [504ff74]

Summary: ✅


AI Review

Summary

This PR changes KeeperHandlingConsumer::lockTemporaryLocksLocked so temporary Kafka partition locks iterate the shuffled available_topic_partitions_copy instead of the original ordered list. The change matches the stated contract, keeps the existing std::shuffle behavior meaningful, and I found no correctness, safety, metadata, or test-evidence blockers in the current diff.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 20, 2026
@alexey-milovidov alexey-milovidov added pr-improvement Pull request with some product improvements and removed pr-bugfix Pull request with bugfix, not backported by default labels Jun 29, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.40% +0.00%
Functions 92.60% 92.60% +0.00%
Branches 77.60% 77.60% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 5/5 (100.00%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 1 line(s) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

LOL

@alexey-milovidov alexey-milovidov self-assigned this Jun 29, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 29, 2026
Merged via the queue into master with commit 4057f1c Jun 29, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-kafka-shuffle-temporary-locks branch June 29, 2026 05:31
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 29, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…e-temporary-locks

Fix Kafka temporary partition locking iterating the unshuffled list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product 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.

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

2 participants