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 {
25 private static final long serialVersionUID = 1L;
27 private final String leaderId;
28 private final long lastIncludedIndex;
29 private final long lastIncludedTerm;
30 private final byte[] data;
31 private final int chunkIndex;
32 private final int totalChunks;
33 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()")
34 private final OptionalInt lastChunkHashCode;
35 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()")
36 private final Optional<ServerConfigurationPayload> serverConfig;
37 private final short recipientRaftVersion;
39 @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "Stores a reference to an externally mutable byte[] "
40 + "object but this is OK since this class is merely a DTO and does not process byte[] internally. "
41 + "Also it would be inefficient to create a copy as the byte[] could be large.")
42 public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
43 final long lastIncludedTerm, final byte[] data, final int chunkIndex, final int totalChunks,
44 final OptionalInt lastChunkHashCode, final Optional<ServerConfigurationPayload> serverConfig,
45 final short recipientRaftVersion) {
47 this.leaderId = leaderId;
48 this.lastIncludedIndex = lastIncludedIndex;
49 this.lastIncludedTerm = lastIncludedTerm;
51 this.chunkIndex = chunkIndex;
52 this.totalChunks = totalChunks;
53 this.lastChunkHashCode = lastChunkHashCode;
54 this.serverConfig = serverConfig;
55 this.recipientRaftVersion = recipientRaftVersion;
59 public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
60 final long lastIncludedTerm, final byte[] data, final int chunkIndex,
61 final int totalChunks) {
62 this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, OptionalInt.empty(),
63 Optional.empty(), RaftVersions.CURRENT_VERSION);
66 public String getLeaderId() {
70 public long getLastIncludedIndex() {
71 return lastIncludedIndex;
74 public long getLastIncludedTerm() {
75 return lastIncludedTerm;
78 @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
79 + "this is OK since this class is merely a DTO and does not process the byte[] internally. "
80 + "Also it would be inefficient to create a return copy as the byte[] could be large.")
81 public byte[] getData() {
85 public int getChunkIndex() {
89 public int getTotalChunks() {
93 public OptionalInt getLastChunkHashCode() {
94 return lastChunkHashCode;
97 public Optional<ServerConfigurationPayload> getServerConfig() {
102 public String toString() {
103 return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
104 + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
105 + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
106 + ", serverConfig=" + serverConfig.orElse(null) + "]";
110 Object writeReplace() {
111 return recipientRaftVersion <= RaftVersions.FLUORINE_VERSION ? new Proxy(this) : new IS(this);
114 private static class Proxy implements Externalizable {
116 private static final long serialVersionUID = 1L;
118 private InstallSnapshot installSnapshot;
120 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
121 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
122 @SuppressWarnings("checkstyle:RedundantModifier")
126 Proxy(final InstallSnapshot installSnapshot) {
127 this.installSnapshot = installSnapshot;
131 public void writeExternal(final ObjectOutput out) throws IOException {
132 out.writeLong(installSnapshot.getTerm());
133 out.writeObject(installSnapshot.leaderId);
134 out.writeLong(installSnapshot.lastIncludedIndex);
135 out.writeLong(installSnapshot.lastIncludedTerm);
136 out.writeInt(installSnapshot.chunkIndex);
137 out.writeInt(installSnapshot.totalChunks);
139 out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0);
140 if (installSnapshot.lastChunkHashCode.isPresent()) {
141 out.writeInt(installSnapshot.lastChunkHashCode.getAsInt());
144 out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0);
145 if (installSnapshot.serverConfig.isPresent()) {
146 out.writeObject(installSnapshot.serverConfig.get());
149 out.writeObject(installSnapshot.data);
153 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
154 long term = in.readLong();
155 String leaderId = (String) in.readObject();
156 long lastIncludedIndex = in.readLong();
157 long lastIncludedTerm = in.readLong();
158 int chunkIndex = in.readInt();
159 int totalChunks = in.readInt();
161 OptionalInt lastChunkHashCode = in.readByte() == 1 ? OptionalInt.of(in.readInt()) : OptionalInt.empty();
162 Optional<ServerConfigurationPayload> serverConfig = in.readByte() == 1
163 ? Optional.of((ServerConfigurationPayload)in.readObject()) : Optional.empty();
165 byte[] data = (byte[])in.readObject();
167 installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
168 chunkIndex, totalChunks, lastChunkHashCode, serverConfig, RaftVersions.CURRENT_VERSION);
172 private Object readResolve() {
173 return installSnapshot;