Skip to content

Fix new materialized view on idle RabbitMQ table missing messages published right after CREATE - #133955

Open
aarvnd wants to merge 1 commit into
ClickHouse:masterfrom
aarvnd:fix/133901-new-mv-created-on-idle-rabbitmq-table-mi
Open

aarvnd wants to merge 1 commit into
ClickHouse:masterfrom
aarvnd:fix/133901-new-mv-created-on-idle-rabbitmq-table-mi

Conversation

@aarvnd

@aarvnd aarvnd commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix a materialized view created on an idle RabbitMQ table silently missing (and acking) messages published right after its CREATE returned.

Details

Each background streaming cycle builds its InterpreterInsertQuery pipeline from the views that exist when the cycle starts. An idle cycle can wait up to stream_poll_timeout_ms for its first message. If a materialized view was created during that wait, the first messages went only to the old views and were acked, so the new view never received them.

Changes:

  • StorageRabbitMQ::streamToViews records the dependent views before building the pipeline and passes them to each RabbitMQSource.
  • While a cycle has no rows yet, RabbitMQSource::generateImpl compares the current dependent views with the recorded ones before consuming a message. If they differ, the cycle ends without consuming (nothing is acked), and the next cycle, built for the new set of views, delivers the message. This is usually one catalog lookup per cycle and does not apply to direct SELECT.
  • StorageRabbitMQ::threadFunc logs the current number of views in Started streaming to N attached views instead of the count taken at the start of the run.

Not covered: a view created after a cycle has already consumed its first message still receives rows from the next cycle (as before, like Kafka). ALTER TABLE ... MODIFY QUERY keeps the view ID, so it is not detected.

Testing

Added the integration test test_rabbitmq_view_added_to_idle_table_gets_new_messages to tests/integration/test_storage_rabbitmq/test.py, based on the reproduction in the issue: it creates mv2 right after a streaming cycle starts, publishes 100 messages and expects both target tables to get 100 rows. Not run locally: there was no build directory or Docker integration environment. The test file was only checked to parse.

Testing
python3 -c ast.parse on tests/integration/test_storage_rabbitmq/test.py: ok. C++ not compiled and the integration test not run (no build directory or Docker integration environment in this worktree).

Closes #133901


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

Copilot AI balanced review requested due to automatic review settings November 24, 2026 01:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@clickhouse-gh clickhouse-gh Bot added comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). external Author is not a member of the ClickHouse organization labels Nov 24, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Nov 25, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [860c5a1]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/can_be_merged.py --review-threads FAIL

AI Review

Summary

The PR snapshots the dependent materialized views of a RabbitMQ table and abandons an idle streaming cycle when that set changes before the first row is consumed. A pre-existing last-view removal race still permits consuming and acknowledging discarded rows, and the new regression test can pass without exercising the fix.

Findings

❌ Blockers

  • StorageRabbitMQ.cpp:1454: An unchanged view set does not guarantee a valid destination. If the last view disappears between threadFunc checking readiness and taking this snapshot, both the snapshot and the later catalog lookup are empty. InterpreterInsertQuery supplies a NullSinkToStorage, dependentViewsChanged allows consumption, and sendAck permanently removes the discarded messages before the readiness check at line 1612. Recheck readiness after pipeline construction and before executing sources, as StorageNATS::streamToViews does, or reject empty/unready snapshots. This race predates the PR; deferring it requires a linked tracking issue.

⚠️ Majors

  • test.py:2358: The regression test does not establish that the pipeline predates mv2. Its log marker is emitted before pipeline construction, and the 500 ms idle cycle may also expire before publishing. Either ordering lets the test pass with the fix removed. Establish that an already-built pipeline is invalidated and demonstrate failure without the guard.
Tests
  • ⚠️ No local execution was possible in this CI review job. The relevant integration and integration bugfix-validation jobs are still pending in the Praktika report. Provide a regression run that fails without the fix and passes with it after strengthening the ordering check.
Final Verdict

Status: ❌ Block

Prevent consumption with an empty/unready view snapshot, or link a dedicated issue for that pre-existing race, and strengthen the regression test so it proves the stale-pipeline behavior.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 25, 2026

/// Taken before the insert pipeline is built, so a view created after this point (and possibly missing from
/// the pipeline) is always detected by the sources, which then end the cycle before consuming a message.
auto streaming_views = DatabaseCatalog::instance().getDependentViews(table_id);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Blocker] Refuse an empty view snapshot before consuming messages

The last view can disappear after threadFunc passes hasDependencies but before this snapshot is taken. Then streaming_views is empty, InterpreterInsertQuery builds a NullSinkToStorage, and dependentViewsChanged compares two empty sets and allows consumption. The cycle discards the rows and calls sendAck before reaching the later hasDependencies check at line 1612. Messages published while a view is detached or being recreated can therefore still be permanently lost.

Revalidate dependency readiness after building the pipeline and before executing the sources, as StorageNATS::streamToViews already does, or otherwise reject an empty/unready snapshot. This race predates the PR but remains reachable through the dependency-change handling being fixed here; if it is deferred, please link a dedicated tracking issue.


# Create mv2 right after a streaming cycle that knows only mv1 has started, then publish. The server log is
# tailed on the host, as the cycle waits for a message only for stream_poll_timeout_ms (500 ms).
started = f"StorageRabbitMQ ({db}.rabbit): Started streaming to 1 attached views"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Major] Establish that the test actually exercises a stale pipeline

Started streaming to 1 attached views is logged in threadFunc before streamToViews takes the snapshot or builds the insert pipeline. After this wait, mv2 can finish creation before the pipeline reads its dependencies, so both tables receive 100 rows even with the fix removed. The same false pass occurs if scheduling the query/publisher takes longer than the 500 ms idle wait and a fresh cycle starts first.

Please make the test establish that an already-built pipeline is invalidated before it consumes the first message, and verify that the test fails with the new guard removed. The current row-count assertions alone do not distinguish exercising this fix from avoiding the original race.

@clickhouse-gh

clickhouse-gh Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 860c5a16d with master d31a6eff9 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

8 translation units recompiled, 16 s compile time in total, 8 of them have a recent master baseline.

Job report

This branch has not been deployed

No deployments
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 comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New MV created on idle RabbitMQ table misses messages sent right after CREATE

3 participants