allthingssecurity opened a new pull request, #30805:
URL: https://github.com/apache/camel/pull/30805

   # Description
   
   [CAMEL-27431](https://issues.apache.org/jira/browse/CAMEL-27431)
   
   The consumer checkpoints a partition with the `EventContext` of the event 
whose exchange completed last: `processCommit` sets it on the window task, and 
the batch size path writes it. It is not the last event below which every 
delivered event has completed. The checkpoint store does not compare sequence 
numbers either: `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 follows the 
order in which exchanges complete and writes land, not sequence order.
   
   - With an asynchronous step in the route, `onEventListener` returns at once 
and the exchanges of a partition run concurrently. With 
`checkpointBatchSize=2`: e1, e2 and e3 are delivered, e1 and e3 complete, 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 timer path has the 
same problem. The DST world of this component already uses such an asynchronous 
step.
   - Same route: e2 then 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.
   - With a synchronous route: the timer takes 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; the e1 write then moves the stored checkpoint from 3 
back to 1, and up to a whole batch is delivered again after a restart.
   
   Change, in camel-azure-eventhubs only:
   
   - New package-private `EventHubsPartitionCheckpoints`, one per partition, 
used under the consumer lock. `onEventListener` records each delivered event by 
sequence number before it is processed. When an exchange is done (completed or 
failed), its event is marked, and the entries below which everything is done 
are removed. Events done one after the other are joined into one entry, under 
the last of them (the only one a checkpoint can use), so memory follows the 
events in flight, not the events completed after them.
   - A checkpoint, by batch size or by timeout, is written for the last event 
below which every delivered event of the partition is done, only if its 
sequence number is after the last checkpoint written. Both paths compare under 
the consumer lock: the timer no longer takes a context and writes it outside 
the lock, it calls the consumer's checkpoint under the lock.
   - Only one checkpoint write per partition is in flight. A checkpoint asked 
for while a write is in flight waits for its answer (a later one replaces a 
waiting one), so the writes land in the order they were asked for. When a write 
fails and nothing waits, the same position can be written again by the next 
checkpoint.
   - The events that completed after an event still in flight stay counted for 
the next checkpoint. When the batch size is reached and nothing can be 
checkpointed yet, the window stays open: the next completion, or the timer, 
checkpoints once the earlier event is done. When a failed exchange lets the 
checkpoint move forward and events are counted with no window open, a window is 
started for them.
   - The completion of an exchange whose event was delivered before the 
consumer was stopped, before the partition was lost, or before the partition 
was delivered again, does not move the checkpoint. Its event is delivered again 
from the stored checkpoint. The consumer now passes a `processPartitionClose` 
handler (new four-argument `EventHubsClientFactory.createEventProcessorClient`; 
the three-argument one is kept). On `LOST_PARTITION_OWNERSHIP` it drops the 
state of the partition and cancels its timer. On `EVENT_PROCESSOR_SHUTDOWN` it 
keeps it, so the exchanges still in flight during a graceful stop checkpoint as 
before. A delivery whose sequence number is not after the last one delivered 
(the partition was claimed again without a lost close) also starts the 
partition over. Both cases are logged once at INFO.
   - An exchange whose completion never runs now holds the checkpoint of its 
partition until the partition is lost or the consumer is restarted; its event 
and the events after it are then delivered again. Before, the next checkpoint 
passed it. This happens, for example, when a `seda` endpoint with 
`discardWhenFull` drops the exchange because its queue is full (the completion 
was handed over to the dropped copy). The events completed after it are kept as 
one entry per run, not one per event, so memory does not grow with them. The 
upgrade guide says so.
   - `checkpointBatchSize` and `checkpointBatchTimeout` keep their meaning. 
With a synchronous route there is at most one event in flight per partition, so 
the checkpoints are the same as before, except that a late timer write can no 
longer move the checkpoint back.
   
   Upgrade guide: a 4.24 entry. With an asynchronous route the stored 
checkpoint can now lag behind an event still being processed (after a restart 
that event is delivered again together with the events completed after it, 
where before it was skipped). An exchange whose completion never runs (seda 
`discardWhenFull`) holds the checkpoint of its partition. The 
`CamelAzureEventHubsCheckpointUpdatedBy` header is `size` only on the exchange 
whose completion wrote a checkpoint. The exchanges of a lost partition no 
longer checkpoint. The new factory overload is listed for tests that mock the 
old one.
   
   Model: found with a TLA+ model of the checkpointing of one partition (the 
SDK pump, completions in any order with an asynchronous route, the window 
timer, checkpoint writes that answer in any order, store errors, a crash with 
writes still landing afterwards). The fix is in the same spec as a second 
specification. TLC and everystate give the same result on every configuration 
(and the same state counts where no error is found). On main, "a stored 
checkpoint never passes an event in flight" is violated with an asynchronous 
route (36 to 127 distinct states depending on the configuration), and "the 
stored checkpoint never goes back" is violated with an asynchronous route and 
an in-order store (188) and with a synchronous route and reordered store 
answers (53). With the fix both hold in every configuration, including exchange 
failures and store errors (up to 1,265 distinct states with 4 events), a crash 
and restart (18,024 with 4 events), and the liveness property "every completed 
even
 t is eventually covered by the stored checkpoint" without store errors (128). 
"A failed exchange is never passed" is violated on both, as intended (see 
below); for the same reason, with an exchange failure and a crash in one run, a 
failed event that a checkpoint passed is in flight again after the restart, and 
the in-flight property reports it on both.
   
   Tests:
   
   - New `EventHubsConsumerCheckpointOrderTest`: a real route and consumer, a 
mocked event processor client, and a checkpoint store fake that behaves like 
the blob checkpoint store (the write that lands last wins). The test runs the 
window timer itself, so nothing depends on timing (no sleeps):
     - an event in flight is not passed by a checkpoint (stored 1, not 3, while 
e2 is in flight);
     - the stored checkpoint does not go back with reordered completions 
(writes land e1, e3; main: e3, e2);
     - the stored checkpoint does not go back when a timer write answers late 
(writes land e1, e3; main: e3, e1);
     - a failed exchange is passed by the next checkpoint (control for the 
unchanged behaviour, passes on main);
     - a failed exchange does not hold back the events completed after it 
(control, passes on main);
     - the exchanges in flight when the partition is lost do not checkpoint;
     - the exchanges in flight when the processor stops still checkpoint;
     - the exchanges of earlier deliveries do not checkpoint when the partition 
is delivered again;
     - the 49 events done after an event still in flight (one of them failed) 
are kept as one entry, and the checkpoint goes to e50 when that event completes.
   - The DST world now also checks, at every successful store answer, that the 
stored checkpoint does not pass an event in flight and does not go back. It 
gives the events sequence numbers and stubs the factory whichever overload the 
consumer calls. The two checks are listed with the world's invariants in the 
component JSON and its catalog copy.
   - The existing unit tests that call `processCommit` directly now record the 
delivery first (`processDelivered`) and give their mocked events a sequence 
number. One test no longer completes the same event context twice.
   
   On main's consumer, timer task and factory (the new tests compiled against 
them): of the 9 new tests, 4 fail on their assertions (in flight passed: stored 
3; writes e3, e2; writes e3, e1; the earlier e2 written after the partition was 
delivered again), the 2 partition close tests cannot run (main passes no close 
handler), the entry test cannot run (main keeps no such state), and the 2 
controls pass. Without the joining of done events, the entry test fails (50 
entries, not 2). The DST test fails at its first seed on the new store check 
("checkpoint p0-e9 stored while e2 is in flight", and later "stored checkpoint 
went back from e29 to p0-e24"). With the change: all unit tests of the module, 
1050 run (1000 DST seeds), 0 failures; the DST test with 5000 seeds, 0 failing. 
The new test class 20 times in one JVM: 180/180; with the window, per-partition 
and timer task tests: 400/400.
   
   Not changed:
   
   - A failed exchange is still passed by the next checkpoint and not delivered 
again. The consumer did this before ("we do nothing here"), and it matches the 
Kafka consumer's default. A failure now counts as done for the ordering, so it 
does not hold the checkpoint back.
   - A failed checkpoint by timeout adds its count back, but no timer is 
scheduled again until the next completion of the partition (or a failure that 
lets the checkpoint move forward), so a quiet partition keeps those events 
uncovered and they are delivered again after a restart. A failed checkpoint by 
batch size still does not keep its count.
   - A route stop/start still creates a new checkpoint executor without 
shutting down the old one (only the consumer shutdown does).
   - A checkpoint of a window timer, or a write still waiting, from before a 
route stop can land after a quick restart (within `checkpointBatchTimeout`), 
after a checkpoint of the new start. The stored checkpoint then goes back, or 
passes events the new start is processing again, but only events that were 
processed before the stop. Main does the same.
   - A checkpoint write that never answers holds the next one of its partition 
(there is one waiting slot, not a queue). The blob checkpoint store's HTTP 
calls have their own timeouts and retries.
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   - [x] If this is a large change, bug fix, or code improvement, I checked 
there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for 
the change (usually before you start working on it): 
[CAMEL-27431](https://issues.apache.org/jira/browse/CAMEL-27431).
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful 
subject line and body.
   - [ ] I have run `mvn clean install -DskipTests` locally from root folder 
and I have committed all auto-generated changes.
     (I built and tested the affected module, including the formatter and 
import-sort plugins. I did not run the full root build.)
   
   # AI-assisted contributions
   
   - [x] If this PR includes AI-generated code, commits have proper 
co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR 
description identifies the AI tool used.
     This PR was prepared with Claude Code (Claude Opus 5.5). The commits carry 
a `Co-Authored-By` trailer.
   
   _Claude Code on behalf of allthingssecurity_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to