X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Fbehaviors%2FFollowerTest.java;h=8006d5a6ac906ed6a11598b59f5c6aba6269f109;hb=HEAD;hp=2ba06d6925efbde5933bc042505d1f39c0cff2ab;hpb=1ffd1f44c4beacdb28683c028bc0eaa209731098;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerTest.java index 2ba06d6925..c0be3eb266 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/behaviors/FollowerTest.java @@ -5,9 +5,9 @@ * 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.behaviors; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -25,22 +25,16 @@ import akka.dispatch.Dispatchers; import akka.protobuf.ByteString; import akka.testkit.TestActorRef; import akka.testkit.javadsl.TestKit; -import com.google.common.base.Optional; import com.google.common.base.Stopwatch; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.google.common.io.ByteSource; import com.google.common.util.concurrent.Uninterruptibles; import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import org.junit.After; -import org.junit.Assert; import org.junit.Test; import org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl; import org.opendaylight.controller.cluster.raft.MockRaftActor; @@ -158,7 +152,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { Uninterruptibles.sleepUninterruptibly(context.getConfigParams() .getElectionTimeOutInterval().toMillis() - 100, TimeUnit.MILLISECONDS); - follower.handleMessage(leaderActor, new AppendEntries(1, "leader", -1, -1, Collections.emptyList(), + follower.handleMessage(leaderActor, new AppendEntries(1, "leader", -1, -1, List.of(), -1, -1, (short) 1)); Uninterruptibles.sleepUninterruptibly(130, TimeUnit.MILLISECONDS); @@ -167,7 +161,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { Uninterruptibles.sleepUninterruptibly(context.getConfigParams() .getElectionTimeOutInterval().toMillis() - 150, TimeUnit.MILLISECONDS); - follower.handleMessage(leaderActor, new AppendEntries(1, "leader", -1, -1, Collections.emptyList(), + follower.handleMessage(leaderActor, new AppendEntries(1, "leader", -1, -1, List.of(), -1, -1, (short) 1)); Uninterruptibles.sleepUninterruptibly(200, TimeUnit.MILLISECONDS); @@ -222,10 +216,9 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { context.getReplicatedLog().append(newReplicatedLogEntry(1,100, "bar")); context.getReplicatedLog().setSnapshotIndex(99); - List entries = Arrays.asList( - newReplicatedLogEntry(2, 101, "foo")); + List entries = List.of(newReplicatedLogEntry(2, 101, "foo")); - Assert.assertEquals(1, context.getReplicatedLog().size()); + assertEquals(1, context.getReplicatedLog().size()); // The new commitIndex is 101 AppendEntries appendEntries = new AppendEntries(2, "leader-1", 100, 1, entries, 101, 100, (short)0); @@ -247,8 +240,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { MockRaftActorContext context = createActorContext(); - List entries = Arrays.asList( - newReplicatedLogEntry(2, 101, "foo")); + List entries = List.of(newReplicatedLogEntry(2, 101, "foo")); // The new commitIndex is 101 AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 101, 100, (short) 0); @@ -273,8 +265,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { context.getReplicatedLog().append(newReplicatedLogEntry(1, 100, "bar")); context.getReplicatedLog().setSnapshotIndex(99); - List entries = Arrays.asList( - newReplicatedLogEntry(2, 101, "foo")); + List entries = List.of(newReplicatedLogEntry(2, 101, "foo")); // The new commitIndex is 101 AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 101, 100, (short) 0); @@ -298,8 +289,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { context.getReplicatedLog().clear(0,2); context.getReplicatedLog().setSnapshotIndex(100); - List entries = Arrays.asList( - newReplicatedLogEntry(2, 101, "foo")); + List entries = List.of(newReplicatedLogEntry(2, 101, "foo")); // The new commitIndex is 101 AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 101, 100, (short) 0); @@ -324,8 +314,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { context.getReplicatedLog().clear(0,2); context.getReplicatedLog().setSnapshotIndex(100); - List entries = Arrays.asList( - newReplicatedLogEntry(2, 105, "foo")); + List entries = List.of(newReplicatedLogEntry(2, 105, "foo")); // The new commitIndex is 101 AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 105, 100, (short) 0); @@ -347,8 +336,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { MockRaftActorContext context = createActorContext(); - List entries = Arrays.asList( - newReplicatedLogEntry(2, 101, "foo")); + List entries = List.of(newReplicatedLogEntry(2, 101, "foo")); // The new commitIndex is 101 AppendEntries appendEntries = new AppendEntries(2, "leader-1", 100, 1, entries, 101, 100, (short)0); @@ -368,7 +356,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { context.setCommitIndex(101); setLastLogEntry(context, 1, 101, new MockRaftActorContext.MockPayload("")); - entries = Arrays.asList(newReplicatedLogEntry(2, 101, "foo")); + entries = List.of(newReplicatedLogEntry(2, 101, "foo")); // The new commitIndex is 101 appendEntries = new AppendEntries(2, "leader-1", 101, 1, entries, 102, 101, (short)0); @@ -395,8 +383,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { MockRaftActorContext context = createActorContext(); - List entries = Arrays.asList( - newReplicatedLogEntry(2, 101, "foo")); + List entries = List.of(newReplicatedLogEntry(2, 101, "foo")); // The new commitIndex is 101 AppendEntries appendEntries = new AppendEntries(2, "leader-1", 100, 1, entries, 101, 100, (short)0); @@ -416,8 +403,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { setLastLogEntry(context, 1, 100, new MockRaftActorContext.MockPayload("")); - entries = Arrays.asList( - newReplicatedLogEntry(2, 101, "foo")); + entries = List.of(newReplicatedLogEntry(2, 101, "foo")); // leader-2 is becoming the leader now and it says the commitIndex is 45 appendEntries = new AppendEntries(2, "leader-2", 45, 1, entries, 46, 100, (short)0); @@ -435,8 +421,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { MockRaftActorContext context = createActorContext(); - List entries = Arrays.asList( - newReplicatedLogEntry(2, 101, "foo")); + List entries = List.of(newReplicatedLogEntry(2, 101, "foo")); // The new commitIndex is 101 AppendEntries appendEntries = new AppendEntries(2, "leader-1", 100, 1, entries, 101, 100, (short)0); @@ -457,8 +442,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { setLastLogEntry(context, 1, 101, new MockRaftActorContext.MockPayload("")); - entries = Arrays.asList( - newReplicatedLogEntry(2, 101, "foo")); + entries = List.of(newReplicatedLogEntry(2, 101, "foo")); // The new commitIndex is 101 appendEntries = new AppendEntries(2, "leader-1", 101, 1, entries, 102, 101, (short)0); @@ -475,8 +459,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { setLastLogEntry(context, 1, 100, new MockRaftActorContext.MockPayload("")); - entries = Arrays.asList( - newReplicatedLogEntry(2, 101, "foo")); + entries = List.of(newReplicatedLogEntry(2, 101, "foo")); // leader-2 is becoming the leader now and it says the commitIndex is 45 appendEntries = new AppendEntries(2, "leader-2", 45, 1, entries, 46, 100, (short)0); @@ -505,8 +488,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { new MockRaftActorContext.MockPayload("")); context.getReplicatedLog().setSnapshotIndex(99); - List entries = Arrays.asList( - newReplicatedLogEntry(2, 101, "foo")); + List entries = List.of(newReplicatedLogEntry(2, 101, "foo")); // The new commitIndex is 101 AppendEntries appendEntries = new AppendEntries(2, "leader-1", 100, 1, entries, 101, 100, (short)0); @@ -528,13 +510,13 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { MockRaftActorContext context = createActorContext(); - AppendEntries appendEntries = new AppendEntries(2, "leader", 0, 2, Collections.emptyList(), 101, -1, (short)0); + AppendEntries appendEntries = new AppendEntries(2, "leader", 0, 2, List.of(), 101, -1, (short)0); follower = createBehavior(context); RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries); - Assert.assertSame(follower, newBehavior); + assertSame(follower, newBehavior); AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class); @@ -551,13 +533,13 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { context.getReplicatedLog().setSnapshotIndex(4); context.getReplicatedLog().setSnapshotTerm(3); - AppendEntries appendEntries = new AppendEntries(3, "leader", 1, 3, Collections.emptyList(), 8, -1, (short)0); + AppendEntries appendEntries = new AppendEntries(3, "leader", 1, 3, List.of(), 8, -1, (short)0); follower = createBehavior(context); RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries); - Assert.assertSame(follower, newBehavior); + assertSame(follower, newBehavior); AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class); @@ -588,9 +570,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { context.setReplicatedLog(log); // Prepare the entries to be sent with AppendEntries - List entries = new ArrayList<>(); - entries.add(newReplicatedLogEntry(1, 3, "three")); - entries.add(newReplicatedLogEntry(1, 4, "four")); + List entries = List.of( + newReplicatedLogEntry(1, 3, "three"), newReplicatedLogEntry(1, 4, "four")); // Send appendEntries with the same term as was set on the receiver // before the new behavior was created (1 in this case) @@ -604,9 +585,9 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries); - Assert.assertSame(follower, newBehavior); + assertSame(follower, newBehavior); - assertEquals("Next index", 5, log.last().getIndex() + 1); + assertEquals("Next index", 5, log.last().index() + 1); assertEquals("Entry 3", entries.get(0), log.get(3)); assertEquals("Entry 4", entries.get(1), log.get(4)); @@ -640,9 +621,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { context.setReplicatedLog(log); // Prepare the entries to be sent with AppendEntries - List entries = new ArrayList<>(); - entries.add(newReplicatedLogEntry(2, 2, "two-1")); - entries.add(newReplicatedLogEntry(2, 3, "three")); + List entries = List.of( + newReplicatedLogEntry(2, 2, "two-1"), newReplicatedLogEntry(2, 3, "three")); // Send appendEntries with the same term as was set on the receiver // before the new behavior was created (1 in this case) @@ -654,13 +634,13 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries); - Assert.assertSame(follower, newBehavior); + assertSame(follower, newBehavior); // 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().getIndex() + 1); + assertEquals("Next index", 4, log.last().index() + 1); //assertEquals("Entry 2", entries.get(0), log.get(2)); assertEquals("Entry 1 data", "one", log.get(1).getData().toString()); @@ -691,9 +671,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { context.setReplicatedLog(log); // Prepare the entries to be sent with AppendEntries - List entries = new ArrayList<>(); - entries.add(newReplicatedLogEntry(2, 2, "two-1")); - entries.add(newReplicatedLogEntry(2, 3, "three")); + List entries = List.of( + newReplicatedLogEntry(2, 2, "two-1"), newReplicatedLogEntry(2, 3, "three")); // Send appendEntries with the same term as was set on the receiver // before the new behavior was created (1 in this case) @@ -706,7 +685,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries); - Assert.assertSame(follower, newBehavior); + assertSame(follower, newBehavior); expectAndVerifyAppendEntriesReply(2, false, context.getId(), 1, 2, true); } @@ -726,8 +705,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { context.setReplicatedLog(log); // Prepare the entries to be sent with AppendEntries - List entries = new ArrayList<>(); - entries.add(newReplicatedLogEntry(1, 4, "four")); + List entries = List.of(newReplicatedLogEntry(1, 4, "four")); AppendEntries appendEntries = new AppendEntries(1, "leader", 3, 1, entries, 4, -1, (short)0); @@ -735,7 +713,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries); - Assert.assertSame(follower, newBehavior); + assertSame(follower, newBehavior); expectAndVerifyAppendEntriesReply(1, false, context.getId(), 1, 2); } @@ -756,25 +734,25 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { context.setReplicatedLog(log); // Send the last entry again. - List entries = Arrays.asList(newReplicatedLogEntry(1, 1, "one")); + List entries = List.of(newReplicatedLogEntry(1, 1, "one")); follower = createBehavior(context); follower.handleMessage(leaderActor, new AppendEntries(1, "leader", 0, 1, entries, 1, -1, (short)0)); - assertEquals("Next index", 2, log.last().getIndex() + 1); + assertEquals("Next index", 2, log.last().index() + 1); assertEquals("Entry 1", entries.get(0), log.get(1)); expectAndVerifyAppendEntriesReply(1, true, context.getId(), 1, 1); // Send the last entry again and also a new one. - entries = Arrays.asList(newReplicatedLogEntry(1, 1, "one"), newReplicatedLogEntry(1, 2, "two")); + entries = List.of(newReplicatedLogEntry(1, 1, "one"), newReplicatedLogEntry(1, 2, "two")); MessageCollectorActor.clearMessages(leaderActor); follower.handleMessage(leaderActor, new AppendEntries(1, "leader", 0, 1, entries, 2, -1, (short)0)); - assertEquals("Next index", 3, log.last().getIndex() + 1); + assertEquals("Next index", 3, log.last().index() + 1); assertEquals("Entry 1", entries.get(0), log.get(1)); assertEquals("Entry 2", entries.get(1), log.get(2)); @@ -797,8 +775,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { context.setReplicatedLog(log); // Prepare the entries to be sent with AppendEntries - List entries = new ArrayList<>(); - entries.add(newReplicatedLogEntry(1, 4, "four")); + List entries = List.of(newReplicatedLogEntry(1, 4, "four")); AppendEntries appendEntries = new AppendEntries(1, "leader", 3, 1, entries, 4, 3, (short)0); @@ -806,7 +783,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries); - Assert.assertSame(follower, newBehavior); + assertSame(follower, newBehavior); expectAndVerifyAppendEntriesReply(1, true, context.getId(), 1, 4); } @@ -854,7 +831,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { snapshot.getLastAppliedIndex()); assertEquals("getLastTerm", lastInstallSnapshot.getLastIncludedTerm(), snapshot.getLastTerm()); assertEquals("getState type", ByteState.class, snapshot.getState().getClass()); - Assert.assertArrayEquals("getState", bsSnapshot.toByteArray(), ((ByteState)snapshot.getState()).getBytes()); + assertArrayEquals("getState", bsSnapshot.toByteArray(), ((ByteState)snapshot.getState()).getBytes()); assertEquals("getElectionTerm", 1, snapshot.getElectionTerm()); assertEquals("getElectionVotedFor", "leader", snapshot.getElectionVotedFor()); applySnapshot.getCallback().onSuccess(); @@ -908,7 +885,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { // Send an append entry AppendEntries appendEntries = new AppendEntries(1, "leader", 1, 1, - Arrays.asList(newReplicatedLogEntry(2, 1, "3")), 2, -1, (short)1); + List.of(newReplicatedLogEntry(2, 1, "3")), 2, -1, (short)1); follower.handleMessage(leaderActor, appendEntries); @@ -951,7 +928,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { // Send appendEntries with a new term and leader. AppendEntries appendEntries = new AppendEntries(2, "new-leader", 1, 1, - Arrays.asList(newReplicatedLogEntry(2, 2, "3")), 2, -1, (short)1); + List.of(newReplicatedLogEntry(2, 2, "3")), 2, -1, (short)1); follower.handleMessage(leaderActor, appendEntries); @@ -1005,8 +982,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { setLastLogEntry(context, 1, 101, new MockRaftActorContext.MockPayload("")); - List entries = Arrays.asList( - newReplicatedLogEntry(2, 101, "foo")); + List entries = List.of(newReplicatedLogEntry(2, 101, "foo")); // The new commitIndex is 101 AppendEntries appendEntries = new AppendEntries(2, "leader", 101, 1, entries, 102, 101, (short)0); @@ -1082,7 +1058,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { @Test public void testFollowerSchedulesElectionIfNonVoting() { MockRaftActorContext context = createActorContext(); - context.updatePeerIds(new ServerConfigurationPayload(Arrays.asList(new ServerInfo(context.getId(), false)))); + context.updatePeerIds(new ServerConfigurationPayload(List.of(new ServerInfo(context.getId(), false)))); ((DefaultConfigParamsImpl)context.getConfigParams()).setHeartBeatInterval( FiniteDuration.apply(100, TimeUnit.MILLISECONDS)); ((DefaultConfigParamsImpl)context.getConfigParams()).setElectionTimeoutFactor(1); @@ -1133,7 +1109,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { final AtomicReference followerRaftActor = new AtomicReference<>(); RaftActorSnapshotCohort snapshotCohort = newRaftActorSnapshotCohort(followerRaftActor); Builder builder = MockRaftActor.builder().persistent(Optional.of(true)).id(id) - .peerAddresses(ImmutableMap.of("leader", "")).config(config).snapshotCohort(snapshotCohort); + .peerAddresses(Map.of("leader", "")).config(config).snapshotCohort(snapshotCohort); TestActorRef followerActorRef = actorFactory.createTestActor(builder.props() .withDispatcher(Dispatchers.DefaultDispatcherId()), id); followerRaftActor.set(followerActorRef.underlyingActor()); @@ -1143,7 +1119,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { InMemoryJournal.addDeleteMessagesCompleteLatch(id); InMemoryJournal.addWriteMessagesCompleteLatch(id, 1, ApplyJournalEntries.class); - List entries = Arrays.asList( + List entries = List.of( newReplicatedLogEntry(1, 0, "one"), newReplicatedLogEntry(1, 1, "two")); AppendEntries appendEntries = new AppendEntries(1, "leader", -1, -1, entries, 1, -1, (short)0); @@ -1170,7 +1146,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { assertEquals("Snapshot getLastAppliedIndex", 1, snapshot.getLastAppliedIndex()); assertEquals("Snapshot getLastTerm", 1, snapshot.getLastTerm()); assertEquals("Snapshot getLastIndex", 1, snapshot.getLastIndex()); - assertEquals("Snapshot state", ImmutableList.of(entries.get(0).getData(), entries.get(1).getData()), + assertEquals("Snapshot state", List.of(entries.get(0).getData(), entries.get(1).getData()), MockRaftActor.fromState(snapshot.getState())); } @@ -1188,7 +1164,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { final AtomicReference followerRaftActor = new AtomicReference<>(); RaftActorSnapshotCohort snapshotCohort = newRaftActorSnapshotCohort(followerRaftActor); Builder builder = MockRaftActor.builder().persistent(Optional.of(true)).id(id) - .peerAddresses(ImmutableMap.of("leader", "")).config(config).snapshotCohort(snapshotCohort); + .peerAddresses(Map.of("leader", "")).config(config).snapshotCohort(snapshotCohort); TestActorRef followerActorRef = actorFactory.createTestActor(builder.props() .withDispatcher(Dispatchers.DefaultDispatcherId()), id); followerRaftActor.set(followerActorRef.underlyingActor()); @@ -1198,7 +1174,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { InMemoryJournal.addDeleteMessagesCompleteLatch(id); InMemoryJournal.addWriteMessagesCompleteLatch(id, 1, ApplyJournalEntries.class); - List entries = Arrays.asList( + List entries = List.of( newReplicatedLogEntry(1, 0, "one"), newReplicatedLogEntry(1, 1, "two"), newReplicatedLogEntry(1, 2, "three")); @@ -1226,7 +1202,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { assertEquals("Snapshot getLastAppliedIndex", 2, snapshot.getLastAppliedIndex()); assertEquals("Snapshot getLastTerm", 1, snapshot.getLastTerm()); assertEquals("Snapshot getLastIndex", 2, snapshot.getLastIndex()); - assertEquals("Snapshot state", ImmutableList.of(entries.get(0).getData(), entries.get(1).getData(), + assertEquals("Snapshot state", List.of(entries.get(0).getData(), entries.get(1).getData(), entries.get(2).getData()), MockRaftActor.fromState(snapshot.getState())); assertEquals("Journal size", 0, followerRaftActor.get().getReplicatedLog().size()); @@ -1245,7 +1221,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { assertEquals("Last index", 2, followerRaftActor.get().getReplicatedLog().lastIndex()); assertEquals("Last applied index", 2, followerRaftActor.get().getRaftActorContext().getLastApplied()); assertEquals("Commit index", 2, followerRaftActor.get().getRaftActorContext().getCommitIndex()); - assertEquals("State", ImmutableList.of(entries.get(0).getData(), entries.get(1).getData(), + assertEquals("State", List.of(entries.get(0).getData(), entries.get(1).getData(), entries.get(2).getData()), followerRaftActor.get().getState()); } @@ -1263,7 +1239,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { final AtomicReference followerRaftActor = new AtomicReference<>(); RaftActorSnapshotCohort snapshotCohort = newRaftActorSnapshotCohort(followerRaftActor); Builder builder = MockRaftActor.builder().persistent(Optional.of(true)).id(id) - .peerAddresses(ImmutableMap.of("leader", "")).config(config).snapshotCohort(snapshotCohort); + .peerAddresses(Map.of("leader", "")).config(config).snapshotCohort(snapshotCohort); TestActorRef followerActorRef = actorFactory.createTestActor(builder.props() .withDispatcher(Dispatchers.DefaultDispatcherId()), id); followerRaftActor.set(followerActorRef.underlyingActor()); @@ -1273,7 +1249,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { InMemoryJournal.addDeleteMessagesCompleteLatch(id); InMemoryJournal.addWriteMessagesCompleteLatch(id, 1, ApplyJournalEntries.class); - List entries = Arrays.asList( + List entries = List.of( newReplicatedLogEntry(1, 0, "one"), newReplicatedLogEntry(1, 1, "two"), newReplicatedLogEntry(1, 2, "three")); @@ -1297,13 +1273,13 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { assertEquals("ApplyJournalEntries index", 0, ((ApplyJournalEntries)journalEntries.get(0)).getToIndex()); assertEquals("Snapshot unapplied size", 2, snapshot.getUnAppliedEntries().size()); - assertEquals("Snapshot unapplied entry index", 1, snapshot.getUnAppliedEntries().get(0).getIndex()); - assertEquals("Snapshot unapplied entry index", 2, snapshot.getUnAppliedEntries().get(1).getIndex()); + assertEquals("Snapshot unapplied entry index", 1, snapshot.getUnAppliedEntries().get(0).index()); + assertEquals("Snapshot unapplied entry index", 2, snapshot.getUnAppliedEntries().get(1).index()); assertEquals("Snapshot getLastAppliedTerm", 1, snapshot.getLastAppliedTerm()); assertEquals("Snapshot getLastAppliedIndex", 0, snapshot.getLastAppliedIndex()); assertEquals("Snapshot getLastTerm", 1, snapshot.getLastTerm()); assertEquals("Snapshot getLastIndex", 2, snapshot.getLastIndex()); - assertEquals("Snapshot state", ImmutableList.of(entries.get(0).getData()), + assertEquals("Snapshot state", List.of(entries.get(0).getData()), MockRaftActor.fromState(snapshot.getState())); } @@ -1319,7 +1295,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { follower = createBehavior(context); follower.handleMessage(leaderActor, - new AppendEntries(1, "leader", -1, -1, Collections.emptyList(), -1, -1, (short)0)); + new AppendEntries(1, "leader", -1, -1, List.of(), -1, -1, (short)0)); AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class); assertTrue(reply.isNeedsLeaderAddress()); @@ -1328,7 +1304,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { PeerAddressResolver mockResolver = mock(PeerAddressResolver.class); ((DefaultConfigParamsImpl)context.getConfigParams()).setPeerAddressResolver(mockResolver); - follower.handleMessage(leaderActor, new AppendEntries(1, "leader", -1, -1, Collections.emptyList(), -1, -1, + follower.handleMessage(leaderActor, new AppendEntries(1, "leader", -1, -1, List.of(), -1, -1, (short)0, RaftVersions.CURRENT_VERSION, leaderActor.path().toString())); reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class); @@ -1342,8 +1318,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { final AtomicReference followerRaftActor) { RaftActorSnapshotCohort snapshotCohort = new RaftActorSnapshotCohort() { @Override - public void createSnapshot(final ActorRef actorRef, - final java.util.Optional installSnapshotStream) { + public void createSnapshot(final ActorRef actorRef, final Optional installSnapshotStream) { try { actorRef.tell(new CaptureSnapshotReply(new MockSnapshotState(followerRaftActor.get().getState()), installSnapshotStream), actorRef); @@ -1372,10 +1347,8 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { int size = chunkSize; if (chunkSize > snapshotLength) { size = snapshotLength; - } else { - if (start + chunkSize > snapshotLength) { - size = snapshotLength - start; - } + } else if (start + chunkSize > snapshotLength) { + size = snapshotLength - start; } byte[] nextChunk = new byte[size]; @@ -1412,12 +1385,7 @@ public class FollowerTest extends AbstractRaftActorBehaviorTest { } private ByteString createSnapshot() { - HashMap followerSnapshot = new HashMap<>(); - followerSnapshot.put("1", "A"); - followerSnapshot.put("2", "B"); - followerSnapshot.put("3", "C"); - - return toByteString(followerSnapshot); + return toByteString(Map.of("1", "A", "2", "B", "3", "C")); } @Override