Repository navigation
Conversation
Events published while the AMQP connection is down were discarded. Keep them in a bounded in-memory FIFO buffer and send them in order once the connection is back. - events are buffered only after the broker was reachable once - buffer is capped (bufferSize, default 1000, oldest dropped first) and entries expire (bufferExpiry, default 300000 ms) - publish() no longer connects on the calling thread while the broker is down - ReconnectionTask runs for the lifetime of the bus, so connections closed by the bus itself are re-established as well This change, including the unit test, was generated with an AI assistant (Claude Code). Fixes: apache#14676 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
- keep the buffer flush inside the try block, so an unexpected exception cannot end the only reconnect thread - check for an existing connection and connect under the same lock as getConnection(), so a connection created by publish() is not replaced and leaked Generated with an AI assistant (Claude Code). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Congratulations on your first Pull Request and welcome to the Apache CloudStack community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/cloudstack/blob/main/CONTRIBUTING.md)
|
Generated with an AI assistant (Claude Code). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
DaanHoogland
left a comment
There was a problem hiding this comment.
thanks @durdin85 , the code lgtm. I have soem style remarks though.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #14677 +/- ##
============================================
+ Coverage 20.80% 20.83% +0.02%
- Complexity 21945 21978 +33
============================================
Files 6496 6496
Lines 591838 592011 +173
Branches 72617 72635 +18
============================================
+ Hits 123147 123320 +173
+ Misses 455315 455307 -8
- Partials 13376 13384 +8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
- reconnect and flush from a ScheduledExecutorService instead of a while(true) loop; the task catches Throwable, as the executor silently stops running a task that ended with an exception - loop over the buffer with while ((bufferedEvent = peekBuffer()) != null) - re-register every subscriber whose channel is not open, also when publish() or subscribe() re-established the connection - replace the two duplicated anonymous consumers by SubscriberConsumer - drop the s_ prefix of the fields, fix Javadoc and typos - log when the background thread has reconnected - default retryInterval to 10 s when it is not positive, as scheduleWithFixedDelay rejects such a delay Generated with an AI assistant (Claude Code). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0148pno3o4K3zhvhP3AJaJg3
…blished event - request a heartbeat timeout of 10 s (bean property heartbeat) instead of the client default of 60 s: a connection that silently stopped working is noticed only after about two heartbeat timeouts, and until then events are sent on the API thread and wait for replies - count an event as sent once basicPublish() has returned; a failure to close the channel afterwards is only logged, as the broker may have the event already and sending it again could duplicate it - log the causes of AMQP client exceptions instead of "null" - document the bean properties on their setters Tested on a 4.22.1.1 management server against RabbitMQ 4.4.0, with outages simulated by iptables (connection reset and dropped packets). Generated with an AI assistant (Claude Code). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0148pno3o4K3zhvhP3AJaJg3
DaanHoogland
left a comment
There was a problem hiding this comment.
clgtm, thanks I am suggesting to remove a lot of comments as you are way too generous with those, But left a question at some of them.
|
|
||
| private static Integer retryInterval; | ||
|
|
||
| // max number of events kept in memory while the AMQP server is unreachable, 0 disables buffering |
There was a problem hiding this comment.
| // max number of events kept in memory while the AMQP server is unreachable, 0 disables buffering | |
| /** | |
| * max number of events kept in memory while the AMQP server is unreachable, 0 disables buffering | |
| */ |
| // max number of events kept in memory while the AMQP server is unreachable, 0 disables buffering | ||
| private static Integer maxEventBufferSize = 1000; | ||
|
|
||
| // time in milliseconds after which a buffered event is dropped if it could not be sent |
There was a problem hiding this comment.
| // time in milliseconds after which a buffered event is dropped if it could not be sent | |
| /** | |
| * time in milliseconds after which a buffered event is dropped if it could not be sent | |
| */ |
| private void registerSubscriber(Connection connection, String queueName) throws Exception { | ||
| Ternary<String, Channel, EventSubscriber> subscriberDetails = subscribers.get(queueName); | ||
|
|
||
| // create a channel dedicated for this subscription |
There was a problem hiding this comment.
| // create a channel dedicated for this subscription |
| // create a channel dedicated for this subscription | ||
| Channel channel = createChannel(connection); | ||
|
|
||
| // create a queue and bind it to the exchange with binding key formed from event topic |
There was a problem hiding this comment.
| // create a queue and bind it to the exchange with binding key formed from event topic |
| // register a callback handler to receive the events that a subscriber subscribed to | ||
| channel.basicConsume(queueName, autoAck, queueName, new SubscriberConsumer(channel)); | ||
|
|
||
| // update the channel details for the subscription |
There was a problem hiding this comment.
| // update the channel details for the subscription |
| private static String getErrorMessage(Throwable exception) { | ||
| StringBuilder message = new StringBuilder(); | ||
| for (Throwable cause = exception; cause != null; cause = cause.getCause()) { | ||
| // skip messages already included, e.g. by exceptions created with the cause as their message |
There was a problem hiding this comment.
| // skip messages already included, e.g. by exceptions created with the cause as their message |
| public boolean start() { | ||
| ReconnectionTask reconnect = new ReconnectionTask(); // initiate connection to AMQP server | ||
| executorService.submit(reconnect); | ||
| // initiate connection to AMQP server |
There was a problem hiding this comment.
| // initiate connection to AMQP server |
| String resourceType = getResourceTypeFromRoutingKey(routingKey); | ||
| String resourceUUID = getResourceUUIDFromRoutingKey(routingKey); | ||
|
|
||
| // create event object from the message details obtained from AMQP server |
There was a problem hiding this comment.
| // create event object from the message details obtained from AMQP server |
| Event event = new Event(eventSource, eventCategory, eventType, resourceType, resourceUUID); | ||
| event.setDescription(new String(body)); | ||
|
|
||
| // deliver the event to call back object provided by subscriber |
There was a problem hiding this comment.
| // deliver the event to call back object provided by subscriber |
| try { | ||
| super.run(); | ||
| } catch (Throwable t) { | ||
| // the executor would never run the task again if it ended with an exception |
There was a problem hiding this comment.
| // the executor would never run the task again if it ended with an exception |
I agree but on the other hand Throwable is a very wide net. If you feel it is needed...
There was a problem hiding this comment.
I'd keep it. If this would be more specific and it ever hit something unmatched the bus would never reconnect or send the buffered events again until a restart.
Comment changes only, as suggested in the review: drop comments that repeat the code, turn the buffer limit comments into Javadoc and reword the comment in flushBuffer() that read as contradicting the code below it. The compiled bytecode is unchanged. Generated with an AI assistant (Claude Code). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Description
This PR makes the RabbitMQ event bus keep events that are published while the
AMQP broker is unreachable and send them, in the order they were published, once
the connection is back. Until now such events were discarded.
Behaviour:
body, time of buffering).
that,
publish()fails as it does today.bufferSizeevents (default 1000, the oldest isdropped when full) and each event is dropped
bufferExpirymilliseconds(default 300000) after it was buffered.
bufferSize0 disables buffering andrestores the previous behaviour.
appended to the buffer, so they never overtake older ones.
publish()returnsnormally for a buffered event and no longer connects on the calling thread
while the broker is down (the connection timeout of amqp-client 5.17.0 is
60 s, so every API call that published an event could hang that long).
ReconnectionTaskis scheduled with a fixed delay ofretryInterval(default10 s) for the lifetime of the event bus, instead of being started by
DisconnectHandler. Each run reconnects if there is no connection,re-registers subscribers whose channel is not open, drops expired events and
sends the buffered ones. This also re-establishes connections closed by the
bus itself (
AlreadyClosedExceptioninpublish(), blocked connection),which previously started no reconnect.
stop()shuts the executor down.basicPublish()has returned, an event counts as sent: a failure toclose the channel afterwards is logged, but the event is not sent again, as
the broker may already have it.
heartbeatseconds (default 10,previously the client default of 60). A connection that silently stopped
working (e.g. packets dropped by a firewall) is only noticed after about two
heartbeat timeouts; until then events are still sent on the API thread and
wait for replies, so API calls hung for about 2 minutes with the old value.
connection error: Connection reset) instead ofnull.All three settings are optional properties of the existing bean, next to the
other AMQP settings:
Known limits, by design:
connection breaks can be lost unnoticed; events are never sent twice.
No new dependencies; the change is confined to
RabbitMQEventBus.Fixes: #14676
AI disclosure: the code, the unit tests and this description were generated with an AI assistant (Claude Code). The design and its constraints were set by the submitter, who also tested the change on a live system. If the project does not accept AI-generated contributions, please say so and treat this as a proposal for discussion.
Types of changes
Feature/Enhancement Scale or Bug Severity
Feature/Enhancement Scale
How Has This Been Tested?
New unit test
RabbitMQEventBusTest(mocked AMQP connection and channel):publish()still fails when the broker was never reachable;an event published while the buffer is not empty does not overtake them;
IOExceptionis buffered and re-sent;events are sent even if a subscriber cannot be re-registered;
The plugin builds and
RabbitMQEventBusTestpasses on main and on 4.22.1.1(
mvn -pl plugins/event-bus/rabbitmq -am test -Dtest=RabbitMQEventBusTest).Live test on a 4.22.1.1 management server (classes replaced in the bundled jar)
with a RabbitMQ 4.4.0 cluster behind a TCP load balancer, outages simulated with
iptables on the management server, events produced by user login/logout and
compared with the events stored by the consumer:
REJECT --reject-with tcp-reset), including two outages in arow: all events buffered during the outage arrived after the reconnect, in
order and without duplicates;
bufferExpirywere droppedand logged, the newer ones arrived after the reconnect;
the reconnect. The first API call publishing an event waits until the client
notices the dead connection: about 2 minutes with the previous heartbeat of
60 s, 23 s with the new default of 10 s (logged as
Failed to create a channel due to connection error: Heartbeat missing with heartbeat = 10 seconds).How did you try to break this feature and the system with this change?
The unit tests cover a full buffer, expired events, a failing send, a failing
channel close, failing subscriber registration, an unexpected failure of the
reconnection task and a connection that returns while events are still
buffered. The live tests cover hard (reset) and silent (dropped packets)
outages, repeated outages and outages longer than the buffer expiry.