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

   # Description
   
   [CAMEL-27489](https://issues.apache.org/jira/browse/CAMEL-27489)
   
   `DefaultContextReloadStrategy.onReload` had no mutual exclusion. A reload 
removes all the routes and then creates them again from their definitions 
(`reloadAllRoutes`), and `lockModel` only marks the calling thread. When two 
reloads overlapped, both removed the routes and both created them again, so the 
context ended with two `Route` objects for each route id. Nothing failed: both 
reloads usually counted as succeeded. The extra routes are not consumed twice 
for most components (only one consumer per id is started), but they stay in 
`getRoutes()`, a later reload does not remove them, and route counts and 
management views are wrong. With a single-consumer endpoint such as `direct:` 
the second copy also fails to start, and that reload is counted as failed.
   
   Reloads can come from several threads at the same time: the secret refresh 
tasks (AWS, GCP, Azure and HashiCorp on the thread of the periodic tasks; 
Kubernetes and IBM on their own threads), JMX (`onReload()`), the developer 
console and the Camel CLI. In a test with 5 timer routes, 30 rounds of two 
concurrent reloads left 155 routes.
   
   Change: `onReload` runs under a `ReentrantLock`. A reload requested while 
another one runs waits for it to complete and then runs its own reload. I chose 
waiting over coalescing the second request into the running reload: every 
request is still served, each reload sees the state at the time it runs (for 
example a secret rotated while the first reload ran), and the code stays small. 
The lock is reentrant, so a reload that triggers another reload on the same 
thread does not block. The counters and `getLastError()` are now written by one 
reload at a time.
   
   What a user can notice: a second reload now starts after the first one ends 
instead of at the same time. A reload requested from inside an exchange (a 
processor that calls `onReload`) while another reload stops that exchange's 
route waits until that reload ends, so the graceful shutdown of the route waits 
for the exchange until its timeout (45 seconds by default), and then the second 
reload runs. No upgrade guide entry: nothing is to be done by users.
   
   Not changed here: the reload of routes from changed files 
(`RouteWatcherReloadStrategy`, a `ResourceReloadStrategy`) is a different 
strategy and is not covered by this lock.
   
   Test, new `CamelContextReloadStrategyConcurrentTest` in camel-core 
(package-private, no sleep): the first reload is held right after it removed 
the routes (in the creation of the first endpoint), a second reload is 
requested, then the first one goes on. The context must end with one copy of 
each of the 3 routes, all started, both reloads succeeded. Without the change 
it ends with routes `[a, a, b, b, c]` and the second reload fails on a second 
consumer of `direct:b` (20 of 20 runs); with the change it passes 20 of 20 
runs. With the change, the camel-support unit tests (182 run, 0 failures, in 
both surefire executions) and the reload, secret rotation and 
`org.apache.camel.support` tests of camel-core (697 run, 0 failures) pass. The 
camel-main tests pass too (366 run, 0 failures); the full camel-core suite was 
not run locally (CI runs it).
   
   A TLA+ model of the refresh tasks and the context reload found this: its 
invariant "at most one reload runs at a time" fails on the current code.
   
   # 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-27489](https://issues.apache.org/jira/browse/CAMEL-27489).
   
   # 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 the core modules with the formatter and import-sort plugins and 
ran the tests above. I did not run the full root build. No generated file 
changes.)
   
   # 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