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=6ef0b898f2117a4bb3a510c0df7af340f4fc8eca;hp=f2f9cd39d81e9d5dd5d8d342f3cc7f018f7b0973;hpb=252ba03242407ee584c38fafdbfa1c322e66151d;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 f2f9cd39d8..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,38 +5,40 @@ * 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 com.google.protobuf.ByteString; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; -import org.opendaylight.controller.cluster.raft.RaftVersions; -import org.opendaylight.controller.protobuff.messages.cluster.raft.InstallSnapshotMessages; +import java.util.Optional; +import java.util.OptionalInt; +import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload; -public class InstallSnapshot extends AbstractRaftRPC implements Externalizable { +/** + * 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; - public static final Class SERIALIZABLE_CLASS = InstallSnapshotMessages.InstallSnapshot.class; - - private String leaderId; - private long lastIncludedIndex; - private long lastIncludedTerm; - private byte[] data; - private int chunkIndex; - private int totalChunks; - private Optional lastChunkHashCode; - - /** - * Empty constructor to satisfy Externalizable. - */ - public InstallSnapshot() { - } - public InstallSnapshot(long term, String leaderId, long lastIncludedIndex, - long lastIncludedTerm, byte[] data, int chunkIndex, int totalChunks, Optional lastChunkHashCode) { + private final String leaderId; + private final long lastIncludedIndex; + private final long lastIncludedTerm; + private final byte[] data; + private final int chunkIndex; + private final int totalChunks; + @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; + + @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; @@ -45,11 +47,14 @@ public class InstallSnapshot extends AbstractRaftRPC implements Externalizable { this.chunkIndex = chunkIndex; this.totalChunks = totalChunks; this.lastChunkHashCode = lastChunkHashCode; + 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()); + 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() { @@ -64,6 +69,9 @@ public class InstallSnapshot extends AbstractRaftRPC implements Externalizable { 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; } @@ -76,96 +84,89 @@ public class InstallSnapshot extends AbstractRaftRPC implements Externalizable { return totalChunks; } - public Optional getLastChunkHashCode() { + public OptionalInt getLastChunkHashCode() { return lastChunkHashCode; } - @Override - public void writeExternal(ObjectOutput out) throws IOException { - out.writeShort(RaftVersions.CURRENT_VERSION); - out.writeLong(getTerm()); - out.writeUTF(leaderId); - out.writeLong(lastIncludedIndex); - out.writeLong(lastIncludedTerm); - out.writeInt(chunkIndex); - out.writeInt(totalChunks); - - out.writeByte(lastChunkHashCode.isPresent() ? 1 : 0); - if(lastChunkHashCode.isPresent()) { - out.writeInt(lastChunkHashCode.get().intValue()); - } - - out.writeObject(data); - } - - @Override - public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - in.readShort(); // raft version - not currently used - setTerm(in.readLong()); - leaderId = in.readUTF(); - lastIncludedIndex = in.readLong(); - lastIncludedTerm = in.readLong(); - chunkIndex = in.readInt(); - totalChunks = in.readInt(); - - lastChunkHashCode = Optional.absent(); - boolean chunkHashCodePresent = in.readByte() == 1; - if(chunkHashCodePresent) { - lastChunkHashCode = Optional.of(in.readInt()); - } - - data = (byte[])in.readObject(); + public Optional getServerConfig() { + return serverConfig; } - public Object toSerializable(short version) { - if(version >= RaftVersions.BORON_VERSION) { - return this; - } else { - InstallSnapshotMessages.InstallSnapshot.Builder builder = InstallSnapshotMessages.InstallSnapshot.newBuilder() - .setTerm(this.getTerm()) - .setLeaderId(this.getLeaderId()) - .setChunkIndex(this.getChunkIndex()) - .setData(ByteString.copyFrom(getData())) - .setLastIncludedIndex(this.getLastIncludedIndex()) - .setLastIncludedTerm(this.getLastIncludedTerm()) - .setTotalChunks(this.getTotalChunks()); - - if(lastChunkHashCode.isPresent()){ - builder.setLastChunkHashCode(lastChunkHashCode.get()); - } - return builder.build(); - } + public Object toSerializable(final short version) { + return this; } @Override public String toString() { return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex=" + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length - + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode + "]"; + + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode + + ", serverConfig=" + serverConfig.orElse(null) + "]"; + } + + @Override + Object writeReplace() { + return new Proxy(this); } - public static InstallSnapshot fromSerializable (Object o) { - if(o instanceof InstallSnapshot) { - return (InstallSnapshot)o; - } else { - InstallSnapshotMessages.InstallSnapshot from = - (InstallSnapshotMessages.InstallSnapshot) o; + private static class Proxy implements Externalizable { + private static final long serialVersionUID = 1L; - Optional lastChunkHashCode = Optional.absent(); - if(from.hasLastChunkHashCode()){ - lastChunkHashCode = Optional.of(from.getLastChunkHashCode()); + 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(final InstallSnapshot installSnapshot) { + this.installSnapshot = installSnapshot; + } + + @Override + public void writeExternal(final ObjectOutput out) throws IOException { + out.writeLong(installSnapshot.getTerm()); + out.writeObject(installSnapshot.leaderId); + out.writeLong(installSnapshot.lastIncludedIndex); + out.writeLong(installSnapshot.lastIncludedTerm); + out.writeInt(installSnapshot.chunkIndex); + out.writeInt(installSnapshot.totalChunks); + + out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0); + if (installSnapshot.lastChunkHashCode.isPresent()) { + out.writeInt(installSnapshot.lastChunkHashCode.getAsInt()); } - InstallSnapshot installSnapshot = new InstallSnapshot(from.getTerm(), - from.getLeaderId(), from.getLastIncludedIndex(), - from.getLastIncludedTerm(), from.getData().toByteArray(), - from.getChunkIndex(), from.getTotalChunks(), lastChunkHashCode); + out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0); + if (installSnapshot.serverConfig.isPresent()) { + out.writeObject(installSnapshot.serverConfig.get()); + } - return installSnapshot; + out.writeObject(installSnapshot.data); } - } - public static boolean isSerializedType(Object message) { - return message instanceof InstallSnapshot || message instanceof InstallSnapshotMessages.InstallSnapshot; + @Override + public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException { + long term = in.readLong(); + String leaderId = (String) in.readObject(); + long lastIncludedIndex = in.readLong(); + long lastIncludedTerm = in.readLong(); + int chunkIndex = in.readInt(); + int totalChunks = in.readInt(); + + 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(); + + installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, + chunkIndex, totalChunks, lastChunkHashCode, serverConfig); + } + + private Object readResolve() { + return installSnapshot; + } } }