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%2Fpersisted%2FSimpleReplicatedLogEntry.java;h=4c07e6b812e254ad026e6d49211e4e22b97f5b6b;hp=85fa51e93f0b4fc8e5ce95eb2a3925671a050e2e;hb=HEAD;hpb=e7512222d7d9e3149feb6a90eeb726e9391887fa diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/persisted/SimpleReplicatedLogEntry.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/persisted/SimpleReplicatedLogEntry.java index 85fa51e93f..610d53a9e7 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/persisted/SimpleReplicatedLogEntry.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/persisted/SimpleReplicatedLogEntry.java @@ -5,17 +5,14 @@ * 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.persisted; -import com.google.common.base.Preconditions; -import java.io.Externalizable; -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; +import static java.util.Objects.requireNonNull; + import java.io.Serializable; +import org.apache.commons.lang3.SerializationUtils; import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry; -import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload; +import org.opendaylight.controller.cluster.raft.messages.Payload; /** * A {@link ReplicatedLogEntry} implementation. @@ -23,40 +20,10 @@ import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payloa * @author Thomas Pantelis */ public final class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Serializable { - private static final class Proxy implements Externalizable { - private static final long serialVersionUID = 1L; - - private ReplicatedLogEntry replicatedLogEntry; - - // 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() { - // For Externalizable - } - - Proxy(final ReplicatedLogEntry replicatedLogEntry) { - this.replicatedLogEntry = replicatedLogEntry; - } - - @Override - public void writeExternal(final ObjectOutput out) throws IOException { - out.writeLong(replicatedLogEntry.getIndex()); - out.writeLong(replicatedLogEntry.getTerm()); - out.writeObject(replicatedLogEntry.getData()); - } - - @Override - public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException { - replicatedLogEntry = new SimpleReplicatedLogEntry(in.readLong(), in.readLong(), (Payload) in.readObject()); - } - - private Object readResolve() { - return replicatedLogEntry; - } - } - + @java.io.Serial private static final long serialVersionUID = 1L; + // Estimate to how big the proxy is. Note this includes object stream overhead, so it is a bit conservative. + private static final int PROXY_SIZE = SerializationUtils.serialize(new LE((Void) null)).length; private final long index; private final long term; @@ -70,10 +37,10 @@ public final class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Seria * @param term the term * @param payload the payload */ - public SimpleReplicatedLogEntry(long index, long term, Payload payload) { + public SimpleReplicatedLogEntry(final long index, final long term, final Payload payload) { this.index = index; this.term = term; - this.payload = Preconditions.checkNotNull(payload); + this.payload = requireNonNull(payload); } @Override @@ -93,7 +60,12 @@ public final class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Seria @Override public int size() { - return getData().size(); + return payload.size(); + } + + @Override + public int serializedSize() { + return PROXY_SIZE + payload.serializedSize(); } @Override @@ -102,14 +74,10 @@ public final class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Seria } @Override - public void setPersistencePending(boolean pending) { + public void setPersistencePending(final boolean pending) { persistencePending = pending; } - private Object writeReplace() { - return new Proxy(this); - } - @Override public int hashCode() { final int prime = 31; @@ -121,21 +89,18 @@ public final class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Seria } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - - if (obj == null || getClass() != obj.getClass()) { - return false; - } - - SimpleReplicatedLogEntry other = (SimpleReplicatedLogEntry) obj; - return index == other.index && term == other.term && payload.equals(other.payload); + public boolean equals(final Object obj) { + return this == obj || obj instanceof SimpleReplicatedLogEntry other && index == other.index + && term == other.term && payload.equals(other.payload); } @Override public String toString() { return "SimpleReplicatedLogEntry [index=" + index + ", term=" + term + ", payload=" + payload + "]"; } + + @java.io.Serial + private Object writeReplace() { + return new LE(this); + } }