Fixup chunk offset movement on resend
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / LeaderInstallSnapshotState.java
index 23a0f6d027c53841644a6ab1c5273708a9e0a011..cc4caa32ebacc8511fd4ea610ea6af2af2e53bf4 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.controller.cluster.raft.behaviors;
 
+import com.google.common.base.MoreObjects;
 import com.google.common.base.Stopwatch;
 import com.google.common.io.ByteSource;
 import java.io.IOException;
@@ -29,13 +30,15 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
     // The index that the follower should respond with if it needs the install snapshot to be reset
     static final int INVALID_CHUNK_INDEX = -1;
 
+    static final int INITIAL_OFFSET = -1;
+
     // This would be passed as the hash code of the last chunk when sending the first chunk
     static final int INITIAL_LAST_CHUNK_HASH_CODE = -1;
 
     private final int snapshotChunkSize;
     private final String logName;
     private ByteSource snapshotBytes;
-    private int offset = 0;
+    private int offset = INITIAL_OFFSET;
     // the next snapshot chunk is sent only if the replyReceivedForOffset matches offset
     private int replyReceivedForOffset = -1;
     // if replyStatus is false, the previous chunk is attempted
@@ -68,13 +71,15 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
 
         LOG.debug("{}: Snapshot {} bytes, total chunks to send: {}", logName, snapshotSize, totalChunks);
 
-        replyReceivedForOffset = -1;
+        replyReceivedForOffset = INITIAL_OFFSET;
         chunkIndex = FIRST_CHUNK_INDEX;
     }
 
     int incrementOffset() {
-        if (replyStatus) {
-            // if prev chunk failed, we would want to sent the same chunk again
+        // if offset is -1 doesnt matter whether it was the initial value or reset, move the offset to 0 to begin with
+        if (offset == INITIAL_OFFSET) {
+            offset = 0;
+        } else {
             offset = offset + snapshotChunkSize;
         }
         return offset;
@@ -82,7 +87,7 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
 
     int incrementChunkIndex() {
         if (replyStatus) {
-            // if prev chunk failed, we would want to sent the same chunk again
+            // if prev chunk failed, we would want to send the same chunk again
             chunkIndex =  chunkIndex + 1;
         }
         return chunkIndex;
@@ -125,15 +130,17 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
             replyStatus = true;
             lastChunkHashCode = nextChunkHashCode;
         } else {
-            // if the chunk sent was failure
-            replyReceivedForOffset = offset;
+            // if the chunk sent was failure, revert offset to previous so we can retry
+            offset = replyReceivedForOffset;
             replyStatus = false;
         }
     }
 
     byte[] getNextChunk() throws IOException {
+        // increment offset to indicate next chunk is in flight, canSendNextChunk() wont let us hit this again until,
+        // markSendStatus() is called with either success or failure
+        int start = incrementOffset();
         if (replyStatus || currentChunk == null) {
-            int start = incrementOffset();
             int size = snapshotChunkSize;
             if (snapshotChunkSize > snapshotSize) {
                 size = (int) snapshotSize;
@@ -165,12 +172,13 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
         closeStream();
         chunkTimer.reset();
 
-        offset = 0;
+        offset = INITIAL_OFFSET;
         replyStatus = false;
-        replyReceivedForOffset = offset;
+        replyReceivedForOffset = INITIAL_OFFSET;
         chunkIndex = FIRST_CHUNK_INDEX;
         currentChunk = null;
         lastChunkHashCode = INITIAL_LAST_CHUNK_HASH_CODE;
+        nextChunkHashCode = INITIAL_LAST_CHUNK_HASH_CODE;
 
         try {
             snapshotInputStream = snapshotBytes.openStream();
@@ -200,4 +208,20 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
     int getLastChunkHashCode() {
         return lastChunkHashCode;
     }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("snapshotChunkSize", snapshotChunkSize)
+                .add("offset", offset)
+                .add("replyReceivedForOffset", replyReceivedForOffset)
+                .add("replyStatus", replyStatus)
+                .add("chunkIndex", chunkIndex)
+                .add("totalChunks", totalChunks)
+                .add("lastChunkHashCode", lastChunkHashCode)
+                .add("nextChunkHashCode", nextChunkHashCode)
+                .add("snapshotSize", snapshotSize)
+                .add("chunkTimer", chunkTimer)
+                .toString();
+    }
 }