Repository navigation
Do not retry a Kafka topic delete that already succeeded - #112039
alexey-milovidov merged 1 commit into
Conversation
kafka_delete_topic, the shared single-topic teardown helper for the whole Kafka integration family, retried a *successful* delete as if it had failed and could never converge, so an otherwise fully passing test was reported FAILED from teardown. UnknownTopicOrPartitionError (error code 3) on a DeleteTopicsRequest means "that topic does not exist", which for a delete is exactly the requested end state. It landed in the helper's generic `except Exception: retry`, so the helper asked the broker 49 more times to delete something already gone, burned ~23 s, and then raised. Read off the failing job's kafka-python client log (2026-07-24, topic Native_stream_err_1784918653): attempt 1's request was sent at 18:44:18.560; at 18:44:48.570 the client gave up on the response after its 30 s request timeout (the shard runs three xdist workers under TSan); the reconnect's metadata refresh at 18:44:49.077 reported ClusterMetadata(brokers: 1, topics: 3, groups: 0), i.e. only the three internal topics, proving the delete had in fact landed broker-side and only its response was lost; every attempt from 18:44:49.078 on returned error_code 3, which kafka/admin/client.py:409 raises on. So this is a classification bug, not a missing retry. More retries provably cannot help: each error-3 response came back in 1-4 ms, and the condition is permanent. Rather than adding a guard, remove the incorrect classification, which makes the teardown operation idempotent - the correct property for a delete. UnknownTopicOrPartitionError is now caught ahead of the generic handler and treated as the absent-topic path. It is not a blind return: it falls through to the helper's existing post-verification loop, which independently confirms the topic is no longer listed and still raises otherwise. The generic retry is untouched, since it is load-bearing for the transient RequestTimedOutError. kafka.errors marks UnknownTopicOrPartitionError retriable, but that describes stale-client-metadata semantics on the produce/fetch paths, where a later metadata refresh can make the topic appear. On the delete path the same code means the opposite, so keying the retry on that flag would preserve the bug. Not a single-test flake: since the retry loop was added (ClickHouse#108635, 2026-06-27) the signature accounts for 4 CI hits across 4 unrelated PRs, 3 test modules and 3 sanitizer flavours, all through this one frame. It also makes a routine operation of tests/casa_del_dolor/catalogs/kafkatest.py, which deletes randomly chosen topic names, stop burning the full 50-attempt loop. The regression test drives the helper with a stub admin client replaying that exact RPC sequence. A broker cannot be made to drop exactly one DeleteTopicsResponse on demand, and the passing path is byte-identical to today's, so a broker-driven test of this interleaving could only be non-deterministic - which is the property this change removes. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111572&sha=43ca40c8c0beb217e3edc66236a546332bb3c000&name_0=PR&name_1=Integration%20tests%20%28amd_tsan%2C%204%2F6%29 Related: ClickHouse#111572 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Internal second-model review (click to expand)Before opening this PR I reviewed the change independently of the author, and a second 💡 Nits (noted, not blocking)
Verified clean during review (each traced in code, not assumed): the specific handler One claim I could not re-verify at review time: the CIDB breadth table's row counts. The Session id: cron:clickhouse-review-slot-48:20260727-070900 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20260727-061400 |
|
cc @antaljanosbenjamin — could you review this? It is a test-only fix to the shared Kafka teardown helper: |
|
Workflow [PR], commit [770cf0e] Summary: ✅ AI ReviewSummaryThis PR makes Final Verdict✅ No actionable findings. |
Related: #83683
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
kafka_delete_topic(tests/integration/helpers/kafka/common.py), the sharedsingle-topic teardown helper for the whole Kafka integration family, retries a
successful delete as if it had failed, and can never converge. An otherwise fully
passing test is then reported FAILED from teardown.
UnknownTopicOrPartitionError(error code 3) on aDeleteTopicsRequestmeans "thattopic does not exist" — which for a delete is exactly the requested end state, not a
failure. Today it lands in the helper's generic
except Exception: retry, so the helperasks the broker 49 more times to delete something that is already gone, burns ~23 s, and
then raises.
The interleaving, read off the failing job's own kafka-python client log (topic
Native_stream_err_1784918653, all timestamps 2026-07-24):Attempt 1 succeeded broker-side and only its response was lost to the kafka-python
30 s request timeout (the shard runs three xdist workers under TSan). The reconnect's
metadata refresh proves it:
topics: 3is only the three internal topics(
_schemas,__confluent.support.metrics,__consumer_offsets) — the topic is alreadygone. Every later attempt therefore returns error code 3, and
kafka/admin/client.py:409raises on any non-NoErrorcode.So this is a classification bug, not a missing retry. More retries provably cannot
help: each error-3 response came back in 1-4 ms, and the condition is permanent. This PR
does not add a guard — it removes an incorrect failure classification, making the
teardown operation idempotent, which is the correct property for a delete.
The fix
UnknownTopicOrPartitionErroris caught ahead of the generic handler and treated as theabsent-topic path. It is not a blind
return: it falls through to the helper'sexisting post-verification loop, which independently confirms
topic not in admin_client.list_topics()and still raises if the broker claims the topicis absent while continuing to list it. The generic
except Exceptionretry is untouched— it is load-bearing for the transient
RequestTimedOutErrorin step 1.The
result.topic_error_codesreporting block is now guarded (resultcan legitimatelybe unset on the absent-topic path), and its loop variable is renamed so it no longer
shadows the
topicparameter that the listing loop reads.kafka.errors.UnknownTopicOrPartitionErrorcarriesretriable = True, but that flagdescribes stale-client-metadata semantics on the produce/fetch paths, where a later
metadata refresh can make the topic appear. On the delete path the same code means the
opposite, and the refresh above confirms it — so keying the retry on the library's flag
would preserve the bug.
Blast radius
Not a single-test flake. CIDB, window strictly after the retry loop was added
(#108635, merged 2026-06-27T00:27:38Z), filtered on
position(test_context_raw, 'kafka_delete_topic') > 0:test_kafka_bad_messages/test.py::test_bad_messages_parsing_streamtest_storage_kafka/test_poll_timeout_after_assignment.py::test_kafka_poll_timeout_restored_after_assignmenttest_kafka_bad_messages/test.py::test_bad_messages_parsing_dead_letter_queuetest_storage_kafka/test_batch_slow_5.py::test_formats_errors4 hits / 4 unrelated PRs / 3 modules / 3 sanitizer flavours / 0 master, all through the
same frame — two via a direct
kafka_delete_topiccall, two via thekafka_topiccontext manager's
finally. Over the same window, of all 363kafka-named FAIL/ERRORrows, every delete-topic teardown failure is this one signature (
via_helper = 4,any_delete_topics = 4,unknown_topic_any = 4,listing_loop_fail = 0) — onemechanism, no residue.
There is a second, independent motivation.
tests/casa_del_dolor/catalogs/kafkatest.pyalso calls this helper, and
KafkaTable.update_tabledeletes a randomly chosen topicname
t{random.randint(0, 19)}, so deleting a topic that does not exist is that caller'sroutine case. Today each such call burns the full 50-attempt / ~23 s loop before the
exception is swallowed by its broad
except Exception, making a normal fuzzer operationboth slow and log-noisy.
Tests
The regression test drives
kafka_delete_topicdirectly with a stub admin client andneeds neither a ClickHouse instance nor a broker. That shape is deliberate: a real broker
cannot be made to drop exactly one
DeleteTopicsResponseon demand, and the passing pathis byte-identical to today's, so a broker-driven test of this interleaving would be
non-deterministic — which is the property this PR exists to remove. The stub replays the
exact RPC sequence quoted above. It lives under
tests/integration/test_kafka_bad_messages/because that is the module which owns thereported failure, and because no other in-tree pytest harness covers
tests/integration/helpers; that path is also what makes the flaky check select andrepeat the module.
The five tests are: the observed interleaving now succeeds; a delete of an already-absent
topic succeeds without retrying; a topic that is still listed after error 3 still makes
the helper raise; a transient error followed by success still succeeds; and a permanently
failing delete RPC is still raised once
max_retriesis exhausted. The first two alsoassert a bounded attempt count — so a fix that merely looped to
max_retriesand thengave up would not pass — and that the listing check is reached rather than bypassed.
Verification:
origin/master: 2 failed, 3 passed — the two new oracles fail withthe exact CIDB signature
kafka.errors.UnknownTopicOrPartitionError: [Error 3], andthe three existing-behaviour assertions pass, so the test is discriminating rather
than uniformly red. With the fix: 5 passed.
two oracles. Replacing it with an unconditional
returnthat skips the listing checkbreaks the still-listed negative control. Both halves are load-bearing.
broker:
test_kafka_bad_messages/test.py(4 passed),test_storage_kafka/test_poll_timeout_after_assignment.py,test_storage_kafka/test_batch_slow_5.py.test_bad_messages_parsing_streamthepost-verification loop is reached the same number of times before and after the change
(
TOPICS LISTED24 vs 24,Topic ... deleted24 vs 24), and the new branch neverfires there (0 occurrences) — the fix does not short-circuit the authoritative check.
CI report for the #111572 occurrence:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111572&sha=43ca40c8c0beb217e3edc66236a546332bb3c000&name_0=PR&name_1=Integration%20tests%20%28amd_tsan%2C%204%2F6%29