Search in sources :

Example 6 with LocalPrimaryShardFound

use of org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound in project controller by opendaylight.

the class ShardManagerTest method testOnReceiveFindPrimaryForLocalLeaderShard.

@Test
public void testOnReceiveFindPrimaryForLocalLeaderShard() throws Exception {
    LOG.info("testOnReceiveFindPrimaryForLocalLeaderShard starting");
    new TestKit(getSystem()) {

        {
            String memberId = "member-1-shard-default-" + shardMrgIDSuffix;
            final ActorRef shardManager = actorFactory.createActor(newPropsShardMgrWithMockShardActor());
            shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
            shardManager.tell(new ActorInitialized(), mockShardActor);
            DataTree mockDataTree = mock(DataTree.class);
            shardManager.tell(new ShardLeaderStateChanged(memberId, memberId, mockDataTree, DataStoreVersions.CURRENT_VERSION), getRef());
            MessageCollectorActor.expectFirstMatching(mockShardActor, RegisterRoleChangeListener.class);
            shardManager.tell(new RoleChangeNotification(memberId, RaftState.Candidate.name(), RaftState.Leader.name()), mockShardActor);
            shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
            LocalPrimaryShardFound primaryFound = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
            assertTrue("Unexpected primary path " + primaryFound.getPrimaryPath(), primaryFound.getPrimaryPath().contains("member-1-shard-default"));
            assertSame("getLocalShardDataTree", mockDataTree, primaryFound.getLocalShardDataTree());
        }
    };
    LOG.info("testOnReceiveFindPrimaryForLocalLeaderShard ending");
}
Also used : UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) FindPrimary(org.opendaylight.controller.cluster.datastore.messages.FindPrimary) LocalPrimaryShardFound(org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound) DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) ShardLeaderStateChanged(org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) RoleChangeNotification(org.opendaylight.controller.cluster.notifications.RoleChangeNotification) ActorInitialized(org.opendaylight.controller.cluster.datastore.messages.ActorInitialized) TestKit(akka.testkit.javadsl.TestKit) AddressFromURIString(akka.actor.AddressFromURIString) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 7 with LocalPrimaryShardFound

use of org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound in project controller by opendaylight.

the class ShardManagerTest method testOnReceiveFindPrimaryForFollowerShardWithNoInitialLeaderId.

@Test
public void testOnReceiveFindPrimaryForFollowerShardWithNoInitialLeaderId() throws Exception {
    LOG.info("testOnReceiveFindPrimaryForFollowerShardWithNoInitialLeaderId starting");
    new TestKit(getSystem()) {

        {
            final ActorRef shardManager = actorFactory.createActor(newPropsShardMgrWithMockShardActor());
            shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
            shardManager.tell(new ActorInitialized(), mockShardActor);
            String memberId = "member-1-shard-default-" + shardMrgIDSuffix;
            shardManager.tell(new RoleChangeNotification(memberId, RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor);
            shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
            expectMsgClass(duration("5 seconds"), NoShardLeaderException.class);
            DataTree mockDataTree = mock(DataTree.class);
            shardManager.tell(new ShardLeaderStateChanged(memberId, memberId, mockDataTree, DataStoreVersions.CURRENT_VERSION), mockShardActor);
            shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
            LocalPrimaryShardFound primaryFound = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
            assertTrue("Unexpected primary path " + primaryFound.getPrimaryPath(), primaryFound.getPrimaryPath().contains("member-1-shard-default"));
            assertSame("getLocalShardDataTree", mockDataTree, primaryFound.getLocalShardDataTree());
        }
    };
    LOG.info("testOnReceiveFindPrimaryForFollowerShardWithNoInitialLeaderId starting");
}
Also used : UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) FindPrimary(org.opendaylight.controller.cluster.datastore.messages.FindPrimary) LocalPrimaryShardFound(org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound) DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) ShardLeaderStateChanged(org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) RoleChangeNotification(org.opendaylight.controller.cluster.notifications.RoleChangeNotification) ActorInitialized(org.opendaylight.controller.cluster.datastore.messages.ActorInitialized) TestKit(akka.testkit.javadsl.TestKit) AddressFromURIString(akka.actor.AddressFromURIString) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 8 with LocalPrimaryShardFound

use of org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound in project controller by opendaylight.

the class ActorContextTest method testFindPrimaryShardAsyncLocalPrimaryFound.

@Test
public void testFindPrimaryShardAsyncLocalPrimaryFound() throws Exception {
    ActorRef shardManager = getSystem().actorOf(MessageCollectorActor.props());
    DatastoreContext dataStoreContext = DatastoreContext.newBuilder().logicalStoreType(LogicalDatastoreType.CONFIGURATION).shardLeaderElectionTimeout(100, TimeUnit.MILLISECONDS).build();
    final DataTree mockDataTree = Mockito.mock(DataTree.class);
    final String expPrimaryPath = "akka://test-system/find-primary-shard";
    ActorContext actorContext = new ActorContext(getSystem(), shardManager, mock(ClusterWrapper.class), mock(Configuration.class), dataStoreContext, new PrimaryShardInfoFutureCache()) {

        @Override
        protected Future<Object> doAsk(final ActorRef actorRef, final Object message, final Timeout timeout) {
            return Futures.successful((Object) new LocalPrimaryShardFound(expPrimaryPath, mockDataTree));
        }
    };
    Future<PrimaryShardInfo> foobar = actorContext.findPrimaryShardAsync("foobar");
    PrimaryShardInfo actual = Await.result(foobar, Duration.apply(5000, TimeUnit.MILLISECONDS));
    assertNotNull(actual);
    assertEquals("LocalShardDataTree present", true, actual.getLocalShardDataTree().isPresent());
    assertSame("LocalShardDataTree", mockDataTree, actual.getLocalShardDataTree().get());
    assertTrue("Unexpected PrimaryShardActor path " + actual.getPrimaryShardActor().path(), expPrimaryPath.endsWith(actual.getPrimaryShardActor().pathString()));
    assertEquals("getPrimaryShardVersion", DataStoreVersions.CURRENT_VERSION, actual.getPrimaryShardVersion());
    Future<PrimaryShardInfo> cached = actorContext.getPrimaryShardInfoCache().getIfPresent("foobar");
    PrimaryShardInfo cachedInfo = Await.result(cached, FiniteDuration.apply(1, TimeUnit.MILLISECONDS));
    assertEquals(cachedInfo, actual);
    actorContext.getPrimaryShardInfoCache().remove("foobar");
    cached = actorContext.getPrimaryShardInfoCache().getIfPresent("foobar");
    assertNull(cached);
}
Also used : Configuration(org.opendaylight.controller.cluster.datastore.config.Configuration) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) Timeout(akka.util.Timeout) ClusterWrapper(org.opendaylight.controller.cluster.datastore.ClusterWrapper) LocalPrimaryShardFound(org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound) DatastoreContext(org.opendaylight.controller.cluster.datastore.DatastoreContext) DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) PrimaryShardInfo(org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Example 9 with LocalPrimaryShardFound

use of org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound in project controller by opendaylight.

the class ShardManager method onAddPrefixShardReplica.

private void onAddPrefixShardReplica(final AddPrefixShardReplica message) {
    LOG.debug("{}: onAddPrefixShardReplica: {}", persistenceId(), message);
    final ShardIdentifier shardId = getShardIdentifier(cluster.getCurrentMemberName(), ClusterUtils.getCleanShardName(message.getShardPrefix()));
    final String shardName = shardId.getShardName();
    // Create the localShard
    if (schemaContext == null) {
        String msg = String.format("No SchemaContext is available in order to create a local shard instance for %s", shardName);
        LOG.debug("{}: {}", persistenceId(), msg);
        getSender().tell(new Status.Failure(new IllegalStateException(msg)), getSelf());
        return;
    }
    findPrimary(shardName, new AutoFindPrimaryFailureResponseHandler(getSender(), shardName, persistenceId(), getSelf()) {

        @Override
        public void onRemotePrimaryShardFound(final RemotePrimaryShardFound response) {
            final RunnableMessage runnable = (RunnableMessage) () -> addPrefixShard(getShardName(), message.getShardPrefix(), response, getSender());
            if (!isPreviousShardActorStopInProgress(getShardName(), runnable)) {
                getSelf().tell(runnable, getTargetActor());
            }
        }

        @Override
        public void onLocalPrimaryFound(final LocalPrimaryShardFound message) {
            sendLocalReplicaAlreadyExistsReply(getShardName(), getTargetActor());
        }
    });
}
Also used : FollowerInitialSyncUpStatus(org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus) ChangeShardMembersVotingStatus(org.opendaylight.controller.cluster.datastore.messages.ChangeShardMembersVotingStatus) FlipShardMembersVotingStatus(org.opendaylight.controller.cluster.datastore.messages.FlipShardMembersVotingStatus) Status(akka.actor.Status) ServerChangeStatus(org.opendaylight.controller.cluster.raft.messages.ServerChangeStatus) ChangeServersVotingStatus(org.opendaylight.controller.cluster.raft.messages.ChangeServersVotingStatus) LocalPrimaryShardFound(org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound) ShardIdentifier(org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier) RemotePrimaryShardFound(org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound)

Example 10 with LocalPrimaryShardFound

use of org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound in project controller by opendaylight.

the class ShardManager method findPrimary.

private void findPrimary(final FindPrimary message) {
    LOG.debug("{}: In findPrimary: {}", persistenceId(), message);
    final String shardName = message.getShardName();
    final boolean canReturnLocalShardState = !(message instanceof RemoteFindPrimary);
    // First see if the there is a local replica for the shard
    final ShardInformation info = localShards.get(shardName);
    if (info != null && info.isActiveMember()) {
        sendResponse(info, message.isWaitUntilReady(), true, () -> {
            String primaryPath = info.getSerializedLeaderActor();
            Object found = canReturnLocalShardState && info.isLeader() ? new LocalPrimaryShardFound(primaryPath, info.getLocalShardDataTree().get()) : new RemotePrimaryShardFound(primaryPath, info.getLeaderVersion());
            LOG.debug("{}: Found primary for {}: {}", persistenceId(), shardName, found);
            return found;
        });
        return;
    }
    final Collection<String> visitedAddresses;
    if (message instanceof RemoteFindPrimary) {
        visitedAddresses = ((RemoteFindPrimary) message).getVisitedAddresses();
    } else {
        visitedAddresses = new ArrayList<>(1);
    }
    visitedAddresses.add(peerAddressResolver.getShardManagerActorPathBuilder(cluster.getSelfAddress()).toString());
    for (String address : peerAddressResolver.getShardManagerPeerActorAddresses()) {
        if (visitedAddresses.contains(address)) {
            continue;
        }
        LOG.debug("{}: findPrimary for {} forwarding to remote ShardManager {}, visitedAddresses: {}", persistenceId(), shardName, address, visitedAddresses);
        getContext().actorSelection(address).forward(new RemoteFindPrimary(shardName, message.isWaitUntilReady(), visitedAddresses), getContext());
        return;
    }
    LOG.debug("{}: No shard found for {}", persistenceId(), shardName);
    getSender().tell(new PrimaryNotFoundException(String.format("No primary shard found for %s.", shardName)), getSelf());
}
Also used : PrimaryNotFoundException(org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException) LocalPrimaryShardFound(org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound) RemotePrimaryShardFound(org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound) RemoteFindPrimary(org.opendaylight.controller.cluster.datastore.messages.RemoteFindPrimary)

Aggregations

LocalPrimaryShardFound (org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound)11 FindPrimary (org.opendaylight.controller.cluster.datastore.messages.FindPrimary)7 ActorRef (akka.actor.ActorRef)6 TestActorRef (akka.testkit.TestActorRef)6 Test (org.junit.Test)6 RemotePrimaryShardFound (org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound)6 AddressFromURIString (akka.actor.AddressFromURIString)5 TestKit (akka.testkit.javadsl.TestKit)5 AbstractShardManagerTest (org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)5 ActorInitialized (org.opendaylight.controller.cluster.datastore.messages.ActorInitialized)5 ShardLeaderStateChanged (org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged)5 UpdateSchemaContext (org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext)5 RoleChangeNotification (org.opendaylight.controller.cluster.notifications.RoleChangeNotification)5 PrimaryShardInfo (org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo)4 DataTree (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree)4 ActorSystem (akka.actor.ActorSystem)2 Timeout (akka.util.Timeout)2 ClusterWrapperImpl (org.opendaylight.controller.cluster.datastore.ClusterWrapperImpl)2 PrimaryNotFoundException (org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException)2 ShardIdentifier (org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier)2