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.TransactionProxy.TransactionType.READ_ONLY;
15 import static org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType.READ_WRITE;
16 import static org.opendaylight.controller.cluster.datastore.TransactionProxy.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 com.google.common.base.Optional;
23 import com.google.common.collect.Sets;
24 import com.google.common.util.concurrent.CheckedFuture;
25 import com.google.common.util.concurrent.FutureCallback;
26 import com.google.common.util.concurrent.Uninterruptibles;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.atomic.AtomicReference;
33 import org.junit.Assert;
34 import org.junit.Test;
35 import org.mockito.InOrder;
36 import org.mockito.Mockito;
37 import org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType;
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.ReadyTransaction;
45 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
46 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
47 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
48 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
49 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
50 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest;
51 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
52 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
53 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
54 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
55 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
56 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
58 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
60 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
61 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
62 import scala.concurrent.Promise;
64 @SuppressWarnings("resource")
65 public class TransactionProxyTest extends AbstractTransactionProxyTest {
67 @SuppressWarnings("serial")
68 static class TestException extends RuntimeException {
71 static interface Invoker {
72 CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception;
76 public void testRead() throws Exception {
77 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
79 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
81 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
82 eq(actorSelection(actorRef)), eqSerializedReadData());
84 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
85 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
87 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
89 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
91 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
92 eq(actorSelection(actorRef)), eqSerializedReadData());
94 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
96 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
98 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
101 @Test(expected = ReadFailedException.class)
102 public void testReadWithInvalidReplyMessageType() throws Exception {
103 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
105 doReturn(Futures.successful(new Object())).when(mockActorContext).
106 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
108 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
110 transactionProxy.read(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
113 @Test(expected = TestException.class)
114 public void testReadWithAsyncRemoteOperatonFailure() throws Throwable {
115 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
117 doReturn(Futures.failed(new TestException())).when(mockActorContext).
118 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
120 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
122 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
125 private void testExceptionOnInitialCreateTransaction(Exception exToThrow, Invoker invoker)
127 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
129 if (exToThrow instanceof PrimaryNotFoundException) {
130 doReturn(Futures.failed(exToThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
132 doReturn(Futures.successful(getSystem().actorSelection(actorRef.path()))).
133 when(mockActorContext).findPrimaryShardAsync(anyString());
136 doReturn(Futures.failed(exToThrow)).when(mockActorContext).executeOperationAsync(
137 any(ActorSelection.class), any());
139 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
141 propagateReadFailedExceptionCause(invoker.invoke(transactionProxy));
144 private void testReadWithExceptionOnInitialCreateTransaction(Exception exToThrow) throws Throwable {
145 testExceptionOnInitialCreateTransaction(exToThrow, new Invoker() {
147 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
148 return proxy.read(TestModel.TEST_PATH);
153 @Test(expected = PrimaryNotFoundException.class)
154 public void testReadWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
155 testReadWithExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"));
158 @Test(expected = TimeoutException.class)
159 public void testReadWhenATimeoutExceptionIsThrown() throws Throwable {
160 testReadWithExceptionOnInitialCreateTransaction(new TimeoutException("test",
161 new Exception("reason")));
164 @Test(expected = TestException.class)
165 public void testReadWhenAnyOtherExceptionIsThrown() throws Throwable {
166 testReadWithExceptionOnInitialCreateTransaction(new TestException());
170 public void testReadWithPriorRecordingOperationSuccessful() throws Throwable {
171 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
173 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
175 expectBatchedModifications(actorRef, 1);
177 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
178 eq(actorSelection(actorRef)), eqSerializedReadData());
180 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
182 transactionProxy.write(TestModel.TEST_PATH, expectedNode);
184 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
185 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
187 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
188 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
190 InOrder inOrder = Mockito.inOrder(mockActorContext);
191 inOrder.verify(mockActorContext).executeOperationAsync(
192 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
194 inOrder.verify(mockActorContext).executeOperationAsync(
195 eq(actorSelection(actorRef)), eqSerializedReadData());
198 @Test(expected=IllegalStateException.class)
199 public void testReadPreConditionCheck() {
200 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
201 transactionProxy.read(TestModel.TEST_PATH);
204 @Test(expected=IllegalArgumentException.class)
205 public void testInvalidCreateTransactionReply() throws Throwable {
206 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
208 doReturn(getSystem().actorSelection(actorRef.path())).when(mockActorContext).
209 actorSelection(actorRef.path().toString());
211 doReturn(Futures.successful(getSystem().actorSelection(actorRef.path()))).
212 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
214 doReturn(Futures.successful(new Object())).when(mockActorContext).executeOperationAsync(
215 eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_ONLY));
217 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
219 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
223 public void testExists() throws Exception {
224 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
226 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
228 doReturn(dataExistsSerializedReply(false)).when(mockActorContext).executeOperationAsync(
229 eq(actorSelection(actorRef)), eqSerializedDataExists());
231 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
233 assertEquals("Exists response", false, exists);
235 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
236 eq(actorSelection(actorRef)), eqSerializedDataExists());
238 exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
240 assertEquals("Exists response", true, exists);
243 @Test(expected = PrimaryNotFoundException.class)
244 public void testExistsWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
245 testExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"), new Invoker() {
247 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
248 return proxy.exists(TestModel.TEST_PATH);
253 @Test(expected = ReadFailedException.class)
254 public void testExistsWithInvalidReplyMessageType() throws Exception {
255 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
257 doReturn(Futures.successful(new Object())).when(mockActorContext).
258 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
260 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext,
263 transactionProxy.exists(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
266 @Test(expected = TestException.class)
267 public void testExistsWithAsyncRemoteOperatonFailure() throws Throwable {
268 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
270 doReturn(Futures.failed(new TestException())).when(mockActorContext).
271 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
273 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
275 propagateReadFailedExceptionCause(transactionProxy.exists(TestModel.TEST_PATH));
279 public void testExistsWithPriorRecordingOperationSuccessful() throws Throwable {
280 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
282 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
284 expectBatchedModifications(actorRef, 1);
286 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
287 eq(actorSelection(actorRef)), eqSerializedDataExists());
289 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
291 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
293 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
295 assertEquals("Exists response", true, exists);
297 InOrder inOrder = Mockito.inOrder(mockActorContext);
298 inOrder.verify(mockActorContext).executeOperationAsync(
299 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
301 inOrder.verify(mockActorContext).executeOperationAsync(
302 eq(actorSelection(actorRef)), eqSerializedDataExists());
305 @Test(expected=IllegalStateException.class)
306 public void testExistsPreConditionCheck() {
307 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
308 transactionProxy.exists(TestModel.TEST_PATH);
312 public void testWrite() throws Exception {
313 dataStoreContextBuilder.shardBatchedModificationCount(1);
314 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
316 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
318 expectBatchedModifications(actorRef, 1);
320 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
322 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
324 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), false);
328 public void testWriteAfterAsyncRead() throws Throwable {
329 ActorRef actorRef = setupActorContextWithoutInitialCreateTransaction(getSystem());
331 Promise<Object> createTxPromise = akka.dispatch.Futures.promise();
332 doReturn(createTxPromise).when(mockActorContext).executeOperationAsync(
333 eq(getSystem().actorSelection(actorRef.path())),
334 eqCreateTransaction(memberName, READ_WRITE));
336 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
337 eq(actorSelection(actorRef)), eqSerializedReadData());
339 expectBatchedModificationsReady(actorRef);
341 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
343 final TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
345 final CountDownLatch readComplete = new CountDownLatch(1);
346 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
347 com.google.common.util.concurrent.Futures.addCallback(transactionProxy.read(TestModel.TEST_PATH),
348 new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
350 public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
352 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
353 } catch (Exception e) {
356 readComplete.countDown();
361 public void onFailure(Throwable t) {
363 readComplete.countDown();
367 createTxPromise.success(createTransactionReply(actorRef, DataStoreVersions.CURRENT_VERSION));
369 Uninterruptibles.awaitUninterruptibly(readComplete, 5, TimeUnit.SECONDS);
371 if(caughtEx.get() != null) {
372 throw caughtEx.get();
375 // This sends the batched modification.
376 transactionProxy.ready();
378 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), true);
381 @Test(expected=IllegalStateException.class)
382 public void testWritePreConditionCheck() {
383 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
384 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
387 @Test(expected=IllegalStateException.class)
388 public void testWriteAfterReadyPreConditionCheck() {
389 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
391 transactionProxy.ready();
393 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
397 public void testMerge() throws Exception {
398 dataStoreContextBuilder.shardBatchedModificationCount(1);
399 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
401 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
403 expectBatchedModifications(actorRef, 1);
405 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
407 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
409 verifyOneBatchedModification(actorRef, new MergeModification(TestModel.TEST_PATH, nodeToWrite), false);
413 public void testDelete() throws Exception {
414 dataStoreContextBuilder.shardBatchedModificationCount(1);
415 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
417 expectBatchedModifications(actorRef, 1);
419 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
421 transactionProxy.delete(TestModel.TEST_PATH);
423 verifyOneBatchedModification(actorRef, new DeleteModification(TestModel.TEST_PATH), false);
427 public void testReadWrite() throws Exception {
428 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
430 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
432 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
433 eq(actorSelection(actorRef)), eqSerializedReadData());
435 expectBatchedModifications(actorRef, 1);
437 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
439 transactionProxy.read(TestModel.TEST_PATH);
441 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
443 transactionProxy.read(TestModel.TEST_PATH);
445 transactionProxy.read(TestModel.TEST_PATH);
447 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
448 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
450 verifyBatchedModifications(batchedModifications.get(0), false,
451 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
455 public void testReadyWithReadWrite() throws Exception {
456 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
458 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
460 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
461 eq(actorSelection(actorRef)), eqSerializedReadData());
463 expectBatchedModificationsReady(actorRef);
465 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
467 transactionProxy.read(TestModel.TEST_PATH);
469 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
471 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
473 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
475 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
477 verifyCohortFutures(proxy, getSystem().actorSelection(actorRef.path()));
479 verify(mockActorContext).executeOperationAsync(eq(actorSelection(actorRef)),
480 isA(BatchedModifications.class));
484 public void testReadyWithNoModifications() throws Exception {
485 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
487 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
488 eq(actorSelection(actorRef)), eqSerializedReadData());
490 expectBatchedModificationsReady(actorRef);
492 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
494 transactionProxy.read(TestModel.TEST_PATH);
496 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
498 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
500 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
502 verifyCohortFutures(proxy, getSystem().actorSelection(actorRef.path()));
504 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
505 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
507 verifyBatchedModifications(batchedModifications.get(0), true);
511 public void testReadyWithWriteOnlyAndLastBatchPending() throws Exception {
512 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
514 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
516 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
518 expectBatchedModificationsReady(actorRef);
520 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
522 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
524 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
526 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
528 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
530 verifyCohortFutures(proxy, getSystem().actorSelection(actorRef.path()));
532 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
533 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
535 verifyBatchedModifications(batchedModifications.get(0), true,
536 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
538 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
539 isA(ReadyTransaction.SERIALIZABLE_CLASS));
543 public void testReadyWithWriteOnlyAndLastBatchEmpty() throws Exception {
544 dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
545 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
547 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
549 expectBatchedModificationsReady(actorRef);
551 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
553 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
555 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
557 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
559 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
561 verifyCohortFutures(proxy, getSystem().actorSelection(actorRef.path()));
563 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
564 assertEquals("Captured BatchedModifications count", 2, batchedModifications.size());
566 verifyBatchedModifications(batchedModifications.get(0), false,
567 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
569 verifyBatchedModifications(batchedModifications.get(1), true);
571 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
572 isA(ReadyTransaction.SERIALIZABLE_CLASS));
576 public void testReadyWithReplyFailure() throws Exception {
577 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
579 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
581 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
583 expectFailedBatchedModifications(actorRef);
585 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
587 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
589 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
591 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
593 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
595 verifyCohortFutures(proxy, TestException.class);
598 private void testWriteOnlyTxWithFindPrimaryShardFailure(Exception toThrow) throws Exception {
599 doReturn(Futures.failed(toThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
601 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
603 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
605 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
607 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
609 transactionProxy.delete(TestModel.TEST_PATH);
611 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
613 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
615 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
617 verifyCohortFutures(proxy, toThrow.getClass());
621 public void testWriteOnlyTxWithPrimaryNotFoundException() throws Exception {
622 testWriteOnlyTxWithFindPrimaryShardFailure(new PrimaryNotFoundException("mock"));
626 public void testWriteOnlyTxWithNotInitializedException() throws Exception {
627 testWriteOnlyTxWithFindPrimaryShardFailure(new NotInitializedException("mock"));
631 public void testWriteOnlyTxWithNoShardLeaderException() throws Exception {
632 testWriteOnlyTxWithFindPrimaryShardFailure(new NoShardLeaderException("mock"));
636 public void testReadyWithInvalidReplyMessageType() throws Exception {
637 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
638 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
640 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
642 //expectBatchedModifications(actorRef, 1);
644 doReturn(Futures.successful(new Object())).when(mockActorContext).
645 executeOperationAsync(eq(actorSelection(actorRef)),
646 isA(BatchedModifications.class));
648 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
650 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
652 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
654 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
656 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
658 verifyCohortFutures(proxy, IllegalArgumentException.class);
662 public void testGetIdentifier() {
663 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
664 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext,
665 TransactionProxy.TransactionType.READ_ONLY);
667 Object id = transactionProxy.getIdentifier();
668 assertNotNull("getIdentifier returned null", id);
669 assertTrue("Invalid identifier: " + id, id.toString().startsWith(memberName));
673 public void testClose() throws Exception{
674 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
676 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
677 eq(actorSelection(actorRef)), eqSerializedReadData());
679 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
681 transactionProxy.read(TestModel.TEST_PATH);
683 transactionProxy.close();
685 verify(mockActorContext).sendOperationAsync(
686 eq(actorSelection(actorRef)), isA(CloseTransaction.SERIALIZABLE_CLASS));
691 * Method to test a local Tx actor. The Tx paths are matched to decide if the
692 * Tx actor is local or not. This is done by mocking the Tx actor path
693 * and the caller paths and ensuring that the paths have the remote-address format
695 * Note: Since the default akka provider for test is not a RemoteActorRefProvider,
696 * the paths returned for the actors for all the tests are not qualified remote paths.
697 * Hence are treated as non-local/remote actors. In short, all tests except
698 * few below run for remote actors
703 public void testLocalTxActorRead() throws Exception {
704 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
705 doReturn(true).when(mockActorContext).isPathLocal(anyString());
707 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext,READ_ONLY);
709 // negative test case with null as the reply
710 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
711 any(ActorSelection.class), eqReadData());
713 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
714 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
716 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
718 // test case with node as read data reply
719 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
721 doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
722 any(ActorSelection.class), eqReadData());
724 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
726 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
728 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
730 // test for local data exists
731 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
732 any(ActorSelection.class), eqDataExists());
734 boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
736 assertEquals("Exists response", true, exists);
740 public void testLocalTxActorReady() throws Exception {
741 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
742 doReturn(true).when(mockActorContext).isPathLocal(anyString());
744 expectBatchedModificationsReady(actorRef);
746 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
748 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
749 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
751 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
753 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
755 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
757 verifyCohortFutures(proxy, getSystem().actorSelection(actorRef.path()));
760 private static interface TransactionProxyOperation {
761 void run(TransactionProxy transactionProxy);
764 private void throttleOperation(TransactionProxyOperation operation) {
765 throttleOperation(operation, 1, true);
768 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound){
769 ActorSystem actorSystem = getSystem();
770 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
772 doReturn(outstandingOpsLimit).when(mockActorContext).getTransactionOutstandingOperationLimit();
774 doReturn(actorSystem.actorSelection(shardActorRef.path())).
775 when(mockActorContext).actorSelection(shardActorRef.path().toString());
778 doReturn(Futures.successful(actorSystem.actorSelection(shardActorRef.path()))).
779 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
781 doReturn(Futures.failed(new Exception("not found")))
782 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
785 String actorPath = "akka.tcp://system@127.0.0.1:2550/user/tx-actor";
786 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
787 setTransactionId("txn-1").setTransactionActorPath(actorPath).
788 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
790 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
791 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
792 eqCreateTransaction(memberName, READ_WRITE));
794 doReturn(true).when(mockActorContext).isPathLocal(actorPath);
796 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
798 long start = System.nanoTime();
800 operation.run(transactionProxy);
802 long end = System.nanoTime();
804 long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
805 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
806 expected, (end-start)), (end - start) > expected);
810 private void completeOperation(TransactionProxyOperation operation){
811 completeOperation(operation, true);
814 private void completeOperation(TransactionProxyOperation operation, boolean shardFound){
815 ActorSystem actorSystem = getSystem();
816 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
818 doReturn(1).when(mockActorContext).getTransactionOutstandingOperationLimit();
820 doReturn(actorSystem.actorSelection(shardActorRef.path())).
821 when(mockActorContext).actorSelection(shardActorRef.path().toString());
824 doReturn(Futures.successful(actorSystem.actorSelection(shardActorRef.path()))).
825 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
827 doReturn(Futures.failed(new PrimaryNotFoundException("test")))
828 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
831 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
832 String actorPath = txActorRef.path().toString();
833 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
834 setTransactionId("txn-1").setTransactionActorPath(actorPath).
835 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
837 doReturn(actorSystem.actorSelection(actorPath)).when(mockActorContext).actorSelection(actorPath);
839 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
840 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
841 eqCreateTransaction(memberName, READ_WRITE));
843 doReturn(true).when(mockActorContext).isPathLocal(anyString());
845 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
847 long start = System.nanoTime();
849 operation.run(transactionProxy);
851 long end = System.nanoTime();
853 long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
854 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
855 expected, (end-start)), (end - start) <= expected);
858 public void testWriteThrottling(boolean shardFound){
860 throttleOperation(new TransactionProxyOperation() {
862 public void run(TransactionProxy transactionProxy) {
863 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
865 expectBatchedModifications(2);
867 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
869 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
875 public void testWriteThrottlingWhenShardFound(){
876 dataStoreContextBuilder.shardBatchedModificationCount(1);
877 throttleOperation(new TransactionProxyOperation() {
879 public void run(TransactionProxy transactionProxy) {
880 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
882 expectIncompleteBatchedModifications();
884 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
886 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
892 public void testWriteThrottlingWhenShardNotFound(){
893 // Confirm that there is no throttling when the Shard is not found
894 dataStoreContextBuilder.shardBatchedModificationCount(1);
895 completeOperation(new TransactionProxyOperation() {
897 public void run(TransactionProxy transactionProxy) {
898 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
900 expectBatchedModifications(2);
902 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
904 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
912 public void testWriteCompletion(){
913 dataStoreContextBuilder.shardBatchedModificationCount(1);
914 completeOperation(new TransactionProxyOperation() {
916 public void run(TransactionProxy transactionProxy) {
917 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
919 expectBatchedModifications(2);
921 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
923 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
929 public void testMergeThrottlingWhenShardFound(){
930 dataStoreContextBuilder.shardBatchedModificationCount(1);
931 throttleOperation(new TransactionProxyOperation() {
933 public void run(TransactionProxy transactionProxy) {
934 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
936 expectIncompleteBatchedModifications();
938 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
940 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
946 public void testMergeThrottlingWhenShardNotFound(){
947 dataStoreContextBuilder.shardBatchedModificationCount(1);
948 completeOperation(new TransactionProxyOperation() {
950 public void run(TransactionProxy transactionProxy) {
951 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
953 expectBatchedModifications(2);
955 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
957 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
963 public void testMergeCompletion(){
964 dataStoreContextBuilder.shardBatchedModificationCount(1);
965 completeOperation(new TransactionProxyOperation() {
967 public void run(TransactionProxy transactionProxy) {
968 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
970 expectBatchedModifications(2);
972 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
974 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
981 public void testDeleteThrottlingWhenShardFound(){
983 throttleOperation(new TransactionProxyOperation() {
985 public void run(TransactionProxy transactionProxy) {
986 expectIncompleteBatchedModifications();
988 transactionProxy.delete(TestModel.TEST_PATH);
990 transactionProxy.delete(TestModel.TEST_PATH);
997 public void testDeleteThrottlingWhenShardNotFound(){
999 completeOperation(new TransactionProxyOperation() {
1001 public void run(TransactionProxy transactionProxy) {
1002 expectBatchedModifications(2);
1004 transactionProxy.delete(TestModel.TEST_PATH);
1006 transactionProxy.delete(TestModel.TEST_PATH);
1012 public void testDeleteCompletion(){
1013 dataStoreContextBuilder.shardBatchedModificationCount(1);
1014 completeOperation(new TransactionProxyOperation() {
1016 public void run(TransactionProxy transactionProxy) {
1017 expectBatchedModifications(2);
1019 transactionProxy.delete(TestModel.TEST_PATH);
1021 transactionProxy.delete(TestModel.TEST_PATH);
1028 public void testReadThrottlingWhenShardFound(){
1030 throttleOperation(new TransactionProxyOperation() {
1032 public void run(TransactionProxy transactionProxy) {
1033 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1034 any(ActorSelection.class), eqReadData());
1036 transactionProxy.read(TestModel.TEST_PATH);
1038 transactionProxy.read(TestModel.TEST_PATH);
1044 public void testReadThrottlingWhenShardNotFound(){
1046 completeOperation(new TransactionProxyOperation() {
1048 public void run(TransactionProxy transactionProxy) {
1049 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1050 any(ActorSelection.class), eqReadData());
1052 transactionProxy.read(TestModel.TEST_PATH);
1054 transactionProxy.read(TestModel.TEST_PATH);
1061 public void testReadCompletion(){
1062 completeOperation(new TransactionProxyOperation() {
1064 public void run(TransactionProxy transactionProxy) {
1065 NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1067 doReturn(readDataReply(nodeToRead)).when(mockActorContext).executeOperationAsync(
1068 any(ActorSelection.class), eqReadData());
1070 transactionProxy.read(TestModel.TEST_PATH);
1072 transactionProxy.read(TestModel.TEST_PATH);
1079 public void testExistsThrottlingWhenShardFound(){
1081 throttleOperation(new TransactionProxyOperation() {
1083 public void run(TransactionProxy transactionProxy) {
1084 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1085 any(ActorSelection.class), eqDataExists());
1087 transactionProxy.exists(TestModel.TEST_PATH);
1089 transactionProxy.exists(TestModel.TEST_PATH);
1095 public void testExistsThrottlingWhenShardNotFound(){
1097 completeOperation(new TransactionProxyOperation() {
1099 public void run(TransactionProxy transactionProxy) {
1100 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1101 any(ActorSelection.class), eqDataExists());
1103 transactionProxy.exists(TestModel.TEST_PATH);
1105 transactionProxy.exists(TestModel.TEST_PATH);
1112 public void testExistsCompletion(){
1113 completeOperation(new TransactionProxyOperation() {
1115 public void run(TransactionProxy transactionProxy) {
1116 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
1117 any(ActorSelection.class), eqDataExists());
1119 transactionProxy.exists(TestModel.TEST_PATH);
1121 transactionProxy.exists(TestModel.TEST_PATH);
1128 public void testReadyThrottling(){
1130 throttleOperation(new TransactionProxyOperation() {
1132 public void run(TransactionProxy transactionProxy) {
1133 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1135 expectBatchedModifications(1);
1137 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1138 any(ActorSelection.class), any(ReadyTransaction.class));
1140 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1142 transactionProxy.ready();
1148 public void testReadyThrottlingWithTwoTransactionContexts(){
1150 throttleOperation(new TransactionProxyOperation() {
1152 public void run(TransactionProxy transactionProxy) {
1153 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1154 NormalizedNode<?, ?> carsNode = ImmutableNodes.containerNode(CarsModel.BASE_QNAME);
1156 expectBatchedModifications(2);
1158 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1159 any(ActorSelection.class), any(ReadyTransaction.class));
1161 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1163 transactionProxy.write(TestModel.TEST_PATH, carsNode);
1165 transactionProxy.ready();
1170 private void testModificationOperationBatching(TransactionType type) throws Exception {
1171 int shardBatchedModificationCount = 3;
1172 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1174 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), type);
1176 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1178 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1179 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1181 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1182 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1184 YangInstanceIdentifier writePath3 = TestModel.INNER_LIST_PATH;
1185 NormalizedNode<?, ?> writeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1187 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1188 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1190 YangInstanceIdentifier mergePath2 = TestModel.OUTER_LIST_PATH;
1191 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1193 YangInstanceIdentifier mergePath3 = TestModel.INNER_LIST_PATH;
1194 NormalizedNode<?, ?> mergeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1196 YangInstanceIdentifier deletePath1 = TestModel.TEST_PATH;
1197 YangInstanceIdentifier deletePath2 = TestModel.OUTER_LIST_PATH;
1199 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, type);
1201 transactionProxy.write(writePath1, writeNode1);
1202 transactionProxy.write(writePath2, writeNode2);
1203 transactionProxy.delete(deletePath1);
1204 transactionProxy.merge(mergePath1, mergeNode1);
1205 transactionProxy.merge(mergePath2, mergeNode2);
1206 transactionProxy.write(writePath3, writeNode3);
1207 transactionProxy.merge(mergePath3, mergeNode3);
1208 transactionProxy.delete(deletePath2);
1210 // This sends the last batch.
1211 transactionProxy.ready();
1213 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1214 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1216 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1217 new WriteModification(writePath2, writeNode2), new DeleteModification(deletePath1));
1219 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1220 new MergeModification(mergePath2, mergeNode2), new WriteModification(writePath3, writeNode3));
1222 verifyBatchedModifications(batchedModifications.get(2), true, new MergeModification(mergePath3, mergeNode3),
1223 new DeleteModification(deletePath2));
1227 public void testReadWriteModificationOperationBatching() throws Throwable {
1228 testModificationOperationBatching(READ_WRITE);
1232 public void testWriteOnlyModificationOperationBatching() throws Throwable {
1233 testModificationOperationBatching(WRITE_ONLY);
1237 public void testOptimizedWriteOnlyModificationOperationBatching() throws Throwable {
1238 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
1239 testModificationOperationBatching(WRITE_ONLY);
1243 public void testModificationOperationBatchingWithInterleavedReads() throws Throwable {
1245 int shardBatchedModificationCount = 10;
1246 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1248 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
1250 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1252 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1253 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1255 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1256 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1258 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1259 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1261 YangInstanceIdentifier mergePath2 = TestModel.INNER_LIST_PATH;
1262 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1264 YangInstanceIdentifier deletePath = TestModel.OUTER_LIST_PATH;
1266 doReturn(readSerializedDataReply(writeNode2)).when(mockActorContext).executeOperationAsync(
1267 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1269 doReturn(readSerializedDataReply(mergeNode2)).when(mockActorContext).executeOperationAsync(
1270 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1272 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
1273 eq(actorSelection(actorRef)), eqSerializedDataExists());
1275 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
1277 transactionProxy.write(writePath1, writeNode1);
1278 transactionProxy.write(writePath2, writeNode2);
1280 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(writePath2).
1281 get(5, TimeUnit.SECONDS);
1283 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1284 assertEquals("Response NormalizedNode", writeNode2, readOptional.get());
1286 transactionProxy.merge(mergePath1, mergeNode1);
1287 transactionProxy.merge(mergePath2, mergeNode2);
1289 readOptional = transactionProxy.read(mergePath2).get(5, TimeUnit.SECONDS);
1291 transactionProxy.delete(deletePath);
1293 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
1294 assertEquals("Exists response", true, exists);
1296 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1297 assertEquals("Response NormalizedNode", mergeNode2, readOptional.get());
1299 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1300 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1302 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1303 new WriteModification(writePath2, writeNode2));
1305 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1306 new MergeModification(mergePath2, mergeNode2));
1308 verifyBatchedModifications(batchedModifications.get(2), false, new DeleteModification(deletePath));
1310 InOrder inOrder = Mockito.inOrder(mockActorContext);
1311 inOrder.verify(mockActorContext).executeOperationAsync(
1312 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1314 inOrder.verify(mockActorContext).executeOperationAsync(
1315 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1317 inOrder.verify(mockActorContext).executeOperationAsync(
1318 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1320 inOrder.verify(mockActorContext).executeOperationAsync(
1321 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1323 inOrder.verify(mockActorContext).executeOperationAsync(
1324 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1326 inOrder.verify(mockActorContext).executeOperationAsync(
1327 eq(actorSelection(actorRef)), eqSerializedDataExists());
1331 public void testReadRoot() throws ReadFailedException, InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
1333 SchemaContext schemaContext = SchemaContextHelper.full();
1334 Configuration configuration = mock(Configuration.class);
1335 doReturn(configuration).when(mockActorContext).getConfiguration();
1336 doReturn(schemaContext).when(mockActorContext).getSchemaContext();
1337 doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames();
1339 NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1340 NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
1342 setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext));
1343 setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext));
1345 doReturn(memberName).when(mockActorContext).getCurrentMemberName();
1347 doReturn(10).when(mockActorContext).getTransactionOutstandingOperationLimit();
1349 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
1351 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
1353 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
1354 YangInstanceIdentifier.builder().build()).get(5, TimeUnit.SECONDS);
1356 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1358 NormalizedNode<?, ?> normalizedNode = readOptional.get();
1360 assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
1362 Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
1364 for(NormalizedNode<?,?> node : collection){
1365 assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
1368 assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
1369 NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null);
1371 assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME));
1373 assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
1374 NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
1376 assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME));
1380 private void setUpReadData(String shardName, NormalizedNode<?, ?> expectedNode) {
1381 ActorSystem actorSystem = getSystem();
1382 ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
1384 doReturn(getSystem().actorSelection(shardActorRef.path())).
1385 when(mockActorContext).actorSelection(shardActorRef.path().toString());
1387 doReturn(Futures.successful(getSystem().actorSelection(shardActorRef.path()))).
1388 when(mockActorContext).findPrimaryShardAsync(eq(shardName));
1390 doReturn(true).when(mockActorContext).isPathLocal(shardActorRef.path().toString());
1392 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
1394 doReturn(actorSystem.actorSelection(txActorRef.path())).
1395 when(mockActorContext).actorSelection(txActorRef.path().toString());
1397 doReturn(Futures.successful(createTransactionReply(txActorRef, DataStoreVersions.CURRENT_VERSION))).when(mockActorContext).
1398 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
1399 eqCreateTransaction(memberName, TransactionType.READ_ONLY));
1401 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
1402 eq(actorSelection(txActorRef)), eqSerializedReadData(YangInstanceIdentifier.builder().build()));