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

   # Description
   
   [CAMEL-27583](https://issues.apache.org/jira/browse/CAMEL-27583)
   
   `RebalancingCamelClusterService` runs its rebalancing passes on a single 
thread scheduled executor, and each pass scheduled the next one at its very 
end. Two problems with that loop:
   
   - One exception ended the rebalancing for good. The pass had no catch around 
it, and `partitionList()` calls `getNamespaces()` of the delegate outside of 
any try. When something in a pass threw (an `Error`, or a `RuntimeException` 
from `getNamespaces()`), the reschedule was never reached, the executor 
swallowed the exception, and no pass ran again while the service still reported 
Started.
   - Each stop and start of the service added a chain of passes. `doStop` used 
`ExecutorServiceManager.shutdown`, and a scheduled executor still runs its 
delayed task after a plain shutdown. After a start, that old pass ran and 
scheduled its next pass on the new executor, next to the chain that `doStart` 
began (measured with a period of 100 ms: about 7 passes per second, 12 after 
one restart, 24 after two), and in that window a pass on the old thread could 
run at the same time as one on the new thread.
   
   Change (`RebalancingCamelClusterService`, camel-support):
   
   - A new private `reconcileAndReschedule(executor)` runs `reconcile()` inside 
a catch of `Throwable`. The first failure is logged at WARN (with the exception 
at DEBUG), repeated failures at DEBUG only, and the next pass is scheduled in a 
`finally`.
   - `doStop` uses `shutdownNow`, which drops the pending pass. A pass only 
schedules the next one on the executor it ran on, and only while that is still 
the executor of the service (`serializedExecutor` is now `volatile` and set to 
null before the shutdown). A pass scheduled by a stop that lands between that 
check and the schedule is dropped by the stopped executor (with the default 
`CallerRuns` policy nothing is thrown; with an `Abort` policy the 
`RejectedExecutionException` is caught and logged at DEBUG).
   - `rescheduleAfterDelay()` (private) is removed, and `reconcile()` no longer 
schedules anything. `reconcile()` stays `protected` with the same signature, 
and the rebalancing decisions are unchanged.
   
   No upgrade guide entry: nothing changes for a service whose passes do not 
fail and that is not restarted. A subclass that overrides `reconcile()` and 
called `super.reconcile()` keeps working; one that did not call it no longer 
has to schedule the next pass itself (before, such an override ended the loop 
after its first pass).
   
   These two fixes came up in the same review as a larger change for 
camel-kubernetes (members of a partition), which needs more work and is not 
part of this PR. This PR is only the loop of the service.
   
   Tests: new `RebalancingClusterServiceLoopTest` in camel-core 
(package-private, a `DefaultCamelContext` with a small in-memory preemptive 
cluster service and one view, period 200 ms):
   
   - `failingPassDoesNotEndRebalancing`: `getNamespaces()` throws once; 
afterwards passes still run, and a view that is then disabled is enabled again 
by a pass.
   - `restartDoesNotAddChainOfPasses`: three stop and start cycles; no pass 
runs on the thread of the previous executor after the stop, the rate stays at 
most one pass per period (plus a margin for the window edges, an upper bound 
only), and no two passes run at the same time.
   
   Results: with main's `RebalancingCamelClusterService`, both new tests fail 
(`failingPassDoesNotEndRebalancing` times out as no pass runs after the 
failure, `restartDoesNotAddChainOfPasses` finds a pass on the thread of the 
previous executor after the stop). With this change both pass, and the new 
class together with `RebalancingClusterServiceReleaseViewTest` passed 20 runs 
out of 20 (100 of 100 tests). The cluster tests of camel-core 
(`org.apache.camel.cluster` and the file lock cluster tests, 78 tests) pass, 
except `ClusteredRoutePolicyFactoryTest.testClusteredRoutePolicyFactory`, which 
failed once in that run and once in 20 separate runs of its class (the route 
status right after the mock is satisfied); it does not use 
`RebalancingCamelClusterService`, so it is not related to this change.
   
   Found with a TLA+ model of the rebalancing service (the member that held 
both partitions keeps them once the loop has died), and reproduced against the 
real class.
   
   # 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-27583](https://issues.apache.org/jira/browse/CAMEL-27583).
   
   # 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-support and camel-core offline with the formatter and 
import-sort plugins and ran the tests listed above. 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