Bug 7521: Add FileBackedOutputStream and use for snapshot chunking
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / SnapshotTracker.java
index 77c9cb5783f4dd59c57246492d02071386107351..6b7e037a339783fb9f2f163648b6cee2f72f1790 100644 (file)
@@ -11,10 +11,11 @@ package org.opendaylight.controller.cluster.raft.behaviors;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.io.ByteSource;
-import com.google.common.io.CountingOutputStream;
-import java.io.ByteArrayOutputStream;
+import java.io.BufferedOutputStream;
 import java.io.IOException;
 import java.util.Arrays;
+import org.opendaylight.controller.cluster.io.FileBackedOutputStream;
+import org.opendaylight.controller.cluster.raft.RaftActorContext;
 import org.slf4j.Logger;
 
 /**
@@ -24,19 +25,19 @@ class SnapshotTracker implements AutoCloseable {
     private final Logger log;
     private final int totalChunks;
     private final String leaderId;
-    private final CountingOutputStream countingStream;
-    private final ByteArrayOutputStream backingStream;
+    private final BufferedOutputStream bufferedStream;
+    private final FileBackedOutputStream fileBackedStream;
     private int lastChunkIndex = LeaderInstallSnapshotState.FIRST_CHUNK_INDEX - 1;
     private boolean sealed = false;
     private int lastChunkHashCode = LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE;
+    private long count;
 
-    SnapshotTracker(Logger log, int totalChunks, String leaderId) {
+    SnapshotTracker(Logger log, int totalChunks, String leaderId, RaftActorContext context) {
         this.log = log;
         this.totalChunks = totalChunks;
         this.leaderId = Preconditions.checkNotNull(leaderId);
-
-        backingStream = new ByteArrayOutputStream();
-        countingStream = new CountingOutputStream(backingStream);
+        fileBackedStream = context.newFileBackedOutputStream();
+        bufferedStream = new BufferedOutputStream(fileBackedStream);
     }
 
     /**
@@ -51,7 +52,7 @@ class SnapshotTracker implements AutoCloseable {
     boolean addChunk(int chunkIndex, byte[] chunk, Optional<Integer> maybeLastChunkHashCode)
             throws InvalidChunkException, IOException {
         log.debug("addChunk: chunkIndex={}, lastChunkIndex={}, collectedChunks.size={}, lastChunkHashCode={}",
-                chunkIndex, lastChunkIndex, countingStream.getCount(), this.lastChunkHashCode);
+                chunkIndex, lastChunkIndex, count, this.lastChunkHashCode);
 
         if (sealed) {
             throw new InvalidChunkException("Invalid chunk received with chunkIndex " + chunkIndex
@@ -68,20 +69,22 @@ class SnapshotTracker implements AutoCloseable {
                     + maybeLastChunkHashCode.get());
         }
 
-        countingStream.write(chunk);
+        bufferedStream.write(chunk);
 
+        count += chunk.length;
         sealed = chunkIndex == totalChunks;
         lastChunkIndex = chunkIndex;
         this.lastChunkHashCode = Arrays.hashCode(chunk);
         return sealed;
     }
 
-    ByteSource getSnapshotBytes() {
+    ByteSource getSnapshotBytes() throws IOException {
         if (!sealed) {
             throw new IllegalStateException("lastChunk not received yet");
         }
 
-        return ByteSource.wrap(backingStream.toByteArray());
+        bufferedStream.close();
+        return fileBackedStream.asByteSource();
     }
 
     String getLeaderId() {
@@ -90,11 +93,7 @@ class SnapshotTracker implements AutoCloseable {
 
     @Override
     public void close() {
-        try {
-            countingStream.close();
-        } catch (IOException e) {
-            log.warn("Error closing snapshot stream");
-        }
+        fileBackedStream.cleanup();
     }
 
     public static class InvalidChunkException extends IOException {