Skip to content

Make test_multi_consumer_with_partition_affinity deterministic - #120556

Merged
antaljanosbenjamin merged 2 commits into
ClickHouse:masterfrom
aparajita31pandey:fix/kafka2-quota-test-flakiness
Sep 25, 2026
Merged

antaljanosbenjamin merged 2 commits into
ClickHouse:masterfrom
aparajita31pandey:fix/kafka2-quota-test-flakiness

Conversation

@aparajita31pandey

@aparajita31pandey aparajita31pandey commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Problem

test_multi_consumer_with_partition_affinity, added in #116843, has been failing at ~0.06% (8 of 11654 runs). Every genuine failure is this test alone in an otherwise healthy job, on sanitizer builds, with:

kafka_aff_s2_r2 never acquired any partition within 240s

It asserts a liveness property, that every replica holds at least one partition, which StorageKafka2 does not currently guarantee on a short timescale. Details in #116844.

Why a replica can legitimately hold nothing

The anti-starvation guard in KeeperHandlingConsumer::lockTemporaryLocksLocked is gated on the consumer already holding a temporary lock:

if (tmp_locks_quota > 0 && has_replica_without_locks)
{
    tmp_locks_quota = 0;   // release, give the starved replica a chance
    return;
}

A sibling consumer on the same replica whose tmp_locks_quota is 0 skips the guard entirely and takes a temporary lock, even while a peer replica holds nothing. This test produces no data after setup, so a poll cycle runs to MAX_FAILED_POLL_ATTEMPTS and lock holders only re-evaluate roughly every ~89 s. Against the 240 s wait, the starved replica gets about two chances. In the failures analysed, the release window was open for 36 ms while the starved replica polls every 573 ms; in one case it lost by 2 ms.

None of this is specific to the assertion being wrong about the product .

What this PR changes

Updated the testcase to fix assertion based on below

max partitions a replica may legitimately hold = node_quota + num_consumers

A replica can hold node_quota permanent locks plus at most one temporary lock per consumer — lockTemporaryLocksLocked only ever flips tmp_locks_quota between 0 and 1 — so 4 + 2 = 6 is a hard upper bound that holds at any instant and needs no convergence.

The resize is what makes that assertion worth having: 6 < P_shard = 8, so the pre-#116843 behaviour (every consumer receiving the full node_quota, one replica taking the entire shard) trips it. At the old sizing the ceiling would have been 2 + 4 = 6 against a shard of 4 — true for every possible distribution, and therefore never able to fail. The docstring records this so the numbers are not "simplified" back later.
Updated the testcase to fix assertion based on below

max partitions a replica may legitimately hold = node_quota + num_consumers (as 1 consumer can hold one temp lock).

tmp_locks_quota would always oscillate between 0 and 1

void KeeperHandlingConsumer::lockTemporaryLocksLocked(
    const TopicPartitions & available_topic_partitions, const bool has_replica_without_locks) TSA_REQUIRES(topic_partition_locks_mutex)
{
    /// There are no available topic partitions, so drop the quota to 0
    if (available_topic_partitions.empty())
    {
        LOG_TRACE(log, "There are no available topic partitions to lock");
        tmp_locks_quota = 0;
        return;
    }
    LOG_TRACE(log, "Starting to lock temporary locks");

    /// We have some temporary lock quota, but there is at least one replica without locks, let's drop the quote to give
    /// the other replica a chance to lock some partitions
    if (tmp_locks_quota > 0 && has_replica_without_locks)
    {
        LOG_TRACE(log, "There is at least one consumer without locks, won't lock any temporary locks this round");
        tmp_locks_quota = 0;
        return;
    }

    /// We have some temporary lock quota, but it is greater than the number of available topic partitions, so we will
    /// reduce the quota to give other replicas a chance to lock some partitions
    if (tmp_locks_quota > 0 && tmp_locks_quota <= available_topic_partitions.size())
    {
        LOG_TRACE(log, "Reducing temporary locks to give other replicas a chance to lock some partitions");
        tmp_locks_quota = std::min(tmp_locks_quota - 1, available_topic_partitions.size() - 1);
    }
    else
    {
        tmp_locks_quota = std::min(available_topic_partitions.size(), tmp_locks_quota + 1);
    }
    LOG_INFO(log, "The consumer can take {} temporary locks in the current round", tmp_locks_quota);


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

Version info

  • Merged into: 26.10.1.659-master (included in 26.10 and later)
  • Backported to: 26.9.3.36

@aparajita31pandey
aparajita31pandey marked this pull request as draft September 17, 2026 09:58
@aparajita31pandey aparajita31pandey changed the title Update test_multi_consumer_with_partition_affinity Make test_multi_consumer_with_partition_affinity deterministic Sep 17, 2026
@clickhouse-gh clickhouse-gh Bot added comp-testing Test content only (no production code). external Author is not a member of the ClickHouse organization labels Sep 17, 2026
@aparajita31pandey
aparajita31pandey marked this pull request as ready for review September 18, 2026 05:08
@rschu1ze rschu1ze added the can be tested Allows running workflows for external contributors label Sep 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [900a831]

Summary: ✅


AI Review

Summary

This PR tries to make test_multi_consumer_with_partition_affinity deterministic by replacing the old "every replica must quickly acquire a partition" assertion with a looser ceiling check and a larger shard. The quota-log assertions still cover the shard-scoped math, but the new helper weakens the end-to-end signal enough that the test can now pass without proving any replica ever acquired a partition, so I would still request changes.

Findings

⚠️ Majors

  • [tests/integration/test_storage_kafka/test_multi_consumer_quota.py:196-214,414-430] [dismissed by author -- https://github.com/Make test_multi_consumer_with_partition_affinity deterministic #120556#discussion_r4062539686] wait_for_shard_partitions still returns the first non-empty snapshot and now also returns set() on timeout. That makes the new affinity check vacuous on the exact failure mode this PR is trying to remove: if a replica never acquires any partition within 240 seconds, both all(pid % shard_count == effective for pid in held) and len(held) <= expected_node_quota + num_consumers pass on the empty set. It can also pass an early 1- or 4-partition snapshot before the sibling consumer takes the rest, so the ceiling described in the docstring is not what the code is actually grading.
    Suggested fix: keep polling until the shard's two replicas collectively own all 8 shard partitions or until one replica exceeds the ceiling, then assert the per-replica bound against that settled snapshot.
Final Verdict

Status: ⚠️ Request changes

  • Restore a positive end-to-end condition for the affinity path so the test cannot pass on timeout or on an early partial snapshot.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 21, 2026
@antaljanosbenjamin
antaljanosbenjamin added this pull request to the merge queue Sep 25, 2026
Merged via the queue into ClickHouse:master with commit d8b1c8c Sep 25, 2026
172 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 25, 2026
@PedroTadim

Copy link
Copy Markdown
Member

Backporting to fix CI

@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 25, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 25, 2026
Backport #120556 to 26.9: Make test_multi_consumer_with_partition_affinity deterministic
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 15, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-kafka Kafka table engine: consumers and producers on librdkafka, the Kafka2 engine that keeps offsets i... label Nov 22, 2026
@clickhouse-gh clickhouse-gh Bot removed the comp-testing Test content only (no production code). label Nov 22, 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-kafka Kafka table engine: consumers and producers on librdkafka, the Kafka2 engine that keeps offsets i... external Author is not a member of the ClickHouse organization 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-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo v26.9-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants