Search in sources :

Example 11 with DatastoreContext

use of org.opendaylight.controller.cluster.datastore.DatastoreContext in project controller by opendaylight.

the class ShardManagerTest method testPerShardDatastoreContext.

@Test
public void testPerShardDatastoreContext() throws Exception {
    LOG.info("testPerShardDatastoreContext starting");
    final DatastoreContextFactory mockFactory = newDatastoreContextFactory(datastoreContextBuilder.shardElectionTimeoutFactor(5).build());
    Mockito.doReturn(DatastoreContext.newBuilderFrom(datastoreContextBuilder.build()).shardElectionTimeoutFactor(6).build()).when(mockFactory).getShardDatastoreContext("default");
    Mockito.doReturn(DatastoreContext.newBuilderFrom(datastoreContextBuilder.build()).shardElectionTimeoutFactor(7).build()).when(mockFactory).getShardDatastoreContext("topology");
    final MockConfiguration mockConfig = new MockConfiguration() {

        @Override
        public Collection<String> getMemberShardNames(final MemberName memberName) {
            return Arrays.asList("default", "topology");
        }

        @Override
        public Collection<MemberName> getMembersFromShardName(final String shardName) {
            return members("member-1");
        }
    };
    final ActorRef defaultShardActor = actorFactory.createActor(MessageCollectorActor.props(), actorFactory.generateActorId("default"));
    final ActorRef topologyShardActor = actorFactory.createActor(MessageCollectorActor.props(), actorFactory.generateActorId("topology"));
    final Map<String, Entry<ActorRef, DatastoreContext>> shardInfoMap = Collections.synchronizedMap(new HashMap<String, Entry<ActorRef, DatastoreContext>>());
    shardInfoMap.put("default", new AbstractMap.SimpleEntry<>(defaultShardActor, null));
    shardInfoMap.put("topology", new AbstractMap.SimpleEntry<>(topologyShardActor, null));
    final PrimaryShardInfoFutureCache primaryShardInfoCache = new PrimaryShardInfoFutureCache();
    final CountDownLatch newShardActorLatch = new CountDownLatch(2);
    class LocalShardManager extends ShardManager {

        LocalShardManager(final AbstractShardManagerCreator<?> creator) {
            super(creator);
        }

        @Override
        protected ActorRef newShardActor(final ShardInformation info) {
            Entry<ActorRef, DatastoreContext> entry = shardInfoMap.get(info.getShardName());
            ActorRef ref = null;
            if (entry != null) {
                ref = entry.getKey();
                entry.setValue(info.getDatastoreContext());
            }
            newShardActorLatch.countDown();
            return ref;
        }
    }
    final Creator<ShardManager> creator = new Creator<ShardManager>() {

        private static final long serialVersionUID = 1L;

        @Override
        public ShardManager create() throws Exception {
            return new LocalShardManager(new GenericCreator<>(LocalShardManager.class).datastoreContextFactory(mockFactory).primaryShardInfoCache(primaryShardInfoCache).configuration(mockConfig));
        }
    };
    TestKit kit = new TestKit(getSystem());
    final ActorRef shardManager = actorFactory.createActor(Props.create(new DelegatingShardManagerCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()));
    shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), kit.getRef());
    assertEquals("Shard actors created", true, newShardActorLatch.await(5, TimeUnit.SECONDS));
    assertEquals("getShardElectionTimeoutFactor", 6, shardInfoMap.get("default").getValue().getShardElectionTimeoutFactor());
    assertEquals("getShardElectionTimeoutFactor", 7, shardInfoMap.get("topology").getValue().getShardElectionTimeoutFactor());
    DatastoreContextFactory newMockFactory = newDatastoreContextFactory(datastoreContextBuilder.shardElectionTimeoutFactor(5).build());
    Mockito.doReturn(DatastoreContext.newBuilderFrom(datastoreContextBuilder.build()).shardElectionTimeoutFactor(66).build()).when(newMockFactory).getShardDatastoreContext("default");
    Mockito.doReturn(DatastoreContext.newBuilderFrom(datastoreContextBuilder.build()).shardElectionTimeoutFactor(77).build()).when(newMockFactory).getShardDatastoreContext("topology");
    shardManager.tell(newMockFactory, kit.getRef());
    DatastoreContext newContext = MessageCollectorActor.expectFirstMatching(defaultShardActor, DatastoreContext.class);
    assertEquals("getShardElectionTimeoutFactor", 66, newContext.getShardElectionTimeoutFactor());
    newContext = MessageCollectorActor.expectFirstMatching(topologyShardActor, DatastoreContext.class);
    assertEquals("getShardElectionTimeoutFactor", 77, newContext.getShardElectionTimeoutFactor());
    LOG.info("testPerShardDatastoreContext ending");
}
Also used : UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) DatastoreContextFactory(org.opendaylight.controller.cluster.datastore.DatastoreContextFactory) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) AddressFromURIString(akka.actor.AddressFromURIString) Creator(akka.japi.Creator) TestKit(akka.testkit.javadsl.TestKit) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractMap(java.util.AbstractMap) Entry(java.util.Map.Entry) DatastoreContext(org.opendaylight.controller.cluster.datastore.DatastoreContext) PrimaryShardInfoFutureCache(org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) MemberName(org.opendaylight.controller.cluster.access.concepts.MemberName) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 12 with DatastoreContext

use of org.opendaylight.controller.cluster.datastore.DatastoreContext in project controller by opendaylight.

the class ShardManagerTest method testOnCreateShard.

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

        {
            datastoreContextBuilder.shardInitializationTimeout(1, TimeUnit.MINUTES).persistent(true);
            ActorRef shardManager = actorFactory.createActor(newShardMgrProps(new ConfigurationImpl(new EmptyModuleShardConfigProvider())).withDispatcher(Dispatchers.DefaultDispatcherId()));
            SchemaContext schemaContext = TestModel.createTestContext();
            shardManager.tell(new UpdateSchemaContext(schemaContext), ActorRef.noSender());
            DatastoreContext datastoreContext = DatastoreContext.newBuilder().shardElectionTimeoutFactor(100).persistent(false).build();
            Shard.Builder shardBuilder = Shard.builder();
            ModuleShardConfiguration config = new ModuleShardConfiguration(URI.create("foo-ns"), "foo-module", "foo", null, members("member-1", "member-5", "member-6"));
            shardManager.tell(new CreateShard(config, shardBuilder, datastoreContext), getRef());
            expectMsgClass(duration("5 seconds"), Success.class);
            shardManager.tell(new FindLocalShard("foo", true), getRef());
            expectMsgClass(duration("5 seconds"), LocalShardFound.class);
            assertEquals("isRecoveryApplicable", false, shardBuilder.getDatastoreContext().isPersistent());
            assertTrue("Epxected ShardPeerAddressResolver", shardBuilder.getDatastoreContext().getShardRaftConfig().getPeerAddressResolver() instanceof ShardPeerAddressResolver);
            assertEquals("peerMembers", Sets.newHashSet(ShardIdentifier.create("foo", MemberName.forName("member-5"), shardMrgIDSuffix).toString(), ShardIdentifier.create("foo", MemberName.forName("member-6"), shardMrgIDSuffix).toString()), shardBuilder.getPeerAddresses().keySet());
            assertEquals("ShardIdentifier", ShardIdentifier.create("foo", MEMBER_1, shardMrgIDSuffix), shardBuilder.getId());
            assertSame("schemaContext", schemaContext, shardBuilder.getSchemaContext());
            // Send CreateShard with same name - should return Success with
            // a message.
            shardManager.tell(new CreateShard(config, shardBuilder, null), getRef());
            Success success = expectMsgClass(duration("5 seconds"), Success.class);
            assertNotNull("Success status is null", success.status());
        }
    };
    LOG.info("testOnCreateShard ending");
}
Also used : UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) FindLocalShard(org.opendaylight.controller.cluster.datastore.messages.FindLocalShard) TestKit(akka.testkit.javadsl.TestKit) CreateShard(org.opendaylight.controller.cluster.datastore.messages.CreateShard) Success(akka.actor.Status.Success) EmptyModuleShardConfigProvider(org.opendaylight.controller.cluster.datastore.config.EmptyModuleShardConfigProvider) DatastoreContext(org.opendaylight.controller.cluster.datastore.DatastoreContext) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) CreateShard(org.opendaylight.controller.cluster.datastore.messages.CreateShard) FindLocalShard(org.opendaylight.controller.cluster.datastore.messages.FindLocalShard) Shard(org.opendaylight.controller.cluster.datastore.Shard) ConfigurationImpl(org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl) ModuleShardConfiguration(org.opendaylight.controller.cluster.datastore.config.ModuleShardConfiguration) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 13 with DatastoreContext

use of org.opendaylight.controller.cluster.datastore.DatastoreContext 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 14 with DatastoreContext

use of org.opendaylight.controller.cluster.datastore.DatastoreContext in project controller by opendaylight.

the class ShardManager method doCreateShard.

private void doCreateShard(final CreateShard createShard) {
    final ModuleShardConfiguration moduleShardConfig = createShard.getModuleShardConfig();
    final String shardName = moduleShardConfig.getShardName();
    configuration.addModuleShardConfiguration(moduleShardConfig);
    DatastoreContext shardDatastoreContext = createShard.getDatastoreContext();
    if (shardDatastoreContext == null) {
        shardDatastoreContext = newShardDatastoreContext(shardName);
    } else {
        shardDatastoreContext = DatastoreContext.newBuilderFrom(shardDatastoreContext).shardPeerAddressResolver(peerAddressResolver).build();
    }
    ShardIdentifier shardId = getShardIdentifier(cluster.getCurrentMemberName(), shardName);
    boolean shardWasInRecoveredSnapshot = currentSnapshot != null && currentSnapshot.getShardList().contains(shardName);
    Map<String, String> peerAddresses;
    boolean isActiveMember;
    if (shardWasInRecoveredSnapshot || configuration.getMembersFromShardName(shardName).contains(cluster.getCurrentMemberName())) {
        peerAddresses = getPeerAddresses(shardName);
        isActiveMember = true;
    } else {
        // The local member is not in the static shard member configuration and the shard did not
        // previously exist (ie !shardWasInRecoveredSnapshot). In this case we'll create
        // the shard with no peers and with elections disabled so it stays as follower. A
        // subsequent AddServer request will be needed to make it an active member.
        isActiveMember = false;
        peerAddresses = Collections.emptyMap();
        shardDatastoreContext = DatastoreContext.newBuilderFrom(shardDatastoreContext).customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName()).build();
    }
    LOG.debug("{} doCreateShard: shardId: {}, memberNames: {}, peerAddresses: {}, isActiveMember: {}", persistenceId(), shardId, moduleShardConfig.getShardMemberNames(), peerAddresses, isActiveMember);
    ShardInformation info = new ShardInformation(shardName, shardId, peerAddresses, shardDatastoreContext, createShard.getShardBuilder(), peerAddressResolver);
    info.setActiveMember(isActiveMember);
    localShards.put(info.getShardName(), info);
    if (schemaContext != null) {
        info.setSchemaContext(schemaContext);
        info.setActor(newShardActor(info));
    }
}
Also used : DatastoreContext(org.opendaylight.controller.cluster.datastore.DatastoreContext) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) ShardIdentifier(org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier) ModuleShardConfiguration(org.opendaylight.controller.cluster.datastore.config.ModuleShardConfiguration)

Example 15 with DatastoreContext

use of org.opendaylight.controller.cluster.datastore.DatastoreContext in project controller by opendaylight.

the class ShardManager method addPrefixShard.

private void addPrefixShard(final String shardName, final YangInstanceIdentifier shardPrefix, final RemotePrimaryShardFound response, final ActorRef sender) {
    if (isShardReplicaOperationInProgress(shardName, sender)) {
        return;
    }
    shardReplicaOperationsInProgress.add(shardName);
    final ShardInformation shardInfo;
    final boolean removeShardOnFailure;
    ShardInformation existingShardInfo = localShards.get(shardName);
    if (existingShardInfo == null) {
        removeShardOnFailure = true;
        ShardIdentifier shardId = getShardIdentifier(cluster.getCurrentMemberName(), shardName);
        final Builder builder = newShardDatastoreContextBuilder(shardName);
        builder.storeRoot(shardPrefix).customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
        DatastoreContext datastoreContext = builder.build();
        shardInfo = new ShardInformation(shardName, shardId, getPeerAddresses(shardName), datastoreContext, Shard.builder(), peerAddressResolver);
        shardInfo.setActiveMember(false);
        shardInfo.setSchemaContext(schemaContext);
        localShards.put(shardName, shardInfo);
        shardInfo.setActor(newShardActor(shardInfo));
    } else {
        removeShardOnFailure = false;
        shardInfo = existingShardInfo;
    }
    execAddShard(shardName, shardInfo, response, removeShardOnFailure, sender);
}
Also used : DatastoreContext(org.opendaylight.controller.cluster.datastore.DatastoreContext) Builder(org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) ShardIdentifier(org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier)

Aggregations

DatastoreContext (org.opendaylight.controller.cluster.datastore.DatastoreContext)15 Timeout (akka.util.Timeout)6 ShardIdentifier (org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier)6 ActorRef (akka.actor.ActorRef)5 TestActorRef (akka.testkit.TestActorRef)5 Test (org.junit.Test)5 Configuration (org.opendaylight.controller.cluster.datastore.config.Configuration)5 ClusterWrapper (org.opendaylight.controller.cluster.datastore.ClusterWrapper)4 DeleteSnapshotsFailure (akka.persistence.DeleteSnapshotsFailure)3 SaveSnapshotFailure (akka.persistence.SaveSnapshotFailure)3 TestKit (akka.testkit.javadsl.TestKit)3 Dispatchers (org.opendaylight.controller.cluster.common.actor.Dispatchers)3 AbstractActorTest (org.opendaylight.controller.cluster.datastore.AbstractActorTest)3 DatastoreContextFactory (org.opendaylight.controller.cluster.datastore.DatastoreContextFactory)3 PrimaryShardInfo (org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo)3 Before (org.junit.Before)2 MemberName (org.opendaylight.controller.cluster.access.concepts.MemberName)2 AbstractShardManagerTest (org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)2 Builder (org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder)2 ConfigurationImpl (org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl)2