Trigger snapshots on legacy persisted entries
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / persisted / SimpleReplicatedLogEntry.java
index 3dc5a286a73f299075047c062950d6c7366ff01c..d075a417017a6066e08d62306f68c85e52508e89 100644 (file)
@@ -5,27 +5,43 @@
  * 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 static java.util.Objects.requireNonNull;
+
 import java.io.Externalizable;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
+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.
  *
  * @author Thomas Pantelis
  */
-public class SimpleReplicatedLogEntry implements ReplicatedLogEntry, MigratedSerializable {
+public sealed class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
+    @Deprecated(since = "7.0.0", forRemoval = true)
+    private static final class Legacy extends SimpleReplicatedLogEntry implements LegacySerializable {
+        @java.io.Serial
+        private static final long serialVersionUID = 1L;
+
+        Legacy(final long index, final long term, final Payload payload) {
+            super(index, term, payload);
+        }
+    }
+
+    @Deprecated(since = "7.0.0", forRemoval = true)
     private static final class Proxy implements Externalizable {
+        @java.io.Serial
         private static final long serialVersionUID = 1L;
 
-        private ReplicatedLogEntry replicatedLogEntry;
+        private long index;
+        private long term;
+        private Payload data;
 
         // 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.
@@ -34,41 +50,41 @@ public class SimpleReplicatedLogEntry implements ReplicatedLogEntry, MigratedSer
             // For Externalizable
         }
 
-        Proxy(final ReplicatedLogEntry replicatedLogEntry) {
-            this.replicatedLogEntry = replicatedLogEntry;
+        Proxy(final SimpleReplicatedLogEntry replicatedLogEntry) {
+            index = replicatedLogEntry.getIndex();
+            term = replicatedLogEntry.getTerm();
+            data = replicatedLogEntry.getData();
         }
 
         @Override
         public void writeExternal(final ObjectOutput out) throws IOException {
-            out.writeLong(replicatedLogEntry.getIndex());
-            out.writeLong(replicatedLogEntry.getTerm());
-            out.writeObject(replicatedLogEntry.getData());
+            out.writeLong(index);
+            out.writeLong(term);
+            out.writeObject(data);
         }
 
         @Override
         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
-            replicatedLogEntry = new SimpleReplicatedLogEntry(in.readLong(), in.readLong(), (Payload) in.readObject());
+            index = in.readLong();
+            term = in.readLong();
+            data = (Payload) in.readObject();
         }
 
+        @java.io.Serial
         private Object readResolve() {
-            return replicatedLogEntry;
+            return new Legacy(index, term, data);
         }
     }
 
+    @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;
     private final Payload payload;
     private boolean persistencePending;
-    private final boolean migrated;
-
-    private SimpleReplicatedLogEntry(long index, long term, Payload payload, boolean migrated) {
-        this.index = index;
-        this.term = term;
-        this.payload = Preconditions.checkNotNull(payload);
-        this.migrated = migrated;
-    }
 
     /**
      * Constructs an instance.
@@ -78,56 +94,53 @@ public class SimpleReplicatedLogEntry implements ReplicatedLogEntry, MigratedSer
      * @param payload the payload
      */
     public SimpleReplicatedLogEntry(final long index, final long term, final Payload payload) {
-        this(index, term, payload, false);
-    }
-
-    @Deprecated
-    public static ReplicatedLogEntry createMigrated(final long index, final long term, final Payload payload) {
-        return new SimpleReplicatedLogEntry(index, term, payload, true);
+        this.index = index;
+        this.term = term;
+        this.payload = requireNonNull(payload);
     }
 
     @Override
-    public Payload getData() {
+    public final Payload getData() {
         return payload;
     }
 
     @Override
-    public long getTerm() {
+    public final long getTerm() {
         return term;
     }
 
     @Override
-    public long getIndex() {
+    public final long getIndex() {
         return index;
     }
 
     @Override
-    public int size() {
-        return getData().size();
+    public final int size() {
+        return payload.size();
     }
 
     @Override
-    public boolean isPersistencePending() {
-        return persistencePending;
+    public final int serializedSize() {
+        return PROXY_SIZE + payload.serializedSize();
     }
 
     @Override
-    public void setPersistencePending(boolean pending) {
-        persistencePending = pending;
+    public final boolean isPersistencePending() {
+        return persistencePending;
     }
 
     @Override
-    public boolean isMigrated() {
-        return migrated;
+    public final void setPersistencePending(final boolean pending) {
+        persistencePending = pending;
     }
 
-    @Override
-    public Object writeReplace() {
-        return new Proxy(this);
+    @java.io.Serial
+    public final Object writeReplace() {
+        return new LE(this);
     }
 
     @Override
-    public int hashCode() {
+    public final int hashCode() {
         final int prime = 31;
         int result = 1;
         result = prime * result + payload.hashCode();
@@ -137,21 +150,13 @@ public class SimpleReplicatedLogEntry implements ReplicatedLogEntry, MigratedSer
     }
 
     @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 final 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() {
+    public final String toString() {
         return "SimpleReplicatedLogEntry [index=" + index + ", term=" + term + ", payload=" + payload + "]";
     }
 }