85ff4193a8cbbef1c2bf8941d5dc404dfbe5fe83
[controller.git] /
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.raft.messages;
9
10 import static java.util.Objects.requireNonNull;
11
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;
22
23 /**
24  * Message sent from a leader to install a snapshot chunk on a follower.
25  */
26 public final class InstallSnapshot extends AbstractRaftRPC {
27     @java.io.Serial
28     private static final long serialVersionUID = 1L;
29
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;
40
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 \
44         large.""")
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) {
49         super(term);
50         this.leaderId = leaderId;
51         this.lastIncludedIndex = lastIncludedIndex;
52         this.lastIncludedTerm = lastIncludedTerm;
53         this.data = data;
54         this.chunkIndex = chunkIndex;
55         this.totalChunks = totalChunks;
56         this.lastChunkHashCode = lastChunkHashCode;
57         this.serverConfig = serverConfig;
58         this.recipientRaftVersion = recipientRaftVersion;
59     }
60
61     @VisibleForTesting
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);
67     }
68
69     public String getLeaderId() {
70         return leaderId;
71     }
72
73     public long getLastIncludedIndex() {
74         return lastIncludedIndex;
75     }
76
77     public long getLastIncludedTerm() {
78         return lastIncludedTerm;
79     }
80
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 \
84         large.""")
85     public byte[] getData() {
86         return data;
87     }
88
89     public int getChunkIndex() {
90         return chunkIndex;
91     }
92
93     public int getTotalChunks() {
94         return totalChunks;
95     }
96
97     public OptionalInt getLastChunkHashCode() {
98         return lastChunkHashCode;
99     }
100
101     public @Nullable ServerConfigurationPayload serverConfig() {
102         return serverConfig;
103     }
104
105     @Override
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 + "]";
111     }
112
113     @Override
114     Object writeReplace() {
115         return recipientRaftVersion <= RaftVersions.FLUORINE_VERSION ? new Proxy(this) : new IS(this);
116     }
117
118     private static class Proxy implements Externalizable {
119         @java.io.Serial
120         private static final long serialVersionUID = 1L;
121
122         private InstallSnapshot installSnapshot;
123
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")
127         public Proxy() {
128         }
129
130         Proxy(final InstallSnapshot installSnapshot) {
131             this.installSnapshot = installSnapshot;
132         }
133
134         @Override
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);
142
143             out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0);
144             if (installSnapshot.lastChunkHashCode.isPresent()) {
145                 out.writeInt(installSnapshot.lastChunkHashCode.orElseThrow());
146             }
147
148             out.writeByte(installSnapshot.serverConfig != null ? 1 : 0);
149             if (installSnapshot.serverConfig != null) {
150                 out.writeObject(installSnapshot.serverConfig);
151             }
152
153             out.writeObject(installSnapshot.data);
154         }
155
156         @Override
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();
164
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());
169             } else {
170                 serverConfig = null;
171             }
172
173             byte[] data = (byte[])in.readObject();
174
175             installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
176                     chunkIndex, totalChunks, lastChunkHashCode, serverConfig, RaftVersions.CURRENT_VERSION);
177         }
178
179         @java.io.Serial
180         private Object readResolve() {
181             return installSnapshot;
182         }
183     }
184 }