X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Fmessages%2FInstallSnapshot.java;h=b64e902f3ff9f93e782c4114e6e690bbef971b0a;hp=119b43ce83acad53740566e50187411308d2fc69;hb=97542f208267cb5392fc8c8d9baf6c1d3ee4ae32;hpb=614324d63a339ef4acbc9e2c3bbaaef469f97868 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 119b43ce83..b64e902f3f 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 @@ -9,24 +9,34 @@ package org.opendaylight.controller.cluster.raft.messages; import com.google.common.base.Optional; -import com.google.protobuf.ByteString; -import org.opendaylight.controller.protobuff.messages.cluster.raft.InstallSnapshotMessages; - +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.persisted.ServerConfigurationPayload; + +/** + * Message sent from a leader to install a snapshot chunk on a follower. + */ public class InstallSnapshot extends AbstractRaftRPC { - - public static final Class SERIALIZABLE_CLASS = InstallSnapshotMessages.InstallSnapshot.class; private static final long serialVersionUID = 1L; private final String leaderId; private final long lastIncludedIndex; private final long lastIncludedTerm; - private final ByteString data; + private final byte[] data; private final int chunkIndex; private final int totalChunks; private final Optional lastChunkHashCode; - - public InstallSnapshot(long term, String leaderId, long lastIncludedIndex, - long lastIncludedTerm, ByteString data, int chunkIndex, int totalChunks, Optional lastChunkHashCode) { + 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(long term, String leaderId, long lastIncludedIndex, long lastIncludedTerm, byte[] data, + int chunkIndex, int totalChunks, Optional lastChunkHashCode, + Optional serverConfig) { super(term); this.leaderId = leaderId; this.lastIncludedIndex = lastIncludedIndex; @@ -35,14 +45,15 @@ public class InstallSnapshot extends AbstractRaftRPC { this.chunkIndex = chunkIndex; this.totalChunks = totalChunks; this.lastChunkHashCode = lastChunkHashCode; + this.serverConfig = serverConfig; } public InstallSnapshot(long term, String leaderId, long lastIncludedIndex, - long lastIncludedTerm, ByteString data, int chunkIndex, int totalChunks) { - this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, Optional.absent()); + long lastIncludedTerm, byte[] data, int chunkIndex, int totalChunks) { + this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, + Optional.absent(), Optional.absent()); } - public String getLeaderId() { return leaderId; } @@ -55,7 +66,10 @@ public class InstallSnapshot extends AbstractRaftRPC { return lastIncludedTerm; } - public ByteString getData() { + @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; } @@ -71,46 +85,93 @@ public class InstallSnapshot extends AbstractRaftRPC { return lastChunkHashCode; } - public Object toSerializable(){ - InstallSnapshotMessages.InstallSnapshot.Builder builder = InstallSnapshotMessages.InstallSnapshot.newBuilder() - .setLeaderId(this.getLeaderId()) - .setChunkIndex(this.getChunkIndex()) - .setData(this.getData()) - .setLastIncludedIndex(this.getLastIncludedIndex()) - .setLastIncludedTerm(this.getLastIncludedTerm()) - .setTotalChunks(this.getTotalChunks()); - - if(lastChunkHashCode.isPresent()){ - builder.setLastChunkHashCode(lastChunkHashCode.get()); - } - return builder.build(); + public Optional getServerConfig() { + return serverConfig; } - public static InstallSnapshot fromSerializable (Object o) { - InstallSnapshotMessages.InstallSnapshot from = - (InstallSnapshotMessages.InstallSnapshot) o; - Optional lastChunkHashCode = Optional.absent(); - if(from.hasLastChunkHashCode()){ - lastChunkHashCode = Optional.of(from.getLastChunkHashCode()); - } - - InstallSnapshot installSnapshot = new InstallSnapshot(from.getTerm(), - from.getLeaderId(), from.getLastIncludedIndex(), - from.getLastIncludedTerm(), from.getData(), - from.getChunkIndex(), from.getTotalChunks(), lastChunkHashCode); - - return installSnapshot; + public Object toSerializable(short version) { + return this; } @Override public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("InstallSnapshot [term=").append(term).append(", leaderId=").append(leaderId) - .append(", lastIncludedIndex=").append(lastIncludedIndex).append(", lastIncludedTerm=") - .append(lastIncludedTerm).append(", data=").append(data).append(", chunkIndex=").append(chunkIndex) - .append(", totalChunks=").append(totalChunks).append(", lastChunkHashCode=").append(lastChunkHashCode) - .append("]"); - return builder.toString(); + return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex=" + + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length + + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode + + ", serverConfig=" + serverConfig.orNull() + "]"; + } + + private Object writeReplace() { + return new Proxy(this); + } + + private static class Proxy implements Externalizable { + private static final long serialVersionUID = 1L; + + 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) { + this.installSnapshot = installSnapshot; + } + + @Override + public void writeExternal(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.get().intValue()); + } + + out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0); + if (installSnapshot.serverConfig.isPresent()) { + out.writeObject(installSnapshot.serverConfig.get()); + } + + out.writeObject(installSnapshot.data); + } + + @Override + public void readExternal(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(); + + 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()); + } + + byte[] data = (byte[])in.readObject(); + + installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, + chunkIndex, totalChunks, lastChunkHashCode, serverConfig); + } + + private Object readResolve() { + return installSnapshot; + } } }