Send commitIndex updates to followers as soon as possible
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / LeaderInstallSnapshotState.java
index 5d47dbd02ec9754e94e5bf0f3c81e1a769caf6ba..23a0f6d027c53841644a6ab1c5273708a9e0a011 100644 (file)
@@ -7,13 +7,15 @@
  */
 package org.opendaylight.controller.cluster.raft.behaviors;
 
-import com.google.common.base.Throwables;
+import com.google.common.base.Stopwatch;
 import com.google.common.io.ByteSource;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
+import java.util.concurrent.TimeUnit;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import scala.concurrent.duration.FiniteDuration;
 
 /**
  * Encapsulates the leader state and logic for sending snapshot chunks to a follower.
@@ -44,13 +46,15 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
     private int nextChunkHashCode = INITIAL_LAST_CHUNK_HASH_CODE;
     private long snapshotSize;
     private InputStream snapshotInputStream;
+    private Stopwatch chunkTimer = Stopwatch.createUnstarted();
+    private byte[] currentChunk = null;
 
-    LeaderInstallSnapshotState(int snapshotChunkSize, String logName) {
+    LeaderInstallSnapshotState(final int snapshotChunkSize, final String logName) {
         this.snapshotChunkSize = snapshotChunkSize;
         this.logName = logName;
     }
 
-    void setSnapshotBytes(ByteSource snapshotBytes) throws IOException {
+    void setSnapshotBytes(final ByteSource snapshotBytes) throws IOException {
         if (this.snapshotBytes != null) {
             return;
         }
@@ -84,6 +88,18 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
         return chunkIndex;
     }
 
+    void startChunkTimer() {
+        chunkTimer.start();
+    }
+
+    void resetChunkTimer() {
+        chunkTimer.reset();
+    }
+
+    boolean isChunkTimedOut(final FiniteDuration timeout) {
+        return chunkTimer.elapsed(TimeUnit.SECONDS) > timeout.toSeconds();
+    }
+
     int getChunkIndex() {
         return chunkIndex;
     }
@@ -94,14 +110,15 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
 
     boolean canSendNextChunk() {
         // we only send a false if a chunk is sent but we have not received a reply yet
-        return snapshotBytes != null && replyReceivedForOffset == offset;
+        return snapshotBytes != null && (nextChunkHashCode == INITIAL_LAST_CHUNK_HASH_CODE
+                || replyReceivedForOffset == offset);
     }
 
-    boolean isLastChunk(int index) {
+    boolean isLastChunk(final int index) {
         return totalChunks == index;
     }
 
-    void markSendStatus(boolean success) {
+    void markSendStatus(final boolean success) {
         if (success) {
             // if the chunk sent was successful
             replyReceivedForOffset = offset;
@@ -115,26 +132,30 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
     }
 
     byte[] getNextChunk() throws IOException {
-        int start = incrementOffset();
-        int size = snapshotChunkSize;
-        if (snapshotChunkSize > snapshotSize) {
-            size = (int) snapshotSize;
-        } else if (start + snapshotChunkSize > snapshotSize) {
-            size = (int) (snapshotSize - start);
-        }
+        if (replyStatus || currentChunk == null) {
+            int start = incrementOffset();
+            int size = snapshotChunkSize;
+            if (snapshotChunkSize > snapshotSize) {
+                size = (int) snapshotSize;
+            } else if (start + snapshotChunkSize > snapshotSize) {
+                size = (int) (snapshotSize - start);
+            }
 
-        byte[] nextChunk = new byte[size];
-        int numRead = snapshotInputStream.read(nextChunk);
-        if (numRead != size) {
-            throw new IOException(String.format(
-                    "The # of bytes read from the imput stream, %d, does not match the expected # %d", numRead, size));
-        }
+            currentChunk = new byte[size];
+            int numRead = snapshotInputStream.read(currentChunk);
+            if (numRead != size) {
+                throw new IOException(String.format(
+                        "The # of bytes read from the input stream, %d,"
+                                + "does not match the expected # %d", numRead, size));
+            }
+
+            nextChunkHashCode = Arrays.hashCode(currentChunk);
 
-        nextChunkHashCode = Arrays.hashCode(nextChunk);
+            LOG.debug("{}: Next chunk: total length={}, offset={}, size={}, hashCode={}", logName,
+                    snapshotSize, start, size, nextChunkHashCode);
+        }
 
-        LOG.debug("{}: Next chunk: total length={}, offset={}, size={}, hashCode={}", logName,
-                snapshotSize, start, size, nextChunkHashCode);
-        return nextChunk;
+        return currentChunk;
     }
 
     /**
@@ -142,17 +163,19 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
      */
     void reset() {
         closeStream();
+        chunkTimer.reset();
 
         offset = 0;
         replyStatus = false;
         replyReceivedForOffset = offset;
         chunkIndex = FIRST_CHUNK_INDEX;
+        currentChunk = null;
         lastChunkHashCode = INITIAL_LAST_CHUNK_HASH_CODE;
 
         try {
             snapshotInputStream = snapshotBytes.openStream();
         } catch (IOException e) {
-            throw Throwables.propagate(e);
+            throw new RuntimeException(e);
         }
     }