X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Fmessages%2FInstallSnapshot.java;h=60c54f7fd01f03996f166ef562ff251bd34c4d56;hb=534bf6f83465cc8a575b097c1e28fbb1f34d110a;hp=5b6f030431e217016f7156b3c56172ba9e09fae1;hpb=bad1f8b8f3c1780cd37ec8a817ef4b0f23901654;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/InstallSnapshot.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/InstallSnapshot.java index 5b6f030431..60c54f7fd0 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/InstallSnapshot.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/InstallSnapshot.java @@ -5,17 +5,21 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.controller.cluster.raft.messages; -import com.google.common.base.Optional; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; +import java.util.Optional; +import java.util.OptionalInt; import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload; -public class InstallSnapshot extends AbstractRaftRPC { +/** + * Message sent from a leader to install a snapshot chunk on a follower. + */ +public final class InstallSnapshot extends AbstractRaftRPC { private static final long serialVersionUID = 1L; private final String leaderId; @@ -24,11 +28,17 @@ public class InstallSnapshot extends AbstractRaftRPC { private final byte[] data; private final int chunkIndex; private final int totalChunks; - private final Optional lastChunkHashCode; + @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()") + private final OptionalInt lastChunkHashCode; + @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()") private final Optional serverConfig; - public InstallSnapshot(long term, String leaderId, long lastIncludedIndex, long lastIncludedTerm, byte[] data, - int chunkIndex, int totalChunks, Optional lastChunkHashCode, Optional serverConfig) { + @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "Stores a reference to an externally mutable byte[] " + + "object but this is OK since this class is merely a DTO and does not process byte[] internally. " + + "Also it would be inefficient to create a copy as the byte[] could be large.") + public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex, + final long lastIncludedTerm, final byte[] data, final int chunkIndex, final int totalChunks, + final OptionalInt lastChunkHashCode, final Optional serverConfig) { super(term); this.leaderId = leaderId; this.lastIncludedIndex = lastIncludedIndex; @@ -40,10 +50,11 @@ public class InstallSnapshot extends AbstractRaftRPC { this.serverConfig = serverConfig; } - public InstallSnapshot(long term, String leaderId, long lastIncludedIndex, - long lastIncludedTerm, byte[] data, int chunkIndex, int totalChunks) { - this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, - Optional.absent(), Optional.absent()); + public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex, + final long lastIncludedTerm, final byte[] data, final int chunkIndex, + final int totalChunks) { + this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, OptionalInt.empty(), + Optional.empty()); } public String getLeaderId() { @@ -58,6 +69,9 @@ public class InstallSnapshot extends AbstractRaftRPC { return lastIncludedTerm; } + @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but " + + "this is OK since this class is merely a DTO and does not process the byte[] internally. " + + "Also it would be inefficient to create a return copy as the byte[] could be large.") public byte[] getData() { return data; } @@ -70,7 +84,7 @@ public class InstallSnapshot extends AbstractRaftRPC { return totalChunks; } - public Optional getLastChunkHashCode() { + public OptionalInt getLastChunkHashCode() { return lastChunkHashCode; } @@ -78,8 +92,7 @@ public class InstallSnapshot extends AbstractRaftRPC { return serverConfig; } - - public Object toSerializable(short version) { + public Object toSerializable(final short version) { return this; } @@ -88,10 +101,11 @@ public class InstallSnapshot extends AbstractRaftRPC { return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex=" + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode - + ", serverConfig=" + serverConfig.orNull() + "]"; + + ", serverConfig=" + serverConfig.orElse(null) + "]"; } - private Object writeReplace() { + @Override + Object writeReplace() { return new Proxy(this); } @@ -100,15 +114,18 @@ public class InstallSnapshot extends AbstractRaftRPC { private InstallSnapshot installSnapshot; + // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't + // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection. + @SuppressWarnings("checkstyle:RedundantModifier") public Proxy() { } - Proxy(InstallSnapshot installSnapshot) { + Proxy(final InstallSnapshot installSnapshot) { this.installSnapshot = installSnapshot; } @Override - public void writeExternal(ObjectOutput out) throws IOException { + public void writeExternal(final ObjectOutput out) throws IOException { out.writeLong(installSnapshot.getTerm()); out.writeObject(installSnapshot.leaderId); out.writeLong(installSnapshot.lastIncludedIndex); @@ -117,12 +134,12 @@ public class InstallSnapshot extends AbstractRaftRPC { out.writeInt(installSnapshot.totalChunks); out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0); - if(installSnapshot.lastChunkHashCode.isPresent()) { - out.writeInt(installSnapshot.lastChunkHashCode.get().intValue()); + if (installSnapshot.lastChunkHashCode.isPresent()) { + out.writeInt(installSnapshot.lastChunkHashCode.getAsInt()); } out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0); - if(installSnapshot.serverConfig.isPresent()) { + if (installSnapshot.serverConfig.isPresent()) { out.writeObject(installSnapshot.serverConfig.get()); } @@ -130,7 +147,7 @@ public class InstallSnapshot extends AbstractRaftRPC { } @Override - public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException { long term = in.readLong(); String leaderId = (String) in.readObject(); long lastIncludedIndex = in.readLong(); @@ -138,17 +155,9 @@ public class InstallSnapshot extends AbstractRaftRPC { int chunkIndex = in.readInt(); int totalChunks = in.readInt(); - Optional lastChunkHashCode = Optional.absent(); - boolean chunkHashCodePresent = in.readByte() == 1; - if(chunkHashCodePresent) { - lastChunkHashCode = Optional.of(in.readInt()); - } - - Optional serverConfig = Optional.absent(); - boolean serverConfigPresent = in.readByte() == 1; - if(serverConfigPresent) { - serverConfig = Optional.of((ServerConfigurationPayload)in.readObject()); - } + OptionalInt lastChunkHashCode = in.readByte() == 1 ? OptionalInt.of(in.readInt()) : OptionalInt.empty(); + Optional serverConfig = in.readByte() == 1 + ? Optional.of((ServerConfigurationPayload)in.readObject()) : Optional.empty(); byte[] data = (byte[])in.readObject();