X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Fbehaviors%2FSnapshotTracker.java;h=9e920bd41e9cf141efec6612b08c41c091e63059;hb=d610d46f30872ebdea65686d0ef8535ac251f582;hp=77c9cb5783f4dd59c57246492d02071386107351;hpb=5fd4213b5bfaf2db21f1b37139f6b98535a872c0;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/SnapshotTracker.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/SnapshotTracker.java index 77c9cb5783..9e920bd41e 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/SnapshotTracker.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/SnapshotTracker.java @@ -5,16 +5,17 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.controller.cluster.raft.behaviors; -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; +import static java.util.Objects.requireNonNull; + 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 java.util.OptionalInt; +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(final Logger log, final int totalChunks, final String leaderId, final RaftActorContext context) { this.log = log; this.totalChunks = totalChunks; - this.leaderId = Preconditions.checkNotNull(leaderId); - - backingStream = new ByteArrayOutputStream(); - countingStream = new CountingOutputStream(backingStream); + this.leaderId = requireNonNull(leaderId); + fileBackedStream = context.getFileBackedOutputStreamFactory().newInstance(); + bufferedStream = new BufferedOutputStream(fileBackedStream); } /** @@ -47,11 +48,12 @@ class SnapshotTracker implements AutoCloseable { * @param lastChunkHashCode the optional hash code for the chunk * @return true if this is the last chunk is received * @throws InvalidChunkException if the chunk index is invalid or out of order + * @throws IOException if there is a problem writing to the stream */ - boolean addChunk(int chunkIndex, byte[] chunk, Optional maybeLastChunkHashCode) - throws InvalidChunkException, IOException { + boolean addChunk(final int chunkIndex, final byte[] chunk, final OptionalInt maybeLastChunkHashCode) + throws IOException { log.debug("addChunk: chunkIndex={}, lastChunkIndex={}, collectedChunks.size={}, lastChunkHashCode={}", - chunkIndex, lastChunkIndex, countingStream.getCount(), this.lastChunkHashCode); + chunkIndex, lastChunkIndex, count, lastChunkHashCode); if (sealed) { throw new InvalidChunkException("Invalid chunk received with chunkIndex " + chunkIndex @@ -62,26 +64,28 @@ class SnapshotTracker implements AutoCloseable { throw new InvalidChunkException("Expected chunkIndex " + (lastChunkIndex + 1) + " got " + chunkIndex); } - if (maybeLastChunkHashCode.isPresent() && maybeLastChunkHashCode.get() != this.lastChunkHashCode) { + if (maybeLastChunkHashCode.isPresent() && maybeLastChunkHashCode.getAsInt() != lastChunkHashCode) { throw new InvalidChunkException("The hash code of the recorded last chunk does not match " - + "the senders hash code, expected " + this.lastChunkHashCode + " was " - + maybeLastChunkHashCode.get()); + + "the senders hash code, expected " + lastChunkHashCode + " was " + + maybeLastChunkHashCode.getAsInt()); } - countingStream.write(chunk); + bufferedStream.write(chunk); + count += chunk.length; sealed = chunkIndex == totalChunks; lastChunkIndex = chunkIndex; - this.lastChunkHashCode = Arrays.hashCode(chunk); + 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,17 +94,13 @@ 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 { private static final long serialVersionUID = 1L; - InvalidChunkException(String message) { + InvalidChunkException(final String message) { super(message); } }