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

   # Description
   
   [CAMEL-27337](https://issues.apache.org/jira/browse/CAMEL-27337)
   
   `JGroupsRaftClusterService` gives one `JGroupsRaftClusterView` per namespace 
(each `master:` endpoint and `ClusteredRoutePolicy` namespace). A node is one 
Raft member with one raft id, so all its views have to use one `RaftHandle`, 
but each view managed the handle as if it were its own:
   
   1. **A namespace started after the Raft election never became leader.** A 
view learned its role only from role change callbacks. Its `doStart` registered 
the listener and called `channel().connect(...)`, which does nothing once the 
shared channel is connected, and never read the current role. A namespace whose 
view starts after the node was elected (a master route added or restarted at 
runtime, or started lazily) kept `isMaster=false`, and as the other nodes are 
followers, no node ran it until the Raft leader changed.
   2. **Stopping one namespace disconnected the handle of all the others.** 
When the last user of a namespace released its view, `doStop` disconnected the 
shared channel and closed its log. jgroups-raft steps the node down on 
disconnect, so every other namespace lost the leadership and the node stayed 
out of Raft (in a 2 node cluster, no namespace had a leader anywhere).
   3. **Without a configured `raftHandle` (the default), a second namespace 
could not start.** Each view created its own `JChannel` and `RaftHandle` with 
the service's single raft id. `NO_DUPES` rejected the second member, and after 
10 tries of 5 s the route failed to start: `SecurityException: join of A 
rejected as it would create a view with duplicate members`. This is not 
mentioned in the docs.
   
   The three have one cause (the views do not share the handle they have to 
share), so they are fixed together.
   
   The change:
   
   - `JGroupsRaftClusterService` owns the node's handle: the configured 
`raftHandle`, or one it creates from `jgroupsConfig` and the raft id (as each 
view did before). It connects the handle when the first view starts (with the 
same 10 tries of 5 s as before) and disconnects it when the last view stops: 
the views are counted under a lock, as `JGroupsRaftEndpoint` and the component 
count the users of their handles (CAMEL-25883). On the last stop a configured 
handle is disconnected and its log closed, as the view did before, so it is 
connected again when a view starts again. A handle the service created is 
closed (channel and log), and a new one is created when a view starts again.
   - One role listener is added to the handle (before it connects), and it 
forwards each role change to the listeners of the views (a 
`CopyOnWriteArrayList`). The views no longer add and remove listeners on the 
handle while it is connected. jgroups-raft keeps its role listeners in a plain 
`ArrayList` that its thread iterates without a lock, so a view starting during 
a role change could make that iteration fail.
   - A view reads the current role (`raft().role()`, which jgroups-raft sets 
before it notifies the listeners) once it is connected, under a lock that the 
role change callbacks of the view take as well, so it does not miss a role 
change, nor apply an old one after a newer one.
   - When a view stops it ignores the role changes still notified to it (from 
the listener snapshot), so a stopped view never becomes leader again. 
`ClusterRoleChangeListener` now calls the view's `leadershipChanged(boolean)`, 
which does the check, set and event under that lock.
   
   The constructor of `JGroupsRaftClusterView` keeps its parameters; the 
configuration and the handle are taken from the service.
   
   Found with a TLA+ model of the cluster views (2 nodes sharing the handle per 
node, Raft role changes abstracted to "at most one leader", the role listeners 
notified one by one, view start and stop step by step). On main it finds a 
started view that does not report the node's leadership (ViewMatchesRaft, 2,473 
distinct states) and a started view whose handle was disconnected (HandleInUse, 
1,884). I extended the model with the fix; all the properties hold (at most one 
leader view per namespace, a stopped view never leads nor fires an event, the 
listeners are not left stale, a started view reports Raft's role, the handle is 
connected while used, the count matches the connected views): 2 nodes, 2 
namespaces: 45,079 distinct states; 3 namespaces and 6 lifecycle steps: 
1,877,450, same counts with TLC and everystate. Without the "ignore once 
stopped" check the model finds a stopped view that becomes leader from a late 
notification (3,828), which is why it is there.
   
   The library behaviour the fix relies on, checked in jgroups-raft 1.1.4.Final 
with javap: `RAFT.changeRole` sets the volatile `impl` before it calls 
`notifyRoleChangeListeners`, `role()` returns `impl`'s simple class name 
(`Leader`, `Follower`, `Learner`), and `impl` is set in the constructor (never 
null). `RAFT.stop()` (on disconnect) closes the log and `RAFT.start()` only 
creates one when there is none, so a configured handle that is connected again 
after its last view stopped reuses its log object, exactly as a single view 
that was stopped and started again did on main.
   
   Tests: new `JGroupsRaftClusterServiceNamespacesTest` (one node with the 
module's `raft.xml`, master routes on timers):
   
   - `testNamespaceStartedAfterElectionGetsLeadership`: a second namespace 
added after the node was elected leader must run;
   - `testStoppingOneNamespaceKeepsTheOthers`: after the route of one namespace 
stops, the handle stays connected and the other namespace keeps running; after 
the last one stops the handle is disconnected, and a namespace started again 
leads again;
   - `testTwoNamespacesWithoutRaftHandle`: two namespaces with no configured 
`raftHandle` both run.
   
   On main all three fail: the second namespace does not run within 20 s, `The 
handle should stay connected while namespace ns2 uses it ==> expected: <true> 
but was: <false>`, and `FailedToStartRouteException: Failed to start route: 
route-ns2 because: java.lang.SecurityException: join of A rejected as it would 
create a view with duplicate members` (after the 10 tries, about 50 s). With 
the change all three pass. The new class 20 times in one JVM: 60 run, 0 
failures.
   
   Module tests: `camel-jgroups-raft`: 34 tests, 0 failures (including 
`ClusterRoleChangeListenerTest`, `JGroupsRaftClusterViewTest`, 
`JGroupsRaftMasterTest` and `JGroupsRaftClusteredRoutePolicyTest`).
   
   Not changed: a view whose connect fails still fails to start after the 10 
tries, as before. A configured `raftHandle` is disconnected (and its log 
closed) when the last view stops, as the view did, but its channel is not 
closed by Camel: on main only `JGroupsRaftClusterView.doShutdown` closed it, 
and nothing shuts the views down. The component closes its own `raftHandle` 
when it stops (CAMEL-25883); the cluster service could do the same in a 
follow-up. The `raftHandle` set on the service is read when the first view 
connects; changing it while views are connected has no effect until they all 
stopped.
   
   # 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-27337](https://issues.apache.org/jira/browse/CAMEL-27337).
   
   # 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 `components/camel-jgroups-raft`, including the 
formatter and import-sort plugins. No option or generated file changes. 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]

Reply via email to