X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Fbehaviors%2FSnapshotTracker.java;h=3ba020b8143aa69f4c106a5c53f150a117bb1d70;hp=fadca3b15d9a174e8b447b963df8bd06e2fe9cde;hb=660c3e22ca97bc613ea6f6288503620bba6fb233;hpb=bf5c6f0a0b509b9f366be15433780d8fb8be1b84;ds=sidebyside 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 fadca3b15d..3ba020b814 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 @@ -15,10 +15,10 @@ import java.util.Arrays; import org.slf4j.Logger; /** - * SnapshotTracker does house keeping for a snapshot that is being installed in chunks on the Follower + * Helper class that maintains state for a snapshot that is being installed in chunks on a Follower. */ public class SnapshotTracker { - private final Logger LOG; + private final Logger log; private final int totalChunks; private final String leaderId; private ByteString collectedChunks = ByteString.EMPTY; @@ -26,37 +26,39 @@ public class SnapshotTracker { private boolean sealed = false; private int lastChunkHashCode = LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE; - SnapshotTracker(Logger LOG, int totalChunks, String leaderId) { - this.LOG = LOG; + SnapshotTracker(Logger log, int totalChunks, String leaderId) { + this.log = log; this.totalChunks = totalChunks; this.leaderId = Preconditions.checkNotNull(leaderId); } /** - * Adds a chunk to the tracker + * Adds a chunk to the tracker. * - * @param chunkIndex - * @param chunk - * @return true when the lastChunk is received - * @throws InvalidChunkException + * @param chunkIndex the index of the chunk + * @param chunk the chunk data + * @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 */ - boolean addChunk(int chunkIndex, byte[] chunk, Optional lastChunkHashCode) throws InvalidChunkException{ - LOG.debug("addChunk: chunkIndex={}, lastChunkIndex={}, collectedChunks.size={}, lastChunkHashCode={}", + boolean addChunk(int chunkIndex, byte[] chunk, Optional maybeLastChunkHashCode) + throws InvalidChunkException { + log.debug("addChunk: chunkIndex={}, lastChunkIndex={}, collectedChunks.size={}, lastChunkHashCode={}", chunkIndex, lastChunkIndex, collectedChunks.size(), this.lastChunkHashCode); - if(sealed){ - throw new InvalidChunkException("Invalid chunk received with chunkIndex " + chunkIndex + " all chunks already received"); + if (sealed) { + throw new InvalidChunkException("Invalid chunk received with chunkIndex " + chunkIndex + + " all chunks already received"); } - if(lastChunkIndex + 1 != chunkIndex){ + if (lastChunkIndex + 1 != chunkIndex) { throw new InvalidChunkException("Expected chunkIndex " + (lastChunkIndex + 1) + " got " + chunkIndex); } - if(lastChunkHashCode.isPresent()){ - if(lastChunkHashCode.get() != this.lastChunkHashCode){ - throw new InvalidChunkException("The hash code of the recorded last chunk does not match " + - "the senders hash code, expected " + this.lastChunkHashCode + " was " + lastChunkHashCode.get()); - } + if (maybeLastChunkHashCode.isPresent() && maybeLastChunkHashCode.get() != this.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()); } sealed = chunkIndex == totalChunks; @@ -66,15 +68,15 @@ public class SnapshotTracker { return sealed; } - byte[] getSnapshot(){ - if(!sealed) { + byte[] getSnapshot() { + if (!sealed) { throw new IllegalStateException("lastChunk not received yet"); } return collectedChunks.toByteArray(); } - ByteString getCollectedChunks(){ + ByteString getCollectedChunks() { return collectedChunks; } @@ -85,9 +87,8 @@ public class SnapshotTracker { public static class InvalidChunkException extends Exception { private static final long serialVersionUID = 1L; - InvalidChunkException(String message){ + InvalidChunkException(String message) { super(message); } } - }