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.messages;
11 import com.google.common.base.Optional;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
20 * Message sent from a leader to install a snapshot chunk on a follower.
22 public class InstallSnapshot extends AbstractRaftRPC {
23 private static final long serialVersionUID = 1L;
25 private final String leaderId;
26 private final long lastIncludedIndex;
27 private final long lastIncludedTerm;
28 private final byte[] data;
29 private final int chunkIndex;
30 private final int totalChunks;
31 private final Optional<Integer> lastChunkHashCode;
32 private final Optional<ServerConfigurationPayload> serverConfig;
34 @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "Stores a reference to an externally mutable byte[] "
35 + "object but this is OK since this class is merely a DTO and does not process byte[] internally. "
36 + "Also it would be inefficient to create a copy as the byte[] could be large.")
37 public InstallSnapshot(long term, String leaderId, long lastIncludedIndex, long lastIncludedTerm, byte[] data,
38 int chunkIndex, int totalChunks, Optional<Integer> lastChunkHashCode,
39 Optional<ServerConfigurationPayload> serverConfig) {
41 this.leaderId = leaderId;
42 this.lastIncludedIndex = lastIncludedIndex;
43 this.lastIncludedTerm = lastIncludedTerm;
45 this.chunkIndex = chunkIndex;
46 this.totalChunks = totalChunks;
47 this.lastChunkHashCode = lastChunkHashCode;
48 this.serverConfig = serverConfig;
51 public InstallSnapshot(long term, String leaderId, long lastIncludedIndex,
52 long lastIncludedTerm, byte[] data, int chunkIndex, int totalChunks) {
53 this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks,
54 Optional.<Integer>absent(), Optional.<ServerConfigurationPayload>absent());
57 public String getLeaderId() {
61 public long getLastIncludedIndex() {
62 return lastIncludedIndex;
65 public long getLastIncludedTerm() {
66 return lastIncludedTerm;
69 @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
70 + "this is OK since this class is merely a DTO and does not process the byte[] internally. "
71 + "Also it would be inefficient to create a return copy as the byte[] could be large.")
72 public byte[] getData() {
76 public int getChunkIndex() {
80 public int getTotalChunks() {
84 public Optional<Integer> getLastChunkHashCode() {
85 return lastChunkHashCode;
88 public Optional<ServerConfigurationPayload> getServerConfig() {
93 public <T> Object toSerializable(short version) {
98 public String toString() {
99 return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
100 + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
101 + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
102 + ", serverConfig=" + serverConfig.orNull() + "]";
105 private Object writeReplace() {
106 return new Proxy(this);
109 private static class Proxy implements Externalizable {
110 private static final long serialVersionUID = 1L;
112 private InstallSnapshot installSnapshot;
114 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
115 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
116 @SuppressWarnings("checkstyle:RedundantModifier")
120 Proxy(InstallSnapshot installSnapshot) {
121 this.installSnapshot = installSnapshot;
125 public void writeExternal(ObjectOutput out) throws IOException {
126 out.writeLong(installSnapshot.getTerm());
127 out.writeObject(installSnapshot.leaderId);
128 out.writeLong(installSnapshot.lastIncludedIndex);
129 out.writeLong(installSnapshot.lastIncludedTerm);
130 out.writeInt(installSnapshot.chunkIndex);
131 out.writeInt(installSnapshot.totalChunks);
133 out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0);
134 if (installSnapshot.lastChunkHashCode.isPresent()) {
135 out.writeInt(installSnapshot.lastChunkHashCode.get().intValue());
138 out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0);
139 if (installSnapshot.serverConfig.isPresent()) {
140 out.writeObject(installSnapshot.serverConfig.get());
143 out.writeObject(installSnapshot.data);
147 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
148 long term = in.readLong();
149 String leaderId = (String) in.readObject();
150 long lastIncludedIndex = in.readLong();
151 long lastIncludedTerm = in.readLong();
152 int chunkIndex = in.readInt();
153 int totalChunks = in.readInt();
155 Optional<Integer> lastChunkHashCode = Optional.absent();
156 boolean chunkHashCodePresent = in.readByte() == 1;
157 if (chunkHashCodePresent) {
158 lastChunkHashCode = Optional.of(in.readInt());
161 Optional<ServerConfigurationPayload> serverConfig = Optional.absent();
162 boolean serverConfigPresent = in.readByte() == 1;
163 if (serverConfigPresent) {
164 serverConfig = Optional.of((ServerConfigurationPayload)in.readObject());
167 byte[] data = (byte[])in.readObject();
169 installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
170 chunkIndex, totalChunks, lastChunkHashCode, serverConfig);
173 private Object readResolve() {
174 return installSnapshot;