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 static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import java.util.OptionalInt;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.controller.cluster.raft.RaftVersions;
21 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
24 * Message sent from a leader to install a snapshot chunk on a follower.
26 public final class InstallSnapshot extends AbstractRaftRPC {
28 private static final long serialVersionUID = 1L;
30 private final String leaderId;
31 private final long lastIncludedIndex;
32 private final long lastIncludedTerm;
33 private final byte[] data;
34 private final int chunkIndex;
35 private final int totalChunks;
36 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()")
37 private final OptionalInt lastChunkHashCode;
38 private final @Nullable ServerConfigurationPayload serverConfig;
39 private final short recipientRaftVersion;
41 @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = """
42 Stores a reference to an externally mutable byte[] object but this is OK since this class is merely a DTO and \
43 does not process byte[] internally. Also it would be inefficient to create a copy as the byte[] could be \
45 public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
46 final long lastIncludedTerm, final byte[] data, final int chunkIndex, final int totalChunks,
47 final OptionalInt lastChunkHashCode, final @Nullable ServerConfigurationPayload serverConfig,
48 final short recipientRaftVersion) {
50 this.leaderId = leaderId;
51 this.lastIncludedIndex = lastIncludedIndex;
52 this.lastIncludedTerm = lastIncludedTerm;
54 this.chunkIndex = chunkIndex;
55 this.totalChunks = totalChunks;
56 this.lastChunkHashCode = lastChunkHashCode;
57 this.serverConfig = serverConfig;
58 this.recipientRaftVersion = recipientRaftVersion;
62 public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
63 final long lastIncludedTerm, final byte[] data, final int chunkIndex,
64 final int totalChunks) {
65 this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, OptionalInt.empty(),
66 null, RaftVersions.CURRENT_VERSION);
69 public String getLeaderId() {
73 public long getLastIncludedIndex() {
74 return lastIncludedIndex;
77 public long getLastIncludedTerm() {
78 return lastIncludedTerm;
81 @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = """
82 Exposes a mutable object stored in a field but this is OK since this class is merely a DTO and does not \
83 process the byte[] internally. Also it would be inefficient to create a return copy as the byte[] could be \
85 public byte[] getData() {
89 public int getChunkIndex() {
93 public int getTotalChunks() {
97 public OptionalInt getLastChunkHashCode() {
98 return lastChunkHashCode;
101 public @Nullable ServerConfigurationPayload serverConfig() {
106 public String toString() {
107 return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
108 + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
109 + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
110 + ", serverConfig=" + serverConfig + "]";
114 Object writeReplace() {
115 return recipientRaftVersion <= RaftVersions.FLUORINE_VERSION ? new Proxy(this) : new IS(this);
118 private static class Proxy implements Externalizable {
120 private static final long serialVersionUID = 1L;
122 private InstallSnapshot installSnapshot;
124 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
125 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
126 @SuppressWarnings("checkstyle:RedundantModifier")
130 Proxy(final InstallSnapshot installSnapshot) {
131 this.installSnapshot = installSnapshot;
135 public void writeExternal(final ObjectOutput out) throws IOException {
136 out.writeLong(installSnapshot.getTerm());
137 out.writeObject(installSnapshot.leaderId);
138 out.writeLong(installSnapshot.lastIncludedIndex);
139 out.writeLong(installSnapshot.lastIncludedTerm);
140 out.writeInt(installSnapshot.chunkIndex);
141 out.writeInt(installSnapshot.totalChunks);
143 out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0);
144 if (installSnapshot.lastChunkHashCode.isPresent()) {
145 out.writeInt(installSnapshot.lastChunkHashCode.orElseThrow());
148 out.writeByte(installSnapshot.serverConfig != null ? 1 : 0);
149 if (installSnapshot.serverConfig != null) {
150 out.writeObject(installSnapshot.serverConfig);
153 out.writeObject(installSnapshot.data);
157 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
158 long term = in.readLong();
159 String leaderId = (String) in.readObject();
160 long lastIncludedIndex = in.readLong();
161 long lastIncludedTerm = in.readLong();
162 int chunkIndex = in.readInt();
163 int totalChunks = in.readInt();
165 OptionalInt lastChunkHashCode = in.readByte() == 1 ? OptionalInt.of(in.readInt()) : OptionalInt.empty();
166 ServerConfigurationPayload serverConfig;
167 if (in.readByte() == 1) {
168 serverConfig = requireNonNull((ServerConfigurationPayload) in.readObject());
173 byte[] data = (byte[])in.readObject();
175 installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
176 chunkIndex, totalChunks, lastChunkHashCode, serverConfig, RaftVersions.CURRENT_VERSION);
180 private Object readResolve() {
181 return installSnapshot;