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 = """
41 Stores a reference to an externally mutable byte[] object but this is OK since this class is merely a DTO and \
42 does not process byte[] internally. Also it would be inefficient to create a copy as the byte[] could be \
44 public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
45 final long lastIncludedTerm, final byte[] data, final int chunkIndex, final int totalChunks,
46 final OptionalInt lastChunkHashCode, final Optional<ServerConfigurationPayload> serverConfig,
47 final short recipientRaftVersion) {
49 this.leaderId = leaderId;
50 this.lastIncludedIndex = lastIncludedIndex;
51 this.lastIncludedTerm = lastIncludedTerm;
53 this.chunkIndex = chunkIndex;
54 this.totalChunks = totalChunks;
55 this.lastChunkHashCode = lastChunkHashCode;
56 this.serverConfig = serverConfig;
57 this.recipientRaftVersion = recipientRaftVersion;
61 public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
62 final long lastIncludedTerm, final byte[] data, final int chunkIndex,
63 final int totalChunks) {
64 this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, OptionalInt.empty(),
65 Optional.empty(), RaftVersions.CURRENT_VERSION);
68 public String getLeaderId() {
72 public long getLastIncludedIndex() {
73 return lastIncludedIndex;
76 public long getLastIncludedTerm() {
77 return lastIncludedTerm;
80 @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = """
81 Exposes a mutable object stored in a field but this is OK since this class is merely a DTO and does not \
82 process the byte[] internally. Also it would be inefficient to create a return copy as the byte[] could be \
84 public byte[] getData() {
88 public int getChunkIndex() {
92 public int getTotalChunks() {
96 public OptionalInt getLastChunkHashCode() {
97 return lastChunkHashCode;
100 public Optional<ServerConfigurationPayload> getServerConfig() {
105 public String toString() {
106 return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
107 + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
108 + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
109 + ", serverConfig=" + serverConfig.orElse(null) + "]";
113 Object writeReplace() {
114 return recipientRaftVersion <= RaftVersions.FLUORINE_VERSION ? new Proxy(this) : new IS(this);
117 private static class Proxy implements Externalizable {
119 private static final long serialVersionUID = 1L;
121 private InstallSnapshot installSnapshot;
123 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
124 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
125 @SuppressWarnings("checkstyle:RedundantModifier")
129 Proxy(final InstallSnapshot installSnapshot) {
130 this.installSnapshot = installSnapshot;
134 public void writeExternal(final ObjectOutput out) throws IOException {
135 out.writeLong(installSnapshot.getTerm());
136 out.writeObject(installSnapshot.leaderId);
137 out.writeLong(installSnapshot.lastIncludedIndex);
138 out.writeLong(installSnapshot.lastIncludedTerm);
139 out.writeInt(installSnapshot.chunkIndex);
140 out.writeInt(installSnapshot.totalChunks);
142 out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0);
143 if (installSnapshot.lastChunkHashCode.isPresent()) {
144 out.writeInt(installSnapshot.lastChunkHashCode.orElseThrow());
147 out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0);
148 if (installSnapshot.serverConfig.isPresent()) {
149 out.writeObject(installSnapshot.serverConfig.orElseThrow());
152 out.writeObject(installSnapshot.data);
156 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
157 long term = in.readLong();
158 String leaderId = (String) in.readObject();
159 long lastIncludedIndex = in.readLong();
160 long lastIncludedTerm = in.readLong();
161 int chunkIndex = in.readInt();
162 int totalChunks = in.readInt();
164 OptionalInt lastChunkHashCode = in.readByte() == 1 ? OptionalInt.of(in.readInt()) : OptionalInt.empty();
165 Optional<ServerConfigurationPayload> serverConfig = in.readByte() == 1
166 ? Optional.of((ServerConfigurationPayload)in.readObject()) : Optional.empty();
168 byte[] data = (byte[])in.readObject();
170 installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
171 chunkIndex, totalChunks, lastChunkHashCode, serverConfig, RaftVersions.CURRENT_VERSION);
175 private Object readResolve() {
176 return installSnapshot;