Skip to content

Fix flaky Kafka integration tests: retry get_admin_client on NoBrokersAvailable - #107337

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-flaky-kafka-get-admin-client-retry
Jun 12, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-flaky-kafka-get-admin-client-retry

Conversation

@groeneai

@groeneai groeneai commented Jun 12, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...

Description

helpers/kafka/common.py::get_admin_client built KafkaAdminClient with no retry. When the broker has not finished its metadata bootstrap yet, the constructor raises kafka.errors.NoBrokersAvailable and fails the test. It is called from the autouse kafka_setup_teardown fixture in several suites, so one transient hiccup fails every test in the file.

Fix: retry the same way get_kafka_producer already does (15 attempts, 1s apart), catching only NoBrokersAvailable so genuine failures still surface once retries are exhausted. retries defaults to 15, so all 62 existing callers are unaffected.

Observed via CIDB across 7 Kafka integration test files / 10 unrelated PRs in the last 30 days (e.g. test_kafka_zone_awareness, test_mv_target_missing, test_batch_fast, test_batch_slow_2), 0 master failures (PR-randomization-only signature).

Version info

  • Merged into: 26.6.1.723

…sAvailable

The KafkaAdminClient created in helpers/kafka/common.py::get_admin_client was
built with no retry. When the broker has not finished its metadata bootstrap
yet, its constructor raises kafka.errors.NoBrokersAvailable, which fails the
test. This most often happens in the autouse kafka_setup_teardown fixture, so a
single transient hiccup fails every test in the file.

Retry the same way get_kafka_producer already does (15 attempts, 1s apart),
catching only NoBrokersAvailable so genuine failures still surface after the
retries are exhausted. The retries argument defaults to 15, so all existing
callers are unaffected.

Observed via CIDB across 7 Kafka integration test files / 10 unrelated PRs in
the last 30 days (e.g. test_kafka_zone_awareness, test_mv_target_missing,
test_batch_fast, test_batch_slow_2), 0 master failures (PR-randomization-only
signature).

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

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. get_admin_client() builds KafkaAdminClient with no retry; when the broker is not yet reachable its constructor raises kafka.errors.NoBrokersAvailable. Simulated deterministically by stubbing the constructor to raise on the first N attempts: the old code raises on attempt 1; the new code succeeds on the first reachable attempt. CIDB signature: test_context_raw LIKE '%NoBrokersAvailable%' at common.py:114, 12 hits / 7 files / 10 unrelated PRs in 30d. (No real Kafka broker needed — and no C++ build, this is a Python test-helper change.)
b Root cause explained? The broker container may not have finished metadata bootstrap when a test (often the autouse kafka_setup_teardown fixture) calls get_admin_client(). The KafkaAdminClient constructor performs a synchronous version probe and raises NoBrokersAvailable if no broker answers in time. With no retry, that transient unavailability fails the call -> and because it is in an autouse fixture, every test in that file fails together.
c Fix matches root cause? Yes. The fix adds a bounded retry loop (15 attempts, 1s apart) around the KafkaAdminClient construction, catching only NoBrokersAvailable — directly absorbing the transient-unavailability window that is the cause. Mirrors the proven pattern already used by get_kafka_producer in the same file.
d Test intent preserved / new tests added? Yes. No test assertions changed; this is a connection helper. The retry catches ONLY NoBrokersAvailable and re-raises after exhausting retries, so a broker that is genuinely down still fails the test (no masking). retries is an optional kwarg (default 15), so all 62 existing callers are unchanged. No new test added — this is a flaky test-infra fix, not a product code bug.
e Both directions demonstrated? Yes (behavioral simulation). OLD: raises NoBrokersAvailable on first attempt (reproduces CI failure). NEW: succeeds after retrying past the transient window. NEW (broker permanently down): correctly raises after exhausting retries — real failures still detected.
f Fix is general, not a narrow patch? Yes. Fixed at the shared helper get_admin_client, so all Kafka integration suites benefit, not just test_kafka_zone_awareness. Verified the same no-retry pattern existed only here (siblings get_kafka_producer, kafka_create_topic, kafka_delete_topic already retry). Not a defensive symptom guard — the bad state (transient broker unavailability at setup time) is legitimately expected, so a bounded retry at the connection point is the correct level.

Session id: cron:clickhouse-worker-slot-2:20260612-134900

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @antaljanosbenjamin — could you review this test-infra fix? get_admin_client in the Kafka integration test helpers builds KafkaAdminClient with no retry, so a broker that hasn't finished metadata bootstrap raises NoBrokersAvailable and fails the test (often in the autouse kafka_setup_teardown fixture, failing every test in the file). It now retries 15x like get_kafka_producer already does. CIDB shows this across 7 Kafka suites / 10 unrelated PRs in 30d.

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Jun 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [32d38ab]

Summary: ✅


AI Review

Summary

This PR adds a bounded retry around helpers.kafka.common.get_admin_client so transient kafka.errors.NoBrokersAvailable during Kafka broker metadata bootstrap does not fail Kafka integration setup immediately. The patch is scoped to test infrastructure, catches only the reported transient exception, leaves existing callers compatible through the default retries value, and the available PR CI report has no failures; I did not find a blocker or major issue.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI summary

CI fully finished on HEAD 32d38ab. All test jobs green. No PR-caused failures.

Check Disposition
All Stateless / Integration / Unit / Stress / Build / Style / Fast test pass
Finish Workflow, Mergeable Check, PR pass
CH Inc sync (build failed) Infra (praktika CIDB-staging-log sync job, not a test). Same infra job that standing PR #106176 addresses. Not PR-caused.

This is a test-helper-only change (Python; bounded retry around KafkaAdminClient on NoBrokersAvailable, mirroring the existing get_kafka_producer pattern). AI review APPROVED. Ready for review.

@alexey-milovidov alexey-milovidov self-assigned this Jun 12, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 12, 2026
Merged via the queue into ClickHouse:master with commit 162afcd Jun 12, 2026
166 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 12, 2026
pull Bot pushed a commit to admariner/ClickHouse that referenced this pull request Aug 30, 2026
get_admin_client retries a broker that is not up yet, but its except named only
kafka.errors.NoBrokersAvailable. Constructing a KafkaAdminClient runs several
readiness probes, and kafka-python chooses the exception class by whether the
probe names a node: a cluster-wide probe raises NoBrokersAvailable, while the
controller probe (admin/client.py:219 -> _refresh_controller_id -> :279, node id
given) raises NodeNotReadyError. KafkaClient.check_version documents the split in
its own docstring (client_async.py:887-891); the leaf swallows NodeNotReadyError
and keeps looping only when node_id is None (:918-922), and the raise is
conn.py:990-992, reached when the socket to the controller does not come up
inside api_version_auto_timeout_ms, which defaults to 2000 ms.

So the loop covered half the class it was written for. I added that loop in
ClickHouse#107337 and narrowed it to one class there deliberately; that was wrong about the
class.

Surfaced as test_partition_affinity.py::test_partition_affinity_settings_validation
erroring out of the autouse fixture in Integration tests (amd_asan_ubsan, db
disk, old analyzer, 4/6):
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115294&sha=47d8d03a62c562bfc1e9a4adb9c2091d2f4a02fd&name_0=PR&name_1=Integration%20tests%20%28amd_asan_ubsan%2C%20db%20disk%2C%20old%20analyzer%2C%204%2F6%29

CIDB over 365 days at this call, split at the instant ClickHouse#107337 merged: before it,
10 NoBrokersAvailable and 1 NodeNotReadyError; after it, 0 NoBrokersAvailable and
2 NodeNotReadyError, against 23.3M Kafka integration test rows after versus 22.8M
before. The retry works, and the uncaught sibling class is all that is left. This
also corrects the "0 master failures" claim in ClickHouse#107337's own message: one master
row exists, 2026-06-02, on test_batch_slow_7.py.

Seventeen places constructed a KafkaAdminClient directly and had no retry at all.
Four of them, test_kafka_bad_messages/test.py:68,229,290 and test_1.py:29, are in
modules with no autouse fixture, so there that construction is the module's first
admin client. All seventeen now call the helper, following ClickHouse#108635, which
converged this suite's delete_topics calls onto the retrying helper the same way.
Exactly one KafkaAdminClient construction is left under tests/integration, inside
get_admin_client.

Deliberately not a bare except Exception: IncompatibleBrokerVersion and
UnrecognizedBrokerVersion are both retriable=False and must keep failing fast
instead of stalling for 15 seconds with a useless message.

Validated by injecting each exception at the constructor seam, with the fix
reverted as a negative control: NodeNotReadyError propagates on the first attempt
without the fix and is retried with it, NoBrokersAvailable is retried in both,
and both non-retriable classes propagate in both.

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

can be tested Allows running workflows for external contributors pr-ci 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