Make RaftActor.initiateLeadershipTransfer() private 70/36870/3
authorRobert Varga <rovarga@cisco.com>
Wed, 30 Mar 2016 09:04:51 +0000 (11:04 +0200)
committerRobert Varga <rovarga@cisco.com>
Wed, 30 Mar 2016 09:27:08 +0000 (11:27 +0200)
the only external caller is RaftActorServerConfigurationSupport,
which invokes the method when the actor should become non-voting.

Expose an explicit method for this case and make the leadership
transfer an implementation detail -- making it easier to refactor.

Change-Id: I06c9d1c0c07a92490ddf111d5723c8361001437f
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActor.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorServerConfigurationSupport.java

index fdd4b23..945af8d 100644 (file)
@@ -270,7 +270,7 @@ public abstract class RaftActor extends AbstractUntypedPersistentActor {
         }
     }
 
-    void initiateLeadershipTransfer(final RaftActorLeadershipTransferCohort.OnComplete onComplete) {
+    private void initiateLeadershipTransfer(final RaftActorLeadershipTransferCohort.OnComplete onComplete) {
         LOG.debug("{}: Initiating leader transfer", persistenceId());
 
         if(leadershipTransferInProgress == null) {
@@ -774,6 +774,36 @@ public abstract class RaftActor extends AbstractUntypedPersistentActor {
         }
     }
 
+    /**
+     * Switch this member to non-voting status. This is a no-op for all behaviors except when we are the leader,
+     * in which case we need to step down.
+     */
+    void becomeNonVoting() {
+        if (isLeader()) {
+            initiateLeadershipTransfer(new RaftActorLeadershipTransferCohort.OnComplete() {
+                @Override
+                public void onSuccess(ActorRef raftActorRef, ActorRef replyTo) {
+                    LOG.debug("{}: leader transfer succeeded after change to non-voting", persistenceId());
+                    ensureFollowerState();
+                }
+
+                @Override
+                public void onFailure(ActorRef raftActorRef, ActorRef replyTo) {
+                    LOG.debug("{}: leader transfer failed after change to non-voting", persistenceId());
+                    ensureFollowerState();
+                }
+
+                private void ensureFollowerState() {
+                    // Whether or not leadership transfer succeeded, we have to step down as leader and
+                    // switch to Follower so ensure that.
+                    if (getRaftState() != RaftState.Follower) {
+                        initializeBehavior();
+                    }
+                }
+            });
+        }
+    }
+
     /**
      * @deprecated Deprecated in favor of {@link org.opendaylight.controller.cluster.raft.base.messages.DeleteEntries}
      *             whose type for fromIndex is long instead of int. This class was kept for backwards
index 7012e0d..46fe626 100644 (file)
@@ -678,28 +678,8 @@ class RaftActorServerConfigurationSupport {
             // leadership.
             boolean localServerChangedToNonVoting = Boolean.FALSE.equals(getOperation().
                     getServerVotingStatusMap().get(raftActor.getRaftActorContext().getId()));
-            if(succeeded && localServerChangedToNonVoting && raftActor.isLeader()) {
-                raftActor.initiateLeadershipTransfer(new RaftActorLeadershipTransferCohort.OnComplete() {
-                    @Override
-                    public void onSuccess(ActorRef raftActorRef, ActorRef replyTo) {
-                        LOG.debug("{}: leader transfer succeeded after change to non-voting", raftActor.persistenceId());
-                        ensureFollowerState(raftActor);
-                    }
-
-                    @Override
-                    public void onFailure(ActorRef raftActorRef, ActorRef replyTo) {
-                        LOG.debug("{}: leader transfer failed after change to non-voting", raftActor.persistenceId());
-                        ensureFollowerState(raftActor);
-                    }
-
-                    private void ensureFollowerState(RaftActor raftActor) {
-                        // Whether or not leadership transfer succeeded, we have to step down as leader and
-                        // switch to Follower so ensure that.
-                        if(raftActor.getRaftState() != RaftState.Follower) {
-                            raftActor.initializeBehavior();
-                        }
-                    }
-                });
+            if (succeeded && localServerChangedToNonVoting) {
+               raftActor.becomeNonVoting();
             }
         }