2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.raft.behaviors;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.protobuf.ByteString;
14 import java.util.Arrays;
15 import org.slf4j.Logger;
18 * Helper class that maintains state for a snapshot that is being installed in chunks on a Follower.
20 public class SnapshotTracker {
21 private final Logger log;
22 private final int totalChunks;
23 private final String leaderId;
24 private ByteString collectedChunks = ByteString.EMPTY;
25 private int lastChunkIndex = LeaderInstallSnapshotState.FIRST_CHUNK_INDEX - 1;
26 private boolean sealed = false;
27 private int lastChunkHashCode = LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE;
29 SnapshotTracker(Logger log, int totalChunks, String leaderId) {
31 this.totalChunks = totalChunks;
32 this.leaderId = Preconditions.checkNotNull(leaderId);
36 * Adds a chunk to the tracker.
38 * @param chunkIndex the index of the chunk
39 * @param chunk the chunk data
40 * @param lastChunkHashCode the optional hash code for the chunk
41 * @return true if this is the last chunk is received
42 * @throws InvalidChunkException if the chunk index is invalid or out of order
44 boolean addChunk(int chunkIndex, byte[] chunk, Optional<Integer> maybeLastChunkHashCode)
45 throws InvalidChunkException {
46 log.debug("addChunk: chunkIndex={}, lastChunkIndex={}, collectedChunks.size={}, lastChunkHashCode={}",
47 chunkIndex, lastChunkIndex, collectedChunks.size(), this.lastChunkHashCode);
50 throw new InvalidChunkException("Invalid chunk received with chunkIndex " + chunkIndex
51 + " all chunks already received");
54 if (lastChunkIndex + 1 != chunkIndex) {
55 throw new InvalidChunkException("Expected chunkIndex " + (lastChunkIndex + 1) + " got " + chunkIndex);
58 if (maybeLastChunkHashCode.isPresent() && maybeLastChunkHashCode.get() != this.lastChunkHashCode) {
59 throw new InvalidChunkException("The hash code of the recorded last chunk does not match "
60 + "the senders hash code, expected " + this.lastChunkHashCode + " was "
61 + maybeLastChunkHashCode.get());
64 sealed = chunkIndex == totalChunks;
65 lastChunkIndex = chunkIndex;
66 collectedChunks = collectedChunks.concat(ByteString.copyFrom(chunk));
67 this.lastChunkHashCode = Arrays.hashCode(chunk);
71 byte[] getSnapshot() {
73 throw new IllegalStateException("lastChunk not received yet");
76 return collectedChunks.toByteArray();
79 ByteString getCollectedChunks() {
80 return collectedChunks;
83 String getLeaderId() {
87 public static class InvalidChunkException extends Exception {
88 private static final long serialVersionUID = 1L;
90 InvalidChunkException(String message) {