Hide RaftActorBehaviour.switchBehavior() 65/115965/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 21 Mar 2025 14:34:31 +0000 (15:34 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 21 Mar 2025 16:40:05 +0000 (17:40 +0100)
Eliminate indirection through RaftState, allowing us to use a single
method for switching behaviors.

Change-Id: I90ff03ed1dc6f82d8b1d917ff6c7ab97cd474ff1
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractLeader.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Candidate.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Follower.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/IsolatedLeader.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/Leader.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/PreLeader.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/RaftActorBehavior.java

index cc8f3abeb97be6d674c68be827b5092f2de219ab..8df9b7e73eaff84bf40cc906045a78cceae6c3bd 100644 (file)
@@ -526,7 +526,7 @@ public abstract sealed class AbstractLeader extends RaftActorBehavior permits Is
                 requestVote(sender, requestVote);
             }
 
-            return internalSwitchBehavior(RaftState.Follower);
+            return switchBehavior(new Follower(context));
         }
 
         if (message instanceof SendHeartBeat) {
index 294833e04c25a41d3932619ed560b85929c0a9e5..843853ae594c5bea3d6db00f15a33649999a5e0b 100644 (file)
@@ -118,12 +118,12 @@ public final class Candidate extends RaftActorBehavior {
         final var lastApplied = context.getLastApplied();
         final var lastIndex = context.getReplicatedLog().lastIndex();
         if (lastApplied >= lastIndex) {
-            return internalSwitchBehavior(RaftState.Leader);
+            return switchBehavior(new Leader(context));
         }
 
         LOG.info("{}: LastApplied index {} is behind last index {} - switching to PreLeader", logName, lastApplied,
             lastIndex);
-        return internalSwitchBehavior(RaftState.PreLeader);
+        return switchBehavior(new PreLeader(context));
     }
 
     @Override
@@ -149,7 +149,7 @@ public final class Candidate extends RaftActorBehavior {
                 // who we do not know about (as a peer)
                 // to send a message to the candidate
 
-                return internalSwitchBehavior(RaftState.Leader);
+                return switchBehavior(new Leader(context));
             }
 
             startNewTerm();
@@ -182,7 +182,7 @@ public final class Candidate extends RaftActorBehavior {
                     super.handleMessage(sender, message);
                 }
 
-                return internalSwitchBehavior(RaftState.Follower);
+                return switchBehavior(new Follower(context));
             }
         }
 
index 2146cfc5e1bf934ae3157ba1c9fd152b7bc68c34..8133681ecddd52083930191fabde855313974614 100644 (file)
@@ -492,7 +492,7 @@ public class Follower extends RaftActorBehavior {
         if (canStartElection()) {
             if (message instanceof TimeoutNow) {
                 LOG.debug("{}: Received TimeoutNow - switching to Candidate", logName);
-                return internalSwitchBehavior(RaftState.Candidate);
+                return switchBehavior(new Candidate(context));
             } else if (noLeaderMessageReceived) {
                 // Check the cluster state to see if the leader is known to be up before we go to Candidate.
                 // However if we haven't heard from the leader in a long time even though the cluster state
@@ -508,7 +508,7 @@ public class Follower extends RaftActorBehavior {
                     scheduleElection(electionDuration());
                 } else {
                     LOG.debug("{}: Received ElectionTimeout - switching to Candidate", logName);
-                    return internalSwitchBehavior(RaftState.Candidate);
+                    return switchBehavior(new Candidate(context));
                 }
             } else {
                 LOG.debug("{}: Received ElectionTimeout but lastLeaderMessageInterval {} < election timeout {}",
index 34f1d962695b2bdb1f335b88658416770bc72b1d..44971ad5ed60ff027b7bef8e77ed468842fe16a5 100644 (file)
@@ -48,7 +48,7 @@ public final class IsolatedLeader extends AbstractLeader {
         // changes its state to Follower, hence we only need to switch to Leader if the state is still Isolated
         if (ret.state() == RaftState.IsolatedLeader && !isLeaderIsolated()) {
             LOG.info("{}: IsolatedLeader {} switching from IsolatedLeader to Leader", getId(), getLeaderId());
-            return internalSwitchBehavior(new Leader(context, this));
+            return switchBehavior(new Leader(context, this));
         }
         return ret;
     }
index e7409363de35bef9a2eb93a069bf8f8fdded9fa0..6166efd9094d5964db3be03ebcc51a0004487795 100644 (file)
@@ -81,7 +81,7 @@ public non-sealed class Leader extends AbstractLeader {
             if (isLeaderIsolated()) {
                 LOG.warn("{}: At least {} followers need to be active, Switching {} from Leader to IsolatedLeader",
                     logName, getMinIsolatedLeaderPeerCount(), getLeaderId());
-                return internalSwitchBehavior(new IsolatedLeader(context, this));
+                return switchBehavior(new IsolatedLeader(context, this));
             } else {
                 return this;
             }
index 4d9f23614d3e00ac442e8104cca7e4f7a0b8bad7..c3a47770e3dc5eefc3540d0ad761b1a25393a33d 100644 (file)
@@ -47,7 +47,7 @@ public final class PreLeader extends AbstractLeader {
             LOG.debug("{}: Received {} - lastApplied: {}, lastIndex: {}", logName, message, lastApplied, lastIndex);
             return lastApplied < lastIndex ? this
                 // We've applied all entries - we can switch to Leader.
-                : internalSwitchBehavior(new Leader(context, this));
+                : switchBehavior(new Leader(context, this));
         } else {
             return super.handleMessage(sender, message);
         }
index bed30304ebbbfb7db60dac4060f1fbcc09d2a81e..15b698741434086ea5cf4a39c142c012398d7d11 100644 (file)
@@ -421,31 +421,23 @@ public abstract class RaftActorBehavior implements AutoCloseable {
         };
     }
 
+    @Override
+    public abstract void close();
+
     /**
      * Closes the current behavior and switches to the specified behavior, if possible.
      *
      * @param behavior the new behavior to switch to
      * @return the new behavior
      */
-    public RaftActorBehavior switchBehavior(final RaftActorBehavior behavior) {
-        return internalSwitchBehavior(behavior);
-    }
-
-    @Override
-    public abstract void close();
-
-    final RaftActorBehavior internalSwitchBehavior(final RaftState newState) {
-        return internalSwitchBehavior(createBehavior(context, newState));
-    }
-
     @SuppressWarnings("checkstyle:IllegalCatch")
-    final RaftActorBehavior internalSwitchBehavior(final RaftActorBehavior newBehavior) {
+    final RaftActorBehavior switchBehavior(final RaftActorBehavior newBehavior) {
         if (!context.getRaftPolicy().automaticElectionsEnabled()) {
             return this;
         }
 
-        LOG.info("{} :- Switching from behavior {} to {}, election term: {}", logName, state(),
-                newBehavior.state(), context.currentTerm());
+        LOG.info("{} :- Switching from behavior {} to {}, election term: {}", logName, state(), newBehavior.state(),
+            context.currentTerm());
         try {
             close();
         } catch (RuntimeException e) {