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

   # Description
   
   [CAMEL-27422](https://issues.apache.org/jira/browse/CAMEL-27422)
   
   With a recoverable aggregation repository (for example 
`JdbcAggregationRepository`), a group discarded by `discardOnCompletionTimeout` 
or `discardOnAggregationFailure` is first moved to the completed store by 
`remove`, and then `AggregateProcessor.discard` confirms it:
   
   ```java
   // must confirm the discarded exchange
   aggregationRepository.confirm(aggregated.getContext(), 
aggregated.getExchangeId());
   ```
   
   Nothing was around this call. When the confirm threw (for example a 
transient database error), the exception left `onCompletion`, whose finally 
block clears the mark that the exchange is being completed. The completion 
timeout checker only logged it and tried the key again, which does nothing as 
the group is no longer in the repository. With `discardOnAggregationFailure`, 
the message whose aggregation failed failed with the repository exception. 
Either way the row stayed in the completed store, held by nobody as in progress 
or as not confirmed, so the next recover pass recovered the discarded group and 
sent it to the route, marked as redelivered. Force discarding a group 
(`AggregateController.forceDiscardingOfGroup`) calls the same `discard`.
   
   CAMEL-26995 fixed this for a processed exchange, and CAMEL-27008 for an 
exchange moved to the dead letter channel: a confirm that throws or returns 
false keeps the exchange as not confirmed, so the recover task confirms it 
again and does not send it again. This PR does the same for a discarded 
exchange. For a recoverable repository, `discard` now calls `confirmWithResult` 
in a new `confirmDiscarded`, shaped like the helpers of those two fixes 
(`AggregateOnCompletion.confirmWithResult`, 
`RecoverTask.confirmMovedToDeadLetter`; both are private to their inner classes 
and log their own case). When the confirm throws or returns false, it logs a 
WARN and adds the id to `unconfirmedCompleteExchanges`. A `VirtualMachineError` 
is rethrown, as in those helpers.
   
   Keeping the id as not confirmed is not enough on its own. A recover pass 
takes its snapshot of the exchanges in progress and the unconfirmed ones before 
it scans. If a pass has taken its snapshot and scanned the completed row before 
the discard, it checks the id only after the discard has released the lock and 
cleared the completing mark. The id is then neither in its snapshot nor marked 
as being completed, so the pass recovers it anyway:
   
   ```
   recover pass:  snapshot = {}, scan -> {X}            (X was just moved to 
the completed store)
   discard:       confirm(X) throws, unconfirmed += X, onCompletion finally: 
completing -= X
   recover pass:  X not in the snapshot, not completing -> recover X and send it
   ```
   
   So, while a pass runs (`recoveryInProgress`), `confirmDiscarded` also adds 
the id to the snapshot of that pass, as `onSubmitCompletion` does for a new 
exchange in progress. Both writes happen while the exchange is still marked as 
being completed (and, without optimistic locking, under the aggregation lock), 
so a pass cannot see the id in between. A repository that is not recoverable is 
called as before, with `confirm` and no catch.
   
   The javadoc of `RecoverableAggregationRepository.confirmWithResult` now says 
it is also invoked for a discarded exchange, next to the dead letter case that 
CAMEL-27008 added.
   
   One existing test changes its expectation. 
`AggregateRecoverInProgressTest.testForceDiscardingNotConfirmedIsRecovered` 
(CAMEL-24943) checked that a force-discarded group is not left marked as being 
completed when its confirm fails. It did that by asserting that 
`forceDiscardingOfGroup` throws and that the recover task then recovers the 
group and sends it again. With this change the group is still not stuck, but it 
is confirmed again instead of being sent, so the test is now 
`testForceDiscardingNotConfirmedIsConfirmedAgain`: the call returns 1, the 
confirm is retried, nothing is recovered or sent.
   
   This reverses part of what the CAMEL-24943 commit message says: for a 
discarded or force-discarded exchange that is not confirmed, it says that 
"those exchanges are still recovered and delivery stays at-least-once". The 
goal of CAMEL-24943 was that such an exchange is not stuck. Sending it is not 
right for a group the route asked to discard, as it is never sent when its 
confirm works. So the change is on purpose. With a recoverable repository whose 
confirm fails, two more things are different, and both now behave like the 
processed and dead letter cases:
   - `forceDiscardingOfGroup` and `forceDiscardingOfAllGroups` no longer throw. 
`forceDiscardingOfAllGroups` used to stop at the first group whose confirm 
threw, and now goes on with the other groups.
   - With `discardOnAggregationFailure`, the message whose aggregation failed 
no longer fails with the repository exception.
   
   The failure is logged at WARN instead. None of this was in the 
documentation, and no option changes, so there is no upgrade guide entry, as 
for CAMEL-26995 and CAMEL-27008.
   
   Not changed here:
   - `unconfirmedCompleteExchanges` is in memory. After a JVM restart, a 
discarded group whose confirm failed and was not yet retried is recovered and 
sent, as a processed exchange is after CAMEL-26995.
   - The recover task counts redeliveries in memory only, and a route 
stop/start clears the count, so a recovered exchange can be redelivered more 
than `maximumRedeliveries` times. This is a separate issue and gets its own 
JIRA.
   - When a confirm commits but the client still gets an error (for example a 
timeout after the commit), the id stays in `unconfirmedCompleteExchanges` until 
the JVM stops, because the scan no longer returns it. This happens on all three 
paths and only costs memory. I can open a follow-up JIRA for it.
   
   Found with a TLA+ model of the remove, confirm and recover steps of one 
aggregated exchange on two or three Camel instances with `recoveryByInstance`. 
Results are the same with everystate and TLC. On main the model sends a 
discarded group after one failed confirm (2 instances). If the fix only keeps 
the id as not confirmed, the model still sends it, through the interleaving 
above. With the full fix, "a discarded group is never sent" holds together with 
the existing properties (at most one processing at a time, a confirmed exchange 
is never recovered, at most one move to the dead letter channel). That covers 3 
instances, 2 failed confirms and 1 route restart (73,710 distinct states). 
Every completed row is eventually confirmed (liveness, 5,580 states). With a 
crash, the model sends the group again after the restart, which is the 
limitation in the first point above.
   
   The issue was first reproduced against the real `JdbcAggregationRepository` 
on H2, with a repository whose first `confirm` throws 
`TransientDataAccessResourceException`, `completionTimeout(1000)`, 
`discardOnCompletionTimeout()` and recovery every 500 ms. On main the discarded 
group reaches the route with `CamelRedelivered=true` and 
`CamelAggregatedCompletedBy=timeout` (4 runs out of 4). That test stays out of 
the PR, which adds no camel-sql dependency. With this change, the same test 
(with `confirmWithResult` made to throw once, since the change calls it instead 
of `confirm`) passes 3 runs out of 3: the group is not sent, and the confirm is 
retried. Against main it fails 2 runs out of 2. The existing camel-sql 
`JdbcAggregate*Test` tests pass on top of this change (30 run, 0 failures).
   
   Tests: `AggregateRecoverableDiscardConfirmFailureTest` uses an in-memory 
recoverable repository, as in `AggregateRecoverableConfirmFailureTest`, whose 
first confirm throws. It covers `discardOnCompletionTimeout`, 
`discardOnAggregationFailure` and force discarding. For the first two there is 
a second variant where the confirm throws only after a recover pass has scanned 
the completed exchange. That pass then waits for the aggregation lock that the 
discard holds, which is the interleaving above. The repository waits on a 
latch, and the tests wait with Awaitility. No sleeps. Each test checks that the 
group is not sent and not recovered, and that it is confirmed twice. The 
`discardOnAggregationFailure` tests also check that the message whose 
aggregation failed does not fail.
   
   On main, all 5 new tests and the changed `AggregateRecoverInProgressTest` 
test fail: the group is sent (`[A+B]`), or `forceDiscardingOfGroup` throws. 
With the snapshot step removed from the fix, the two "during recover check" 
tests fail (3 runs out of 3) and the other three pass. With the change, the 
camel-core processor tests (`org.apache.camel.processor.**`) pass: 3371 run, 1 
failure, 17 skipped. The failure is `BacklogTracerCalledRouteTimestampTest` (a 
timestamp comparison), which fails the same way on other branches and does not 
touch the aggregator. The new test class and the recovery tests next to it 
(`AggregateRecoverInProgressTest`, `AggregateRecoverableConfirmFailureTest`, 
`AggregateRecoverRestartTest`) ran 20 times in one JVM: 340 run, 0 failures. 
The aggregator tests (`org.apache.camel.processor.aggregator`) pass on the 
final commit: 268 run, 0 failures, 2 skipped.
   
   # 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-27422](https://issues.apache.org/jira/browse/CAMEL-27422).
   
   # 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.)
   
   # 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