Search in sources :

Example 1 with MockConfiguration

use of org.opendaylight.controller.cluster.datastore.utils.MockConfiguration in project controller by opendaylight.

the class ThreePhaseCommitCohortProxyTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    actorContext = new ActorContext(getSystem(), actorFactory.createActor(Props.create(DoNothingActor.class)), new MockClusterWrapper(), new MockConfiguration(), DatastoreContext.newBuilder().build(), new PrimaryShardInfoFutureCache()) {

        @Override
        public Timer getOperationTimer(final String operationName) {
            return commitTimer;
        }

        @Override
        public double getTxCreationLimit() {
            return 10.0;
        }
    };
    doReturn(commitTimerContext).when(commitTimer).time();
    doReturn(commitSnapshot).when(commitTimer).getSnapshot();
    for (int i = 1; i < 11; i++) {
        // Keep on increasing the amount of time it takes to complete transaction for each tenth of a
        // percentile. Essentially this would be 1ms for the 10th percentile, 2ms for 20th percentile and so on.
        doReturn(TimeUnit.MILLISECONDS.toNanos(i) * 1D).when(commitSnapshot).getValue(i * 0.1);
    }
}
Also used : Timer(com.codahale.metrics.Timer) DoNothingActor(org.opendaylight.controller.cluster.raft.utils.DoNothingActor) MockClusterWrapper(org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper) PrimaryShardInfoFutureCache(org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) Before(org.junit.Before)

Example 2 with MockConfiguration

use of org.opendaylight.controller.cluster.datastore.utils.MockConfiguration 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");
}
Also used : LocalShardNotFound(org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound) ShardManagerSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot) FindLocalShard(org.opendaylight.controller.cluster.datastore.messages.FindLocalShard) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) TestKit(akka.testkit.javadsl.TestKit) AddressFromURIString(akka.actor.AddressFromURIString) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 3 with MockConfiguration

use of org.opendaylight.controller.cluster.datastore.utils.MockConfiguration 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);
        }
    };
}
Also used : ActorSystem(akka.actor.ActorSystem) FollowerInitialSyncUpStatus(org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus) ChangeShardMembersVotingStatus(org.opendaylight.controller.cluster.datastore.messages.ChangeShardMembersVotingStatus) Status(akka.actor.Status) ServerChangeStatus(org.opendaylight.controller.cluster.raft.messages.ServerChangeStatus) ChangeServersVotingStatus(org.opendaylight.controller.cluster.raft.messages.ChangeServersVotingStatus) ClusterWrapperImpl(org.opendaylight.controller.cluster.datastore.ClusterWrapperImpl) UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) ForwardingActor(org.opendaylight.controller.cluster.datastore.utils.ForwardingActor) ShardLeaderStateChanged(org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) RoleChangeNotification(org.opendaylight.controller.cluster.notifications.RoleChangeNotification) AddressFromURIString(akka.actor.AddressFromURIString) TestKit(akka.testkit.javadsl.TestKit) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) List(java.util.List) ActorInitialized(org.opendaylight.controller.cluster.datastore.messages.ActorInitialized) RemoveShardReplica(org.opendaylight.controller.cluster.datastore.messages.RemoveShardReplica) RemoveServer(org.opendaylight.controller.cluster.raft.messages.RemoveServer) RemoveServerReply(org.opendaylight.controller.cluster.raft.messages.RemoveServerReply) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 4 with MockConfiguration

use of org.opendaylight.controller.cluster.datastore.utils.MockConfiguration 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");
}
Also used : FollowerInitialSyncUpStatus(org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus) ChangeShardMembersVotingStatus(org.opendaylight.controller.cluster.datastore.messages.ChangeShardMembersVotingStatus) Status(akka.actor.Status) ServerChangeStatus(org.opendaylight.controller.cluster.raft.messages.ServerChangeStatus) ChangeServersVotingStatus(org.opendaylight.controller.cluster.raft.messages.ChangeServersVotingStatus) UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) TestKit(akka.testkit.javadsl.TestKit) AddressFromURIString(akka.actor.AddressFromURIString) ShardSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot) ShardManagerSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) AddShardReplica(org.opendaylight.controller.cluster.datastore.messages.AddShardReplica) DatastoreSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot) Failure(akka.actor.Status.Failure) AddServerReply(org.opendaylight.controller.cluster.raft.messages.AddServerReply) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 5 with MockConfiguration

use of org.opendaylight.controller.cluster.datastore.utils.MockConfiguration 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");
}
Also used : UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) ShardManagerSnapshot(org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) TestKit(akka.testkit.javadsl.TestKit) AddressFromURIString(akka.actor.AddressFromURIString) DatastoreSnapshot(org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Aggregations

MockConfiguration (org.opendaylight.controller.cluster.datastore.utils.MockConfiguration)19 AddressFromURIString (akka.actor.AddressFromURIString)17 Test (org.junit.Test)17 TestKit (akka.testkit.javadsl.TestKit)16 AbstractShardManagerTest (org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)16 UpdateSchemaContext (org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext)14 ActorRef (akka.actor.ActorRef)12 TestActorRef (akka.testkit.TestActorRef)11 ActorInitialized (org.opendaylight.controller.cluster.datastore.messages.ActorInitialized)8 RoleChangeNotification (org.opendaylight.controller.cluster.notifications.RoleChangeNotification)7 ActorSystem (akka.actor.ActorSystem)6 ClusterWrapperImpl (org.opendaylight.controller.cluster.datastore.ClusterWrapperImpl)6 ShardLeaderStateChanged (org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged)6 List (java.util.List)5 FollowerInitialSyncUpStatus (org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus)5 Status (akka.actor.Status)4 AddShardReplica (org.opendaylight.controller.cluster.datastore.messages.AddShardReplica)4 ChangeShardMembersVotingStatus (org.opendaylight.controller.cluster.datastore.messages.ChangeShardMembersVotingStatus)4 FindPrimary (org.opendaylight.controller.cluster.datastore.messages.FindPrimary)4 ShardManagerSnapshot (org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot)4