allthingssecurity opened a new pull request, #30527: URL: https://github.com/apache/camel/pull/30527
# Description [CAMEL-27296](https://issues.apache.org/jira/browse/CAMEL-27296) `KubernetesClusterView.doStop` only stopped the leadership controller (its executors) and closed the client. It did not clear `currentLeader`, did not tell the listeners, and did not touch the lease. A view is stopped when its last user releases it (`MasterConsumer`, `ClusteredRoutePolicy`), or when the cluster service stops. After that: - the stopped view kept reporting the pod as leader (`getLocalMember().isLeader()`, `getLeader()`, `CamelClusterService.isLeader(namespace)`); - the lease still named the pod, and the pod was still ready, so every other pod kept treating it as the valid leader: no pod could lead the group until this pod became unready or went away; - when the view was used again (for example a clustered route added again), the restarted view reported the old leadership at once, and kept doing so until its first successful lookup (for ever if the API server could not be reached). `FileLockClusterView.doStop` releases its lock and fires leadership lost. This change does the same for Kubernetes: - `KubernetesLeadershipController.stop` now waits for a refresh that is running (up to the renew deadline) after it shuts the executor down, so no lease write of the refresh comes after the stop. `tryAcquireLeadership` does not take the lease once the controller is stopping. - The view clears `currentLeader` and, if the local pod was the leader, fires leadership lost before the lease is released, so another pod can only take over once the listeners on this pod have been told. - The new `KubernetesLeadershipController.releaseLeadership` reads the lease again and, if it names the pod, removes the pod from it with `optimisticDeleteLeaderInfo` (the update the controller already uses to give up the leadership when it is disabled), for both the `Lease` and the `ConfigMap` resource types. A conflict (409, for example the renewal of another group in a shared `ConfigMap`) reads the lease again, up to 3 tries. Any other failure is logged at WARN and the lease is kept until the pod is no longer ready, as before; `doStop` never throws for it. The release runs on a thread of its own and the stop waits for it up to the renew deadline, then gives it up with a WARN (the lease is kept): the Kubernetes client tries a request again on its own after a connection error, a 429 or a 5xx (by default up to 10 times, with delays growing to 3.2 s, and each try can wait for the connection timeout), which could otherwise hold the stop of a route or of the CamelContext for minutes when the API server cannot be reached. A release that was given up does not update the lease any more once it is interrupted, but a request that was already sent may still be applied by the API server. - `setDisabled` reads the executor once, and ignores a refresh that is rejected because the controller is stopping (it read the field twice, and could fail with a `NullPointerException` or `RejectedExecutionException` when it raced with a stop). - The lease is released only when the leadership of the pod has taken effect (LEADER, or losing it after `setDisabled`). A pod that is stopped while it waits in BECOMING_LEADER keeps the lease, as before: it waits there for the previous leader to give up, and an empty lease would let another pod take the lease with immediate effect while the previous leader may still act as one. The stop interrupts that wait, and the controller no longer moves on to LEADER when it is interrupted (before, it logged a WARN with the `InterruptedException` and moved on). - Leadership and member events of a controller are ignored once its view has been stopped (a generation counter, as in `FileLockClusterView`), so a late event of the old notifier cannot set `currentLeader` again, also not after a restart. Model: the bug was found with a TLA+ model of the leadership state machine (controller, notifier and view, two pods, with readiness flaps, failed lookups, crashes, `setDisabled`, and one stop and restart of a view). I adapted that model to the fix (`K8sStop.tla`). On main it finds: a stopped view that keeps claiming leadership, a lease that a stopped pod keeps holding, two pods that report leadership at the same time (the restarted view with its old leader), and the same counting stopped views. With the fix all of these hold (everystate: 7,898,844 distinct states; TLC on the scope without crashes and `setDisabled`: 471,242). Releasing the lease in any state instead (BECOMING_LEADER included) gives two leaders again, which is why the state check is there. Tests: new `KubernetesClusterViewStopTest` (the module's `LockTestServer` mock, both lease resource types): - the leader's view is released by its last user: the view must stop reporting the leadership at once, the listener must get "no leader", the lease must no longer name the pod, and the other pod must take over; - the view is used again while the API server refuses requests: it must not report the leadership (checked for twice the lease duration); - the lease update is refused on stop: the view still stops and no longer reports the leadership, the lease keeps naming the pod, and the other pod takes over once the pod is no longer ready; - the API server answers the leader only after five times the renew deadline: the stop ends within the renew deadline (plus the wait for the running refresh), the view no longer reports the leadership, and the lease is kept; - a pod that is stopped while it waits to become the leader keeps the lease (control, passes on main). On main 8 of the 10 tests fail (the two BECOMING_LEADER cases are the controls); a first version of this change that let the interrupted wait move on to LEADER failed them, and a version that released the lease on the stopping thread failed the renew deadline case (the stop took 10 s). With the change the cluster tests of the module pass: 36 run, 0 failures (`KubernetesClusterServiceTest` 21, `KubernetesClusterViewStopTest` 10, `TimedLeaderNotifierTest` 5). The new class 10 times in one JVM: 100 run, 0 failures. Upgrade guide: a 4.24 entry, as the leadership now moves to another pod when a view stops, instead of when the pod becomes unready. It also says that a stop now waits for a running check of the lease and for the release, each up to the renew deadline. Not changed: - The timing leads of the same model run (an unready lease holder that claims leadership again from NOT_LEADER, and a deleted lease that lets a second pod lead at once) are separate issues. - If a running refresh does not end within the renew deadline (an API call that hangs), its write can still come after the release. - A view stopped while the CamelContext runs (a clustered route removed, or the view stopped through JMX) fires leadership lost and then releases the lease, while `ClusteredRoutePolicy` stops its routes later on its own thread. Another pod can then start its routes while the stopping routes still finish their in-flight exchanges. `FileLockClusterView` behaves the same way when it stops or loses its lock; I would fix both together in a follow-up (keep the lease or lock until the routes of the policy have stopped). A CamelContext stop is not affected, as the routes stop before the cluster service. # 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-27296](https://issues.apache.org/jira/browse/CAMEL-27296). # 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 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]
