Search in sources :

Example 1 with TransactionIdentifier

use of org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier in project controller by opendaylight.

the class EntityOwnershipShardCommitCoordinator method newBatchedModifications.

private BatchedModifications newBatchedModifications() {
    BatchedModifications modifications = new BatchedModifications(new TransactionIdentifier(historyId, ++transactionIDCounter), DataStoreVersions.CURRENT_VERSION);
    modifications.setDoCommitOnReady(true);
    modifications.setReady(true);
    modifications.setTotalMessagesSent(1);
    return modifications;
}
Also used : TransactionIdentifier(org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier) BatchedModifications(org.opendaylight.controller.cluster.datastore.messages.BatchedModifications)

Example 2 with TransactionIdentifier

use of org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier in project controller by opendaylight.

the class ShardTest method testApplyState.

@Test
public void testApplyState() throws Exception {
    final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testApplyState");
    ShardTestKit.waitUntilLeader(shard);
    final DataTree store = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SCHEMA_CONTEXT);
    final DataTreeModification writeMod = store.takeSnapshot().newModification();
    final ContainerNode node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
    writeMod.write(TestModel.TEST_PATH, node);
    writeMod.ready();
    final TransactionIdentifier tx = nextTransactionId();
    shard.underlyingActor().applyState(null, null, payloadForModification(store, writeMod, tx));
    final Stopwatch sw = Stopwatch.createStarted();
    while (sw.elapsed(TimeUnit.SECONDS) <= 5) {
        Uninterruptibles.sleepUninterruptibly(75, TimeUnit.MILLISECONDS);
        final NormalizedNode<?, ?> actual = readStore(shard, TestModel.TEST_PATH);
        if (actual != null) {
            assertEquals("Applied state", node, actual);
            return;
        }
    }
    fail("State was not applied");
}
Also used : DataTreeModification(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification) DataTree(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree) TransactionIdentifier(org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier) Stopwatch(com.google.common.base.Stopwatch) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) InMemoryDataTreeFactory(org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory) Test(org.junit.Test)

Example 3 with TransactionIdentifier

use of org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier in project controller by opendaylight.

the class ShardTest method testReadyLocalTransactionWithThreePhaseCommit.

@Test
public void testReadyLocalTransactionWithThreePhaseCommit() throws Exception {
    new ShardTestKit(getSystem()) {

        {
            final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testReadyLocalTransactionWithThreePhaseCommit");
            waitUntilLeader(shard);
            final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
            final DataTreeModification modification = dataStore.newModification();
            final ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
            new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
            final MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
            new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
            final TransactionIdentifier txId = nextTransactionId();
            modification.ready();
            final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(txId, modification, false);
            shard.tell(readyMessage, getRef());
            expectMsgClass(ReadyTransactionReply.class);
            // Send the CanCommitTransaction message.
            shard.tell(new CanCommitTransaction(txId, CURRENT_VERSION).toSerializable(), getRef());
            final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(expectMsgClass(CanCommitTransactionReply.class));
            assertEquals("Can commit", true, canCommitReply.getCanCommit());
            // Send the CanCommitTransaction message.
            shard.tell(new CommitTransaction(txId, CURRENT_VERSION).toSerializable(), getRef());
            expectMsgClass(CommitTransactionReply.class);
            final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.OUTER_LIST_PATH);
            assertEquals(TestModel.OUTER_LIST_QNAME.getLocalName(), mergeData, actualNode);
        }
    };
}
Also used : DataTreeModification(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification) WriteModification(org.opendaylight.controller.cluster.datastore.modification.WriteModification) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) CanCommitTransactionReply(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply) MergeModification(org.opendaylight.controller.cluster.datastore.modification.MergeModification) CanCommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction) CommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CommitTransaction) ReadyLocalTransaction(org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction) TransactionIdentifier(org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier) CanCommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) Test(org.junit.Test)

Example 4 with TransactionIdentifier

use of org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier in project controller by opendaylight.

the class ShardTest method testBatchedModificationsWithNoCommitOnReady.

@Test
public void testBatchedModificationsWithNoCommitOnReady() throws Exception {
    new ShardTestKit(getSystem()) {

        {
            final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testBatchedModificationsWithNoCommitOnReady");
            waitUntilLeader(shard);
            final TransactionIdentifier transactionID = nextTransactionId();
            final FiniteDuration duration = duration("5 seconds");
            // Send a BatchedModifications to start a transaction.
            shard.tell(newBatchedModifications(transactionID, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), false, false, 1), getRef());
            expectMsgClass(duration, BatchedModificationsReply.class);
            // Send a couple more BatchedModifications.
            shard.tell(newBatchedModifications(transactionID, TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false, false, 2), getRef());
            expectMsgClass(duration, BatchedModificationsReply.class);
            shard.tell(newBatchedModifications(transactionID, YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(), ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), true, false, 3), getRef());
            expectMsgClass(duration, ReadyTransactionReply.class);
            // Send the CanCommitTransaction message.
            shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
            final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(expectMsgClass(duration, CanCommitTransactionReply.class));
            assertEquals("Can commit", true, canCommitReply.getCanCommit());
            // Send the CommitTransaction message.
            shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
            expectMsgClass(duration, CommitTransactionReply.class);
            // Verify data in the data store.
            verifyOuterListEntry(shard, 1);
        }
    };
}
Also used : CanCommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction) CommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CommitTransaction) TransactionIdentifier(org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier) CanCommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction) FiniteDuration(scala.concurrent.duration.FiniteDuration) CanCommitTransactionReply(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply) Test(org.junit.Test)

Example 5 with TransactionIdentifier

use of org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier in project controller by opendaylight.

the class ShardTest method testConcurrentThreePhaseCommits.

@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testConcurrentThreePhaseCommits() throws Exception {
    final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
    final CountDownLatch commitLatch = new CountDownLatch(2);
    final long timeoutSec = 5;
    final FiniteDuration duration = FiniteDuration.create(timeoutSec, TimeUnit.SECONDS);
    final Timeout timeout = new Timeout(duration);
    final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testConcurrentThreePhaseCommits");
    class OnFutureComplete extends OnComplete<Object> {

        private final Class<?> expRespType;

        OnFutureComplete(final Class<?> expRespType) {
            this.expRespType = expRespType;
        }

        @Override
        public void onComplete(final Throwable error, final Object resp) {
            if (error != null) {
                caughtEx.set(new AssertionError(getClass().getSimpleName() + " failure", error));
            } else {
                try {
                    assertEquals("Commit response type", expRespType, resp.getClass());
                    onSuccess(resp);
                } catch (final Exception e) {
                    caughtEx.set(e);
                }
            }
        }

        void onSuccess(final Object resp) throws Exception {
        }
    }
    class OnCommitFutureComplete extends OnFutureComplete {

        OnCommitFutureComplete() {
            super(CommitTransactionReply.class);
        }

        @Override
        public void onComplete(final Throwable error, final Object resp) {
            super.onComplete(error, resp);
            commitLatch.countDown();
        }
    }
    class OnCanCommitFutureComplete extends OnFutureComplete {

        private final TransactionIdentifier transactionID;

        OnCanCommitFutureComplete(final TransactionIdentifier transactionID) {
            super(CanCommitTransactionReply.class);
            this.transactionID = transactionID;
        }

        @Override
        void onSuccess(final Object resp) throws Exception {
            final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(resp);
            assertEquals("Can commit", true, canCommitReply.getCanCommit());
            final Future<Object> commitFuture = Patterns.ask(shard, new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), timeout);
            commitFuture.onComplete(new OnCommitFutureComplete(), getSystem().dispatcher());
        }
    }
    new ShardTestKit(getSystem()) {

        {
            waitUntilLeader(shard);
            final TransactionIdentifier transactionID1 = nextTransactionId();
            final TransactionIdentifier transactionID2 = nextTransactionId();
            final TransactionIdentifier transactionID3 = nextTransactionId();
            final Map<TransactionIdentifier, CapturingShardDataTreeCohort> cohortMap = setupCohortDecorator(shard.underlyingActor(), transactionID1, transactionID2, transactionID3);
            final CapturingShardDataTreeCohort cohort1 = cohortMap.get(transactionID1);
            final CapturingShardDataTreeCohort cohort2 = cohortMap.get(transactionID2);
            final CapturingShardDataTreeCohort cohort3 = cohortMap.get(transactionID3);
            shard.tell(prepareBatchedModifications(transactionID1, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
            final ReadyTransactionReply readyReply = ReadyTransactionReply.fromSerializable(expectMsgClass(duration, ReadyTransactionReply.class));
            assertEquals("Cohort path", shard.path().toString(), readyReply.getCohortPath());
            // Send the CanCommitTransaction message for the first Tx.
            shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
            final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(expectMsgClass(duration, CanCommitTransactionReply.class));
            assertEquals("Can commit", true, canCommitReply.getCanCommit());
            // Ready 2 more Tx's.
            shard.tell(prepareBatchedModifications(transactionID2, TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false), getRef());
            expectMsgClass(duration, ReadyTransactionReply.class);
            shard.tell(prepareBatchedModifications(transactionID3, YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(), ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), false), getRef());
            expectMsgClass(duration, ReadyTransactionReply.class);
            // Send the CanCommitTransaction message for the next 2 Tx's.
            // These should get queued and
            // processed after the first Tx completes.
            final Future<Object> canCommitFuture1 = Patterns.ask(shard, new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
            final Future<Object> canCommitFuture2 = Patterns.ask(shard, new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), timeout);
            // Send the CommitTransaction message for the first Tx. After it
            // completes, it should
            // trigger the 2nd Tx to proceed which should in turn then
            // trigger the 3rd.
            shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
            expectMsgClass(duration, CommitTransactionReply.class);
            // Wait for the next 2 Tx's to complete.
            canCommitFuture1.onComplete(new OnCanCommitFutureComplete(transactionID2), getSystem().dispatcher());
            canCommitFuture2.onComplete(new OnCanCommitFutureComplete(transactionID3), getSystem().dispatcher());
            final boolean done = commitLatch.await(timeoutSec, TimeUnit.SECONDS);
            final Throwable t = caughtEx.get();
            if (t != null) {
                Throwables.propagateIfPossible(t, Exception.class);
                throw new RuntimeException(t);
            }
            assertEquals("Commits complete", true, done);
            // final InOrder inOrder = inOrder(cohort1.getCanCommit(), cohort1.getPreCommit(), cohort1.getCommit(),
            // cohort2.getCanCommit(), cohort2.getPreCommit(), cohort2.getCommit(), cohort3.getCanCommit(),
            // cohort3.getPreCommit(), cohort3.getCommit());
            // inOrder.verify(cohort1.getCanCommit()).onSuccess(any(Void.class));
            // inOrder.verify(cohort1.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
            // inOrder.verify(cohort2.getCanCommit()).onSuccess(any(Void.class));
            // inOrder.verify(cohort2.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
            // inOrder.verify(cohort3.getCanCommit()).onSuccess(any(Void.class));
            // inOrder.verify(cohort3.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
            // inOrder.verify(cohort1.getCommit()).onSuccess(any(UnsignedLong.class));
            // inOrder.verify(cohort2.getCommit()).onSuccess(any(UnsignedLong.class));
            // inOrder.verify(cohort3.getCommit()).onSuccess(any(UnsignedLong.class));
            // Verify data in the data store.
            verifyOuterListEntry(shard, 1);
            verifyLastApplied(shard, 5);
        }
    };
}
Also used : ReadyTransactionReply(org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply) CommitTransactionReply(org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply) CanCommitTransactionReply(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply) CanCommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction) CommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CommitTransaction) TransactionIdentifier(org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier) Timeout(akka.util.Timeout) ElectionTimeout(org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout) FiniteDuration(scala.concurrent.duration.FiniteDuration) OnComplete(akka.dispatch.OnComplete) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) NoShardLeaderException(org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException) DataValidationFailedException(org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException) CanCommitTransactionReply(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply) CanCommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction) Test(org.junit.Test)

Aggregations

TransactionIdentifier (org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier)57 Test (org.junit.Test)37 FiniteDuration (scala.concurrent.duration.FiniteDuration)17 CanCommitTransaction (org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction)16 CommitTransaction (org.opendaylight.controller.cluster.datastore.messages.CommitTransaction)12 DataTreeModification (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification)11 CanCommitTransactionReply (org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply)10 ActorRef (akka.actor.ActorRef)8 FollowerInitialSyncUpStatus (org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus)8 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)8 BatchedModifications (org.opendaylight.controller.cluster.datastore.messages.BatchedModifications)7 WriteModification (org.opendaylight.controller.cluster.datastore.modification.WriteModification)7 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)7 DataTree (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree)7 LocalHistoryIdentifier (org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier)6 CanCommit (org.opendaylight.controller.cluster.datastore.DataTreeCohortActor.CanCommit)6 Failure (akka.actor.Status.Failure)5 Timeout (akka.util.Timeout)5 Response (org.opendaylight.controller.cluster.access.concepts.Response)5 ReadyLocalTransaction (org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction)5