Bug 6540: Fix journal issues on leader changes
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / LeaderInstallSnapshotState.java
index 81c3eff45180b70b6c5001563bfba394dbc87c4e..8000217c9e695fd08830acfbfeaa43bee5b0e718 100644 (file)
@@ -27,26 +27,36 @@ public final class LeaderInstallSnapshotState {
     // 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 int snapshotChunkSize;
-    private final ByteString snapshotBytes;
+    private final int snapshotChunkSize;
     private final String logName;
+    private ByteString snapshotBytes;
     private int offset = 0;
     // the next snapshot chunk is sent only if the replyReceivedForOffset matches offset
-    private int replyReceivedForOffset;
+    private int replyReceivedForOffset = -1;
     // if replyStatus is false, the previous chunk is attempted
     private boolean replyStatus = false;
-    private int chunkIndex;
-    private final int totalChunks;
+    private int chunkIndex = FIRST_CHUNK_INDEX;
+    private int totalChunks;
     private int lastChunkHashCode = INITIAL_LAST_CHUNK_HASH_CODE;
     private int nextChunkHashCode = INITIAL_LAST_CHUNK_HASH_CODE;
 
-    LeaderInstallSnapshotState(ByteString snapshotBytes, int snapshotChunkSize, String logName) {
+    LeaderInstallSnapshotState(int snapshotChunkSize, String logName) {
         this.snapshotChunkSize = snapshotChunkSize;
-        this.snapshotBytes = snapshotBytes;
         this.logName = logName;
+    }
+
+    ByteString getSnapshotBytes() {
+        return snapshotBytes;
+    }
+
+    void setSnapshotBytes(ByteString snapshotBytes) {
+        if(this.snapshotBytes != null) {
+            return;
+        }
+
+        this.snapshotBytes = snapshotBytes;
         int size = snapshotBytes.size();
-        totalChunks = size / snapshotChunkSize +
-                (size % snapshotChunkSize > 0 ? 1 : 0);
+        totalChunks = (size / snapshotChunkSize) + (size % snapshotChunkSize > 0 ? 1 : 0);
 
         LOG.debug("{}: Snapshot {} bytes, total chunks to send: {}", logName, size, totalChunks);
 
@@ -54,10 +64,6 @@ public final class LeaderInstallSnapshotState {
         chunkIndex = FIRST_CHUNK_INDEX;
     }
 
-    ByteString getSnapshotBytes() {
-        return snapshotBytes;
-    }
-
     int incrementOffset() {
         if(replyStatus) {
             // if prev chunk failed, we would want to sent the same chunk again
@@ -84,7 +90,7 @@ public final class LeaderInstallSnapshotState {
 
     boolean canSendNextChunk() {
         // we only send a false if a chunk is sent but we have not received a reply yet
-        return replyReceivedForOffset == offset;
+        return snapshotBytes != null && replyReceivedForOffset == offset;
     }
 
     boolean isLastChunk(int index) {
@@ -110,7 +116,7 @@ public final class LeaderInstallSnapshotState {
         int size = snapshotChunkSize;
         if (snapshotChunkSize > snapshotLength) {
             size = snapshotLength;
-        } else if (start + snapshotChunkSize > snapshotLength) {
+        } else if ((start + snapshotChunkSize) > snapshotLength) {
             size = snapshotLength - start;
         }