Skip to content

Retry Kafka admin client construction on NodeNotReadyError too - #116700

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:retry-kafka-admin-client-node-not-ready
Aug 30, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:retry-kafka-admin-client-node-not-ready

Conversation

@groeneai

@groeneai groeneai commented Aug 27, 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

Related: #115294
Related: #107337
Related: #108635

test_storage_kafka/test_partition_affinity.py::test_partition_affinity_settings_validation errored
with kafka.errors.NodeNotReadyError out of the autouse fixture, not the test body:
Integration tests (amd_asan_ubsan, db disk, old analyzer, 4/6).

get_admin_client already retries a broker that is not up yet, but its except named only
kafka.errors.NoBrokersAvailable. A KafkaAdminClient runs several readiness probes on construction,
and kafka-python picks the exception class by whether the probe names a node: a cluster-wide probe
raises NoBrokersAvailable, the controller probe raises NodeNotReadyError, as
KafkaClient.check_version's own docstring documents. So the loop covered half the class it was
written for. I added it in #107337, so the single-class catch was mine.

CIDB over 365 days at this call, split at the instant #107337 merged: 10 NoBrokersAvailable and 1
NodeNotReadyError before, 0 and 2 after, against a larger denominator. It reached master once, on
2026-06-02, before the loop existed.

Separately, 17 places constructed a KafkaAdminClient directly with no retry, four of them in modules
with no autouse fixture, where that construction is the module's first admin client. All 17 now call
the helper, following #108635, which converged this suite's delete_topics calls the same way. The one
direct construction left, in the casa_del_dolor fuzzer, is in no CI job.

tests/integration/test_kafka_bad_messages/test_admin_client_helper.py covers this with a stub
constructor and no broker: two ids for the classes to retry, two for IncompatibleBrokerVersion and
UnrecognizedBrokerVersion, which are retriable=False and must fail fast rather than stall for 15
seconds. Three mutations of the except clause redden pairwise disjoint subsets of the four ids, so no
id is redundant; the first is byte-identical to the clause before this change, so it doubles as the
negative control.

Not fixed here: a failed attempt strands a partially constructed client, because kafka-python builds
its internal client before the probes that can raise.


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

Version info

  • Merged into: 26.9.1.400 (included in 26.9 and later)

groeneai and others added 2 commits August 27, 2026 10:47
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]>
`get_admin_client` retries the construction of a `KafkaAdminClient` so that a
broker which is not up yet does not fail a test. Nothing in the tree failed if
that `except` clause was narrowed back to a single class, so add
tests/integration/test_kafka_bad_messages/test_admin_client_helper.py: two
functions and four parametrized ids, driving the helper with a stub constructor
and needing neither a Kafka broker nor a ClickHouse instance.

Two ids cover the readiness class that has to be retried, `NoBrokersAvailable`
when the constructor's probe is cluster-wide and `NodeNotReadyError` when it
targets a specific node. Two cover the version faults that have to fail fast,
`IncompatibleBrokerVersion` and `UnrecognizedBrokerVersion`. Both functions
assert the number of attempts; without that an arm which succeeded on its first
attempt would pass as well, so the ids would show only that nothing escaped and
never that a retry happened.

Three mutations of the `except` clause redden pairwise disjoint sets of those
four ids, so no id is redundant and no mutation is a blanket break. Dropping
`NodeNotReadyError` from the tuple reddens its id alone, dropping
`NoBrokersAvailable` reddens its id alone, and a bare `except Exception` reddens
only the two version ids, which then report the loop's exhaustion message
instead of the version mismatch itself. The first of the three mutations is
byte-identical to the line as it stood before the retry was widened, so it is
also the negative control.

The test passes `retries=3` explicitly. Each failed attempt sleeps one second,
and three is the smallest budget that admits two failures followed by a success.

Also wrap the two class names in the comment above the loop in inline code
blocks, as .claude/CLAUDE.md asks for.

CI report of the failure this covers:
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
Related: ClickHouse#115294

Co-Authored-By: Claude Opus 5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Aug 27, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex). Two rounds: a full pass on the
first commit returned 1 finding, and a delta re-review of the fix commit returned 0.

# Sev Finding Verdict Evidence / action
1 ⚠️ The readiness retry contract has no deterministic regression test: the touched tests only reach get_admin_client after a real broker is up, so they pass with the new catch reverted (tests/integration/helpers/kafka/common.py:154-167) AGREE, fixed @ 7239e42 Added a broker-free test module in the style of the kafka_delete_topic tests from #112039, stubbing the constructor: two functions, four ids, asserting both that each readiness class is retried and that both retriable=False version classes still fail on the first attempt. Three mutations of the except clause redden pairwise disjoint subsets of the four ids, and the first is byte-identical to the clause before this change, so it doubles as the negative control
2 💡 The new comment does not wrap the two class names in inline code blocks, which .claude/CLAUDE.md requires for comments AGREE, fixed @ 7239e42 Backticked; the comment still states only the local property, at two lines
3 💡 The one same-shape KafkaAdminClient construction outside tests/integration, in tests/casa_del_dolor/catalogs/kafkatest.py, is not converted DISAGREE grep -rn casa_del_dolor ci/ returns nothing (positive control: 15 hits for test_storage_kafka), and tests/casa_del_dolor/dolor.py is a standalone tool with an opt-in --with-kafka flag, so no CI job reaches it. The description says so rather than changing it
4 💡 Direct KafkaProducer constructions that bypass get_kafka_producer have the same readiness race DISAGREE Different helper and different class: KafkaClient's own probe is cluster-wide, so that seam raises NoBrokersAvailable. CIDB over 365 days shows 4 such rows, all before #107337 merged and none since

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-10:20260827-114900

@clickhouse-gh clickhouse-gh Bot closed this Aug 27, 2026
@clickhouse-gh clickhouse-gh Bot reopened this Aug 27, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes for the defect: injecting NodeNotReadyError at the constructor seam makes the base except propagate it on the first attempt, every run. The CI race itself (a controller socket missing the 2000 ms api_version_auto_timeout_ms budget under ASAN+UBSAN on a 6-way shard) is not triggerable on demand and I do not claim it is, which is why the oracle is injection plus a negative control rather than a timing story.
b Root cause explained? KafkaAdminClient construction runs several readiness probes, and kafka-python picks the exception class by whether the probe names a node: a cluster-wide probe raises NoBrokersAvailable, the controller probe (admin/client.py:219 -> :279) raises NodeNotReadyError, documented in KafkaClient.check_version's docstring. get_admin_client caught only the first, so its retry loop covered half the class it exists for.
c Fix matches root cause? Yes. The except is widened to exactly the two-element readiness class at the seam that already exists. No bound widened, no tag added, no data reduced, no second mechanism. Rejected: bare except Exception (swallows IncompatibleBrokerVersion / UnrecognizedBrokerVersion, both retriable=False, turning a version fault into a 15 s stall); except KafkaError gated on retriable (also admits KafkaConnectionError and RequestTimedOutError, 0 occurrences at this seam); raising api_version_auto_timeout_ms (widens a bound and leaves the guard unable to catch its class).
d Test intent preserved / new tests added? No assertion is changed, weakened or removed; part 2 is a call-site substitution onto an already-tested helper. A regression test is added: tests/integration/test_kafka_bad_messages/test_admin_client_helper.py, two functions and four parametrized ids driving the helper with a stub constructor, needing neither a broker nor a server. Both functions assert the number of attempts, without which an arm that succeeded on its first attempt would pass as well.
e Both directions demonstrated? Yes, at test level. Three mutations of the except clause redden pairwise disjoint sets of the four ids: dropping NodeNotReadyError reddens its id alone, dropping NoBrokersAvailable reddens its id alone, and a bare except Exception reddens only the two version ids, which then report the loop's exhaustion message instead of the version mismatch. The first mutation is byte-identical to the line as it stood before the widening, so it is also the negative control. Each run prints the except line it loaded, so it certifies which tree produced its numbers.
f Fix is general across code paths? Yes. Every other retry loop in the same helper (get_kafka_producer, kafka_create_topic, kafka_delete_topic, the delete_topics loop) already catches bare Exception, so none shares this gap; and the 17 direct KafkaAdminClient(...) constructions that bypassed the helper entirely are converted, leaving exactly one construction in tests/integration, inside the fixed function.
g Fix generalizes across inputs (params/datatypes/wrappers)? The input axis for an except clause is the exception class, and that matrix is closed and exercised: the two readiness classes are retried, the two configuration faults still propagate, all four measured on both trees. Type wrappers do not apply.
h Backward compatible? (maintainer-approved exception only) N/A, test harness only: no product code, no setting default, no SettingsChangesHistory.cpp, no serialization format, no experimental gate. On the success path behaviour is byte-identical everywhere; the only change is on a path that fails today.
i Invariants and contracts preserved? get_admin_client's contract, return a constructed client or raise after retries attempts, is unchanged; only the retryable set grows. The converted sites differ only in the exhaustion case, where a generic Exception replaces a kafka class: exactly one except .*kafka.errors exists outside the helper (test_batch_fast.py:1364, a consumer-group call), and none of the 17 sites is inside a try:, so no caller stops matching. Diagnostics do not degrade either, since str(NodeNotReadyError()) is 'NodeNotReadyError'. Configuration faults still leave the loop immediately.

Session id: cron:clickhouse-impl-slot-40:20260827-112800

@clickhouse-gh

clickhouse-gh Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7239e42]

Summary: ✅


AI Review

Summary

This PR routes the touched Kafka integration tests through get_admin_client, widens that helper to retry NodeNotReadyError alongside NoBrokersAvailable, and adds a deterministic regression test that proves the retry/fail-fast split for the relevant constructor exceptions. The change matches the reported CI failure mode, the remaining direct KafkaAdminClient construction in tests/integration is the helper itself, and I did not find an unresolved correctness or test-coverage gap in the current diff.

Final Verdict

Status: ✅ Approve

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @kssenii, could you review this? The Kafka integration-test helper already retried a broker that is not up yet but caught only NoBrokersAvailable, while KafkaAdminClient construction raises NodeNotReadyError when its readiness probe targets a specific node, so the controller probe fell straight through the retry loop.

@alexey-milovidov alexey-milovidov self-assigned this Aug 30, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 30, 2026
Merged via the queue into ClickHouse:master with commit f14f29d Aug 30, 2026
360 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 Aug 30, 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 groeneai-origin-request PR origin: a maintainer pinged or directed groeneai 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.

3 participants