bb7a24248188f0a83b9225a32a9b2a7e3e16f250
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / SnapshotTracker.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.raft.behaviors;
10
11 import com.google.common.base.Optional;
12 import com.google.protobuf.ByteString;
13 import java.util.Arrays;
14 import org.slf4j.Logger;
15
16 /**
17  * SnapshotTracker does house keeping for a snapshot that is being installed in chunks on the Follower
18  */
19 public class SnapshotTracker {
20     private final Logger LOG;
21     private final int totalChunks;
22     private ByteString collectedChunks = ByteString.EMPTY;
23     private int lastChunkIndex = AbstractLeader.FIRST_CHUNK_INDEX - 1;
24     private boolean sealed = false;
25     private int lastChunkHashCode = AbstractLeader.INITIAL_LAST_CHUNK_HASH_CODE;
26
27     SnapshotTracker(Logger LOG, int totalChunks){
28         this.LOG = LOG;
29         this.totalChunks = totalChunks;
30     }
31
32     /**
33      * Adds a chunk to the tracker
34      *
35      * @param chunkIndex
36      * @param chunk
37      * @return true when the lastChunk is received
38      * @throws InvalidChunkException
39      */
40     boolean addChunk(int chunkIndex, byte[] chunk, Optional<Integer> lastChunkHashCode) throws InvalidChunkException{
41         LOG.debug("addChunk: chunkIndex={}, lastChunkIndex={}, collectedChunks.size={}, lastChunkHashCode={}",
42                 chunkIndex, lastChunkIndex, collectedChunks.size(), this.lastChunkHashCode);
43
44         if(sealed){
45             throw new InvalidChunkException("Invalid chunk received with chunkIndex " + chunkIndex + " all chunks already received");
46         }
47
48         if(lastChunkIndex + 1 != chunkIndex){
49             throw new InvalidChunkException("Expected chunkIndex " + (lastChunkIndex + 1) + " got " + chunkIndex);
50         }
51
52         if(lastChunkHashCode.isPresent()){
53             if(lastChunkHashCode.get() != this.lastChunkHashCode){
54                 throw new InvalidChunkException("The hash code of the recorded last chunk does not match " +
55                         "the senders hash code, expected " + this.lastChunkHashCode + " was " + lastChunkHashCode.get());
56             }
57         }
58
59         sealed = (chunkIndex == totalChunks);
60         lastChunkIndex = chunkIndex;
61         collectedChunks = collectedChunks.concat(ByteString.copyFrom(chunk));
62         this.lastChunkHashCode = Arrays.hashCode(chunk);
63         return sealed;
64     }
65
66     byte[] getSnapshot(){
67         if(!sealed) {
68             throw new IllegalStateException("lastChunk not received yet");
69         }
70
71         return collectedChunks.toByteArray();
72     }
73
74     ByteString getCollectedChunks(){
75         return collectedChunks;
76     }
77
78     public static class InvalidChunkException extends Exception {
79         private static final long serialVersionUID = 1L;
80
81         InvalidChunkException(String message){
82             super(message);
83         }
84     }
85
86 }