shashank created CAMEL-27527:
--------------------------------

             Summary: camel-disruptor - when a consumer is added or removed, 
producers that publish at that moment can lose their messages or wait forever, 
and the backlog of a queue without consumer is partly dropped
                 Key: CAMEL-27527
                 URL: https://issues.apache.org/jira/browse/CAMEL-27527
             Project: Camel
          Issue Type: Bug
          Components: camel-disruptor
            Reporter: shashank
            Assignee: shashank


h3. What happens

When a consumer of a {{disruptor}} endpoint starts, stops, is suspended or 
resumed, {{DisruptorReference.reconfigure()}} replaces the Disruptor: it clears 
the reference, calls {{Disruptor.shutdown()}} on the old one (which waits until 
the event handlers have consumed every claimed slot, then halts them) and 
starts a new one. A producer reads the reference, then claims and publishes a 
slot on that ring buffer, without a lock. Nothing makes the reconfiguration 
wait for a producer that read the reference just before it was cleared, so the 
producer can publish after the drain, on a ring buffer whose event handlers are 
halted:
* an InOnly exchange is accepted without exception and never processed;
* an InOut producer without timeout waits for its reply forever;
* with {{blockWhenFull=true}} (the default), once more such producers arrive 
than the old ring buffer has free slots, they wait in {{RingBuffer.next()}} 
forever.

Producers that wait for a free slot on a full ring buffer have read the old 
reference already, so they hit this easily: they take the slots the drain 
frees, and the last ones come after the drain is done.

A queue without consumer also drops part of its backlog when its first consumer 
starts. Its ring buffer is held by a blocking event handler, which is unblocked 
and copies the exchanges into {{temporaryExchangeBuffer}}, an 
{{ArrayBlockingQueue}} of the queue size, ignoring the result of {{offer()}}. 
The producers that waited on the full ring buffer publish into the freed slots, 
and their exchanges are dropped (or left on the halted ring buffer).

The class comment of {{DelayedExecutor}} says the ring buffer is swapped to 
keep the producers operational without the risk of losing messages, and the 
component page says a reconfiguration flushes the pending exchanges.

h3. How to reproduce

A queue of size 4 without consumer, 4 messages sent, 4 more producers waiting 
for a slot, then a consumer route is added: all 8 sends return normally, the 
consumer gets 4 (one dropped by {{offer()}}, three left on the halted ring 
buffer). With a consumer that is busy while a second consumer route is added, 
the producers that waited for a slot publish on the halted ring buffer; InOut 
producers without timeout never return. Without any injected pause, 4 producers 
and a reconfiguration in a loop for 20 s lost 96 of 30,647 accepted exchanges.

Found with a TLA+ model of the producers, the event handlers and the 
reconfiguration, and reproduced against the real component. 
{{DisruptorReconfigureWithBlockingProducerTest}} (CAMEL-23437) also sends while 
a consumer route is added; its original flakiness came from its own 
expectations (asynchronous delay, wall-clock check), not from this race: its 
original scenario, counted after waiting, gives the first route all 20 messages 
and the added route none in 10 runs each with and without the fix, as the 
producer is done before the route is added.

h3. Fix

The producers between reading the reference and publishing are counted; a 
reconfiguration clears the reference, unblocks the blocking event handler, 
waits until that count is 0, and only then drains and halts the old Disruptor. 
A producer waiting for a free slot is counted too: the old event handlers keep 
running until the drain, so they free its slot. The buffer that carries the 
backlog to the next Disruptor has no bound, so nothing is dropped; the part 
that fits the new ring buffer is published before the new Disruptor is set, the 
rest, in the backlog's own order, by a separate thread that publishes as a 
producer without holding a lock (so a consumer that takes a lock of the 
disruptor, the endpoint or the CamelContext while the rest is published cannot 
block the route start), and a Disruptor without consumer keeps the backlog 
buffered for the first consumer.

"In order" is the order of the backlog only: the order of one producer is not 
kept across the hand-over. A producer that waited on the full ring buffer when 
the first consumer starts returns once its exchange is in the backlog, and its 
next sends can overtake that exchange (main lost it instead); the same happens 
on each suspend and resume of the only consumer, where the exchanges of one 
producer can arrive out of order when several producers send (main lost 
exchanges there).

_Filed with Claude Code on behalf of allthingssecurity._



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to