Search in sources :

Example 1 with DOMStoreReadWriteTransaction

use of org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction in project controller by opendaylight.

the class ClientBackedDataStoreTest method testNewReadWriteTransaction.

@Test
public void testNewReadWriteTransaction() throws Exception {
    try (ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(actorContext, UNKNOWN_ID, clientActor)) {
        final DOMStoreReadWriteTransaction tx = clientBackedDataStore.newReadWriteTransaction();
        Assert.assertNotNull(tx);
        Mockito.verify(clientActor, Mockito.times(1)).createTransaction();
    }
}
Also used : DOMStoreReadWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction) Test(org.junit.Test)

Example 2 with DOMStoreReadWriteTransaction

use of org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction in project controller by opendaylight.

the class ConcurrentDOMDataBrokerTest method testSubmitWithOnlyOneSubTransaction.

@Test
public void testSubmitWithOnlyOneSubTransaction() throws InterruptedException {
    DOMStore configDomStore = mock(DOMStore.class);
    DOMStore operationalDomStore = mock(DOMStore.class);
    DOMStoreReadWriteTransaction mockStoreReadWriteTransaction = mock(DOMStoreReadWriteTransaction.class);
    DOMStoreThreePhaseCommitCohort mockCohort = mock(DOMStoreThreePhaseCommitCohort.class);
    doReturn(mockStoreReadWriteTransaction).when(operationalDomStore).newReadWriteTransaction();
    doReturn(mockCohort).when(mockStoreReadWriteTransaction).ready();
    doReturn(Futures.immediateFuture(false)).when(mockCohort).canCommit();
    doReturn(Futures.immediateFuture(null)).when(mockCohort).abort();
    final CountDownLatch latch = new CountDownLatch(1);
    final List<DOMStoreThreePhaseCommitCohort> commitCohorts = new ArrayList<>();
    try (ConcurrentDOMDataBroker dataBroker = new ConcurrentDOMDataBroker(ImmutableMap.of(LogicalDatastoreType.OPERATIONAL, operationalDomStore, LogicalDatastoreType.CONFIGURATION, configDomStore), futureExecutor) {

        @Override
        public CheckedFuture<Void, TransactionCommitFailedException> submit(DOMDataTreeWriteTransaction writeTx, Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
            commitCohorts.addAll(cohorts);
            latch.countDown();
            return super.submit(writeTx, cohorts);
        }
    }) {
        DOMDataTreeReadWriteTransaction domDataReadWriteTransaction = dataBroker.newReadWriteTransaction();
        domDataReadWriteTransaction.delete(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.EMPTY);
        domDataReadWriteTransaction.submit();
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        assertTrue(commitCohorts.size() == 1);
    }
}
Also used : TransactionCommitFailedException(org.opendaylight.mdsal.common.api.TransactionCommitFailedException) DOMDataTreeReadWriteTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction) DOMStore(org.opendaylight.mdsal.dom.spi.store.DOMStore) DOMDataTreeWriteTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction) DOMStoreReadWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction) ArrayList(java.util.ArrayList) Collection(java.util.Collection) CountDownLatch(java.util.concurrent.CountDownLatch) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) Test(org.junit.Test)

Example 3 with DOMStoreReadWriteTransaction

use of org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction 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 4 with DOMStoreReadWriteTransaction

use of org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction 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)

Example 5 with DOMStoreReadWriteTransaction

use of org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction in project controller by opendaylight.

the class DistributedDataStoreIntegrationTest method testReadWriteTransactionWithSingleShard.

@Test
public void testReadWriteTransactionWithSingleShard() throws Exception {
    new IntegrationTestKit(getSystem(), datastoreContextBuilder) {

        {
            try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "testReadWriteTransactionWithSingleShard", "test-1")) {
                // 1. Create a read-write Tx
                final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
                assertNotNull("newReadWriteTransaction returned null", readWriteTx);
                // 2. Write some data
                final YangInstanceIdentifier nodePath = TestModel.TEST_PATH;
                final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
                readWriteTx.write(nodePath, nodeToWrite);
                // 3. Read the data from Tx
                final Boolean exists = readWriteTx.exists(nodePath).checkedGet(5, TimeUnit.SECONDS);
                assertEquals("exists", true, exists);
                Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(nodePath).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", true, optional.isPresent());
                assertEquals("Data node", nodeToWrite, optional.get());
                // 4. Ready the Tx for commit
                final DOMStoreThreePhaseCommitCohort cohort = readWriteTx.ready();
                // 5. Commit the Tx
                doCommit(cohort);
                // 6. Verify the data in the store
                final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
                optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", true, optional.isPresent());
                assertEquals("Data node", nodeToWrite, optional.get());
            }
        }
    };
}
Also used : DOMStoreReadTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction) DOMStoreReadWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)22 DOMStoreReadWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction)22 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)11 DOMStoreReadTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction)10 DOMStoreThreePhaseCommitCohort (org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort)8 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)7 DOMStoreTransactionChain (org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain)6 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)6 AddressFromURIString (akka.actor.AddressFromURIString)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ExecutionException (java.util.concurrent.ExecutionException)5 RequestTimeoutException (org.opendaylight.controller.cluster.access.client.RequestTimeoutException)5 NoShardLeaderException (org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException)5 TransactionCommitFailedException (org.opendaylight.mdsal.common.api.TransactionCommitFailedException)5 DOMStoreWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction)5 DOMStore (org.opendaylight.mdsal.dom.spi.store.DOMStore)4 IOException (java.io.IOException)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 NotInitializedException (org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException)3 ReadFailedException (org.opendaylight.mdsal.common.api.ReadFailedException)3