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

   # Description
   
   [CAMEL-27502](https://issues.apache.org/jira/browse/CAMEL-27502)
   
   When the route of a transacted (`transacted=true`) Hazelcast SEDA consumer 
stops while an exchange is being routed, `HazelcastSedaConsumer.doStop` shuts 
its threads down with `shutdownGraceful`. That waits `shutdownAwaitTermination` 
(10 s by default) and then interrupts the threads. The shutdown strategy stops 
the consumer first and only then waits for the exchanges in flight (45 s by 
default), so an exchange that takes longer than 10 s was interrupted although 
the shutdown would still have waited for it:
   
   - route that continues on another thread (`threads()`, an asynchronous 
producer): the interrupt ends the wait of the consumer thread for the exchange, 
the exchange gets an `InterruptedException`, and the consumer rolls the 
transaction back, so the message is back at the head of the queue. The exchange 
goes on and completes. The message is processed a second time after the 
restart, or by a consumer on another member.
   - synchronous route: the interrupt aborts the route at 10 s and the 
transaction is rolled back too, so whatever the route did before is done again.
   
   The interrupt came with CAMEL-25840 (#28261), which made the stop wait for 
the consumer threads. Waiting for the poll is right; the interrupt after 10 s 
is what rolls back an exchange that is still running.
   
   Change (transacted mode only):
   - a consumer thread counts itself before it polls, under a lock together 
with the `isRunAllowed()` check, and uncounts itself once the transaction of 
the exchange is committed or rolled back. So the stop either waits for a 
thread, or the thread sees the stop and does not poll.
   - `doStop` first waits until no thread polls or routes an exchange, at most 
the timeout of the shutdown strategy (45 s by default), and then shuts the 
threads down as before. A thread that only polls is done within a second; when 
the stop waits longer, for an exchange, it logs one INFO line with the number 
of exchanges.
   - the delay after a rolled back exchange (`onErrorDelay`) now starts after 
the thread uncounted itself, so the stop does not wait for it (as before).
   - an exchange that is still being routed when that timeout expires is rolled 
back, as before, and its message is consumed again; this is logged at WARN. The 
shutdown strategy usually times out first, as its timeout started a little 
earlier: it cancels its task, the wait ends, and the threads are interrupted at 
once, with the same rollback (and the WARN of the shutdown strategy).
   - the stop does not wait at all once the shutdown strategy has timed out and 
stops the remaining consumers at once (a route with `shutdownRoute(Defer)`, or 
a route that the timed out shutdown had not reached yet), so a forced shutdown 
does not take another shutdown timeout.
   - a consumer thread that stops the consumer itself, such as a route policy 
that stops it when its exchange completes on that thread, does not wait for 
itself.
   - a consumer thread that finds the consumer stopping skips the delay after 
an error, since the loop ends anyway. In the case above, the stop no longer 
waits for the interrupted thread, and that thread used to start the delay on 
the scheduler that the stop had already shut down (a `NullPointerException` in 
the log). The scheduler field is now volatile, as the consumer threads read it.
   
   Behaviour for users: stopping a transacted route waits for an exchange in 
progress (up to the shutdown timeout) instead of rolling it back after 10 s, so 
the stop of a route with a slow synchronous step can take longer than before. 
The total time of a stop with an asynchronous route does not change: the 
shutdown strategy already waited for that exchange after the rollback. Nothing 
to configure, so no upgrade guide entry. The non-transacted consumer is not 
changed.
   
   Not changed here (found by the same analysis):
   - a message that always fails is taken again after every rollback (after 
`onErrorDelay`) without a limit, so it blocks the queue behind it. A route can 
end that with a dead letter channel or a handled `onException`; a redelivery 
limit for the consumer would be a new feature, which I will raise separately.
   - with `transacted=false` and an asynchronous route, the consumer polls the 
next message as soon as the route goes asynchronous, so the messages in flight 
are not bounded. camel-seda behaves the same way.
   
   Test: new `HazelcastSedaTransactedStopTest` (package-private, embedded 
Hazelcast member, no Docker, no sleep; `shutdownAwaitTermination` set to 1 s in 
the test, the step blocks on a latch):
   - `exchangeThatContinuesOnAnotherThreadIsCommittedWhenTheRouteStops`: route 
with `threads(1)`; the route is stopped while the step runs. For 3 s (longer 
than the 1 s after which the threads were interrupted) the message must stay 
out of the queue and the stop must still wait. The step is then released: the 
stop completes, the message is committed, and a restart does not consume it 
again.
   - `synchronousExchangeIsCommittedWhenTheRouteStops`: the same with a 
synchronous route.
   - `exchangeThatOutlastsTheShutdownTimeoutIsRolledBack`: a control for the 
fallback, which also passes on main: with a shutdown timeout of 2 s, the stop 
returns within 8 s and the exchange is rolled back.
   - `forcedShutdownDoesNotWaitForTheExchangeAgain`: a deferred route with a 
shutdown timeout of 4 s; once the shutdown strategy times out and stops the 
consumer at once, the stop must not wait another 4 s (the route stops within 7 
s and the exchange is rolled back).
   - `consumerStoppedByARoutePolicyOfItsOwnExchangeDoesNotWaitForIt`: a route 
policy stops the consumer when its exchange completes, on the consumer thread; 
the stop must return well within the 6 s shutdown timeout.
   
   The first two fail on main (the message is back in the queue within 3 s); 
the other three pass on main, as main does not wait at all. The last two fail 
without the forced and own-thread checks (the stop waits another shutdown 
timeout). With the change the unit tests of camel-hazelcast pass except two 
that failed the same way in an earlier local build of the module without this 
change 
(`HazelcastAggregationRepositoryRoutesTest.checkAggregationFromTwoRoutes` and 
`HazelcastUserConfigSerializationFilterWarningTest`, which tries to reach a 
cluster): 309 run. The new class passes 20 of 20 runs (100 tests).
   
   A TLA+ model of the consumer loop (transactions, timeouts, rollbacks, 
asynchronous continuations, stop and start) found it: "a successful exchange is 
never rolled back" fails on main at the stop.
   
   # 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-27502](https://issues.apache.org/jira/browse/CAMEL-27502).
   
   # 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 camel-hazelcast and the core modules with the formatter and 
import-sort plugins and ran the module's unit tests, offline. I did not run the 
full root build. No option or generated file changed.)
   
   # 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 commit 
carries 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