1 package org.opendaylight.controller.cluster.datastore;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.Matchers.any;
7 import static org.mockito.Matchers.anyString;
8 import static org.mockito.Matchers.eq;
9 import static org.mockito.Matchers.isA;
10 import static org.mockito.Mockito.doReturn;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.never;
13 import static org.mockito.Mockito.verify;
14 import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_ONLY;
15 import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_WRITE;
16 import static org.opendaylight.controller.cluster.datastore.TransactionType.WRITE_ONLY;
17 import akka.actor.ActorRef;
18 import akka.actor.ActorSelection;
19 import akka.actor.ActorSystem;
20 import akka.actor.Props;
21 import akka.dispatch.Futures;
22 import akka.util.Timeout;
23 import com.google.common.base.Optional;
24 import com.google.common.collect.Sets;
25 import com.google.common.util.concurrent.CheckedFuture;
26 import com.google.common.util.concurrent.FutureCallback;
27 import com.google.common.util.concurrent.Uninterruptibles;
28 import java.util.Collection;
29 import java.util.List;
30 import java.util.concurrent.CountDownLatch;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.TimeUnit;
33 import java.util.concurrent.atomic.AtomicReference;
34 import org.junit.Assert;
35 import org.junit.Test;
36 import org.mockito.InOrder;
37 import org.mockito.Mockito;
38 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
39 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
40 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
41 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
42 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
43 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
44 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
45 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
46 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
47 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
48 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
49 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
50 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
51 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
52 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest;
53 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
54 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
55 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
56 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
57 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
58 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
60 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
62 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
63 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
64 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
65 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
67 import scala.concurrent.Promise;
69 @SuppressWarnings("resource")
70 public class TransactionProxyTest extends AbstractTransactionProxyTest {
72 @SuppressWarnings("serial")
73 static class TestException extends RuntimeException {
76 static interface Invoker {
77 CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception;
81 public void testRead() throws Exception {
82 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
84 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
86 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
87 eq(actorSelection(actorRef)), eqSerializedReadData());
89 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
90 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
92 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
94 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
96 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
97 eq(actorSelection(actorRef)), eqSerializedReadData());
99 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
101 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
103 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
106 @Test(expected = ReadFailedException.class)
107 public void testReadWithInvalidReplyMessageType() throws Exception {
108 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
110 doReturn(Futures.successful(new Object())).when(mockActorContext).
111 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
113 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
115 transactionProxy.read(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
118 @Test(expected = TestException.class)
119 public void testReadWithAsyncRemoteOperatonFailure() throws Throwable {
120 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
122 doReturn(Futures.failed(new TestException())).when(mockActorContext).
123 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
125 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
127 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
130 private void testExceptionOnInitialCreateTransaction(Exception exToThrow, Invoker invoker)
132 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
134 if (exToThrow instanceof PrimaryNotFoundException) {
135 doReturn(Futures.failed(exToThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
137 doReturn(primaryShardInfoReply(getSystem(), actorRef)).
138 when(mockActorContext).findPrimaryShardAsync(anyString());
141 doReturn(Futures.failed(exToThrow)).when(mockActorContext).executeOperationAsync(
142 any(ActorSelection.class), any(), any(Timeout.class));
144 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
146 propagateReadFailedExceptionCause(invoker.invoke(transactionProxy));
149 private void testReadWithExceptionOnInitialCreateTransaction(Exception exToThrow) throws Throwable {
150 testExceptionOnInitialCreateTransaction(exToThrow, new Invoker() {
152 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
153 return proxy.read(TestModel.TEST_PATH);
158 @Test(expected = PrimaryNotFoundException.class)
159 public void testReadWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
160 testReadWithExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"));
163 @Test(expected = TimeoutException.class)
164 public void testReadWhenATimeoutExceptionIsThrown() throws Throwable {
165 testReadWithExceptionOnInitialCreateTransaction(new TimeoutException("test",
166 new Exception("reason")));
169 @Test(expected = TestException.class)
170 public void testReadWhenAnyOtherExceptionIsThrown() throws Throwable {
171 testReadWithExceptionOnInitialCreateTransaction(new TestException());
175 public void testReadWithPriorRecordingOperationSuccessful() throws Throwable {
176 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
178 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
180 expectBatchedModifications(actorRef, 1);
182 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
183 eq(actorSelection(actorRef)), eqSerializedReadData());
185 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
187 transactionProxy.write(TestModel.TEST_PATH, expectedNode);
189 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
190 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
192 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
193 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
195 InOrder inOrder = Mockito.inOrder(mockActorContext);
196 inOrder.verify(mockActorContext).executeOperationAsync(
197 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
199 inOrder.verify(mockActorContext).executeOperationAsync(
200 eq(actorSelection(actorRef)), eqSerializedReadData());
203 @Test(expected=IllegalStateException.class)
204 public void testReadPreConditionCheck() {
205 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
206 transactionProxy.read(TestModel.TEST_PATH);
209 @Test(expected=IllegalArgumentException.class)
210 public void testInvalidCreateTransactionReply() throws Throwable {
211 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
213 doReturn(getSystem().actorSelection(actorRef.path())).when(mockActorContext).
214 actorSelection(actorRef.path().toString());
216 doReturn(primaryShardInfoReply(getSystem(), actorRef)).
217 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
219 doReturn(Futures.successful(new Object())).when(mockActorContext).executeOperationAsync(
220 eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_ONLY),
223 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
225 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
229 public void testExists() throws Exception {
230 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
232 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
234 doReturn(dataExistsSerializedReply(false)).when(mockActorContext).executeOperationAsync(
235 eq(actorSelection(actorRef)), eqSerializedDataExists());
237 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
239 assertEquals("Exists response", false, exists);
241 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
242 eq(actorSelection(actorRef)), eqSerializedDataExists());
244 exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
246 assertEquals("Exists response", true, exists);
249 @Test(expected = PrimaryNotFoundException.class)
250 public void testExistsWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
251 testExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"), new Invoker() {
253 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
254 return proxy.exists(TestModel.TEST_PATH);
259 @Test(expected = ReadFailedException.class)
260 public void testExistsWithInvalidReplyMessageType() throws Exception {
261 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
263 doReturn(Futures.successful(new Object())).when(mockActorContext).
264 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
266 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
268 transactionProxy.exists(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
271 @Test(expected = TestException.class)
272 public void testExistsWithAsyncRemoteOperatonFailure() throws Throwable {
273 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
275 doReturn(Futures.failed(new TestException())).when(mockActorContext).
276 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
278 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
280 propagateReadFailedExceptionCause(transactionProxy.exists(TestModel.TEST_PATH));
284 public void testExistsWithPriorRecordingOperationSuccessful() throws Throwable {
285 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
287 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
289 expectBatchedModifications(actorRef, 1);
291 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
292 eq(actorSelection(actorRef)), eqSerializedDataExists());
294 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
296 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
298 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
300 assertEquals("Exists response", true, exists);
302 InOrder inOrder = Mockito.inOrder(mockActorContext);
303 inOrder.verify(mockActorContext).executeOperationAsync(
304 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
306 inOrder.verify(mockActorContext).executeOperationAsync(
307 eq(actorSelection(actorRef)), eqSerializedDataExists());
310 @Test(expected=IllegalStateException.class)
311 public void testExistsPreConditionCheck() {
312 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
313 transactionProxy.exists(TestModel.TEST_PATH);
317 public void testWrite() throws Exception {
318 dataStoreContextBuilder.shardBatchedModificationCount(1);
319 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
321 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
323 expectBatchedModifications(actorRef, 1);
325 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
327 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
329 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), false);
333 public void testWriteAfterAsyncRead() throws Throwable {
334 ActorRef actorRef = setupActorContextWithoutInitialCreateTransaction(getSystem(), DefaultShardStrategy.DEFAULT_SHARD);
336 Promise<Object> createTxPromise = akka.dispatch.Futures.promise();
337 doReturn(createTxPromise).when(mockActorContext).executeOperationAsync(
338 eq(getSystem().actorSelection(actorRef.path())),
339 eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
341 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
342 eq(actorSelection(actorRef)), eqSerializedReadData());
344 expectBatchedModificationsReady(actorRef);
346 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
348 final TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
350 final CountDownLatch readComplete = new CountDownLatch(1);
351 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
352 com.google.common.util.concurrent.Futures.addCallback(transactionProxy.read(TestModel.TEST_PATH),
353 new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
355 public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
357 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
358 } catch (Exception e) {
361 readComplete.countDown();
366 public void onFailure(Throwable t) {
368 readComplete.countDown();
372 createTxPromise.success(createTransactionReply(actorRef, DataStoreVersions.CURRENT_VERSION));
374 Uninterruptibles.awaitUninterruptibly(readComplete, 5, TimeUnit.SECONDS);
376 if(caughtEx.get() != null) {
377 throw caughtEx.get();
380 // This sends the batched modification.
381 transactionProxy.ready();
383 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), true);
386 @Test(expected=IllegalStateException.class)
387 public void testWritePreConditionCheck() {
388 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
389 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
392 @Test(expected=IllegalStateException.class)
393 public void testWriteAfterReadyPreConditionCheck() {
394 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
396 transactionProxy.ready();
398 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
402 public void testMerge() throws Exception {
403 dataStoreContextBuilder.shardBatchedModificationCount(1);
404 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
406 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
408 expectBatchedModifications(actorRef, 1);
410 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
412 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
414 verifyOneBatchedModification(actorRef, new MergeModification(TestModel.TEST_PATH, nodeToWrite), false);
418 public void testDelete() throws Exception {
419 dataStoreContextBuilder.shardBatchedModificationCount(1);
420 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
422 expectBatchedModifications(actorRef, 1);
424 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
426 transactionProxy.delete(TestModel.TEST_PATH);
428 verifyOneBatchedModification(actorRef, new DeleteModification(TestModel.TEST_PATH), false);
432 public void testReadWrite() throws Exception {
433 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
435 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
437 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
438 eq(actorSelection(actorRef)), eqSerializedReadData());
440 expectBatchedModifications(actorRef, 1);
442 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
444 transactionProxy.read(TestModel.TEST_PATH);
446 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
448 transactionProxy.read(TestModel.TEST_PATH);
450 transactionProxy.read(TestModel.TEST_PATH);
452 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
453 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
455 verifyBatchedModifications(batchedModifications.get(0), false,
456 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
460 public void testReadyWithReadWrite() throws Exception {
461 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
463 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
465 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
466 eq(actorSelection(actorRef)), eqSerializedReadData());
468 expectBatchedModificationsReady(actorRef, true);
470 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
472 transactionProxy.read(TestModel.TEST_PATH);
474 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
476 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
478 assertTrue(ready instanceof SingleCommitCohortProxy);
480 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
482 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
483 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
485 verifyBatchedModifications(batchedModifications.get(0), true, true,
486 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
488 assertEquals("getTotalMessageCount", 1, batchedModifications.get(0).getTotalMessagesSent());
492 public void testReadyWithNoModifications() throws Exception {
493 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
495 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
496 eq(actorSelection(actorRef)), eqSerializedReadData());
498 expectBatchedModificationsReady(actorRef, true);
500 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
502 transactionProxy.read(TestModel.TEST_PATH);
504 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
506 assertTrue(ready instanceof SingleCommitCohortProxy);
508 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
510 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
511 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
513 verifyBatchedModifications(batchedModifications.get(0), true, true);
517 public void testReadyWithMultipleShardWrites() throws Exception {
518 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
520 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
522 expectBatchedModificationsReady(actorRef1);
523 expectBatchedModificationsReady(actorRef2);
525 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
527 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
528 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
530 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
532 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
534 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef1),
535 actorSelection(actorRef2));
539 public void testReadyWithWriteOnlyAndLastBatchPending() throws Exception {
540 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
542 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
544 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
546 expectBatchedModificationsReady(actorRef, true);
548 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
550 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
552 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
554 assertTrue(ready instanceof SingleCommitCohortProxy);
556 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
558 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
559 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
561 verifyBatchedModifications(batchedModifications.get(0), true, true,
562 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
564 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
565 isA(ReadyTransaction.SERIALIZABLE_CLASS));
569 public void testReadyWithWriteOnlyAndLastBatchEmpty() throws Exception {
570 dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
571 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
573 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
575 expectBatchedModificationsReady(actorRef, true);
577 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
579 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
581 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
583 assertTrue(ready instanceof SingleCommitCohortProxy);
585 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
587 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
588 assertEquals("Captured BatchedModifications count", 2, batchedModifications.size());
590 verifyBatchedModifications(batchedModifications.get(0), false,
591 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
593 verifyBatchedModifications(batchedModifications.get(1), true, true);
595 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
596 isA(ReadyTransaction.SERIALIZABLE_CLASS));
600 public void testReadyWithReplyFailure() throws Exception {
601 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
603 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
605 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
607 expectFailedBatchedModifications(actorRef);
609 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
611 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
613 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
615 assertTrue(ready instanceof SingleCommitCohortProxy);
617 verifyCohortFutures((SingleCommitCohortProxy)ready, TestException.class);
621 public void testReadyWithDebugContextEnabled() throws Exception {
622 dataStoreContextBuilder.transactionDebugContextEnabled(true);
624 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
626 expectBatchedModificationsReady(actorRef, true);
628 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
630 transactionProxy.merge(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
632 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
634 assertTrue(ready instanceof DebugThreePhaseCommitCohort);
636 verifyCohortFutures((DebugThreePhaseCommitCohort)ready, new CommitTransactionReply().toSerializable());
639 private void testWriteOnlyTxWithFindPrimaryShardFailure(Exception toThrow) throws Exception {
640 doReturn(Futures.failed(toThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
642 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
644 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
646 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
648 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
650 transactionProxy.delete(TestModel.TEST_PATH);
652 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
654 assertTrue(ready instanceof SingleCommitCohortProxy);
656 verifyCohortFutures((SingleCommitCohortProxy)ready, toThrow.getClass());
660 public void testWriteOnlyTxWithPrimaryNotFoundException() throws Exception {
661 testWriteOnlyTxWithFindPrimaryShardFailure(new PrimaryNotFoundException("mock"));
665 public void testWriteOnlyTxWithNotInitializedException() throws Exception {
666 testWriteOnlyTxWithFindPrimaryShardFailure(new NotInitializedException("mock"));
670 public void testWriteOnlyTxWithNoShardLeaderException() throws Exception {
671 testWriteOnlyTxWithFindPrimaryShardFailure(new NoShardLeaderException("mock"));
675 public void testReadyWithInvalidReplyMessageType() throws Exception {
676 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
677 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
679 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
681 doReturn(Futures.successful(new Object())).when(mockActorContext).
682 executeOperationAsync(eq(actorSelection(actorRef1)), isA(BatchedModifications.class));
684 expectBatchedModificationsReady(actorRef2);
686 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
688 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
689 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
691 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
693 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
695 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef2),
696 IllegalArgumentException.class);
700 public void testGetIdentifier() {
701 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
702 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
704 Object id = transactionProxy.getIdentifier();
705 assertNotNull("getIdentifier returned null", id);
706 assertTrue("Invalid identifier: " + id, id.toString().startsWith(memberName));
710 public void testClose() throws Exception{
711 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
713 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
714 eq(actorSelection(actorRef)), eqSerializedReadData());
716 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
718 transactionProxy.read(TestModel.TEST_PATH);
720 transactionProxy.close();
722 verify(mockActorContext).sendOperationAsync(
723 eq(actorSelection(actorRef)), isA(CloseTransaction.SERIALIZABLE_CLASS));
728 * Method to test a local Tx actor. The Tx paths are matched to decide if the
729 * Tx actor is local or not. This is done by mocking the Tx actor path
730 * and the caller paths and ensuring that the paths have the remote-address format
732 * Note: Since the default akka provider for test is not a RemoteActorRefProvider,
733 * the paths returned for the actors for all the tests are not qualified remote paths.
734 * Hence are treated as non-local/remote actors. In short, all tests except
735 * few below run for remote actors
740 public void testLocalTxActorRead() throws Exception {
741 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
742 doReturn(true).when(mockActorContext).isPathLocal(anyString());
744 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
746 // negative test case with null as the reply
747 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
748 any(ActorSelection.class), eqReadData());
750 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
751 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
753 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
755 // test case with node as read data reply
756 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
758 doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
759 any(ActorSelection.class), eqReadData());
761 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
763 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
765 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
767 // test for local data exists
768 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
769 any(ActorSelection.class), eqDataExists());
771 boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
773 assertEquals("Exists response", true, exists);
777 public void testLocalTxActorReady() throws Exception {
778 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
779 doReturn(true).when(mockActorContext).isPathLocal(anyString());
781 expectBatchedModificationsReady(actorRef, true);
783 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
785 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
786 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
788 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
790 assertTrue(ready instanceof SingleCommitCohortProxy);
792 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
795 private static interface TransactionProxyOperation {
796 void run(TransactionProxy transactionProxy);
799 private void throttleOperation(TransactionProxyOperation operation) {
800 throttleOperation(operation, 1, true);
803 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound){
804 throttleOperation(operation, outstandingOpsLimit, shardFound, TimeUnit.MILLISECONDS.toNanos(
805 mockActorContext.getDatastoreContext().getOperationTimeoutInMillis()));
808 private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef){
809 return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), DataStoreVersions.CURRENT_VERSION,
810 Optional.<DataTree>absent());
813 private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef, Optional<DataTree> dataTreeOptional){
814 return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), DataStoreVersions.CURRENT_VERSION,
819 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound, long expectedCompletionTime){
820 ActorSystem actorSystem = getSystem();
821 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
823 // Note that we setting batchedModificationCount to one less than what we need because in TransactionProxy
824 // we now allow one extra permit to be allowed for ready
825 doReturn(dataStoreContextBuilder.operationTimeoutInSeconds(2).
826 shardBatchedModificationCount(outstandingOpsLimit-1).build()).when(mockActorContext).getDatastoreContext();
828 doReturn(actorSystem.actorSelection(shardActorRef.path())).
829 when(mockActorContext).actorSelection(shardActorRef.path().toString());
832 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
833 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
834 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
835 when(mockActorContext).findPrimaryShardAsync(eq("cars"));
838 doReturn(Futures.failed(new Exception("not found")))
839 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
842 String actorPath = "akka.tcp://system@127.0.0.1:2550/user/tx-actor";
844 doReturn(incompleteFuture()).when(mockActorContext).
845 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
846 eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
848 doReturn(true).when(mockActorContext).isPathLocal(actorPath);
850 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
852 long start = System.nanoTime();
854 operation.run(transactionProxy);
856 long end = System.nanoTime();
858 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
859 expectedCompletionTime, (end-start)),
860 ((end - start) > expectedCompletionTime) && ((end - start) < expectedCompletionTime*2));
864 private void completeOperation(TransactionProxyOperation operation){
865 completeOperation(operation, true);
868 private void completeOperation(TransactionProxyOperation operation, boolean shardFound){
869 ActorSystem actorSystem = getSystem();
870 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
872 doReturn(actorSystem.actorSelection(shardActorRef.path())).
873 when(mockActorContext).actorSelection(shardActorRef.path().toString());
876 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
877 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
879 doReturn(Futures.failed(new PrimaryNotFoundException("test")))
880 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
883 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
884 String actorPath = txActorRef.path().toString();
885 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
886 setTransactionId("txn-1").setTransactionActorPath(actorPath).
887 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
889 doReturn(actorSystem.actorSelection(actorPath)).when(mockActorContext).actorSelection(actorPath);
891 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
892 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
893 eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
895 doReturn(true).when(mockActorContext).isPathLocal(anyString());
897 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
899 long start = System.nanoTime();
901 operation.run(transactionProxy);
903 long end = System.nanoTime();
905 long expected = TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis());
906 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
907 expected, (end-start)), (end - start) <= expected);
910 private void completeOperationLocal(TransactionProxyOperation operation, Optional<DataTree> dataTreeOptional){
911 ActorSystem actorSystem = getSystem();
912 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
914 doReturn(actorSystem.actorSelection(shardActorRef.path())).
915 when(mockActorContext).actorSelection(shardActorRef.path().toString());
917 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef, dataTreeOptional))).
918 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
920 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
922 long start = System.nanoTime();
924 operation.run(transactionProxy);
926 long end = System.nanoTime();
928 long expected = TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis());
929 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
930 expected, (end-start)), (end - start) <= expected);
933 private Optional<DataTree> createDataTree(){
934 DataTree dataTree = mock(DataTree.class);
935 Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
936 DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
937 DataTreeModification dataTreeModification = mock(DataTreeModification.class);
939 doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
940 doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
942 return dataTreeOptional;
945 private Optional<DataTree> createDataTree(NormalizedNode readResponse){
946 DataTree dataTree = mock(DataTree.class);
947 Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
948 DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
949 DataTreeModification dataTreeModification = mock(DataTreeModification.class);
951 doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
952 doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
953 doReturn(Optional.of(readResponse)).when(dataTreeModification).readNode(any(YangInstanceIdentifier.class));
955 return dataTreeOptional;
960 public void testWriteCompletionForLocalShard(){
961 completeOperationLocal(new TransactionProxyOperation() {
963 public void run(TransactionProxy transactionProxy) {
964 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
966 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
968 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
971 }, createDataTree());
975 public void testWriteThrottlingWhenShardFound(){
976 throttleOperation(new TransactionProxyOperation() {
978 public void run(TransactionProxy transactionProxy) {
979 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
981 expectIncompleteBatchedModifications();
983 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
985 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
991 public void testWriteThrottlingWhenShardNotFound(){
992 // Confirm that there is no throttling when the Shard is not found
993 completeOperation(new TransactionProxyOperation() {
995 public void run(TransactionProxy transactionProxy) {
996 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
998 expectBatchedModifications(2);
1000 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1002 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1010 public void testWriteCompletion(){
1011 completeOperation(new TransactionProxyOperation() {
1013 public void run(TransactionProxy transactionProxy) {
1014 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1016 expectBatchedModifications(2);
1018 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1020 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1026 public void testMergeThrottlingWhenShardFound(){
1027 throttleOperation(new TransactionProxyOperation() {
1029 public void run(TransactionProxy transactionProxy) {
1030 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1032 expectIncompleteBatchedModifications();
1034 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1036 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1042 public void testMergeThrottlingWhenShardNotFound(){
1043 completeOperation(new TransactionProxyOperation() {
1045 public void run(TransactionProxy transactionProxy) {
1046 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1048 expectBatchedModifications(2);
1050 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1052 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1058 public void testMergeCompletion(){
1059 completeOperation(new TransactionProxyOperation() {
1061 public void run(TransactionProxy transactionProxy) {
1062 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1064 expectBatchedModifications(2);
1066 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1068 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1075 public void testMergeCompletionForLocalShard(){
1076 completeOperationLocal(new TransactionProxyOperation() {
1078 public void run(TransactionProxy transactionProxy) {
1079 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1081 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1083 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1086 }, createDataTree());
1091 public void testDeleteThrottlingWhenShardFound(){
1093 throttleOperation(new TransactionProxyOperation() {
1095 public void run(TransactionProxy transactionProxy) {
1096 expectIncompleteBatchedModifications();
1098 transactionProxy.delete(TestModel.TEST_PATH);
1100 transactionProxy.delete(TestModel.TEST_PATH);
1107 public void testDeleteThrottlingWhenShardNotFound(){
1109 completeOperation(new TransactionProxyOperation() {
1111 public void run(TransactionProxy transactionProxy) {
1112 expectBatchedModifications(2);
1114 transactionProxy.delete(TestModel.TEST_PATH);
1116 transactionProxy.delete(TestModel.TEST_PATH);
1122 public void testDeleteCompletionForLocalShard(){
1123 completeOperationLocal(new TransactionProxyOperation() {
1125 public void run(TransactionProxy transactionProxy) {
1127 transactionProxy.delete(TestModel.TEST_PATH);
1129 transactionProxy.delete(TestModel.TEST_PATH);
1131 }, createDataTree());
1136 public void testDeleteCompletion(){
1137 completeOperation(new TransactionProxyOperation() {
1139 public void run(TransactionProxy transactionProxy) {
1140 expectBatchedModifications(2);
1142 transactionProxy.delete(TestModel.TEST_PATH);
1144 transactionProxy.delete(TestModel.TEST_PATH);
1151 public void testReadThrottlingWhenShardFound(){
1153 throttleOperation(new TransactionProxyOperation() {
1155 public void run(TransactionProxy transactionProxy) {
1156 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1157 any(ActorSelection.class), eqReadData());
1159 transactionProxy.read(TestModel.TEST_PATH);
1161 transactionProxy.read(TestModel.TEST_PATH);
1167 public void testReadThrottlingWhenShardNotFound(){
1169 completeOperation(new TransactionProxyOperation() {
1171 public void run(TransactionProxy transactionProxy) {
1172 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1173 any(ActorSelection.class), eqReadData());
1175 transactionProxy.read(TestModel.TEST_PATH);
1177 transactionProxy.read(TestModel.TEST_PATH);
1184 public void testReadCompletion(){
1185 completeOperation(new TransactionProxyOperation() {
1187 public void run(TransactionProxy transactionProxy) {
1188 NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1190 doReturn(readDataReply(nodeToRead)).when(mockActorContext).executeOperationAsync(
1191 any(ActorSelection.class), eqReadData());
1193 transactionProxy.read(TestModel.TEST_PATH);
1195 transactionProxy.read(TestModel.TEST_PATH);
1202 public void testReadCompletionForLocalShard(){
1203 final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1204 completeOperationLocal(new TransactionProxyOperation() {
1206 public void run(TransactionProxy transactionProxy) {
1207 transactionProxy.read(TestModel.TEST_PATH);
1209 transactionProxy.read(TestModel.TEST_PATH);
1211 }, createDataTree(nodeToRead));
1216 public void testReadCompletionForLocalShardWhenExceptionOccurs(){
1217 completeOperationLocal(new TransactionProxyOperation() {
1219 public void run(TransactionProxy transactionProxy) {
1220 transactionProxy.read(TestModel.TEST_PATH);
1222 transactionProxy.read(TestModel.TEST_PATH);
1224 }, createDataTree());
1229 public void testExistsThrottlingWhenShardFound(){
1231 throttleOperation(new TransactionProxyOperation() {
1233 public void run(TransactionProxy transactionProxy) {
1234 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1235 any(ActorSelection.class), eqDataExists());
1237 transactionProxy.exists(TestModel.TEST_PATH);
1239 transactionProxy.exists(TestModel.TEST_PATH);
1245 public void testExistsThrottlingWhenShardNotFound(){
1247 completeOperation(new TransactionProxyOperation() {
1249 public void run(TransactionProxy transactionProxy) {
1250 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1251 any(ActorSelection.class), eqDataExists());
1253 transactionProxy.exists(TestModel.TEST_PATH);
1255 transactionProxy.exists(TestModel.TEST_PATH);
1262 public void testExistsCompletion(){
1263 completeOperation(new TransactionProxyOperation() {
1265 public void run(TransactionProxy transactionProxy) {
1266 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
1267 any(ActorSelection.class), eqDataExists());
1269 transactionProxy.exists(TestModel.TEST_PATH);
1271 transactionProxy.exists(TestModel.TEST_PATH);
1278 public void testExistsCompletionForLocalShard(){
1279 final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1280 completeOperationLocal(new TransactionProxyOperation() {
1282 public void run(TransactionProxy transactionProxy) {
1283 transactionProxy.exists(TestModel.TEST_PATH);
1285 transactionProxy.exists(TestModel.TEST_PATH);
1287 }, createDataTree(nodeToRead));
1292 public void testExistsCompletionForLocalShardWhenExceptionOccurs(){
1293 completeOperationLocal(new TransactionProxyOperation() {
1295 public void run(TransactionProxy transactionProxy) {
1296 transactionProxy.exists(TestModel.TEST_PATH);
1298 transactionProxy.exists(TestModel.TEST_PATH);
1300 }, createDataTree());
1304 public void testReadyThrottling(){
1306 throttleOperation(new TransactionProxyOperation() {
1308 public void run(TransactionProxy transactionProxy) {
1309 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1311 expectBatchedModifications(1);
1313 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1315 transactionProxy.ready();
1321 public void testReadyThrottlingWithTwoTransactionContexts(){
1322 throttleOperation(new TransactionProxyOperation() {
1324 public void run(TransactionProxy transactionProxy) {
1325 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1326 NormalizedNode<?, ?> carsNode = ImmutableNodes.containerNode(CarsModel.BASE_QNAME);
1328 expectBatchedModifications(2);
1330 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1331 any(ActorSelection.class), any(ReadyTransaction.class));
1333 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1335 // Trying to write to Cars will cause another transaction context to get created
1336 transactionProxy.write(CarsModel.BASE_PATH, carsNode);
1338 // Now ready should block for both transaction contexts
1339 transactionProxy.ready();
1341 }, 1, true, TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis()) * 2);
1344 private void testModificationOperationBatching(TransactionType type) throws Exception {
1345 int shardBatchedModificationCount = 3;
1346 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1348 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), type);
1350 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1352 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1353 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1355 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1356 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1358 YangInstanceIdentifier writePath3 = TestModel.INNER_LIST_PATH;
1359 NormalizedNode<?, ?> writeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1361 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1362 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1364 YangInstanceIdentifier mergePath2 = TestModel.OUTER_LIST_PATH;
1365 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1367 YangInstanceIdentifier mergePath3 = TestModel.INNER_LIST_PATH;
1368 NormalizedNode<?, ?> mergeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1370 YangInstanceIdentifier deletePath1 = TestModel.TEST_PATH;
1371 YangInstanceIdentifier deletePath2 = TestModel.OUTER_LIST_PATH;
1373 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, type);
1375 transactionProxy.write(writePath1, writeNode1);
1376 transactionProxy.write(writePath2, writeNode2);
1377 transactionProxy.delete(deletePath1);
1378 transactionProxy.merge(mergePath1, mergeNode1);
1379 transactionProxy.merge(mergePath2, mergeNode2);
1380 transactionProxy.write(writePath3, writeNode3);
1381 transactionProxy.merge(mergePath3, mergeNode3);
1382 transactionProxy.delete(deletePath2);
1384 // This sends the last batch.
1385 transactionProxy.ready();
1387 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1388 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1390 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1391 new WriteModification(writePath2, writeNode2), new DeleteModification(deletePath1));
1393 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1394 new MergeModification(mergePath2, mergeNode2), new WriteModification(writePath3, writeNode3));
1396 verifyBatchedModifications(batchedModifications.get(2), true, true,
1397 new MergeModification(mergePath3, mergeNode3), new DeleteModification(deletePath2));
1399 assertEquals("getTotalMessageCount", 3, batchedModifications.get(2).getTotalMessagesSent());
1403 public void testReadWriteModificationOperationBatching() throws Throwable {
1404 testModificationOperationBatching(READ_WRITE);
1408 public void testWriteOnlyModificationOperationBatching() throws Throwable {
1409 testModificationOperationBatching(WRITE_ONLY);
1413 public void testOptimizedWriteOnlyModificationOperationBatching() throws Throwable {
1414 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
1415 testModificationOperationBatching(WRITE_ONLY);
1419 public void testModificationOperationBatchingWithInterleavedReads() throws Throwable {
1421 int shardBatchedModificationCount = 10;
1422 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1424 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
1426 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1428 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1429 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1431 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1432 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1434 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1435 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1437 YangInstanceIdentifier mergePath2 = TestModel.INNER_LIST_PATH;
1438 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1440 YangInstanceIdentifier deletePath = TestModel.OUTER_LIST_PATH;
1442 doReturn(readSerializedDataReply(writeNode2)).when(mockActorContext).executeOperationAsync(
1443 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1445 doReturn(readSerializedDataReply(mergeNode2)).when(mockActorContext).executeOperationAsync(
1446 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1448 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
1449 eq(actorSelection(actorRef)), eqSerializedDataExists());
1451 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
1453 transactionProxy.write(writePath1, writeNode1);
1454 transactionProxy.write(writePath2, writeNode2);
1456 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(writePath2).
1457 get(5, TimeUnit.SECONDS);
1459 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1460 assertEquals("Response NormalizedNode", writeNode2, readOptional.get());
1462 transactionProxy.merge(mergePath1, mergeNode1);
1463 transactionProxy.merge(mergePath2, mergeNode2);
1465 readOptional = transactionProxy.read(mergePath2).get(5, TimeUnit.SECONDS);
1467 transactionProxy.delete(deletePath);
1469 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
1470 assertEquals("Exists response", true, exists);
1472 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1473 assertEquals("Response NormalizedNode", mergeNode2, readOptional.get());
1475 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1476 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1478 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1479 new WriteModification(writePath2, writeNode2));
1481 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1482 new MergeModification(mergePath2, mergeNode2));
1484 verifyBatchedModifications(batchedModifications.get(2), false, new DeleteModification(deletePath));
1486 InOrder inOrder = Mockito.inOrder(mockActorContext);
1487 inOrder.verify(mockActorContext).executeOperationAsync(
1488 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1490 inOrder.verify(mockActorContext).executeOperationAsync(
1491 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1493 inOrder.verify(mockActorContext).executeOperationAsync(
1494 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1496 inOrder.verify(mockActorContext).executeOperationAsync(
1497 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1499 inOrder.verify(mockActorContext).executeOperationAsync(
1500 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1502 inOrder.verify(mockActorContext).executeOperationAsync(
1503 eq(actorSelection(actorRef)), eqSerializedDataExists());
1507 public void testReadRoot() throws ReadFailedException, InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
1509 SchemaContext schemaContext = SchemaContextHelper.full();
1510 Configuration configuration = mock(Configuration.class);
1511 doReturn(configuration).when(mockActorContext).getConfiguration();
1512 doReturn(schemaContext).when(mockActorContext).getSchemaContext();
1513 doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames();
1515 NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1516 NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
1518 setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext));
1519 setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext));
1521 doReturn(memberName).when(mockActorContext).getCurrentMemberName();
1523 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
1525 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
1527 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
1528 YangInstanceIdentifier.builder().build()).get(5, TimeUnit.SECONDS);
1530 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1532 NormalizedNode<?, ?> normalizedNode = readOptional.get();
1534 assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
1536 Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
1538 for(NormalizedNode<?,?> node : collection){
1539 assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
1542 assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
1543 NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null);
1545 assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME));
1547 assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
1548 NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
1550 assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME));
1554 private void setUpReadData(String shardName, NormalizedNode<?, ?> expectedNode) {
1555 ActorSystem actorSystem = getSystem();
1556 ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
1558 doReturn(getSystem().actorSelection(shardActorRef.path())).
1559 when(mockActorContext).actorSelection(shardActorRef.path().toString());
1561 doReturn(primaryShardInfoReply(getSystem(), shardActorRef)).
1562 when(mockActorContext).findPrimaryShardAsync(eq(shardName));
1564 doReturn(true).when(mockActorContext).isPathLocal(shardActorRef.path().toString());
1566 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
1568 doReturn(actorSystem.actorSelection(txActorRef.path())).
1569 when(mockActorContext).actorSelection(txActorRef.path().toString());
1571 doReturn(Futures.successful(createTransactionReply(txActorRef, DataStoreVersions.CURRENT_VERSION))).when(mockActorContext).
1572 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
1573 eqCreateTransaction(memberName, TransactionType.READ_ONLY), any(Timeout.class));
1575 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
1576 eq(actorSelection(txActorRef)), eqSerializedReadData(YangInstanceIdentifier.builder().build()));