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

   # Description
   
   [CAMEL-27587](https://issues.apache.org/jira/browse/CAMEL-27587)
   
   When `MasterConsumer` holds the leadership, a background task creates and 
starts the delegated consumer. If the master consumer is stopped while that 
start is still running, `doStop` cancels the task with an interrupt and shuts 
the leadership thread pool down without waiting. A delegated consumer whose 
start does not stop on the interrupt (a client connect that catches 
`InterruptedException`, or blocking I/O) keeps starting, and `stop()` returns.
   
   When the same master consumer is started again (for example with the start 
and stop operations of its MBean), `doInit` creates a new thread pool, the 
cluster view reports the current leadership, and a new task starts a second 
delegated consumer and publishes it. When the first start then completes, it 
only checks that the leadership is taken and that the master consumer may run. 
Both are true again, so it publishes the first delegated consumer over the 
second one. The second one keeps running and nothing references it. Losing the 
leadership stops only the first, so the second keeps consuming on a node that 
is no longer the leader, next to the consumer of the new leader, and a later 
stop does not stop it either.
   
   The fix has two parts:
   
   - The start task publishes its delegated consumer only while it is still the 
current task (`leaderTask`) and no delegated consumer is set. Otherwise it 
stops its own consumer. It no longer stops the delegated endpoint there: a 
newer delegated consumer (of this master consumer, or of the new master 
consumer after a route restart) may run on it, and `onLeadershipLost` and 
`doStop` stop it. After a start that finished once the leadership was lost, the 
endpoint therefore stays started until then, as it already does after a start 
that failed. Stopping a consumer also stops the processor it was given, which 
is the processor of the route, so when another delegated consumer runs, the 
processor is started again right after.
   - `doStop` waits for a start that is still running before it stops the 
delegated consumer: `shutdownGraceful` of the leadership pool instead of 
`shutdown`. That is bounded by the `shutdownAwaitTermination` of the 
`ExecutorServiceManager` (10 seconds by default), and as long again after 
interrupting the start. Without this part, the delegated consumer could also 
finish starting after the route had stopped, until the task saw that and 
stopped it again.
   
   The leadership pool has a single thread, so within one lifecycle a second 
start never runs while the first is still starting. The race needs a stop and a 
start of the same instance (a route stop and start, or a suspend and resume of 
the route, creates a new master consumer instead), and a delegated start that 
outlasts the stop. With the wait in `doStop`, it now needs a start that blocks 
past both waits.
   
   Also visible: when the leadership is taken, lost and taken again while a 
start is slow, the late start now stops the consumer it started and the newer 
attempt starts a fresh one; the delegated endpoint then stays started until the 
next loss of the leadership or stop.
   
   Model: found with a TLA+ model of the start attempts across a stop and start 
of the same master consumer (at most one delegated consumer, none on a node 
that is not the leader, none after the stop). With both parts the properties 
hold with two restarts of the same instance; the publish check alone still lets 
the stale consumer run for a moment, so both are needed.
   
   Tests: two new tests in `MasterConsumerLeadershipTest`, with a delegated 
consumer whose start waits on a latch and ignores interrupts:
   
   - `testStartAttemptFromBeforeARestartDoesNotReplaceTheNewConsumer`: the 
start blocks, the same master consumer is stopped (with a 100 ms 
`shutdownAwaitTermination`, so the stop does not wait for it) and started 
again, and the new lifecycle starts a second delegated consumer. When the first 
start is released, its consumer must be stopped, the second must still run with 
its endpoint started, a message routed by the second must reach `mock:result`, 
and losing the leadership must stop it.
   - `testStopWaitsForAStartAttemptInFlight`: the stop must not return while 
the start is blocked, and when it returns the delegated consumer must have been 
stopped.
   
   On main both new tests fail (the first because the consumer of the stale 
start is never stopped, the second because the stop returns at once). With only 
the publish check and without the wait in `doStop`, the second test fails; 
without starting the processor again, the message of the first test fails with 
`RouteStoppingException`. With the change all 16 tests of camel-master pass, 
and `MasterConsumerLeadershipTest` 20 times in one JVM: 280 run, 0 failures.
   
   The upgrade guide for 4.24 gets a short entry, as a stop can now take longer 
when the start of a delegated consumer hangs.
   
   Not changed here: if a start outlasts the wait of a route stop and the route 
is started again (a new master consumer), the late start of the old one still 
stops the processor of the route, which the consumer of the new one shares. 
That needs the start to block past both waits; I will open a follow-up issue 
for it.
   
   # 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-27587](https://issues.apache.org/jira/browse/CAMEL-27587).
   
   # 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