Search in sources :

Example 1 with BatchedModificationsReply

use of org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply in project controller by opendaylight.

the class ShardCommitCoordinator method handleBatchedModifications.

/**
 * This method handles a BatchedModifications message for a transaction being prepared directly on the
 * Shard actor instead of via a ShardTransaction actor. If there's no currently cached
 * DOMStoreWriteTransaction, one is created. The batched modifications are applied to the write Tx. If
 * the BatchedModifications is ready to commit then a DOMStoreThreePhaseCommitCohort is created.
 *
 * @param batched the BatchedModifications message to process
 * @param sender the sender of the message
 */
void handleBatchedModifications(final BatchedModifications batched, final ActorRef sender, final Shard shard) {
    CohortEntry cohortEntry = cohortCache.get(batched.getTransactionId());
    if (cohortEntry == null) {
        cohortEntry = CohortEntry.createOpen(dataTree.newReadWriteTransaction(batched.getTransactionId()), batched.getVersion());
        cohortCache.put(cohortEntry.getTransactionId(), cohortEntry);
    }
    if (log.isDebugEnabled()) {
        log.debug("{}: Applying {} batched modifications for Tx {}", name, batched.getModifications().size(), batched.getTransactionId());
    }
    cohortEntry.applyModifications(batched.getModifications());
    if (batched.isReady()) {
        if (cohortEntry.getLastBatchedModificationsException() != null) {
            cohortCache.remove(cohortEntry.getTransactionId());
            throw cohortEntry.getLastBatchedModificationsException();
        }
        if (cohortEntry.getTotalBatchedModificationsReceived() != batched.getTotalMessagesSent()) {
            cohortCache.remove(cohortEntry.getTransactionId());
            throw new IllegalStateException(String.format("The total number of batched messages received %d does not match the number sent %d", cohortEntry.getTotalBatchedModificationsReceived(), batched.getTotalMessagesSent()));
        }
        if (log.isDebugEnabled()) {
            log.debug("{}: Readying Tx {}, client version {}", name, batched.getTransactionId(), batched.getVersion());
        }
        cohortEntry.setDoImmediateCommit(batched.isDoCommitOnReady());
        cohortEntry.ready(cohortDecorator);
        if (batched.isDoCommitOnReady()) {
            cohortEntry.setReplySender(sender);
            cohortEntry.setShard(shard);
            handleCanCommit(cohortEntry);
        } else {
            sender.tell(readyTransactionReply(shard.self()), shard.self());
        }
    } else {
        sender.tell(new BatchedModificationsReply(batched.getModifications().size()), shard.self());
    }
}
Also used : BatchedModificationsReply(org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply)

Example 2 with BatchedModificationsReply

use of org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply in project controller by opendaylight.

the class ShardTransactionTest method testOnReceiveBatchedModifications.

@Test
public void testOnReceiveBatchedModifications() throws Exception {
    new TestKit(getSystem()) {

        {
            ShardDataTreeTransactionParent parent = Mockito.mock(ShardDataTreeTransactionParent.class);
            DataTreeModification mockModification = Mockito.mock(DataTreeModification.class);
            ReadWriteShardDataTreeTransaction mockWriteTx = new ReadWriteShardDataTreeTransaction(parent, nextTransactionId(), mockModification);
            final ActorRef transaction = newTransactionActor(RW, mockWriteTx, "testOnReceiveBatchedModifications");
            YangInstanceIdentifier writePath = TestModel.TEST_PATH;
            NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
            YangInstanceIdentifier mergePath = TestModel.OUTER_LIST_PATH;
            NormalizedNode<?, ?> mergeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME)).build();
            YangInstanceIdentifier deletePath = TestModel.TEST_PATH;
            BatchedModifications batched = new BatchedModifications(nextTransactionId(), DataStoreVersions.CURRENT_VERSION);
            batched.addModification(new WriteModification(writePath, writeData));
            batched.addModification(new MergeModification(mergePath, mergeData));
            batched.addModification(new DeleteModification(deletePath));
            transaction.tell(batched, getRef());
            BatchedModificationsReply reply = expectMsgClass(duration("5 seconds"), BatchedModificationsReply.class);
            assertEquals("getNumBatched", 3, reply.getNumBatched());
            InOrder inOrder = Mockito.inOrder(mockModification);
            inOrder.verify(mockModification).write(writePath, writeData);
            inOrder.verify(mockModification).merge(mergePath, mergeData);
            inOrder.verify(mockModification).delete(deletePath);
        }
    };
}
Also used : DataTreeModification(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification) WriteModification(org.opendaylight.controller.cluster.datastore.modification.WriteModification) InOrder(org.mockito.InOrder) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) DeleteModification(org.opendaylight.controller.cluster.datastore.modification.DeleteModification) TestKit(akka.testkit.javadsl.TestKit) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) BatchedModifications(org.opendaylight.controller.cluster.datastore.messages.BatchedModifications) MergeModification(org.opendaylight.controller.cluster.datastore.modification.MergeModification) BatchedModificationsReply(org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply) Test(org.junit.Test)

Example 3 with BatchedModificationsReply

use of org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply in project controller by opendaylight.

the class ShardTransactionTest method testOnReceiveBatchedModificationsReadyWithoutImmediateCommit.

@Test
public void testOnReceiveBatchedModificationsReadyWithoutImmediateCommit() throws Exception {
    new TestKit(getSystem()) {

        {
            final ActorRef transaction = newTransactionActor(WO, readWriteTransaction(), "testOnReceiveBatchedModificationsReadyWithoutImmediateCommit");
            TestKit watcher = new TestKit(getSystem());
            watcher.watch(transaction);
            YangInstanceIdentifier writePath = TestModel.TEST_PATH;
            NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
            final TransactionIdentifier tx1 = nextTransactionId();
            BatchedModifications batched = new BatchedModifications(tx1, DataStoreVersions.CURRENT_VERSION);
            batched.addModification(new WriteModification(writePath, writeData));
            transaction.tell(batched, getRef());
            BatchedModificationsReply reply = expectMsgClass(duration("5 seconds"), BatchedModificationsReply.class);
            assertEquals("getNumBatched", 1, reply.getNumBatched());
            batched = new BatchedModifications(tx1, DataStoreVersions.CURRENT_VERSION);
            batched.setReady(true);
            batched.setTotalMessagesSent(2);
            transaction.tell(batched, getRef());
            expectMsgClass(duration("5 seconds"), ReadyTransactionReply.class);
            watcher.expectMsgClass(duration("5 seconds"), Terminated.class);
        }
    };
}
Also used : WriteModification(org.opendaylight.controller.cluster.datastore.modification.WriteModification) BatchedModificationsReply(org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) TransactionIdentifier(org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier) TestKit(akka.testkit.javadsl.TestKit) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) BatchedModifications(org.opendaylight.controller.cluster.datastore.messages.BatchedModifications) Test(org.junit.Test)

Aggregations

BatchedModificationsReply (org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply)3 ActorRef (akka.actor.ActorRef)2 TestActorRef (akka.testkit.TestActorRef)2 TestKit (akka.testkit.javadsl.TestKit)2 Test (org.junit.Test)2 BatchedModifications (org.opendaylight.controller.cluster.datastore.messages.BatchedModifications)2 WriteModification (org.opendaylight.controller.cluster.datastore.modification.WriteModification)2 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)2 InOrder (org.mockito.InOrder)1 TransactionIdentifier (org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier)1 DeleteModification (org.opendaylight.controller.cluster.datastore.modification.DeleteModification)1 MergeModification (org.opendaylight.controller.cluster.datastore.modification.MergeModification)1 DataTreeModification (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification)1