2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.anyString;
16 import static org.mockito.Matchers.eq;
17 import static org.mockito.Matchers.isA;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.doThrow;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22 import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_ONLY;
23 import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_WRITE;
24 import static org.opendaylight.controller.cluster.datastore.TransactionType.WRITE_ONLY;
25 import akka.actor.ActorRef;
26 import akka.actor.ActorSelection;
27 import akka.actor.ActorSystem;
28 import akka.actor.Props;
29 import akka.dispatch.Futures;
30 import akka.util.Timeout;
31 import com.google.common.base.Optional;
32 import com.google.common.collect.Sets;
33 import com.google.common.util.concurrent.CheckedFuture;
34 import com.google.common.util.concurrent.FutureCallback;
35 import com.google.common.util.concurrent.Uninterruptibles;
36 import java.util.Collection;
37 import java.util.List;
38 import java.util.concurrent.CountDownLatch;
39 import java.util.concurrent.ExecutionException;
40 import java.util.concurrent.TimeUnit;
41 import java.util.concurrent.atomic.AtomicReference;
42 import org.junit.Assert;
43 import org.junit.Test;
44 import org.mockito.InOrder;
45 import org.mockito.Mockito;
46 import org.opendaylight.controller.cluster.access.concepts.MemberName;
47 import org.opendaylight.controller.cluster.datastore.config.Configuration;
48 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
49 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
50 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
51 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
52 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
53 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
54 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
55 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
56 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
57 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
58 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
59 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
60 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
61 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest;
62 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
63 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
64 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
65 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
66 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
67 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
68 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
69 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
70 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
71 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
72 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
73 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
74 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
75 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
76 import scala.concurrent.Promise;
78 @SuppressWarnings("resource")
79 public class TransactionProxyTest extends AbstractTransactionProxyTest {
81 @SuppressWarnings("serial")
82 static class TestException extends RuntimeException {
85 static interface Invoker {
86 CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception;
90 public void testRead() throws Exception {
91 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
93 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
95 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
96 eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class));
98 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
99 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
101 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
103 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
105 doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
106 eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class));
108 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
110 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
112 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
115 @Test(expected = ReadFailedException.class)
116 public void testReadWithInvalidReplyMessageType() throws Exception {
117 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
119 doReturn(Futures.successful(new Object())).when(mockActorContext).
120 executeOperationAsync(eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class));
122 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
124 transactionProxy.read(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
127 @Test(expected = TestException.class)
128 public void testReadWithAsyncRemoteOperatonFailure() throws Throwable {
129 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
131 doReturn(Futures.failed(new TestException())).when(mockActorContext).
132 executeOperationAsync(eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class));
134 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
136 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
139 private void testExceptionOnInitialCreateTransaction(Exception exToThrow, Invoker invoker)
141 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
143 if (exToThrow instanceof PrimaryNotFoundException) {
144 doReturn(Futures.failed(exToThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
146 doReturn(primaryShardInfoReply(getSystem(), actorRef)).
147 when(mockActorContext).findPrimaryShardAsync(anyString());
150 doReturn(Futures.failed(exToThrow)).when(mockActorContext).executeOperationAsync(
151 any(ActorSelection.class), any(), any(Timeout.class));
153 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
155 propagateReadFailedExceptionCause(invoker.invoke(transactionProxy));
158 private void testReadWithExceptionOnInitialCreateTransaction(Exception exToThrow) throws Throwable {
159 testExceptionOnInitialCreateTransaction(exToThrow, new Invoker() {
161 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
162 return proxy.read(TestModel.TEST_PATH);
167 @Test(expected = PrimaryNotFoundException.class)
168 public void testReadWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
169 testReadWithExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"));
172 @Test(expected = TimeoutException.class)
173 public void testReadWhenATimeoutExceptionIsThrown() throws Throwable {
174 testReadWithExceptionOnInitialCreateTransaction(new TimeoutException("test",
175 new Exception("reason")));
178 @Test(expected = TestException.class)
179 public void testReadWhenAnyOtherExceptionIsThrown() throws Throwable {
180 testReadWithExceptionOnInitialCreateTransaction(new TestException());
184 public void testReadWithPriorRecordingOperationSuccessful() throws Throwable {
185 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
187 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
189 expectBatchedModifications(actorRef, 1);
191 doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
192 eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class));
194 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
196 transactionProxy.write(TestModel.TEST_PATH, expectedNode);
198 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
199 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
201 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
202 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
204 InOrder inOrder = Mockito.inOrder(mockActorContext);
205 inOrder.verify(mockActorContext).executeOperationAsync(
206 eq(actorSelection(actorRef)), isA(BatchedModifications.class), any(Timeout.class));
208 inOrder.verify(mockActorContext).executeOperationAsync(
209 eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class));
212 @Test(expected=IllegalStateException.class)
213 public void testReadPreConditionCheck() {
214 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
215 transactionProxy.read(TestModel.TEST_PATH);
218 @Test(expected=IllegalArgumentException.class)
219 public void testInvalidCreateTransactionReply() throws Throwable {
220 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
222 doReturn(getSystem().actorSelection(actorRef.path())).when(mockActorContext).
223 actorSelection(actorRef.path().toString());
225 doReturn(primaryShardInfoReply(getSystem(), actorRef)).
226 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
228 doReturn(Futures.successful(new Object())).when(mockActorContext).executeOperationAsync(
229 eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_ONLY),
232 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
234 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
238 public void testExists() throws Exception {
239 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
241 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
243 doReturn(dataExistsReply(false)).when(mockActorContext).executeOperationAsync(
244 eq(actorSelection(actorRef)), eqDataExists(), any(Timeout.class));
246 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
248 assertEquals("Exists response", false, exists);
250 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
251 eq(actorSelection(actorRef)), eqDataExists(), any(Timeout.class));
253 exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
255 assertEquals("Exists response", true, exists);
258 @Test(expected = PrimaryNotFoundException.class)
259 public void testExistsWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
260 testExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"), new Invoker() {
262 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
263 return proxy.exists(TestModel.TEST_PATH);
268 @Test(expected = ReadFailedException.class)
269 public void testExistsWithInvalidReplyMessageType() throws Exception {
270 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
272 doReturn(Futures.successful(new Object())).when(mockActorContext).
273 executeOperationAsync(eq(actorSelection(actorRef)), eqDataExists(), any(Timeout.class));
275 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
277 transactionProxy.exists(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
280 @Test(expected = TestException.class)
281 public void testExistsWithAsyncRemoteOperatonFailure() throws Throwable {
282 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
284 doReturn(Futures.failed(new TestException())).when(mockActorContext).
285 executeOperationAsync(eq(actorSelection(actorRef)), eqDataExists(), any(Timeout.class));
287 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
289 propagateReadFailedExceptionCause(transactionProxy.exists(TestModel.TEST_PATH));
293 public void testExistsWithPriorRecordingOperationSuccessful() throws Throwable {
294 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
296 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
298 expectBatchedModifications(actorRef, 1);
300 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
301 eq(actorSelection(actorRef)), eqDataExists(), any(Timeout.class));
303 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
305 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
307 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
309 assertEquals("Exists response", true, exists);
311 InOrder inOrder = Mockito.inOrder(mockActorContext);
312 inOrder.verify(mockActorContext).executeOperationAsync(
313 eq(actorSelection(actorRef)), isA(BatchedModifications.class), any(Timeout.class));
315 inOrder.verify(mockActorContext).executeOperationAsync(
316 eq(actorSelection(actorRef)), eqDataExists(), any(Timeout.class));
319 @Test(expected=IllegalStateException.class)
320 public void testExistsPreConditionCheck() {
321 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
322 transactionProxy.exists(TestModel.TEST_PATH);
326 public void testWrite() throws Exception {
327 dataStoreContextBuilder.shardBatchedModificationCount(1);
328 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
330 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
332 expectBatchedModifications(actorRef, 1);
334 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
336 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
338 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), false);
342 public void testWriteAfterAsyncRead() throws Throwable {
343 ActorRef actorRef = setupActorContextWithoutInitialCreateTransaction(getSystem(), DefaultShardStrategy.DEFAULT_SHARD);
345 Promise<Object> createTxPromise = akka.dispatch.Futures.promise();
346 doReturn(createTxPromise).when(mockActorContext).executeOperationAsync(
347 eq(getSystem().actorSelection(actorRef.path())),
348 eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
350 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
351 eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class));
353 expectBatchedModificationsReady(actorRef);
355 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
357 final TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
359 final CountDownLatch readComplete = new CountDownLatch(1);
360 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
361 com.google.common.util.concurrent.Futures.addCallback(transactionProxy.read(TestModel.TEST_PATH),
362 new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
364 public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
366 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
367 } catch (Exception e) {
370 readComplete.countDown();
375 public void onFailure(Throwable t) {
377 readComplete.countDown();
381 createTxPromise.success(createTransactionReply(actorRef, DataStoreVersions.CURRENT_VERSION));
383 Uninterruptibles.awaitUninterruptibly(readComplete, 5, TimeUnit.SECONDS);
385 if(caughtEx.get() != null) {
386 throw caughtEx.get();
389 // This sends the batched modification.
390 transactionProxy.ready();
392 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), true);
395 @Test(expected=IllegalStateException.class)
396 public void testWritePreConditionCheck() {
397 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
398 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
401 @Test(expected=IllegalStateException.class)
402 public void testWriteAfterReadyPreConditionCheck() {
403 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
405 transactionProxy.ready();
407 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
411 public void testMerge() throws Exception {
412 dataStoreContextBuilder.shardBatchedModificationCount(1);
413 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
415 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
417 expectBatchedModifications(actorRef, 1);
419 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
421 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
423 verifyOneBatchedModification(actorRef, new MergeModification(TestModel.TEST_PATH, nodeToWrite), false);
427 public void testDelete() throws Exception {
428 dataStoreContextBuilder.shardBatchedModificationCount(1);
429 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
431 expectBatchedModifications(actorRef, 1);
433 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
435 transactionProxy.delete(TestModel.TEST_PATH);
437 verifyOneBatchedModification(actorRef, new DeleteModification(TestModel.TEST_PATH), false);
441 public void testReadWrite() throws Exception {
442 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
444 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
446 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
447 eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class));
449 expectBatchedModifications(actorRef, 1);
451 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
453 transactionProxy.read(TestModel.TEST_PATH);
455 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
457 transactionProxy.read(TestModel.TEST_PATH);
459 transactionProxy.read(TestModel.TEST_PATH);
461 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
462 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
464 verifyBatchedModifications(batchedModifications.get(0), false,
465 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
469 public void testReadyWithReadWrite() throws Exception {
470 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
472 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
474 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
475 eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class));
477 expectBatchedModificationsReady(actorRef, true);
479 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
481 transactionProxy.read(TestModel.TEST_PATH);
483 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
485 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
487 assertTrue(ready instanceof SingleCommitCohortProxy);
489 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
491 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
492 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
494 verifyBatchedModifications(batchedModifications.get(0), true, true,
495 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
497 assertEquals("getTotalMessageCount", 1, batchedModifications.get(0).getTotalMessagesSent());
501 public void testReadyWithNoModifications() throws Exception {
502 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
504 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
505 eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class));
507 expectBatchedModificationsReady(actorRef, true);
509 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
511 transactionProxy.read(TestModel.TEST_PATH);
513 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
515 assertTrue(ready instanceof SingleCommitCohortProxy);
517 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
519 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
520 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
522 verifyBatchedModifications(batchedModifications.get(0), true, true);
526 public void testReadyWithMultipleShardWrites() throws Exception {
527 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
529 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
531 expectBatchedModificationsReady(actorRef1);
532 expectBatchedModificationsReady(actorRef2);
534 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
536 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
537 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
539 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
541 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
543 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef1),
544 actorSelection(actorRef2));
548 public void testReadyWithWriteOnlyAndLastBatchPending() throws Exception {
549 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
551 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
553 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
555 expectBatchedModificationsReady(actorRef, true);
557 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
559 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
561 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
563 assertTrue(ready instanceof SingleCommitCohortProxy);
565 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
567 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
568 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
570 verifyBatchedModifications(batchedModifications.get(0), true, true,
571 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
575 public void testReadyWithWriteOnlyAndLastBatchEmpty() throws Exception {
576 dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
577 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
579 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
581 expectBatchedModificationsReady(actorRef, true);
583 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
585 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
587 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
589 assertTrue(ready instanceof SingleCommitCohortProxy);
591 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
593 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
594 assertEquals("Captured BatchedModifications count", 2, batchedModifications.size());
596 verifyBatchedModifications(batchedModifications.get(0), false,
597 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
599 verifyBatchedModifications(batchedModifications.get(1), true, true);
603 public void testReadyWithReplyFailure() throws Exception {
604 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
606 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
608 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
610 expectFailedBatchedModifications(actorRef);
612 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
614 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
616 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
618 assertTrue(ready instanceof SingleCommitCohortProxy);
620 verifyCohortFutures((SingleCommitCohortProxy)ready, TestException.class);
624 public void testReadyWithDebugContextEnabled() throws Exception {
625 dataStoreContextBuilder.transactionDebugContextEnabled(true);
627 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
629 expectBatchedModificationsReady(actorRef, true);
631 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
633 transactionProxy.merge(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
635 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
637 assertTrue(ready instanceof DebugThreePhaseCommitCohort);
639 verifyCohortFutures((DebugThreePhaseCommitCohort)ready, new CommitTransactionReply().toSerializable());
643 public void testReadyWithLocalTransaction() throws Exception {
644 ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
646 doReturn(getSystem().actorSelection(shardActorRef.path())).
647 when(mockActorContext).actorSelection(shardActorRef.path().toString());
649 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef, createDataTree()))).
650 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
652 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
654 expectReadyLocalTransaction(shardActorRef, true);
656 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
657 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
659 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
660 assertTrue(ready instanceof SingleCommitCohortProxy);
661 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
665 public void testReadyWithLocalTransactionWithFailure() throws Exception {
666 ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
668 doReturn(getSystem().actorSelection(shardActorRef.path())).
669 when(mockActorContext).actorSelection(shardActorRef.path().toString());
671 DataTree mockDataTree = createDataTree();
672 DataTreeModification mockModification = mockDataTree.takeSnapshot().newModification();
673 doThrow(new RuntimeException("mock")).when(mockModification).ready();
675 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef, mockDataTree))).
676 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
678 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
680 expectReadyLocalTransaction(shardActorRef, true);
682 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
683 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
685 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
686 assertTrue(ready instanceof SingleCommitCohortProxy);
687 verifyCohortFutures((SingleCommitCohortProxy)ready, RuntimeException.class);
690 private void testWriteOnlyTxWithFindPrimaryShardFailure(Exception toThrow) throws Exception {
691 doReturn(Futures.failed(toThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
693 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
695 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
697 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
699 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
701 transactionProxy.delete(TestModel.TEST_PATH);
703 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
705 assertTrue(ready instanceof SingleCommitCohortProxy);
707 verifyCohortFutures((SingleCommitCohortProxy)ready, toThrow.getClass());
711 public void testWriteOnlyTxWithPrimaryNotFoundException() throws Exception {
712 testWriteOnlyTxWithFindPrimaryShardFailure(new PrimaryNotFoundException("mock"));
716 public void testWriteOnlyTxWithNotInitializedException() throws Exception {
717 testWriteOnlyTxWithFindPrimaryShardFailure(new NotInitializedException("mock"));
721 public void testWriteOnlyTxWithNoShardLeaderException() throws Exception {
722 testWriteOnlyTxWithFindPrimaryShardFailure(new NoShardLeaderException("mock"));
726 public void testReadyWithInvalidReplyMessageType() throws Exception {
727 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
728 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
730 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
732 doReturn(Futures.successful(new Object())).when(mockActorContext).
733 executeOperationAsync(eq(actorSelection(actorRef1)), isA(BatchedModifications.class),
736 expectBatchedModificationsReady(actorRef2);
738 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
740 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
741 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
743 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
745 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
747 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef2),
748 IllegalArgumentException.class);
752 public void testGetIdentifier() {
753 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
754 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
756 Object id = transactionProxy.getIdentifier();
757 assertNotNull("getIdentifier returned null", id);
758 assertTrue("Invalid identifier: " + id, id.toString().contains(memberName));
762 public void testClose() throws Exception{
763 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
765 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
766 eq(actorSelection(actorRef)), eqReadData(), any(Timeout.class));
768 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
770 transactionProxy.read(TestModel.TEST_PATH);
772 transactionProxy.close();
774 verify(mockActorContext).sendOperationAsync(
775 eq(actorSelection(actorRef)), isA(CloseTransaction.class));
778 private static interface TransactionProxyOperation {
779 void run(TransactionProxy transactionProxy);
782 private void throttleOperation(TransactionProxyOperation operation) {
783 throttleOperation(operation, 1, true);
786 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound){
787 throttleOperation(operation, outstandingOpsLimit, shardFound, TimeUnit.MILLISECONDS.toNanos(
788 mockActorContext.getDatastoreContext().getOperationTimeoutInMillis()));
791 private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef){
792 return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), DataStoreVersions.CURRENT_VERSION);
795 private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef, DataTree dataTree){
796 return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), DataStoreVersions.CURRENT_VERSION,
801 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound, long expectedCompletionTime){
802 ActorSystem actorSystem = getSystem();
803 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
805 // Note that we setting batchedModificationCount to one less than what we need because in TransactionProxy
806 // we now allow one extra permit to be allowed for ready
807 doReturn(dataStoreContextBuilder.operationTimeoutInSeconds(2).
808 shardBatchedModificationCount(outstandingOpsLimit-1).build()).when(mockActorContext).getDatastoreContext();
810 doReturn(actorSystem.actorSelection(shardActorRef.path())).
811 when(mockActorContext).actorSelection(shardActorRef.path().toString());
814 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
815 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
816 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
817 when(mockActorContext).findPrimaryShardAsync(eq("cars"));
820 doReturn(Futures.failed(new Exception("not found")))
821 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
824 String actorPath = "akka.tcp://system@127.0.0.1:2550/user/tx-actor";
826 doReturn(incompleteFuture()).when(mockActorContext).
827 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
828 eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
830 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
832 long start = System.nanoTime();
834 operation.run(transactionProxy);
836 long end = System.nanoTime();
838 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
839 expectedCompletionTime, (end-start)),
840 ((end - start) > expectedCompletionTime) && ((end - start) < expectedCompletionTime*2));
844 private void completeOperation(TransactionProxyOperation operation){
845 completeOperation(operation, true);
848 private void completeOperation(TransactionProxyOperation operation, boolean shardFound){
849 ActorSystem actorSystem = getSystem();
850 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
852 doReturn(actorSystem.actorSelection(shardActorRef.path())).
853 when(mockActorContext).actorSelection(shardActorRef.path().toString());
856 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
857 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
859 doReturn(Futures.failed(new PrimaryNotFoundException("test")))
860 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
863 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
864 String actorPath = txActorRef.path().toString();
865 CreateTransactionReply createTransactionReply = new CreateTransactionReply(actorPath, nextTransactionId(),
866 DataStoreVersions.CURRENT_VERSION);
868 doReturn(actorSystem.actorSelection(actorPath)).when(mockActorContext).actorSelection(actorPath);
870 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
871 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
872 eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
874 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
876 long start = System.nanoTime();
878 operation.run(transactionProxy);
880 long end = System.nanoTime();
882 long expected = TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis());
883 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
884 expected, (end-start)), (end - start) <= expected);
887 private void completeOperationLocal(TransactionProxyOperation operation, DataTree dataTree){
888 ActorSystem actorSystem = getSystem();
889 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
891 doReturn(actorSystem.actorSelection(shardActorRef.path())).
892 when(mockActorContext).actorSelection(shardActorRef.path().toString());
894 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef, dataTree))).
895 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
897 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
899 long start = System.nanoTime();
901 operation.run(transactionProxy);
903 long end = System.nanoTime();
905 long expected = TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis());
906 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
907 expected, (end-start)), (end - start) <= expected);
910 private static DataTree createDataTree(){
911 DataTree dataTree = mock(DataTree.class);
912 DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
913 DataTreeModification dataTreeModification = mock(DataTreeModification.class);
915 doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
916 doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
921 private static DataTree createDataTree(NormalizedNode<?, ?> readResponse){
922 DataTree dataTree = mock(DataTree.class);
923 DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
924 DataTreeModification dataTreeModification = mock(DataTreeModification.class);
926 doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
927 doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
928 doReturn(Optional.of(readResponse)).when(dataTreeModification).readNode(any(YangInstanceIdentifier.class));
935 public void testWriteCompletionForLocalShard(){
936 completeOperationLocal(new TransactionProxyOperation() {
938 public void run(TransactionProxy transactionProxy) {
939 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
941 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
943 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
946 }, createDataTree());
950 public void testWriteThrottlingWhenShardFound(){
951 throttleOperation(new TransactionProxyOperation() {
953 public void run(TransactionProxy transactionProxy) {
954 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
956 expectIncompleteBatchedModifications();
958 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
960 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
966 public void testWriteThrottlingWhenShardNotFound(){
967 // Confirm that there is no throttling when the Shard is not found
968 completeOperation(new TransactionProxyOperation() {
970 public void run(TransactionProxy transactionProxy) {
971 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
973 expectBatchedModifications(2);
975 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
977 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
985 public void testWriteCompletion(){
986 completeOperation(new TransactionProxyOperation() {
988 public void run(TransactionProxy transactionProxy) {
989 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
991 expectBatchedModifications(2);
993 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
995 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1001 public void testMergeThrottlingWhenShardFound(){
1002 throttleOperation(new TransactionProxyOperation() {
1004 public void run(TransactionProxy transactionProxy) {
1005 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1007 expectIncompleteBatchedModifications();
1009 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1011 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1017 public void testMergeThrottlingWhenShardNotFound(){
1018 completeOperation(new TransactionProxyOperation() {
1020 public void run(TransactionProxy transactionProxy) {
1021 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1023 expectBatchedModifications(2);
1025 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1027 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1033 public void testMergeCompletion(){
1034 completeOperation(new TransactionProxyOperation() {
1036 public void run(TransactionProxy transactionProxy) {
1037 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1039 expectBatchedModifications(2);
1041 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1043 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1050 public void testMergeCompletionForLocalShard(){
1051 completeOperationLocal(new TransactionProxyOperation() {
1053 public void run(TransactionProxy transactionProxy) {
1054 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1056 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1058 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1061 }, createDataTree());
1066 public void testDeleteThrottlingWhenShardFound(){
1068 throttleOperation(new TransactionProxyOperation() {
1070 public void run(TransactionProxy transactionProxy) {
1071 expectIncompleteBatchedModifications();
1073 transactionProxy.delete(TestModel.TEST_PATH);
1075 transactionProxy.delete(TestModel.TEST_PATH);
1082 public void testDeleteThrottlingWhenShardNotFound(){
1084 completeOperation(new TransactionProxyOperation() {
1086 public void run(TransactionProxy transactionProxy) {
1087 expectBatchedModifications(2);
1089 transactionProxy.delete(TestModel.TEST_PATH);
1091 transactionProxy.delete(TestModel.TEST_PATH);
1097 public void testDeleteCompletionForLocalShard(){
1098 completeOperationLocal(new TransactionProxyOperation() {
1100 public void run(TransactionProxy transactionProxy) {
1102 transactionProxy.delete(TestModel.TEST_PATH);
1104 transactionProxy.delete(TestModel.TEST_PATH);
1106 }, createDataTree());
1111 public void testDeleteCompletion(){
1112 completeOperation(new TransactionProxyOperation() {
1114 public void run(TransactionProxy transactionProxy) {
1115 expectBatchedModifications(2);
1117 transactionProxy.delete(TestModel.TEST_PATH);
1119 transactionProxy.delete(TestModel.TEST_PATH);
1126 public void testReadThrottlingWhenShardFound(){
1128 throttleOperation(new TransactionProxyOperation() {
1130 public void run(TransactionProxy transactionProxy) {
1131 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1132 any(ActorSelection.class), eqReadData());
1134 transactionProxy.read(TestModel.TEST_PATH);
1136 transactionProxy.read(TestModel.TEST_PATH);
1142 public void testReadThrottlingWhenShardNotFound(){
1144 completeOperation(new TransactionProxyOperation() {
1146 public void run(TransactionProxy transactionProxy) {
1147 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1148 any(ActorSelection.class), eqReadData());
1150 transactionProxy.read(TestModel.TEST_PATH);
1152 transactionProxy.read(TestModel.TEST_PATH);
1159 public void testReadCompletion(){
1160 completeOperation(new TransactionProxyOperation() {
1162 public void run(TransactionProxy transactionProxy) {
1163 NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1165 doReturn(readDataReply(nodeToRead)).when(mockActorContext).executeOperationAsync(
1166 any(ActorSelection.class), eqReadData(), any(Timeout.class));
1168 transactionProxy.read(TestModel.TEST_PATH);
1170 transactionProxy.read(TestModel.TEST_PATH);
1177 public void testReadCompletionForLocalShard(){
1178 final NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1179 completeOperationLocal(new TransactionProxyOperation() {
1181 public void run(TransactionProxy transactionProxy) {
1182 transactionProxy.read(TestModel.TEST_PATH);
1184 transactionProxy.read(TestModel.TEST_PATH);
1186 }, createDataTree(nodeToRead));
1191 public void testReadCompletionForLocalShardWhenExceptionOccurs(){
1192 completeOperationLocal(new TransactionProxyOperation() {
1194 public void run(TransactionProxy transactionProxy) {
1195 transactionProxy.read(TestModel.TEST_PATH);
1197 transactionProxy.read(TestModel.TEST_PATH);
1199 }, createDataTree());
1204 public void testExistsThrottlingWhenShardFound(){
1206 throttleOperation(new TransactionProxyOperation() {
1208 public void run(TransactionProxy transactionProxy) {
1209 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1210 any(ActorSelection.class), eqDataExists());
1212 transactionProxy.exists(TestModel.TEST_PATH);
1214 transactionProxy.exists(TestModel.TEST_PATH);
1220 public void testExistsThrottlingWhenShardNotFound(){
1222 completeOperation(new TransactionProxyOperation() {
1224 public void run(TransactionProxy transactionProxy) {
1225 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1226 any(ActorSelection.class), eqDataExists());
1228 transactionProxy.exists(TestModel.TEST_PATH);
1230 transactionProxy.exists(TestModel.TEST_PATH);
1237 public void testExistsCompletion(){
1238 completeOperation(new TransactionProxyOperation() {
1240 public void run(TransactionProxy transactionProxy) {
1241 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
1242 any(ActorSelection.class), eqDataExists(), any(Timeout.class));
1244 transactionProxy.exists(TestModel.TEST_PATH);
1246 transactionProxy.exists(TestModel.TEST_PATH);
1253 public void testExistsCompletionForLocalShard(){
1254 final NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1255 completeOperationLocal(new TransactionProxyOperation() {
1257 public void run(TransactionProxy transactionProxy) {
1258 transactionProxy.exists(TestModel.TEST_PATH);
1260 transactionProxy.exists(TestModel.TEST_PATH);
1262 }, createDataTree(nodeToRead));
1267 public void testExistsCompletionForLocalShardWhenExceptionOccurs(){
1268 completeOperationLocal(new TransactionProxyOperation() {
1270 public void run(TransactionProxy transactionProxy) {
1271 transactionProxy.exists(TestModel.TEST_PATH);
1273 transactionProxy.exists(TestModel.TEST_PATH);
1275 }, createDataTree());
1279 public void testReadyThrottling(){
1281 throttleOperation(new TransactionProxyOperation() {
1283 public void run(TransactionProxy transactionProxy) {
1284 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1286 expectBatchedModifications(1);
1288 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1290 transactionProxy.ready();
1296 public void testReadyThrottlingWithTwoTransactionContexts(){
1297 throttleOperation(new TransactionProxyOperation() {
1299 public void run(TransactionProxy transactionProxy) {
1300 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1301 NormalizedNode<?, ?> carsNode = ImmutableNodes.containerNode(CarsModel.BASE_QNAME);
1303 expectBatchedModifications(2);
1305 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1307 // Trying to write to Cars will cause another transaction context to get created
1308 transactionProxy.write(CarsModel.BASE_PATH, carsNode);
1310 // Now ready should block for both transaction contexts
1311 transactionProxy.ready();
1313 }, 1, true, TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis()) * 2);
1316 private void testModificationOperationBatching(TransactionType type) throws Exception {
1317 int shardBatchedModificationCount = 3;
1318 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1320 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), type);
1322 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1324 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1325 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1327 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1328 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1330 YangInstanceIdentifier writePath3 = TestModel.INNER_LIST_PATH;
1331 NormalizedNode<?, ?> writeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1333 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1334 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1336 YangInstanceIdentifier mergePath2 = TestModel.OUTER_LIST_PATH;
1337 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1339 YangInstanceIdentifier mergePath3 = TestModel.INNER_LIST_PATH;
1340 NormalizedNode<?, ?> mergeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1342 YangInstanceIdentifier deletePath1 = TestModel.TEST_PATH;
1343 YangInstanceIdentifier deletePath2 = TestModel.OUTER_LIST_PATH;
1345 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, type);
1347 transactionProxy.write(writePath1, writeNode1);
1348 transactionProxy.write(writePath2, writeNode2);
1349 transactionProxy.delete(deletePath1);
1350 transactionProxy.merge(mergePath1, mergeNode1);
1351 transactionProxy.merge(mergePath2, mergeNode2);
1352 transactionProxy.write(writePath3, writeNode3);
1353 transactionProxy.merge(mergePath3, mergeNode3);
1354 transactionProxy.delete(deletePath2);
1356 // This sends the last batch.
1357 transactionProxy.ready();
1359 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1360 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1362 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1363 new WriteModification(writePath2, writeNode2), new DeleteModification(deletePath1));
1365 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1366 new MergeModification(mergePath2, mergeNode2), new WriteModification(writePath3, writeNode3));
1368 verifyBatchedModifications(batchedModifications.get(2), true, true,
1369 new MergeModification(mergePath3, mergeNode3), new DeleteModification(deletePath2));
1371 assertEquals("getTotalMessageCount", 3, batchedModifications.get(2).getTotalMessagesSent());
1375 public void testReadWriteModificationOperationBatching() throws Throwable {
1376 testModificationOperationBatching(READ_WRITE);
1380 public void testWriteOnlyModificationOperationBatching() throws Throwable {
1381 testModificationOperationBatching(WRITE_ONLY);
1385 public void testOptimizedWriteOnlyModificationOperationBatching() throws Throwable {
1386 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
1387 testModificationOperationBatching(WRITE_ONLY);
1391 public void testModificationOperationBatchingWithInterleavedReads() throws Throwable {
1393 int shardBatchedModificationCount = 10;
1394 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1396 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
1398 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1400 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1401 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1403 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1404 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1406 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1407 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1409 YangInstanceIdentifier mergePath2 = TestModel.INNER_LIST_PATH;
1410 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1412 YangInstanceIdentifier deletePath = TestModel.OUTER_LIST_PATH;
1414 doReturn(readDataReply(writeNode2)).when(mockActorContext).executeOperationAsync(
1415 eq(actorSelection(actorRef)), eqReadData(writePath2), any(Timeout.class));
1417 doReturn(readDataReply(mergeNode2)).when(mockActorContext).executeOperationAsync(
1418 eq(actorSelection(actorRef)), eqReadData(mergePath2), any(Timeout.class));
1420 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
1421 eq(actorSelection(actorRef)), eqDataExists(), any(Timeout.class));
1423 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
1425 transactionProxy.write(writePath1, writeNode1);
1426 transactionProxy.write(writePath2, writeNode2);
1428 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(writePath2).
1429 get(5, TimeUnit.SECONDS);
1431 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1432 assertEquals("Response NormalizedNode", writeNode2, readOptional.get());
1434 transactionProxy.merge(mergePath1, mergeNode1);
1435 transactionProxy.merge(mergePath2, mergeNode2);
1437 readOptional = transactionProxy.read(mergePath2).get(5, TimeUnit.SECONDS);
1439 transactionProxy.delete(deletePath);
1441 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
1442 assertEquals("Exists response", true, exists);
1444 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1445 assertEquals("Response NormalizedNode", mergeNode2, readOptional.get());
1447 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1448 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1450 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1451 new WriteModification(writePath2, writeNode2));
1453 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1454 new MergeModification(mergePath2, mergeNode2));
1456 verifyBatchedModifications(batchedModifications.get(2), false, new DeleteModification(deletePath));
1458 InOrder inOrder = Mockito.inOrder(mockActorContext);
1459 inOrder.verify(mockActorContext).executeOperationAsync(
1460 eq(actorSelection(actorRef)), isA(BatchedModifications.class), any(Timeout.class));
1462 inOrder.verify(mockActorContext).executeOperationAsync(
1463 eq(actorSelection(actorRef)), eqReadData(writePath2), any(Timeout.class));
1465 inOrder.verify(mockActorContext).executeOperationAsync(
1466 eq(actorSelection(actorRef)), isA(BatchedModifications.class), any(Timeout.class));
1468 inOrder.verify(mockActorContext).executeOperationAsync(
1469 eq(actorSelection(actorRef)), eqReadData(mergePath2), any(Timeout.class));
1471 inOrder.verify(mockActorContext).executeOperationAsync(
1472 eq(actorSelection(actorRef)), isA(BatchedModifications.class), any(Timeout.class));
1474 inOrder.verify(mockActorContext).executeOperationAsync(
1475 eq(actorSelection(actorRef)), eqDataExists(), any(Timeout.class));
1479 public void testReadRoot() throws ReadFailedException, InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
1481 SchemaContext schemaContext = SchemaContextHelper.full();
1482 Configuration configuration = mock(Configuration.class);
1483 doReturn(configuration).when(mockActorContext).getConfiguration();
1484 doReturn(schemaContext).when(mockActorContext).getSchemaContext();
1485 doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames();
1487 NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1488 NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
1490 setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext));
1491 setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext));
1493 doReturn(MemberName.forName(memberName)).when(mockActorContext).getCurrentMemberName();
1495 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
1497 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
1499 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
1500 YangInstanceIdentifier.EMPTY).get(5, TimeUnit.SECONDS);
1502 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1504 NormalizedNode<?, ?> normalizedNode = readOptional.get();
1506 assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
1508 @SuppressWarnings("unchecked")
1509 Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
1511 for(NormalizedNode<?,?> node : collection){
1512 assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
1515 assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
1516 NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null);
1518 assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME));
1520 assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
1521 NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
1523 assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME));
1527 private void setUpReadData(String shardName, NormalizedNode<?, ?> expectedNode) {
1528 ActorSystem actorSystem = getSystem();
1529 ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
1531 doReturn(getSystem().actorSelection(shardActorRef.path())).
1532 when(mockActorContext).actorSelection(shardActorRef.path().toString());
1534 doReturn(primaryShardInfoReply(getSystem(), shardActorRef)).
1535 when(mockActorContext).findPrimaryShardAsync(eq(shardName));
1537 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
1539 doReturn(actorSystem.actorSelection(txActorRef.path())).
1540 when(mockActorContext).actorSelection(txActorRef.path().toString());
1542 doReturn(Futures.successful(createTransactionReply(txActorRef, DataStoreVersions.CURRENT_VERSION))).when(mockActorContext).
1543 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
1544 eqCreateTransaction(memberName, TransactionType.READ_ONLY), any(Timeout.class));
1546 doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
1547 eq(actorSelection(txActorRef)), eqReadData(YangInstanceIdentifier.EMPTY), any(Timeout.class));