Trigger snapshots on legacy persisted entries
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / persisted / Snapshot.java
index 5f8a55f9efaf36f950722ce667e001d19ea50565..0acede1086ef20f3de26531cb7baf95a5f850507 100644 (file)
@@ -22,9 +22,8 @@ import org.opendaylight.controller.cluster.raft.messages.Payload;
  *
  * @author Thomas Pantelis
  */
-// Not final for mocking
+// Not final and non-sealed for mocking
 public class Snapshot implements Serializable {
-
     /**
      * Implementations of this interface are used as the state payload for a snapshot.
      *
@@ -42,10 +41,25 @@ public class Snapshot implements Serializable {
         }
     }
 
+    @Deprecated(since = "7.0.0", forRemoval = true)
+    private static final class Legacy extends Snapshot implements LegacySerializable {
+        @java.io.Serial
+        private static final long serialVersionUID = 1L;
+
+        Legacy(final State state, final List<ReplicatedLogEntry> unAppliedEntries, final long lastIndex,
+                final long lastTerm, final long lastAppliedIndex, final long lastAppliedTerm, final long electionTerm,
+                final String electionVotedFor, final ServerConfigurationPayload serverConfig) {
+            super(state, unAppliedEntries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm, electionTerm,
+                electionVotedFor, serverConfig);
+        }
+    }
+
+    @Deprecated(since = "7.0.0", forRemoval = true)
     private static final class Proxy implements Externalizable {
+        @java.io.Serial
         private static final long serialVersionUID = 1L;
 
-        private Snapshot snapshot;
+        private Snapshot snapshot = null;
 
         // 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.
@@ -54,10 +68,6 @@ public class Snapshot implements Serializable {
             // For Externalizable
         }
 
-        Proxy(final Snapshot snapshot) {
-            this.snapshot = snapshot;
-        }
-
         @Override
         public void writeExternal(final ObjectOutput out) throws IOException {
             out.writeLong(snapshot.lastIndex);
@@ -89,7 +99,7 @@ public class Snapshot implements Serializable {
             ServerConfigurationPayload serverConfig = (ServerConfigurationPayload) in.readObject();
 
             int size = in.readInt();
-            List<ReplicatedLogEntry> unAppliedEntries = new ArrayList<>(size);
+            var unAppliedEntries = new ArrayList<ReplicatedLogEntry>(size);
             for (int i = 0; i < size; i++) {
                 unAppliedEntries.add(new SimpleReplicatedLogEntry(in.readLong(), in.readLong(),
                         (Payload) in.readObject()));
@@ -97,15 +107,17 @@ public class Snapshot implements Serializable {
 
             State state = (State) in.readObject();
 
-            snapshot = Snapshot.create(state, unAppliedEntries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
+            snapshot = new Legacy(state, unAppliedEntries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
                     electionTerm, electionVotedFor, serverConfig);
         }
 
+        @java.io.Serial
         private Object readResolve() {
             return snapshot;
         }
     }
 
+    @java.io.Serial
     private static final long serialVersionUID = 1L;
 
     private final State state;
@@ -160,7 +172,7 @@ public class Snapshot implements Serializable {
     }
 
     public long getLastIndex() {
-        return this.lastIndex;
+        return lastIndex;
     }
 
     public long getElectionTerm() {
@@ -175,12 +187,13 @@ public class Snapshot implements Serializable {
         return serverConfig;
     }
 
-    private Object writeReplace() {
-        return new Proxy(this);
+    @java.io.Serial
+    public final Object writeReplace() {
+        return new SS(this);
     }
 
     @Override
-    public String toString() {
+    public final String toString() {
         return "Snapshot [lastIndex=" + lastIndex + ", lastTerm=" + lastTerm + ", lastAppliedIndex=" + lastAppliedIndex
                 + ", lastAppliedTerm=" + lastAppliedTerm + ", unAppliedEntries size=" + unAppliedEntries.size()
                 + ", state=" + state + ", electionTerm=" + electionTerm + ", electionVotedFor="