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.common.io.ByteSource;
14 import java.io.BufferedOutputStream;
15 import java.io.IOException;
16 import java.util.Arrays;
17 import org.opendaylight.controller.cluster.io.FileBackedOutputStream;
18 import org.opendaylight.controller.cluster.raft.RaftActorContext;
19 import org.slf4j.Logger;
22 * Helper class that maintains state for a snapshot that is being installed in chunks on a Follower.
24 class SnapshotTracker implements AutoCloseable {
25 private final Logger log;
26 private final int totalChunks;
27 private final String leaderId;
28 private final BufferedOutputStream bufferedStream;
29 private final FileBackedOutputStream fileBackedStream;
30 private int lastChunkIndex = LeaderInstallSnapshotState.FIRST_CHUNK_INDEX - 1;
31 private boolean sealed = false;
32 private int lastChunkHashCode = LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE;
35 SnapshotTracker(Logger log, int totalChunks, String leaderId, RaftActorContext context) {
37 this.totalChunks = totalChunks;
38 this.leaderId = Preconditions.checkNotNull(leaderId);
39 fileBackedStream = context.newFileBackedOutputStream();
40 bufferedStream = new BufferedOutputStream(fileBackedStream);
44 * Adds a chunk to the tracker.
46 * @param chunkIndex the index of the chunk
47 * @param chunk the chunk data
48 * @param lastChunkHashCode the optional hash code for the chunk
49 * @return true if this is the last chunk is received
50 * @throws InvalidChunkException if the chunk index is invalid or out of order
52 boolean addChunk(int chunkIndex, byte[] chunk, Optional<Integer> maybeLastChunkHashCode)
53 throws InvalidChunkException, IOException {
54 log.debug("addChunk: chunkIndex={}, lastChunkIndex={}, collectedChunks.size={}, lastChunkHashCode={}",
55 chunkIndex, lastChunkIndex, count, this.lastChunkHashCode);
58 throw new InvalidChunkException("Invalid chunk received with chunkIndex " + chunkIndex
59 + " all chunks already received");
62 if (lastChunkIndex + 1 != chunkIndex) {
63 throw new InvalidChunkException("Expected chunkIndex " + (lastChunkIndex + 1) + " got " + chunkIndex);
66 if (maybeLastChunkHashCode.isPresent() && maybeLastChunkHashCode.get() != this.lastChunkHashCode) {
67 throw new InvalidChunkException("The hash code of the recorded last chunk does not match "
68 + "the senders hash code, expected " + this.lastChunkHashCode + " was "
69 + maybeLastChunkHashCode.get());
72 bufferedStream.write(chunk);
74 count += chunk.length;
75 sealed = chunkIndex == totalChunks;
76 lastChunkIndex = chunkIndex;
77 this.lastChunkHashCode = Arrays.hashCode(chunk);
81 ByteSource getSnapshotBytes() throws IOException {
83 throw new IllegalStateException("lastChunk not received yet");
86 bufferedStream.close();
87 return fileBackedStream.asByteSource();
90 String getLeaderId() {
96 fileBackedStream.cleanup();
99 public static class InvalidChunkException extends IOException {
100 private static final long serialVersionUID = 1L;
102 InvalidChunkException(String message) {