Skip to content

Reduce idle RabbitMQ CPU by 95% by not rebuilding the insert pipeline on every empty read - #133527

Merged
alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
comino:rabbitmq-empty-read-like-kafka
Nov 23, 2026
Merged

alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
comino:rabbitmq-empty-read-like-kafka

Conversation

@comino

@comino comino commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Performance Improvement

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

An idle RabbitMQ table with materialized views no longer rebuilds its insert pipeline for every empty read: an empty read now waits up to stream_poll_timeout_ms for a message and then ends the streaming round, as Kafka does.

Details

Each streaming call builds a context copy, an InterpreterInsertQuery with all attached materialized view chains and a PipelineExecutor. On an empty queue the source returned at once, and a round made up to 10 such calls (MAX_FAILED_READ_ATTEMPTS) before sleeping ~110 ms, so an idle table rebuilt its pipeline continuously. After #131355 this was the largest remaining cost of idle RabbitMQ tables in a production profile.

flowchart LR
  subgraph before["master: idle round ≈ 120 ms, 10 builds"]
    direction LR
    b1["build pipeline"] --> b2["empty read,<br/>returns at once"] -->|"× 10"| b1
    b2 --> b3["sleep 110 ms"]
  end
  subgraph after["this PR: idle round ≈ 610 ms, 1 build"]
    direction LR
    a1["build pipeline"] --> a2["wait up to 500 ms<br/>for a message"] --> a3["sleep 110 ms"]
  end
Loading

master vs this PR: pipelines built while idle, server CPU, publish-to-row latency

measured on one host, 2 alternating runs per side master this PR
insert pipelines built in 10 s idle, 1 table 850 16–17
server CPU, 40 idle tables 1.12 cores 0.06 cores (−95 %)
publish to row, one of 40 idle tables: median / slowest of 10 0.65 / 0.69 s 0.74 / 1.2–1.8 s

One materialized view per table, flush interval 1000 ms (pipelines) or 500 ms (CPU, latency), other settings default; master = official builds 26.12.1.148 and .160.

The change follows KafkaSource, which waits up to the poll timeout instead of returning an empty block (NATS also ends its round on an empty read):

  • RabbitMQSource: during background streaming, an empty first poll waits up to stream_poll_timeout_ms (default 500 ms, capped by the flush interval), reusing driveLoopUntilMessage from the SYSTEM REFRESH path, so delivery during the wait does not depend on the looping task getting a worker. Direct SELECT is unchanged.
  • StorageRabbitMQ: the source already waited, so an empty read ends the round; MAX_FAILED_READ_ATTEMPTS and read_attempts are removed. The backoff resets after each cycle that returns, so the idle cadence stays rabbitmq_empty_queue_backoff_start_ms + _step_ms as on master, and it still grows while cycles throw.
  • SYSTEM PAUSE only blocks future cycles, so the wait checks it, also after a delivery wakes it. A message received at that moment stays unacked in the consumer and is read in order after START.

Behaviour changes:

  • A message published during the wait is read at once; otherwise in the next round, as on master.
  • The wait counts towards the flush interval, as in Kafka: the first block after an idle period is flushed up to stream_poll_timeout_ms earlier.
  • An idle table holds a message-broker pool worker during the wait (~80 % of the time instead of a busy loop of builds), as Kafka does for its poll; with 40 idle tables on the default pool of 16 workers, a message to one of them waited up to ~1 s longer for a worker (table above). A larger background_message_broker_schedule_pool_size gives the tables more workers.

tests/performance has no broker, so the new integration test test_rabbitmq_idle_table_does_not_rebuild_insert_pipeline counts the pipelines built in 10 s of idle (Processed 0 rows per call); its bound follows from the poll timeout. It fails on master and passes with this change; the whole test_storage_rabbitmq module passes.


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

Version info

  • Merged into: 26.12.1.296-master (included in 26.12 and later)

… idle

An empty first poll returned at once, so each of the up to 10 empty-read
retries of a streaming round built the whole insert pipeline: context copy,
interpreter with the views chain and executor, 50-80 times a second for an
idle table. Like KafkaSource, an empty first poll now waits up to
stream_poll_timeout_ms for a message inside the same pipeline. Direct SELECT
keeps returning at once.
The source already waited up to stream_poll_timeout_ms, so nine more empty
reads only held a message-broker worker for 5 s per round. Drop
MAX_FAILED_READ_ATTEMPTS and end the round at once, as Kafka does; the
backoff still resets after each cycle that ran. Stop the wait on SYSTEM
PAUSE, which blocks only future cycles.
@clickhouse-gh clickhouse-gh Bot added the comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). label Nov 22, 2026
@comino
comino marked this pull request as ready for review November 22, 2026 17:14
@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Nov 23, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Nov 23, 2026
@alexey-milovidov alexey-milovidov self-assigned this Nov 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [095a29f]

Summary: ✅


AI Review

Summary

The change reduces idle RabbitMQ pipeline construction by waiting for messages within RabbitMQSource and ending an empty streaming round after that wait. The PR supplies before/after CPU and latency measurements and a focused regression test. The previous documentation finding is fixed in registerStorageRabbitMQ; no unresolved findings remain at 095a29f3fe5541807c0fd5a0ae4ff0c047138b9f. Integration tests were not rerun locally; this review used the current source, test coverage, reported measurements, and CI summary.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 095a29f3f with master 0b5613b71 (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, 15 s compile time in total, 8 of them have a recent master baseline.

Job report

@stresshouse

stresshouse Bot commented Nov 23, 2026

Copy link
Copy Markdown

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 29 queries analysed

No performance change can be attributed to this PR, because it only changes how the RabbitMQ engine handles empty-queue polling and rescheduling, and neither benchmark runs that code. The only flagged query is clickbench Q28 at -5.3%. That result is inconclusive: the two tests disagree, measurements on both sides were noisy, and the query is a MergeTree regex aggregation that never creates a RabbitMQ source. Queries 10, 30 and 31 on clickbench were marked unstable because master's performance shifted partway through the baseline period, but none of them is flagged here, and nothing in the diff points the other way.

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 28 not_sure 1060 1004 -5.3% <0.0001 The -5.3% shift is borderline, and the two tests disagree on it. This PR only changes RabbitMQ streaming, which a MergeTree regex GROUP BY never runs, so the shift is not from this PR.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 74ac22cc-c35b-45d1-b75c-8b9075101aae
  • MIRAI run: 9f3b9420-e7fd-47ab-a9d3-c558066b8e7b
  • PR check IDs:
    • clickbench_910136_1795405483
    • clickbench_910142_1795405483
    • tpch_adapted_1_official_910159_1795405483
    • tpch_adapted_1_official_910186_1795405483

alexey-milovidov and others added 2 commits November 23, 2026 09:37
…red `RabbitMQ` description

The sentence was added inside the `AUTOGENERATED_START` / `AUTOGENERATED_END` region of
`docs/reference/engines/table-engines/integrations/rabbitmq.mdx`, which is regenerated from
the description registered in `registerStorageRabbitMQ`. Put it into that description and
revert the direct edit of the generated page, which also fails the docs check
"No direct edits to generated or read-only docs".

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 23, 2026
Merged via the queue into ClickHouse:master with commit 3d50995 Nov 23, 2026
195 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 Nov 23, 2026
a819721810 pushed a commit to a819721810/ClickHouse that referenced this pull request Nov 25, 2026
…ty-read-like-kafka"

This reverts commit 3d50995, reversing
changes made to 73c474c.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
clickhouse-gh Bot pushed a commit that referenced this pull request Nov 25, 2026
Edited by the nightly changelog CI job following .claude/skills/edit-changelog/SKILL.md.

Changelog-revert: 131167 122380 Revert "Record the static conditions of a PREWHERE shared with a join runtime filter in the query condition cache"
Changelog-revert: 134110 133527 Revert "Reduce idle RabbitMQ CPU by 95% by not rebuilding the insert pipeline on every empty read"
Changelog-deleted-entry: 133527 [Performance Improvement] [section] * An idle `RabbitMQ` table with materialized views no longer rebuilds its insert pipeline for every empty read: an empty read now waits up to `stream_poll_timeout_ms` for a message and then ends the streaming round, as `Kafka` does. [#133527](#133527) ([Sven Eliasson](https://github.com/comino)).
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-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants