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

   # Description
   
   [CAMEL-27417](https://issues.apache.org/jira/browse/CAMEL-27417)
   
   `InMemorySagaCoordinator.compensate()` calls `end(COMPENSATING)`. When it 
loses (the timeout of a step, or another caller, already started the 
compensation), it read `currentStatus` once and returned an already successful 
future if the status was COMPENSATING. The running compensation could still use 
up its retries and set the saga to FAILED. `complete()` had the same shape for 
COMPLETING. So:
   
   - with the Saga EIP in `AUTO` mode, a step with a `timeout` whose body 
outlives the timeout and then fails: the compensate of the owner loses to the 
timeout compensation, gets success at once, and 
`SagaProcessor.handleSagaCompletion` takes the success branch. The exchange 
returns while the compensation still runs, without the compensation failure 
that CAMEL-25419 attaches to it, and the saga then ends FAILED;
   - a `saga:compensate` or `saga:complete` route (or a second split part in 
`MANUAL` mode) that comes while the saga is being compensated or completed is 
told the saga was compensated or completed, also when it then ends FAILED.
   
   Without the race, the same failure is reported 
(`SagaCompensationFailureTest`), so this is the window while COMPENSATING or 
COMPLETING that CAMEL-25419 did not cover.
   
   Changes, in `InMemorySagaCoordinator`:
   
   - `end()` creates the outcome of the finalization (a 
`CompletableFuture<Boolean>` and its kind, compensation or completion) together 
with the change of status, under the lock it already holds, and the winner 
(`compensate()`, `complete()` or the timeout task) completes it with the result 
of `doCompensate`/`doComplete`. That result is complete only after its 
`whenComplete` set the final status and removed the saga, so the status is 
final when the outcome completes.
   - A caller that lost `end()` and finds a finalization of the kind it asks 
for still running waits for that outcome, and then answers from the final 
status exactly as before: success for COMPENSATED (COMPLETED), otherwise 
`IllegalStateException("Cannot compensate: status is FAILED")`. A caller that 
asks for the other kind, or comes after the finalization is done, is answered 
at once as before. The winner's answer is unchanged.
   - The exchanges that compensate or complete a step now carry the exchange 
property `CamelInMemorySagaFinalizing` with the id of their saga. A call from 
such an exchange for its own saga (a compensation route that calls 
`saga:compensate`) is answered at once, as before: waiting would wait for the 
very compensation it is part of, which holds a thread of the saga executor in 
`ProducerTemplate.send`, so it would never end. A mutant without this check 
fails the control test (its bounded wait times out); with `saga:compensate`, 
which waits without a bound, it would hang.
   - The waiting is asynchronous (the future of the caller completes later), as 
for the winner: the Saga EIP and `SagaProducer` already handle it through their 
callbacks. The future completes on the thread that ends the finalization (a 
thread of the saga executor when a step has a compensation or completion 
endpoint), as the winner's does, so the Saga EIP goes on with the route there.
   - The check covers the saga of the finalization itself. A compensation route 
that starts a saga of its own (`REQUIRES_NEW`) and asks `saga:compensate` for 
it while that saga's step timeout compensates it now waits for that 
compensation, and holds its thread of the saga executor meanwhile (the 
compensation it waits for runs on another one).
   
   Upgrade guide entry for 4.24 (a caller now waits for a running compensation 
or completion and fails when it fails; the new property), and one sentence in 
saga-eip.adoc (with its catalog copy).
   
   Leads, not changed:
   
   - `beginStep` can throw after the step was enlisted when the saga executor 
is shut down (the service stopped with routes still processing): the exception 
escapes synchronously and the saga stays RUNNING in the map (model lead, not 
reproduced).
   - A service stop drops the scheduled timeouts and the finalization tasks in 
flight; the saga then stays COMPENSATING or RUNNING (documented by the WARN of 
`InMemorySagaService` on stop). With this change a caller that waits for such a 
dropped compensation does not get an answer; before, it got a success that was 
not true.
   
   Model: found with a TLA+ model of the in-memory saga (the owner step and 
joining steps with timeouts, option evaluation that throws, finalizations that 
fail or abort, the timeout task, a second caller of 
`compensate()`/`complete()`, a stop and start of the service). I added a 
`Fixed` switch to it. On main the invariant "a caller is told success only when 
the saga ended compensated (completed)" is violated (everystate 309 distinct 
states; the `complete()` variant 5,997). With the fix it holds, with all the 
invariants of the model (every enlisted step finalized once, ends once, no 
enlistment after the end) and with "every call returns" and "the saga ends" 
when the service is not stopped: 17,456 distinct states with one joining step, 
234,301 with two, and the safety part with a service stop 1,472,559. TLC gives 
the same counts (17,456 and 234,301, liveness included) and the same violation 
on main.
   
   Tests: new `SagaFinalizationInProgressTest` (5, `ContextTestSupport`, 
`maxRetryAttempts=1`, finalization routes that wait on a latch and then fail or 
succeed, no sleeps):
   
   - `ownerThatLosesToTheTimeoutCompensationGetsItsFailure` (`AUTO`, 
`timeout(200ms)`): the exchange must not return while the compensation runs, 
and then carries the compensation failure as a suppressed exception. On main it 
returns at once without it;
   - `compensateWhileTheTimeoutCompensationRunsGetsItsFailure` (the second 
caller of `saga:compensate`): on main the future completes at once, 
successfully;
   - `completeWhileAnotherCompletionRunsGetsItsFailure`: the same for 
`complete()`;
   - `compensateWhileTheCompensationRunsGetsItsSuccess`: control, a second 
caller gets success once the compensation succeeded;
   - `compensationThatCompensatesItsOwnSagaDoesNotWaitForItself`: control, the 
compensation route calls `compensate()` on its own saga and waits for it.
   
   On main the first 3 fail and the 2 controls pass. With the change the saga 
tests of camel-core pass: 44 run, 0 failures. All 13 saga test classes 20 times 
in one JVM: 880 run, 0 failures.
   
   Backport: the FAILED status this relies on came with CAMEL-25419 (4.23). I 
did not check or run the maintenance branches.
   
   # 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-27417](https://issues.apache.org/jira/browse/CAMEL-27417).
   
   # 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, and copied the EIP page to the catalog. 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