use of org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot in project controller by opendaylight.
the class ShardManagerTest method testShardPersistenceWithRestoredData.
@Test
public void testShardPersistenceWithRestoredData() throws Exception {
LOG.info("testShardPersistenceWithRestoredData 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[] restoredShards = { "default", "astronauts" };
ShardManagerSnapshot snapshot = new ShardManagerSnapshot(Arrays.asList(restoredShards), Collections.emptyMap());
InMemorySnapshotStore.addSnapshot("shard-manager-" + shardMrgIDSuffix, snapshot);
// create shardManager to come up with restored data
TestActorRef<TestShardManager> newRestoredShardManager = actorFactory.createTestActor(newShardMgrProps(mockConfig).withDispatcher(Dispatchers.DefaultDispatcherId()));
newRestoredShardManager.underlyingActor().waitForRecoveryComplete();
newRestoredShardManager.tell(new FindLocalShard("people", false), getRef());
LocalShardNotFound notFound = expectMsgClass(duration("5 seconds"), LocalShardNotFound.class);
assertEquals("for uninitialized shard", "people", notFound.getShardName());
// Verify a local shard is created for the restored shards,
// although we expect a NotInitializedException for the shards
// as the actor initialization
// message is not sent for them
newRestoredShardManager.tell(new FindLocalShard("default", false), getRef());
expectMsgClass(duration("5 seconds"), NotInitializedException.class);
newRestoredShardManager.tell(new FindLocalShard("astronauts", false), getRef());
expectMsgClass(duration("5 seconds"), NotInitializedException.class);
}
};
LOG.info("testShardPersistenceWithRestoredData ending");
}
use of org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot in project controller by opendaylight.
the class ShardManagerTest method testGetSnapshot.
@Test
public void testGetSnapshot() throws Exception {
LOG.info("testGetSnapshot starting");
TestKit kit = new TestKit(getSystem());
MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("shard1", Arrays.asList("member-1")).put("shard2", Arrays.asList("member-1")).put("astronauts", Collections.<String>emptyList()).build());
TestActorRef<TestShardManager> shardManager = actorFactory.createTestActor(newShardMgrProps(mockConfig).withDispatcher(Dispatchers.DefaultDispatcherId()));
shardManager.tell(GetSnapshot.INSTANCE, kit.getRef());
Failure failure = kit.expectMsgClass(Failure.class);
assertEquals("Failure cause type", IllegalStateException.class, failure.cause().getClass());
shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), ActorRef.noSender());
waitForShardInitialized(shardManager, "shard1", kit);
waitForShardInitialized(shardManager, "shard2", kit);
shardManager.tell(GetSnapshot.INSTANCE, kit.getRef());
DatastoreSnapshot datastoreSnapshot = expectMsgClassOrFailure(DatastoreSnapshot.class, kit, "GetSnapshot");
assertEquals("getType", shardMrgIDSuffix, datastoreSnapshot.getType());
assertNull("Expected null ShardManagerSnapshot", datastoreSnapshot.getShardManagerSnapshot());
Function<ShardSnapshot, String> shardNameTransformer = ShardSnapshot::getName;
assertEquals("Shard names", Sets.newHashSet("shard1", "shard2"), Sets.newHashSet(Lists.transform(datastoreSnapshot.getShardSnapshots(), shardNameTransformer)));
// Add a new replica
TestKit mockShardLeaderKit = new TestKit(getSystem());
TestShardManager shardManagerInstance = shardManager.underlyingActor();
shardManagerInstance.setMessageInterceptor(newFindPrimaryInterceptor(mockShardLeaderKit.getRef()));
shardManager.tell(new AddShardReplica("astronauts"), kit.getRef());
mockShardLeaderKit.expectMsgClass(AddServer.class);
mockShardLeaderKit.reply(new AddServerReply(ServerChangeStatus.OK, ""));
kit.expectMsgClass(Status.Success.class);
waitForShardInitialized(shardManager, "astronauts", kit);
// Send another GetSnapshot and verify
shardManager.tell(GetSnapshot.INSTANCE, kit.getRef());
datastoreSnapshot = expectMsgClassOrFailure(DatastoreSnapshot.class, kit, "GetSnapshot");
assertEquals("Shard names", Sets.newHashSet("shard1", "shard2", "astronauts"), Sets.newHashSet(Lists.transform(datastoreSnapshot.getShardSnapshots(), shardNameTransformer)));
ShardManagerSnapshot snapshot = datastoreSnapshot.getShardManagerSnapshot();
assertNotNull("Expected ShardManagerSnapshot", snapshot);
assertEquals("Shard names", Sets.newHashSet("shard1", "shard2", "astronauts"), Sets.newHashSet(snapshot.getShardList()));
LOG.info("testGetSnapshot ending");
}
use of org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot in project controller by opendaylight.
the class ShardManagerTest method testRestoreFromSnapshot.
@Test
public void testRestoreFromSnapshot() throws Exception {
LOG.info("testRestoreFromSnapshot starting");
datastoreContextBuilder.shardInitializationTimeout(3, TimeUnit.SECONDS);
TestKit kit = new TestKit(getSystem());
MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("shard1", Collections.<String>emptyList()).put("shard2", Collections.<String>emptyList()).put("astronauts", Collections.<String>emptyList()).build());
ShardManagerSnapshot snapshot = new ShardManagerSnapshot(Arrays.asList("shard1", "shard2", "astronauts"), Collections.emptyMap());
DatastoreSnapshot restoreFromSnapshot = new DatastoreSnapshot(shardMrgIDSuffix, snapshot, Collections.<ShardSnapshot>emptyList());
TestActorRef<TestShardManager> shardManager = actorFactory.createTestActor(newTestShardMgrBuilder(mockConfig).restoreFromSnapshot(restoreFromSnapshot).props().withDispatcher(Dispatchers.DefaultDispatcherId()));
shardManager.underlyingActor().waitForRecoveryComplete();
shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), ActorRef.noSender());
waitForShardInitialized(shardManager, "shard1", kit);
waitForShardInitialized(shardManager, "shard2", kit);
waitForShardInitialized(shardManager, "astronauts", kit);
shardManager.tell(GetSnapshot.INSTANCE, kit.getRef());
DatastoreSnapshot datastoreSnapshot = expectMsgClassOrFailure(DatastoreSnapshot.class, kit, "GetSnapshot");
assertEquals("getType", shardMrgIDSuffix, datastoreSnapshot.getType());
assertNotNull("Expected ShardManagerSnapshot", datastoreSnapshot.getShardManagerSnapshot());
assertEquals("Shard names", Sets.newHashSet("shard1", "shard2", "astronauts"), Sets.newHashSet(datastoreSnapshot.getShardManagerSnapshot().getShardList()));
LOG.info("testRestoreFromSnapshot ending");
}
use of org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot 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");
}
use of org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot in project controller by opendaylight.
the class ShardManagerGetSnapshotReplyActorTest method testSuccess.
@Test
public void testSuccess() {
TestKit kit = new TestKit(getSystem());
List<String> shardList = Arrays.asList("shard1", "shard2", "shard3");
ShardManagerSnapshot shardManagerSnapshot = new ShardManagerSnapshot(shardList, Collections.emptyMap());
ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(shardList, "config", shardManagerSnapshot, kit.getRef(), "shard-manager", Duration.create(100, TimeUnit.SECONDS)), "testSuccess");
kit.watch(replyActor);
ByteState shard1SnapshotState = ByteState.of(new byte[] { 1, 2, 3 });
replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(), Snapshot.create(shard1SnapshotState, Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
ByteState shard2SnapshotState = ByteState.of(new byte[] { 4, 5, 6 });
replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard2", MEMBER_1, "config").toString(), Snapshot.create(shard2SnapshotState, Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
ByteState shard3SnapshotState = ByteState.of(new byte[] { 7, 8, 9 });
replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard3", MEMBER_1, "config").toString(), Snapshot.create(shard3SnapshotState, Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
DatastoreSnapshot datastoreSnapshot = kit.expectMsgClass(DatastoreSnapshot.class);
assertEquals("getType", "config", datastoreSnapshot.getType());
assertEquals("getShardManagerSnapshot", shardManagerSnapshot.getShardList(), datastoreSnapshot.getShardManagerSnapshot().getShardList());
List<ShardSnapshot> shardSnapshots = datastoreSnapshot.getShardSnapshots();
assertEquals("ShardSnapshot size", 3, shardSnapshots.size());
assertEquals("ShardSnapshot 1 getName", "shard1", shardSnapshots.get(0).getName());
assertEquals("ShardSnapshot 1 getSnapshot", shard1SnapshotState, shardSnapshots.get(0).getSnapshot().getState());
assertEquals("ShardSnapshot 2 getName", "shard2", shardSnapshots.get(1).getName());
assertEquals("ShardSnapshot 2 getSnapshot", shard2SnapshotState, shardSnapshots.get(1).getSnapshot().getState());
assertEquals("ShardSnapshot 3 getName", "shard3", shardSnapshots.get(2).getName());
assertEquals("ShardSnapshot 3 getSnapshot", shard3SnapshotState, shardSnapshots.get(2).getSnapshot().getState());
kit.expectMsgClass(Terminated.class);
}
Aggregations