Skip to content

Fix flaky test_kafka_formats_with_broken_message - #111701

Merged
antaljanosbenjamin merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-flaky-kafka-formats-with-broken-message
Jul 24, 2026
Merged

antaljanosbenjamin merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-flaky-kafka-formats-with-broken-message

Conversation

@groeneai

@groeneai groeneai commented Jul 23, 2026 •

Copy link
Copy Markdown
Collaborator

Fix flaky test_kafka_formats_with_broken_message

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

Found by @ alexey-milovidov on #109369 (report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109369&sha=119f160ca922641691ce1c4bf7dffbe6119a5bb1&name_0=PR&name_1=Integration%20tests%20%28amd_asan_ubsan%2C%20db%20disk%2C%20old%20analyzer%2C%204%2F6%29). Unrelated to that PR.

The per-format setup produced the whole batch (including the tail broken message) to the topic, then created the Kafka table, the data materialized view, and the errors materialized view in sequence. Creating the first materialized view starts the streaming loop, so on slow (sanitizer) builds the loop could consume and commit the broken message before the errors materialized view was attached. The broken message is then past the committed offset and never reaches the errors view: the data view has its rows but the errors view stays empty, and the test fails at "Error row for format X did not appear in kafka_errors_X_mv".

Fix: create both materialized views, detach/re-attach the Kafka table, and only then produce the messages, so nothing is consumable until both views exist. This mirrors the same fix already accepted for the sibling test test_kafka_engine_put_errors_to_stream in #107025.

CIDB: 0 master failures, 25 PR failures over 45 days (both create-query parameters, random format each time, across amd_msan / amd_asan_ubsan / amd_tsan / amd_llvm_coverage). #108252 only added a diagnostic guard and a retry bump; it did not address this ordering race.

Version info

  • Merged into: 26.8.1.92 (included in 26.8 and later)
  • Backported to: 26.7.6.42, 26.6.4.31, 26.3.33.66

The per-format setup produced all messages (including the tail broken
message) to the topic before the destination materialized views existed,
then created the Kafka table, the data view and the errors view in
sequence. Creating the first materialized view starts the streaming loop,
so on slow builds the loop could consume and commit the broken message
before the errors view was attached. The broken message is then past the
committed offset and never reaches the errors view: the data view has its
rows but the errors view stays empty, failing the test at
"Error row for format X did not appear in kafka_errors_X_mv".

Create both materialized views, detach/re-attach the Kafka table, and only
then produce the messages, so nothing is consumable until both views exist.
This mirrors the fix accepted for the sibling test
test_kafka_engine_put_errors_to_stream in ClickHouse#107025.

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

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. A standalone harness used system.kafka_consumers.{num_messages_read,num_commits} to force the exact window: produce, create only the data view (streaming starts), wait until the batch is committed, then create the errors view. The errors view is then empty every time (the CI assert 0 > 0).
b Root cause explained? Yes. Creating the first materialized view starts the streaming loop. On slow builds the loop consumes and commits the tail broken message before the errors view is attached, so that message is past the committed offset and never reaches the errors view; the data view still has its good rows.
c Fix matches root cause? Yes. Produce only after both views exist, and detach/re-attach the Kafka table so the loop restarts with both dependents. Nothing is consumable before the errors view exists. Not a retry/timeout band-aid.
d Test intent preserved / new tests added? Yes. All broken-message and data-row assertions are unchanged; no assertion weakened, no no-parallel, no retry bump, no data reduction.
e Both directions demonstrated? Yes. Pre-fix ordering leaves the errors view empty (reproduces the failure); the fix routes the error row every time. Real test both params passed, plus --count 8 --workers 2 = 16/16 clean.
f Fix is general across code paths? N/A (test-only fix, not a source-code bug). The sibling test test_kafka_engine_put_errors_to_stream already carries this exact pattern (#107025).
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test-only). The change applies uniformly to every format and both create-query parameters iterated in the loop.
h Backward compatible? N/A (test-only; no user-facing behavior, setting, or format change).
i Invariants and contracts preserved? Yes. The topic is created empty first and no message is consumed before both views exist, so no offset/state invariant is violated. Detach/attach between DDL and produce is the established idiom (#107025).

Session id: cron:clickhouse-author-slot-7:20260723-214600

@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; 0 findings; bounded: 1 full pass + 1 fix round + 1 delta recheck).

Coverage: read the complete changed test, fixtures, helpers, old/new Kafka implementations, the accepted sibling tests, PR metadata, and the supplied CI logs. No blockers, majors, or nits raised.

Severity: none. No AGREE/DISAGREE verdicts required (empty finding set).

Session id: cron:clickhouse-author-slot-7:20260723-214600

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jul 23, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @antaljanosbenjamin - could you review this? It applies the same fix you landed in #107025 (create both materialized views, then detach/attach the Kafka table, before producing) to test_kafka_formats_with_broken_message, which hit the identical ordering race: the streaming loop committed the broken message before the errors view was attached, so the errors view stayed empty.

@clickhouse-gh

clickhouse-gh Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [231c36c]

Summary: ✅


AI Review

Summary

This PR fixes the ordering race in test_kafka_formats_with_broken_message by creating both materialized views before any Kafka messages are produced, then restarting the Kafka table so both dependents are attached before consumption begins. I reviewed the current diff, the full changed test, the sibling test_kafka_engine_put_errors_to_stream pattern it mirrors, the relevant Kafka helper contracts, the existing PR discussion, and the current Praktika report, and I did not find a correctness, coverage, or PR-metadata issue in the patch.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 23, 2026
@antaljanosbenjamin
antaljanosbenjamin added this pull request to the merge queue Jul 24, 2026
Merged via the queue into ClickHouse:master with commit 4782cdc Jul 24, 2026
178 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 Jul 24, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

@alexey-milovidov one label ask, plus a nudge on its stalled sibling.

26.6 is the only active release branch missing either of the two Kafka materialized-view-race test fixes, and it is missing both. Fix presence in the shipped blob per branch:

test fix master 26.8 26.7 26.6 26.5 26.3 25.8
test_batch_slow_0.py::test_kafka_formats_with_broken_message this PR yes yes no no no no no
test_batch_fast.py::test_kafka_engine_put_errors_to_stream_with_random_malformed_json #107025 yes yes yes no yes yes n/a

Both fixes hold: over 45 days all 34 materialized-view-race failures of these two tests sit on a tree lacking the corresponding fix and none on a tree that has it (the 26.5 rows predate #113974 merging on 2026-08-09, the master-based ones predate this PR). This test passes 326 to 758 times on each of 26.7, 26.6, 26.5, 26.3 and 25.8 in 30 days, so those zeros are passes and not absent jobs.

26.6 was clean for 50 consecutive commits, then went red at d26e5239316d (2026-08-25 09:54) and has failed on 7 of the 8 branch commits since plus 13 distinct 26.6 pull requests: 25 failures in under three days. Only one pull request merged in that window, #116097, and it touches nothing in Kafka (a new integration test, a compose file, one line in ci/jobs/integration_test_job.py, DataLake src/ changes), so what moved is suite timing rather than behaviour. Hence the ask for the fix, which removes the timing dependency, rather than chasing the trigger.

Request: v26.6-must-backport on this PR. Computed from select_backport_branches against the six branches with an open release pull request, it expands to 26.8, 26.7, 26.6. 26.8 already carries the fix byte-identical, so that resolution is empty and opens no pull request; 26.7's copy of the file is byte-identical to this PR's cherry-pick base, so it cannot conflict; on 26.6 the patch applies with line offsets only and no fuzz, and I grepped the resolved file rather than trusting the return code. It also applies cleanly to 26.5, 26.3 and 25.8, so v25.8-must-backport is available if you want a lower floor, but those three show zero failures of this test in 30 days, so I would stop at 26.6 and leave how far back to you.

Separately, the other half of 26.6's pain is #113975, the 26.6 backport the automation opened from the v26.3-must-backport label you applied to #107025 on 2026-08-08. Its siblings #113973 and #113974 merged the next day; #113975 is still open and UNSTABLE with no review since 2026-08-10, and it is what fails the test_batch_fast.py half on 26.6 pull requests. Getting it merged closes that half.

One residual, since it is a property of the fix I am asking you to spread: the ATTACH this patch adds can lose a race with the still-running streaming query and throw Code: 57 ... detached but still used by some query. That has happened once in 45 days across all branches and all tests (master, 2026-08-21, on a tree that did contain the fix), against 25 failures in three days from the race it removes, so I have not chased it.

clickhouse-gh Bot pushed a commit that referenced this pull request Aug 27, 2026
…-broken-message

Fix flaky test_kafka_formats_with_broken_message

(cherry picked from commit 4782cdc)
clickhouse-gh Bot added a commit that referenced this pull request Aug 27, 2026
Backport #111701 to 26.6: Fix flaky test_kafka_formats_with_broken_message
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 29, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 29, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Aug 29, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 29, 2026
Backport #111701 to 26.7: Fix flaky test_kafka_formats_with_broken_message
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…rmats-with-broken-message

Fix flaky test_kafka_formats_with_broken_message
@PedroTadim PedroTadim added v26.3-must-backport and removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Sep 12, 2026
@PedroTadim

Copy link
Copy Markdown
Member

Backporting further to fix CI

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 12, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 12, 2026
alexey-milovidov added a commit that referenced this pull request Sep 12, 2026
Backport #111701 to 26.3: Fix flaky test_kafka_formats_with_broken_message
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 18, 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 pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-ci pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.3-must-backport v26.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants