Fix intermittent testOwnerChangesOnPeerAvailabilityChanges failure
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / PreLeader.java
index e3ae4f427ac362c4859f22a40cda707b6145944a..6d2f429de278d2f3ad47d671c582847fc81e8320 100644 (file)
@@ -10,7 +10,7 @@ package org.opendaylight.controller.cluster.raft.behaviors;
 import akka.actor.ActorRef;
 import org.opendaylight.controller.cluster.raft.RaftActorContext;
 import org.opendaylight.controller.cluster.raft.RaftState;
-import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
+import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
 import org.opendaylight.controller.cluster.raft.persisted.NoopPayload;
 
 /**
@@ -22,6 +22,7 @@ import org.opendaylight.controller.cluster.raft.persisted.NoopPayload;
  * the log with the leader's current term. Once the no-op entry is committed, all prior entries are committed
  * indirectly. Once all entries are committed, ie commitIndex matches the last log index, it switches to the
  * normal Leader state.
+ *
  * <p>
  * The use of a no-op entry in this manner is outlined in the last paragraph in ยง8 of the
  * <a href="https://raft.github.io/raft.pdf">extended raft version</a>.
@@ -37,14 +38,18 @@ public class PreLeader extends AbstractLeader {
     }
 
     @Override
-    protected RaftActorBehavior handleAppendEntriesReply(ActorRef sender, AppendEntriesReply appendEntriesReply) {
-        RaftActorBehavior returnBehavior = super.handleAppendEntriesReply(sender, appendEntriesReply);
-
-        if(context.getCommitIndex() >= context.getReplicatedLog().lastIndex()) {
-            // We've committed all entries - we can switch to Leader.
-            returnBehavior = internalSwitchBehavior(new Leader(context, this));
+    public RaftActorBehavior handleMessage(ActorRef sender, Object message) {
+        if (message instanceof ApplyState) {
+            log.debug("{}: Received {} - lastApplied: {}, lastIndex: {}", logName(), message, context.getLastApplied(),
+                    context.getReplicatedLog().lastIndex());
+            if (context.getLastApplied() >= context.getReplicatedLog().lastIndex()) {
+                // We've applied all entries - we can switch to Leader.
+                return internalSwitchBehavior(new Leader(context, this));
+            } else {
+                return this;
+            }
+        } else {
+            return super.handleMessage(sender, message);
         }
-
-        return returnBehavior;
     }
 }