use of org.opendaylight.controller.cluster.datastore.messages.AddShardReplica 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.messages.AddShardReplica 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.messages.AddShardReplica in project controller by opendaylight.
the class ShardManagerTest method testAddShardReplicaWithPreExistingReplicaInRemoteShardLeader.
@Test
public void testAddShardReplicaWithPreExistingReplicaInRemoteShardLeader() throws Exception {
LOG.info("testAddShardReplicaWithPreExistingReplicaInRemoteShardLeader starting");
new TestKit(getSystem()) {
{
TestActorRef<TestShardManager> shardManager = actorFactory.createTestActor(newPropsShardMgrWithMockShardActor(), shardMgrID);
shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager.tell(new ActorInitialized(), mockShardActor);
String leaderId = "leader-member-shard-default-" + shardMrgIDSuffix;
AddServerReply addServerReply = new AddServerReply(ServerChangeStatus.ALREADY_EXISTS, null);
ActorRef leaderShardActor = shardManager.underlyingActor().getContext().actorOf(Props.create(MockRespondActor.class, AddServer.class, addServerReply), leaderId);
MockClusterWrapper.sendMemberUp(shardManager, "leader-member", leaderShardActor.path().toString());
String newReplicaId = "member-1-shard-default-" + shardMrgIDSuffix;
shardManager.tell(new RoleChangeNotification(newReplicaId, RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor);
shardManager.tell(new ShardLeaderStateChanged(newReplicaId, leaderId, DataStoreVersions.CURRENT_VERSION), mockShardActor);
shardManager.tell(new AddShardReplica(Shard.DEFAULT_NAME), getRef());
MessageCollectorActor.expectFirstMatching(leaderShardActor, AddServer.class);
Failure resp = expectMsgClass(duration("5 seconds"), Failure.class);
assertEquals("Failure cause", AlreadyExistsException.class, resp.cause().getClass());
shardManager.tell(new FindLocalShard(Shard.DEFAULT_NAME, false), getRef());
expectMsgClass(duration("5 seconds"), LocalShardFound.class);
// Send message again to verify previous in progress state is
// cleared
shardManager.tell(new AddShardReplica(Shard.DEFAULT_NAME), getRef());
resp = expectMsgClass(duration("5 seconds"), Failure.class);
assertEquals("Failure cause", AlreadyExistsException.class, resp.cause().getClass());
// Send message again with an AddServer timeout to verify the
// pre-existing shard actor isn't terminated.
shardManager.tell(newDatastoreContextFactory(datastoreContextBuilder.shardLeaderElectionTimeout(100, TimeUnit.MILLISECONDS).build()), getRef());
leaderShardActor.tell(MockRespondActor.CLEAR_RESPONSE, ActorRef.noSender());
shardManager.tell(new AddShardReplica(Shard.DEFAULT_NAME), getRef());
expectMsgClass(duration("5 seconds"), Failure.class);
shardManager.tell(new FindLocalShard(Shard.DEFAULT_NAME, false), getRef());
expectMsgClass(duration("5 seconds"), LocalShardFound.class);
}
};
LOG.info("testAddShardReplicaWithPreExistingReplicaInRemoteShardLeader ending");
}
use of org.opendaylight.controller.cluster.datastore.messages.AddShardReplica in project controller by opendaylight.
the class ShardManagerTest method testAddShardReplicaWithPreExistingLocalReplicaLeader.
@Test
public void testAddShardReplicaWithPreExistingLocalReplicaLeader() throws Exception {
LOG.info("testAddShardReplicaWithPreExistingLocalReplicaLeader starting");
new TestKit(getSystem()) {
{
String memberId = "member-1-shard-default-" + shardMrgIDSuffix;
ActorRef shardManager = actorFactory.createActor(newPropsShardMgrWithMockShardActor());
shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager.tell(new ActorInitialized(), mockShardActor);
shardManager.tell(new ShardLeaderStateChanged(memberId, memberId, mock(DataTree.class), DataStoreVersions.CURRENT_VERSION), getRef());
shardManager.tell(new RoleChangeNotification(memberId, RaftState.Candidate.name(), RaftState.Leader.name()), mockShardActor);
shardManager.tell(new AddShardReplica(Shard.DEFAULT_NAME), getRef());
Failure resp = expectMsgClass(duration("5 seconds"), Failure.class);
assertEquals("Failure cause", AlreadyExistsException.class, resp.cause().getClass());
shardManager.tell(new FindLocalShard(Shard.DEFAULT_NAME, false), getRef());
expectMsgClass(duration("5 seconds"), LocalShardFound.class);
}
};
LOG.info("testAddShardReplicaWithPreExistingLocalReplicaLeader ending");
}
use of org.opendaylight.controller.cluster.datastore.messages.AddShardReplica in project controller by opendaylight.
the class ClusterAdminRpcService method addShardReplica.
@Override
public Future<RpcResult<Void>> addShardReplica(final AddShardReplicaInput input) {
final String shardName = input.getShardName();
if (Strings.isNullOrEmpty(shardName)) {
return newFailedRpcResultFuture("A valid shard name must be specified");
}
DataStoreType dataStoreType = input.getDataStoreType();
if (dataStoreType == null) {
return newFailedRpcResultFuture("A valid DataStoreType must be specified");
}
LOG.info("Adding replica for shard {}", shardName);
final SettableFuture<RpcResult<Void>> returnFuture = SettableFuture.create();
ListenableFuture<Success> future = sendMessageToShardManager(dataStoreType, new AddShardReplica(shardName));
Futures.addCallback(future, new FutureCallback<Success>() {
@Override
public void onSuccess(Success success) {
LOG.info("Successfully added replica for shard {}", shardName);
returnFuture.set(newSuccessfulResult());
}
@Override
public void onFailure(Throwable failure) {
onMessageFailure(String.format("Failed to add replica for shard %s", shardName), returnFuture, failure);
}
}, MoreExecutors.directExecutor());
return returnFuture;
}
Aggregations