Repository navigation
Conversation
|
Dependency: this PR needs ClickHouse/AMQP-CPP#9 (plus the submodule bump). AMQP-CPP treats |
Without basic.qos the broker pushed every ready message. The delivery callback blocked in a full ConcurrentBoundedQueue while the streaming thread waited for that loop to stop, so consumption and DROP TABLE hung. - Limit unacked deliveries with basic.qos; the received queue no longer blocks. - Ack messages that produce no rows, so they cannot hold prefetch slots. - End a block once the prefetch window is used up and drained. Closes ClickHouse#54775
631358b to
ed652fa
Compare
|
Workflow [PR], commit [3a227d8] Summary: ✅
AI ReviewSummaryAdds per-subscription prefetch limits to prevent the Findings❌ Blockers
Missing context / blind spots
Final VerdictStatus: ❌ Block until the |
…left unacked A message a direct SELECT read without acking it held a prefetch slot of the pooled consumer: with rabbitmq_flush_interval_ms = 0 a view's block waited forever for a full window.
The broker requeues them. With the queue now unbounded, every reconnect would otherwise add a prefetch window.
# Conflicts: # src/Storages/RabbitMQ/RabbitMQConsumer.cpp
The prefetch window bounds the deliveries of a channel to num_queues * prefetch_count <= queue_size, so the queue never fills and ClickHouse#125105's wait for space never blocks the loop.
|
@kssenii CI is green after merging master (including #125105). Ready for review 🙂 ( ClickHouse/AMQP-CPP#9 is pending - the code here doesn't change with that fix; only the submodule pin follows.) |
|
Any change to pick this up? @groeneai |
|
I can't merge PRs in this repo or in ClickHouse/AMQP-CPP, so I can't land ClickHouse/AMQP-CPP#9 myself. It needs a maintainer with write access to that fork. One thing changed today: @ tavplubix merged ClickHouse/AMQP-CPP#11 into the fork's On this PR: the description says the lost wake-up in |
|
@groeneai, merged the AMQP-CPP PR. |
|
Acknowledged, thanks. I'm opening the ClickHouse PR that moves |
|
The PR is #130649. It moves |
The AMQP event loop was driven by a BackgroundSchedulePool task that spun uv_run(UV_RUN_NOWAIT) + yield for the whole streaming round, burning a core per table while a round waited out its flush interval. It also had to be stopped and restarted around every block, because acks from the streaming thread raced with it. RabbitMQHandler now owns a thread that blocks in uv_run(UV_RUN_DEFAULT) and is woken through a uv_async_t. While it runs, run() posts AMQP-CPP calls (ack, nack, channel open and close, connect, disconnect, heartbeat) to it and waits for the result; otherwise run() executes them inline. Inline execution, loop start and the exiting thread's final drain hold exec_mutex, so the two never overlap. The thread runs across the blocks of a streaming round and stops at its end unless a SELECT still reads, as the loop task did. The loop now keeps running while a channel is replaced, so a delivery of the old channel can still arrive. updateChannel() bumps the channel generation and clears the queue under channel_mutex, and wakes a delivery that waits for space in a full queue; deliveries of the old generation are dropped and redelivered by the broker. The loop thread must never wait for space in the queue, because acks now run on it: the prefetch window (ClickHouse#124655) keeps the queue from filling. Nothing may unwind through libuv. If an exception still escapes, the thread exits, marks the connection broken and run() executes inline until startLoop() replaces the thread. Such a connection is still usable, so reconnect() closes it at once instead of gracefully: a graceful close ends the channels without an error, and the consumers would keep them. Removed: the looping task, startup_mutex, the iterateLoop() try_lock, deactivateTask/task_mutex, the loop stop in read(), the self-driving driveLoopUntilMessage() in RabbitMQSource (a one-shot REFRESH round now waits on the consumer's condition variable) and the unused unbindExchange().
The AMQP event loop was driven by a BackgroundSchedulePool task that spun uv_run(UV_RUN_NOWAIT) + yield for the whole streaming round, burning a core per table while a round waited out its flush interval. It also had to be stopped and restarted around every block, because acks from the streaming thread raced with it. RabbitMQHandler now owns a thread that blocks in uv_run(UV_RUN_DEFAULT) and is woken through a uv_async_t. While it runs, run() posts AMQP-CPP calls (ack, nack, channel open and close, connect, disconnect, heartbeat) to it and waits for the result; otherwise run() executes them inline. Inline execution, loop start and the exiting thread's final drain hold exec_mutex, so the two never overlap. The thread runs across the blocks of a streaming round and stops at its end unless a SELECT still reads, as the loop task did. The loop now keeps running while a channel is replaced, so a delivery of the old channel can still arrive. updateChannel() bumps the channel generation and clears the queue under channel_mutex, and wakes a delivery that waits for space in a full queue; deliveries of the old generation are dropped and redelivered by the broker. Acks now run on the loop thread, so it must not wait for space in the queue: the prefetch window (ClickHouse#124655) keeps the queue from filling. Nothing may unwind through libuv, so the loop thread does not throw on the memory limit. If an exception still escapes, the thread exits, marks the connection broken and run() executes inline until startLoop() replaces the thread. Such a connection may still look usable, so reconnect() closes it at once instead of gracefully: a graceful close ends the channels without an error, and the consumers would keep them. Removed: the looping task, startup_mutex, the iterateLoop() try_lock, deactivateTask/task_mutex, the loop stop in read(), the self-driving driveLoopUntilMessage() in RabbitMQSource (a one-shot REFRESH round now waits on the consumer's condition variable) and the unused unbindExchange().
The AMQP event loop was driven by a BackgroundSchedulePool task that spun uv_run(UV_RUN_NOWAIT) + yield for the whole streaming round, burning a core per table while a round waited out its flush interval. It also had to be stopped and restarted around every block, because acks from the streaming thread raced with it. RabbitMQHandler now owns a thread that blocks in uv_run(UV_RUN_DEFAULT) and is woken through a uv_async_t. While it runs, run() posts AMQP-CPP calls (ack, nack, channel open and close, connect, disconnect, heartbeat) to it and waits for the result; otherwise run() executes them inline. Inline execution, loop start and the exiting thread's final drain hold exec_mutex, so the two never overlap. The thread runs across the blocks of a streaming round and stops at its end unless a SELECT still reads, as the loop task did. The loop now keeps running while a channel is replaced, so a delivery of the old channel can still arrive. updateChannel() bumps the channel generation and clears the queue under channel_mutex, and wakes a delivery that waits for space in a full queue; deliveries of the old generation are dropped and redelivered by the broker. Acks now run on the loop thread, so it must not wait for space in the queue: the prefetch window (ClickHouse#124655) keeps the queue from filling. Nothing may unwind through libuv, so the loop thread does not throw on the memory limit. If an exception still escapes, the thread exits, marks the connection broken and run() executes inline until startLoop() replaces the thread. Such a connection may still look usable, so reconnect() closes it at once instead of gracefully: a graceful close ends the channels without an error, and the consumers would keep them. Removed: the looping task, startup_mutex, the iterateLoop() try_lock, deactivateTask/task_mutex, the loop stop in read(), the self-driving driveLoopUntilMessage() in RabbitMQSource (a one-shot REFRESH round now waits on the consumer's condition variable) and the unused unbindExchange().
The AMQP event loop was driven by a BackgroundSchedulePool task that spun uv_run(UV_RUN_NOWAIT) + yield for the whole streaming round, burning a core per table while a round waited out its flush interval. It also had to be stopped and restarted around every block, because acks from the streaming thread raced with it. RabbitMQHandler now owns a thread that blocks in uv_run(UV_RUN_DEFAULT) and is woken through a uv_async_t. While it runs, run() posts AMQP-CPP calls (ack, nack, channel open and close, connect, disconnect, heartbeat) to it and waits for the result; otherwise run() executes them inline. Inline execution, loop start and the exiting thread's final drain hold exec_mutex, so the two never overlap. The thread runs across the blocks of a streaming round and stops at its end unless a SELECT still reads, as the loop task did. The loop now keeps running while a channel is replaced, so a delivery of the old channel can still arrive. updateChannel() bumps the channel generation and clears the queue under channel_mutex, and wakes a delivery that waits for space in a full queue; deliveries of the old generation are dropped and redelivered by the broker. Acks now run on the loop thread, so it must not wait for space in the queue: the prefetch window (ClickHouse#124655) keeps the queue from filling. Nothing may unwind through libuv, so the loop thread does not throw on the memory limit. If an exception still escapes, the thread exits, marks the connection broken and run() executes inline until startLoop() replaces the thread. Such a connection may still look usable, so reconnect() closes it at once instead of gracefully: a graceful close ends the channels without an error, and the consumers would keep them. Removed: the looping task, startup_mutex, the iterateLoop() try_lock, deactivateTask/task_mutex, the loop stop in read(), the self-driving driveLoopUntilMessage() in RabbitMQSource (a one-shot REFRESH round now waits on the consumer's condition variable) and the unused unbindExchange().
|
@alexey-milovidov The AMQP-CPP dependency is resolved: master pins AMQP-CPP 5f4c447 (the merge of ClickHouse/AMQP-CPP#9) since #130541. CI is green and nothing else blocks this PR. It fixes RabbitMQ consumption stopping once the backlog exceeds the internal queue (#54775). Could you take a look? @groeneai |
|
I re-checked this PR at
|
Closes: #54775
Related: #74430
Related: #50692
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes
RabbitMQtables that silently stop consuming, withDROP TABLEhanging until a restart, when inserts fall far behind the broker (about 1 million messages with default settings, 100000 with a smallrabbitmq_max_block_size). ClickHouse now limits how many unacknowledged messages RabbitMQ sends at once.Description
The consumer sets no
basic.qos, so RabbitMQ pushes every ready message into a bounded queue ofmax(100000, block size)entries (block size:rabbitmq_max_block_size, ormax_insert_block_size / rabbitmq_num_consumers= 1048449 by default). Once inserts are that far behind, the libuv delivery callback blocks inpush()whilestreamToViews, the only thread that drains the queue, waits for the loop task to return: consumption stops without any error in the log andDROP TABLEhangs inshutdown(). In production this hits as soon as inserts fall behind under load: consumption stops without any error,DROP TABLEhangs, and only a server restart recovers. Because of this we had to build our own ingestion system instead of using the RabbitMQ engine.Fix:
basic.qosper subscription,clamp(queue_size / num_queues, 1, 65535)(65535 unless there are several queues). A channel then has at mostnum_queues * prefetch_count <= queue_sizedeliveries outstanding, soreceivednever fills and the delivery callback never waits for space.SELECT); if they hold all of it, the block acks them, as the next block does on master.Dependency. With the AMQP-CPP currently pinned, one
basic.nack(a failed insert into a view,SYSTEM STOP/CANCELduring a block) makes AMQP-CPP queue every later ack without sending it (#76163). On master that gives duplicates; with a prefetch window the broker stops after 65535 unacked messages and the table stalls until it reconnects. Measured: one failing message, then 70000 messages:Resolved: master pins AMQP-CPP 5f4c447 (the merge of AMQP-CPP#9) since #130541, merged 2026-11-12.
Behaviour changes:
prefetch_countmessages per consumer (65535, orqueue_size / num_queueswith several queues, documented), so a largerabbitmq_max_block_sizecan give more, smaller parts. Draining a 300000-message backlog with default settings takes the same time as on master (8.5–8.9 s vs 8.8–8.9 s, 7 parts each, 3 runs each).SELECTwithoutrabbitmq_commit_on_selectstill leaves what it read unacked, as on master, and a view's next block acks it. Without a view,prefetch_count(up to 65535) such reads use up the window until the connection is re-established.waitForMessageslikelier: withrabbitmq_flush_interval_ms = 0a block can sleep until the next delivery or the sanity timeout. Fixed in a follow-up PR.Tests: five new integration tests pass with the fix at
3a227d8e483(3 of 3 runs each after merging master with #125105, 10 of 10 before). Four fail on current master; the fifth, a window held entirely by directSELECTs, fails oned652fa8dand passes on master, which has no window. Before the merge, the rest oftest_storage_rabbitmqpassed on a minimal local build, except two tests that need Arrow or the JSON type and failed on master too.Repro, one minute on the official 26.9 image: https://gist.github.com/comino/349122b3f425d277569c08187289852f
Stacks and design notes
Stacks while stuck (
system.stack_trace, official 26.9.12.8, shortened):basic.qos, RabbitMQ caps quorum-queue consumers at 2000 unacked messages; with an explicit prefetch a 20000-message quorum backlog drained in 8 s instead of 56 s (RabbitMQ 4,rabbitmq_flush_interval_ms = 5000).test_rabbitmq_backlog_larger_than_received_queue,test_rabbitmq_empty_messages_are_acked(waits for the ack in the log),test_rabbitmq_block_ends_when_prefetch_is_used_up,test_rabbitmq_view_acks_messages_a_select_left_unacked,test_rabbitmq_view_acks_a_window_selects_left_unacked.cc @alexey-milovidov
Workflow [PR]
Sync PR [sync-upstream/pr/124655]