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
8 package org.opendaylight.controller.cluster.raft.messages;
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.io.Externalizable;
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import java.util.Optional;
16 import java.util.OptionalInt;
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 final 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 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()")
32 private final OptionalInt lastChunkHashCode;
33 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()")
34 private final Optional<ServerConfigurationPayload> serverConfig;
36 @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "Stores a reference to an externally mutable byte[] "
37 + "object but this is OK since this class is merely a DTO and does not process byte[] internally. "
38 + "Also it would be inefficient to create a copy as the byte[] could be large.")
39 public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
40 final long lastIncludedTerm, final byte[] data, final int chunkIndex, final int totalChunks,
41 final OptionalInt lastChunkHashCode, final Optional<ServerConfigurationPayload> serverConfig) {
43 this.leaderId = leaderId;
44 this.lastIncludedIndex = lastIncludedIndex;
45 this.lastIncludedTerm = lastIncludedTerm;
47 this.chunkIndex = chunkIndex;
48 this.totalChunks = totalChunks;
49 this.lastChunkHashCode = lastChunkHashCode;
50 this.serverConfig = serverConfig;
53 public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
54 final long lastIncludedTerm, final byte[] data, final int chunkIndex,
55 final int totalChunks) {
56 this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, OptionalInt.empty(),
60 public String getLeaderId() {
64 public long getLastIncludedIndex() {
65 return lastIncludedIndex;
68 public long getLastIncludedTerm() {
69 return lastIncludedTerm;
72 @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
73 + "this is OK since this class is merely a DTO and does not process the byte[] internally. "
74 + "Also it would be inefficient to create a return copy as the byte[] could be large.")
75 public byte[] getData() {
79 public int getChunkIndex() {
83 public int getTotalChunks() {
87 public OptionalInt getLastChunkHashCode() {
88 return lastChunkHashCode;
91 public Optional<ServerConfigurationPayload> getServerConfig() {
95 public <T> Object toSerializable(final short version) {
100 public String toString() {
101 return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
102 + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
103 + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
104 + ", serverConfig=" + serverConfig.orElse(null) + "]";
108 Object writeReplace() {
109 return new Proxy(this);
112 private static class Proxy implements Externalizable {
113 private static final long serialVersionUID = 1L;
115 private InstallSnapshot installSnapshot;
117 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
118 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
119 @SuppressWarnings("checkstyle:RedundantModifier")
123 Proxy(final InstallSnapshot installSnapshot) {
124 this.installSnapshot = installSnapshot;
128 public void writeExternal(final ObjectOutput out) throws IOException {
129 out.writeLong(installSnapshot.getTerm());
130 out.writeObject(installSnapshot.leaderId);
131 out.writeLong(installSnapshot.lastIncludedIndex);
132 out.writeLong(installSnapshot.lastIncludedTerm);
133 out.writeInt(installSnapshot.chunkIndex);
134 out.writeInt(installSnapshot.totalChunks);
136 out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0);
137 if (installSnapshot.lastChunkHashCode.isPresent()) {
138 out.writeInt(installSnapshot.lastChunkHashCode.getAsInt());
141 out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0);
142 if (installSnapshot.serverConfig.isPresent()) {
143 out.writeObject(installSnapshot.serverConfig.get());
146 out.writeObject(installSnapshot.data);
150 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
151 long term = in.readLong();
152 String leaderId = (String) in.readObject();
153 long lastIncludedIndex = in.readLong();
154 long lastIncludedTerm = in.readLong();
155 int chunkIndex = in.readInt();
156 int totalChunks = in.readInt();
158 OptionalInt lastChunkHashCode = in.readByte() == 1 ? OptionalInt.of(in.readInt()) : OptionalInt.empty();
159 Optional<ServerConfigurationPayload> serverConfig = in.readByte() == 1
160 ? Optional.of((ServerConfigurationPayload)in.readObject()) : Optional.empty();
162 byte[] data = (byte[])in.readObject();
164 installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
165 chunkIndex, totalChunks, lastChunkHashCode, serverConfig);
168 private Object readResolve() {
169 return installSnapshot;