Search in sources :

Example 6 with DOMStoreThreePhaseCommitCohort

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

the class DistributedDataStoreIntegrationTest method testTransactionAbort.

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

        {
            try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "transactionAbortIntegrationTest", "test-1")) {
                final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
                assertNotNull("newWriteOnlyTransaction returned null", writeTx);
                writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
                final DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
                cohort.canCommit().get(5, TimeUnit.SECONDS);
                cohort.abort().get(5, TimeUnit.SECONDS);
                testWriteTransaction(dataStore, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
            }
        }
    };
}
Also used : DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) Test(org.junit.Test)

Example 7 with DOMStoreThreePhaseCommitCohort

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

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

the class DistributedDataStoreIntegrationTest method testTransactionCommitFailureWithNoShardLeader.

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

        {
            final String shardName = "default";
            // We don't want the shard to become the leader so prevent shard
            // elections.
            datastoreContextBuilder.customRaftPolicyImplementation("org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy");
            // The ShardManager uses the election timeout for FindPrimary so
            // reset it low so it will timeout quickly.
            datastoreContextBuilder.shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(1).shardInitializationTimeout(200, TimeUnit.MILLISECONDS).frontendRequestTimeoutInSeconds(2);
            try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, testName, false, shardName)) {
                final Object result = dataStore.getActorContext().executeOperation(dataStore.getActorContext().getShardManager(), new FindLocalShard(shardName, true));
                assertTrue("Expected LocalShardFound. Actual: " + result, result instanceof LocalShardFound);
                // Create the write Tx.
                DOMStoreWriteTransaction writeTxToClose = null;
                try {
                    writeTxToClose = writeOnly ? dataStore.newWriteOnlyTransaction() : dataStore.newReadWriteTransaction();
                    final DOMStoreWriteTransaction writeTx = writeTxToClose;
                    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.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_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);
                    // exception cause.
                    try {
                        txCohort.get().canCommit().get(10, TimeUnit.SECONDS);
                        fail("Expected NoShardLeaderException");
                    } catch (final ExecutionException e) {
                        final String msg = "Unexpected exception: " + Throwables.getStackTraceAsString(e.getCause());
                        if (DistributedDataStore.class.equals(testParameter)) {
                            assertTrue(Throwables.getRootCause(e) instanceof NoShardLeaderException);
                        } else {
                            assertTrue(msg, Throwables.getRootCause(e) instanceof RequestTimeoutException);
                        }
                    }
                } finally {
                    try {
                        if (writeTxToClose != null) {
                            writeTxToClose.close();
                        }
                    } catch (Exception e) {
                    // FIXME TransactionProxy.close throws IllegalStateException:
                    // Transaction is ready, it cannot be closed
                    }
                }
            }
        }
    };
}
Also used : LocalShardFound(org.opendaylight.controller.cluster.datastore.messages.LocalShardFound) DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) FindLocalShard(org.opendaylight.controller.cluster.datastore.messages.FindLocalShard) 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) NoShardLeaderException(org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException) RequestTimeoutException(org.opendaylight.controller.cluster.access.client.RequestTimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with DOMStoreThreePhaseCommitCohort

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

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

DOMStoreThreePhaseCommitCohort (org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort)40 Test (org.junit.Test)31 DOMStoreWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction)17 ActorRef (akka.actor.ActorRef)11 TransactionCommitFailedException (org.opendaylight.mdsal.common.api.TransactionCommitFailedException)11 NormalizedNodeAggregatorTest (org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest)10 DOMStoreReadTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction)8 DOMStoreReadWriteTransaction (org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction)8 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)8 CountDownLatch (java.util.concurrent.CountDownLatch)6 ExecutionException (java.util.concurrent.ExecutionException)6 NoShardLeaderException (org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException)6 CommitTransactionReply (org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply)6 ReadFailedException (org.opendaylight.mdsal.common.api.ReadFailedException)6 AddressFromURIString (akka.actor.AddressFromURIString)5 BatchedModifications (org.opendaylight.controller.cluster.datastore.messages.BatchedModifications)5 DOMStoreTransactionChain (org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain)5 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)5 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)5 IOException (java.io.IOException)4