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 com.google.common.annotations.VisibleForTesting;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.util.Optional;
17 import java.util.OptionalInt;
18 import org.opendaylight.controller.cluster.raft.RaftVersions;
19 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
22 * Message sent from a leader to install a snapshot chunk on a follower.
24 public final class InstallSnapshot extends AbstractRaftRPC {
26 private static final long serialVersionUID = 1L;
28 private final String leaderId;
29 private final long lastIncludedIndex;
30 private final long lastIncludedTerm;
31 private final byte[] data;
32 private final int chunkIndex;
33 private final int totalChunks;
34 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()")
35 private final OptionalInt lastChunkHashCode;
36 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()")
37 private final Optional<ServerConfigurationPayload> serverConfig;
38 private final short recipientRaftVersion;
40 @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "Stores a reference to an externally mutable byte[] "
41 + "object but this is OK since this class is merely a DTO and does not process byte[] internally. "
42 + "Also it would be inefficient to create a copy as the byte[] could be large.")
43 public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
44 final long lastIncludedTerm, final byte[] data, final int chunkIndex, final int totalChunks,
45 final OptionalInt lastChunkHashCode, final Optional<ServerConfigurationPayload> serverConfig,
46 final short recipientRaftVersion) {
48 this.leaderId = leaderId;
49 this.lastIncludedIndex = lastIncludedIndex;
50 this.lastIncludedTerm = lastIncludedTerm;
52 this.chunkIndex = chunkIndex;
53 this.totalChunks = totalChunks;
54 this.lastChunkHashCode = lastChunkHashCode;
55 this.serverConfig = serverConfig;
56 this.recipientRaftVersion = recipientRaftVersion;
60 public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
61 final long lastIncludedTerm, final byte[] data, final int chunkIndex,
62 final int totalChunks) {
63 this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, OptionalInt.empty(),
64 Optional.empty(), RaftVersions.CURRENT_VERSION);
67 public String getLeaderId() {
71 public long getLastIncludedIndex() {
72 return lastIncludedIndex;
75 public long getLastIncludedTerm() {
76 return lastIncludedTerm;
79 @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
80 + "this is OK since this class is merely a DTO and does not process the byte[] internally. "
81 + "Also it would be inefficient to create a return copy as the byte[] could be large.")
82 public byte[] getData() {
86 public int getChunkIndex() {
90 public int getTotalChunks() {
94 public OptionalInt getLastChunkHashCode() {
95 return lastChunkHashCode;
98 public Optional<ServerConfigurationPayload> getServerConfig() {
103 public String toString() {
104 return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
105 + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
106 + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
107 + ", serverConfig=" + serverConfig.orElse(null) + "]";
111 Object writeReplace() {
112 return recipientRaftVersion <= RaftVersions.FLUORINE_VERSION ? new Proxy(this) : new IS(this);
115 private static class Proxy implements Externalizable {
117 private static final long serialVersionUID = 1L;
119 private InstallSnapshot installSnapshot;
121 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
122 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
123 @SuppressWarnings("checkstyle:RedundantModifier")
127 Proxy(final InstallSnapshot installSnapshot) {
128 this.installSnapshot = installSnapshot;
132 public void writeExternal(final ObjectOutput out) throws IOException {
133 out.writeLong(installSnapshot.getTerm());
134 out.writeObject(installSnapshot.leaderId);
135 out.writeLong(installSnapshot.lastIncludedIndex);
136 out.writeLong(installSnapshot.lastIncludedTerm);
137 out.writeInt(installSnapshot.chunkIndex);
138 out.writeInt(installSnapshot.totalChunks);
140 out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0);
141 if (installSnapshot.lastChunkHashCode.isPresent()) {
142 out.writeInt(installSnapshot.lastChunkHashCode.getAsInt());
145 out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0);
146 if (installSnapshot.serverConfig.isPresent()) {
147 out.writeObject(installSnapshot.serverConfig.get());
150 out.writeObject(installSnapshot.data);
154 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
155 long term = in.readLong();
156 String leaderId = (String) in.readObject();
157 long lastIncludedIndex = in.readLong();
158 long lastIncludedTerm = in.readLong();
159 int chunkIndex = in.readInt();
160 int totalChunks = in.readInt();
162 OptionalInt lastChunkHashCode = in.readByte() == 1 ? OptionalInt.of(in.readInt()) : OptionalInt.empty();
163 Optional<ServerConfigurationPayload> serverConfig = in.readByte() == 1
164 ? Optional.of((ServerConfigurationPayload)in.readObject()) : Optional.empty();
166 byte[] data = (byte[])in.readObject();
168 installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
169 chunkIndex, totalChunks, lastChunkHashCode, serverConfig, RaftVersions.CURRENT_VERSION);
173 private Object readResolve() {
174 return installSnapshot;