use of akka.testkit.TestActorRef in project controller by opendaylight.
the class MigratedMessagesTest method testNoSnapshotAfterStartupWithNoMigratedMessages.
@Test
public void testNoSnapshotAfterStartupWithNoMigratedMessages() {
TEST_LOG.info("testNoSnapshotAfterStartupWithNoMigratedMessages starting");
String id = factory.generateActorId("test-actor-");
InMemoryJournal.addEntry(id, 1, new UpdateElectionTerm(1, id));
InMemoryJournal.addEntry(id, 2, new SimpleReplicatedLogEntry(0, 1, new MockRaftActorContext.MockPayload("A")));
InMemoryJournal.addEntry(id, 3, new ApplyJournalEntries(0));
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(ByteState.empty(), installSnapshotStream), actorRef);
}
@Override
public void applySnapshot(Snapshot.State snapshotState) {
}
@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(true)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), id);
MockRaftActor mockRaftActor = raftActorRef.underlyingActor();
mockRaftActor.waitForRecoveryComplete();
Uninterruptibles.sleepUninterruptibly(750, TimeUnit.MILLISECONDS);
List<Snapshot> snapshots = InMemorySnapshotStore.getSnapshots(id, Snapshot.class);
assertEquals("Snapshots", 0, snapshots.size());
TEST_LOG.info("testNoSnapshotAfterStartupWithNoMigratedMessages ending");
}
use of akka.testkit.TestActorRef in project controller by opendaylight.
the class ShardManagerTest method testRemoveShardReplicaRemote.
@Test
public void testRemoveShardReplicaRemote() throws Exception {
MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("default", Arrays.asList("member-1", "member-2")).put("astronauts", Arrays.asList("member-1")).build());
String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
// Create an ActorSystem ShardManager actor for member-1.
final ActorSystem system1 = newActorSystem("Member1");
Cluster.get(system1).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
ActorRef mockDefaultShardActor = newMockShardActor(system1, Shard.DEFAULT_NAME, "member-1");
final TestActorRef<TestShardManager> newReplicaShardManager = TestActorRef.create(system1, newTestShardMgrBuilder().configuration(mockConfig).shardActor(mockDefaultShardActor).cluster(new ClusterWrapperImpl(system1)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
// Create an ActorSystem ShardManager actor for member-2.
final ActorSystem system2 = newActorSystem("Member2");
Cluster.get(system2).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
String name = ShardIdentifier.create("default", MEMBER_2, shardMrgIDSuffix).toString();
String memberId2 = "member-2-shard-default-" + shardMrgIDSuffix;
final TestActorRef<MockRespondActor> mockShardLeaderActor = TestActorRef.create(system2, Props.create(MockRespondActor.class, RemoveServer.class, new RemoveServerReply(ServerChangeStatus.OK, memberId2)), name);
LOG.error("Mock Shard Leader Actor : {}", mockShardLeaderActor);
final TestActorRef<TestShardManager> leaderShardManager = TestActorRef.create(system2, newTestShardMgrBuilder().configuration(mockConfig).shardActor(mockShardLeaderActor).cluster(new ClusterWrapperImpl(system2)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
// Because mockShardLeaderActor is created at the top level of the actor system it has an address like so,
// akka://cluster-test@127.0.0.1:2559/user/member-2-shard-default-config1
// However when a shard manager has a local shard which is a follower and a leader that is remote it will
// try to compute an address for the remote shard leader using the ShardPeerAddressResolver. This address will
// look like so,
// akka://cluster-test@127.0.0.1:2559/user/shardmanager-config1/member-2-shard-default-config1
// In this specific case if we did a FindPrimary for shard default from member-1 we would come up
// with the address of an actor which does not exist, therefore any message sent to that actor would go to
// dead letters.
// To work around this problem we create a ForwardingActor with the right address and pass to it the
// mockShardLeaderActor. The ForwardingActor simply forwards all messages to the mockShardLeaderActor and every
// thing works as expected
final ActorRef actorRef = leaderShardManager.underlyingActor().context().actorOf(Props.create(ForwardingActor.class, mockShardLeaderActor), "member-2-shard-default-" + shardMrgIDSuffix);
LOG.error("Forwarding actor : {}", actorRef);
new TestKit(system1) {
{
newReplicaShardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
leaderShardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
leaderShardManager.tell(new ActorInitialized(), mockShardLeaderActor);
newReplicaShardManager.tell(new ActorInitialized(), mockShardLeaderActor);
short leaderVersion = DataStoreVersions.CURRENT_VERSION - 1;
leaderShardManager.tell(new ShardLeaderStateChanged(memberId2, memberId2, mock(DataTree.class), leaderVersion), mockShardLeaderActor);
leaderShardManager.tell(new RoleChangeNotification(memberId2, RaftState.Candidate.name(), RaftState.Leader.name()), mockShardLeaderActor);
String memberId1 = "member-1-shard-default-" + shardMrgIDSuffix;
newReplicaShardManager.tell(new ShardLeaderStateChanged(memberId1, memberId2, mock(DataTree.class), leaderVersion), mockShardActor);
newReplicaShardManager.tell(new RoleChangeNotification(memberId1, RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor);
newReplicaShardManager.underlyingActor().waitForMemberUp();
leaderShardManager.underlyingActor().waitForMemberUp();
// construct a mock response message
newReplicaShardManager.tell(new RemoveShardReplica("default", MEMBER_1), getRef());
RemoveServer removeServer = MessageCollectorActor.expectFirstMatching(mockShardLeaderActor, RemoveServer.class);
String removeServerId = ShardIdentifier.create("default", MEMBER_1, shardMrgIDSuffix).toString();
assertEquals("RemoveServer serverId", removeServerId, removeServer.getServerId());
expectMsgClass(duration("5 seconds"), Status.Success.class);
}
};
}
use of akka.testkit.TestActorRef in project controller by opendaylight.
the class ShardManagerTest method testOnReceiveFindPrimaryForRemoteShard.
@Test
public void testOnReceiveFindPrimaryForRemoteShard() throws Exception {
LOG.info("testOnReceiveFindPrimaryForRemoteShard starting");
String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
// Create an ActorSystem ShardManager actor for member-1.
final ActorSystem system1 = newActorSystem("Member1");
Cluster.get(system1).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
final TestActorRef<TestShardManager> shardManager1 = TestActorRef.create(system1, newTestShardMgrBuilderWithMockShardActor().cluster(new ClusterWrapperImpl(system1)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
// Create an ActorSystem ShardManager actor for member-2.
final ActorSystem system2 = newActorSystem("Member2");
Cluster.get(system2).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
final ActorRef mockShardActor2 = newMockShardActor(system2, "astronauts", "member-2");
MockConfiguration mockConfig2 = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("default", Arrays.asList("member-1", "member-2")).put("astronauts", Arrays.asList("member-2")).build());
final TestActorRef<TestShardManager> shardManager2 = TestActorRef.create(system2, newTestShardMgrBuilder(mockConfig2).shardActor(mockShardActor2).cluster(new ClusterWrapperImpl(system2)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
new TestKit(system1) {
{
shardManager1.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager2.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager2.tell(new ActorInitialized(), mockShardActor2);
String memberId2 = "member-2-shard-astronauts-" + shardMrgIDSuffix;
short leaderVersion = DataStoreVersions.CURRENT_VERSION - 1;
shardManager2.tell(new ShardLeaderStateChanged(memberId2, memberId2, mock(DataTree.class), leaderVersion), mockShardActor2);
shardManager2.tell(new RoleChangeNotification(memberId2, RaftState.Candidate.name(), RaftState.Leader.name()), mockShardActor2);
shardManager1.underlyingActor().waitForMemberUp();
shardManager1.tell(new FindPrimary("astronauts", false), getRef());
RemotePrimaryShardFound found = expectMsgClass(duration("5 seconds"), RemotePrimaryShardFound.class);
String path = found.getPrimaryPath();
assertTrue("Unexpected primary path " + path, path.contains("member-2-shard-astronauts-config"));
assertEquals("getPrimaryVersion", leaderVersion, found.getPrimaryVersion());
shardManager2.underlyingActor().verifyFindPrimary();
// This part times out quite a bit on jenkins for some reason
// Cluster.get(system2).down(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
//
// shardManager1.underlyingActor().waitForMemberRemoved();
//
// shardManager1.tell(new FindPrimary("astronauts", false), getRef());
//
// expectMsgClass(duration("5 seconds"), PrimaryNotFoundException.class);
}
};
LOG.info("testOnReceiveFindPrimaryForRemoteShard ending");
}
use of akka.testkit.TestActorRef in project controller by opendaylight.
the class ShardManagerTest method testServerRemovedShardActorRunning.
@Test
public void testServerRemovedShardActorRunning() throws Exception {
LOG.info("testServerRemovedShardActorRunning starting");
new TestKit(getSystem()) {
{
MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("default", Arrays.asList("member-1", "member-2")).put("astronauts", Arrays.asList("member-2")).put("people", Arrays.asList("member-1", "member-2")).build());
String shardId = ShardIdentifier.create("default", MEMBER_1, shardMrgIDSuffix).toString();
ActorRef shard = actorFactory.createActor(MessageCollectorActor.props(), shardId);
TestActorRef<TestShardManager> shardManager = actorFactory.createTestActor(newTestShardMgrBuilder(mockConfig).addShardActor("default", shard).props().withDispatcher(Dispatchers.DefaultDispatcherId()));
shardManager.underlyingActor().waitForRecoveryComplete();
shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager.tell(new ActorInitialized(), shard);
waitForShardInitialized(shardManager, "people", this);
waitForShardInitialized(shardManager, "default", this);
// Removed the default shard replica from member-1
shardManager.tell(new ServerRemoved(shardId), getRef());
shardManager.underlyingActor().verifySnapshotPersisted(Sets.newHashSet("people"));
MessageCollectorActor.expectFirstMatching(shard, Shutdown.class);
}
};
LOG.info("testServerRemovedShardActorRunning ending");
}
use of akka.testkit.TestActorRef in project controller by opendaylight.
the class ShardManagerTest method testAddShardReplica.
@Test
public void testAddShardReplica() throws Exception {
LOG.info("testAddShardReplica starting");
MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("default", Arrays.asList("member-1", "member-2")).put("astronauts", Arrays.asList("member-2")).build());
final String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
datastoreContextBuilder.shardManagerPersistenceId(shardManagerID);
// Create an ActorSystem ShardManager actor for member-1.
final ActorSystem system1 = newActorSystem("Member1");
Cluster.get(system1).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
ActorRef mockDefaultShardActor = newMockShardActor(system1, Shard.DEFAULT_NAME, "member-1");
final TestActorRef<TestShardManager> newReplicaShardManager = TestActorRef.create(system1, newTestShardMgrBuilder(mockConfig).shardActor(mockDefaultShardActor).cluster(new ClusterWrapperImpl(system1)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
// Create an ActorSystem ShardManager actor for member-2.
final ActorSystem system2 = newActorSystem("Member2");
Cluster.get(system2).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
String memberId2 = "member-2-shard-astronauts-" + shardMrgIDSuffix;
String name = ShardIdentifier.create("astronauts", MEMBER_2, "config").toString();
final TestActorRef<MockRespondActor> mockShardLeaderActor = TestActorRef.create(system2, Props.create(MockRespondActor.class, AddServer.class, new AddServerReply(ServerChangeStatus.OK, memberId2)).withDispatcher(Dispatchers.DefaultDispatcherId()), name);
final TestActorRef<TestShardManager> leaderShardManager = TestActorRef.create(system2, newTestShardMgrBuilder(mockConfig).shardActor(mockShardLeaderActor).cluster(new ClusterWrapperImpl(system2)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
new TestKit(system1) {
{
newReplicaShardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
leaderShardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
leaderShardManager.tell(new ActorInitialized(), mockShardLeaderActor);
short leaderVersion = DataStoreVersions.CURRENT_VERSION - 1;
leaderShardManager.tell(new ShardLeaderStateChanged(memberId2, memberId2, mock(DataTree.class), leaderVersion), mockShardLeaderActor);
leaderShardManager.tell(new RoleChangeNotification(memberId2, RaftState.Candidate.name(), RaftState.Leader.name()), mockShardLeaderActor);
newReplicaShardManager.underlyingActor().waitForMemberUp();
leaderShardManager.underlyingActor().waitForMemberUp();
// Have a dummy snapshot to be overwritten by the new data
// persisted.
String[] restoredShards = { "default", "people" };
ShardManagerSnapshot snapshot = new ShardManagerSnapshot(Arrays.asList(restoredShards), Collections.emptyMap());
InMemorySnapshotStore.addSnapshot(shardManagerID, snapshot);
Uninterruptibles.sleepUninterruptibly(2, TimeUnit.MILLISECONDS);
InMemorySnapshotStore.addSnapshotSavedLatch(shardManagerID);
InMemorySnapshotStore.addSnapshotDeletedLatch(shardManagerID);
// construct a mock response message
newReplicaShardManager.tell(new AddShardReplica("astronauts"), getRef());
AddServer addServerMsg = MessageCollectorActor.expectFirstMatching(mockShardLeaderActor, AddServer.class);
String addServerId = "member-1-shard-astronauts-" + shardMrgIDSuffix;
assertEquals("AddServer serverId", addServerId, addServerMsg.getNewServerId());
expectMsgClass(duration("5 seconds"), Status.Success.class);
InMemorySnapshotStore.waitForSavedSnapshot(shardManagerID, ShardManagerSnapshot.class);
InMemorySnapshotStore.waitForDeletedSnapshot(shardManagerID);
List<ShardManagerSnapshot> persistedSnapshots = InMemorySnapshotStore.getSnapshots(shardManagerID, ShardManagerSnapshot.class);
assertEquals("Number of snapshots persisted", 1, persistedSnapshots.size());
ShardManagerSnapshot shardManagerSnapshot = persistedSnapshots.get(0);
assertEquals("Persisted local shards", Sets.newHashSet("default", "astronauts"), Sets.newHashSet(shardManagerSnapshot.getShardList()));
}
};
LOG.info("testAddShardReplica ending");
}
Aggregations