Bump persisted PayloadVersion
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / FollowerLogInformation.java
index fe836362c819211dc0720daadbbdaa89c8c8dd5d..a5f24990f6a54b4542bcc67dee35f2cdd4c56467 100644 (file)
@@ -5,15 +5,16 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.controller.cluster.raft;
 
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
 import com.google.common.base.Stopwatch;
 import java.util.concurrent.TimeUnit;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.controller.cluster.raft.behaviors.LeaderInstallSnapshotState;
 
 /**
@@ -65,7 +66,7 @@ public final class FollowerLogInformation {
         this.nextIndex = context.getCommitIndex();
         this.matchIndex = matchIndex;
         this.context = context;
-        this.peerInfo = Preconditions.checkNotNull(peerInfo);
+        this.peerInfo = requireNonNull(peerInfo);
     }
 
     /**
@@ -89,16 +90,23 @@ public final class FollowerLogInformation {
     }
 
     /**
-     * Decrements the value of the follower's next index.
+     * Decrements the value of the follower's next index, taking into account its reported last log index.
      *
-     * @return true if the next index was decremented, ie it was previously >= 0, false otherwise.
+     * @param followerLastIndex follower's last reported index.
+     * @return true if the next index was decremented, i.e. it was previously >= 0, false otherwise.
      */
-    public boolean decrNextIndex() {
+    public boolean decrNextIndex(final long followerLastIndex) {
         if (nextIndex < 0) {
             return false;
         }
 
-        nextIndex--;
+        if (followerLastIndex >= 0 && nextIndex > followerLastIndex) {
+            // If the follower's last log index is lower than nextIndex, jump directly to it, so we converge
+            // on a common index more quickly.
+            nextIndex = followerLastIndex;
+        } else {
+            nextIndex--;
+        }
         return true;
     }
 
@@ -294,8 +302,7 @@ public final class FollowerLogInformation {
      *
      * @return the LeaderInstallSnapshotState if a snapshot install is in progress, null otherwise.
      */
-    @Nullable
-    public LeaderInstallSnapshotState getInstallSnapshotState() {
+    public @Nullable LeaderInstallSnapshotState getInstallSnapshotState() {
         return installSnapshotState;
     }
 
@@ -304,9 +311,9 @@ public final class FollowerLogInformation {
      *
      * @param state the LeaderInstallSnapshotState
      */
-    public void setLeaderInstallSnapshotState(@Nonnull final LeaderInstallSnapshotState state) {
+    public void setLeaderInstallSnapshotState(final @NonNull LeaderInstallSnapshotState state) {
         if (this.installSnapshotState == null) {
-            this.installSnapshotState = Preconditions.checkNotNull(state);
+            this.installSnapshotState = requireNonNull(state);
         }
     }
 
@@ -314,7 +321,7 @@ public final class FollowerLogInformation {
      * Clears the LeaderInstallSnapshotState when an install snapshot is complete.
      */
     public void clearLeaderInstallSnapshotState() {
-        Preconditions.checkState(installSnapshotState != null);
+        checkState(installSnapshotState != null);
         installSnapshotState.close();
         installSnapshotState = null;
     }
@@ -342,8 +349,7 @@ public final class FollowerLogInformation {
         needsLeaderAddress = value;
     }
 
-    @Nullable
-    public String needsLeaderAddress(String leaderId) {
+    public @Nullable String needsLeaderAddress(String leaderId) {
         return needsLeaderAddress ? context.getPeerAddress(leaderId) : null;
     }