Search in sources :

Example 21 with DOMStoreWriteTransaction

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

the class ClientBackedDataStoreTest method testNewWriteOnlyTransaction.

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

Example 22 with DOMStoreWriteTransaction

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

the class DistributedDataStoreIntegrationTest method testChainWithReadOnlyTxAfterPreviousReady.

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

        {
            try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "testChainWithReadOnlyTxAfterPreviousReady", "test-1")) {
                final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
                // Create a write tx and submit.
                final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
                writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
                final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
                // Create read-only tx's and issue a read.
                CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture1 = txChain.newReadOnlyTransaction().read(TestModel.TEST_PATH);
                CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture2 = txChain.newReadOnlyTransaction().read(TestModel.TEST_PATH);
                // Create another write tx and issue the write.
                DOMStoreWriteTransaction writeTx2 = txChain.newWriteOnlyTransaction();
                writeTx2.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
                // Ensure the reads succeed.
                assertEquals("isPresent", true, readFuture1.checkedGet(5, TimeUnit.SECONDS).isPresent());
                assertEquals("isPresent", true, readFuture2.checkedGet(5, TimeUnit.SECONDS).isPresent());
                // Ensure the writes succeed.
                DOMStoreThreePhaseCommitCohort cohort2 = writeTx2.ready();
                doCommit(cohort1);
                doCommit(cohort2);
                assertEquals("isPresent", true, txChain.newReadOnlyTransaction().read(TestModel.OUTER_LIST_PATH).checkedGet(5, TimeUnit.SECONDS).isPresent());
            }
        }
    };
}
Also used : ReadFailedException(org.opendaylight.mdsal.common.api.ReadFailedException) Optional(com.google.common.base.Optional) DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) DOMStoreTransactionChain(org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) Test(org.junit.Test)

Example 23 with DOMStoreWriteTransaction

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

the class DistributedDataStoreIntegrationTest method testTransactionWritesWithShardNotInitiallyReady.

@SuppressWarnings("checkstyle:IllegalCatch")
private void testTransactionWritesWithShardNotInitiallyReady(final String testName, final boolean writeOnly) throws Exception {
    new IntegrationTestKit(getSystem(), datastoreContextBuilder) {

        {
            final String shardName = "test-1";
            // Setup the InMemoryJournal to block shard recovery to ensure
            // the shard isn't
            // initialized until we create and submit the write 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 write Tx
                final DOMStoreWriteTransaction writeTx = writeOnly ? dataStore.newWriteOnlyTransaction() : dataStore.newReadWriteTransaction();
                assertNotNull("newReadWriteTransaction returned null", writeTx);
                // Do some modification operations and ready the Tx on a
                // separate thread.
                final YangInstanceIdentifier listEntryPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
                final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
                final AtomicReference<Exception> caughtEx = new AtomicReference<>();
                final CountDownLatch txReady = new CountDownLatch(1);
                final Thread txThread = new Thread(() -> {
                    try {
                        writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
                        writeTx.merge(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
                        writeTx.write(listEntryPath, ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
                        writeTx.delete(listEntryPath);
                        txCohort.set(writeTx.ready());
                    } catch (Exception e) {
                        caughtEx.set(e);
                    } finally {
                        txReady.countDown();
                    }
                });
                txThread.start();
                // Wait for the Tx operations to complete.
                final boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
                if (caughtEx.get() != null) {
                    throw caughtEx.get();
                }
                assertEquals("Tx ready", 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 Tx commit to complete.
                doCommit(txCohort.get());
                // Verify the data in the store
                final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
                Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", true, optional.isPresent());
                optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", true, optional.isPresent());
                optional = readTx.read(listEntryPath).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", false, optional.isPresent());
            }
        }
    };
}
Also used : DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) DOMStoreReadTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction) AtomicReference(java.util.concurrent.atomic.AtomicReference) AddressFromURIString(akka.actor.AddressFromURIString) CountDownLatch(java.util.concurrent.CountDownLatch) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) 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)

Example 24 with DOMStoreWriteTransaction

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

the class DistributedDataStoreIntegrationTest method testTransactionChainWithMultipleShards.

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

        {
            try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "testTransactionChainWithMultipleShards", "cars-1", "people-1")) {
                final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
                DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
                assertNotNull("newWriteOnlyTransaction returned null", writeTx);
                writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
                writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
                writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
                writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
                final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
                final DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
                final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
                final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
                readWriteTx.write(carPath, car);
                final MapEntryNode person = PeopleModel.newPersonEntry("jack");
                final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
                readWriteTx.merge(personPath, person);
                Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", true, optional.isPresent());
                assertEquals("Data node", car, optional.get());
                optional = readWriteTx.read(personPath).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", true, optional.isPresent());
                assertEquals("Data node", person, optional.get());
                final DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
                writeTx = txChain.newWriteOnlyTransaction();
                writeTx.delete(carPath);
                final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
                final ListenableFuture<Boolean> canCommit1 = cohort1.canCommit();
                final ListenableFuture<Boolean> canCommit2 = cohort2.canCommit();
                doCommit(canCommit1, cohort1);
                doCommit(canCommit2, cohort2);
                doCommit(cohort3);
                txChain.close();
                final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
                optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", false, optional.isPresent());
                optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", true, optional.isPresent());
                assertEquals("Data node", person, 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) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Test(org.junit.Test)

Example 25 with DOMStoreWriteTransaction

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

the class DistributedDataStoreRemotingIntegrationTest method testSingleShardTransactionsWithLeaderChanges.

@Test
public void testSingleShardTransactionsWithLeaderChanges() throws Exception {
    followerDatastoreContextBuilder.backendAlivenessTimerIntervalInSeconds(2);
    final String testName = "testSingleShardTransactionsWithLeaderChanges";
    initDatastoresWithCars(testName);
    final String followerCarShardName = "member-2-shard-cars-" + testName;
    InMemoryJournal.addWriteMessagesCompleteLatch(followerCarShardName, 1, ApplyJournalEntries.class);
    // Write top-level car container from the follower so it uses a remote Tx.
    DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
    writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
    writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
    followerTestKit.doCommit(writeTx.ready());
    InMemoryJournal.waitForWriteMessagesComplete(followerCarShardName);
    // Switch the leader to the follower
    sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder.shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
    TestKit.shutdownActorSystem(leaderSystem, true);
    Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
    followerTestKit.waitUntilNoLeader(followerDistributedDataStore.getActorContext(), CARS);
    leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
    Cluster.get(leaderSystem).join(MEMBER_2_ADDRESS);
    final DatastoreContext.Builder newMember1Builder = DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
    IntegrationTestKit newMember1TestKit = new IntegrationTestKit(leaderSystem, newMember1Builder, commitTimeout);
    try (AbstractDataStore ds = newMember1TestKit.setupAbstractDataStore(testParameter, testName, MODULE_SHARDS_CARS_ONLY_1_2, false, CARS)) {
        followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), CARS);
        // Write a car entry to the new leader - should switch to local Tx
        writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
        MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
        YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
        writeTx.merge(car1Path, car1);
        followerTestKit.doCommit(writeTx.ready());
        verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1);
    }
}
Also used : DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) Builder(org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder) AddressFromURIString(akka.actor.AddressFromURIString) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Aggregations

DOMStoreWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction)30 Test (org.junit.Test)25 DOMStoreThreePhaseCommitCohort (org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort)17 DOMStoreReadTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction)9 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)9 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)9 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)9 AddressFromURIString (akka.actor.AddressFromURIString)7 DOMStoreTransactionChain (org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 ExecutionException (java.util.concurrent.ExecutionException)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ReadFailedException (org.opendaylight.mdsal.common.api.ReadFailedException)6 NoShardLeaderException (org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException)5 DOMStoreReadWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction)5 ActorRef (akka.actor.ActorRef)4 IOException (java.io.IOException)4 RequestTimeoutException (org.opendaylight.controller.cluster.access.client.RequestTimeoutException)4 NotInitializedException (org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException)4 TransactionChainClosedException (org.opendaylight.mdsal.common.api.TransactionChainClosedException)4