X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FThreePhaseCommitCohortProxyTest.java;h=8b5665244a6493414feeca9f58e11b45613dd2f5;hp=3c9d857fe81db981d341233e2c7249f06273bee6;hb=ecccb6d5b43dd73aef0d2d19349d19ee9b4728f7;hpb=95d1074f7f446784f76dc41525cecdb65688df6c diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ThreePhaseCommitCohortProxyTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ThreePhaseCommitCohortProxyTest.java index 3c9d857fe8..8b5665244a 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ThreePhaseCommitCohortProxyTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ThreePhaseCommitCohortProxyTest.java @@ -1,22 +1,32 @@ +/* + * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + package org.opendaylight.controller.cluster.datastore; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.isA; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import akka.actor.ActorPath; import akka.actor.ActorSelection; import akka.actor.Props; import akka.dispatch.Futures; - +import akka.util.Timeout; +import com.codahale.metrics.Snapshot; +import com.codahale.metrics.Timer; import com.google.common.collect.Lists; import com.google.common.util.concurrent.ListenableFuture; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.isA; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.times; - +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -33,12 +43,8 @@ import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransacti import org.opendaylight.controller.cluster.datastore.messages.SerializableMessage; import org.opendaylight.controller.cluster.datastore.utils.ActorContext; import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor; - import scala.concurrent.Future; - -import java.util.List; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; +import scala.concurrent.duration.Duration; public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { @@ -49,35 +55,59 @@ public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { @Mock private ActorContext actorContext; + @Mock + private DatastoreContext datastoreContext; + + @Mock + private Timer commitTimer; + + @Mock + private Timer.Context commitTimerContext; + + @Mock + private Snapshot commitSnapshot; + @Before public void setUp() { MockitoAnnotations.initMocks(this); doReturn(getSystem()).when(actorContext).getActorSystem(); + doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(actorContext).getClientDispatcher(); + doReturn(datastoreContext).when(actorContext).getDatastoreContext(); + doReturn(30).when(datastoreContext).getShardTransactionCommitTimeoutInSeconds(); + doReturn(commitTimer).when(actorContext).getOperationTimer("commit"); + doReturn(commitTimerContext).when(commitTimer).time(); + doReturn(commitSnapshot).when(commitTimer).getSnapshot(); + for(int i=1;i<11;i++){ + // Keep on increasing the amount of time it takes to complete transaction for each tenth of a + // percentile. Essentially this would be 1ms for the 10th percentile, 2ms for 20th percentile and so on. + doReturn(TimeUnit.MILLISECONDS.toNanos(i) * 1D).when(commitSnapshot).getValue(i * 0.1); + } + doReturn(10.0).when(actorContext).getTxCreationLimit(); } - private Future newCohortPath() { + private Future newCohort() { ActorPath path = getSystem().actorOf(Props.create(DoNothingActor.class)).path(); - doReturn(mock(ActorSelection.class)).when(actorContext).actorSelection(path); - return Futures.successful(path); + ActorSelection actorSelection = getSystem().actorSelection(path); + return Futures.successful(actorSelection); } private final ThreePhaseCommitCohortProxy setupProxy(int nCohorts) throws Exception { - List> cohortPathFutures = Lists.newArrayList(); + List> cohortFutures = Lists.newArrayList(); for(int i = 1; i <= nCohorts; i++) { - cohortPathFutures.add(newCohortPath()); + cohortFutures.add(newCohort()); } - return new ThreePhaseCommitCohortProxy(actorContext, cohortPathFutures, "txn-1"); + return new ThreePhaseCommitCohortProxy(actorContext, cohortFutures, "txn-1"); } private ThreePhaseCommitCohortProxy setupProxyWithFailedCohortPath() throws Exception { - List> cohortPathFutures = Lists.newArrayList(); - cohortPathFutures.add(newCohortPath()); - cohortPathFutures.add(Futures.failed(new TestException())); + List> cohortFutures = Lists.newArrayList(); + cohortFutures.add(newCohort()); + cohortFutures.add(Futures.failed(new TestException())); - return new ThreePhaseCommitCohortProxy(actorContext, cohortPathFutures, "txn-1"); + return new ThreePhaseCommitCohortProxy(actorContext, cohortFutures, "txn-1"); } private void setupMockActorContext(Class requestType, Object... responses) { @@ -92,15 +122,18 @@ public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { } stubber.when(actorContext).executeOperationAsync(any(ActorSelection.class), - isA(requestType)); + isA(requestType), any(Timeout.class)); + + doReturn(new Timeout(Duration.apply(1000, TimeUnit.MILLISECONDS))) + .when(actorContext).getTransactionCommitOperationTimeout(); } private void verifyCohortInvocations(int nCohorts, Class requestType) { verify(actorContext, times(nCohorts)).executeOperationAsync( - any(ActorSelection.class), isA(requestType)); + any(ActorSelection.class), isA(requestType), any(Timeout.class)); } - private void propagateExecutionExceptionCause(ListenableFuture future) throws Throwable { + private static void propagateExecutionExceptionCause(ListenableFuture future) throws Throwable { try { future.get(5, TimeUnit.SECONDS); @@ -116,14 +149,14 @@ public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { ThreePhaseCommitCohortProxy proxy = setupProxy(1); setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS, - new CanCommitTransactionReply(true)); + CanCommitTransactionReply.YES); ListenableFuture future = proxy.canCommit(); assertEquals("canCommit", true, future.get(5, TimeUnit.SECONDS)); setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS, - new CanCommitTransactionReply(false)); + CanCommitTransactionReply.NO); future = proxy.canCommit(); @@ -138,7 +171,7 @@ public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { ThreePhaseCommitCohortProxy proxy = setupProxy(2); setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS, - new CanCommitTransactionReply(true), new CanCommitTransactionReply(true)); + CanCommitTransactionReply.YES, CanCommitTransactionReply.YES); ListenableFuture future = proxy.canCommit(); @@ -153,14 +186,15 @@ public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { ThreePhaseCommitCohortProxy proxy = setupProxy(3); setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS, - new CanCommitTransactionReply(true), new CanCommitTransactionReply(false), - new CanCommitTransactionReply(true)); + CanCommitTransactionReply.YES, CanCommitTransactionReply.NO, CanCommitTransactionReply.YES); ListenableFuture future = proxy.canCommit(); - assertEquals("canCommit", false, future.get(5, TimeUnit.SECONDS)); + Boolean actual = future.get(5, TimeUnit.SECONDS); - verifyCohortInvocations(3, CanCommitTransaction.SERIALIZABLE_CLASS); + assertEquals("canCommit", false, actual); + + verifyCohortInvocations(2, CanCommitTransaction.SERIALIZABLE_CLASS); } @Test(expected = TestException.class) @@ -198,24 +232,13 @@ public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { @Test public void testPreCommit() throws Exception { + // Precommit is currently a no-op ThreePhaseCommitCohortProxy proxy = setupProxy(1); setupMockActorContext(PreCommitTransaction.SERIALIZABLE_CLASS, new PreCommitTransactionReply()); proxy.preCommit().get(5, TimeUnit.SECONDS); - - verifyCohortInvocations(1, PreCommitTransaction.SERIALIZABLE_CLASS); - } - - @Test(expected = ExecutionException.class) - public void testPreCommitWithFailure() throws Exception { - ThreePhaseCommitCohortProxy proxy = setupProxy(2); - - setupMockActorContext(PreCommitTransaction.SERIALIZABLE_CLASS, - new PreCommitTransactionReply(), new RuntimeException("mock")); - - proxy.preCommit().get(5, TimeUnit.SECONDS); } @Test @@ -294,8 +317,10 @@ public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { try { propagateExecutionExceptionCause(proxy.commit()); } finally { + verifyCohortInvocations(0, CommitTransaction.SERIALIZABLE_CLASS); } + } @Test @@ -304,7 +329,7 @@ public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { ThreePhaseCommitCohortProxy proxy = setupProxy(2); setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS, - new CanCommitTransactionReply(true), new CanCommitTransactionReply(true)); + CanCommitTransactionReply.YES, CanCommitTransactionReply.YES); setupMockActorContext(PreCommitTransaction.SERIALIZABLE_CLASS, new PreCommitTransactionReply(), new PreCommitTransactionReply()); @@ -312,12 +337,27 @@ public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { setupMockActorContext(CommitTransaction.SERIALIZABLE_CLASS, new CommitTransactionReply(), new CommitTransactionReply()); + assertEquals(10.0, actorContext.getTxCreationLimit(), 1e-15); + proxy.canCommit().get(5, TimeUnit.SECONDS); proxy.preCommit().get(5, TimeUnit.SECONDS); proxy.commit().get(5, TimeUnit.SECONDS); verifyCohortInvocations(2, CanCommitTransaction.SERIALIZABLE_CLASS); - verifyCohortInvocations(2, PreCommitTransaction.SERIALIZABLE_CLASS); verifyCohortInvocations(2, CommitTransaction.SERIALIZABLE_CLASS); + + } + + @Test + public void testDoNotChangeTxCreationLimitWhenCommittingEmptyTxn() throws Exception { + + ThreePhaseCommitCohortProxy proxy = setupProxy(0); + + assertEquals(10.0, actorContext.getTxCreationLimit(), 1e-15); + + proxy.canCommit().get(5, TimeUnit.SECONDS); + proxy.preCommit().get(5, TimeUnit.SECONDS); + proxy.commit().get(5, TimeUnit.SECONDS); + } }