Reduce use of ReplicatedLog.last() 07/116007/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 22 Mar 2025 22:38:32 +0000 (23:38 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 22 Mar 2025 22:46:20 +0000 (23:46 +0100)
We have a few call sites which are accessing the entry, whereas they
only need its metadata. Update them to not call last().

JIRA: CONTROLLER-2137
Change-Id: If13079a9a7d95cffb70a0befb877d8dcfb859fcd
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/AbstractReplicatedLog.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerTest.java

index c9a4ced4434e58e752679f00ef8e27292a639563..9c893ea1cb92006837a350dc28bf87fd723afefa 100644 (file)
@@ -77,7 +77,7 @@ public abstract class AbstractReplicatedLog implements ReplicatedLog {
 
     @Override
     public final long lastIndex() {
-        final var last = last();
+        final var last = lastMeta();
         // it can happen that after snapshot, all the entries of the journal are trimmed till lastApplied,
         // so lastIndex = snapshotIndex
         return last != null ? last.index() : snapshotIndex;
@@ -85,7 +85,7 @@ public abstract class AbstractReplicatedLog implements ReplicatedLog {
 
     @Override
     public final long lastTerm() {
-        final var last = last();
+        final var last = lastMeta();
         // it can happen that after snapshot, all the entries of the journal are trimmed till lastApplied,
         // so lastTerm = snapshotTerm
         return last != null ? last.term() : snapshotTerm;
@@ -131,7 +131,7 @@ public abstract class AbstractReplicatedLog implements ReplicatedLog {
     }
 
     @Override
-    public boolean append(final ReplicatedLogEntry replicatedLogEntry) {
+    public final boolean append(final ReplicatedLogEntry replicatedLogEntry) {
         final var entryIndex = replicatedLogEntry.index();
         final var lastIndex = lastIndex();
         if (entryIndex > lastIndex) {
index 3336bf2a666ffdc7d1f1280a533725272c47df87..de10c77b46875334595ab4690ec102b0676469df 100644 (file)
@@ -583,7 +583,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest<Follower> {
 
         assertSame(follower, newBehavior);
 
-        assertEquals("Next index", 5, log.last().index() + 1);
+        assertEquals("Next index", 4, log.lastIndex());
         assertEquals("Entry 3", entries.get(0), log.get(3));
         assertEquals("Entry 4", entries.get(1), log.get(4));
 
@@ -632,11 +632,10 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest<Follower> {
 
         assertSame(follower, newBehavior);
 
-        // The entry at index 2 will be found out-of-sync with the leader
-        // and will be removed
+        // The entry at index 2 will be found out-of-sync with the leader and will be removed
         // Then the two new entries will be added to the log
         // Thus making the log to have 4 entries
-        assertEquals("Next index", 4, log.last().index() + 1);
+        assertEquals("Next index", 3, log.lastIndex());
         //assertEquals("Entry 2", entries.get(0), log.get(2));
 
         assertEquals("Entry 1 data", "one", log.get(1).getData().toString());
@@ -736,7 +735,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest<Follower> {
 
         follower.handleMessage(leaderActor, new AppendEntries(1, "leader", 0, 1, entries, 1, -1, (short)0));
 
-        assertEquals("Next index", 2, log.last().index() + 1);
+        assertEquals("Next index", 1, log.lastIndex());
         assertEquals("Entry 1", entries.get(0), log.get(1));
 
         expectAndVerifyAppendEntriesReply(1, true, "follower", 1, 1);