Fix testLeaderAndFollowerEntityOwnersReassignedAfterShutdown failure
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / AbstractRaftActorBehavior.java
index 5c5c520761ecc4fba107cf5274a95292dc207273..e8c1b09772f172157c177c747b1bc2f7c5b51430 100644 (file)
@@ -131,7 +131,7 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior {
 
         // 1. Reply false if term < currentTerm (ยง5.1)
         if (appendEntries.getTerm() < currentTerm()) {
-            log.debug("{}: Cannot append entries because sender term {} is less than {}", logName(),
+            log.info("{}: Cannot append entries because sender's term {} is less than {}", logName(),
                     appendEntries.getTerm(), currentTerm());
 
             sender.tell(new AppendEntriesReply(context.getId(), currentTerm(), false, lastIndex(), lastTerm(),
@@ -167,7 +167,8 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior {
      */
     protected RaftActorBehavior requestVote(ActorRef sender, RequestVote requestVote) {
 
-        log.debug("{}: In requestVote:  {}", logName(), requestVote);
+        log.debug("{}: In requestVote:  {} - currentTerm: {}, votedFor: {}, lastIndex: {}, lastTerm: {}", logName(),
+                requestVote, currentTerm(), votedFor(), lastIndex(), lastTerm());
 
         boolean grantVote = canGrantVote(requestVote);
 
@@ -367,7 +368,6 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior {
      * @param index the log index
      */
     protected void applyLogToStateMachine(final long index) {
-        long newLastApplied = context.getLastApplied();
         // Now maybe we apply to the state machine
         for (long i = context.getLastApplied() + 1; i < index + 1; i++) {
 
@@ -376,16 +376,18 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior {
                 // Send a local message to the local RaftActor (it's derived class to be
                 // specific to apply the log to it's index)
 
-                final ApplyState msg;
+                final ApplyState applyState;
                 final ClientRequestTracker tracker = removeClientRequestTracker(i);
                 if (tracker != null) {
-                    msg = new ApplyState(tracker.getClientActor(), tracker.getIdentifier(), replicatedLogEntry);
+                    applyState = new ApplyState(tracker.getClientActor(), tracker.getIdentifier(), replicatedLogEntry);
                 } else {
-                    msg = new ApplyState(null, null, replicatedLogEntry);
+                    applyState = new ApplyState(null, null, replicatedLogEntry);
                 }
 
-                actor().tell(msg, actor());
-                newLastApplied = i;
+                log.debug("{}: Setting last applied to {}", logName(), i);
+
+                context.setLastApplied(i);
+                context.getApplyStateConsumer().accept(applyState);
             } else {
                 //if one index is not present in the log, no point in looping
                 // around as the rest wont be present either
@@ -395,10 +397,6 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior {
             }
         }
 
-        log.debug("{}: Setting last applied to {}", logName(), newLastApplied);
-
-        context.setLastApplied(newLastApplied);
-
         // send a message to persist a ApplyLogEntries marker message into akka's persistent journal
         // will be used during recovery
         //in case if the above code throws an error and this message is not sent, it would be fine
@@ -436,7 +434,8 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior {
             return this;
         }
 
-        log.info("{} :- Switching from behavior {} to {}", logName(), this.state(), newBehavior.state());
+        log.info("{} :- Switching from behavior {} to {}, election term: {}", logName(), this.state(),
+                newBehavior.state(), context.getTermInformation().getCurrentTerm());
         try {
             close();
         } catch (RuntimeException e) {