Make ApplySnapshot a record 64/114464/5
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 12 Nov 2024 18:37:36 +0000 (19:37 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 13 Nov 2024 16:59:27 +0000 (17:59 +0100)
This is purely-internal DTO, make it a record.

Change-Id: I4570d9a78362f4dff7d4aceb4b469652396d3643
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorSnapshotMessageSupport.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/SnapshotManager.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/base/messages/ApplySnapshot.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorServerConfigurationSupportTest.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/ReplicationAndSnapshotsWithLaggingFollowerIntegrationTest.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerTest.java

index b68e35a1a85b09d1b0528739ccdf9d25119c3923..326a1f5443d9c2ab4697acb6cd60471f8ac46d36 100644 (file)
@@ -106,7 +106,7 @@ class RaftActorSnapshotMessageSupport {
     }
 
     private void onApplySnapshot(final ApplySnapshot message) {
-        log.info("{}: Applying snapshot on follower:  {}", context.getId(), message.getSnapshot());
+        log.info("{}: Applying snapshot on follower: {}", context.getId(), message.snapshot());
 
         context.getSnapshotManager().apply(message);
     }
index 488393579be4eab67baac67664d4fa7d26398e3c..69b94db6d44c63bf28fcfdc788a99504e176cf57 100644 (file)
@@ -345,7 +345,7 @@ public class SnapshotManager implements SnapshotState {
 
             log.debug("lastSequenceNumber prior to persisting applied snapshot: {}", lastSequenceNumber);
 
-            context.getPersistenceProvider().saveSnapshot(toApply.getSnapshot());
+            context.getPersistenceProvider().saveSnapshot(toApply.snapshot());
 
             currentState = PERSISTING;
         }
@@ -471,7 +471,7 @@ public class SnapshotManager implements SnapshotState {
 
             if (applySnapshot != null) {
                 try {
-                    Snapshot snapshot = applySnapshot.getSnapshot();
+                    Snapshot snapshot = applySnapshot.snapshot();
 
                     //clears the followers log, sets the snapshot index to ensure adjusted-index works
                     context.setReplicatedLog(ReplicatedLogImpl.newInstance(snapshot, context));
@@ -487,7 +487,7 @@ public class SnapshotManager implements SnapshotState {
                         snapshotCohort.applySnapshot(snapshot.getState());
                     }
 
-                    applySnapshot.getCallback().onSuccess();
+                    applySnapshot.callback().onSuccess();
                 } catch (Exception e) {
                     log.error("{}: Error applying snapshot", context.getId(), e);
                 }
@@ -515,7 +515,7 @@ public class SnapshotManager implements SnapshotState {
                         context.getReplicatedLog().getSnapshotTerm(),
                         context.getReplicatedLog().size());
             } else {
-                applySnapshot.getCallback().onFailure();
+                applySnapshot.callback().onFailure();
             }
 
             snapshotComplete();
index b0fc3876afcc53407ec3f5d00214f7c803f2b9c5..c12c8c6f35bfc784e7b4b66e50f0bc960e3b00b0 100644 (file)
@@ -10,13 +10,22 @@ package org.opendaylight.controller.cluster.raft.base.messages;
 import static java.util.Objects.requireNonNull;
 
 import org.apache.pekko.dispatch.ControlMessage;
-import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
 
 /**
  * Internal message, issued by follower to its actor.
  */
-public class ApplySnapshot implements ControlMessage {
+@NonNullByDefault
+public record ApplySnapshot(Snapshot snapshot, Callback callback) implements ControlMessage {
+
+    public interface Callback {
+
+        void onSuccess();
+
+        void onFailure();
+    }
+
     private static final Callback NOOP_CALLBACK = new Callback() {
         @Override
         public void onSuccess() {
@@ -29,29 +38,12 @@ public class ApplySnapshot implements ControlMessage {
         }
     };
 
-    private final Snapshot snapshot;
-    private final Callback callback;
-
-    public ApplySnapshot(@NonNull Snapshot snapshot) {
-        this(snapshot, NOOP_CALLBACK);
+    public ApplySnapshot {
+        requireNonNull(snapshot);
+        requireNonNull(callback);
     }
 
-    public ApplySnapshot(@NonNull Snapshot snapshot, @NonNull Callback callback) {
-        this.snapshot = requireNonNull(snapshot);
-        this.callback = requireNonNull(callback);
-    }
-
-    public @NonNull Snapshot getSnapshot() {
-        return snapshot;
-    }
-
-    public @NonNull Callback getCallback() {
-        return callback;
-    }
-
-    public interface Callback {
-        void onSuccess();
-
-        void onFailure();
+    public ApplySnapshot(final Snapshot snapshot) {
+        this(snapshot, NOOP_CALLBACK);
     }
 }
index 0d8a7a9fed60f5b6ab420a3e65a58c5d84338d58..ea09e3b6df68061032353f549710bc1498084a74 100644 (file)
@@ -168,7 +168,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest {
         // Leader should install snapshot - capture and verify ApplySnapshot contents
 
         ApplySnapshot applySnapshot = expectFirstMatching(newFollowerCollectorActor, ApplySnapshot.class);
-        List<Object> snapshotState = MockRaftActor.fromState(applySnapshot.getSnapshot().getState());
+        List<Object> snapshotState = MockRaftActor.fromState(applySnapshot.snapshot().getState());
         assertEquals("Snapshot state", snapshotState, leaderRaftActor.getState());
 
         AddServerReply addServerReply = testKit.expectMsgClass(Duration.ofSeconds(5), AddServerReply.class);
@@ -247,7 +247,7 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest {
         // Leader should install snapshot - capture and verify ApplySnapshot contents
 
         ApplySnapshot applySnapshot = expectFirstMatching(newFollowerCollectorActor, ApplySnapshot.class);
-        List<Object> snapshotState = MockRaftActor.fromState(applySnapshot.getSnapshot().getState());
+        List<Object> snapshotState = MockRaftActor.fromState(applySnapshot.snapshot().getState());
         assertEquals("Snapshot state", snapshotState, leaderRaftActor.getState());
 
         AddServerReply addServerReply = testKit.expectMsgClass(Duration.ofSeconds(5), AddServerReply.class);
index ca4a61392ad796f05cab516b91262f64b178a0c8..bd9958058fa81180283f25d492dce2866ad00fb2 100644 (file)
@@ -665,10 +665,10 @@ public class ReplicationAndSnapshotsWithLaggingFollowerIntegrationTest extends A
         // Verify follower 2 applies the snapshot.
         ApplySnapshot applySnapshot = MessageCollectorActor.expectFirstMatching(follower2CollectorActor,
                 ApplySnapshot.class);
-        verifySnapshot("Follower 2", applySnapshot.getSnapshot(), currentTerm, lastAppliedIndex, currentTerm,
+        verifySnapshot("Follower 2", applySnapshot.snapshot(), currentTerm, lastAppliedIndex, currentTerm,
                 lastAppliedIndex);
         assertEquals("Persisted Snapshot getUnAppliedEntries size", 0,
-                applySnapshot.getSnapshot().getUnAppliedEntries().size());
+                applySnapshot.snapshot().getUnAppliedEntries().size());
 
         // Wait for the snapshot to complete.
         MessageCollectorActor.expectFirstMatching(leaderCollectorActor, SaveSnapshotSuccess.class);
@@ -687,7 +687,7 @@ public class ReplicationAndSnapshotsWithLaggingFollowerIntegrationTest extends A
                 Set.copyOf(persistedSnapshot.getServerConfiguration().getServerConfig()));
 
             assertEquals("Follower 2 snapshot server config", expServerInfo,
-                Set.copyOf(applySnapshot.getSnapshot().getServerConfiguration().getServerConfig()));
+                Set.copyOf(applySnapshot.snapshot().getServerConfiguration().getServerConfig()));
 
             ServerConfigurationPayload follower2ServerConfig = follower2Context.getPeerServerInfo(true);
             assertNotNull("Follower 2 server config is null", follower2ServerConfig);
index 1c7b649d8b5c376f2fd4cf396705c9db00f4e484..da69471ba3ad3e361a0255f993c52f6e5a0ad66e 100644 (file)
@@ -821,7 +821,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest<Follower> {
 
         ApplySnapshot applySnapshot = MessageCollectorActor.expectFirstMatching(followerActor,
                 ApplySnapshot.class);
-        Snapshot snapshot = applySnapshot.getSnapshot();
+        Snapshot snapshot = applySnapshot.snapshot();
         assertNotNull(lastInstallSnapshot);
         assertEquals("getLastIndex", lastInstallSnapshot.getLastIncludedIndex(), snapshot.getLastIndex());
         assertEquals("getLastIncludedTerm", lastInstallSnapshot.getLastIncludedTerm(),
@@ -832,7 +832,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest<Follower> {
         assertEquals("getState type", ByteState.class, snapshot.getState().getClass());
         assertArrayEquals("getState", bsSnapshot.toByteArray(), ((ByteState)snapshot.getState()).getBytes());
         assertEquals(new TermInfo(1, "leader"), snapshot.termInfo());
-        applySnapshot.getCallback().onSuccess();
+        applySnapshot.callback().onSuccess();
 
         List<InstallSnapshotReply> replies = MessageCollectorActor.getAllMatching(
                 leaderActor, InstallSnapshotReply.class);