Repository navigation
Fix flaky test_kafka_engine_put_errors_to_stream and _with_random_malformed_json - #105138
Conversation
…formed_json
Both tests in test_storage_kafka/test_batch_fast.py wait for the
Kafka 'Committed offset 128' log line and then assert the destination
row counts immediately:
instance.wait_for_log_line(f'{kafka_table}.*Committed offset 128')
assert TSV(instance.query(f'SELECT count() FROM test.{kafka_table}_data'))
== TSV('...')
assert TSV(instance.query(f'SELECT count() FROM test.{kafka_table}_errors'))
== TSV('64')
The offset commit and the materialized-view INSERT into the destination
MergeTree are not strictly ordered: the consumer side can log
'Committed offset 128' before the last MV block has finished flushing,
so the immediate SELECT undercounts (typically by exactly the last
block, i.e. assert 54 == 64).
Across both parametrized variants of each test the family has hit 23+
failures across 5 distinct unrelated PRs in the last 30 days (e.g.
ClickHouse#96886, ClickHouse#104902, ClickHouse#104905), with 0 master failures. Surfaced again by
@alexey-milovidov on
ClickHouse#104905 (comment).
The helper file in the same suite already acknowledges this anti-pattern
(tests/integration/helpers/kafka/common.py:463):
"Since everything is async and shaky when receiving messages from
Kafka, we may want to try and check results multiple times in a loop."
Most other tests in test_batch_fast.py already use query_with_retry
with a check_callback for exactly this reason (see e.g. line 1053,
1516). These two tests were the outliers.
Replace the bare assert with query_with_retry + check_callback. The
asserted values are unchanged, so no test intent is weakened: if the
count never reaches the expected value, query_with_retry returns the
last observed result and the surrounding assert ... == EXPECTED fails
loudly.
|
Pre-PR Validation Gate ( a) Deterministic repro? Partial. The race is between the Kafka b) Root cause explained? Yes. c) Fix matches root cause? Yes. d) Test intent preserved? Yes. The asserted values ( e) Both directions demonstrated? Partial. The fix's forward direction is verified — local smoke runs of both parametrized variants of f) Fix is general, not a narrow patch? Yes. The same race exists in both I'm not adding |
|
cc @antaljanosbenjamin — could you review this? It replaces a brittle |
|
Workflow [PR], commit [5b102c4] Summary: ✅ AI ReviewSummaryThis PR updates two flaky Kafka integration tests in Final VerdictStatus: ✅ Approve |
|
I am only writing this from my head, haven't checked it, but I thinkwe don't commit the offset until the data is written, therefore I am not sure if that comment (merge tree runs in a separate path and my not be flushed yet) is accurate. I will double check this on Monday. |
|
@antaljanosbenjamin Thanks for catching this — you're right, and I want to correct the PR description before you spend Monday double-checking. I traced the offset-commit path in
So my "MV The merged fix ( The actual cause is still open. CIDB shows two distinct failure shapes over the last 30 days (all 13 numeric-assertion hits on sanitizer builds —
The |
test_kafka_engine_put_errors_to_streamandtest_kafka_engine_put_errors_to_stream_with_random_malformed_jsonintests/integration/test_storage_kafka/test_batch_fast.pyare chronically flaky. Across both parametrized variants of each test the family has hit 23+ failures across 5 distinct unrelated PRs in the last 30 days, with the most common symptom beingAssertionError: assert 54 == 64(i.e. 10 expected error rows missing) onIntegration tests (amd_asan_ubsan, db disk, old analyzer, 2/6). The most recent occurrences are on#96886,#104902,#104905.Surfaced again by @alexey-milovidov on #104905 (comment) — no open issue exists for this family.
Root cause: both tests wait for the Kafka
Committed offset 128log line and then assert the destination row counts immediately:Committed offset 128only tells us the Kafka consumer side committed the offset; the materialized-viewINSERTinto the destinationMergeTreeruns in a separate path and is not strictly ordered with the offset commit. So the immediateSELECT count() FROM ..._errorscan fire while the last block(s) are still in flight, undercounting by typically 10 rows (one block atkafka_max_block_size-aligned boundaries underkafka_handle_error_mode = stream).The helper file in the same suite already acknowledges this anti-pattern:
tests/integration/helpers/kafka/common.py:463says "Since everything is async and shaky when receiving messages from Kafka, we may want to try and check results multiple times in a loop." Most other tests intest_batch_fast.pyalready useinstance.query_with_retry(..., check_callback=lambda x: int(x) == N)for exactly this reason (see e.g. line 1053, 1516). These two tests were the outliers.Fix: replace the bare assertions in both tests with
instance.query_with_retry(..., check_callback=lambda x: int(x) == EXPECTED). The polled comparison is the same value the test was always asserting — no test intent is weakened. If the count never reaches the expected value,query_with_retryreturns the last observed count and the surroundingassert ... == EXPECTEDfails loudly, so the test still catches real undercounts; it just gives the materialized-view flush a window to complete first.Verification
Local smoke run with the fix on
Integration tests (amd_tsan, 1/6)passes both parametrized variants oftest_kafka_engine_put_errors_to_stream(and is currently rerunning the_with_random_malformed_jsonvariants in another container). Reproducing the original ~10% failure rate locally is impractical (each iteration is a full Docker-orchestrated test), but the diagnosis matches the observed symptom exactly: a constant 10-row undercount in the_errorstable, never an over-count. The PR's own CI run will exercise the fix at scale.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Not for changelog — test-only fix.
Documentation entry for user-facing changes
No documentation needed.
cc @alexey-milovidov — surfaced this on #104905.
Version info
26.5.1.714