Repository navigation
Fix flaky test_kafka_flush_by_time by reusing one Kafka producer - #112004
alexey-milovidov merged 1 commit into
Conversation
`test_kafka_flush_by_time` asserts `uniqExact` over the insert timestamp plus a row count of at least 15 after an 18 second window. The flush count, which the test is named for, is correct in every failure; the row count is what falls short. The produce loop sent one message per `k.kafka_produce` call, and that helper constructs a new `KafkaProducer` on every call, so every message paid a full client bootstrap including the `kafka-python` broker-version probe. In the failing CI run that probe took 2.1 s on one iteration (3.023 s for the whole iteration, against 0.107-0.113 s for the other 17), which cost about 3 messages out of a budget that had roughly half a message of slack. The result was 5 + 9 = 14 rows over 2 distinct flush timestamps, so the flush-by-time behaviour was correct and only the total was short. Both engines flushed on schedule (7841/7599 ms, versus 7537/7535 ms in the parametrization that passed on the same server 21 seconds earlier), so this is not a `StorageKafka` or `StorageKafka2` flush race. Hoist one producer out of the loop, reusing the existing `k.get_kafka_producer` and `k.producer_serializer` helpers that five other producers in this file already use, and close it after the producer thread is joined. Per-iteration cost drops from 0.107-3.02 s to a single send plus flush round trip. The assertion, the threshold of 15, the 18 second window and the 0.8 s message rate are unchanged. Measured after the change: producer constructions per run drop from 17 to 1; there are still exactly 2 in-window flushes in 25 of 25 runs and never 3, so the distinct-timestamp assertion stays live and the cadence stays on the `stream_flush_interval_ms` default at 7514-9020 ms; in-window row totals are 19 to 21 against the threshold of 15, instead of 14. 50 executions across both parametrizations pass. Verified in both directions by injecting the measured 2.9 s stall into the produce path: on master the test fails with the CI signature, with this change it passes even when the stall fires inside the loop, and reverting only the hoist under the same stall brings the failure back.
Internal second-model review (2 rounds) - 3 findings adjudicated, 2 fixedAn independent model reviewed this change against the ClickHouse review skill and the
1 - both-direction evidence (DISAGREE). The premise is correct and I do not dispute 2 and 3 - inline code for literal names (AGREE). |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-42:20260727-004100 |
|
cc @antaljanosbenjamin — could you review this? |
|
Workflow [PR], commit [dce0ed2] Summary: ✅ AI ReviewSummaryThis PR stabilizes Final VerdictStatus: ✅ Approve |
alexey-milovidov
left a comment
There was a problem hiding this comment.
LGTM, appreciate the work!
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
test_storage_kafka/test_batch_fast.py::test_kafka_flush_by_timeassertsafter an 18 second window. The first column, the number of flushes, is what the test is named
for and it is correct in every failure I looked at. The second column is the one that fails:
fewer than 15 rows arrive in time.
The produce loop sent one message per
k.kafka_producecall, and that helper builds a newKafkaProduceron every call, so every single message paid a full client bootstrap includingkafka-python's broker-version probe. In the failing CI run that probe took 2.1 s on one
iteration (3.023 s for the whole iteration, against 0.107-0.113 s for the other 17), which cost
about 3 messages out of a budget that had roughly half a message of slack. Result: 5 + 9 = 14
rows with
uniqExact(ts)= 2, i.e. the flush behaviour was correct and only the row count wasshort. Both engines flushed on schedule (7841/7599 ms, versus 7537/7535 ms in the run of the
other parametrization that passed 21 seconds earlier on the same server), so this is not a
StorageKafka/StorageKafka2flush race.This hoists one producer out of the loop, using the existing
k.get_kafka_producerandk.producer_serializerhelpers that five other producers in the same file already use, andcloses it after the producer thread is joined. The per-iteration cost drops from 0.107-3.02 s to
a single send plus flush round trip.
The assertion, the
>= 15threshold, the 18 second window and the 0.8 s message rate areunchanged. Nothing is weakened: no threshold lowered, no sleep lengthened, no retry wrapper, no
determinism tag.
Measurements on the fixed test, locally:
uniqExact(ts) == 2stays alive assertion and the flush cadence stays on the
stream_flush_interval_msdefault(measured 7514-9020 ms).
--count 25, both parametrizations) pass.Verified in both directions by injecting the measured 2.9 s stall into the produce path: on
master the test fails with
assert 2\t0 == 2\t1, matching the CI signature exactly, and withthis change it passes even when the stall fires inside the loop. Reverting only the hoist and
keeping the injected stall brings the failure back.
One behaviour note:
retries=15now guards producer construction once at test start instead ofper iteration, so a transient broker outage surfaces as a clear setup error rather than a silent
under-count. That matches how the other call sites in this file already use the helper.
CI report for the failure this addresses:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110344&sha=006dde1db1b596687365eddd7a30586f45c4f332&name_0=PR&name_1=Integration%20tests%20%28amd_asan_ubsan%2C%20db%20disk%2C%20old%20analyzer%2C%204%2F6%29
No related open issue found for this test.
The
kafka_setup_teardown/DROP DATABASE IF EXISTS test SYNCfailures filed under the sametest name are a different root cause and are not addressed here.