Search in sources :

Example 16 with DOMStoreReadWriteTransaction

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

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

the class DistributedDataStoreIntegrationTest method testTransactionReadFailureWithShardNotInitialized.

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

        {
            final String testName = "testTransactionReadFailureWithShardNotInitialized";
            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");
            try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, testName, false, shardName)) {
                // Create the read-write Tx
                final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
                assertNotNull("newReadWriteTransaction returned null", readWriteTx);
                // Do a read on the Tx on a separate thread.
                final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>> txReadFuture = new AtomicReference<>();
                final AtomicReference<Exception> caughtEx = new AtomicReference<>();
                final CountDownLatch txReadDone = new CountDownLatch(1);
                final Thread txThread = new Thread(() -> {
                    try {
                        readWriteTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
                        txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
                        readWriteTx.close();
                    } catch (Exception e) {
                        caughtEx.set(e);
                    } finally {
                        txReadDone.countDown();
                    }
                });
                txThread.start();
                // Wait for the Tx operations to complete.
                boolean done = Uninterruptibles.awaitUninterruptibly(txReadDone, 5, TimeUnit.SECONDS);
                if (caughtEx.get() != null) {
                    throw caughtEx.get();
                }
                assertEquals("Tx read done", true, done);
                // have timed out and throw an appropriate exception cause.
                try {
                    txReadFuture.get().checkedGet(5, TimeUnit.SECONDS);
                    fail("Expected NotInitializedException");
                } catch (final ReadFailedException e) {
                    final Throwable root = Throwables.getRootCause(e);
                    Throwables.throwIfUnchecked(root);
                    throw new RuntimeException(root);
                } finally {
                    blockRecoveryLatch.countDown();
                }
            }
        }
    };
}
Also used : ReadFailedException(org.opendaylight.mdsal.common.api.ReadFailedException) 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 18 with DOMStoreReadWriteTransaction

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

the class DistributedDataStoreRemotingIntegrationTest method testTransactionWithCreateTxFailureDueToNoLeader.

@Test
public void testTransactionWithCreateTxFailureDueToNoLeader() throws Exception {
    followerDatastoreContextBuilder.frontendRequestTimeoutInSeconds(2);
    initDatastoresWithCars("testTransactionWithCreateTxFailureDueToNoLeader");
    // Do an initial read to get the primary shard info cached.
    final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
    readTx.read(CarsModel.BASE_PATH).checkedGet(5, TimeUnit.SECONDS);
    // Shutdown the leader and try to create a new tx.
    TestKit.shutdownActorSystem(leaderSystem, true);
    Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
    Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
    sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder.operationTimeoutInMillis(10).shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
    final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
    rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
    try {
        followerTestKit.doCommit(rwTx.ready());
        fail("Exception expected");
    } catch (final ExecutionException e) {
        final String msg = "Unexpected exception: " + Throwables.getStackTraceAsString(e.getCause());
        if (DistributedDataStore.class.equals(testParameter)) {
            assertTrue(msg, Throwables.getRootCause(e) instanceof NoShardLeaderException);
        } else {
            assertTrue(msg, Throwables.getRootCause(e) instanceof RequestTimeoutException);
        }
    }
}
Also used : RequestTimeoutException(org.opendaylight.controller.cluster.access.client.RequestTimeoutException) DOMStoreReadTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction) DOMStoreReadWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction) AddressFromURIString(akka.actor.AddressFromURIString) ExecutionException(java.util.concurrent.ExecutionException) NoShardLeaderException(org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException) Test(org.junit.Test)

Example 19 with DOMStoreReadWriteTransaction

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

the class DistributedDataStoreRemotingIntegrationTest method testTransactionRetryWithInitialAskTimeoutExOnCreateTx.

@Test
public void testTransactionRetryWithInitialAskTimeoutExOnCreateTx() throws Exception {
    followerDatastoreContextBuilder.backendAlivenessTimerIntervalInSeconds(2);
    String testName = "testTransactionRetryWithInitialAskTimeoutExOnCreateTx";
    initDatastores(testName, MODULE_SHARDS_CARS_1_2_3, CARS);
    final DatastoreContext.Builder follower2DatastoreContextBuilder = DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(10);
    final IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System, follower2DatastoreContextBuilder, commitTimeout);
    try (AbstractDataStore ds = follower2TestKit.setupAbstractDataStore(testParameter, testName, MODULE_SHARDS_CARS_1_2_3, false, CARS)) {
        followerTestKit.waitForMembersUp("member-1", "member-3");
        follower2TestKit.waitForMembersUp("member-1", "member-2");
        // Do an initial read to get the primary shard info cached.
        final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
        readTx.read(CarsModel.BASE_PATH).checkedGet(5, TimeUnit.SECONDS);
        // Shutdown the leader and try to create a new tx.
        TestKit.shutdownActorSystem(leaderSystem, true);
        Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
        sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder.operationTimeoutInMillis(500).shardElectionTimeoutFactor(5).customRaftPolicyImplementation(null));
        final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
        rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
        followerTestKit.doCommit(rwTx.ready());
    }
}
Also used : DOMStoreReadTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction) DOMStoreReadWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction) Builder(org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder) AddressFromURIString(akka.actor.AddressFromURIString) Test(org.junit.Test)

Example 20 with DOMStoreReadWriteTransaction

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

the class DistributedDataStoreRemotingIntegrationTest method testReadWriteTransactionWithSingleShard.

@Test
public void testReadWriteTransactionWithSingleShard() throws Exception {
    initDatastoresWithCars("testReadWriteTransactionWithSingleShard");
    final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
    assertNotNull("newReadWriteTransaction returned null", rwTx);
    rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
    rwTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
    final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
    rwTx.merge(CarsModel.newCarPath("optima"), car1);
    verifyCars(rwTx, car1);
    final MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
    final YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
    rwTx.merge(car2Path, car2);
    verifyExists(rwTx, car2Path);
    followerTestKit.doCommit(rwTx.ready());
    verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
}
Also used : 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) 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