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%2FAbstractRaftActorIntegrationTest.java;h=7cd893691284b7349e4d09efacd9911fdeae4025;hb=3fda1a923defdbf18849c6080c3aa19f1ebf2c5f;hp=977cf0ef5eb60fc905ef7ce35177b46d23ed265b;hpb=2720ff2c662769d1c72e5723c18be4f7d79cd642;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractRaftActorIntegrationTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractRaftActorIntegrationTest.java index 977cf0ef5e..7cd8936912 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractRaftActorIntegrationTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/AbstractRaftActorIntegrationTest.java @@ -8,7 +8,9 @@ package org.opendaylight.controller.cluster.raft; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import akka.actor.ActorRef; +import akka.actor.InvalidActorNameException; import akka.actor.PoisonPill; import akka.actor.Props; import akka.actor.Terminated; @@ -19,6 +21,7 @@ import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.base.Supplier; import com.google.common.collect.ImmutableMap; +import com.google.common.util.concurrent.Uninterruptibles; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -47,6 +50,24 @@ import scala.concurrent.duration.FiniteDuration; */ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest { + public static class SetPeerAddress { + private final String peerId; + private final String peerAddress; + + public SetPeerAddress(String peerId, String peerAddress) { + this.peerId = peerId; + this.peerAddress = peerAddress; + } + + public String getPeerId() { + return peerId; + } + + public String getPeerAddress() { + return peerAddress; + } + } + public static class TestRaftActor extends MockRaftActor { private final TestActorRef collectorActor; @@ -94,6 +115,12 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest return; } + if(message instanceof SetPeerAddress) { + setPeerAddress(((SetPeerAddress) message).getPeerId().toString(), + ((SetPeerAddress) message).getPeerAddress()); + return; + } + try { if(!dropMessages.containsKey(message.getClass())) { super.handleCommand(message); @@ -151,7 +178,9 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest protected long initialTerm = 5; protected long currentTerm; - protected List expSnapshotState = new ArrayList<>(); + protected int snapshotBatchCount = 4; + + protected List expSnapshotState = new ArrayList<>(); @After public void tearDown() { @@ -164,7 +193,7 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl(); configParams.setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS)); configParams.setElectionTimeoutFactor(1); - configParams.setSnapshotBatchCount(4); + configParams.setSnapshotBatchCount(snapshotBatchCount); configParams.setSnapshotDataThresholdPercentage(70); configParams.setIsolatedLeaderCheckInterval(new FiniteDuration(1, TimeUnit.DAYS)); return configParams; @@ -186,9 +215,21 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest TestActorRef collectorActor = factory.createTestActor( MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()), factory.generateActorId(id + "-collector")); - return factory.createTestActor(TestRaftActor.props(id, - peerAddresses != null ? peerAddresses : Collections.emptyMap(), - configParams, collectorActor), id); + + InvalidActorNameException lastEx = null; + for(int i = 0; i < 10; i++) { + try { + return factory.createTestActor(TestRaftActor.props(id, + peerAddresses != null ? peerAddresses : Collections.emptyMap(), + configParams, collectorActor), id); + } catch (InvalidActorNameException e) { + lastEx = e; + Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); + } + } + + assertNotNull(lastEx); + throw lastEx; } protected void killActor(TestActorRef leaderActor) { @@ -212,7 +253,7 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest @SuppressWarnings("unchecked") protected void verifySnapshot(String prefix, Snapshot snapshot, long lastAppliedTerm, - int lastAppliedIndex, long lastTerm, long lastIndex) + long lastAppliedIndex, long lastTerm, long lastIndex) throws Exception { assertEquals(prefix + " Snapshot getLastAppliedTerm", lastAppliedTerm, snapshot.getLastAppliedTerm()); assertEquals(prefix + " Snapshot getLastAppliedIndex", lastAppliedIndex, snapshot.getLastAppliedIndex()); @@ -220,7 +261,8 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest assertEquals(prefix + " Snapshot getLastIndex", lastIndex, snapshot.getLastIndex()); List actualState = (List)MockRaftActor.toObject(snapshot.getState()); - assertEquals(prefix + " Snapshot getState size", expSnapshotState.size(), actualState.size()); + assertEquals(String.format("%s Snapshot getState size. Expected %s: . Actual: %s", prefix, expSnapshotState, + actualState), expSnapshotState.size(), actualState.size()); for(int i = 0; i < expSnapshotState.size(); i++) { assertEquals(prefix + " Snapshot state " + i, expSnapshotState.get(i), actualState.get(i)); } @@ -270,4 +312,32 @@ public abstract class AbstractRaftActorIntegrationTest extends AbstractActorTest protected String testActorPath(String id){ return "akka://test/user" + id; } + + protected void verifyLeadersTrimmedLog(long lastIndex) { + verifyTrimmedLog("Leader", leaderActor, lastIndex, lastIndex - 1); + } + + protected void verifyLeadersTrimmedLog(long lastIndex, long replicatedToAllIndex) { + verifyTrimmedLog("Leader", leaderActor, lastIndex, replicatedToAllIndex); + } + + protected void verifyFollowersTrimmedLog(int num, TestActorRef actorRef, long lastIndex) { + verifyTrimmedLog("Follower " + num, actorRef, lastIndex, lastIndex - 1); + } + + protected void verifyTrimmedLog(String name, TestActorRef actorRef, long lastIndex, + long replicatedToAllIndex) { + TestRaftActor actor = actorRef.underlyingActor(); + RaftActorContext context = actor.getRaftActorContext(); + long snapshotIndex = lastIndex - 1; + assertEquals(name + " snapshot term", snapshotIndex < 0 ? -1 : currentTerm, + context.getReplicatedLog().getSnapshotTerm()); + assertEquals(name + " snapshot index", snapshotIndex, context.getReplicatedLog().getSnapshotIndex()); + assertEquals(name + " journal log size", 1, context.getReplicatedLog().size()); + assertEquals(name + " journal last index", lastIndex, context.getReplicatedLog().lastIndex()); + assertEquals(name + " commit index", lastIndex, context.getCommitIndex()); + assertEquals(name + " last applied", lastIndex, context.getLastApplied()); + assertEquals(name + " replicatedToAllIndex", replicatedToAllIndex, + actor.getCurrentBehavior().getReplicatedToAllIndex()); + } }