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 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.exceptions.NoShardLeaderException;
38 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
39 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
40 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
41 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
42 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
43 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
44 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
45 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
46 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
47 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
48 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
49 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
50 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
51 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest;
52 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
53 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
54 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
55 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
56 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
57 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
58 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
59 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
62 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
63 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
64 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
65 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
66 import scala.concurrent.Promise;
68 @SuppressWarnings("resource")
69 public class TransactionProxyTest extends AbstractTransactionProxyTest {
71 @SuppressWarnings("serial")
72 static class TestException extends RuntimeException {
75 static interface Invoker {
76 CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception;
80 public void testRead() throws Exception {
81 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
83 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
85 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
86 eq(actorSelection(actorRef)), eqSerializedReadData());
88 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
89 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
91 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
93 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
95 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
96 eq(actorSelection(actorRef)), eqSerializedReadData());
98 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
100 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
102 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
105 @Test(expected = ReadFailedException.class)
106 public void testReadWithInvalidReplyMessageType() throws Exception {
107 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
109 doReturn(Futures.successful(new Object())).when(mockActorContext).
110 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
112 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
114 transactionProxy.read(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
117 @Test(expected = TestException.class)
118 public void testReadWithAsyncRemoteOperatonFailure() throws Throwable {
119 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
121 doReturn(Futures.failed(new TestException())).when(mockActorContext).
122 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
124 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
126 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
129 private void testExceptionOnInitialCreateTransaction(Exception exToThrow, Invoker invoker)
131 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
133 if (exToThrow instanceof PrimaryNotFoundException) {
134 doReturn(Futures.failed(exToThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
136 doReturn(primaryShardInfoReply(getSystem(), actorRef)).
137 when(mockActorContext).findPrimaryShardAsync(anyString());
140 doReturn(Futures.failed(exToThrow)).when(mockActorContext).executeOperationAsync(
141 any(ActorSelection.class), any());
143 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
145 propagateReadFailedExceptionCause(invoker.invoke(transactionProxy));
148 private void testReadWithExceptionOnInitialCreateTransaction(Exception exToThrow) throws Throwable {
149 testExceptionOnInitialCreateTransaction(exToThrow, new Invoker() {
151 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
152 return proxy.read(TestModel.TEST_PATH);
157 @Test(expected = PrimaryNotFoundException.class)
158 public void testReadWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
159 testReadWithExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"));
162 @Test(expected = TimeoutException.class)
163 public void testReadWhenATimeoutExceptionIsThrown() throws Throwable {
164 testReadWithExceptionOnInitialCreateTransaction(new TimeoutException("test",
165 new Exception("reason")));
168 @Test(expected = TestException.class)
169 public void testReadWhenAnyOtherExceptionIsThrown() throws Throwable {
170 testReadWithExceptionOnInitialCreateTransaction(new TestException());
174 public void testReadWithPriorRecordingOperationSuccessful() throws Throwable {
175 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
177 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
179 expectBatchedModifications(actorRef, 1);
181 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
182 eq(actorSelection(actorRef)), eqSerializedReadData());
184 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
186 transactionProxy.write(TestModel.TEST_PATH, expectedNode);
188 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
189 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
191 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
192 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
194 InOrder inOrder = Mockito.inOrder(mockActorContext);
195 inOrder.verify(mockActorContext).executeOperationAsync(
196 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
198 inOrder.verify(mockActorContext).executeOperationAsync(
199 eq(actorSelection(actorRef)), eqSerializedReadData());
202 @Test(expected=IllegalStateException.class)
203 public void testReadPreConditionCheck() {
204 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
205 transactionProxy.read(TestModel.TEST_PATH);
208 @Test(expected=IllegalArgumentException.class)
209 public void testInvalidCreateTransactionReply() throws Throwable {
210 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
212 doReturn(getSystem().actorSelection(actorRef.path())).when(mockActorContext).
213 actorSelection(actorRef.path().toString());
215 doReturn(primaryShardInfoReply(getSystem(), actorRef)).
216 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
218 doReturn(Futures.successful(new Object())).when(mockActorContext).executeOperationAsync(
219 eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_ONLY));
221 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
223 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
227 public void testExists() throws Exception {
228 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
230 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
232 doReturn(dataExistsSerializedReply(false)).when(mockActorContext).executeOperationAsync(
233 eq(actorSelection(actorRef)), eqSerializedDataExists());
235 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
237 assertEquals("Exists response", false, exists);
239 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
240 eq(actorSelection(actorRef)), eqSerializedDataExists());
242 exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
244 assertEquals("Exists response", true, exists);
247 @Test(expected = PrimaryNotFoundException.class)
248 public void testExistsWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
249 testExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"), new Invoker() {
251 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
252 return proxy.exists(TestModel.TEST_PATH);
257 @Test(expected = ReadFailedException.class)
258 public void testExistsWithInvalidReplyMessageType() throws Exception {
259 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
261 doReturn(Futures.successful(new Object())).when(mockActorContext).
262 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
264 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
266 transactionProxy.exists(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
269 @Test(expected = TestException.class)
270 public void testExistsWithAsyncRemoteOperatonFailure() throws Throwable {
271 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
273 doReturn(Futures.failed(new TestException())).when(mockActorContext).
274 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
276 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
278 propagateReadFailedExceptionCause(transactionProxy.exists(TestModel.TEST_PATH));
282 public void testExistsWithPriorRecordingOperationSuccessful() throws Throwable {
283 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
285 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
287 expectBatchedModifications(actorRef, 1);
289 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
290 eq(actorSelection(actorRef)), eqSerializedDataExists());
292 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
294 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
296 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
298 assertEquals("Exists response", true, exists);
300 InOrder inOrder = Mockito.inOrder(mockActorContext);
301 inOrder.verify(mockActorContext).executeOperationAsync(
302 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
304 inOrder.verify(mockActorContext).executeOperationAsync(
305 eq(actorSelection(actorRef)), eqSerializedDataExists());
308 @Test(expected=IllegalStateException.class)
309 public void testExistsPreConditionCheck() {
310 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
311 transactionProxy.exists(TestModel.TEST_PATH);
315 public void testWrite() throws Exception {
316 dataStoreContextBuilder.shardBatchedModificationCount(1);
317 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
319 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
321 expectBatchedModifications(actorRef, 1);
323 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
325 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
327 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), false);
331 public void testWriteAfterAsyncRead() throws Throwable {
332 ActorRef actorRef = setupActorContextWithoutInitialCreateTransaction(getSystem(), DefaultShardStrategy.DEFAULT_SHARD);
334 Promise<Object> createTxPromise = akka.dispatch.Futures.promise();
335 doReturn(createTxPromise).when(mockActorContext).executeOperationAsync(
336 eq(getSystem().actorSelection(actorRef.path())),
337 eqCreateTransaction(memberName, READ_WRITE));
339 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
340 eq(actorSelection(actorRef)), eqSerializedReadData());
342 expectBatchedModificationsReady(actorRef);
344 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
346 final TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
348 final CountDownLatch readComplete = new CountDownLatch(1);
349 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
350 com.google.common.util.concurrent.Futures.addCallback(transactionProxy.read(TestModel.TEST_PATH),
351 new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
353 public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
355 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
356 } catch (Exception e) {
359 readComplete.countDown();
364 public void onFailure(Throwable t) {
366 readComplete.countDown();
370 createTxPromise.success(createTransactionReply(actorRef, DataStoreVersions.CURRENT_VERSION));
372 Uninterruptibles.awaitUninterruptibly(readComplete, 5, TimeUnit.SECONDS);
374 if(caughtEx.get() != null) {
375 throw caughtEx.get();
378 // This sends the batched modification.
379 transactionProxy.ready();
381 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), true);
384 @Test(expected=IllegalStateException.class)
385 public void testWritePreConditionCheck() {
386 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
387 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
390 @Test(expected=IllegalStateException.class)
391 public void testWriteAfterReadyPreConditionCheck() {
392 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
394 transactionProxy.ready();
396 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
400 public void testMerge() throws Exception {
401 dataStoreContextBuilder.shardBatchedModificationCount(1);
402 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
404 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
406 expectBatchedModifications(actorRef, 1);
408 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
410 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
412 verifyOneBatchedModification(actorRef, new MergeModification(TestModel.TEST_PATH, nodeToWrite), false);
416 public void testDelete() throws Exception {
417 dataStoreContextBuilder.shardBatchedModificationCount(1);
418 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
420 expectBatchedModifications(actorRef, 1);
422 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
424 transactionProxy.delete(TestModel.TEST_PATH);
426 verifyOneBatchedModification(actorRef, new DeleteModification(TestModel.TEST_PATH), false);
430 public void testReadWrite() throws Exception {
431 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
433 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
435 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
436 eq(actorSelection(actorRef)), eqSerializedReadData());
438 expectBatchedModifications(actorRef, 1);
440 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
442 transactionProxy.read(TestModel.TEST_PATH);
444 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
446 transactionProxy.read(TestModel.TEST_PATH);
448 transactionProxy.read(TestModel.TEST_PATH);
450 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
451 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
453 verifyBatchedModifications(batchedModifications.get(0), false,
454 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
458 public void testReadyWithReadWrite() throws Exception {
459 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
461 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
463 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
464 eq(actorSelection(actorRef)), eqSerializedReadData());
466 expectBatchedModificationsReady(actorRef, true);
468 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
470 transactionProxy.read(TestModel.TEST_PATH);
472 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
474 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
476 assertTrue(ready instanceof SingleCommitCohortProxy);
478 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
480 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
481 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
483 verifyBatchedModifications(batchedModifications.get(0), true, true,
484 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
486 assertEquals("getTotalMessageCount", 1, batchedModifications.get(0).getTotalMessagesSent());
490 public void testReadyWithNoModifications() throws Exception {
491 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
493 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
494 eq(actorSelection(actorRef)), eqSerializedReadData());
496 expectBatchedModificationsReady(actorRef, true);
498 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
500 transactionProxy.read(TestModel.TEST_PATH);
502 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
504 assertTrue(ready instanceof SingleCommitCohortProxy);
506 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
508 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
509 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
511 verifyBatchedModifications(batchedModifications.get(0), true, true);
515 public void testReadyWithMultipleShardWrites() throws Exception {
516 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
518 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
520 expectBatchedModificationsReady(actorRef1);
521 expectBatchedModificationsReady(actorRef2);
523 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
525 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
526 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
528 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
530 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
532 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef1),
533 actorSelection(actorRef2));
537 public void testReadyWithWriteOnlyAndLastBatchPending() throws Exception {
538 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
540 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
542 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
544 expectBatchedModificationsReady(actorRef, true);
546 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
548 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
550 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
552 assertTrue(ready instanceof SingleCommitCohortProxy);
554 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
556 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
557 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
559 verifyBatchedModifications(batchedModifications.get(0), true, true,
560 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
562 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
563 isA(ReadyTransaction.SERIALIZABLE_CLASS));
567 public void testReadyWithWriteOnlyAndLastBatchEmpty() throws Exception {
568 dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
569 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
571 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
573 expectBatchedModificationsReady(actorRef, true);
575 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
577 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
579 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
581 assertTrue(ready instanceof SingleCommitCohortProxy);
583 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
585 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
586 assertEquals("Captured BatchedModifications count", 2, batchedModifications.size());
588 verifyBatchedModifications(batchedModifications.get(0), false,
589 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
591 verifyBatchedModifications(batchedModifications.get(1), true, true);
593 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
594 isA(ReadyTransaction.SERIALIZABLE_CLASS));
598 public void testReadyWithReplyFailure() throws Exception {
599 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
601 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
603 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
605 expectFailedBatchedModifications(actorRef);
607 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
609 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
611 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
613 assertTrue(ready instanceof SingleCommitCohortProxy);
615 verifyCohortFutures((SingleCommitCohortProxy)ready, TestException.class);
619 public void testReadyWithDebugContextEnabled() throws Exception {
620 dataStoreContextBuilder.transactionDebugContextEnabled(true);
622 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
624 expectBatchedModificationsReady(actorRef, true);
626 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
628 transactionProxy.merge(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
630 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
632 assertTrue(ready instanceof DebugThreePhaseCommitCohort);
634 verifyCohortFutures((DebugThreePhaseCommitCohort)ready, new CommitTransactionReply().toSerializable());
637 private void testWriteOnlyTxWithFindPrimaryShardFailure(Exception toThrow) throws Exception {
638 doReturn(Futures.failed(toThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
640 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
642 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
644 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
646 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
648 transactionProxy.delete(TestModel.TEST_PATH);
650 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
652 assertTrue(ready instanceof SingleCommitCohortProxy);
654 verifyCohortFutures((SingleCommitCohortProxy)ready, toThrow.getClass());
658 public void testWriteOnlyTxWithPrimaryNotFoundException() throws Exception {
659 testWriteOnlyTxWithFindPrimaryShardFailure(new PrimaryNotFoundException("mock"));
663 public void testWriteOnlyTxWithNotInitializedException() throws Exception {
664 testWriteOnlyTxWithFindPrimaryShardFailure(new NotInitializedException("mock"));
668 public void testWriteOnlyTxWithNoShardLeaderException() throws Exception {
669 testWriteOnlyTxWithFindPrimaryShardFailure(new NoShardLeaderException("mock"));
673 public void testReadyWithInvalidReplyMessageType() throws Exception {
674 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
675 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
677 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
679 doReturn(Futures.successful(new Object())).when(mockActorContext).
680 executeOperationAsync(eq(actorSelection(actorRef1)), isA(BatchedModifications.class));
682 expectBatchedModificationsReady(actorRef2);
684 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
686 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
687 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
689 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
691 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
693 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef2),
694 IllegalArgumentException.class);
698 public void testGetIdentifier() {
699 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
700 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
702 Object id = transactionProxy.getIdentifier();
703 assertNotNull("getIdentifier returned null", id);
704 assertTrue("Invalid identifier: " + id, id.toString().startsWith(memberName));
708 public void testClose() throws Exception{
709 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
711 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
712 eq(actorSelection(actorRef)), eqSerializedReadData());
714 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
716 transactionProxy.read(TestModel.TEST_PATH);
718 transactionProxy.close();
720 verify(mockActorContext).sendOperationAsync(
721 eq(actorSelection(actorRef)), isA(CloseTransaction.SERIALIZABLE_CLASS));
726 * Method to test a local Tx actor. The Tx paths are matched to decide if the
727 * Tx actor is local or not. This is done by mocking the Tx actor path
728 * and the caller paths and ensuring that the paths have the remote-address format
730 * Note: Since the default akka provider for test is not a RemoteActorRefProvider,
731 * the paths returned for the actors for all the tests are not qualified remote paths.
732 * Hence are treated as non-local/remote actors. In short, all tests except
733 * few below run for remote actors
738 public void testLocalTxActorRead() throws Exception {
739 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
740 doReturn(true).when(mockActorContext).isPathLocal(anyString());
742 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
744 // negative test case with null as the reply
745 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
746 any(ActorSelection.class), eqReadData());
748 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
749 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
751 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
753 // test case with node as read data reply
754 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
756 doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
757 any(ActorSelection.class), eqReadData());
759 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
761 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
763 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
765 // test for local data exists
766 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
767 any(ActorSelection.class), eqDataExists());
769 boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
771 assertEquals("Exists response", true, exists);
775 public void testLocalTxActorReady() throws Exception {
776 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
777 doReturn(true).when(mockActorContext).isPathLocal(anyString());
779 expectBatchedModificationsReady(actorRef, true);
781 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
783 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
784 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
786 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
788 assertTrue(ready instanceof SingleCommitCohortProxy);
790 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
793 private static interface TransactionProxyOperation {
794 void run(TransactionProxy transactionProxy);
797 private void throttleOperation(TransactionProxyOperation operation) {
798 throttleOperation(operation, 1, true);
801 private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef){
802 return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), Optional.<DataTree>absent());
805 private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef, Optional<DataTree> dataTreeOptional){
806 return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), dataTreeOptional);
810 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound){
811 ActorSystem actorSystem = getSystem();
812 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
814 doReturn(outstandingOpsLimit).when(mockActorContext).getTransactionOutstandingOperationLimit();
816 doReturn(actorSystem.actorSelection(shardActorRef.path())).
817 when(mockActorContext).actorSelection(shardActorRef.path().toString());
820 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
821 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
823 doReturn(Futures.failed(new Exception("not found")))
824 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
827 String actorPath = "akka.tcp://system@127.0.0.1:2550/user/tx-actor";
828 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
829 setTransactionId("txn-1").setTransactionActorPath(actorPath).
830 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
832 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
833 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
834 eqCreateTransaction(memberName, READ_WRITE));
836 doReturn(true).when(mockActorContext).isPathLocal(actorPath);
838 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
840 long start = System.nanoTime();
842 operation.run(transactionProxy);
844 long end = System.nanoTime();
846 long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
847 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
848 expected, (end-start)), (end - start) > expected);
852 private void completeOperation(TransactionProxyOperation operation){
853 completeOperation(operation, true);
856 private void completeOperation(TransactionProxyOperation operation, boolean shardFound){
857 ActorSystem actorSystem = getSystem();
858 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
860 doReturn(1).when(mockActorContext).getTransactionOutstandingOperationLimit();
862 doReturn(actorSystem.actorSelection(shardActorRef.path())).
863 when(mockActorContext).actorSelection(shardActorRef.path().toString());
866 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
867 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
869 doReturn(Futures.failed(new PrimaryNotFoundException("test")))
870 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
873 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
874 String actorPath = txActorRef.path().toString();
875 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
876 setTransactionId("txn-1").setTransactionActorPath(actorPath).
877 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
879 doReturn(actorSystem.actorSelection(actorPath)).when(mockActorContext).actorSelection(actorPath);
881 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
882 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
883 eqCreateTransaction(memberName, READ_WRITE));
885 doReturn(true).when(mockActorContext).isPathLocal(anyString());
887 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
889 long start = System.nanoTime();
891 operation.run(transactionProxy);
893 long end = System.nanoTime();
895 long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
896 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
897 expected, (end-start)), (end - start) <= expected);
900 private void completeOperationLocal(TransactionProxyOperation operation, Optional<DataTree> dataTreeOptional){
901 ActorSystem actorSystem = getSystem();
902 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
904 doReturn(1).when(mockActorContext).getTransactionOutstandingOperationLimit();
906 doReturn(actorSystem.actorSelection(shardActorRef.path())).
907 when(mockActorContext).actorSelection(shardActorRef.path().toString());
909 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef, dataTreeOptional))).
910 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
912 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
914 long start = System.nanoTime();
916 operation.run(transactionProxy);
918 long end = System.nanoTime();
920 long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
921 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
922 expected, (end-start)), (end - start) <= expected);
925 private Optional<DataTree> createDataTree(){
926 DataTree dataTree = mock(DataTree.class);
927 Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
928 DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
929 DataTreeModification dataTreeModification = mock(DataTreeModification.class);
931 doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
932 doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
934 return dataTreeOptional;
937 private Optional<DataTree> createDataTree(NormalizedNode readResponse){
938 DataTree dataTree = mock(DataTree.class);
939 Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
940 DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
941 DataTreeModification dataTreeModification = mock(DataTreeModification.class);
943 doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
944 doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
945 doReturn(Optional.of(readResponse)).when(dataTreeModification).readNode(any(YangInstanceIdentifier.class));
947 return dataTreeOptional;
952 public void testWriteCompletionForLocalShard(){
953 dataStoreContextBuilder.shardBatchedModificationCount(1);
954 completeOperationLocal(new TransactionProxyOperation() {
956 public void run(TransactionProxy transactionProxy) {
957 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
959 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
961 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
964 }, createDataTree());
968 public void testWriteThrottlingWhenShardFound(){
969 dataStoreContextBuilder.shardBatchedModificationCount(1);
970 throttleOperation(new TransactionProxyOperation() {
972 public void run(TransactionProxy transactionProxy) {
973 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
975 expectIncompleteBatchedModifications();
977 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
979 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
985 public void testWriteThrottlingWhenShardNotFound(){
986 // Confirm that there is no throttling when the Shard is not found
987 dataStoreContextBuilder.shardBatchedModificationCount(1);
988 completeOperation(new TransactionProxyOperation() {
990 public void run(TransactionProxy transactionProxy) {
991 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
993 expectBatchedModifications(2);
995 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
997 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1005 public void testWriteCompletion(){
1006 dataStoreContextBuilder.shardBatchedModificationCount(1);
1007 completeOperation(new TransactionProxyOperation() {
1009 public void run(TransactionProxy transactionProxy) {
1010 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1012 expectBatchedModifications(2);
1014 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1016 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1022 public void testMergeThrottlingWhenShardFound(){
1023 dataStoreContextBuilder.shardBatchedModificationCount(1);
1024 throttleOperation(new TransactionProxyOperation() {
1026 public void run(TransactionProxy transactionProxy) {
1027 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1029 expectIncompleteBatchedModifications();
1031 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1033 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1039 public void testMergeThrottlingWhenShardNotFound(){
1040 dataStoreContextBuilder.shardBatchedModificationCount(1);
1041 completeOperation(new TransactionProxyOperation() {
1043 public void run(TransactionProxy transactionProxy) {
1044 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1046 expectBatchedModifications(2);
1048 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1050 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1056 public void testMergeCompletion(){
1057 dataStoreContextBuilder.shardBatchedModificationCount(1);
1058 completeOperation(new TransactionProxyOperation() {
1060 public void run(TransactionProxy transactionProxy) {
1061 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1063 expectBatchedModifications(2);
1065 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1067 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1074 public void testMergeCompletionForLocalShard(){
1075 dataStoreContextBuilder.shardBatchedModificationCount(1);
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 dataStoreContextBuilder.shardBatchedModificationCount(1);
1124 completeOperationLocal(new TransactionProxyOperation() {
1126 public void run(TransactionProxy transactionProxy) {
1128 transactionProxy.delete(TestModel.TEST_PATH);
1130 transactionProxy.delete(TestModel.TEST_PATH);
1132 }, createDataTree());
1137 public void testDeleteCompletion(){
1138 dataStoreContextBuilder.shardBatchedModificationCount(1);
1139 completeOperation(new TransactionProxyOperation() {
1141 public void run(TransactionProxy transactionProxy) {
1142 expectBatchedModifications(2);
1144 transactionProxy.delete(TestModel.TEST_PATH);
1146 transactionProxy.delete(TestModel.TEST_PATH);
1153 public void testReadThrottlingWhenShardFound(){
1155 throttleOperation(new TransactionProxyOperation() {
1157 public void run(TransactionProxy transactionProxy) {
1158 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1159 any(ActorSelection.class), eqReadData());
1161 transactionProxy.read(TestModel.TEST_PATH);
1163 transactionProxy.read(TestModel.TEST_PATH);
1169 public void testReadThrottlingWhenShardNotFound(){
1171 completeOperation(new TransactionProxyOperation() {
1173 public void run(TransactionProxy transactionProxy) {
1174 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1175 any(ActorSelection.class), eqReadData());
1177 transactionProxy.read(TestModel.TEST_PATH);
1179 transactionProxy.read(TestModel.TEST_PATH);
1186 public void testReadCompletion(){
1187 completeOperation(new TransactionProxyOperation() {
1189 public void run(TransactionProxy transactionProxy) {
1190 NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1192 doReturn(readDataReply(nodeToRead)).when(mockActorContext).executeOperationAsync(
1193 any(ActorSelection.class), eqReadData());
1195 transactionProxy.read(TestModel.TEST_PATH);
1197 transactionProxy.read(TestModel.TEST_PATH);
1204 public void testReadCompletionForLocalShard(){
1205 final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1206 completeOperationLocal(new TransactionProxyOperation() {
1208 public void run(TransactionProxy transactionProxy) {
1209 transactionProxy.read(TestModel.TEST_PATH);
1211 transactionProxy.read(TestModel.TEST_PATH);
1213 }, createDataTree(nodeToRead));
1218 public void testReadCompletionForLocalShardWhenExceptionOccurs(){
1219 completeOperationLocal(new TransactionProxyOperation() {
1221 public void run(TransactionProxy transactionProxy) {
1222 transactionProxy.read(TestModel.TEST_PATH);
1224 transactionProxy.read(TestModel.TEST_PATH);
1226 }, createDataTree());
1231 public void testExistsThrottlingWhenShardFound(){
1233 throttleOperation(new TransactionProxyOperation() {
1235 public void run(TransactionProxy transactionProxy) {
1236 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1237 any(ActorSelection.class), eqDataExists());
1239 transactionProxy.exists(TestModel.TEST_PATH);
1241 transactionProxy.exists(TestModel.TEST_PATH);
1247 public void testExistsThrottlingWhenShardNotFound(){
1249 completeOperation(new TransactionProxyOperation() {
1251 public void run(TransactionProxy transactionProxy) {
1252 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1253 any(ActorSelection.class), eqDataExists());
1255 transactionProxy.exists(TestModel.TEST_PATH);
1257 transactionProxy.exists(TestModel.TEST_PATH);
1264 public void testExistsCompletion(){
1265 completeOperation(new TransactionProxyOperation() {
1267 public void run(TransactionProxy transactionProxy) {
1268 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
1269 any(ActorSelection.class), eqDataExists());
1271 transactionProxy.exists(TestModel.TEST_PATH);
1273 transactionProxy.exists(TestModel.TEST_PATH);
1280 public void testExistsCompletionForLocalShard(){
1281 final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1282 completeOperationLocal(new TransactionProxyOperation() {
1284 public void run(TransactionProxy transactionProxy) {
1285 transactionProxy.exists(TestModel.TEST_PATH);
1287 transactionProxy.exists(TestModel.TEST_PATH);
1289 }, createDataTree(nodeToRead));
1294 public void testExistsCompletionForLocalShardWhenExceptionOccurs(){
1295 completeOperationLocal(new TransactionProxyOperation() {
1297 public void run(TransactionProxy transactionProxy) {
1298 transactionProxy.exists(TestModel.TEST_PATH);
1300 transactionProxy.exists(TestModel.TEST_PATH);
1302 }, createDataTree());
1306 public void testReadyThrottling(){
1308 throttleOperation(new TransactionProxyOperation() {
1310 public void run(TransactionProxy transactionProxy) {
1311 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1313 expectBatchedModifications(1);
1315 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1316 any(ActorSelection.class), any(ReadyTransaction.class));
1318 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1320 transactionProxy.ready();
1326 public void testReadyThrottlingWithTwoTransactionContexts(){
1328 throttleOperation(new TransactionProxyOperation() {
1330 public void run(TransactionProxy transactionProxy) {
1331 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1332 NormalizedNode<?, ?> carsNode = ImmutableNodes.containerNode(CarsModel.BASE_QNAME);
1334 expectBatchedModifications(2);
1336 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1337 any(ActorSelection.class), any(ReadyTransaction.class));
1339 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1341 transactionProxy.write(TestModel.TEST_PATH, carsNode);
1343 transactionProxy.ready();
1348 private void testModificationOperationBatching(TransactionType type) throws Exception {
1349 int shardBatchedModificationCount = 3;
1350 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1352 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), type);
1354 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1356 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1357 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1359 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1360 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1362 YangInstanceIdentifier writePath3 = TestModel.INNER_LIST_PATH;
1363 NormalizedNode<?, ?> writeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1365 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1366 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1368 YangInstanceIdentifier mergePath2 = TestModel.OUTER_LIST_PATH;
1369 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1371 YangInstanceIdentifier mergePath3 = TestModel.INNER_LIST_PATH;
1372 NormalizedNode<?, ?> mergeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1374 YangInstanceIdentifier deletePath1 = TestModel.TEST_PATH;
1375 YangInstanceIdentifier deletePath2 = TestModel.OUTER_LIST_PATH;
1377 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, type);
1379 transactionProxy.write(writePath1, writeNode1);
1380 transactionProxy.write(writePath2, writeNode2);
1381 transactionProxy.delete(deletePath1);
1382 transactionProxy.merge(mergePath1, mergeNode1);
1383 transactionProxy.merge(mergePath2, mergeNode2);
1384 transactionProxy.write(writePath3, writeNode3);
1385 transactionProxy.merge(mergePath3, mergeNode3);
1386 transactionProxy.delete(deletePath2);
1388 // This sends the last batch.
1389 transactionProxy.ready();
1391 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1392 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1394 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1395 new WriteModification(writePath2, writeNode2), new DeleteModification(deletePath1));
1397 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1398 new MergeModification(mergePath2, mergeNode2), new WriteModification(writePath3, writeNode3));
1400 verifyBatchedModifications(batchedModifications.get(2), true, true,
1401 new MergeModification(mergePath3, mergeNode3), new DeleteModification(deletePath2));
1403 assertEquals("getTotalMessageCount", 3, batchedModifications.get(2).getTotalMessagesSent());
1407 public void testReadWriteModificationOperationBatching() throws Throwable {
1408 testModificationOperationBatching(READ_WRITE);
1412 public void testWriteOnlyModificationOperationBatching() throws Throwable {
1413 testModificationOperationBatching(WRITE_ONLY);
1417 public void testOptimizedWriteOnlyModificationOperationBatching() throws Throwable {
1418 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
1419 testModificationOperationBatching(WRITE_ONLY);
1423 public void testModificationOperationBatchingWithInterleavedReads() throws Throwable {
1425 int shardBatchedModificationCount = 10;
1426 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1428 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
1430 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1432 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1433 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1435 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1436 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1438 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1439 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1441 YangInstanceIdentifier mergePath2 = TestModel.INNER_LIST_PATH;
1442 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1444 YangInstanceIdentifier deletePath = TestModel.OUTER_LIST_PATH;
1446 doReturn(readSerializedDataReply(writeNode2)).when(mockActorContext).executeOperationAsync(
1447 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1449 doReturn(readSerializedDataReply(mergeNode2)).when(mockActorContext).executeOperationAsync(
1450 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1452 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
1453 eq(actorSelection(actorRef)), eqSerializedDataExists());
1455 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
1457 transactionProxy.write(writePath1, writeNode1);
1458 transactionProxy.write(writePath2, writeNode2);
1460 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(writePath2).
1461 get(5, TimeUnit.SECONDS);
1463 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1464 assertEquals("Response NormalizedNode", writeNode2, readOptional.get());
1466 transactionProxy.merge(mergePath1, mergeNode1);
1467 transactionProxy.merge(mergePath2, mergeNode2);
1469 readOptional = transactionProxy.read(mergePath2).get(5, TimeUnit.SECONDS);
1471 transactionProxy.delete(deletePath);
1473 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
1474 assertEquals("Exists response", true, exists);
1476 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1477 assertEquals("Response NormalizedNode", mergeNode2, readOptional.get());
1479 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1480 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1482 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1483 new WriteModification(writePath2, writeNode2));
1485 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1486 new MergeModification(mergePath2, mergeNode2));
1488 verifyBatchedModifications(batchedModifications.get(2), false, new DeleteModification(deletePath));
1490 InOrder inOrder = Mockito.inOrder(mockActorContext);
1491 inOrder.verify(mockActorContext).executeOperationAsync(
1492 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1494 inOrder.verify(mockActorContext).executeOperationAsync(
1495 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1497 inOrder.verify(mockActorContext).executeOperationAsync(
1498 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1500 inOrder.verify(mockActorContext).executeOperationAsync(
1501 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1503 inOrder.verify(mockActorContext).executeOperationAsync(
1504 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1506 inOrder.verify(mockActorContext).executeOperationAsync(
1507 eq(actorSelection(actorRef)), eqSerializedDataExists());
1511 public void testReadRoot() throws ReadFailedException, InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
1513 SchemaContext schemaContext = SchemaContextHelper.full();
1514 Configuration configuration = mock(Configuration.class);
1515 doReturn(configuration).when(mockActorContext).getConfiguration();
1516 doReturn(schemaContext).when(mockActorContext).getSchemaContext();
1517 doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames();
1519 NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1520 NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
1522 setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext));
1523 setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext));
1525 doReturn(memberName).when(mockActorContext).getCurrentMemberName();
1527 doReturn(10).when(mockActorContext).getTransactionOutstandingOperationLimit();
1529 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
1531 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
1533 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
1534 YangInstanceIdentifier.builder().build()).get(5, TimeUnit.SECONDS);
1536 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1538 NormalizedNode<?, ?> normalizedNode = readOptional.get();
1540 assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
1542 Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
1544 for(NormalizedNode<?,?> node : collection){
1545 assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
1548 assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
1549 NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null);
1551 assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME));
1553 assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
1554 NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
1556 assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME));
1560 private void setUpReadData(String shardName, NormalizedNode<?, ?> expectedNode) {
1561 ActorSystem actorSystem = getSystem();
1562 ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
1564 doReturn(getSystem().actorSelection(shardActorRef.path())).
1565 when(mockActorContext).actorSelection(shardActorRef.path().toString());
1567 doReturn(primaryShardInfoReply(getSystem(), shardActorRef)).
1568 when(mockActorContext).findPrimaryShardAsync(eq(shardName));
1570 doReturn(true).when(mockActorContext).isPathLocal(shardActorRef.path().toString());
1572 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
1574 doReturn(actorSystem.actorSelection(txActorRef.path())).
1575 when(mockActorContext).actorSelection(txActorRef.path().toString());
1577 doReturn(Futures.successful(createTransactionReply(txActorRef, DataStoreVersions.CURRENT_VERSION))).when(mockActorContext).
1578 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
1579 eqCreateTransaction(memberName, TransactionType.READ_ONLY));
1581 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
1582 eq(actorSelection(txActorRef)), eqSerializedReadData(YangInstanceIdentifier.builder().build()));