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

   # Description
   
   [CAMEL-27439](https://issues.apache.org/jira/browse/CAMEL-27439)
   
   With a recoverable aggregation repository, `maximumRedeliveries` and a 
`deadLetterUri`, the recover task of `AggregateProcessor` counts the 
redeliveries of each recovered exchange in an in-memory map 
(`redeliveryState`), and the dead letter decision reads only that map. `doStop` 
cleared the map. So after a route stop and start, the next recover check found 
no count and started again from 1:
   
   - a recovered exchange got up to `maximumRedeliveries` more attempts before 
it was moved to the dead letter channel;
   - the copy sent to the dead letter channel had a `CamelRedeliveryCounter` 
lower than the number of redeliveries that happened.
   
   With `maximumRedeliveries=2` and a route that always fails, the exchange is 
processed 3 times without a restart (the first attempt and 2 redeliveries), and 
the dead letter copy has `CamelRedeliveryCounter=2`. With a route stop and 
start after the first redelivery it was processed 4 times, and the dead letter 
copy still had `CamelRedeliveryCounter=2`.
   
   The change:
   
   - `doStop` keeps the redelivery counts, so the recover task of the next 
start goes on counting from them; `doShutdown` clears them (route removed, 
CamelContext stopped). A route suspend either suspends only the consumer (the 
aggregator and its recover task keep running) or, when the consumer cannot be 
suspended, stops the route, so it is covered as well.
   - Since a stop no longer clears the map, the recover task drops the count of 
any exchange that its scan did not return, right after each scan 
(`redeliveryState.keySet().retainAll(exchangeIds)`). Counts are only added by 
the recover task for exchanges of its scan, and removed when the exchange is 
confirmed, discarded or moved to the dead letter channel, as before. A count 
stayed behind only when someone else removed the exchange (another node, or by 
hand); on main that entry lived until the next route stop, now it goes at the 
next recover check. So the map never holds more entries than the last scan 
returned (plus the exchanges recovered since), however many times the route is 
stopped and started. The cost is one `contains` on the scanned set per entry of 
the map (for Redis, whose scan returns a live key view, one `HEXISTS` per 
entry). This relies on the scan returning every exchange of the repository that 
this aggregator recovers, which the repositories in Camel do (with `recovery
 ByInstance`, the clustered JDBC repository returns the rows of its own 
instance, the only ones it recovers). A scan that misses an exchange it still 
holds would start its count again, as a stop did before: a custom repository 
that returns only part of its exchanges per scan, or Cassandra read at a 
consistency level that can miss a row.
   - The counter is still a plain `int` written and read only by the recover 
task. The recover checks of two starts do not overlap (CAMEL-26642, 
`recoveryInProgress`), and that flag also orders the writes of the old check 
before the reads of the new one.
   
   Not changed: the count is only in memory, so after the route is removed, the 
CamelContext is stopped, or the JVM restarts or crashes, a recovered exchange 
gets up to `maximumRedeliveries` attempts again (and an exchange whose 
processing crashes the JVM is never moved to the dead letter channel). Keeping 
the count across a JVM restart would need the repositories to store it, which 
is a bigger change for a separate issue. The SQL component page now says that 
the count is kept in memory: it survives a route stop and start, and starts 
again when the route is removed, the CamelContext is stopped or the JVM is 
restarted.
   
   Found with a TLA+ model of the recovery of the JDBC aggregation repository 
(route restart, crash, several instances). On main the model breaks the 
redelivery bound with one route restart. With the count kept across the 
restart, the bound holds with up to 2 route restarts (3,996 distinct states), 
and the other properties of the model still hold with crashes, failed confirms 
and a restart under a new instance id (58,908 distinct states).
   
   Tests: `AggregateRecoverRedeliveryCountRouteRestartTest`, with the in-memory 
`KeyValueAggregationRepository` (a recoverable repository in camel-support) 
whose recover call only loads the exchange when the test lets it, so every 
redelivery happens when the test expects it. No sleeps.
   
   - `testRedeliveryCountWithoutRouteRestart`: control, no restart: 3 attempts 
(`CamelRedeliveryCounter` null, 1, 2), then the dead letter channel with 
`CamelRedeliveryCounter=2`. Passes on main as well.
   - `testRedeliveryCountKeptWhenRouteRestarted`: the same with a route stop 
and start after the first redelivery: the same 3 attempts and the same dead 
letter copy. On main: 4 attempts.
   - `testRedeliveryCountDroppedWhenExchangeLeftRepository`: after the first 
redelivery the exchange is confirmed by someone else, a recover check runs 
without it, then it is put back under the same id: it starts again from 
`CamelRedeliveryCounter=1`. On main (and with the fix but without the pruning 
line): 2.
   
   On main 2 of the 3 tests fail (3 runs out of 3); with the fix all pass. The 
camel-core aggregator tests (`org.apache.camel.processor.aggregator.**`): 257 
run, 1 failure, 2 skipped. The failure is `DistributedTimeoutTest`, a timing 
test (a 200 ms completion timeout that can fire between its sends when the 
machine is loaded). It uses the non-recoverable memory repository, so none of 
the changed code runs in it, and it fails now and then on main as well: run on 
its own 80 times with the code of main and 100 times with this change, 2 
failures each, with the same assertion. With the queued aggregator changes for 
CAMEL-27412 and CAMEL-27422 merged on top, the aggregator tests ran 266, 0 
failures, 2 skipped, and the recovery tests below plus theirs 20 times in one 
JVM: 480 run, 0 failures. The new class with the other recovery tests 
(`AggregateRecoverRestartTest`, `AggregateRecoverInProgressTest`, 
`AggregateRecoverableConfirmFailureTest`, 
`AggregateKeyValueRepositoryRecoverTest`, `Aggreg
 ateAutoDiscoveredKeyValueRepositoryRecoverTest`) 20 times in one JVM: 340 run, 
0 failures.
   
   The same scenario with the real `JdbcAggregationRepository` on H2 
(`maximumRedeliveries=2`, a route that always fails, a route stop and start 
after the first redelivery), run as a local test that is not part of this PR: 4 
attempts on main, 3 attempts with the fix and `CamelRedeliveryCounter=2` on the 
dead letter copy. The camel-sql JDBC aggregation tests 
(`org.apache.camel.processor.aggregate.jdbc.**`) with the fix: 74 run, 0 
failures.
   
   # 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-27439](https://issues.apache.org/jira/browse/CAMEL-27439).
   
   # 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 modules, including the formatter and 
import-sort plugins. I did not run the full root build. The catalog copy of the 
SQL component page is updated with it.)
   
   # 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