1 package org.opendaylight.controller.cluster.datastore;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.fail;
5 import static org.mockito.Matchers.any;
6 import static org.mockito.Matchers.anyLong;
7 import static org.mockito.Matchers.isA;
8 import static org.mockito.Mockito.doReturn;
9 import static org.mockito.Mockito.never;
10 import static org.mockito.Mockito.timeout;
11 import static org.mockito.Mockito.times;
12 import static org.mockito.Mockito.verify;
13 import akka.actor.ActorPath;
14 import akka.actor.ActorSelection;
15 import akka.actor.Props;
16 import akka.dispatch.Futures;
17 import akka.util.Timeout;
18 import com.codahale.metrics.Snapshot;
19 import com.codahale.metrics.Timer;
20 import com.google.common.collect.Lists;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import java.util.List;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.TimeUnit;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.mockito.stubbing.Stubber;
30 import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction;
31 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
32 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
33 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
34 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
35 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
36 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
37 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
38 import org.opendaylight.controller.cluster.datastore.messages.SerializableMessage;
39 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
40 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
41 import scala.concurrent.Future;
43 public class ThreePhaseCommitCohortProxyTest extends AbstractActorTest {
45 @SuppressWarnings("serial")
46 static class TestException extends RuntimeException {
50 private ActorContext actorContext;
53 private DatastoreContext datastoreContext;
56 private Timer commitTimer;
59 private Timer.Context commitTimerContext;
62 private Snapshot commitSnapshot;
66 MockitoAnnotations.initMocks(this);
68 doReturn(getSystem()).when(actorContext).getActorSystem();
69 doReturn(datastoreContext).when(actorContext).getDatastoreContext();
70 doReturn(100).when(datastoreContext).getShardTransactionCommitTimeoutInSeconds();
71 doReturn(commitTimer).when(actorContext).getOperationTimer("commit");
72 doReturn(commitTimerContext).when(commitTimer).time();
73 doReturn(commitSnapshot).when(commitTimer).getSnapshot();
74 doReturn(TimeUnit.MILLISECONDS.toNanos(2000) * 1.0).when(commitSnapshot).get95thPercentile();
75 doReturn(10.0).when(actorContext).getTxCreationLimit();
78 private Future<ActorSelection> newCohort() {
79 ActorPath path = getSystem().actorOf(Props.create(DoNothingActor.class)).path();
80 ActorSelection actorSelection = getSystem().actorSelection(path);
81 return Futures.successful(actorSelection);
84 private final ThreePhaseCommitCohortProxy setupProxy(int nCohorts) throws Exception {
85 List<Future<ActorSelection>> cohortFutures = Lists.newArrayList();
86 for(int i = 1; i <= nCohorts; i++) {
87 cohortFutures.add(newCohort());
90 return new ThreePhaseCommitCohortProxy(actorContext, cohortFutures, "txn-1");
93 private ThreePhaseCommitCohortProxy setupProxyWithFailedCohortPath()
95 List<Future<ActorSelection>> cohortFutures = Lists.newArrayList();
96 cohortFutures.add(newCohort());
97 cohortFutures.add(Futures.<ActorSelection>failed(new TestException()));
99 return new ThreePhaseCommitCohortProxy(actorContext, cohortFutures, "txn-1");
102 private void setupMockActorContext(Class<?> requestType, Object... responses) {
103 Stubber stubber = doReturn(responses[0] instanceof Throwable ? Futures
104 .failed((Throwable) responses[0]) : Futures
105 .successful(((SerializableMessage) responses[0]).toSerializable()));
107 for(int i = 1; i < responses.length; i++) {
108 stubber = stubber.doReturn(responses[i] instanceof Throwable ? Futures
109 .failed((Throwable) responses[i]) : Futures
110 .successful(((SerializableMessage) responses[i]).toSerializable()));
113 stubber.when(actorContext).executeOperationAsync(any(ActorSelection.class),
114 isA(requestType), any(Timeout.class));
117 private void verifyCohortInvocations(int nCohorts, Class<?> requestType) {
118 verify(actorContext, times(nCohorts)).executeOperationAsync(
119 any(ActorSelection.class), isA(requestType), any(Timeout.class));
122 private void propagateExecutionExceptionCause(ListenableFuture<?> future) throws Throwable {
125 future.get(5, TimeUnit.SECONDS);
126 fail("Expected ExecutionException");
127 } catch(ExecutionException e) {
133 public void testCanCommitWithOneCohort() throws Exception {
135 ThreePhaseCommitCohortProxy proxy = setupProxy(1);
137 setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS,
138 CanCommitTransactionReply.YES);
140 ListenableFuture<Boolean> future = proxy.canCommit();
142 assertEquals("canCommit", true, future.get(5, TimeUnit.SECONDS));
144 setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS,
145 CanCommitTransactionReply.NO);
147 future = proxy.canCommit();
149 assertEquals("canCommit", false, future.get(5, TimeUnit.SECONDS));
151 verifyCohortInvocations(2, CanCommitTransaction.SERIALIZABLE_CLASS);
155 public void testCanCommitWithMultipleCohorts() throws Exception {
157 ThreePhaseCommitCohortProxy proxy = setupProxy(2);
159 setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS,
160 CanCommitTransactionReply.YES, CanCommitTransactionReply.YES);
162 ListenableFuture<Boolean> future = proxy.canCommit();
164 assertEquals("canCommit", true, future.get(5, TimeUnit.SECONDS));
166 verifyCohortInvocations(2, CanCommitTransaction.SERIALIZABLE_CLASS);
170 public void testCanCommitWithMultipleCohortsAndOneFailure() throws Exception {
172 ThreePhaseCommitCohortProxy proxy = setupProxy(3);
174 setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS,
175 CanCommitTransactionReply.YES, CanCommitTransactionReply.NO, CanCommitTransactionReply.YES);
177 ListenableFuture<Boolean> future = proxy.canCommit();
179 assertEquals("canCommit", false, future.get(5, TimeUnit.SECONDS));
181 verifyCohortInvocations(3, CanCommitTransaction.SERIALIZABLE_CLASS);
184 @Test(expected = TestException.class)
185 public void testCanCommitWithExceptionFailure() throws Throwable {
187 ThreePhaseCommitCohortProxy proxy = setupProxy(1);
189 setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS, new TestException());
191 propagateExecutionExceptionCause(proxy.canCommit());
194 @Test(expected = ExecutionException.class)
195 public void testCanCommitWithInvalidResponseType() throws Exception {
197 ThreePhaseCommitCohortProxy proxy = setupProxy(1);
199 setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS,
200 new PreCommitTransactionReply());
202 proxy.canCommit().get(5, TimeUnit.SECONDS);
205 @Test(expected = TestException.class)
206 public void testCanCommitWithFailedCohortPath() throws Throwable {
208 ThreePhaseCommitCohortProxy proxy = setupProxyWithFailedCohortPath();
211 propagateExecutionExceptionCause(proxy.canCommit());
213 verifyCohortInvocations(0, CanCommitTransaction.SERIALIZABLE_CLASS);
218 public void testPreCommit() throws Exception {
219 // Precommit is currently a no-op
220 ThreePhaseCommitCohortProxy proxy = setupProxy(1);
222 setupMockActorContext(PreCommitTransaction.SERIALIZABLE_CLASS,
223 new PreCommitTransactionReply());
225 proxy.preCommit().get(5, TimeUnit.SECONDS);
229 public void testAbort() throws Exception {
230 ThreePhaseCommitCohortProxy proxy = setupProxy(1);
232 setupMockActorContext(AbortTransaction.SERIALIZABLE_CLASS, new AbortTransactionReply());
234 proxy.abort().get(5, TimeUnit.SECONDS);
236 verifyCohortInvocations(1, AbortTransaction.SERIALIZABLE_CLASS);
240 public void testAbortWithFailure() throws Exception {
241 ThreePhaseCommitCohortProxy proxy = setupProxy(1);
243 setupMockActorContext(AbortTransaction.SERIALIZABLE_CLASS, new RuntimeException("mock"));
245 // The exception should not get propagated.
246 proxy.abort().get(5, TimeUnit.SECONDS);
248 verifyCohortInvocations(1, AbortTransaction.SERIALIZABLE_CLASS);
252 public void testAbortWithFailedCohortPath() throws Throwable {
254 ThreePhaseCommitCohortProxy proxy = setupProxyWithFailedCohortPath();
256 // The exception should not get propagated.
257 proxy.abort().get(5, TimeUnit.SECONDS);
259 verifyCohortInvocations(0, AbortTransaction.SERIALIZABLE_CLASS);
263 public void testCommit() throws Exception {
265 ThreePhaseCommitCohortProxy proxy = setupProxy(2);
267 setupMockActorContext(CommitTransaction.SERIALIZABLE_CLASS, new CommitTransactionReply(),
268 new CommitTransactionReply());
270 proxy.commit().get(5, TimeUnit.SECONDS);
272 verifyCohortInvocations(2, CommitTransaction.SERIALIZABLE_CLASS);
275 @Test(expected = TestException.class)
276 public void testCommitWithFailure() throws Throwable {
278 ThreePhaseCommitCohortProxy proxy = setupProxy(2);
280 setupMockActorContext(CommitTransaction.SERIALIZABLE_CLASS, new CommitTransactionReply(),
281 new TestException());
283 propagateExecutionExceptionCause(proxy.commit());
286 @Test(expected = ExecutionException.class)
287 public void testCommitWithInvalidResponseType() throws Exception {
289 ThreePhaseCommitCohortProxy proxy = setupProxy(1);
291 setupMockActorContext(CommitTransaction.SERIALIZABLE_CLASS, new PreCommitTransactionReply());
293 proxy.commit().get(5, TimeUnit.SECONDS);
296 @Test(expected = TestException.class)
297 public void testCommitWithFailedCohortPath() throws Throwable {
299 ThreePhaseCommitCohortProxy proxy = setupProxyWithFailedCohortPath();
302 propagateExecutionExceptionCause(proxy.commit());
305 verify(actorContext, never()).setTxCreationLimit(anyLong());
306 verifyCohortInvocations(0, CommitTransaction.SERIALIZABLE_CLASS);
312 public void testAllThreePhasesSuccessful() throws Exception {
314 ThreePhaseCommitCohortProxy proxy = setupProxy(2);
316 setupMockActorContext(CanCommitTransaction.SERIALIZABLE_CLASS,
317 CanCommitTransactionReply.YES, CanCommitTransactionReply.YES);
319 setupMockActorContext(PreCommitTransaction.SERIALIZABLE_CLASS,
320 new PreCommitTransactionReply(), new PreCommitTransactionReply());
322 setupMockActorContext(CommitTransaction.SERIALIZABLE_CLASS,
323 new CommitTransactionReply(), new CommitTransactionReply());
325 assertEquals(10.0, actorContext.getTxCreationLimit(), 1e-15);
327 proxy.canCommit().get(5, TimeUnit.SECONDS);
328 proxy.preCommit().get(5, TimeUnit.SECONDS);
329 proxy.commit().get(5, TimeUnit.SECONDS);
331 verifyCohortInvocations(2, CanCommitTransaction.SERIALIZABLE_CLASS);
332 verifyCohortInvocations(2, CommitTransaction.SERIALIZABLE_CLASS);
334 // Verify that the creation limit was changed to 0.5 (based on setup)
335 verify(actorContext, timeout(5000)).setTxCreationLimit(0.5);
339 public void testDoNotChangeTxCreationLimitWhenCommittingEmptyTxn() throws Exception {
341 ThreePhaseCommitCohortProxy proxy = setupProxy(0);
343 assertEquals(10.0, actorContext.getTxCreationLimit(), 1e-15);
345 proxy.canCommit().get(5, TimeUnit.SECONDS);
346 proxy.preCommit().get(5, TimeUnit.SECONDS);
347 proxy.commit().get(5, TimeUnit.SECONDS);
349 verify(actorContext, never()).setTxCreationLimit(anyLong());