Search in sources :

Example 6 with DistributedShardRegistration

use of org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration in project controller by opendaylight.

the class PrefixShardHandler method onCreatePrefixShard.

public ListenableFuture<RpcResult<Void>> onCreatePrefixShard(final CreatePrefixShardInput input) {
    final SettableFuture<RpcResult<Void>> future = SettableFuture.create();
    final CompletionStage<DistributedShardRegistration> completionStage;
    final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
    try {
        completionStage = shardFactory.createDistributedShard(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, identifier), input.getReplicas().stream().map(MemberName::forName).collect(Collectors.toList()));
        completionStage.thenAccept(registration -> {
            LOG.debug("Shard[{}] created successfully.", identifier);
            registrations.put(identifier, registration);
            final ListenableFuture<Void> ensureFuture = ensureListExists();
            Futures.addCallback(ensureFuture, new FutureCallback<Void>() {

                @Override
                public void onSuccess(@Nullable final Void result) {
                    LOG.debug("Initial list write successful.");
                    future.set(RpcResultBuilder.<Void>success().build());
                }

                @Override
                public void onFailure(final Throwable throwable) {
                    LOG.warn("Shard[{}] creation failed:", identifier, throwable);
                    final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed", "Shard creation failed", "cluster-test-app", "", throwable);
                    future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
                }
            }, MoreExecutors.directExecutor());
        });
        completionStage.exceptionally(throwable -> {
            LOG.warn("Shard[{}] creation failed:", identifier, throwable);
            final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed", "Shard creation failed", "cluster-test-app", "", throwable);
            future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
            return null;
        });
    } catch (final DOMDataTreeShardingConflictException e) {
        LOG.warn("Unable to register shard for: {}.", identifier);
        final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed", "Sharding conflict", "cluster-test-app", "", e);
        future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
    }
    return future;
}
Also used : DistributedShardRegistration(org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration) DOMDataTreeIdentifier(org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) RpcError(org.opendaylight.yangtools.yang.common.RpcError) DOMDataTreeShardingConflictException(org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 7 with DistributedShardRegistration

use of org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration in project controller by opendaylight.

the class PrefixShardHandler method onRemovePrefixShard.

public ListenableFuture<RpcResult<Void>> onRemovePrefixShard(final RemovePrefixShardInput input) {
    final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
    final DistributedShardRegistration registration = registrations.get(identifier);
    if (registration == null) {
        final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "registration-missing", "No shard registered at this prefix.");
        return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
    }
    final SettableFuture<RpcResult<Void>> future = SettableFuture.create();
    final CompletionStage<Void> close = registration.close();
    close.thenRun(() -> future.set(RpcResultBuilder.<Void>success().build()));
    close.exceptionally(throwable -> {
        LOG.warn("Shard[{}] removal failed:", identifier, throwable);
        final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "remove-shard-failed", "Shard removal failed", "cluster-test-app", "", throwable);
        future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
        return null;
    });
    return future;
}
Also used : DistributedShardRegistration(org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration) RpcError(org.opendaylight.yangtools.yang.common.RpcError) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 8 with DistributedShardRegistration

use of org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration in project controller by opendaylight.

the class DistributedShardedDOMDataTreeRemotingTest method testWriteIntoMultipleShards.

@Test
public void testWriteIntoMultipleShards() throws Exception {
    LOG.info("testWriteIntoMultipleShards starting");
    initEmptyDatastores();
    leaderTestKit.waitForMembersUp("member-2");
    LOG.debug("registering first shard");
    final DistributedShardRegistration shardRegistration = waitOnAsyncTask(leaderShardFactory.createDistributedShard(TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
    leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(), ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
    findLocalShard(followerConfigDatastore.getActorContext(), ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
    final Set<String> peers = new HashSet<>();
    IntegrationTestKit.verifyShardState(leaderConfigDatastore, ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()), onDemandShardState -> peers.addAll(onDemandShardState.getPeerAddresses().values()));
    assertEquals(peers.size(), 1);
    LOG.debug("Got after waiting for nonleader");
    final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singleton(TEST_ID));
    final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(true);
    final DOMDataTreeWriteCursor cursor = tx.createCursor(TEST_ID);
    Assert.assertNotNull(cursor);
    final YangInstanceIdentifier nameId = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(TestModel.NAME_QNAME).build();
    cursor.write(nameId.getLastPathArgument(), ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.NAME_QNAME)).withValue("Test Value").build());
    cursor.close();
    LOG.warn("Got to pre submit");
    tx.submit().checkedGet();
    shardRegistration.close().toCompletableFuture().get();
    LOG.info("testWriteIntoMultipleShards ending");
}
Also used : DistributedShardRegistration(org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration) DOMDataTreeWriteCursor(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) AddressFromURIString(akka.actor.AddressFromURIString) DOMDataTreeProducer(org.opendaylight.mdsal.dom.api.DOMDataTreeProducer) DOMDataTreeCursorAwareTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) HashSet(java.util.HashSet) Test(org.junit.Test) AbstractTest(org.opendaylight.controller.cluster.datastore.AbstractTest)

Example 9 with DistributedShardRegistration

use of org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration in project controller by opendaylight.

the class DistributedShardedDOMDataTreeTest method testMultipleWritesIntoSingleMapEntry.

@Test
public void testMultipleWritesIntoSingleMapEntry() throws Exception {
    initEmptyDatastores();
    final DistributedShardRegistration shardRegistration = waitOnAsyncTask(leaderShardFactory.createDistributedShard(TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME)), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
    leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(), ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
    LOG.warn("Got after waiting for nonleader");
    final ActorRef leaderShardManager = leaderDistributedDataStore.getActorContext().getShardManager();
    leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(), ClusterUtils.getCleanShardName(TestModel.TEST_PATH));
    final YangInstanceIdentifier oid1 = getOuterListIdFor(0);
    final DOMDataTreeIdentifier outerListPath = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1);
    final DistributedShardRegistration outerListShardReg = waitOnAsyncTask(leaderShardFactory.createDistributedShard(outerListPath, Lists.newArrayList(AbstractTest.MEMBER_NAME)), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
    leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(), ClusterUtils.getCleanShardName(outerListPath.getRootIdentifier()));
    final DOMDataTreeProducer shardProducer = leaderShardFactory.createProducer(Collections.singletonList(outerListPath));
    final DOMDataTreeCursorAwareTransaction tx = shardProducer.createTransaction(false);
    final DOMDataTreeWriteCursor cursor = tx.createCursor(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1));
    assertNotNull(cursor);
    MapNode innerList = ImmutableMapNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME)).build();
    cursor.write(new NodeIdentifier(TestModel.INNER_LIST_QNAME), innerList);
    cursor.close();
    tx.submit().checkedGet();
    final ArrayList<CheckedFuture<Void, TransactionCommitFailedException>> futures = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        final Collection<MapEntryNode> innerListMapEntries = createInnerListMapEntries(1000, "run-" + i);
        for (final MapEntryNode innerListMapEntry : innerListMapEntries) {
            final DOMDataTreeCursorAwareTransaction tx1 = shardProducer.createTransaction(false);
            final DOMDataTreeWriteCursor cursor1 = tx1.createCursor(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1.node(new NodeIdentifier(TestModel.INNER_LIST_QNAME))));
            cursor1.write(innerListMapEntry.getIdentifier(), innerListMapEntry);
            cursor1.close();
            futures.add(tx1.submit());
        }
    }
    futures.get(futures.size() - 1).checkedGet();
    final DOMDataTreeListener mockedDataTreeListener = mock(DOMDataTreeListener.class);
    doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
    leaderShardFactory.registerListener(mockedDataTreeListener, Collections.singletonList(INNER_LIST_ID), true, Collections.emptyList());
    verify(mockedDataTreeListener, timeout(1000).times(1)).onDataTreeChanged(captorForChanges.capture(), captorForSubtrees.capture());
    verifyNoMoreInteractions(mockedDataTreeListener);
    final List<Collection<DataTreeCandidate>> capturedValue = captorForChanges.getAllValues();
    final NormalizedNode<?, ?> expected = ImmutableMapNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME)).withValue(createInnerListMapEntries(1000, "run-999")).build();
    assertEquals("List values dont match the expected values from the last run", expected, capturedValue.get(0).iterator().next().getRootNode().getDataAfter().get());
}
Also used : DistributedShardRegistration(org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration) DOMDataTreeWriteCursor(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor) ActorRef(akka.actor.ActorRef) DOMDataTreeIdentifier(org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier) ArrayList(java.util.ArrayList) DOMDataTreeListener(org.opendaylight.mdsal.dom.api.DOMDataTreeListener) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) Collection(java.util.Collection) Matchers.anyCollection(org.mockito.Matchers.anyCollection) DOMDataTreeProducer(org.opendaylight.mdsal.dom.api.DOMDataTreeProducer) CheckedFuture(com.google.common.util.concurrent.CheckedFuture) DOMDataTreeCursorAwareTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction) Test(org.junit.Test) AbstractTest(org.opendaylight.controller.cluster.datastore.AbstractTest)

Example 10 with DistributedShardRegistration

use of org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration in project controller by opendaylight.

the class DistributedShardedDOMDataTreeTest method testSingleNodeWritesAndRead.

@Test
public void testSingleNodeWritesAndRead() throws Exception {
    initEmptyDatastores();
    final DistributedShardRegistration shardRegistration = waitOnAsyncTask(leaderShardFactory.createDistributedShard(TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME)), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
    leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(), ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
    final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singleton(TEST_ID));
    final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(true);
    final DOMDataTreeWriteCursor cursor = tx.createCursor(TEST_ID);
    Assert.assertNotNull(cursor);
    final YangInstanceIdentifier nameId = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(TestModel.NAME_QNAME).build();
    final LeafNode<String> valueToCheck = ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.NAME_QNAME)).withValue("Test Value").build();
    LOG.debug("Writing data {} at {}, cursor {}", nameId.getLastPathArgument(), valueToCheck, cursor);
    cursor.write(nameId.getLastPathArgument(), valueToCheck);
    cursor.close();
    LOG.debug("Got to pre submit");
    tx.submit().checkedGet();
    final DOMDataTreeListener mockedDataTreeListener = mock(DOMDataTreeListener.class);
    doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
    leaderShardFactory.registerListener(mockedDataTreeListener, Collections.singletonList(TEST_ID), true, Collections.emptyList());
    verify(mockedDataTreeListener, timeout(1000).times(1)).onDataTreeChanged(captorForChanges.capture(), captorForSubtrees.capture());
    final List<Collection<DataTreeCandidate>> capturedValue = captorForChanges.getAllValues();
    final java.util.Optional<NormalizedNode<?, ?>> dataAfter = capturedValue.get(0).iterator().next().getRootNode().getDataAfter();
    final NormalizedNode<?, ?> expected = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).withChild(valueToCheck).build();
    assertEquals(expected, dataAfter.get());
    verifyNoMoreInteractions(mockedDataTreeListener);
    final String shardName = ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier());
    LOG.debug("Creating distributed datastore client for shard {}", shardName);
    final ActorContext actorContext = leaderDistributedDataStore.getActorContext();
    final Props distributedDataStoreClientProps = SimpleDataStoreClientActor.props(actorContext.getCurrentMemberName(), "Shard-" + shardName, actorContext, shardName);
    final ActorRef clientActor = leaderSystem.actorOf(distributedDataStoreClientProps);
    final DataStoreClient distributedDataStoreClient = SimpleDataStoreClientActor.getDistributedDataStoreClient(clientActor, 30, TimeUnit.SECONDS);
    final ClientLocalHistory localHistory = distributedDataStoreClient.createLocalHistory();
    final ClientTransaction tx2 = localHistory.createTransaction();
    final CheckedFuture<Optional<NormalizedNode<?, ?>>, org.opendaylight.mdsal.common.api.ReadFailedException> read = tx2.read(YangInstanceIdentifier.EMPTY);
    final Optional<NormalizedNode<?, ?>> optional = read.checkedGet();
    tx2.abort();
    localHistory.close();
    shardRegistration.close().toCompletableFuture().get();
}
Also used : DistributedShardRegistration(org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration) DOMDataTreeWriteCursor(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor) ActorRef(akka.actor.ActorRef) AddressFromURIString(akka.actor.AddressFromURIString) Props(akka.actor.Props) DataStoreClient(org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient) DOMDataTreeProducer(org.opendaylight.mdsal.dom.api.DOMDataTreeProducer) DOMDataTreeCursorAwareTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Optional(com.google.common.base.Optional) ClientTransaction(org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction) DOMDataTreeListener(org.opendaylight.mdsal.dom.api.DOMDataTreeListener) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) ClientLocalHistory(org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) Collection(java.util.Collection) Matchers.anyCollection(org.mockito.Matchers.anyCollection) Test(org.junit.Test) AbstractTest(org.opendaylight.controller.cluster.datastore.AbstractTest)

Aggregations

DistributedShardRegistration (org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration)11 Test (org.junit.Test)9 AbstractTest (org.opendaylight.controller.cluster.datastore.AbstractTest)9 DOMDataTreeProducer (org.opendaylight.mdsal.dom.api.DOMDataTreeProducer)6 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)6 DOMDataTreeIdentifier (org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier)5 AddressFromURIString (akka.actor.AddressFromURIString)4 DOMDataTreeCursorAwareTransaction (org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction)4 DOMDataTreeWriteCursor (org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor)4 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)4 ActorRef (akka.actor.ActorRef)3 HashSet (java.util.HashSet)3 DOMDataTreeListener (org.opendaylight.mdsal.dom.api.DOMDataTreeListener)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Matchers.anyCollection (org.mockito.Matchers.anyCollection)2 DOMDataTreeShardingConflictException (org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException)2 RpcError (org.opendaylight.yangtools.yang.common.RpcError)2 RpcResult (org.opendaylight.yangtools.yang.common.RpcResult)2 MapNode (org.opendaylight.yangtools.yang.data.api.schema.MapNode)2