Search in sources :

Example 6 with WriteModification

use of org.opendaylight.controller.cluster.datastore.modification.WriteModification in project controller by opendaylight.

the class AbstractShardTest method newBatchedModifications.

static BatchedModifications newBatchedModifications(final TransactionIdentifier transactionID, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final boolean ready, final boolean doCommitOnReady, final int messagesSent) {
    final BatchedModifications batched = new BatchedModifications(transactionID, CURRENT_VERSION);
    batched.addModification(new WriteModification(path, data));
    batched.setReady(ready);
    batched.setDoCommitOnReady(doCommitOnReady);
    batched.setTotalMessagesSent(messagesSent);
    return batched;
}
Also used : WriteModification(org.opendaylight.controller.cluster.datastore.modification.WriteModification) BatchedModifications(org.opendaylight.controller.cluster.datastore.messages.BatchedModifications)

Example 7 with WriteModification

use of org.opendaylight.controller.cluster.datastore.modification.WriteModification in project controller by opendaylight.

the class AbstractShardTest method prepareBatchedModifications.

protected static BatchedModifications prepareBatchedModifications(final TransactionIdentifier transactionID, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final boolean doCommitOnReady) {
    final MutableCompositeModification modification = new MutableCompositeModification();
    modification.addModification(new WriteModification(path, data));
    return prepareBatchedModifications(transactionID, modification, doCommitOnReady);
}
Also used : WriteModification(org.opendaylight.controller.cluster.datastore.modification.WriteModification) MutableCompositeModification(org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification)

Example 8 with WriteModification

use of org.opendaylight.controller.cluster.datastore.modification.WriteModification 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 9 with WriteModification

use of org.opendaylight.controller.cluster.datastore.modification.WriteModification 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 10 with WriteModification

use of org.opendaylight.controller.cluster.datastore.modification.WriteModification in project controller by opendaylight.

the class TransactionProxyTest method testWriteAfterAsyncRead.

@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testWriteAfterAsyncRead() throws Exception {
    ActorRef actorRef = setupActorContextWithoutInitialCreateTransaction(getSystem(), DefaultShardStrategy.DEFAULT_SHARD);
    Promise<Object> createTxPromise = akka.dispatch.Futures.promise();
    doReturn(createTxPromise).when(mockActorContext).executeOperationAsync(eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
    doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class));
    expectBatchedModificationsReady(actorRef);
    final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
    final TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
    final CountDownLatch readComplete = new CountDownLatch(1);
    final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
    com.google.common.util.concurrent.Futures.addCallback(transactionProxy.read(TestModel.TEST_PATH), new FutureCallback<Optional<NormalizedNode<?, ?>>>() {

        @Override
        public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
            try {
                transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
            } catch (Exception e) {
                caughtEx.set(e);
            } finally {
                readComplete.countDown();
            }
        }

        @Override
        public void onFailure(final Throwable failure) {
            caughtEx.set(failure);
            readComplete.countDown();
        }
    }, MoreExecutors.directExecutor());
    createTxPromise.success(createTransactionReply(actorRef, DataStoreVersions.CURRENT_VERSION));
    Uninterruptibles.awaitUninterruptibly(readComplete, 5, TimeUnit.SECONDS);
    final Throwable t = caughtEx.get();
    if (t != null) {
        Throwables.propagateIfPossible(t, Exception.class);
        throw new RuntimeException(t);
    }
    // This sends the batched modification.
    transactionProxy.ready();
    verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), true);
}
Also used : WriteModification(org.opendaylight.controller.cluster.datastore.modification.WriteModification) Optional(com.google.common.base.Optional) ActorRef(akka.actor.ActorRef) Timeout(akka.util.Timeout) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ReadFailedException(org.opendaylight.mdsal.common.api.ReadFailedException) NotInitializedException(org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException) PrimaryNotFoundException(org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException) NoShardLeaderException(org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException) TimeoutException(org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) NormalizedNodeAggregatorTest(org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest) Test(org.junit.Test)

Aggregations

WriteModification (org.opendaylight.controller.cluster.datastore.modification.WriteModification)30 Test (org.junit.Test)22 ActorRef (akka.actor.ActorRef)16 BatchedModifications (org.opendaylight.controller.cluster.datastore.messages.BatchedModifications)14 MergeModification (org.opendaylight.controller.cluster.datastore.modification.MergeModification)11 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)11 DataTreeModification (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification)8 TransactionIdentifier (org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier)7 NormalizedNodeAggregatorTest (org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest)7 Timeout (akka.util.Timeout)6 DeleteModification (org.opendaylight.controller.cluster.datastore.modification.DeleteModification)6 TestActorRef (akka.testkit.TestActorRef)4 TestKit (akka.testkit.javadsl.TestKit)4 ReadyLocalTransaction (org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction)4 Modification (org.opendaylight.controller.cluster.datastore.modification.Modification)4 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 CommitTransactionReply (org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply)3 DOMStoreThreePhaseCommitCohort (org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort)3