Search in sources :

Example 6 with DOMStoreWriteTransaction

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

the class DistributedDataStoreIntegrationTest method testTransactionCommitFailureWithShardNotInitialized.

@Test(expected = NotInitializedException.class)
@SuppressWarnings("checkstyle:IllegalCatch")
public void testTransactionCommitFailureWithShardNotInitialized() throws Exception {
    new IntegrationTestKit(getSystem(), datastoreContextBuilder) {

        {
            final String testName = "testTransactionCommitFailureWithShardNotInitialized";
            final String shardName = "test-1";
            // Set the shard initialization timeout low for the test.
            datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
            // Setup the InMemoryJournal to block shard recovery
            // indefinitely.
            final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
            final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
            InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
            InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
            final AbstractDataStore dataStore = setupAbstractDataStore(testParameter, testName, false, shardName);
            // Create the write Tx
            final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
            assertNotNull("newReadWriteTransaction returned null", writeTx);
            // Do some modifications and ready the Tx on a separate
            // thread.
            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));
                    txCohort.set(writeTx.ready());
                } catch (Exception e) {
                    caughtEx.set(e);
                } finally {
                    txReady.countDown();
                }
            });
            txThread.start();
            // Wait for the Tx operations to complete.
            boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
            if (caughtEx.get() != null) {
                throw caughtEx.get();
            }
            assertEquals("Tx ready", true, done);
            // have timed out and throw an appropriate exception cause.
            try {
                txCohort.get().canCommit().get(5, TimeUnit.SECONDS);
                fail("Expected NotInitializedException");
            } catch (final Exception e) {
                final Throwable root = Throwables.getRootCause(e);
                Throwables.throwIfUnchecked(root);
                throw new RuntimeException(root);
            } finally {
                blockRecoveryLatch.countDown();
            }
        }
    };
}
Also used : DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) AtomicReference(java.util.concurrent.atomic.AtomicReference) AddressFromURIString(akka.actor.AddressFromURIString) 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) Test(org.junit.Test)

Example 7 with DOMStoreWriteTransaction

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

the class DistributedDataStoreIntegrationTest method testCreateChainedTransactionWhenPreviousNotReady.

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

        {
            try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "testCreateChainedTransactionWhenPreviousNotReady", "test-1")) {
                final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
                final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
                assertNotNull("newWriteOnlyTransaction returned null", writeTx);
                writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
                // Try to create another Tx of each type - each should fail
                // b/c the previous Tx wasn't
                // readied.
                assertExceptionOnTxChainCreates(txChain, IllegalStateException.class);
            }
        }
    };
}
Also used : DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) DOMStoreTransactionChain(org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain) Test(org.junit.Test)

Example 8 with DOMStoreWriteTransaction

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

the class DistributedDataStoreIntegrationTest method testSingleTransactionsWritesInQuickSuccession.

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

        {
            try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "testSingleTransactionsWritesInQuickSuccession", "cars-1")) {
                final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
                DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
                writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
                writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
                doCommit(writeTx.ready());
                writeTx = txChain.newWriteOnlyTransaction();
                int numCars = 5;
                for (int i = 0; i < numCars; i++) {
                    writeTx.write(CarsModel.newCarPath("car" + i), CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
                }
                doCommit(writeTx.ready());
                final Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction().read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
                assertEquals("isPresent", true, optional.isPresent());
                assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
            }
        }
    };
}
Also used : DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) DOMStoreTransactionChain(org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Test(org.junit.Test)

Example 9 with DOMStoreWriteTransaction

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

the class DistributedDataStoreRemotingIntegrationTest method testTransactionChainWithMultipleShards.

@Test
public void testTransactionChainWithMultipleShards() throws Exception {
    initDatastoresWithCarsAndPeople("testTransactionChainWithMultipleShards");
    final DOMStoreTransactionChain txChain = followerDistributedDataStore.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());
    followerTestKit.doCommit(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(personPath);
    final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
    followerTestKit.doCommit(cohort2);
    followerTestKit.doCommit(cohort3);
    txChain.close();
    final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
    verifyCars(readTx, car);
    optional = readTx.read(personPath).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) 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) 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)

Example 10 with DOMStoreWriteTransaction

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

the class DistributedDataStoreRemotingIntegrationTest method testTransactionWithIsolatedLeader.

@Test
public void testTransactionWithIsolatedLeader() throws Exception {
    // TODO remove when test passes also for ClientBackedDataStore
    Assume.assumeTrue(testParameter.equals(DistributedDataStore.class));
    // Set the isolated leader check interval high so we can control the switch to IsolatedLeader.
    leaderDatastoreContextBuilder.shardIsolatedLeaderCheckIntervalInMillis(10000000);
    final String testName = "testTransactionWithIsolatedLeader";
    initDatastoresWithCars(testName);
    // Tx that is submitted after the follower is stopped but before the leader transitions to IsolatedLeader.
    final DOMStoreWriteTransaction preIsolatedLeaderWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
    preIsolatedLeaderWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
    // Tx that is submitted after the leader transitions to IsolatedLeader.
    final DOMStoreWriteTransaction noShardLeaderWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
    noShardLeaderWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
    // Tx that is submitted after the follower is reinstated.
    final DOMStoreWriteTransaction successWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
    successWriteTx.merge(CarsModel.BASE_PATH, CarsModel.emptyContainer());
    // Stop the follower
    followerTestKit.watch(followerDistributedDataStore.getActorContext().getShardManager());
    followerDistributedDataStore.close();
    followerTestKit.expectTerminated(followerDistributedDataStore.getActorContext().getShardManager());
    // Submit the preIsolatedLeaderWriteTx so it's pending
    final DOMStoreThreePhaseCommitCohort preIsolatedLeaderTxCohort = preIsolatedLeaderWriteTx.ready();
    // Change the isolated leader check interval low so it changes to IsolatedLeader.
    sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder.shardIsolatedLeaderCheckIntervalInMillis(200));
    MemberNode.verifyRaftState(leaderDistributedDataStore, "cars", raftState -> assertEquals("getRaftState", "IsolatedLeader", raftState.getRaftState()));
    try {
        leaderTestKit.doCommit(noShardLeaderWriteTx.ready());
        fail("Expected NoShardLeaderException");
    } catch (final ExecutionException e) {
        assertEquals("getCause", NoShardLeaderException.class, Throwables.getRootCause(e).getClass());
    }
    sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder.shardElectionTimeoutFactor(100));
    final DOMStoreThreePhaseCommitCohort successTxCohort = successWriteTx.ready();
    followerDistributedDataStore = followerTestKit.setupAbstractDataStore(testParameter, testName, MODULE_SHARDS_CARS_ONLY_1_2, false, CARS);
    leaderTestKit.doCommit(preIsolatedLeaderTxCohort);
    leaderTestKit.doCommit(successTxCohort);
}
Also used : DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) AddressFromURIString(akka.actor.AddressFromURIString) ExecutionException(java.util.concurrent.ExecutionException) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) NoShardLeaderException(org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException) 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