use of akka.testkit.TestActorRef in project controller by opendaylight.
the class RecoveryIntegrationSingleNodeTest method testJournalReplayAfterSnapshotWithSingleNode.
@Test
public void testJournalReplayAfterSnapshotWithSingleNode() throws Exception {
String persistenceId = factory.generateActorId("singleNode");
TestActorRef<AbstractRaftActorIntegrationTest.TestRaftActor> singleNodeActorRef = newTestRaftActor(persistenceId, ImmutableMap.<String, String>builder().build(), leaderConfigParams);
waitUntilLeader(singleNodeActorRef);
ActorRef singleNodeCollectorActor = singleNodeActorRef.underlyingActor().collectorActor();
final RaftActorContext singleNodeContext = singleNodeActorRef.underlyingActor().getRaftActorContext();
InMemoryJournal.addWriteMessagesCompleteLatch(persistenceId, 6, ApplyJournalEntries.class);
final MockRaftActorContext.MockPayload payload0 = sendPayloadData(singleNodeActorRef, "zero");
final MockRaftActorContext.MockPayload payload1 = sendPayloadData(singleNodeActorRef, "one");
final MockRaftActorContext.MockPayload payload2 = sendPayloadData(singleNodeActorRef, "two");
MessageCollectorActor.expectMatching(singleNodeCollectorActor, ApplyJournalEntries.class, 3);
// this should trigger a snapshot
final MockRaftActorContext.MockPayload payload3 = sendPayloadData(singleNodeActorRef, "three");
MessageCollectorActor.expectMatching(singleNodeCollectorActor, ApplyJournalEntries.class, 4);
// add 2 more
final MockRaftActorContext.MockPayload payload4 = sendPayloadData(singleNodeActorRef, "four");
final MockRaftActorContext.MockPayload payload5 = sendPayloadData(singleNodeActorRef, "five");
// Wait for snapshot complete.
MessageCollectorActor.expectFirstMatching(singleNodeCollectorActor, SaveSnapshotSuccess.class);
MessageCollectorActor.expectMatching(singleNodeCollectorActor, ApplyJournalEntries.class, 6);
assertEquals("Last applied", 5, singleNodeContext.getLastApplied());
assertEquals("Incorrect State after snapshot success is received ", Lists.newArrayList(payload0, payload1, payload2, payload3, payload4, payload5), singleNodeActorRef.underlyingActor().getState());
InMemoryJournal.waitForWriteMessagesComplete(persistenceId);
// we get 2 log entries (4 and 5 indexes) and 3 ApplyJournalEntries (for 3, 4, and 5 indexes)
assertEquals(5, InMemoryJournal.get(persistenceId).size());
List<Snapshot> persistedSnapshots = InMemorySnapshotStore.getSnapshots(persistenceId, Snapshot.class);
assertEquals(1, persistedSnapshots.size());
List<Object> snapshottedState = MockRaftActor.fromState(persistedSnapshots.get(0).getState());
assertEquals("Incorrect Snapshot", Lists.newArrayList(payload0, payload1, payload2, payload3), snapshottedState);
// recovery logic starts
killActor(singleNodeActorRef);
singleNodeActorRef = newTestRaftActor(persistenceId, ImmutableMap.<String, String>builder().build(), leaderConfigParams);
singleNodeActorRef.underlyingActor().waitForRecoveryComplete();
assertEquals("Incorrect State after Recovery ", Lists.newArrayList(payload0, payload1, payload2, payload3, payload4, payload5), singleNodeActorRef.underlyingActor().getState());
}
use of akka.testkit.TestActorRef in project controller by opendaylight.
the class AbstractLeaderTest method testLeaderSchedulesHeartbeatsEvenWhenNoFollowersRespondToInitialAppendEntries.
/**
* When we removed scheduling of heartbeat in the AbstractLeader constructor we ended up with a situation where
* if no follower responded to an initial AppendEntries heartbeats would not be sent to it. This test verifies
* that regardless of whether followers respond or not we schedule heartbeats.
*/
@Test
public void testLeaderSchedulesHeartbeatsEvenWhenNoFollowersRespondToInitialAppendEntries() throws Exception {
logStart("testLeaderSchedulesHeartbeatsEvenWhenNoFollowersRespondToInitialAppendEntries");
String leaderActorId = actorFactory.generateActorId("leader");
String follower1ActorId = actorFactory.generateActorId("follower");
String follower2ActorId = actorFactory.generateActorId("follower");
TestActorRef<ForwardMessageToBehaviorActor> leaderActor = actorFactory.createTestActor(ForwardMessageToBehaviorActor.props(), leaderActorId);
final ActorRef follower1Actor = actorFactory.createActor(MessageCollectorActor.props(), follower1ActorId);
final ActorRef follower2Actor = actorFactory.createActor(MessageCollectorActor.props(), follower2ActorId);
MockRaftActorContext leaderActorContext = new MockRaftActorContext(leaderActorId, getSystem(), leaderActor);
DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
configParams.setHeartBeatInterval(new FiniteDuration(200, TimeUnit.MILLISECONDS));
configParams.setIsolatedLeaderCheckInterval(new FiniteDuration(10, TimeUnit.SECONDS));
leaderActorContext.setConfigParams(configParams);
leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(1, 5, 1).build());
Map<String, String> peerAddresses = new HashMap<>();
peerAddresses.put(follower1ActorId, follower1Actor.path().toString());
peerAddresses.put(follower2ActorId, follower2Actor.path().toString());
leaderActorContext.setPeerAddresses(peerAddresses);
RaftActorBehavior leader = createBehavior(leaderActorContext);
leaderActor.underlyingActor().setBehavior(leader);
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
List<SendHeartBeat> allMessages = MessageCollectorActor.getAllMatching(leaderActor, SendHeartBeat.class);
// Need more than 1 heartbeat to be delivered because we waited for 1 second with heartbeat interval 200ms
assertTrue(String.format("%s messages is less than expected", allMessages.size()), allMessages.size() > 1);
}
use of akka.testkit.TestActorRef in project controller by opendaylight.
the class MigratedMessagesTest method doTestSnapshotAfterStartupWithMigratedMessage.
@SuppressWarnings("checkstyle:IllegalCatch")
private TestActorRef<MockRaftActor> doTestSnapshotAfterStartupWithMigratedMessage(String id, boolean persistent, Consumer<Snapshot> snapshotVerifier, final State snapshotState) {
InMemorySnapshotStore.addSnapshotSavedLatch(id);
InMemoryJournal.addDeleteMessagesCompleteLatch(id);
DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
RaftActorSnapshotCohort snapshotCohort = new RaftActorSnapshotCohort() {
@Override
public void createSnapshot(ActorRef actorRef, java.util.Optional<OutputStream> installSnapshotStream) {
actorRef.tell(new CaptureSnapshotReply(snapshotState, installSnapshotStream), actorRef);
}
@Override
public void applySnapshot(State newState) {
}
@Override
public State deserializeSnapshot(ByteSource snapshotBytes) {
throw new UnsupportedOperationException();
}
};
TestActorRef<MockRaftActor> raftActorRef = factory.createTestActor(MockRaftActor.builder().id(id).config(config).snapshotCohort(snapshotCohort).persistent(Optional.of(persistent)).peerAddresses(ImmutableMap.of("peer", "")).props().withDispatcher(Dispatchers.DefaultDispatcherId()), id);
MockRaftActor mockRaftActor = raftActorRef.underlyingActor();
mockRaftActor.waitForRecoveryComplete();
Snapshot snapshot = InMemorySnapshotStore.waitForSavedSnapshot(id, Snapshot.class);
snapshotVerifier.accept(snapshot);
InMemoryJournal.waitForDeleteMessagesComplete(id);
assertEquals("InMemoryJournal size", 0, InMemoryJournal.get(id).size());
return raftActorRef;
}
use of akka.testkit.TestActorRef in project controller by opendaylight.
the class ShardManagerTest method testShardAvailabilityChangeOnMemberWithNameContainedInLeaderIdUnreachable.
@Test
public void testShardAvailabilityChangeOnMemberWithNameContainedInLeaderIdUnreachable() throws Exception {
LOG.info("testShardAvailabilityChangeOnMemberWithNameContainedInLeaderIdUnreachable starting");
String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("default", Arrays.asList("member-256", "member-2")).build());
// Create an ActorSystem, ShardManager and actor for member-256.
final ActorSystem system256 = newActorSystem("Member256");
// 2562 is the tcp port of Member256 in src/test/resources/application.conf.
Cluster.get(system256).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2562"));
final ActorRef mockShardActor256 = newMockShardActor(system256, Shard.DEFAULT_NAME, "member-256");
final PrimaryShardInfoFutureCache primaryShardInfoCache = new PrimaryShardInfoFutureCache();
// ShardManager must be created with shard configuration to let its localShards has shards.
final TestActorRef<TestShardManager> shardManager256 = TestActorRef.create(system256, newTestShardMgrBuilder(mockConfig).shardActor(mockShardActor256).cluster(new ClusterWrapperImpl(system256)).primaryShardInfoCache(primaryShardInfoCache).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
// Create an ActorSystem, ShardManager and actor for member-2 whose name is contained in member-256.
final ActorSystem system2 = newActorSystem("Member2");
// Join member-2 into the cluster of member-256.
Cluster.get(system2).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2562"));
final ActorRef mockShardActor2 = newMockShardActor(system2, Shard.DEFAULT_NAME, "member-2");
final TestActorRef<TestShardManager> shardManager2 = TestActorRef.create(system2, newTestShardMgrBuilder(mockConfig).shardActor(mockShardActor2).cluster(new ClusterWrapperImpl(system2)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
new TestKit(system256) {
{
shardManager256.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager2.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager256.tell(new ActorInitialized(), mockShardActor256);
shardManager2.tell(new ActorInitialized(), mockShardActor2);
String memberId256 = "member-256-shard-default-" + shardMrgIDSuffix;
String memberId2 = "member-2-shard-default-" + shardMrgIDSuffix;
shardManager256.tell(new ShardLeaderStateChanged(memberId256, memberId256, mock(DataTree.class), DataStoreVersions.CURRENT_VERSION), mockShardActor256);
shardManager256.tell(new RoleChangeNotification(memberId256, RaftState.Candidate.name(), RaftState.Leader.name()), mockShardActor256);
shardManager2.tell(new ShardLeaderStateChanged(memberId2, memberId256, mock(DataTree.class), DataStoreVersions.CURRENT_VERSION), mockShardActor2);
shardManager2.tell(new RoleChangeNotification(memberId2, RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor2);
shardManager256.underlyingActor().waitForMemberUp();
shardManager256.tell(new FindPrimary("default", true), getRef());
LocalPrimaryShardFound found = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
String path = found.getPrimaryPath();
assertTrue("Unexpected primary path " + path + " which must on member-256", path.contains("member-256-shard-default-config"));
PrimaryShardInfo primaryShardInfo = new PrimaryShardInfo(system256.actorSelection(mockShardActor256.path()), DataStoreVersions.CURRENT_VERSION);
primaryShardInfoCache.putSuccessful("default", primaryShardInfo);
// Simulate member-2 become unreachable.
shardManager256.tell(MockClusterWrapper.createUnreachableMember("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
shardManager256.underlyingActor().waitForUnreachableMember();
// Make sure leader shard on member-256 is still leader and still in the cache.
shardManager256.tell(new FindPrimary("default", true), getRef());
found = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
path = found.getPrimaryPath();
assertTrue("Unexpected primary path " + path + " which must still not on member-256", path.contains("member-256-shard-default-config"));
Future<PrimaryShardInfo> futurePrimaryShard = primaryShardInfoCache.getIfPresent("default");
futurePrimaryShard.onComplete(new OnComplete<PrimaryShardInfo>() {
@Override
public void onComplete(final Throwable failure, final PrimaryShardInfo futurePrimaryShardInfo) {
if (failure != null) {
assertTrue("Primary shard info is unexpectedly removed from primaryShardInfoCache", false);
} else {
assertEquals("Expected primaryShardInfoCache entry", primaryShardInfo, futurePrimaryShardInfo);
}
}
}, system256.dispatchers().defaultGlobalDispatcher());
}
};
LOG.info("testShardAvailabilityChangeOnMemberWithNameContainedInLeaderIdUnreachable ending");
}
use of akka.testkit.TestActorRef in project controller by opendaylight.
the class ShardManagerTest method testAddShardReplicaWithAddServerReplyFailure.
@Test
public void testAddShardReplicaWithAddServerReplyFailure() throws Exception {
LOG.info("testAddShardReplicaWithAddServerReplyFailure starting");
new TestKit(getSystem()) {
{
TestKit mockShardLeaderKit = new TestKit(getSystem());
MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("astronauts", Arrays.asList("member-2")).build());
ActorRef mockNewReplicaShardActor = newMockShardActor(getSystem(), "astronauts", "member-1");
final TestActorRef<TestShardManager> shardManager = actorFactory.createTestActor(newTestShardMgrBuilder(mockConfig).shardActor(mockNewReplicaShardActor).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardMgrID);
shardManager.underlyingActor().setMessageInterceptor(newFindPrimaryInterceptor(mockShardLeaderKit.getRef()));
shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
TestKit terminateWatcher = new TestKit(getSystem());
terminateWatcher.watch(mockNewReplicaShardActor);
shardManager.tell(new AddShardReplica("astronauts"), getRef());
AddServer addServerMsg = mockShardLeaderKit.expectMsgClass(AddServer.class);
assertEquals("AddServer serverId", "member-1-shard-astronauts-" + shardMrgIDSuffix, addServerMsg.getNewServerId());
mockShardLeaderKit.reply(new AddServerReply(ServerChangeStatus.TIMEOUT, null));
Failure failure = expectMsgClass(duration("5 seconds"), Failure.class);
assertEquals("Failure cause", TimeoutException.class, failure.cause().getClass());
shardManager.tell(new FindLocalShard("astronauts", false), getRef());
expectMsgClass(duration("5 seconds"), LocalShardNotFound.class);
terminateWatcher.expectTerminated(mockNewReplicaShardActor);
shardManager.tell(new AddShardReplica("astronauts"), getRef());
mockShardLeaderKit.expectMsgClass(AddServer.class);
mockShardLeaderKit.reply(new AddServerReply(ServerChangeStatus.NO_LEADER, null));
failure = expectMsgClass(duration("5 seconds"), Failure.class);
assertEquals("Failure cause", NoShardLeaderException.class, failure.cause().getClass());
}
};
LOG.info("testAddShardReplicaWithAddServerReplyFailure ending");
}
Aggregations