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.
| // max number of events kept in memory while the AMQP server is unreachable, 0 disables buffering | ||
| private static Integer bufferSize = 1000; |
There was a problem hiding this comment.
| // max number of events kept in memory while the AMQP server is unreachable, 0 disables buffering | |
| private static Integer bufferSize = 1000; | |
| private static Integer maxEventBufferSize = 1000; |
| // time in milliseconds after which a buffered event is dropped if it could not be sent | ||
| private static Integer bufferExpiry = 300000; |
There was a problem hiding this comment.
| // time in milliseconds after which a buffered event is dropped if it could not be sent | |
| private static Integer bufferExpiry = 300000; | |
| private static Integer eventBufferExpiryTime = 300000; |
| // events (routing key, description, time of buffering) waiting to be sent, oldest first | ||
| private static final ArrayDeque<Ternary<String, String, Long>> s_buffer = new ArrayDeque<>(); |
There was a problem hiding this comment.
| // events (routing key, description, time of buffering) waiting to be sent, oldest first | |
| private static final ArrayDeque<Ternary<String, String, Long>> s_buffer = new ArrayDeque<>(); | |
| /** | |
| * events, stored as {routing key, description, time of buffering}, waiting to be sent, oldest first | |
| */ | |
| private static final ArrayDeque<Ternary<String, String, Long>> eventBuffer = new ArrayDeque<>(); |
needs renaming below as well!
| public static void setBufferSize(Integer bufferSize) { | ||
| RabbitMQEventBus.bufferSize = bufferSize; | ||
| } |
There was a problem hiding this comment.
| public static void setBufferSize(Integer bufferSize) { | |
| RabbitMQEventBus.bufferSize = bufferSize; | |
| } | |
| /** | |
| * maximum size to which the event buffer may grow | |
| */ | |
| public static void setBufferSize(Integer bufferSize) { | |
| maxEventBufferSize = bufferSize; | |
| } |
| public static void setBufferExpiry(Integer bufferExpiry) { | ||
| RabbitMQEventBus.bufferExpiry = bufferExpiry; | ||
| } |
There was a problem hiding this comment.
| public static void setBufferExpiry(Integer bufferExpiry) { | |
| RabbitMQEventBus.bufferExpiry = bufferExpiry; | |
| } | |
| /** | |
| * expriy time to set for events that cannot be send | |
| * | |
| */ | |
| public static void setBufferExpiry(Integer bufferExpiry) { | |
| eventBufferExpiryTime = bufferExpiry; | |
| } |
| return true; | ||
| } | ||
|
|
||
| // drops the expired events and, if connected, sends the buffered ones in the order they were added |
There was a problem hiding this comment.
top of method comment:
| // drops the expired events and, if connected, sends the buffered ones in the order they were added | |
| /** | |
| * drops the expired events and, if connected, sends the buffered ones in the order they were added | |
| */ |
| // the event stays in the buffer while being sent, so that newer events queue up behind it | ||
| synchronized (s_buffer) { | ||
| if (s_buffer.peek() == bufferedEvent) { | ||
| s_buffer.poll(); | ||
| } |
There was a problem hiding this comment.
can you extract this to a separate method? sounds trivial but it makes for better reading
| } | ||
|
|
||
| abortConnection(); // disconnected to AMQP server, so abort the connection and channels | ||
| // the ReconnectionTask started with the event bus takes care of the re-connect |
There was a problem hiding this comment.
| // the ReconnectionTask started with the event bus takes care of the re-connect |
| // runs for the lifetime of the event bus: connects back to AMQP server after any loss of connection | ||
| // and sends the events buffered in the meantime |
There was a problem hiding this comment.
| // runs for the lifetime of the event bus: connects back to AMQP server after any loss of connection | |
| // and sends the events buffered in the meantime | |
| /* | |
| * runs for the lifetime of the event bus: connects back to AMQP server after any loss of connection | |
| * and sends the events buffered in the meantime | |
| */ |
| protected void runInContext() { | ||
|
|
||
| while (!connected) { | ||
| while (true) { |
There was a problem hiding this comment.
not all of this is on you, @durdin85 , but this is a forever loop with nested try blocks and an anonymous class inside. Could you at least put the new part (the sync block in a separate method? If you have the energy, feel free to simplify it further.
There was a problem hiding this comment.
Thanks @DaanHoogland for review, I'll take a look on this. The old while task ended after one successful connect if I am not mistaken, and a new one was spawned per disconnect, whereas now one task lives as long as the bus and does all reconnecting and flushing, but maybe it is a good idea to redo it.
There was a problem hiding this comment.
I was thinking about that, and instead of the endless loop, the loop content might be moved to the SingleThreadScheduledExecutor, which then does the repetition, and should be guaranteed to be in order and unique. There's second endless loop in buffer flush, but that can be easily fixed.
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
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.