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

   # Description
   
   [CAMEL-27094](https://issues.apache.org/jira/browse/CAMEL-27094): 
`removeAllRoutes()` is quadratic in the number of routes, as suggested by 
@davsclaus in the review of #30255.
   
   `removeAllRoutes()` removed each route with `removeRoute()`. Each removal of 
a stopped route gathered the endpoints of all remaining routes, and 
`doRemove()` then counted, for each endpoint of the removed route, how many 
route endpoint sets contain it. Removing n routes was O(n²) in both steps.
   
   `removeAllRoutes()` now gathers the endpoints in use once, as the endpoints 
of each route and the number of routes using each endpoint (`EndpointsInUse`), 
and `removeRoute()` uses and updates them. A removal then only looks at the 
endpoints of the removed route, and removes those that no other route uses, 
which are the same endpoints as before.
   
   Following the approach discussed on the JIRA issue:
   
   - `removeAllRoutes()` still calls the overridable `removeRoute(String, 
LoggingLevel)` for each route, and does not hold `lock` across the loop, as 
`DefaultCamelContext` takes the model lock first.
   - The gathered endpoints are only read and updated under `lock`. Starting a 
route while `removeAllRoutes()` runs discards them, so `removeRoute()` gathers 
them again, as it does outside `removeAllRoutes()`, and keeps the endpoints the 
new route uses.
   - `removeRoute()` called on its own gathers the endpoints for that removal, 
as before.
   
   # Tests
   
   New `RemoveAllRoutesGatherEndpointsTest`:
   
   - each route's endpoints are gathered once by `removeAllRoutes()` (fails 
without the change: `endpoints of route b gathered ==> expected: <1> but was: 
<2>`);
   - shared and private endpoints are removed once all routes are removed;
   - a route added while `removeAllRoutes()` runs, sharing an endpoint with a 
route removed later, keeps that endpoint (fails if starting a route does not 
discard the gathered endpoints).
   
   All 166 camel-core tests that call `removeRoute`, `removeRouteDefinition` or 
`removeAllRoutes` pass, including `RouteRemoveGatherEndpointsTest` from #30255. 
`formatter:format impsort:sort` leaves the change unchanged.
   
   No user-visible behaviour changes, so there is no upgrade guide entry.
   
   # 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).
   
   _Claude Code on behalf of Ayush0612005_
   


-- 
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