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.junit.Assert.fail;
7 import static org.mockito.Matchers.any;
8 import static org.mockito.Matchers.anyString;
9 import static org.mockito.Matchers.eq;
10 import static org.mockito.Matchers.isA;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.never;
14 import static org.mockito.Mockito.verify;
15 import static org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType.READ_ONLY;
16 import static org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType.READ_WRITE;
17 import static org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType.WRITE_ONLY;
18 import akka.actor.ActorRef;
19 import akka.actor.ActorSelection;
20 import akka.actor.ActorSystem;
21 import akka.actor.Props;
22 import akka.dispatch.Futures;
23 import com.google.common.base.Optional;
24 import com.google.common.collect.Sets;
25 import com.google.common.util.concurrent.CheckedFuture;
26 import com.google.common.util.concurrent.FutureCallback;
27 import com.google.common.util.concurrent.Uninterruptibles;
28 import java.util.Collection;
29 import java.util.List;
30 import java.util.concurrent.CountDownLatch;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.TimeUnit;
33 import java.util.concurrent.atomic.AtomicReference;
34 import org.junit.Assert;
35 import org.junit.Test;
36 import org.mockito.InOrder;
37 import org.mockito.Mockito;
38 import org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType;
39 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
40 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
41 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
42 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
43 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
44 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
45 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
46 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
47 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
48 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
49 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
50 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
51 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
52 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest;
53 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
54 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
55 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
56 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
57 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
58 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
60 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
62 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
63 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
64 import scala.concurrent.Await;
65 import scala.concurrent.Future;
66 import scala.concurrent.Promise;
67 import scala.concurrent.duration.Duration;
69 @SuppressWarnings("resource")
70 public class TransactionProxyTest extends AbstractTransactionProxyTest {
72 @SuppressWarnings("serial")
73 static class TestException extends RuntimeException {
76 static interface Invoker {
77 CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception;
81 public void testRead() throws Exception {
82 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
84 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
86 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
87 eq(actorSelection(actorRef)), eqSerializedReadData());
89 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
90 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
92 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
94 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
96 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
97 eq(actorSelection(actorRef)), eqSerializedReadData());
99 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
101 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
103 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
106 @Test(expected = ReadFailedException.class)
107 public void testReadWithInvalidReplyMessageType() throws Exception {
108 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
110 doReturn(Futures.successful(new Object())).when(mockActorContext).
111 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
113 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
115 transactionProxy.read(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
118 @Test(expected = TestException.class)
119 public void testReadWithAsyncRemoteOperatonFailure() throws Throwable {
120 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
122 doReturn(Futures.failed(new TestException())).when(mockActorContext).
123 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
125 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
127 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
130 private void testExceptionOnInitialCreateTransaction(Exception exToThrow, Invoker invoker)
132 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
134 if (exToThrow instanceof PrimaryNotFoundException) {
135 doReturn(Futures.failed(exToThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
137 doReturn(Futures.successful(getSystem().actorSelection(actorRef.path()))).
138 when(mockActorContext).findPrimaryShardAsync(anyString());
141 doReturn(Futures.failed(exToThrow)).when(mockActorContext).executeOperationAsync(
142 any(ActorSelection.class), any());
144 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
146 propagateReadFailedExceptionCause(invoker.invoke(transactionProxy));
149 private void testReadWithExceptionOnInitialCreateTransaction(Exception exToThrow) throws Throwable {
150 testExceptionOnInitialCreateTransaction(exToThrow, new Invoker() {
152 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
153 return proxy.read(TestModel.TEST_PATH);
158 @Test(expected = PrimaryNotFoundException.class)
159 public void testReadWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
160 testReadWithExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"));
163 @Test(expected = TimeoutException.class)
164 public void testReadWhenATimeoutExceptionIsThrown() throws Throwable {
165 testReadWithExceptionOnInitialCreateTransaction(new TimeoutException("test",
166 new Exception("reason")));
169 @Test(expected = TestException.class)
170 public void testReadWhenAnyOtherExceptionIsThrown() throws Throwable {
171 testReadWithExceptionOnInitialCreateTransaction(new TestException());
175 public void testReadWithPriorRecordingOperationSuccessful() throws Throwable {
176 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
178 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
180 expectBatchedModifications(actorRef, 1);
182 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
183 eq(actorSelection(actorRef)), eqSerializedReadData());
185 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
187 transactionProxy.write(TestModel.TEST_PATH, expectedNode);
189 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
190 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
192 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
193 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
195 InOrder inOrder = Mockito.inOrder(mockActorContext);
196 inOrder.verify(mockActorContext).executeOperationAsync(
197 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
199 inOrder.verify(mockActorContext).executeOperationAsync(
200 eq(actorSelection(actorRef)), eqSerializedReadData());
203 @Test(expected=IllegalStateException.class)
204 public void testReadPreConditionCheck() {
205 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
206 transactionProxy.read(TestModel.TEST_PATH);
209 @Test(expected=IllegalArgumentException.class)
210 public void testInvalidCreateTransactionReply() throws Throwable {
211 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
213 doReturn(getSystem().actorSelection(actorRef.path())).when(mockActorContext).
214 actorSelection(actorRef.path().toString());
216 doReturn(Futures.successful(getSystem().actorSelection(actorRef.path()))).
217 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
219 doReturn(Futures.successful(new Object())).when(mockActorContext).executeOperationAsync(
220 eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_ONLY));
222 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
224 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
228 public void testExists() throws Exception {
229 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
231 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
233 doReturn(dataExistsSerializedReply(false)).when(mockActorContext).executeOperationAsync(
234 eq(actorSelection(actorRef)), eqSerializedDataExists());
236 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
238 assertEquals("Exists response", false, exists);
240 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
241 eq(actorSelection(actorRef)), eqSerializedDataExists());
243 exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
245 assertEquals("Exists response", true, exists);
248 @Test(expected = PrimaryNotFoundException.class)
249 public void testExistsWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
250 testExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"), new Invoker() {
252 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
253 return proxy.exists(TestModel.TEST_PATH);
258 @Test(expected = ReadFailedException.class)
259 public void testExistsWithInvalidReplyMessageType() throws Exception {
260 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
262 doReturn(Futures.successful(new Object())).when(mockActorContext).
263 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
265 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext,
268 transactionProxy.exists(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
271 @Test(expected = TestException.class)
272 public void testExistsWithAsyncRemoteOperatonFailure() throws Throwable {
273 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
275 doReturn(Futures.failed(new TestException())).when(mockActorContext).
276 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
278 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
280 propagateReadFailedExceptionCause(transactionProxy.exists(TestModel.TEST_PATH));
284 public void testExistsWithPriorRecordingOperationSuccessful() throws Throwable {
285 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
287 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
289 expectBatchedModifications(actorRef, 1);
291 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
292 eq(actorSelection(actorRef)), eqSerializedDataExists());
294 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
296 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
298 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
300 assertEquals("Exists response", true, exists);
302 InOrder inOrder = Mockito.inOrder(mockActorContext);
303 inOrder.verify(mockActorContext).executeOperationAsync(
304 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
306 inOrder.verify(mockActorContext).executeOperationAsync(
307 eq(actorSelection(actorRef)), eqSerializedDataExists());
310 @Test(expected=IllegalStateException.class)
311 public void testExistsPreConditionCheck() {
312 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
313 transactionProxy.exists(TestModel.TEST_PATH);
316 private void verifyRecordingOperationFutures(List<Future<Object>> futures,
317 Class<?>... expResultTypes) throws Exception {
318 assertEquals("getRecordingOperationFutures size", expResultTypes.length, futures.size());
321 for( Future<Object> future: futures) {
322 assertNotNull("Recording operation Future is null", future);
324 Class<?> expResultType = expResultTypes[i++];
325 if(Throwable.class.isAssignableFrom(expResultType)) {
327 Await.result(future, Duration.create(5, TimeUnit.SECONDS));
328 fail("Expected exception from recording operation Future");
329 } catch(Exception e) {
333 assertEquals(String.format("Recording operation %d Future result type", i +1 ), expResultType,
334 Await.result(future, Duration.create(5, TimeUnit.SECONDS)).getClass());
340 public void testWrite() throws Exception {
341 dataStoreContextBuilder.shardBatchedModificationCount(1);
342 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
344 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
346 expectBatchedModifications(actorRef, 1);
348 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
350 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
352 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), false);
356 public void testWriteAfterAsyncRead() throws Throwable {
357 ActorRef actorRef = setupActorContextWithoutInitialCreateTransaction(getSystem());
359 Promise<Object> createTxPromise = akka.dispatch.Futures.promise();
360 doReturn(createTxPromise).when(mockActorContext).executeOperationAsync(
361 eq(getSystem().actorSelection(actorRef.path())),
362 eqCreateTransaction(memberName, READ_WRITE));
364 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
365 eq(actorSelection(actorRef)), eqSerializedReadData());
367 expectBatchedModifications(actorRef, 1);
368 expectReadyTransaction(actorRef);
370 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
372 final TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
374 final CountDownLatch readComplete = new CountDownLatch(1);
375 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
376 com.google.common.util.concurrent.Futures.addCallback(transactionProxy.read(TestModel.TEST_PATH),
377 new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
379 public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
381 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
382 } catch (Exception e) {
385 readComplete.countDown();
390 public void onFailure(Throwable t) {
392 readComplete.countDown();
396 createTxPromise.success(createTransactionReply(actorRef, DataStoreVersions.CURRENT_VERSION));
398 Uninterruptibles.awaitUninterruptibly(readComplete, 5, TimeUnit.SECONDS);
400 if(caughtEx.get() != null) {
401 throw caughtEx.get();
404 // This sends the batched modification.
405 transactionProxy.ready();
407 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), false);
409 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures(),
410 BatchedModificationsReply.class);
413 @Test(expected=IllegalStateException.class)
414 public void testWritePreConditionCheck() {
415 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
416 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
419 @Test(expected=IllegalStateException.class)
420 public void testWriteAfterReadyPreConditionCheck() {
421 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
423 transactionProxy.ready();
425 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
429 public void testMerge() throws Exception {
430 dataStoreContextBuilder.shardBatchedModificationCount(1);
431 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
433 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
435 expectBatchedModifications(actorRef, 1);
437 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
439 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
441 verifyOneBatchedModification(actorRef, new MergeModification(TestModel.TEST_PATH, nodeToWrite), false);
445 public void testDelete() throws Exception {
446 dataStoreContextBuilder.shardBatchedModificationCount(1);
447 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
449 expectBatchedModifications(actorRef, 1);
451 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
453 transactionProxy.delete(TestModel.TEST_PATH);
455 verifyOneBatchedModification(actorRef, new DeleteModification(TestModel.TEST_PATH), false);
459 public void testReadyWithReadWrite() throws Exception {
460 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
462 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
464 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
465 eq(actorSelection(actorRef)), eqSerializedReadData());
467 expectBatchedModifications(actorRef, 1);
468 expectReadyTransaction(actorRef);
470 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
472 transactionProxy.read(TestModel.TEST_PATH);
474 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
476 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
478 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
480 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
482 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures(),
483 BatchedModificationsReply.class);
485 verifyCohortFutures(proxy, getSystem().actorSelection(actorRef.path()));
487 verify(mockActorContext).executeOperationAsync(eq(actorSelection(actorRef)),
488 isA(BatchedModifications.class));
490 verify(mockActorContext).executeOperationAsync(eq(actorSelection(actorRef)),
491 isA(ReadyTransaction.SERIALIZABLE_CLASS));
495 public void testReadyWithWriteOnlyAndLastBatchPending() throws Exception {
496 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
498 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
500 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
502 expectBatchedModificationsReady(actorRef, 1);
504 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
506 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
508 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
510 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
512 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
514 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures());
516 verifyCohortFutures(proxy, getSystem().actorSelection(actorRef.path()));
518 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
519 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
521 verifyBatchedModifications(batchedModifications.get(0), true,
522 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
524 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
525 isA(ReadyTransaction.SERIALIZABLE_CLASS));
529 public void testReadyWithWriteOnlyAndLastBatchEmpty() throws Exception {
530 dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
531 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
533 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
535 expectBatchedModificationsReady(actorRef, 1);
537 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
539 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
541 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
543 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
545 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
547 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures(),
548 BatchedModificationsReply.class);
550 verifyCohortFutures(proxy, getSystem().actorSelection(actorRef.path()));
552 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
553 assertEquals("Captured BatchedModifications count", 2, batchedModifications.size());
555 verifyBatchedModifications(batchedModifications.get(0), false,
556 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
558 verifyBatchedModifications(batchedModifications.get(1), true);
560 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
561 isA(ReadyTransaction.SERIALIZABLE_CLASS));
565 public void testReadyWithRecordingOperationFailure() throws Exception {
566 dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
568 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
570 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
572 expectFailedBatchedModifications(actorRef);
574 doReturn(false).when(mockActorContext).isPathLocal(actorRef.path().toString());
576 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
578 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
580 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
582 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
584 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
586 verifyCohortFutures(proxy, TestException.class);
588 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures(), TestException.class);
592 public void testReadyWithReplyFailure() throws Exception {
593 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
595 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
597 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
599 expectFailedBatchedModifications(actorRef);
601 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
603 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
605 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
607 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
609 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
611 verifyCohortFutures(proxy, TestException.class);
614 private void testWriteOnlyTxWithFindPrimaryShardFailure(Exception toThrow) throws Exception {
615 doReturn(Futures.failed(toThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
617 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
619 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
621 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
623 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
625 transactionProxy.delete(TestModel.TEST_PATH);
627 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
629 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
631 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
633 verifyCohortFutures(proxy, 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 actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
656 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
658 //expectBatchedModifications(actorRef, 1);
660 doReturn(Futures.successful(new Object())).when(mockActorContext).
661 executeOperationAsync(eq(actorSelection(actorRef)),
662 isA(BatchedModifications.class));
664 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
666 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
668 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
670 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
672 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
674 verifyCohortFutures(proxy, IllegalArgumentException.class);
678 public void testGetIdentifier() {
679 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
680 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext,
681 TransactionProxy.TransactionType.READ_ONLY);
683 Object id = transactionProxy.getIdentifier();
684 assertNotNull("getIdentifier returned null", id);
685 assertTrue("Invalid identifier: " + id, id.toString().startsWith(memberName));
689 public void testClose() throws Exception{
690 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
692 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
693 eq(actorSelection(actorRef)), eqSerializedReadData());
695 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
697 transactionProxy.read(TestModel.TEST_PATH);
699 transactionProxy.close();
701 verify(mockActorContext).sendOperationAsync(
702 eq(actorSelection(actorRef)), isA(CloseTransaction.SERIALIZABLE_CLASS));
707 * Method to test a local Tx actor. The Tx paths are matched to decide if the
708 * Tx actor is local or not. This is done by mocking the Tx actor path
709 * and the caller paths and ensuring that the paths have the remote-address format
711 * Note: Since the default akka provider for test is not a RemoteActorRefProvider,
712 * the paths returned for the actors for all the tests are not qualified remote paths.
713 * Hence are treated as non-local/remote actors. In short, all tests except
714 * few below run for remote actors
719 public void testLocalTxActorRead() throws Exception {
720 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
721 doReturn(true).when(mockActorContext).isPathLocal(anyString());
723 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext,READ_ONLY);
725 // negative test case with null as the reply
726 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
727 any(ActorSelection.class), eqReadData());
729 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
730 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
732 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
734 // test case with node as read data reply
735 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
737 doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
738 any(ActorSelection.class), eqReadData());
740 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
742 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
744 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
746 // test for local data exists
747 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
748 any(ActorSelection.class), eqDataExists());
750 boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
752 assertEquals("Exists response", true, exists);
756 public void testLocalTxActorReady() throws Exception {
757 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
758 doReturn(true).when(mockActorContext).isPathLocal(anyString());
760 doReturn(batchedModificationsReply(1)).when(mockActorContext).executeOperationAsync(
761 any(ActorSelection.class), isA(BatchedModifications.class));
763 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
765 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
766 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
769 doReturn(readyTxReply(actorRef.path().toString())).when(mockActorContext).executeOperationAsync(
770 eq(actorSelection(actorRef)), isA(ReadyTransaction.class));
772 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
774 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
776 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
778 verifyCohortFutures(proxy, getSystem().actorSelection(actorRef.path()));
781 private static interface TransactionProxyOperation {
782 void run(TransactionProxy transactionProxy);
785 private void throttleOperation(TransactionProxyOperation operation) {
786 throttleOperation(operation, 1, true);
789 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound){
790 ActorSystem actorSystem = getSystem();
791 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
793 doReturn(outstandingOpsLimit).when(mockActorContext).getTransactionOutstandingOperationLimit();
795 doReturn(actorSystem.actorSelection(shardActorRef.path())).
796 when(mockActorContext).actorSelection(shardActorRef.path().toString());
799 doReturn(Futures.successful(actorSystem.actorSelection(shardActorRef.path()))).
800 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
802 doReturn(Futures.failed(new Exception("not found")))
803 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
806 String actorPath = "akka.tcp://system@127.0.0.1:2550/user/tx-actor";
807 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
808 setTransactionId("txn-1").setTransactionActorPath(actorPath).
809 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
811 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
812 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
813 eqCreateTransaction(memberName, READ_WRITE));
815 doReturn(true).when(mockActorContext).isPathLocal(actorPath);
817 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
819 long start = System.nanoTime();
821 operation.run(transactionProxy);
823 long end = System.nanoTime();
825 long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
826 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
827 expected, (end-start)), (end - start) > expected);
831 private void completeOperation(TransactionProxyOperation operation){
832 completeOperation(operation, true);
835 private void completeOperation(TransactionProxyOperation operation, boolean shardFound){
836 ActorSystem actorSystem = getSystem();
837 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
839 doReturn(1).when(mockActorContext).getTransactionOutstandingOperationLimit();
841 doReturn(actorSystem.actorSelection(shardActorRef.path())).
842 when(mockActorContext).actorSelection(shardActorRef.path().toString());
845 doReturn(Futures.successful(actorSystem.actorSelection(shardActorRef.path()))).
846 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
848 doReturn(Futures.failed(new PrimaryNotFoundException("test")))
849 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
852 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
853 String actorPath = txActorRef.path().toString();
854 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
855 setTransactionId("txn-1").setTransactionActorPath(actorPath).
856 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
858 doReturn(actorSystem.actorSelection(actorPath)).when(mockActorContext).actorSelection(actorPath);
860 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
861 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
862 eqCreateTransaction(memberName, READ_WRITE));
864 doReturn(true).when(mockActorContext).isPathLocal(anyString());
866 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
868 long start = System.nanoTime();
870 operation.run(transactionProxy);
872 long end = System.nanoTime();
874 long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
875 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
876 expected, (end-start)), (end - start) <= expected);
879 public void testWriteThrottling(boolean shardFound){
881 throttleOperation(new TransactionProxyOperation() {
883 public void run(TransactionProxy transactionProxy) {
884 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
886 expectBatchedModifications(2);
888 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
890 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
896 public void testWriteThrottlingWhenShardFound(){
897 dataStoreContextBuilder.shardBatchedModificationCount(1);
898 throttleOperation(new TransactionProxyOperation() {
900 public void run(TransactionProxy transactionProxy) {
901 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
903 expectIncompleteBatchedModifications();
905 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
907 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
913 public void testWriteThrottlingWhenShardNotFound(){
914 // Confirm that there is no throttling when the Shard is not found
915 dataStoreContextBuilder.shardBatchedModificationCount(1);
916 completeOperation(new TransactionProxyOperation() {
918 public void run(TransactionProxy transactionProxy) {
919 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
921 expectBatchedModifications(2);
923 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
925 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
933 public void testWriteCompletion(){
934 dataStoreContextBuilder.shardBatchedModificationCount(1);
935 completeOperation(new TransactionProxyOperation() {
937 public void run(TransactionProxy transactionProxy) {
938 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
940 expectBatchedModifications(2);
942 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
944 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
950 public void testMergeThrottlingWhenShardFound(){
951 dataStoreContextBuilder.shardBatchedModificationCount(1);
952 throttleOperation(new TransactionProxyOperation() {
954 public void run(TransactionProxy transactionProxy) {
955 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
957 expectIncompleteBatchedModifications();
959 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
961 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
967 public void testMergeThrottlingWhenShardNotFound(){
968 dataStoreContextBuilder.shardBatchedModificationCount(1);
969 completeOperation(new TransactionProxyOperation() {
971 public void run(TransactionProxy transactionProxy) {
972 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
974 expectBatchedModifications(2);
976 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
978 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
984 public void testMergeCompletion(){
985 dataStoreContextBuilder.shardBatchedModificationCount(1);
986 completeOperation(new TransactionProxyOperation() {
988 public void run(TransactionProxy transactionProxy) {
989 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
991 expectBatchedModifications(2);
993 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
995 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1002 public void testDeleteThrottlingWhenShardFound(){
1004 throttleOperation(new TransactionProxyOperation() {
1006 public void run(TransactionProxy transactionProxy) {
1007 expectIncompleteBatchedModifications();
1009 transactionProxy.delete(TestModel.TEST_PATH);
1011 transactionProxy.delete(TestModel.TEST_PATH);
1018 public void testDeleteThrottlingWhenShardNotFound(){
1020 completeOperation(new TransactionProxyOperation() {
1022 public void run(TransactionProxy transactionProxy) {
1023 expectBatchedModifications(2);
1025 transactionProxy.delete(TestModel.TEST_PATH);
1027 transactionProxy.delete(TestModel.TEST_PATH);
1033 public void testDeleteCompletion(){
1034 dataStoreContextBuilder.shardBatchedModificationCount(1);
1035 completeOperation(new TransactionProxyOperation() {
1037 public void run(TransactionProxy transactionProxy) {
1038 expectBatchedModifications(2);
1040 transactionProxy.delete(TestModel.TEST_PATH);
1042 transactionProxy.delete(TestModel.TEST_PATH);
1049 public void testReadThrottlingWhenShardFound(){
1051 throttleOperation(new TransactionProxyOperation() {
1053 public void run(TransactionProxy transactionProxy) {
1054 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1055 any(ActorSelection.class), eqReadData());
1057 transactionProxy.read(TestModel.TEST_PATH);
1059 transactionProxy.read(TestModel.TEST_PATH);
1065 public void testReadThrottlingWhenShardNotFound(){
1067 completeOperation(new TransactionProxyOperation() {
1069 public void run(TransactionProxy transactionProxy) {
1070 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1071 any(ActorSelection.class), eqReadData());
1073 transactionProxy.read(TestModel.TEST_PATH);
1075 transactionProxy.read(TestModel.TEST_PATH);
1082 public void testReadCompletion(){
1083 completeOperation(new TransactionProxyOperation() {
1085 public void run(TransactionProxy transactionProxy) {
1086 NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1088 doReturn(readDataReply(nodeToRead)).when(mockActorContext).executeOperationAsync(
1089 any(ActorSelection.class), eqReadData());
1091 transactionProxy.read(TestModel.TEST_PATH);
1093 transactionProxy.read(TestModel.TEST_PATH);
1100 public void testExistsThrottlingWhenShardFound(){
1102 throttleOperation(new TransactionProxyOperation() {
1104 public void run(TransactionProxy transactionProxy) {
1105 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1106 any(ActorSelection.class), eqDataExists());
1108 transactionProxy.exists(TestModel.TEST_PATH);
1110 transactionProxy.exists(TestModel.TEST_PATH);
1116 public void testExistsThrottlingWhenShardNotFound(){
1118 completeOperation(new TransactionProxyOperation() {
1120 public void run(TransactionProxy transactionProxy) {
1121 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1122 any(ActorSelection.class), eqDataExists());
1124 transactionProxy.exists(TestModel.TEST_PATH);
1126 transactionProxy.exists(TestModel.TEST_PATH);
1133 public void testExistsCompletion(){
1134 completeOperation(new TransactionProxyOperation() {
1136 public void run(TransactionProxy transactionProxy) {
1137 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
1138 any(ActorSelection.class), eqDataExists());
1140 transactionProxy.exists(TestModel.TEST_PATH);
1142 transactionProxy.exists(TestModel.TEST_PATH);
1149 public void testReadyThrottling(){
1151 throttleOperation(new TransactionProxyOperation() {
1153 public void run(TransactionProxy transactionProxy) {
1154 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1156 expectBatchedModifications(1);
1158 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1159 any(ActorSelection.class), any(ReadyTransaction.class));
1161 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1163 transactionProxy.ready();
1169 public void testReadyThrottlingWithTwoTransactionContexts(){
1171 throttleOperation(new TransactionProxyOperation() {
1173 public void run(TransactionProxy transactionProxy) {
1174 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1175 NormalizedNode<?, ?> carsNode = ImmutableNodes.containerNode(CarsModel.BASE_QNAME);
1177 expectBatchedModifications(2);
1179 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1180 any(ActorSelection.class), any(ReadyTransaction.class));
1182 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1184 transactionProxy.write(TestModel.TEST_PATH, carsNode);
1186 transactionProxy.ready();
1191 private void testModificationOperationBatching(TransactionType type) throws Exception {
1192 int shardBatchedModificationCount = 3;
1193 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1195 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), type);
1197 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1199 expectReadyTransaction(actorRef);
1201 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1202 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1204 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1205 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1207 YangInstanceIdentifier writePath3 = TestModel.INNER_LIST_PATH;
1208 NormalizedNode<?, ?> writeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1210 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1211 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1213 YangInstanceIdentifier mergePath2 = TestModel.OUTER_LIST_PATH;
1214 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1216 YangInstanceIdentifier mergePath3 = TestModel.INNER_LIST_PATH;
1217 NormalizedNode<?, ?> mergeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1219 YangInstanceIdentifier deletePath1 = TestModel.TEST_PATH;
1220 YangInstanceIdentifier deletePath2 = TestModel.OUTER_LIST_PATH;
1222 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, type);
1224 transactionProxy.write(writePath1, writeNode1);
1225 transactionProxy.write(writePath2, writeNode2);
1226 transactionProxy.delete(deletePath1);
1227 transactionProxy.merge(mergePath1, mergeNode1);
1228 transactionProxy.merge(mergePath2, mergeNode2);
1229 transactionProxy.write(writePath3, writeNode3);
1230 transactionProxy.merge(mergePath3, mergeNode3);
1231 transactionProxy.delete(deletePath2);
1233 // This sends the last batch.
1234 transactionProxy.ready();
1236 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1237 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1239 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1240 new WriteModification(writePath2, writeNode2), new DeleteModification(deletePath1));
1242 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1243 new MergeModification(mergePath2, mergeNode2), new WriteModification(writePath3, writeNode3));
1245 boolean optimizedWriteOnly = type == WRITE_ONLY && dataStoreContextBuilder.build().isWriteOnlyTransactionOptimizationsEnabled();
1246 verifyBatchedModifications(batchedModifications.get(2), optimizedWriteOnly, new MergeModification(mergePath3, mergeNode3),
1247 new DeleteModification(deletePath2));
1249 if(optimizedWriteOnly) {
1250 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures(),
1251 BatchedModificationsReply.class, BatchedModificationsReply.class);
1253 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures(),
1254 BatchedModificationsReply.class, BatchedModificationsReply.class, BatchedModificationsReply.class);
1259 public void testReadWriteModificationOperationBatching() throws Throwable {
1260 testModificationOperationBatching(READ_WRITE);
1264 public void testWriteOnlyModificationOperationBatching() throws Throwable {
1265 testModificationOperationBatching(WRITE_ONLY);
1269 public void testOptimizedWriteOnlyModificationOperationBatching() throws Throwable {
1270 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
1271 testModificationOperationBatching(WRITE_ONLY);
1275 public void testModificationOperationBatchingWithInterleavedReads() throws Throwable {
1277 int shardBatchedModificationCount = 10;
1278 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1280 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
1282 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1284 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1285 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1287 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1288 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1290 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1291 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1293 YangInstanceIdentifier mergePath2 = TestModel.INNER_LIST_PATH;
1294 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1296 YangInstanceIdentifier deletePath = TestModel.OUTER_LIST_PATH;
1298 doReturn(readSerializedDataReply(writeNode2)).when(mockActorContext).executeOperationAsync(
1299 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1301 doReturn(readSerializedDataReply(mergeNode2)).when(mockActorContext).executeOperationAsync(
1302 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1304 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
1305 eq(actorSelection(actorRef)), eqSerializedDataExists());
1307 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
1309 transactionProxy.write(writePath1, writeNode1);
1310 transactionProxy.write(writePath2, writeNode2);
1312 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(writePath2).
1313 get(5, TimeUnit.SECONDS);
1315 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1316 assertEquals("Response NormalizedNode", writeNode2, readOptional.get());
1318 transactionProxy.merge(mergePath1, mergeNode1);
1319 transactionProxy.merge(mergePath2, mergeNode2);
1321 readOptional = transactionProxy.read(mergePath2).get(5, TimeUnit.SECONDS);
1323 transactionProxy.delete(deletePath);
1325 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
1326 assertEquals("Exists response", true, exists);
1328 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1329 assertEquals("Response NormalizedNode", mergeNode2, readOptional.get());
1331 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1332 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1334 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1335 new WriteModification(writePath2, writeNode2));
1337 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1338 new MergeModification(mergePath2, mergeNode2));
1340 verifyBatchedModifications(batchedModifications.get(2), false, new DeleteModification(deletePath));
1342 InOrder inOrder = Mockito.inOrder(mockActorContext);
1343 inOrder.verify(mockActorContext).executeOperationAsync(
1344 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1346 inOrder.verify(mockActorContext).executeOperationAsync(
1347 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1349 inOrder.verify(mockActorContext).executeOperationAsync(
1350 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1352 inOrder.verify(mockActorContext).executeOperationAsync(
1353 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1355 inOrder.verify(mockActorContext).executeOperationAsync(
1356 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1358 inOrder.verify(mockActorContext).executeOperationAsync(
1359 eq(actorSelection(actorRef)), eqSerializedDataExists());
1361 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures(),
1362 BatchedModificationsReply.class, BatchedModificationsReply.class, BatchedModificationsReply.class);
1366 public void testReadRoot() throws ReadFailedException, InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
1368 SchemaContext schemaContext = SchemaContextHelper.full();
1369 Configuration configuration = mock(Configuration.class);
1370 doReturn(configuration).when(mockActorContext).getConfiguration();
1371 doReturn(schemaContext).when(mockActorContext).getSchemaContext();
1372 doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames();
1374 NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1375 NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
1377 setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext));
1378 setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext));
1380 doReturn(memberName).when(mockActorContext).getCurrentMemberName();
1382 doReturn(10).when(mockActorContext).getTransactionOutstandingOperationLimit();
1384 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
1386 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
1388 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
1389 YangInstanceIdentifier.builder().build()).get(5, TimeUnit.SECONDS);
1391 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1393 NormalizedNode<?, ?> normalizedNode = readOptional.get();
1395 assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
1397 Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
1399 for(NormalizedNode<?,?> node : collection){
1400 assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
1403 assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
1404 NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null);
1406 assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME));
1408 assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
1409 NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
1411 assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME));
1415 private void setUpReadData(String shardName, NormalizedNode<?, ?> expectedNode) {
1416 ActorSystem actorSystem = getSystem();
1417 ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
1419 doReturn(getSystem().actorSelection(shardActorRef.path())).
1420 when(mockActorContext).actorSelection(shardActorRef.path().toString());
1422 doReturn(Futures.successful(getSystem().actorSelection(shardActorRef.path()))).
1423 when(mockActorContext).findPrimaryShardAsync(eq(shardName));
1425 doReturn(true).when(mockActorContext).isPathLocal(shardActorRef.path().toString());
1427 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
1429 doReturn(actorSystem.actorSelection(txActorRef.path())).
1430 when(mockActorContext).actorSelection(txActorRef.path().toString());
1432 doReturn(Futures.successful(createTransactionReply(txActorRef, DataStoreVersions.CURRENT_VERSION))).when(mockActorContext).
1433 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
1434 eqCreateTransaction(memberName, TransactionType.READ_ONLY));
1436 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
1437 eq(actorSelection(txActorRef)), eqSerializedReadData(YangInstanceIdentifier.builder().build()));