shashank created CAMEL-27431:
--------------------------------

             Summary: camel-azure-eventhubs - with an asynchronous route a 
checkpoint can pass an event still being processed (lost after a stop or 
rebalance), and the stored checkpoint can move backwards
                 Key: CAMEL-27431
                 URL: https://issues.apache.org/jira/browse/CAMEL-27431
             Project: Camel
          Issue Type: Bug
          Components: camel-azure-eventhubs
            Reporter: shashank
            Assignee: shashank


h3. What happens

The consumer checkpoints a partition with the {{EventContext}} of the event 
whose exchange completed *last* ({{EventHubsConsumer.processCommit}} sets it on 
the window task and writes it on the batch size path), not with the last event 
below which every delivered event has completed. The checkpoint store does not 
compare sequence numbers: {{EventContext.updateCheckpointAsync}} stores that 
event's own sequence number and {{BlobCheckpointStore.updateCheckpoint}} 
overwrites the blob metadata, so the write that lands last wins. The stored 
checkpoint therefore follows completion order and the order in which writes 
land, not sequence order.

*1. A checkpoint passes an event still in flight; a stop, crash or rebalance 
then loses it.* With any asynchronous step in the route, {{onEventListener}} 
returns at once and the exchanges of a partition run concurrently. Example with 
{{checkpointBatchSize=2}}: e1, e2 and e3 are delivered, e1 and e3 complete, the 
count reaches 2 and the checkpoint stores e3 while e2 is still being processed. 
If the consumer stops, crashes or the partition moves to another consumer 
before e2 completes, the next owner resumes at e4 and e2 is never processed. 
The batch timeout path has the same problem (its context is the last completer 
too). The component's own DST world uses exactly such an asynchronous step.

*2. The stored checkpoint moves backwards with reordered completions.* Same 
route: after the size checkpoint at e3, e2 completes, starts a new window whose 
context is e2, and its timer writes e2: the stored checkpoint goes from 3 back 
to 2, and e3 is delivered again after a restart.

*3. With a synchronous route, a late timer write moves the checkpoint back.* 
The timer takes the context of e1 under the consumer lock and writes it outside 
the lock. If that write answers late (or the timer thread is preempted before 
it writes), e2 and e3 reach the batch size and their checkpoint lands first; 
then the e1 write lands and the stored checkpoint goes from 3 back to 1. Up to 
a whole batch is delivered again after a restart.

h3. How to reproduce

A real route and {{EventHubsConsumer}}, a mocked {{EventProcessorClient}} and a 
checkpoint store fake with the semantics of {{BlobCheckpointStore}} (last write 
wins):
* 1: asynchronous route, {{checkpointBatchSize=2}}: deliver e1, e2, e3, 
complete e1 and e3: the stored checkpoint is 3 while e2 is in flight.
* 2: then complete e2 and run the window timer: writes land as e3, e2, stored 2.
* 3: synchronous route: deliver e1, run the timer with its write held, deliver 
e2 and e3, release the e1 write: writes land as e3, e1, stored 1.

Found with a TLA+ model of the checkpointing (pump, completions in any order, 
timer, checkpoint store answers in any order, crash and restart): "the 
checkpoint never passes an event in flight" and "the stored checkpoint never 
decreases" are violated.

h3. Fix

Per partition, the consumer tracks the delivered events by sequence number 
until every event before them has completed (successfully or not). A checkpoint 
(by batch size or by timeout) is written for the last event below which every 
delivered event has completed, only if it is after the last checkpoint written 
(compared under the consumer lock, for both paths), and only when the previous 
write of the partition has answered (a later checkpoint replaces a waiting 
one), so writes land in order. Events completed after an event still in flight 
stay counted for the next checkpoint. When a partition is lost (or delivered 
again from the stored checkpoint), its state is dropped and the exchanges still 
in flight for it no longer write checkpoints. {{checkpointBatchSize}} and 
{{checkpointBatchTimeout}} keep their meaning.

Consequence: an exchange whose completion never runs (for example one that a 
seda endpoint with {{discardWhenFull}} drops when its queue is full, as the 
completion was handed over to the dropped copy) now holds the checkpoint of its 
partition until the partition is lost or the consumer restarts, and its event 
and the events after it are then delivered again; before, the next checkpoint 
passed it. The events completed after it are kept as one entry per run, so 
memory does not grow with them.

Not changed: a failed exchange is still passed by the next checkpoint (as the 
consumer did before, and as the Kafka consumer does by default); a failed timer 
checkpoint is still retried only when the partition has another event; a route 
stop/start still creates a new checkpoint executor without shutting down the 
old one, and a timer checkpoint from before the stop can still land after a 
quick restart.

Related: CAMEL-25683 and CAMEL-25781 (window and count accounting of the same 
checkpointing).

_Filed with Claude Code on behalf of allthingssecurity._



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

Reply via email to