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=fadca3b15d9a174e8b447b963df8bd06e2fe9cde;hp=26fbde07117e38e8b06096108b471401a503f426;hb=d3e310b940b60f6590f0e94a576aece95a055942;hpb=3e3f54ed3ff69a24f663cf2d3b10e61c49c58ccd 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 26fbde0711..fadca3b15d 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 @@ -8,24 +8,28 @@ package org.opendaylight.controller.cluster.raft.behaviors; -import akka.event.LoggingAdapter; import com.google.common.base.Optional; +import com.google.common.base.Preconditions; import com.google.protobuf.ByteString; +import java.util.Arrays; +import org.slf4j.Logger; /** * SnapshotTracker does house keeping for a snapshot that is being installed in chunks on the Follower */ public class SnapshotTracker { - private final LoggingAdapter LOG; + private final Logger LOG; private final int totalChunks; + private final String leaderId; private ByteString collectedChunks = ByteString.EMPTY; - private int lastChunkIndex = AbstractLeader.FIRST_CHUNK_INDEX - 1; + private int lastChunkIndex = LeaderInstallSnapshotState.FIRST_CHUNK_INDEX - 1; private boolean sealed = false; - private int lastChunkHashCode = AbstractLeader.INITIAL_LAST_CHUNK_HASH_CODE; + private int lastChunkHashCode = LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE; - SnapshotTracker(LoggingAdapter LOG, int totalChunks){ + SnapshotTracker(Logger LOG, int totalChunks, String leaderId) { this.LOG = LOG; this.totalChunks = totalChunks; + this.leaderId = Preconditions.checkNotNull(leaderId); } /** @@ -36,7 +40,10 @@ public class SnapshotTracker { * @return true when the lastChunk is received * @throws InvalidChunkException */ - boolean addChunk(int chunkIndex, ByteString chunk, Optional lastChunkHashCode) throws InvalidChunkException{ + boolean addChunk(int chunkIndex, byte[] chunk, Optional lastChunkHashCode) 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"); } @@ -48,19 +55,14 @@ public class SnapshotTracker { 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 " + lastChunkHashCode + " was " + lastChunkHashCode.get()); + "the senders hash code, expected " + this.lastChunkHashCode + " was " + lastChunkHashCode.get()); } } - if(LOG.isDebugEnabled()) { - LOG.debug("Chunk={},collectedChunks.size:{}", - chunkIndex, collectedChunks.size()); - } - - sealed = (chunkIndex == totalChunks); + sealed = chunkIndex == totalChunks; lastChunkIndex = chunkIndex; - collectedChunks = collectedChunks.concat(chunk); - this.lastChunkHashCode = chunk.hashCode(); + collectedChunks = collectedChunks.concat(ByteString.copyFrom(chunk)); + this.lastChunkHashCode = Arrays.hashCode(chunk); return sealed; } @@ -76,7 +78,13 @@ public class SnapshotTracker { return collectedChunks; } + String getLeaderId() { + return leaderId; + } + public static class InvalidChunkException extends Exception { + private static final long serialVersionUID = 1L; + InvalidChunkException(String message){ super(message); }