Search in sources :

Example 16 with DOMStoreWriteTransaction

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

the class DataTreeCohortIntegrationTest method testAbortAfterCanCommit.

/**
 * FIXME: Since we invoke DOMDataTreeCommitCohort#canCommit on preCommit (as that's when we generate a
 * DataTreeCandidate) and since currently preCommit is a noop in the Shard backend (it is combined with commit),
 * we can't actually test abort after canCommit.
 */
@SuppressWarnings("unchecked")
@Test
@Ignore
public void testAbortAfterCanCommit() throws Exception {
    final DOMDataTreeCommitCohort cohortToAbort = mock(DOMDataTreeCommitCohort.class);
    final PostCanCommitStep stepToAbort = mock(PostCanCommitStep.class);
    doReturn(ThreePhaseCommitStep.NOOP_ABORT_FUTURE).when(stepToAbort).abort();
    doReturn(PostPreCommitStep.NOOP_FUTURE).when(stepToAbort).preCommit();
    doReturn(Futures.immediateCheckedFuture(stepToAbort)).when(cohortToAbort).canCommit(any(Object.class), any(Collection.class), any(SchemaContext.class));
    IntegrationTestKit kit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
    try (AbstractDataStore dataStore = kit.setupAbstractDataStore(DistributedDataStore.class, "testAbortAfterCanCommit", "test-1", "cars-1")) {
        dataStore.registerCommitCohort(TEST_ID, cohortToAbort);
        IntegrationTestKit.verifyShardState(dataStore, "test-1", state -> assertEquals("Cohort registrations", 1, state.getCommitCohortActors().size()));
        DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
        writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
        writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
        DOMStoreThreePhaseCommitCohort dsCohort = writeTx.ready();
        dsCohort.canCommit().get(5, TimeUnit.SECONDS);
        dsCohort.preCommit().get(5, TimeUnit.SECONDS);
        dsCohort.abort().get(5, TimeUnit.SECONDS);
        verify(stepToAbort).abort();
    }
}
Also used : PostCanCommitStep(org.opendaylight.mdsal.common.api.PostCanCommitStep) DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) Collection(java.util.Collection) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) DOMDataTreeCommitCohort(org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with DOMStoreWriteTransaction

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

the class TransactionChainProxyTest method testChainedReadWriteTransactions.

/**
 * Tests 2 successive chained read-write transactions and verifies the second transaction isn't
 * initiated until the first one completes its read future.
 */
@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testChainedReadWriteTransactions() throws Exception {
    try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) {
        ActorRef txActorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
        expectBatchedModifications(txActorRef1, 1);
        Promise<Object> readyReplyPromise1 = akka.dispatch.Futures.promise();
        doReturn(readyReplyPromise1.future()).when(mockActorContext).executeOperationAsync(eq(actorSelection(txActorRef1)), isA(BatchedModifications.class), any(Timeout.class));
        DOMStoreWriteTransaction writeTx1 = txChainProxy.newReadWriteTransaction();
        NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
        writeTx1.write(TestModel.TEST_PATH, writeNode1);
        writeTx1.ready();
        verifyOneBatchedModification(txActorRef1, new WriteModification(TestModel.TEST_PATH, writeNode1), true);
        String tx2MemberName = "mock-member";
        ActorRef shardActorRef2 = setupActorContextWithoutInitialCreateTransaction(getSystem());
        ActorRef txActorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE, DataStoreVersions.CURRENT_VERSION, tx2MemberName, shardActorRef2);
        expectBatchedModifications(txActorRef2, 1);
        final NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
        final DOMStoreWriteTransaction writeTx2 = txChainProxy.newReadWriteTransaction();
        final AtomicReference<Exception> caughtEx = new AtomicReference<>();
        final CountDownLatch write2Complete = new CountDownLatch(1);
        new Thread(() -> {
            try {
                writeTx2.write(TestModel.OUTER_LIST_PATH, writeNode2);
            } catch (Exception e) {
                caughtEx.set(e);
            } finally {
                write2Complete.countDown();
            }
        }).start();
        assertEquals("Tx 2 write should've completed", true, write2Complete.await(5, TimeUnit.SECONDS));
        if (caughtEx.get() != null) {
            throw caughtEx.get();
        }
        try {
            verify(mockActorContext, never()).executeOperationAsync(eq(getSystem().actorSelection(shardActorRef2.path())), eqCreateTransaction(tx2MemberName, READ_WRITE));
        } catch (AssertionError e) {
            fail("Tx 2 should not have initiated until the Tx 1's ready future completed");
        }
        readyReplyPromise1.success(readyTxReply(txActorRef1.path().toString()).value().get().get());
        verify(mockActorContext, timeout(5000)).executeOperationAsync(eq(getSystem().actorSelection(shardActorRef2.path())), eqCreateTransaction(tx2MemberName, READ_WRITE), any(Timeout.class));
    }
}
Also used : WriteModification(org.opendaylight.controller.cluster.datastore.modification.WriteModification) ActorRef(akka.actor.ActorRef) Timeout(akka.util.Timeout) DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) BatchedModifications(org.opendaylight.controller.cluster.datastore.messages.BatchedModifications) Test(org.junit.Test)

Example 18 with DOMStoreWriteTransaction

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

the class TransactionChainProxyTest method testChainedWriteOnlyTransactions.

/**
 * Tests 2 successive chained write-only transactions and verifies the second transaction isn't
 * initiated until the first one completes its read future.
 */
@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testChainedWriteOnlyTransactions() throws Exception {
    dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
    try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) {
        ActorRef txActorRef1 = setupActorContextWithoutInitialCreateTransaction(getSystem());
        Promise<Object> batchedReplyPromise1 = akka.dispatch.Futures.promise();
        doReturn(batchedReplyPromise1.future()).when(mockActorContext).executeOperationAsync(eq(actorSelection(txActorRef1)), isA(BatchedModifications.class), any(Timeout.class));
        DOMStoreWriteTransaction writeTx1 = txChainProxy.newWriteOnlyTransaction();
        NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
        writeTx1.write(TestModel.TEST_PATH, writeNode1);
        writeTx1.ready();
        verify(mockActorContext, times(1)).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
        verifyOneBatchedModification(txActorRef1, new WriteModification(TestModel.TEST_PATH, writeNode1), true);
        ActorRef txActorRef2 = setupActorContextWithoutInitialCreateTransaction(getSystem());
        expectBatchedModifications(txActorRef2, 1);
        final NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
        final DOMStoreWriteTransaction writeTx2 = txChainProxy.newWriteOnlyTransaction();
        final AtomicReference<Exception> caughtEx = new AtomicReference<>();
        final CountDownLatch write2Complete = new CountDownLatch(1);
        new Thread(() -> {
            try {
                writeTx2.write(TestModel.OUTER_LIST_PATH, writeNode2);
            } catch (Exception e) {
                caughtEx.set(e);
            } finally {
                write2Complete.countDown();
            }
        }).start();
        assertEquals("Tx 2 write should've completed", true, write2Complete.await(5, TimeUnit.SECONDS));
        if (caughtEx.get() != null) {
            throw caughtEx.get();
        }
        try {
            verify(mockActorContext, times(1)).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
        } catch (AssertionError e) {
            fail("Tx 2 should not have initiated until the Tx 1's ready future completed");
        }
        batchedReplyPromise1.success(readyTxReply(txActorRef1.path().toString()).value().get().get());
        // Tx 2 should've proceeded to find the primary shard.
        verify(mockActorContext, timeout(5000).times(2)).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
    }
}
Also used : WriteModification(org.opendaylight.controller.cluster.datastore.modification.WriteModification) ActorRef(akka.actor.ActorRef) Timeout(akka.util.Timeout) DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) BatchedModifications(org.opendaylight.controller.cluster.datastore.messages.BatchedModifications) Test(org.junit.Test)

Example 19 with DOMStoreWriteTransaction

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

the class ClusterAdminRpcServiceTest method writeCarsNodeAndVerify.

private static NormalizedNode<?, ?> writeCarsNodeAndVerify(AbstractDataStore writeToStore, AbstractDataStore readFromStore) throws Exception {
    DOMStoreWriteTransaction writeTx = writeToStore.newWriteOnlyTransaction();
    NormalizedNode<?, ?> carsNode = CarsModel.create();
    writeTx.write(CarsModel.BASE_PATH, carsNode);
    DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
    Boolean canCommit = cohort.canCommit().get(7, TimeUnit.SECONDS);
    assertEquals("canCommit", TRUE, canCommit);
    cohort.preCommit().get(5, TimeUnit.SECONDS);
    cohort.commit().get(5, TimeUnit.SECONDS);
    readCarsNodeAndVerify(readFromStore, carsNode);
    return carsNode;
}
Also used : DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort)

Example 20 with DOMStoreWriteTransaction

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

the class NormalizedNodeAggregatorTest method getRootNode.

public static NormalizedNode<?, ?> getRootNode(NormalizedNode<?, ?> moduleNode, SchemaContext schemaContext) throws ReadFailedException, ExecutionException, InterruptedException {
    try (InMemoryDOMDataStore store = new InMemoryDOMDataStore("test", Executors.newSingleThreadExecutor())) {
        store.onGlobalContextUpdated(schemaContext);
        DOMStoreWriteTransaction writeTransaction = store.newWriteOnlyTransaction();
        writeTransaction.merge(YangInstanceIdentifier.of(moduleNode.getNodeType()), moduleNode);
        DOMStoreThreePhaseCommitCohort ready = writeTransaction.ready();
        ready.canCommit().get();
        ready.preCommit().get();
        ready.commit().get();
        DOMStoreReadTransaction readTransaction = store.newReadOnlyTransaction();
        CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = readTransaction.read(YangInstanceIdentifier.EMPTY);
        Optional<NormalizedNode<?, ?>> nodeOptional = read.checkedGet();
        return nodeOptional.get();
    }
}
Also used : ReadFailedException(org.opendaylight.mdsal.common.api.ReadFailedException) Optional(com.google.common.base.Optional) DOMStoreWriteTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction) DOMStoreReadTransaction(org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction) InMemoryDOMDataStore(org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort)

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