Failed chunks should move offset backwards 25/82725/1
authorTomas Cere <tomas.cere@pantheon.tech>
Tue, 25 Jun 2019 08:45:15 +0000 (10:45 +0200)
committerTomas Cere <tomas.cere@pantheon.tech>
Tue, 25 Jun 2019 12:06:19 +0000 (14:06 +0200)
If a chunk has failed it needs to move the offset backwards,
otherwise this would misalign the stream, or make it possible
to call sendNextChunk() right after resend which could be
seen as the chunkTimer attempted to be started while already running.

Change-Id: I6d44195ae9bcb23075ffea9c4f1d1ab1d0540c93
Signed-off-by: Tomas Cere <tomas.cere@pantheon.tech>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/LeaderInstallSnapshotState.java

index 18881ec..7583452 100644 (file)
@@ -74,8 +74,12 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
     }
 
     int incrementOffset() {
+        // if first chunk was retried, reset offset back to initial 0
+        if (offset == -1) {
+            offset = 0;
+        }
         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
             offset = offset + snapshotChunkSize;
         }
         return offset;
@@ -83,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;
@@ -126,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;

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.