gnodet-bot commented on code in PR #30624:
URL: https://github.com/apache/camel/pull/30624#discussion_r4662894630
##########
components/camel-jgroups-raft/src/main/java/org/apache/camel/component/jgroups/raft/cluster/JGroupsRaftClusterView.java:
##########
@@ -76,81 +87,72 @@ public List<CamelClusterMember> getMembers() {
@Override
protected void doStart() throws Exception {
- if (raftHandle == null && jgroupsConfig != null &&
!jgroupsConfig.isEmpty()) {
- raftHandle = new RaftHandle(new JChannel(jgroupsConfig), new
NopStateMachine()).raftId(raftId);
- } else if (raftHandle == null) {
- raftHandle = new RaftHandle(new
JChannel(JGroupsRaftConstants.DEFAULT_JGROUPSRAFT_CONFIG), new
NopStateMachine())
- .raftId(raftId);
- }
fireLeadershipChangedEvent((CamelClusterMember) null);
-
- // Register the listener once before attempting to connect;
registering inside
- // the retry loop would accumulate duplicate listeners across retries.
- roleListener = new ClusterRoleChangeListener(this);
- raftHandle.addRoleListener(roleListener);
-
- // it may take a while for event to trigger and allow us to join so
retry a while
- Exception cause = null;
- for (int i = 1; i < 11; i++) {
- LOG.debug("Attempt #{} for raft {} to join {}", i, raftId,
jgroupsClusterName);
- try {
- raftHandle.channel().connect(jgroupsClusterName);
- LOG.debug("Joined and connected to {} with raft id: {}",
jgroupsClusterName, raftId);
- cause = null;
- break;
- } catch (Exception e) {
- cause = e;
- }
- // wait for next attempt
- Thread.sleep(5000);
+ roleLock.lock();
+ try {
+ disconnected = false;
+ } finally {
+ roleLock.unlock();
}
- if (cause != null) {
- throw cause;
+
+ ClusterRoleChangeListener listener = new
ClusterRoleChangeListener(this);
+ RaftHandle handle = getRaftClusterService().connectView(listener);
+ roleListener = listener;
+
+ // the node may already be the leader (elected before this view
started), and then no role change is notified
+ roleLock.lock();
+ try {
+ leadershipChanged(Role.Leader.name().equals(handle.raft().role()));
Review Comment:
💡 `RaftHandle.isLeader()` is simpler and avoids fragile string coupling:
```suggestion
leadershipChanged(handle.isLeader());
```
`handle.raft().role()` returns `impl.getClass().getSimpleName()` — a
`String` tied to the internal class names (`"Leader"`, `"Follower"`, ...) of
jgroups-raft's state-machine implementations. `RaftHandle.isLeader()` does the
same check directly and already appears in `JGroupsRaftEndpoint` and the
integration tests in this module.
--
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]