Measure follower activity in nanoseconds
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / LeaderInstallSnapshotState.java
index 5d47dbd02ec9754e94e5bf0f3c81e1a769caf6ba..946c56bec091e6dba8c490a1c9ede601e8d7e532 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.controller.cluster.raft.behaviors;
 
-import com.google.common.base.Throwables;
 import com.google.common.io.ByteSource;
 import java.io.IOException;
 import java.io.InputStream;
@@ -45,12 +44,12 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
     private long snapshotSize;
     private InputStream snapshotInputStream;
 
-    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;
         }
@@ -94,14 +93,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;
@@ -127,7 +127,7 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
         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));
+                    "The # of bytes read from the input stream, %d, does not match the expected # %d", numRead, size));
         }
 
         nextChunkHashCode = Arrays.hashCode(nextChunk);
@@ -152,7 +152,7 @@ public final class LeaderInstallSnapshotState implements AutoCloseable {
         try {
             snapshotInputStream = snapshotBytes.openStream();
         } catch (IOException e) {
-            throw Throwables.propagate(e);
+            throw new RuntimeException(e);
         }
     }