Repository navigation
Fix RabbitMQ tables inserting received messages twice after a reconnect - #125105
Conversation
After a channel or connection error the consumer gets a new channel, but messages already received on the old one stayed in the queue. They can no longer be acked (delivery tags are per channel) and the broker redelivers them, so they were inserted twice, and a block could mix tags of both channels in one commit. Clear them when the channel is replaced.
|
Workflow [PR], commit [4d28d9b] Summary: ✅
AI ReviewSummaryThe PR prevents queued Final VerdictStatus: ✅ Approve. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units9 translation units recompiled, 23 s compile time in total, 9 of them have a recent master baseline. |
…ale_deliveries` by two blocks With one consumer, only the block in flight when the connection dropped is replayed legitimately; a second block is possible only if the connection drops while that block is being acked. Allowing up to 10% of the backlog would let a regression that replays several stale blocks pass. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
I'll send a separate PR that makes this query robust to that jitter and link it here. |
AMQP-CPP can dispatch callbacks of the old channel after `updateChannel`. A late `onReceived` could refill `received` after it was cleared and tag the stale delivery with the new `channel_id`. Capture a per-channel generation by value in every channel callback and return early when the channel was replaced. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
|
…annel` `streamToViews` only asks the background loop to stop before replacing the channels and does not wait for it, so an `onReceived` callback of the old channel that had already passed the generation check could push a stale delivery after `updateChannel` cleared `received`. The channel callbacks now check the generation and touch the consumer state under `channel_mutex`, and `updateChannel` bumps the generation and clears `received` under the same mutex. A callback of the old channel either completes before the queue is cleared or sees the new generation and does nothing. `onReceived` waits for space in the queue on a condition variable with the mutex released, so a full queue cannot block `updateChannel`; the wait ends when the channel is replaced or the consumer is stopped. The mutex is recursive because AMQP-CPP calls the error callback of a failed `consume` right away, from `subscribe` inside `onReady`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Thanks @groeneai. |
# Conflicts: # tests/integration/test_storage_rabbitmq/test.py
…sumer A callback woken up by `stop` could still push a delivery if the queue had space. Such a delivery is never read and is redelivered by the broker anyway. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=125105&sha=c60fb087b8648d5380c3abcb74bae14cc55d2421&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 ( |
|
STID 2467-34d3: the fix is already in progress, #125043 (mine, open). I ran the exact fatal query, |
|
🕵 Thanks @groeneai. The AST fuzzer failure (STID 2467-34d3) is tracked by #125043. I merged the latest |
|
🕵 The only red checks on e46d3cf were the |
c22dc6a
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.
…es without rabbitmqctl `rabbitmqctl close_all_connections test` timed out after 300 s in 3 runs of `Integration tests (arm_binary, 2/4)` (master 524a0c2, ClickHouse#127389, ClickHouse#126642). The close never reached the broker: no `Closing connection ... because "test"` line in its log, and ClickHouse's AMQP connection stayed open for 5 minutes until the test dropped the database. The broker passed its `rabbitmq-diagnostics ping` healthcheck 29/29 times in each window, so a new CLI process again never got to its RPC, like the `list_queues` call before. Bounding the call and asking again does not fit here: the drop must happen while most of the backlog is still queued (`view_rows() < messages_num // 2`), which leaves about 9 s after the 500-row trigger at ~490 rows/s, and a timed-out `docker exec` leaves the CLI running in the container, where a late close would force a second reconnect. Close through the management API instead: `DELETE /api/connections/username/root` with `X-Reason: test` reaches the same `rabbit_networking:close_connection(Pid, "test")` for every connection of the broker's only user, synchronously before the 204 reply, and starts no CLI process. Validated: an injected never-returning close fails the unfixed test after 300 s with the CI error; without the DELETE the test fails waiting for the reconnect; the pre-ClickHouse#125105 binary still fails on the duplicates bound (9500, 9400); 50/50 runs pass with `204` (p50 0.1 s) and ClickHouse logs `CONNECTION_FORCED - test` then `Connection restored`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…_drops_stale_deliveries The drain loop started a new `rabbitmqctl list_queues` (an Erlang CLI VM via `docker exec`) on every iteration, 15-22 times per run on arm, and waited up to the harness default of 300 s for each without a retry. In 3 runs of `Integration tests (arm_binary, 2/4)` on 2026-10-19 one call never returned and printed nothing, while ClickHouse had inserted all 10100 rows and the broker passed its `rabbitmq-diagnostics ping` healthcheck 29/29 times in that window, so the loop's own 180 s deadline was never checked. Ask the broker only once the row count has been stable for 3 s (same exit condition, about one call per run), and abandon a call after 30 s and ask again under the existing deadline. Only a timeout is treated as "not drained yet". `close_all_connections` is unchanged: a retry could force a second reconnect. Validated with an injected never-returning call (unfixed: fails after 300 s with the CI error; fixed: passes 2/2 with one abandoned call), 50/50 normal runs, and the pre-ClickHouse#125105 binary, which still fails on the duplicates bound. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #124655
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes
RabbitMQtables inserting most of the already received but unprocessed messages twice after a lost connection or channel, sometimes followed byunknown delivery tagchannel errors.Description
Impact: one lost connection to the broker makes a
RabbitMQtable insert the messages its consumers had already received a second time: 9000 of 10000 rows in the test below, on master and on 26.9. Broker restarts, network blips and missed heartbeats are routine in production, and each of them can silently duplicate up to the whole received backlog in the target tables.Fix: clear the consumer's queue of received messages when its channel is replaced. Nothing is acked or rejected, so nothing is lost: the broker redelivers the messages of the closed channel once. Channel callbacks capture a channel generation, check it under a mutex that
updateChannelalso holds while it clears the queue, and do nothing once their channel is replaced. A late delivery from the old channel, including one already running in the background loop, therefore cannot refill the queue, and a block can no longer mix two channels. Small enough to backport.Proof:
test_rabbitmq_reconnect_drops_stale_deliveriesqueues 10000 messages, closes the connections withrabbitmqctl close_all_connectionswhile most of them are still in the consumer's queue, and waits until the broker holds nothing and no rows are added. It fails on master (9000 and 9100 duplicates in two runs) and on every other build without this change, also with #124655; it passes 10 of 10 runs with it. The rest oftest_storage_rabbitmqpasses on a minimal local build, except two tests that need Arrow or the JSON type and fail on master too. One-minute repro on the official 26.9 image:./stale-deliveries.shin https://gist.github.com/comino/9f0f008a24d0cbf9606b36a5426c4a6c (26.9: about 9000 duplicates; a build of this branch: 100, the block that was in flight, which comes back as before).Mechanism and scope
After a lost connection,
streamToViewsgives every consumer a new channel (after a channel error, only the failed one), but deliveries already received on the old channel stay in the consumer's queue. They are inserted, cannot be acked (delivery tags are per channel), and the broker redelivers them, so they are inserted twice. A block that holds old and new deliveries also commits the largest tag (old channel) with the channel id of its last message (new channel); the ack check passes, and if the new channel has not delivered that many messages yet, the broker answersPRECONDITION_FAILED - unknown delivery tag, closes the new channel and redelivers its whole window, and the cycle can repeat.Not changed here: a block that is being read when the connection drops still takes deliveries of the dead channel until it is full or its flush interval ends, and those come back too; with a large block size that can be many. A direct
SELECTreconnects without replacing channels.How it was found, server log
Found while benchmarking #124655 (2M-message backlog, 4 consumers × 4 queues, builds that include #124655, which master needs to drain a backlog that large): about 1 in 6 runs lost the connection once (cause not known yet) and then had 10–90k duplicates and
unknown delivery tagchannel errors. With this change such runs had 3–25k duplicates (4 runs), the blocks that were inserted but not yet acked.Server log of one such run:
connection lost→Connection restored, updating channels→ one second laterConsumer committed messages with deliveryTags up to 134253 on channel 4_1_…→ broker:operation basic.ack caused a channel exception precondition_failed: unknown delivery tag 134253.If the new channel has already delivered as many messages as the stale tag, the broker accepts the bad ack instead, and it acks deliveries that are not inserted yet; they are lost if the server stops before inserting them.
cc @kssenii
Workflow [PR]
Sync PR [sync-upstream/pr/125105]
Version info
26.11.1.159-master(included in26.11and later)