Search in sources :

Example 6 with NormalizedNode

use of org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode in project controller by opendaylight.

the class ShardDataTreeTest method testAbortWithFailedRebase.

@SuppressWarnings("unchecked")
@Test
public void testAbortWithFailedRebase() throws Exception {
    immediatePayloadReplication(shardDataTree, mockShard);
    final ShardDataTreeCohort cohort1 = newShardDataTreeCohort(snapshot -> snapshot.write(CarsModel.BASE_PATH, CarsModel.emptyContainer()));
    final ShardDataTreeCohort cohort2 = newShardDataTreeCohort(snapshot -> snapshot.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()));
    NormalizedNode<?, ?> peopleNode = PeopleModel.create();
    final ShardDataTreeCohort cohort3 = newShardDataTreeCohort(snapshot -> snapshot.write(PeopleModel.BASE_PATH, peopleNode));
    immediateCanCommit(cohort1);
    FutureCallback<Void> canCommitCallback2 = coordinatedCanCommit(cohort2);
    coordinatedPreCommit(cohort1);
    verify(canCommitCallback2).onSuccess(null);
    FutureCallback<Void> mockAbortCallback = mock(FutureCallback.class);
    doNothing().when(mockAbortCallback).onSuccess(null);
    cohort1.abort(mockAbortCallback);
    verify(mockAbortCallback).onSuccess(null);
    FutureCallback<DataTreeCandidate> preCommitCallback2 = coordinatedPreCommit(cohort2);
    verify(preCommitCallback2).onFailure(any(Throwable.class));
    immediateCanCommit(cohort3);
    immediatePreCommit(cohort3);
    immediateCommit(cohort3);
    final DataTreeSnapshot snapshot = shardDataTree.newReadOnlyTransaction(nextTransactionId()).getSnapshot();
    Optional<NormalizedNode<?, ?>> optional = snapshot.readNode(PeopleModel.BASE_PATH);
    assertEquals("People node present", true, optional.isPresent());
    assertEquals("People node", peopleNode, optional.get());
}
Also used : DataTreeCandidate(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate) DataTreeSnapshot(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Test(org.junit.Test)

Example 7 with NormalizedNode

use of org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode in project controller by opendaylight.

the class ShardDataTreeTest method testAbortWithPendingCommits.

@SuppressWarnings("unchecked")
@Test
public void testAbortWithPendingCommits() throws Exception {
    final ShardDataTreeCohort cohort1 = newShardDataTreeCohort(snapshot -> snapshot.write(CarsModel.BASE_PATH, CarsModel.emptyContainer()));
    final ShardDataTreeCohort cohort2 = newShardDataTreeCohort(snapshot -> snapshot.write(PeopleModel.BASE_PATH, PeopleModel.create()));
    final ShardDataTreeCohort cohort3 = newShardDataTreeCohort(snapshot -> snapshot.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()));
    YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
    MapEntryNode carNode = CarsModel.newCarEntry("optima", new BigInteger("100"));
    final ShardDataTreeCohort cohort4 = newShardDataTreeCohort(snapshot -> snapshot.write(carPath, carNode));
    coordinatedCanCommit(cohort2);
    immediateCanCommit(cohort1);
    coordinatedCanCommit(cohort3);
    coordinatedCanCommit(cohort4);
    coordinatedPreCommit(cohort1);
    coordinatedPreCommit(cohort2);
    coordinatedPreCommit(cohort3);
    FutureCallback<Void> mockAbortCallback = mock(FutureCallback.class);
    doNothing().when(mockAbortCallback).onSuccess(null);
    cohort2.abort(mockAbortCallback);
    verify(mockAbortCallback).onSuccess(null);
    coordinatedPreCommit(cohort4);
    coordinatedCommit(cohort1);
    coordinatedCommit(cohort3);
    coordinatedCommit(cohort4);
    InOrder inOrder = inOrder(mockShard);
    inOrder.verify(mockShard).persistPayload(eq(cohort1.getIdentifier()), any(CommitTransactionPayload.class), eq(false));
    inOrder.verify(mockShard).persistPayload(eq(cohort3.getIdentifier()), any(CommitTransactionPayload.class), eq(false));
    inOrder.verify(mockShard).persistPayload(eq(cohort4.getIdentifier()), any(CommitTransactionPayload.class), eq(false));
    // The payload instance doesn't matter - it just needs to be of type CommitTransactionPayload.
    CommitTransactionPayload mockPayload = CommitTransactionPayload.create(nextTransactionId(), cohort1.getCandidate());
    shardDataTree.applyReplicatedPayload(cohort1.getIdentifier(), mockPayload);
    shardDataTree.applyReplicatedPayload(cohort3.getIdentifier(), mockPayload);
    shardDataTree.applyReplicatedPayload(cohort4.getIdentifier(), mockPayload);
    final DataTreeSnapshot snapshot = shardDataTree.newReadOnlyTransaction(nextTransactionId()).getSnapshot();
    Optional<NormalizedNode<?, ?>> optional = snapshot.readNode(carPath);
    assertEquals("Car node present", true, optional.isPresent());
    assertEquals("Car node", carNode, optional.get());
}
Also used : InOrder(org.mockito.InOrder) BigInteger(java.math.BigInteger) CommitTransactionPayload(org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload) DataTreeSnapshot(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 8 with NormalizedNode

use of org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode in project controller by opendaylight.

the class ShardDataTreeTest method testPipelinedTransactionsWithImmediateCommits.

@Test
public void testPipelinedTransactionsWithImmediateCommits() throws Exception {
    final ShardDataTreeCohort cohort1 = newShardDataTreeCohort(snapshot -> snapshot.write(CarsModel.BASE_PATH, CarsModel.emptyContainer()));
    final ShardDataTreeCohort cohort2 = newShardDataTreeCohort(snapshot -> snapshot.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()));
    YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
    MapEntryNode carNode = CarsModel.newCarEntry("optima", new BigInteger("100"));
    final ShardDataTreeCohort cohort3 = newShardDataTreeCohort(snapshot -> snapshot.write(carPath, carNode));
    final FutureCallback<UnsignedLong> commitCallback2 = immediate3PhaseCommit(cohort2);
    final FutureCallback<UnsignedLong> commitCallback3 = immediate3PhaseCommit(cohort3);
    final FutureCallback<UnsignedLong> commitCallback1 = immediate3PhaseCommit(cohort1);
    InOrder inOrder = inOrder(mockShard);
    inOrder.verify(mockShard).persistPayload(eq(cohort1.getIdentifier()), any(CommitTransactionPayload.class), eq(true));
    inOrder.verify(mockShard).persistPayload(eq(cohort2.getIdentifier()), any(CommitTransactionPayload.class), eq(true));
    inOrder.verify(mockShard).persistPayload(eq(cohort3.getIdentifier()), any(CommitTransactionPayload.class), eq(false));
    // The payload instance doesn't matter - it just needs to be of type CommitTransactionPayload.
    CommitTransactionPayload mockPayload = CommitTransactionPayload.create(nextTransactionId(), cohort1.getCandidate());
    shardDataTree.applyReplicatedPayload(cohort1.getIdentifier(), mockPayload);
    shardDataTree.applyReplicatedPayload(cohort2.getIdentifier(), mockPayload);
    shardDataTree.applyReplicatedPayload(cohort3.getIdentifier(), mockPayload);
    inOrder = inOrder(commitCallback1, commitCallback2, commitCallback3);
    inOrder.verify(commitCallback1).onSuccess(any(UnsignedLong.class));
    inOrder.verify(commitCallback2).onSuccess(any(UnsignedLong.class));
    inOrder.verify(commitCallback3).onSuccess(any(UnsignedLong.class));
    final DataTreeSnapshot snapshot = shardDataTree.newReadOnlyTransaction(nextTransactionId()).getSnapshot();
    Optional<NormalizedNode<?, ?>> optional = snapshot.readNode(carPath);
    assertEquals("Car node present", true, optional.isPresent());
    assertEquals("Car node", carNode, optional.get());
}
Also used : InOrder(org.mockito.InOrder) UnsignedLong(com.google.common.primitives.UnsignedLong) BigInteger(java.math.BigInteger) CommitTransactionPayload(org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload) DataTreeSnapshot(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 9 with NormalizedNode

use of org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode in project controller by opendaylight.

the class DistributedDataStoreIntegrationTest method testTransactionReadsWithShardNotInitiallyReady.

@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testTransactionReadsWithShardNotInitiallyReady() throws Exception {
    new IntegrationTestKit(getSystem(), datastoreContextBuilder) {

        {
            final String testName = "testTransactionReadsWithShardNotInitiallyReady";
            final String shardName = "test-1";
            // Setup the InMemoryJournal to block shard recovery to ensure
            // the shard isn't
            // initialized until we create the Tx.
            final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
            final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
            InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
            try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, testName, false, shardName)) {
                // Create the read-write Tx
                final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
                assertNotNull("newReadWriteTransaction returned null", readWriteTx);
                // Do some reads on the Tx on a separate thread.
                final AtomicReference<CheckedFuture<Boolean, ReadFailedException>> txExistsFuture = new AtomicReference<>();
                final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>> txReadFuture = new AtomicReference<>();
                final AtomicReference<Exception> caughtEx = new AtomicReference<>();
                final CountDownLatch txReadsDone = new CountDownLatch(1);
                final Thread txThread = new Thread(() -> {
                    try {
                        readWriteTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
                        txExistsFuture.set(readWriteTx.exists(TestModel.TEST_PATH));
                        txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
                    } catch (Exception e) {
                        caughtEx.set(e);
                    } finally {
                        txReadsDone.countDown();
                    }
                });
                txThread.start();
                // Wait for the Tx operations to complete.
                boolean done = Uninterruptibles.awaitUninterruptibly(txReadsDone, 5, TimeUnit.SECONDS);
                if (caughtEx.get() != null) {
                    throw caughtEx.get();
                }
                assertEquals("Tx reads done", true, done);
                // At this point the Tx operations should be waiting for the
                // shard to initialize so
                // trigger the latch to let the shard recovery to continue.
                blockRecoveryLatch.countDown();
                // Wait for the reads to complete and verify.
                assertEquals("exists", true, txExistsFuture.get().checkedGet(5, TimeUnit.SECONDS));
                assertEquals("read", true, txReadFuture.get().checkedGet(5, TimeUnit.SECONDS).isPresent());
                readWriteTx.close();
            }
        }
    };
}
Also used : DOMStoreReadWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction) AtomicReference(java.util.concurrent.atomic.AtomicReference) AddressFromURIString(akka.actor.AddressFromURIString) CountDownLatch(java.util.concurrent.CountDownLatch) ReadFailedException(org.opendaylight.mdsal.common.api.ReadFailedException) NotInitializedException(org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException) TransactionChainClosedException(org.opendaylight.mdsal.common.api.TransactionChainClosedException) RequestTimeoutException(org.opendaylight.controller.cluster.access.client.RequestTimeoutException) NoShardLeaderException(org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException) IOException(java.io.IOException) TransactionCommitFailedException(org.opendaylight.mdsal.common.api.TransactionCommitFailedException) ExecutionException(java.util.concurrent.ExecutionException) CheckedFuture(com.google.common.util.concurrent.CheckedFuture) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Test(org.junit.Test)

Example 10 with NormalizedNode

use of org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode in project controller by opendaylight.

the class DistributedDataStoreIntegrationTest method testTransactionChainWithSingleShard.

@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testTransactionChainWithSingleShard() throws Exception {
    new IntegrationTestKit(getSystem(), datastoreContextBuilder) {

        {
            try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "testTransactionChainWithSingleShard", "test-1")) {
                // 1. Create a Tx chain and write-only Tx
                final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
                final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
                assertNotNull("newWriteOnlyTransaction returned null", writeTx);
                // 2. Write some data
                final NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
                writeTx.write(TestModel.TEST_PATH, testNode);
                // 3. Ready the Tx for commit
                final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
                // 4. Commit the Tx on another thread that first waits for
                // the second read Tx.
                final CountDownLatch continueCommit1 = new CountDownLatch(1);
                final CountDownLatch commit1Done = new CountDownLatch(1);
                final AtomicReference<Exception> commit1Error = new AtomicReference<>();
                new Thread(() -> {
                    try {
                        continueCommit1.await();
                        doCommit(cohort1);
                    } catch (Exception e) {
                        commit1Error.set(e);
                    } finally {
                        commit1Done.countDown();
                    }
                }).start();
                // 5. Create a new read Tx from the chain to read and verify
                // the data from the first
                // Tx is visible after being readied.
                DOMStoreReadTransaction readTx = txChain.newReadOnlyTransaction();
                Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", true, optional.isPresent());
                assertEquals("Data node", testNode, optional.get());
                // 6. Create a new RW Tx from the chain, write more data,
                // and ready it
                final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
                final MapNode outerNode = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
                rwTx.write(TestModel.OUTER_LIST_PATH, outerNode);
                final DOMStoreThreePhaseCommitCohort cohort2 = rwTx.ready();
                // 7. Create a new read Tx from the chain to read the data
                // from the last RW Tx to
                // verify it is visible.
                readTx = txChain.newReadWriteTransaction();
                optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", true, optional.isPresent());
                assertEquals("Data node", outerNode, optional.get());
                // 8. Wait for the 2 commits to complete and close the
                // chain.
                continueCommit1.countDown();
                Uninterruptibles.awaitUninterruptibly(commit1Done, 5, TimeUnit.SECONDS);
                if (commit1Error.get() != null) {
                    throw commit1Error.get();
                }
                doCommit(cohort2);
                txChain.close();
                // 9. Create a new read Tx from the data store and verify
                // committed data.
                readTx = dataStore.newReadOnlyTransaction();
                optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", true, optional.isPresent());
                assertEquals("Data node", outerNode, optional.get());
            }
        }
    };
}
Also used : DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) DOMStoreReadTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction) DOMStoreTransactionChain(org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain) DOMStoreReadWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction) AtomicReference(java.util.concurrent.atomic.AtomicReference) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) CountDownLatch(java.util.concurrent.CountDownLatch) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) ReadFailedException(org.opendaylight.mdsal.common.api.ReadFailedException) NotInitializedException(org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException) TransactionChainClosedException(org.opendaylight.mdsal.common.api.TransactionChainClosedException) RequestTimeoutException(org.opendaylight.controller.cluster.access.client.RequestTimeoutException) NoShardLeaderException(org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException) IOException(java.io.IOException) TransactionCommitFailedException(org.opendaylight.mdsal.common.api.TransactionCommitFailedException) ExecutionException(java.util.concurrent.ExecutionException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Test(org.junit.Test)

Aggregations

NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)94 Test (org.junit.Test)55 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)39 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)18 Optional (com.google.common.base.Optional)15 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)11 DOMStoreReadTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction)10 ReadFailedException (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException)9 DOMStoreWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction)9 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)9 ActorRef (akka.actor.ActorRef)8 InOrder (org.mockito.InOrder)8 DOMStoreReadWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction)8 DOMStoreThreePhaseCommitCohort (org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort)8 ExecutionException (java.util.concurrent.ExecutionException)7 ReadFailedException (org.opendaylight.mdsal.common.api.ReadFailedException)7 DataTreeCandidate (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate)7 Map (java.util.Map)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6