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=647b6e7b542508953bb9750555aaf193c0ba2864;hp=87231f08849ed02398472bb792a40a48753a96be;hb=61722bffcf885c04d11f684f2f03b09fa96f2002;hpb=51e91f6bdcc88c5aa96f956e516d31dbb5e5d5e0 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 87231f0884..647b6e7b54 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,21 +1,26 @@ 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.anyLong; +import static org.mockito.Matchers.isA; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; +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.assertNotNull; -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; @@ -31,33 +36,71 @@ import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransacti import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply; import org.opendaylight.controller.cluster.datastore.messages.SerializableMessage; import org.opendaylight.controller.cluster.datastore.utils.ActorContext; -import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor; -import scala.concurrent.duration.FiniteDuration; - -import java.util.List; -import java.util.concurrent.ExecutionException; +import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor; +import scala.concurrent.Future; public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { + @SuppressWarnings("serial") + static class TestException extends RuntimeException { + } + @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 ThreePhaseCommitCohortProxy setupProxy(int nCohorts) { - List cohorts = Lists.newArrayList(); + private Future newCohort() { + ActorPath path = getSystem().actorOf(Props.create(DoNothingActor.class)).path(); + ActorSelection actorSelection = getSystem().actorSelection(path); + return Futures.successful(actorSelection); + } + + private final ThreePhaseCommitCohortProxy setupProxy(int nCohorts) throws Exception { + List> cohortFutures = Lists.newArrayList(); for(int i = 1; i <= nCohorts; i++) { - ActorPath path = getSystem().actorOf(Props.create(MessageCollectorActor.class)).path(); - cohorts.add(path); - doReturn(mock(ActorSelection.class)).when(actorContext).actorSelection(path); + cohortFutures.add(newCohort()); } - return new ThreePhaseCommitCohortProxy(actorContext, cohorts, "txn-1"); + return new ThreePhaseCommitCohortProxy(actorContext, cohortFutures, "txn-1"); + } + + private ThreePhaseCommitCohortProxy setupProxyWithFailedCohortPath() + throws Exception { + List> cohortFutures = Lists.newArrayList(); + cohortFutures.add(newCohort()); + cohortFutures.add(Futures.failed(new TestException())); + + return new ThreePhaseCommitCohortProxy(actorContext, cohortFutures, "txn-1"); } private void setupMockActorContext(Class requestType, Object... responses) { @@ -71,13 +114,23 @@ public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { .successful(((SerializableMessage) responses[i]).toSerializable())); } - stubber.when(actorContext).executeRemoteOperationAsync(any(ActorSelection.class), - isA(requestType), any(FiniteDuration.class)); + stubber.when(actorContext).executeOperationAsync(any(ActorSelection.class), + isA(requestType), any(Timeout.class)); } private void verifyCohortInvocations(int nCohorts, Class requestType) { - verify(actorContext, times(nCohorts)).executeRemoteOperationAsync( - any(ActorSelection.class), isA(requestType), any(FiniteDuration.class)); + verify(actorContext, times(nCohorts)).executeOperationAsync( + any(ActorSelection.class), isA(requestType), any(Timeout.class)); + } + + private void propagateExecutionExceptionCause(ListenableFuture future) throws Throwable { + + try { + future.get(5, TimeUnit.SECONDS); + fail("Expected ExecutionException"); + } catch(ExecutionException e) { + throw e.getCause(); + } } @Test @@ -86,18 +139,18 @@ 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()); + assertEquals("canCommit", true, future.get(5, TimeUnit.SECONDS)); setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS, - new CanCommitTransactionReply(false)); + CanCommitTransactionReply.NO); future = proxy.canCommit(); - assertEquals("canCommit", false, future.get()); + assertEquals("canCommit", false, future.get(5, TimeUnit.SECONDS)); verifyCohortInvocations(2, CanCommitTransaction.SERIALIZABLE_CLASS); } @@ -108,11 +161,11 @@ 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(); - assertEquals("canCommit", true, future.get()); + assertEquals("canCommit", true, future.get(5, TimeUnit.SECONDS)); verifyCohortInvocations(2, CanCommitTransaction.SERIALIZABLE_CLASS); } @@ -123,24 +176,23 @@ 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()); + assertEquals("canCommit", false, future.get(5, TimeUnit.SECONDS)); verifyCohortInvocations(3, CanCommitTransaction.SERIALIZABLE_CLASS); } - @Test(expected = ExecutionException.class) - public void testCanCommitWithExceptionFailure() throws Exception { + @Test(expected = TestException.class) + public void testCanCommitWithExceptionFailure() throws Throwable { ThreePhaseCommitCohortProxy proxy = setupProxy(1); - setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS, new RuntimeException("mock")); + setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS, new TestException()); - proxy.canCommit().get(); + propagateExecutionExceptionCause(proxy.canCommit()); } @Test(expected = ExecutionException.class) @@ -151,29 +203,30 @@ public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS, new PreCommitTransactionReply()); - proxy.canCommit().get(); + proxy.canCommit().get(5, TimeUnit.SECONDS); } - @Test - public void testPreCommit() throws Exception { - ThreePhaseCommitCohortProxy proxy = setupProxy(1); + @Test(expected = TestException.class) + public void testCanCommitWithFailedCohortPath() throws Throwable { - setupMockActorContext(PreCommitTransaction.SERIALIZABLE_CLASS, - new PreCommitTransactionReply()); + ThreePhaseCommitCohortProxy proxy = setupProxyWithFailedCohortPath(); - proxy.preCommit().get(); - - verifyCohortInvocations(1, PreCommitTransaction.SERIALIZABLE_CLASS); + try { + propagateExecutionExceptionCause(proxy.canCommit()); + } finally { + verifyCohortInvocations(0, CanCommitTransaction.SERIALIZABLE_CLASS); + } } - @Test(expected = ExecutionException.class) - public void testPreCommitWithFailure() throws Exception { - ThreePhaseCommitCohortProxy proxy = setupProxy(2); + @Test + public void testPreCommit() throws Exception { + // Precommit is currently a no-op + ThreePhaseCommitCohortProxy proxy = setupProxy(1); setupMockActorContext(PreCommitTransaction.SERIALIZABLE_CLASS, - new PreCommitTransactionReply(), new RuntimeException("mock")); + new PreCommitTransactionReply()); - proxy.preCommit().get(); + proxy.preCommit().get(5, TimeUnit.SECONDS); } @Test @@ -182,7 +235,7 @@ public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { setupMockActorContext(AbortTransaction.SERIALIZABLE_CLASS, new AbortTransactionReply()); - proxy.abort().get(); + proxy.abort().get(5, TimeUnit.SECONDS); verifyCohortInvocations(1, AbortTransaction.SERIALIZABLE_CLASS); } @@ -194,11 +247,22 @@ public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { setupMockActorContext(AbortTransaction.SERIALIZABLE_CLASS, new RuntimeException("mock")); // The exception should not get propagated. - proxy.abort().get(); + proxy.abort().get(5, TimeUnit.SECONDS); verifyCohortInvocations(1, AbortTransaction.SERIALIZABLE_CLASS); } + @Test + public void testAbortWithFailedCohortPath() throws Throwable { + + ThreePhaseCommitCohortProxy proxy = setupProxyWithFailedCohortPath(); + + // The exception should not get propagated. + proxy.abort().get(5, TimeUnit.SECONDS); + + verifyCohortInvocations(0, AbortTransaction.SERIALIZABLE_CLASS); + } + @Test public void testCommit() throws Exception { @@ -207,39 +271,83 @@ public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest { setupMockActorContext(CommitTransaction.SERIALIZABLE_CLASS, new CommitTransactionReply(), new CommitTransactionReply()); - proxy.commit().get(); + proxy.commit().get(5, TimeUnit.SECONDS); verifyCohortInvocations(2, CommitTransaction.SERIALIZABLE_CLASS); } - @Test(expected = ExecutionException.class) - public void testCommitWithFailure() throws Exception { + @Test(expected = TestException.class) + public void testCommitWithFailure() throws Throwable { ThreePhaseCommitCohortProxy proxy = setupProxy(2); setupMockActorContext(CommitTransaction.SERIALIZABLE_CLASS, new CommitTransactionReply(), - new RuntimeException("mock")); + new TestException()); - proxy.commit().get(); + propagateExecutionExceptionCause(proxy.commit()); } @Test(expected = ExecutionException.class) - public void teseCommitWithInvalidResponseType() throws Exception { + public void testCommitWithInvalidResponseType() throws Exception { ThreePhaseCommitCohortProxy proxy = setupProxy(1); setupMockActorContext(CommitTransaction.SERIALIZABLE_CLASS, new PreCommitTransactionReply()); - proxy.commit().get(); + proxy.commit().get(5, TimeUnit.SECONDS); + } + + @Test(expected = TestException.class) + public void testCommitWithFailedCohortPath() throws Throwable { + + ThreePhaseCommitCohortProxy proxy = setupProxyWithFailedCohortPath(); + + try { + propagateExecutionExceptionCause(proxy.commit()); + } finally { + + verify(actorContext, never()).setTxCreationLimit(anyLong()); + verifyCohortInvocations(0, CommitTransaction.SERIALIZABLE_CLASS); + } + } @Test - public void testGetCohortPaths() { + public void testAllThreePhasesSuccessful() throws Exception { ThreePhaseCommitCohortProxy proxy = setupProxy(2); - List paths = proxy.getCohortPaths(); - assertNotNull("getCohortPaths returned null", paths); - assertEquals("getCohortPaths size", 2, paths.size()); + setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS, + CanCommitTransactionReply.YES, CanCommitTransactionReply.YES); + + setupMockActorContext(PreCommitTransaction.SERIALIZABLE_CLASS, + new PreCommitTransactionReply(), new PreCommitTransactionReply()); + + 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, 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); + + verify(actorContext, never()).setTxCreationLimit(anyLong()); } }