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.impl.schema.ImmutableNodes;
63 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
64 import scala.concurrent.Promise;
66 @SuppressWarnings("resource")
67 public class TransactionProxyTest extends AbstractTransactionProxyTest {
69 @SuppressWarnings("serial")
70 static class TestException extends RuntimeException {
73 static interface Invoker {
74 CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception;
78 public void testRead() throws Exception {
79 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
81 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
83 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
84 eq(actorSelection(actorRef)), eqSerializedReadData());
86 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
87 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
89 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
91 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
93 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
94 eq(actorSelection(actorRef)), eqSerializedReadData());
96 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
98 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
100 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
103 @Test(expected = ReadFailedException.class)
104 public void testReadWithInvalidReplyMessageType() throws Exception {
105 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
107 doReturn(Futures.successful(new Object())).when(mockActorContext).
108 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
110 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
112 transactionProxy.read(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
115 @Test(expected = TestException.class)
116 public void testReadWithAsyncRemoteOperatonFailure() throws Throwable {
117 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
119 doReturn(Futures.failed(new TestException())).when(mockActorContext).
120 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
122 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
124 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
127 private void testExceptionOnInitialCreateTransaction(Exception exToThrow, Invoker invoker)
129 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
131 if (exToThrow instanceof PrimaryNotFoundException) {
132 doReturn(Futures.failed(exToThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
134 doReturn(primaryShardInfoReply(getSystem(), actorRef)).
135 when(mockActorContext).findPrimaryShardAsync(anyString());
138 doReturn(Futures.failed(exToThrow)).when(mockActorContext).executeOperationAsync(
139 any(ActorSelection.class), any());
141 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
143 propagateReadFailedExceptionCause(invoker.invoke(transactionProxy));
146 private void testReadWithExceptionOnInitialCreateTransaction(Exception exToThrow) throws Throwable {
147 testExceptionOnInitialCreateTransaction(exToThrow, new Invoker() {
149 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
150 return proxy.read(TestModel.TEST_PATH);
155 @Test(expected = PrimaryNotFoundException.class)
156 public void testReadWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
157 testReadWithExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"));
160 @Test(expected = TimeoutException.class)
161 public void testReadWhenATimeoutExceptionIsThrown() throws Throwable {
162 testReadWithExceptionOnInitialCreateTransaction(new TimeoutException("test",
163 new Exception("reason")));
166 @Test(expected = TestException.class)
167 public void testReadWhenAnyOtherExceptionIsThrown() throws Throwable {
168 testReadWithExceptionOnInitialCreateTransaction(new TestException());
172 public void testReadWithPriorRecordingOperationSuccessful() throws Throwable {
173 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
175 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
177 expectBatchedModifications(actorRef, 1);
179 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
180 eq(actorSelection(actorRef)), eqSerializedReadData());
182 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
184 transactionProxy.write(TestModel.TEST_PATH, expectedNode);
186 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
187 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
189 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
190 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
192 InOrder inOrder = Mockito.inOrder(mockActorContext);
193 inOrder.verify(mockActorContext).executeOperationAsync(
194 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
196 inOrder.verify(mockActorContext).executeOperationAsync(
197 eq(actorSelection(actorRef)), eqSerializedReadData());
200 @Test(expected=IllegalStateException.class)
201 public void testReadPreConditionCheck() {
202 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
203 transactionProxy.read(TestModel.TEST_PATH);
206 @Test(expected=IllegalArgumentException.class)
207 public void testInvalidCreateTransactionReply() throws Throwable {
208 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
210 doReturn(getSystem().actorSelection(actorRef.path())).when(mockActorContext).
211 actorSelection(actorRef.path().toString());
213 doReturn(primaryShardInfoReply(getSystem(), actorRef)).
214 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
216 doReturn(Futures.successful(new Object())).when(mockActorContext).executeOperationAsync(
217 eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_ONLY));
219 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
221 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
225 public void testExists() throws Exception {
226 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
228 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
230 doReturn(dataExistsSerializedReply(false)).when(mockActorContext).executeOperationAsync(
231 eq(actorSelection(actorRef)), eqSerializedDataExists());
233 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
235 assertEquals("Exists response", false, exists);
237 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
238 eq(actorSelection(actorRef)), eqSerializedDataExists());
240 exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
242 assertEquals("Exists response", true, exists);
245 @Test(expected = PrimaryNotFoundException.class)
246 public void testExistsWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
247 testExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"), new Invoker() {
249 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
250 return proxy.exists(TestModel.TEST_PATH);
255 @Test(expected = ReadFailedException.class)
256 public void testExistsWithInvalidReplyMessageType() throws Exception {
257 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
259 doReturn(Futures.successful(new Object())).when(mockActorContext).
260 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
262 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
264 transactionProxy.exists(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
267 @Test(expected = TestException.class)
268 public void testExistsWithAsyncRemoteOperatonFailure() throws Throwable {
269 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
271 doReturn(Futures.failed(new TestException())).when(mockActorContext).
272 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
274 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
276 propagateReadFailedExceptionCause(transactionProxy.exists(TestModel.TEST_PATH));
280 public void testExistsWithPriorRecordingOperationSuccessful() throws Throwable {
281 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
283 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
285 expectBatchedModifications(actorRef, 1);
287 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
288 eq(actorSelection(actorRef)), eqSerializedDataExists());
290 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
292 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
294 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
296 assertEquals("Exists response", true, exists);
298 InOrder inOrder = Mockito.inOrder(mockActorContext);
299 inOrder.verify(mockActorContext).executeOperationAsync(
300 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
302 inOrder.verify(mockActorContext).executeOperationAsync(
303 eq(actorSelection(actorRef)), eqSerializedDataExists());
306 @Test(expected=IllegalStateException.class)
307 public void testExistsPreConditionCheck() {
308 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
309 transactionProxy.exists(TestModel.TEST_PATH);
313 public void testWrite() throws Exception {
314 dataStoreContextBuilder.shardBatchedModificationCount(1);
315 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
317 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
319 expectBatchedModifications(actorRef, 1);
321 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
323 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
325 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), false);
329 public void testWriteAfterAsyncRead() throws Throwable {
330 ActorRef actorRef = setupActorContextWithoutInitialCreateTransaction(getSystem(), DefaultShardStrategy.DEFAULT_SHARD);
332 Promise<Object> createTxPromise = akka.dispatch.Futures.promise();
333 doReturn(createTxPromise).when(mockActorContext).executeOperationAsync(
334 eq(getSystem().actorSelection(actorRef.path())),
335 eqCreateTransaction(memberName, READ_WRITE));
337 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
338 eq(actorSelection(actorRef)), eqSerializedReadData());
340 expectBatchedModificationsReady(actorRef);
342 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
344 final TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
346 final CountDownLatch readComplete = new CountDownLatch(1);
347 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
348 com.google.common.util.concurrent.Futures.addCallback(transactionProxy.read(TestModel.TEST_PATH),
349 new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
351 public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
353 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
354 } catch (Exception e) {
357 readComplete.countDown();
362 public void onFailure(Throwable t) {
364 readComplete.countDown();
368 createTxPromise.success(createTransactionReply(actorRef, DataStoreVersions.CURRENT_VERSION));
370 Uninterruptibles.awaitUninterruptibly(readComplete, 5, TimeUnit.SECONDS);
372 if(caughtEx.get() != null) {
373 throw caughtEx.get();
376 // This sends the batched modification.
377 transactionProxy.ready();
379 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), true);
382 @Test(expected=IllegalStateException.class)
383 public void testWritePreConditionCheck() {
384 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
385 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
388 @Test(expected=IllegalStateException.class)
389 public void testWriteAfterReadyPreConditionCheck() {
390 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
392 transactionProxy.ready();
394 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
398 public void testMerge() throws Exception {
399 dataStoreContextBuilder.shardBatchedModificationCount(1);
400 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
402 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
404 expectBatchedModifications(actorRef, 1);
406 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
408 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
410 verifyOneBatchedModification(actorRef, new MergeModification(TestModel.TEST_PATH, nodeToWrite), false);
414 public void testDelete() throws Exception {
415 dataStoreContextBuilder.shardBatchedModificationCount(1);
416 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
418 expectBatchedModifications(actorRef, 1);
420 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
422 transactionProxy.delete(TestModel.TEST_PATH);
424 verifyOneBatchedModification(actorRef, new DeleteModification(TestModel.TEST_PATH), false);
428 public void testReadWrite() throws Exception {
429 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
431 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
433 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
434 eq(actorSelection(actorRef)), eqSerializedReadData());
436 expectBatchedModifications(actorRef, 1);
438 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
440 transactionProxy.read(TestModel.TEST_PATH);
442 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
444 transactionProxy.read(TestModel.TEST_PATH);
446 transactionProxy.read(TestModel.TEST_PATH);
448 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
449 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
451 verifyBatchedModifications(batchedModifications.get(0), false,
452 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
456 public void testReadyWithReadWrite() throws Exception {
457 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
459 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
461 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
462 eq(actorSelection(actorRef)), eqSerializedReadData());
464 expectBatchedModificationsReady(actorRef, true);
466 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
468 transactionProxy.read(TestModel.TEST_PATH);
470 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
472 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
474 assertTrue(ready instanceof SingleCommitCohortProxy);
476 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
478 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
479 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
481 verifyBatchedModifications(batchedModifications.get(0), true, true,
482 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
484 assertEquals("getTotalMessageCount", 1, batchedModifications.get(0).getTotalMessagesSent());
488 public void testReadyWithNoModifications() throws Exception {
489 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
491 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
492 eq(actorSelection(actorRef)), eqSerializedReadData());
494 expectBatchedModificationsReady(actorRef, true);
496 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
498 transactionProxy.read(TestModel.TEST_PATH);
500 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
502 assertTrue(ready instanceof SingleCommitCohortProxy);
504 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
506 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
507 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
509 verifyBatchedModifications(batchedModifications.get(0), true, true);
513 public void testReadyWithMultipleShardWrites() throws Exception {
514 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
516 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
518 expectBatchedModificationsReady(actorRef1);
519 expectBatchedModificationsReady(actorRef2);
521 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
523 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
524 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
526 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
528 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
530 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef1),
531 actorSelection(actorRef2));
535 public void testReadyWithWriteOnlyAndLastBatchPending() throws Exception {
536 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
538 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
540 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
542 expectBatchedModificationsReady(actorRef, true);
544 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
546 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
548 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
550 assertTrue(ready instanceof SingleCommitCohortProxy);
552 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
554 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
555 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
557 verifyBatchedModifications(batchedModifications.get(0), true, true,
558 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
560 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
561 isA(ReadyTransaction.SERIALIZABLE_CLASS));
565 public void testReadyWithWriteOnlyAndLastBatchEmpty() throws Exception {
566 dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
567 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
569 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
571 expectBatchedModificationsReady(actorRef, true);
573 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
575 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
577 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
579 assertTrue(ready instanceof SingleCommitCohortProxy);
581 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
583 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
584 assertEquals("Captured BatchedModifications count", 2, batchedModifications.size());
586 verifyBatchedModifications(batchedModifications.get(0), false,
587 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
589 verifyBatchedModifications(batchedModifications.get(1), true, true);
591 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
592 isA(ReadyTransaction.SERIALIZABLE_CLASS));
596 public void testReadyWithReplyFailure() throws Exception {
597 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
599 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
601 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
603 expectFailedBatchedModifications(actorRef);
605 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
607 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
609 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
611 assertTrue(ready instanceof SingleCommitCohortProxy);
613 verifyCohortFutures((SingleCommitCohortProxy)ready, TestException.class);
616 private void testWriteOnlyTxWithFindPrimaryShardFailure(Exception toThrow) throws Exception {
617 doReturn(Futures.failed(toThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
619 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
621 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
623 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
625 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
627 transactionProxy.delete(TestModel.TEST_PATH);
629 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
631 assertTrue(ready instanceof SingleCommitCohortProxy);
633 verifyCohortFutures((SingleCommitCohortProxy)ready, toThrow.getClass());
637 public void testWriteOnlyTxWithPrimaryNotFoundException() throws Exception {
638 testWriteOnlyTxWithFindPrimaryShardFailure(new PrimaryNotFoundException("mock"));
642 public void testWriteOnlyTxWithNotInitializedException() throws Exception {
643 testWriteOnlyTxWithFindPrimaryShardFailure(new NotInitializedException("mock"));
647 public void testWriteOnlyTxWithNoShardLeaderException() throws Exception {
648 testWriteOnlyTxWithFindPrimaryShardFailure(new NoShardLeaderException("mock"));
652 public void testReadyWithInvalidReplyMessageType() throws Exception {
653 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
654 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
656 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
658 doReturn(Futures.successful(new Object())).when(mockActorContext).
659 executeOperationAsync(eq(actorSelection(actorRef1)), isA(BatchedModifications.class));
661 expectBatchedModificationsReady(actorRef2);
663 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
665 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
666 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
668 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
670 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
672 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef2),
673 IllegalArgumentException.class);
677 public void testGetIdentifier() {
678 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
679 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
681 Object id = transactionProxy.getIdentifier();
682 assertNotNull("getIdentifier returned null", id);
683 assertTrue("Invalid identifier: " + id, id.toString().startsWith(memberName));
687 public void testClose() throws Exception{
688 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
690 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
691 eq(actorSelection(actorRef)), eqSerializedReadData());
693 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
695 transactionProxy.read(TestModel.TEST_PATH);
697 transactionProxy.close();
699 verify(mockActorContext).sendOperationAsync(
700 eq(actorSelection(actorRef)), isA(CloseTransaction.SERIALIZABLE_CLASS));
705 * Method to test a local Tx actor. The Tx paths are matched to decide if the
706 * Tx actor is local or not. This is done by mocking the Tx actor path
707 * and the caller paths and ensuring that the paths have the remote-address format
709 * Note: Since the default akka provider for test is not a RemoteActorRefProvider,
710 * the paths returned for the actors for all the tests are not qualified remote paths.
711 * Hence are treated as non-local/remote actors. In short, all tests except
712 * few below run for remote actors
717 public void testLocalTxActorRead() throws Exception {
718 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
719 doReturn(true).when(mockActorContext).isPathLocal(anyString());
721 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
723 // negative test case with null as the reply
724 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
725 any(ActorSelection.class), eqReadData());
727 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
728 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
730 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
732 // test case with node as read data reply
733 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
735 doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
736 any(ActorSelection.class), eqReadData());
738 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
740 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
742 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
744 // test for local data exists
745 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
746 any(ActorSelection.class), eqDataExists());
748 boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
750 assertEquals("Exists response", true, exists);
754 public void testLocalTxActorReady() throws Exception {
755 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
756 doReturn(true).when(mockActorContext).isPathLocal(anyString());
758 expectBatchedModificationsReady(actorRef, true);
760 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
762 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
763 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
765 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
767 assertTrue(ready instanceof SingleCommitCohortProxy);
769 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
772 private static interface TransactionProxyOperation {
773 void run(TransactionProxy transactionProxy);
776 private void throttleOperation(TransactionProxyOperation operation) {
777 throttleOperation(operation, 1, true);
780 private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef){
781 return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), Optional.<DataTree>absent());
784 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound){
785 ActorSystem actorSystem = getSystem();
786 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
788 doReturn(outstandingOpsLimit).when(mockActorContext).getTransactionOutstandingOperationLimit();
790 doReturn(actorSystem.actorSelection(shardActorRef.path())).
791 when(mockActorContext).actorSelection(shardActorRef.path().toString());
794 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
795 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
797 doReturn(Futures.failed(new Exception("not found")))
798 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
801 String actorPath = "akka.tcp://system@127.0.0.1:2550/user/tx-actor";
802 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
803 setTransactionId("txn-1").setTransactionActorPath(actorPath).
804 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
806 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
807 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
808 eqCreateTransaction(memberName, READ_WRITE));
810 doReturn(true).when(mockActorContext).isPathLocal(actorPath);
812 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
814 long start = System.nanoTime();
816 operation.run(transactionProxy);
818 long end = System.nanoTime();
820 long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
821 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
822 expected, (end-start)), (end - start) > expected);
826 private void completeOperation(TransactionProxyOperation operation){
827 completeOperation(operation, true);
830 private void completeOperation(TransactionProxyOperation operation, boolean shardFound){
831 ActorSystem actorSystem = getSystem();
832 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
834 doReturn(1).when(mockActorContext).getTransactionOutstandingOperationLimit();
836 doReturn(actorSystem.actorSelection(shardActorRef.path())).
837 when(mockActorContext).actorSelection(shardActorRef.path().toString());
840 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
841 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
843 doReturn(Futures.failed(new PrimaryNotFoundException("test")))
844 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
847 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
848 String actorPath = txActorRef.path().toString();
849 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
850 setTransactionId("txn-1").setTransactionActorPath(actorPath).
851 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
853 doReturn(actorSystem.actorSelection(actorPath)).when(mockActorContext).actorSelection(actorPath);
855 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
856 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
857 eqCreateTransaction(memberName, READ_WRITE));
859 doReturn(true).when(mockActorContext).isPathLocal(anyString());
861 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
863 long start = System.nanoTime();
865 operation.run(transactionProxy);
867 long end = System.nanoTime();
869 long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
870 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
871 expected, (end-start)), (end - start) <= expected);
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(mockComponentFactory, 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, true,
1223 new MergeModification(mergePath3, mergeNode3), new DeleteModification(deletePath2));
1225 assertEquals("getTotalMessageCount", 3, batchedModifications.get(2).getTotalMessagesSent());
1229 public void testReadWriteModificationOperationBatching() throws Throwable {
1230 testModificationOperationBatching(READ_WRITE);
1234 public void testWriteOnlyModificationOperationBatching() throws Throwable {
1235 testModificationOperationBatching(WRITE_ONLY);
1239 public void testOptimizedWriteOnlyModificationOperationBatching() throws Throwable {
1240 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
1241 testModificationOperationBatching(WRITE_ONLY);
1245 public void testModificationOperationBatchingWithInterleavedReads() throws Throwable {
1247 int shardBatchedModificationCount = 10;
1248 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1250 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
1252 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1254 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1255 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1257 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1258 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1260 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1261 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1263 YangInstanceIdentifier mergePath2 = TestModel.INNER_LIST_PATH;
1264 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1266 YangInstanceIdentifier deletePath = TestModel.OUTER_LIST_PATH;
1268 doReturn(readSerializedDataReply(writeNode2)).when(mockActorContext).executeOperationAsync(
1269 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1271 doReturn(readSerializedDataReply(mergeNode2)).when(mockActorContext).executeOperationAsync(
1272 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1274 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
1275 eq(actorSelection(actorRef)), eqSerializedDataExists());
1277 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
1279 transactionProxy.write(writePath1, writeNode1);
1280 transactionProxy.write(writePath2, writeNode2);
1282 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(writePath2).
1283 get(5, TimeUnit.SECONDS);
1285 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1286 assertEquals("Response NormalizedNode", writeNode2, readOptional.get());
1288 transactionProxy.merge(mergePath1, mergeNode1);
1289 transactionProxy.merge(mergePath2, mergeNode2);
1291 readOptional = transactionProxy.read(mergePath2).get(5, TimeUnit.SECONDS);
1293 transactionProxy.delete(deletePath);
1295 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
1296 assertEquals("Exists response", true, exists);
1298 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1299 assertEquals("Response NormalizedNode", mergeNode2, readOptional.get());
1301 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1302 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1304 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1305 new WriteModification(writePath2, writeNode2));
1307 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1308 new MergeModification(mergePath2, mergeNode2));
1310 verifyBatchedModifications(batchedModifications.get(2), false, new DeleteModification(deletePath));
1312 InOrder inOrder = Mockito.inOrder(mockActorContext);
1313 inOrder.verify(mockActorContext).executeOperationAsync(
1314 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1316 inOrder.verify(mockActorContext).executeOperationAsync(
1317 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1319 inOrder.verify(mockActorContext).executeOperationAsync(
1320 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1322 inOrder.verify(mockActorContext).executeOperationAsync(
1323 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1325 inOrder.verify(mockActorContext).executeOperationAsync(
1326 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1328 inOrder.verify(mockActorContext).executeOperationAsync(
1329 eq(actorSelection(actorRef)), eqSerializedDataExists());
1333 public void testReadRoot() throws ReadFailedException, InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
1335 SchemaContext schemaContext = SchemaContextHelper.full();
1336 Configuration configuration = mock(Configuration.class);
1337 doReturn(configuration).when(mockActorContext).getConfiguration();
1338 doReturn(schemaContext).when(mockActorContext).getSchemaContext();
1339 doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames();
1341 NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1342 NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
1344 setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext));
1345 setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext));
1347 doReturn(memberName).when(mockActorContext).getCurrentMemberName();
1349 doReturn(10).when(mockActorContext).getTransactionOutstandingOperationLimit();
1351 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
1353 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
1355 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
1356 YangInstanceIdentifier.builder().build()).get(5, TimeUnit.SECONDS);
1358 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1360 NormalizedNode<?, ?> normalizedNode = readOptional.get();
1362 assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
1364 Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
1366 for(NormalizedNode<?,?> node : collection){
1367 assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
1370 assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
1371 NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null);
1373 assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME));
1375 assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
1376 NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
1378 assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME));
1382 private void setUpReadData(String shardName, NormalizedNode<?, ?> expectedNode) {
1383 ActorSystem actorSystem = getSystem();
1384 ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
1386 doReturn(getSystem().actorSelection(shardActorRef.path())).
1387 when(mockActorContext).actorSelection(shardActorRef.path().toString());
1389 doReturn(primaryShardInfoReply(getSystem(), shardActorRef)).
1390 when(mockActorContext).findPrimaryShardAsync(eq(shardName));
1392 doReturn(true).when(mockActorContext).isPathLocal(shardActorRef.path().toString());
1394 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
1396 doReturn(actorSystem.actorSelection(txActorRef.path())).
1397 when(mockActorContext).actorSelection(txActorRef.path().toString());
1399 doReturn(Futures.successful(createTransactionReply(txActorRef, DataStoreVersions.CURRENT_VERSION))).when(mockActorContext).
1400 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
1401 eqCreateTransaction(memberName, TransactionType.READ_ONLY));
1403 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
1404 eq(actorSelection(txActorRef)), eqSerializedReadData(YangInstanceIdentifier.builder().build()));