Fix issue when AE leader differs from prior install snapshot leader
[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.common.base.Preconditions;
13 import com.google.protobuf.ByteString;
14 import java.util.Arrays;
15 import org.slf4j.Logger;
16
17 /**
18  * SnapshotTracker does house keeping for a snapshot that is being installed in chunks on the Follower
19  */
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 = AbstractLeader.FIRST_CHUNK_INDEX - 1;
26     private boolean sealed = false;
27     private int lastChunkHashCode = AbstractLeader.INITIAL_LAST_CHUNK_HASH_CODE;
28
29     SnapshotTracker(Logger LOG, int totalChunks, String leaderId) {
30         this.LOG = LOG;
31         this.totalChunks = totalChunks;
32         this.leaderId = Preconditions.checkNotNull(leaderId);
33     }
34
35     /**
36      * Adds a chunk to the tracker
37      *
38      * @param chunkIndex
39      * @param chunk
40      * @return true when the lastChunk is received
41      * @throws InvalidChunkException
42      */
43     boolean addChunk(int chunkIndex, byte[] chunk, Optional<Integer> lastChunkHashCode) throws InvalidChunkException{
44         LOG.debug("addChunk: chunkIndex={}, lastChunkIndex={}, collectedChunks.size={}, lastChunkHashCode={}",
45                 chunkIndex, lastChunkIndex, collectedChunks.size(), this.lastChunkHashCode);
46
47         if(sealed){
48             throw new InvalidChunkException("Invalid chunk received with chunkIndex " + chunkIndex + " all chunks already received");
49         }
50
51         if(lastChunkIndex + 1 != chunkIndex){
52             throw new InvalidChunkException("Expected chunkIndex " + (lastChunkIndex + 1) + " got " + chunkIndex);
53         }
54
55         if(lastChunkHashCode.isPresent()){
56             if(lastChunkHashCode.get() != this.lastChunkHashCode){
57                 throw new InvalidChunkException("The hash code of the recorded last chunk does not match " +
58                         "the senders hash code, expected " + this.lastChunkHashCode + " was " + lastChunkHashCode.get());
59             }
60         }
61
62         sealed = chunkIndex == totalChunks;
63         lastChunkIndex = chunkIndex;
64         collectedChunks = collectedChunks.concat(ByteString.copyFrom(chunk));
65         this.lastChunkHashCode = Arrays.hashCode(chunk);
66         return sealed;
67     }
68
69     byte[] getSnapshot(){
70         if(!sealed) {
71             throw new IllegalStateException("lastChunk not received yet");
72         }
73
74         return collectedChunks.toByteArray();
75     }
76
77     ByteString getCollectedChunks(){
78         return collectedChunks;
79     }
80
81     String getLeaderId() {
82         return leaderId;
83     }
84
85     public static class InvalidChunkException extends Exception {
86         private static final long serialVersionUID = 1L;
87
88         InvalidChunkException(String message){
89             super(message);
90         }
91     }
92
93 }