X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FDistributedDataStoreRemotingIntegrationTest.java;h=005e904e4c9401938fd334738b3fdfa97a478399;hb=d54ecf76d2bb2a06b0d94a8d4b8ad6ca0d7830e0;hp=247185cf53cdd5f2eb25ab7b6d542afc92944635;hpb=a05824023cb4f527ed38eacb78dda2ee1424a3ef;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreRemotingIntegrationTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreRemotingIntegrationTest.java index 247185cf53..005e904e4c 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreRemotingIntegrationTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreRemotingIntegrationTest.java @@ -16,10 +16,12 @@ import static org.mockito.Matchers.eq; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; import akka.actor.ActorRef; +import akka.actor.ActorSelection; import akka.actor.ActorSystem; import akka.actor.Address; import akka.actor.AddressFromURIString; import akka.cluster.Cluster; +import akka.dispatch.Futures; import akka.pattern.AskTimeoutException; import akka.testkit.JavaTestKit; import com.google.common.base.Optional; @@ -28,6 +30,7 @@ import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.Uninterruptibles; import com.typesafe.config.ConfigFactory; import java.math.BigInteger; +import java.util.Arrays; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import org.junit.After; @@ -40,7 +43,10 @@ import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder; import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException; import org.opendaylight.controller.cluster.datastore.exceptions.ShardLeaderNotRespondingException; import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply; +import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction; +import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree; import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction; +import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply; import org.opendaylight.controller.cluster.datastore.modification.MergeModification; import org.opendaylight.controller.cluster.datastore.modification.WriteModification; import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries; @@ -65,8 +71,10 @@ import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree; +import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder; @@ -522,18 +530,20 @@ public class DistributedDataStoreRemotingIntegrationTest { Optional carsFollowerShard = followerDistributedDataStore.getActorContext().findLocalShard("cars"); assertEquals("Cars follower shard found", true, carsFollowerShard.isPresent()); - TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(); + TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL); dataTree.setSchemaContext(SchemaContextHelper.full()); - DataTreeModification modification = dataTree.takeSnapshot().newModification(); + // Send a tx with immediate commit. + + DataTreeModification modification = dataTree.takeSnapshot().newModification(); new WriteModification(CarsModel.BASE_PATH, CarsModel.emptyContainer()).apply(modification); new MergeModification(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()).apply(modification); - MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000)); - new WriteModification(CarsModel.newCarPath("optima"), car).apply(modification); + MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000)); + new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification); + modification.ready(); - String transactionID = "tx-1"; - ReadyLocalTransaction readyLocal = new ReadyLocalTransaction(transactionID , modification, true); + ReadyLocalTransaction readyLocal = new ReadyLocalTransaction("tx-1" , modification, true); carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef()); Object resp = followerTestKit.expectMsgClass(Object.class); @@ -541,10 +551,101 @@ public class DistributedDataStoreRemotingIntegrationTest { throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause()); } - assertTrue("Expected response of type " + CommitTransactionReply.SERIALIZABLE_CLASS, - CommitTransactionReply.SERIALIZABLE_CLASS.equals(resp.getClass())); + assertEquals("Response type", CommitTransactionReply.SERIALIZABLE_CLASS, resp.getClass()); + + verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1); + + // Send another tx without immediate commit. + + modification = dataTree.takeSnapshot().newModification(); + MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000)); + new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification); + modification.ready(); + + readyLocal = new ReadyLocalTransaction("tx-2" , modification, false); + + carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef()); + resp = followerTestKit.expectMsgClass(Object.class); + if(resp instanceof akka.actor.Status.Failure) { + throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause()); + } + + assertEquals("Response type", ReadyTransactionReply.class, resp.getClass()); + + ActorSelection txActor = leaderDistributedDataStore.getActorContext().actorSelection( + ((ReadyTransactionReply)resp).getCohortPath()); + + ThreePhaseCommitCohortProxy cohort = new ThreePhaseCommitCohortProxy( + leaderDistributedDataStore.getActorContext(), Arrays.asList(Futures.successful(txActor)), "tx-2"); + cohort.canCommit().get(5, TimeUnit.SECONDS); + cohort.preCommit().get(5, TimeUnit.SECONDS); + cohort.commit().get(5, TimeUnit.SECONDS); + + verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2); + } + + @Test + public void testForwardedReadyTransactionForwardedToLeader() throws Exception { + initDatastores("testForwardedReadyTransactionForwardedToLeader"); + followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), "cars"); + + Optional carsFollowerShard = followerDistributedDataStore.getActorContext().findLocalShard("cars"); + assertEquals("Cars follower shard found", true, carsFollowerShard.isPresent()); + + carsFollowerShard.get().tell(GetShardDataTree.INSTANCE, followerTestKit.getRef()); + DataTree dataTree = followerTestKit.expectMsgClass(DataTree.class); - verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car); + // Send a tx with immediate commit. + + DataTreeModification modification = dataTree.takeSnapshot().newModification(); + new WriteModification(CarsModel.BASE_PATH, CarsModel.emptyContainer()).apply(modification); + new MergeModification(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()).apply(modification); + + MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000)); + new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification); + + ForwardedReadyTransaction forwardedReady = new ForwardedReadyTransaction("tx-1", + DataStoreVersions.CURRENT_VERSION, new ReadWriteShardDataTreeTransaction( + Mockito.mock(ShardDataTreeTransactionParent.class), "tx-1", modification), true, true); + + carsFollowerShard.get().tell(forwardedReady, followerTestKit.getRef()); + Object resp = followerTestKit.expectMsgClass(Object.class); + if(resp instanceof akka.actor.Status.Failure) { + throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause()); + } + + assertEquals("Response type", CommitTransactionReply.SERIALIZABLE_CLASS, resp.getClass()); + + verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1); + + // Send another tx without immediate commit. + + modification = dataTree.takeSnapshot().newModification(); + MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000)); + new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification); + + forwardedReady = new ForwardedReadyTransaction("tx-2", + DataStoreVersions.CURRENT_VERSION, new ReadWriteShardDataTreeTransaction( + Mockito.mock(ShardDataTreeTransactionParent.class), "tx-2", modification), true, false); + + carsFollowerShard.get().tell(forwardedReady, followerTestKit.getRef()); + resp = followerTestKit.expectMsgClass(Object.class); + if(resp instanceof akka.actor.Status.Failure) { + throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause()); + } + + assertEquals("Response type", ReadyTransactionReply.class, resp.getClass()); + + ActorSelection txActor = leaderDistributedDataStore.getActorContext().actorSelection( + ((ReadyTransactionReply)resp).getCohortPath()); + + ThreePhaseCommitCohortProxy cohort = new ThreePhaseCommitCohortProxy( + leaderDistributedDataStore.getActorContext(), Arrays.asList(Futures.successful(txActor)), "tx-2"); + cohort.canCommit().get(5, TimeUnit.SECONDS); + cohort.preCommit().get(5, TimeUnit.SECONDS); + cohort.commit().get(5, TimeUnit.SECONDS); + + verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2); } @Test(expected=NoShardLeaderException.class)