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.ReadyTransaction;
45 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
46 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
47 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
48 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
49 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
50 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest;
51 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
52 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
53 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
54 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
55 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
56 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
58 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
60 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
61 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
62 import scala.concurrent.Promise;
64 @SuppressWarnings("resource")
65 public class TransactionProxyTest extends AbstractTransactionProxyTest {
67 @SuppressWarnings("serial")
68 static class TestException extends RuntimeException {
71 static interface Invoker {
72 CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception;
76 public void testRead() throws Exception {
77 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
79 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
81 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
82 eq(actorSelection(actorRef)), eqSerializedReadData());
84 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
85 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
87 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
89 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
91 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
92 eq(actorSelection(actorRef)), eqSerializedReadData());
94 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
96 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
98 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
101 @Test(expected = ReadFailedException.class)
102 public void testReadWithInvalidReplyMessageType() throws Exception {
103 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
105 doReturn(Futures.successful(new Object())).when(mockActorContext).
106 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
108 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
110 transactionProxy.read(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
113 @Test(expected = TestException.class)
114 public void testReadWithAsyncRemoteOperatonFailure() throws Throwable {
115 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
117 doReturn(Futures.failed(new TestException())).when(mockActorContext).
118 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
120 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
122 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
125 private void testExceptionOnInitialCreateTransaction(Exception exToThrow, Invoker invoker)
127 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
129 if (exToThrow instanceof PrimaryNotFoundException) {
130 doReturn(Futures.failed(exToThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
132 doReturn(primaryShardInfoReply(getSystem(), actorRef)).
133 when(mockActorContext).findPrimaryShardAsync(anyString());
136 doReturn(Futures.failed(exToThrow)).when(mockActorContext).executeOperationAsync(
137 any(ActorSelection.class), any());
139 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
141 propagateReadFailedExceptionCause(invoker.invoke(transactionProxy));
144 private void testReadWithExceptionOnInitialCreateTransaction(Exception exToThrow) throws Throwable {
145 testExceptionOnInitialCreateTransaction(exToThrow, new Invoker() {
147 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
148 return proxy.read(TestModel.TEST_PATH);
153 @Test(expected = PrimaryNotFoundException.class)
154 public void testReadWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
155 testReadWithExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"));
158 @Test(expected = TimeoutException.class)
159 public void testReadWhenATimeoutExceptionIsThrown() throws Throwable {
160 testReadWithExceptionOnInitialCreateTransaction(new TimeoutException("test",
161 new Exception("reason")));
164 @Test(expected = TestException.class)
165 public void testReadWhenAnyOtherExceptionIsThrown() throws Throwable {
166 testReadWithExceptionOnInitialCreateTransaction(new TestException());
170 public void testReadWithPriorRecordingOperationSuccessful() throws Throwable {
171 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
173 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
175 expectBatchedModifications(actorRef, 1);
177 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
178 eq(actorSelection(actorRef)), eqSerializedReadData());
180 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
182 transactionProxy.write(TestModel.TEST_PATH, expectedNode);
184 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
185 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
187 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
188 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
190 InOrder inOrder = Mockito.inOrder(mockActorContext);
191 inOrder.verify(mockActorContext).executeOperationAsync(
192 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
194 inOrder.verify(mockActorContext).executeOperationAsync(
195 eq(actorSelection(actorRef)), eqSerializedReadData());
198 @Test(expected=IllegalStateException.class)
199 public void testReadPreConditionCheck() {
200 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
201 transactionProxy.read(TestModel.TEST_PATH);
204 @Test(expected=IllegalArgumentException.class)
205 public void testInvalidCreateTransactionReply() throws Throwable {
206 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
208 doReturn(getSystem().actorSelection(actorRef.path())).when(mockActorContext).
209 actorSelection(actorRef.path().toString());
211 doReturn(primaryShardInfoReply(getSystem(), actorRef)).
212 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
214 doReturn(Futures.successful(new Object())).when(mockActorContext).executeOperationAsync(
215 eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_ONLY));
217 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
219 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
223 public void testExists() throws Exception {
224 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
226 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
228 doReturn(dataExistsSerializedReply(false)).when(mockActorContext).executeOperationAsync(
229 eq(actorSelection(actorRef)), eqSerializedDataExists());
231 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
233 assertEquals("Exists response", false, exists);
235 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
236 eq(actorSelection(actorRef)), eqSerializedDataExists());
238 exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
240 assertEquals("Exists response", true, exists);
243 @Test(expected = PrimaryNotFoundException.class)
244 public void testExistsWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
245 testExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"), new Invoker() {
247 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
248 return proxy.exists(TestModel.TEST_PATH);
253 @Test(expected = ReadFailedException.class)
254 public void testExistsWithInvalidReplyMessageType() throws Exception {
255 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
257 doReturn(Futures.successful(new Object())).when(mockActorContext).
258 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
260 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext,
263 transactionProxy.exists(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
266 @Test(expected = TestException.class)
267 public void testExistsWithAsyncRemoteOperatonFailure() throws Throwable {
268 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
270 doReturn(Futures.failed(new TestException())).when(mockActorContext).
271 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
273 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
275 propagateReadFailedExceptionCause(transactionProxy.exists(TestModel.TEST_PATH));
279 public void testExistsWithPriorRecordingOperationSuccessful() throws Throwable {
280 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
282 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
284 expectBatchedModifications(actorRef, 1);
286 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
287 eq(actorSelection(actorRef)), eqSerializedDataExists());
289 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
291 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
293 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
295 assertEquals("Exists response", true, exists);
297 InOrder inOrder = Mockito.inOrder(mockActorContext);
298 inOrder.verify(mockActorContext).executeOperationAsync(
299 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
301 inOrder.verify(mockActorContext).executeOperationAsync(
302 eq(actorSelection(actorRef)), eqSerializedDataExists());
305 @Test(expected=IllegalStateException.class)
306 public void testExistsPreConditionCheck() {
307 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
308 transactionProxy.exists(TestModel.TEST_PATH);
312 public void testWrite() throws Exception {
313 dataStoreContextBuilder.shardBatchedModificationCount(1);
314 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
316 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
318 expectBatchedModifications(actorRef, 1);
320 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
322 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
324 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), false);
328 public void testWriteAfterAsyncRead() throws Throwable {
329 ActorRef actorRef = setupActorContextWithoutInitialCreateTransaction(getSystem(), DefaultShardStrategy.DEFAULT_SHARD);
331 Promise<Object> createTxPromise = akka.dispatch.Futures.promise();
332 doReturn(createTxPromise).when(mockActorContext).executeOperationAsync(
333 eq(getSystem().actorSelection(actorRef.path())),
334 eqCreateTransaction(memberName, READ_WRITE));
336 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
337 eq(actorSelection(actorRef)), eqSerializedReadData());
339 expectBatchedModificationsReady(actorRef);
341 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
343 final TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
345 final CountDownLatch readComplete = new CountDownLatch(1);
346 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
347 com.google.common.util.concurrent.Futures.addCallback(transactionProxy.read(TestModel.TEST_PATH),
348 new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
350 public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
352 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
353 } catch (Exception e) {
356 readComplete.countDown();
361 public void onFailure(Throwable t) {
363 readComplete.countDown();
367 createTxPromise.success(createTransactionReply(actorRef, DataStoreVersions.CURRENT_VERSION));
369 Uninterruptibles.awaitUninterruptibly(readComplete, 5, TimeUnit.SECONDS);
371 if(caughtEx.get() != null) {
372 throw caughtEx.get();
375 // This sends the batched modification.
376 transactionProxy.ready();
378 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), true);
381 @Test(expected=IllegalStateException.class)
382 public void testWritePreConditionCheck() {
383 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
384 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
387 @Test(expected=IllegalStateException.class)
388 public void testWriteAfterReadyPreConditionCheck() {
389 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
391 transactionProxy.ready();
393 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
397 public void testMerge() throws Exception {
398 dataStoreContextBuilder.shardBatchedModificationCount(1);
399 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
401 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
403 expectBatchedModifications(actorRef, 1);
405 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
407 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
409 verifyOneBatchedModification(actorRef, new MergeModification(TestModel.TEST_PATH, nodeToWrite), false);
413 public void testDelete() throws Exception {
414 dataStoreContextBuilder.shardBatchedModificationCount(1);
415 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
417 expectBatchedModifications(actorRef, 1);
419 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
421 transactionProxy.delete(TestModel.TEST_PATH);
423 verifyOneBatchedModification(actorRef, new DeleteModification(TestModel.TEST_PATH), false);
427 public void testReadWrite() throws Exception {
428 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
430 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
432 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
433 eq(actorSelection(actorRef)), eqSerializedReadData());
435 expectBatchedModifications(actorRef, 1);
437 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
439 transactionProxy.read(TestModel.TEST_PATH);
441 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
443 transactionProxy.read(TestModel.TEST_PATH);
445 transactionProxy.read(TestModel.TEST_PATH);
447 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
448 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
450 verifyBatchedModifications(batchedModifications.get(0), false,
451 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
455 public void testReadyWithReadWrite() throws Exception {
456 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
458 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
460 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
461 eq(actorSelection(actorRef)), eqSerializedReadData());
463 expectBatchedModificationsReady(actorRef, true);
465 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
467 transactionProxy.read(TestModel.TEST_PATH);
469 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
471 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
473 assertTrue(ready instanceof SingleCommitCohortProxy);
475 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
477 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
478 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
480 verifyBatchedModifications(batchedModifications.get(0), true, true,
481 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
483 assertEquals("getTotalMessageCount", 1, batchedModifications.get(0).getTotalMessagesSent());
487 public void testReadyWithNoModifications() throws Exception {
488 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
490 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
491 eq(actorSelection(actorRef)), eqSerializedReadData());
493 expectBatchedModificationsReady(actorRef, true);
495 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
497 transactionProxy.read(TestModel.TEST_PATH);
499 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
501 assertTrue(ready instanceof SingleCommitCohortProxy);
503 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
505 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
506 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
508 verifyBatchedModifications(batchedModifications.get(0), true, true);
512 public void testReadyWithMultipleShardWrites() throws Exception {
513 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
515 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
517 expectBatchedModificationsReady(actorRef1);
518 expectBatchedModificationsReady(actorRef2);
520 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
522 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
523 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
525 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
527 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
529 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef1),
530 actorSelection(actorRef2));
534 public void testReadyWithWriteOnlyAndLastBatchPending() throws Exception {
535 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
537 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
539 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
541 expectBatchedModificationsReady(actorRef, true);
543 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
545 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
547 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
549 assertTrue(ready instanceof SingleCommitCohortProxy);
551 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
553 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
554 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
556 verifyBatchedModifications(batchedModifications.get(0), true, true,
557 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
559 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
560 isA(ReadyTransaction.SERIALIZABLE_CLASS));
564 public void testReadyWithWriteOnlyAndLastBatchEmpty() throws Exception {
565 dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
566 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
568 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
570 expectBatchedModificationsReady(actorRef, true);
572 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
574 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
576 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
578 assertTrue(ready instanceof SingleCommitCohortProxy);
580 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
582 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
583 assertEquals("Captured BatchedModifications count", 2, batchedModifications.size());
585 verifyBatchedModifications(batchedModifications.get(0), false,
586 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
588 verifyBatchedModifications(batchedModifications.get(1), true, true);
590 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
591 isA(ReadyTransaction.SERIALIZABLE_CLASS));
595 public void testReadyWithReplyFailure() throws Exception {
596 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
598 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
600 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
602 expectFailedBatchedModifications(actorRef);
604 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
606 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
608 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
610 assertTrue(ready instanceof SingleCommitCohortProxy);
612 verifyCohortFutures((SingleCommitCohortProxy)ready, TestException.class);
615 private void testWriteOnlyTxWithFindPrimaryShardFailure(Exception toThrow) throws Exception {
616 doReturn(Futures.failed(toThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
618 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
620 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
622 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
624 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
626 transactionProxy.delete(TestModel.TEST_PATH);
628 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
630 assertTrue(ready instanceof SingleCommitCohortProxy);
632 verifyCohortFutures((SingleCommitCohortProxy)ready, toThrow.getClass());
636 public void testWriteOnlyTxWithPrimaryNotFoundException() throws Exception {
637 testWriteOnlyTxWithFindPrimaryShardFailure(new PrimaryNotFoundException("mock"));
641 public void testWriteOnlyTxWithNotInitializedException() throws Exception {
642 testWriteOnlyTxWithFindPrimaryShardFailure(new NotInitializedException("mock"));
646 public void testWriteOnlyTxWithNoShardLeaderException() throws Exception {
647 testWriteOnlyTxWithFindPrimaryShardFailure(new NoShardLeaderException("mock"));
651 public void testReadyWithInvalidReplyMessageType() throws Exception {
652 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
653 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
655 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
657 doReturn(Futures.successful(new Object())).when(mockActorContext).
658 executeOperationAsync(eq(actorSelection(actorRef1)), isA(BatchedModifications.class));
660 expectBatchedModificationsReady(actorRef2);
662 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
664 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
665 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
667 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
669 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
671 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef2),
672 IllegalArgumentException.class);
676 public void testGetIdentifier() {
677 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
678 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext,
679 TransactionType.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(mockActorContext, 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(mockActorContext,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(mockActorContext, 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 void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound){
781 ActorSystem actorSystem = getSystem();
782 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
784 doReturn(outstandingOpsLimit).when(mockActorContext).getTransactionOutstandingOperationLimit();
786 doReturn(actorSystem.actorSelection(shardActorRef.path())).
787 when(mockActorContext).actorSelection(shardActorRef.path().toString());
790 doReturn(Futures.successful(actorSystem.actorSelection(shardActorRef.path()))).
791 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
793 doReturn(Futures.failed(new Exception("not found")))
794 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
797 String actorPath = "akka.tcp://system@127.0.0.1:2550/user/tx-actor";
798 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
799 setTransactionId("txn-1").setTransactionActorPath(actorPath).
800 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
802 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
803 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
804 eqCreateTransaction(memberName, READ_WRITE));
806 doReturn(true).when(mockActorContext).isPathLocal(actorPath);
808 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
810 long start = System.nanoTime();
812 operation.run(transactionProxy);
814 long end = System.nanoTime();
816 long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
817 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
818 expected, (end-start)), (end - start) > expected);
822 private void completeOperation(TransactionProxyOperation operation){
823 completeOperation(operation, true);
826 private void completeOperation(TransactionProxyOperation operation, boolean shardFound){
827 ActorSystem actorSystem = getSystem();
828 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
830 doReturn(1).when(mockActorContext).getTransactionOutstandingOperationLimit();
832 doReturn(actorSystem.actorSelection(shardActorRef.path())).
833 when(mockActorContext).actorSelection(shardActorRef.path().toString());
836 doReturn(primaryShardInfoReply(actorSystem, shardActorRef)).
837 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
839 doReturn(Futures.failed(new PrimaryNotFoundException("test")))
840 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
843 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
844 String actorPath = txActorRef.path().toString();
845 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
846 setTransactionId("txn-1").setTransactionActorPath(actorPath).
847 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
849 doReturn(actorSystem.actorSelection(actorPath)).when(mockActorContext).actorSelection(actorPath);
851 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
852 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
853 eqCreateTransaction(memberName, READ_WRITE));
855 doReturn(true).when(mockActorContext).isPathLocal(anyString());
857 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
859 long start = System.nanoTime();
861 operation.run(transactionProxy);
863 long end = System.nanoTime();
865 long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
866 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
867 expected, (end-start)), (end - start) <= expected);
870 public void testWriteThrottling(boolean shardFound){
872 throttleOperation(new TransactionProxyOperation() {
874 public void run(TransactionProxy transactionProxy) {
875 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
877 expectBatchedModifications(2);
879 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
881 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
887 public void testWriteThrottlingWhenShardFound(){
888 dataStoreContextBuilder.shardBatchedModificationCount(1);
889 throttleOperation(new TransactionProxyOperation() {
891 public void run(TransactionProxy transactionProxy) {
892 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
894 expectIncompleteBatchedModifications();
896 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
898 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
904 public void testWriteThrottlingWhenShardNotFound(){
905 // Confirm that there is no throttling when the Shard is not found
906 dataStoreContextBuilder.shardBatchedModificationCount(1);
907 completeOperation(new TransactionProxyOperation() {
909 public void run(TransactionProxy transactionProxy) {
910 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
912 expectBatchedModifications(2);
914 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
916 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
924 public void testWriteCompletion(){
925 dataStoreContextBuilder.shardBatchedModificationCount(1);
926 completeOperation(new TransactionProxyOperation() {
928 public void run(TransactionProxy transactionProxy) {
929 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
931 expectBatchedModifications(2);
933 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
935 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
941 public void testMergeThrottlingWhenShardFound(){
942 dataStoreContextBuilder.shardBatchedModificationCount(1);
943 throttleOperation(new TransactionProxyOperation() {
945 public void run(TransactionProxy transactionProxy) {
946 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
948 expectIncompleteBatchedModifications();
950 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
952 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
958 public void testMergeThrottlingWhenShardNotFound(){
959 dataStoreContextBuilder.shardBatchedModificationCount(1);
960 completeOperation(new TransactionProxyOperation() {
962 public void run(TransactionProxy transactionProxy) {
963 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
965 expectBatchedModifications(2);
967 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
969 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
975 public void testMergeCompletion(){
976 dataStoreContextBuilder.shardBatchedModificationCount(1);
977 completeOperation(new TransactionProxyOperation() {
979 public void run(TransactionProxy transactionProxy) {
980 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
982 expectBatchedModifications(2);
984 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
986 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
993 public void testDeleteThrottlingWhenShardFound(){
995 throttleOperation(new TransactionProxyOperation() {
997 public void run(TransactionProxy transactionProxy) {
998 expectIncompleteBatchedModifications();
1000 transactionProxy.delete(TestModel.TEST_PATH);
1002 transactionProxy.delete(TestModel.TEST_PATH);
1009 public void testDeleteThrottlingWhenShardNotFound(){
1011 completeOperation(new TransactionProxyOperation() {
1013 public void run(TransactionProxy transactionProxy) {
1014 expectBatchedModifications(2);
1016 transactionProxy.delete(TestModel.TEST_PATH);
1018 transactionProxy.delete(TestModel.TEST_PATH);
1024 public void testDeleteCompletion(){
1025 dataStoreContextBuilder.shardBatchedModificationCount(1);
1026 completeOperation(new TransactionProxyOperation() {
1028 public void run(TransactionProxy transactionProxy) {
1029 expectBatchedModifications(2);
1031 transactionProxy.delete(TestModel.TEST_PATH);
1033 transactionProxy.delete(TestModel.TEST_PATH);
1040 public void testReadThrottlingWhenShardFound(){
1042 throttleOperation(new TransactionProxyOperation() {
1044 public void run(TransactionProxy transactionProxy) {
1045 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1046 any(ActorSelection.class), eqReadData());
1048 transactionProxy.read(TestModel.TEST_PATH);
1050 transactionProxy.read(TestModel.TEST_PATH);
1056 public void testReadThrottlingWhenShardNotFound(){
1058 completeOperation(new TransactionProxyOperation() {
1060 public void run(TransactionProxy transactionProxy) {
1061 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1062 any(ActorSelection.class), eqReadData());
1064 transactionProxy.read(TestModel.TEST_PATH);
1066 transactionProxy.read(TestModel.TEST_PATH);
1073 public void testReadCompletion(){
1074 completeOperation(new TransactionProxyOperation() {
1076 public void run(TransactionProxy transactionProxy) {
1077 NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1079 doReturn(readDataReply(nodeToRead)).when(mockActorContext).executeOperationAsync(
1080 any(ActorSelection.class), eqReadData());
1082 transactionProxy.read(TestModel.TEST_PATH);
1084 transactionProxy.read(TestModel.TEST_PATH);
1091 public void testExistsThrottlingWhenShardFound(){
1093 throttleOperation(new TransactionProxyOperation() {
1095 public void run(TransactionProxy transactionProxy) {
1096 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1097 any(ActorSelection.class), eqDataExists());
1099 transactionProxy.exists(TestModel.TEST_PATH);
1101 transactionProxy.exists(TestModel.TEST_PATH);
1107 public void testExistsThrottlingWhenShardNotFound(){
1109 completeOperation(new TransactionProxyOperation() {
1111 public void run(TransactionProxy transactionProxy) {
1112 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1113 any(ActorSelection.class), eqDataExists());
1115 transactionProxy.exists(TestModel.TEST_PATH);
1117 transactionProxy.exists(TestModel.TEST_PATH);
1124 public void testExistsCompletion(){
1125 completeOperation(new TransactionProxyOperation() {
1127 public void run(TransactionProxy transactionProxy) {
1128 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
1129 any(ActorSelection.class), eqDataExists());
1131 transactionProxy.exists(TestModel.TEST_PATH);
1133 transactionProxy.exists(TestModel.TEST_PATH);
1140 public void testReadyThrottling(){
1142 throttleOperation(new TransactionProxyOperation() {
1144 public void run(TransactionProxy transactionProxy) {
1145 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1147 expectBatchedModifications(1);
1149 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1150 any(ActorSelection.class), any(ReadyTransaction.class));
1152 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1154 transactionProxy.ready();
1160 public void testReadyThrottlingWithTwoTransactionContexts(){
1162 throttleOperation(new TransactionProxyOperation() {
1164 public void run(TransactionProxy transactionProxy) {
1165 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1166 NormalizedNode<?, ?> carsNode = ImmutableNodes.containerNode(CarsModel.BASE_QNAME);
1168 expectBatchedModifications(2);
1170 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1171 any(ActorSelection.class), any(ReadyTransaction.class));
1173 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1175 transactionProxy.write(TestModel.TEST_PATH, carsNode);
1177 transactionProxy.ready();
1182 private void testModificationOperationBatching(TransactionType type) throws Exception {
1183 int shardBatchedModificationCount = 3;
1184 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1186 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), type);
1188 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1190 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1191 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1193 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1194 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1196 YangInstanceIdentifier writePath3 = TestModel.INNER_LIST_PATH;
1197 NormalizedNode<?, ?> writeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1199 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1200 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1202 YangInstanceIdentifier mergePath2 = TestModel.OUTER_LIST_PATH;
1203 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1205 YangInstanceIdentifier mergePath3 = TestModel.INNER_LIST_PATH;
1206 NormalizedNode<?, ?> mergeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1208 YangInstanceIdentifier deletePath1 = TestModel.TEST_PATH;
1209 YangInstanceIdentifier deletePath2 = TestModel.OUTER_LIST_PATH;
1211 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, type);
1213 transactionProxy.write(writePath1, writeNode1);
1214 transactionProxy.write(writePath2, writeNode2);
1215 transactionProxy.delete(deletePath1);
1216 transactionProxy.merge(mergePath1, mergeNode1);
1217 transactionProxy.merge(mergePath2, mergeNode2);
1218 transactionProxy.write(writePath3, writeNode3);
1219 transactionProxy.merge(mergePath3, mergeNode3);
1220 transactionProxy.delete(deletePath2);
1222 // This sends the last batch.
1223 transactionProxy.ready();
1225 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1226 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1228 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1229 new WriteModification(writePath2, writeNode2), new DeleteModification(deletePath1));
1231 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1232 new MergeModification(mergePath2, mergeNode2), new WriteModification(writePath3, writeNode3));
1234 verifyBatchedModifications(batchedModifications.get(2), true, true,
1235 new MergeModification(mergePath3, mergeNode3), new DeleteModification(deletePath2));
1237 assertEquals("getTotalMessageCount", 3, batchedModifications.get(2).getTotalMessagesSent());
1241 public void testReadWriteModificationOperationBatching() throws Throwable {
1242 testModificationOperationBatching(READ_WRITE);
1246 public void testWriteOnlyModificationOperationBatching() throws Throwable {
1247 testModificationOperationBatching(WRITE_ONLY);
1251 public void testOptimizedWriteOnlyModificationOperationBatching() throws Throwable {
1252 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
1253 testModificationOperationBatching(WRITE_ONLY);
1257 public void testModificationOperationBatchingWithInterleavedReads() throws Throwable {
1259 int shardBatchedModificationCount = 10;
1260 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1262 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
1264 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1266 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1267 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1269 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1270 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1272 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1273 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1275 YangInstanceIdentifier mergePath2 = TestModel.INNER_LIST_PATH;
1276 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1278 YangInstanceIdentifier deletePath = TestModel.OUTER_LIST_PATH;
1280 doReturn(readSerializedDataReply(writeNode2)).when(mockActorContext).executeOperationAsync(
1281 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1283 doReturn(readSerializedDataReply(mergeNode2)).when(mockActorContext).executeOperationAsync(
1284 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1286 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
1287 eq(actorSelection(actorRef)), eqSerializedDataExists());
1289 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
1291 transactionProxy.write(writePath1, writeNode1);
1292 transactionProxy.write(writePath2, writeNode2);
1294 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(writePath2).
1295 get(5, TimeUnit.SECONDS);
1297 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1298 assertEquals("Response NormalizedNode", writeNode2, readOptional.get());
1300 transactionProxy.merge(mergePath1, mergeNode1);
1301 transactionProxy.merge(mergePath2, mergeNode2);
1303 readOptional = transactionProxy.read(mergePath2).get(5, TimeUnit.SECONDS);
1305 transactionProxy.delete(deletePath);
1307 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
1308 assertEquals("Exists response", true, exists);
1310 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1311 assertEquals("Response NormalizedNode", mergeNode2, readOptional.get());
1313 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1314 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1316 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1317 new WriteModification(writePath2, writeNode2));
1319 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1320 new MergeModification(mergePath2, mergeNode2));
1322 verifyBatchedModifications(batchedModifications.get(2), false, new DeleteModification(deletePath));
1324 InOrder inOrder = Mockito.inOrder(mockActorContext);
1325 inOrder.verify(mockActorContext).executeOperationAsync(
1326 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1328 inOrder.verify(mockActorContext).executeOperationAsync(
1329 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1331 inOrder.verify(mockActorContext).executeOperationAsync(
1332 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1334 inOrder.verify(mockActorContext).executeOperationAsync(
1335 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1337 inOrder.verify(mockActorContext).executeOperationAsync(
1338 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1340 inOrder.verify(mockActorContext).executeOperationAsync(
1341 eq(actorSelection(actorRef)), eqSerializedDataExists());
1345 public void testReadRoot() throws ReadFailedException, InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
1347 SchemaContext schemaContext = SchemaContextHelper.full();
1348 Configuration configuration = mock(Configuration.class);
1349 doReturn(configuration).when(mockActorContext).getConfiguration();
1350 doReturn(schemaContext).when(mockActorContext).getSchemaContext();
1351 doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames();
1353 NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1354 NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
1356 setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext));
1357 setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext));
1359 doReturn(memberName).when(mockActorContext).getCurrentMemberName();
1361 doReturn(10).when(mockActorContext).getTransactionOutstandingOperationLimit();
1363 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
1365 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
1367 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
1368 YangInstanceIdentifier.builder().build()).get(5, TimeUnit.SECONDS);
1370 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1372 NormalizedNode<?, ?> normalizedNode = readOptional.get();
1374 assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
1376 Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
1378 for(NormalizedNode<?,?> node : collection){
1379 assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
1382 assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
1383 NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null);
1385 assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME));
1387 assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
1388 NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
1390 assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME));
1394 private void setUpReadData(String shardName, NormalizedNode<?, ?> expectedNode) {
1395 ActorSystem actorSystem = getSystem();
1396 ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
1398 doReturn(getSystem().actorSelection(shardActorRef.path())).
1399 when(mockActorContext).actorSelection(shardActorRef.path().toString());
1401 doReturn(primaryShardInfoReply(getSystem(), shardActorRef)).
1402 when(mockActorContext).findPrimaryShardAsync(eq(shardName));
1404 doReturn(true).when(mockActorContext).isPathLocal(shardActorRef.path().toString());
1406 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
1408 doReturn(actorSystem.actorSelection(txActorRef.path())).
1409 when(mockActorContext).actorSelection(txActorRef.path().toString());
1411 doReturn(Futures.successful(createTransactionReply(txActorRef, DataStoreVersions.CURRENT_VERSION))).when(mockActorContext).
1412 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
1413 eqCreateTransaction(memberName, TransactionType.READ_ONLY));
1415 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
1416 eq(actorSelection(txActorRef)), eqSerializedReadData(YangInstanceIdentifier.builder().build()));