1 package org.opendaylight.controller.cluster.datastore;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.Matchers.any;
7 import static org.mockito.Matchers.anyString;
8 import static org.mockito.Matchers.eq;
9 import static org.mockito.Matchers.isA;
10 import static org.mockito.Mockito.doReturn;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.never;
13 import static org.mockito.Mockito.verify;
14 import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_ONLY;
15 import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_WRITE;
16 import static org.opendaylight.controller.cluster.datastore.TransactionType.WRITE_ONLY;
17 import akka.actor.ActorRef;
18 import akka.actor.ActorSelection;
19 import akka.actor.ActorSystem;
20 import akka.actor.Props;
21 import akka.dispatch.Futures;
22 import akka.util.Timeout;
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.config.Configuration;
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.CloseTransaction;
45 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
46 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
47 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
48 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
49 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
50 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
51 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
52 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
53 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest;
54 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
55 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
56 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
57 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
58 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
59 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
60 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
61 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
62 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
64 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
65 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
66 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
67 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
68 import scala.concurrent.Promise;
70 @SuppressWarnings("resource")
71 public class TransactionProxyTest extends AbstractTransactionProxyTest {
73 @SuppressWarnings("serial")
74 static class TestException extends RuntimeException {
77 static interface Invoker {
78 CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception;
82 public void testRead() throws Exception {
83 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
85 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
87 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
88 eq(actorSelection(actorRef)), eqSerializedReadData());
90 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
91 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
93 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
95 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
97 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
98 eq(actorSelection(actorRef)), eqSerializedReadData());
100 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
102 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
104 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
107 @Test(expected = ReadFailedException.class)
108 public void testReadWithInvalidReplyMessageType() throws Exception {
109 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
111 doReturn(Futures.successful(new Object())).when(mockActorContext).
112 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
114 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
116 transactionProxy.read(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
119 @Test(expected = TestException.class)
120 public void testReadWithAsyncRemoteOperatonFailure() throws Throwable {
121 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
123 doReturn(Futures.failed(new TestException())).when(mockActorContext).
124 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
126 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
128 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
131 private void testExceptionOnInitialCreateTransaction(Exception exToThrow, Invoker invoker)
133 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
135 if (exToThrow instanceof PrimaryNotFoundException) {
136 doReturn(Futures.failed(exToThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
138 doReturn(primaryShardInfoReply(getSystem(), actorRef)).
139 when(mockActorContext).findPrimaryShardAsync(anyString());
142 doReturn(Futures.failed(exToThrow)).when(mockActorContext).executeOperationAsync(
143 any(ActorSelection.class), any(), any(Timeout.class));
145 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
147 propagateReadFailedExceptionCause(invoker.invoke(transactionProxy));
150 private void testReadWithExceptionOnInitialCreateTransaction(Exception exToThrow) throws Throwable {
151 testExceptionOnInitialCreateTransaction(exToThrow, new Invoker() {
153 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
154 return proxy.read(TestModel.TEST_PATH);
159 @Test(expected = PrimaryNotFoundException.class)
160 public void testReadWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
161 testReadWithExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"));
164 @Test(expected = TimeoutException.class)
165 public void testReadWhenATimeoutExceptionIsThrown() throws Throwable {
166 testReadWithExceptionOnInitialCreateTransaction(new TimeoutException("test",
167 new Exception("reason")));
170 @Test(expected = TestException.class)
171 public void testReadWhenAnyOtherExceptionIsThrown() throws Throwable {
172 testReadWithExceptionOnInitialCreateTransaction(new TestException());
176 public void testReadWithPriorRecordingOperationSuccessful() throws Throwable {
177 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
179 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
181 expectBatchedModifications(actorRef, 1);
183 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
184 eq(actorSelection(actorRef)), eqSerializedReadData());
186 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
188 transactionProxy.write(TestModel.TEST_PATH, expectedNode);
190 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
191 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
193 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
194 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
196 InOrder inOrder = Mockito.inOrder(mockActorContext);
197 inOrder.verify(mockActorContext).executeOperationAsync(
198 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
200 inOrder.verify(mockActorContext).executeOperationAsync(
201 eq(actorSelection(actorRef)), eqSerializedReadData());
204 @Test(expected=IllegalStateException.class)
205 public void testReadPreConditionCheck() {
206 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
207 transactionProxy.read(TestModel.TEST_PATH);
210 @Test(expected=IllegalArgumentException.class)
211 public void testInvalidCreateTransactionReply() throws Throwable {
212 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
214 doReturn(getSystem().actorSelection(actorRef.path())).when(mockActorContext).
215 actorSelection(actorRef.path().toString());
217 doReturn(primaryShardInfoReply(getSystem(), actorRef)).
218 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
220 doReturn(Futures.successful(new Object())).when(mockActorContext).executeOperationAsync(
221 eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_ONLY),
224 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
226 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
230 public void testExists() throws Exception {
231 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
233 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
235 doReturn(dataExistsSerializedReply(false)).when(mockActorContext).executeOperationAsync(
236 eq(actorSelection(actorRef)), eqSerializedDataExists());
238 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
240 assertEquals("Exists response", false, exists);
242 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
243 eq(actorSelection(actorRef)), eqSerializedDataExists());
245 exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
247 assertEquals("Exists response", true, exists);
250 @Test(expected = PrimaryNotFoundException.class)
251 public void testExistsWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
252 testExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"), new Invoker() {
254 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
255 return proxy.exists(TestModel.TEST_PATH);
260 @Test(expected = ReadFailedException.class)
261 public void testExistsWithInvalidReplyMessageType() throws Exception {
262 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
264 doReturn(Futures.successful(new Object())).when(mockActorContext).
265 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
267 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
269 transactionProxy.exists(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
272 @Test(expected = TestException.class)
273 public void testExistsWithAsyncRemoteOperatonFailure() throws Throwable {
274 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
276 doReturn(Futures.failed(new TestException())).when(mockActorContext).
277 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
279 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
281 propagateReadFailedExceptionCause(transactionProxy.exists(TestModel.TEST_PATH));
285 public void testExistsWithPriorRecordingOperationSuccessful() throws Throwable {
286 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
288 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
290 expectBatchedModifications(actorRef, 1);
292 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
293 eq(actorSelection(actorRef)), eqSerializedDataExists());
295 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
297 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
299 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
301 assertEquals("Exists response", true, exists);
303 InOrder inOrder = Mockito.inOrder(mockActorContext);
304 inOrder.verify(mockActorContext).executeOperationAsync(
305 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
307 inOrder.verify(mockActorContext).executeOperationAsync(
308 eq(actorSelection(actorRef)), eqSerializedDataExists());
311 @Test(expected=IllegalStateException.class)
312 public void testExistsPreConditionCheck() {
313 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
314 transactionProxy.exists(TestModel.TEST_PATH);
318 public void testWrite() throws Exception {
319 dataStoreContextBuilder.shardBatchedModificationCount(1);
320 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
322 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
324 expectBatchedModifications(actorRef, 1);
326 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
328 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
330 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), false);
334 public void testWriteAfterAsyncRead() throws Throwable {
335 ActorRef actorRef = setupActorContextWithoutInitialCreateTransaction(getSystem(), DefaultShardStrategy.DEFAULT_SHARD);
337 Promise<Object> createTxPromise = akka.dispatch.Futures.promise();
338 doReturn(createTxPromise).when(mockActorContext).executeOperationAsync(
339 eq(getSystem().actorSelection(actorRef.path())),
340 eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
342 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
343 eq(actorSelection(actorRef)), eqSerializedReadData());
345 expectBatchedModificationsReady(actorRef);
347 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
349 final TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
351 final CountDownLatch readComplete = new CountDownLatch(1);
352 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
353 com.google.common.util.concurrent.Futures.addCallback(transactionProxy.read(TestModel.TEST_PATH),
354 new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
356 public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
358 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
359 } catch (Exception e) {
362 readComplete.countDown();
367 public void onFailure(Throwable t) {
369 readComplete.countDown();
373 createTxPromise.success(createTransactionReply(actorRef, DataStoreVersions.CURRENT_VERSION));
375 Uninterruptibles.awaitUninterruptibly(readComplete, 5, TimeUnit.SECONDS);
377 if(caughtEx.get() != null) {
378 throw caughtEx.get();
381 // This sends the batched modification.
382 transactionProxy.ready();
384 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), true);
387 @Test(expected=IllegalStateException.class)
388 public void testWritePreConditionCheck() {
389 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
390 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
393 @Test(expected=IllegalStateException.class)
394 public void testWriteAfterReadyPreConditionCheck() {
395 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
397 transactionProxy.ready();
399 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
403 public void testMerge() throws Exception {
404 dataStoreContextBuilder.shardBatchedModificationCount(1);
405 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
407 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
409 expectBatchedModifications(actorRef, 1);
411 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
413 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
415 verifyOneBatchedModification(actorRef, new MergeModification(TestModel.TEST_PATH, nodeToWrite), false);
419 public void testDelete() throws Exception {
420 dataStoreContextBuilder.shardBatchedModificationCount(1);
421 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
423 expectBatchedModifications(actorRef, 1);
425 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
427 transactionProxy.delete(TestModel.TEST_PATH);
429 verifyOneBatchedModification(actorRef, new DeleteModification(TestModel.TEST_PATH), false);
433 public void testReadWrite() throws Exception {
434 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
436 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
438 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
439 eq(actorSelection(actorRef)), eqSerializedReadData());
441 expectBatchedModifications(actorRef, 1);
443 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
445 transactionProxy.read(TestModel.TEST_PATH);
447 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
449 transactionProxy.read(TestModel.TEST_PATH);
451 transactionProxy.read(TestModel.TEST_PATH);
453 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
454 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
456 verifyBatchedModifications(batchedModifications.get(0), false,
457 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
461 public void testReadyWithReadWrite() throws Exception {
462 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
464 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
466 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
467 eq(actorSelection(actorRef)), eqSerializedReadData());
469 expectBatchedModificationsReady(actorRef, true);
471 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
473 transactionProxy.read(TestModel.TEST_PATH);
475 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
477 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
479 assertTrue(ready instanceof SingleCommitCohortProxy);
481 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
483 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
484 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
486 verifyBatchedModifications(batchedModifications.get(0), true, true,
487 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
489 assertEquals("getTotalMessageCount", 1, batchedModifications.get(0).getTotalMessagesSent());
493 public void testReadyWithNoModifications() throws Exception {
494 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
496 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
497 eq(actorSelection(actorRef)), eqSerializedReadData());
499 expectBatchedModificationsReady(actorRef, true);
501 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
503 transactionProxy.read(TestModel.TEST_PATH);
505 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
507 assertTrue(ready instanceof SingleCommitCohortProxy);
509 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
511 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
512 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
514 verifyBatchedModifications(batchedModifications.get(0), true, true);
518 public void testReadyWithMultipleShardWrites() throws Exception {
519 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
521 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
523 expectBatchedModificationsReady(actorRef1);
524 expectBatchedModificationsReady(actorRef2);
526 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
528 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
529 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
531 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
533 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
535 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef1),
536 actorSelection(actorRef2));
540 public void testReadyWithWriteOnlyAndLastBatchPending() throws Exception {
541 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
543 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
545 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
547 expectBatchedModificationsReady(actorRef, true);
549 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
551 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
553 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
555 assertTrue(ready instanceof SingleCommitCohortProxy);
557 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
559 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
560 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
562 verifyBatchedModifications(batchedModifications.get(0), true, true,
563 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
565 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
566 isA(ReadyTransaction.SERIALIZABLE_CLASS));
570 public void testReadyWithWriteOnlyAndLastBatchEmpty() throws Exception {
571 dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
572 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
574 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
576 expectBatchedModificationsReady(actorRef, true);
578 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
580 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
582 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
584 assertTrue(ready instanceof SingleCommitCohortProxy);
586 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
588 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
589 assertEquals("Captured BatchedModifications count", 2, batchedModifications.size());
591 verifyBatchedModifications(batchedModifications.get(0), false,
592 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
594 verifyBatchedModifications(batchedModifications.get(1), true, true);
596 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
597 isA(ReadyTransaction.SERIALIZABLE_CLASS));
601 public void testReadyWithReplyFailure() throws Exception {
602 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
604 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
606 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
608 expectFailedBatchedModifications(actorRef);
610 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
612 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
614 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
616 assertTrue(ready instanceof SingleCommitCohortProxy);
618 verifyCohortFutures((SingleCommitCohortProxy)ready, TestException.class);
622 public void testReadyWithDebugContextEnabled() throws Exception {
623 dataStoreContextBuilder.transactionDebugContextEnabled(true);
625 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
627 expectBatchedModificationsReady(actorRef, true);
629 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
631 transactionProxy.merge(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
633 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
635 assertTrue(ready instanceof DebugThreePhaseCommitCohort);
637 verifyCohortFutures((DebugThreePhaseCommitCohort)ready, new CommitTransactionReply().toSerializable());
640 private void testWriteOnlyTxWithFindPrimaryShardFailure(Exception toThrow) throws Exception {
641 doReturn(Futures.failed(toThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
643 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
645 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
647 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
649 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
651 transactionProxy.delete(TestModel.TEST_PATH);
653 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
655 assertTrue(ready instanceof SingleCommitCohortProxy);
657 verifyCohortFutures((SingleCommitCohortProxy)ready, toThrow.getClass());
661 public void testWriteOnlyTxWithPrimaryNotFoundException() throws Exception {
662 testWriteOnlyTxWithFindPrimaryShardFailure(new PrimaryNotFoundException("mock"));
666 public void testWriteOnlyTxWithNotInitializedException() throws Exception {
667 testWriteOnlyTxWithFindPrimaryShardFailure(new NotInitializedException("mock"));
671 public void testWriteOnlyTxWithNoShardLeaderException() throws Exception {
672 testWriteOnlyTxWithFindPrimaryShardFailure(new NoShardLeaderException("mock"));
676 public void testReadyWithInvalidReplyMessageType() throws Exception {
677 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
678 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
680 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
682 doReturn(Futures.successful(new Object())).when(mockActorContext).
683 executeOperationAsync(eq(actorSelection(actorRef1)), isA(BatchedModifications.class));
685 expectBatchedModificationsReady(actorRef2);
687 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
689 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
690 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
692 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
694 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
696 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef2),
697 IllegalArgumentException.class);
701 public void testGetIdentifier() {
702 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
703 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
705 Object id = transactionProxy.getIdentifier();
706 assertNotNull("getIdentifier returned null", id);
707 assertTrue("Invalid identifier: " + id, id.toString().startsWith(memberName));
711 public void testClose() throws Exception{
712 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
714 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
715 eq(actorSelection(actorRef)), eqSerializedReadData());
717 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
719 transactionProxy.read(TestModel.TEST_PATH);
721 transactionProxy.close();
723 verify(mockActorContext).sendOperationAsync(
724 eq(actorSelection(actorRef)), isA(CloseTransaction.SERIALIZABLE_CLASS));
729 * Method to test a local Tx actor. The Tx paths are matched to decide if the
730 * Tx actor is local or not. This is done by mocking the Tx actor path
731 * and the caller paths and ensuring that the paths have the remote-address format
733 * Note: Since the default akka provider for test is not a RemoteActorRefProvider,
734 * the paths returned for the actors for all the tests are not qualified remote paths.
735 * Hence are treated as non-local/remote actors. In short, all tests except
736 * few below run for remote actors
741 public void testLocalTxActorRead() throws Exception {
742 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
743 doReturn(true).when(mockActorContext).isPathLocal(anyString());
745 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
747 // negative test case with null as the reply
748 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
749 any(ActorSelection.class), eqReadData());
751 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
752 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
754 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
756 // test case with node as read data reply
757 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
759 doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
760 any(ActorSelection.class), eqReadData());
762 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
764 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
766 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
768 // test for local data exists
769 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
770 any(ActorSelection.class), eqDataExists());
772 boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
774 assertEquals("Exists response", true, exists);
778 public void testLocalTxActorReady() throws Exception {
779 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
780 doReturn(true).when(mockActorContext).isPathLocal(anyString());
782 expectBatchedModificationsReady(actorRef, true);
784 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
786 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
787 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
789 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
791 assertTrue(ready instanceof SingleCommitCohortProxy);
793 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
796 private static interface TransactionProxyOperation {
797 void run(TransactionProxy transactionProxy);
800 private void throttleOperation(TransactionProxyOperation operation) {
801 throttleOperation(operation, 1, true);
804 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound){
805 throttleOperation(operation, outstandingOpsLimit, shardFound, TimeUnit.MILLISECONDS.toNanos(
806 mockActorContext.getDatastoreContext().getOperationTimeoutInMillis()));
809 private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef){
810 return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), DataStoreVersions.CURRENT_VERSION,
811 Optional.<DataTree>absent());
814 private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef, Optional<DataTree> dataTreeOptional){
815 return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), DataStoreVersions.CURRENT_VERSION,
820 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound, long expectedCompletionTime){
821 ActorSystem actorSystem = getSystem();
822 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
824 // Note that we setting batchedModificationCount to one less than what we need because in TransactionProxy
825 // we now allow one extra permit to be allowed for ready
826 doReturn(dataStoreContextBuilder.operationTimeoutInSeconds(2).
827 shardBatchedModificationCount(outstandingOpsLimit-1).build()).when(mockActorContext).getDatastoreContext();
829 doReturn(actorSystem.actorSelection(shardActorRef.path())).
830 when(mockActorContext).actorSelection(shardActorRef.path().toString());
833 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
834 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
835 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
836 when(mockActorContext).findPrimaryShardAsync(eq("cars"));
839 doReturn(Futures.failed(new Exception("not found")))
840 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
843 String actorPath = "akka.tcp://system@127.0.0.1:2550/user/tx-actor";
845 doReturn(incompleteFuture()).when(mockActorContext).
846 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
847 eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
849 doReturn(true).when(mockActorContext).isPathLocal(actorPath);
851 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
853 long start = System.nanoTime();
855 operation.run(transactionProxy);
857 long end = System.nanoTime();
859 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
860 expectedCompletionTime, (end-start)),
861 ((end - start) > expectedCompletionTime) && ((end - start) < expectedCompletionTime*2));
865 private void completeOperation(TransactionProxyOperation operation){
866 completeOperation(operation, true);
869 private void completeOperation(TransactionProxyOperation operation, boolean shardFound){
870 ActorSystem actorSystem = getSystem();
871 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
873 doReturn(actorSystem.actorSelection(shardActorRef.path())).
874 when(mockActorContext).actorSelection(shardActorRef.path().toString());
877 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
878 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
880 doReturn(Futures.failed(new PrimaryNotFoundException("test")))
881 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
884 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
885 String actorPath = txActorRef.path().toString();
886 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
887 setTransactionId("txn-1").setTransactionActorPath(actorPath).
888 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
890 doReturn(actorSystem.actorSelection(actorPath)).when(mockActorContext).actorSelection(actorPath);
892 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
893 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
894 eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
896 doReturn(true).when(mockActorContext).isPathLocal(anyString());
898 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
900 long start = System.nanoTime();
902 operation.run(transactionProxy);
904 long end = System.nanoTime();
906 long expected = TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis());
907 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
908 expected, (end-start)), (end - start) <= expected);
911 private void completeOperationLocal(TransactionProxyOperation operation, Optional<DataTree> dataTreeOptional){
912 ActorSystem actorSystem = getSystem();
913 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
915 doReturn(actorSystem.actorSelection(shardActorRef.path())).
916 when(mockActorContext).actorSelection(shardActorRef.path().toString());
918 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef, dataTreeOptional))).
919 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
921 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
923 long start = System.nanoTime();
925 operation.run(transactionProxy);
927 long end = System.nanoTime();
929 long expected = TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis());
930 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
931 expected, (end-start)), (end - start) <= expected);
934 private Optional<DataTree> createDataTree(){
935 DataTree dataTree = mock(DataTree.class);
936 Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
937 DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
938 DataTreeModification dataTreeModification = mock(DataTreeModification.class);
940 doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
941 doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
943 return dataTreeOptional;
946 private Optional<DataTree> createDataTree(NormalizedNode readResponse){
947 DataTree dataTree = mock(DataTree.class);
948 Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
949 DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
950 DataTreeModification dataTreeModification = mock(DataTreeModification.class);
952 doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
953 doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
954 doReturn(Optional.of(readResponse)).when(dataTreeModification).readNode(any(YangInstanceIdentifier.class));
956 return dataTreeOptional;
961 public void testWriteCompletionForLocalShard(){
962 completeOperationLocal(new TransactionProxyOperation() {
964 public void run(TransactionProxy transactionProxy) {
965 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
967 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
969 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
972 }, createDataTree());
976 public void testWriteThrottlingWhenShardFound(){
977 throttleOperation(new TransactionProxyOperation() {
979 public void run(TransactionProxy transactionProxy) {
980 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
982 expectIncompleteBatchedModifications();
984 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
986 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
992 public void testWriteThrottlingWhenShardNotFound(){
993 // Confirm that there is no throttling when the Shard is not found
994 completeOperation(new TransactionProxyOperation() {
996 public void run(TransactionProxy transactionProxy) {
997 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
999 expectBatchedModifications(2);
1001 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1003 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1011 public void testWriteCompletion(){
1012 completeOperation(new TransactionProxyOperation() {
1014 public void run(TransactionProxy transactionProxy) {
1015 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1017 expectBatchedModifications(2);
1019 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1021 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1027 public void testMergeThrottlingWhenShardFound(){
1028 throttleOperation(new TransactionProxyOperation() {
1030 public void run(TransactionProxy transactionProxy) {
1031 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1033 expectIncompleteBatchedModifications();
1035 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1037 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1043 public void testMergeThrottlingWhenShardNotFound(){
1044 completeOperation(new TransactionProxyOperation() {
1046 public void run(TransactionProxy transactionProxy) {
1047 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1049 expectBatchedModifications(2);
1051 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1053 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1059 public void testMergeCompletion(){
1060 completeOperation(new TransactionProxyOperation() {
1062 public void run(TransactionProxy transactionProxy) {
1063 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1065 expectBatchedModifications(2);
1067 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1069 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1076 public void testMergeCompletionForLocalShard(){
1077 completeOperationLocal(new TransactionProxyOperation() {
1079 public void run(TransactionProxy transactionProxy) {
1080 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1082 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1084 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1087 }, createDataTree());
1092 public void testDeleteThrottlingWhenShardFound(){
1094 throttleOperation(new TransactionProxyOperation() {
1096 public void run(TransactionProxy transactionProxy) {
1097 expectIncompleteBatchedModifications();
1099 transactionProxy.delete(TestModel.TEST_PATH);
1101 transactionProxy.delete(TestModel.TEST_PATH);
1108 public void testDeleteThrottlingWhenShardNotFound(){
1110 completeOperation(new TransactionProxyOperation() {
1112 public void run(TransactionProxy transactionProxy) {
1113 expectBatchedModifications(2);
1115 transactionProxy.delete(TestModel.TEST_PATH);
1117 transactionProxy.delete(TestModel.TEST_PATH);
1123 public void testDeleteCompletionForLocalShard(){
1124 completeOperationLocal(new TransactionProxyOperation() {
1126 public void run(TransactionProxy transactionProxy) {
1128 transactionProxy.delete(TestModel.TEST_PATH);
1130 transactionProxy.delete(TestModel.TEST_PATH);
1132 }, createDataTree());
1137 public void testDeleteCompletion(){
1138 completeOperation(new TransactionProxyOperation() {
1140 public void run(TransactionProxy transactionProxy) {
1141 expectBatchedModifications(2);
1143 transactionProxy.delete(TestModel.TEST_PATH);
1145 transactionProxy.delete(TestModel.TEST_PATH);
1152 public void testReadThrottlingWhenShardFound(){
1154 throttleOperation(new TransactionProxyOperation() {
1156 public void run(TransactionProxy transactionProxy) {
1157 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1158 any(ActorSelection.class), eqReadData());
1160 transactionProxy.read(TestModel.TEST_PATH);
1162 transactionProxy.read(TestModel.TEST_PATH);
1168 public void testReadThrottlingWhenShardNotFound(){
1170 completeOperation(new TransactionProxyOperation() {
1172 public void run(TransactionProxy transactionProxy) {
1173 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1174 any(ActorSelection.class), eqReadData());
1176 transactionProxy.read(TestModel.TEST_PATH);
1178 transactionProxy.read(TestModel.TEST_PATH);
1185 public void testReadCompletion(){
1186 completeOperation(new TransactionProxyOperation() {
1188 public void run(TransactionProxy transactionProxy) {
1189 NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1191 doReturn(readDataReply(nodeToRead)).when(mockActorContext).executeOperationAsync(
1192 any(ActorSelection.class), eqReadData());
1194 transactionProxy.read(TestModel.TEST_PATH);
1196 transactionProxy.read(TestModel.TEST_PATH);
1203 public void testReadCompletionForLocalShard(){
1204 final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1205 completeOperationLocal(new TransactionProxyOperation() {
1207 public void run(TransactionProxy transactionProxy) {
1208 transactionProxy.read(TestModel.TEST_PATH);
1210 transactionProxy.read(TestModel.TEST_PATH);
1212 }, createDataTree(nodeToRead));
1217 public void testReadCompletionForLocalShardWhenExceptionOccurs(){
1218 completeOperationLocal(new TransactionProxyOperation() {
1220 public void run(TransactionProxy transactionProxy) {
1221 transactionProxy.read(TestModel.TEST_PATH);
1223 transactionProxy.read(TestModel.TEST_PATH);
1225 }, createDataTree());
1230 public void testExistsThrottlingWhenShardFound(){
1232 throttleOperation(new TransactionProxyOperation() {
1234 public void run(TransactionProxy transactionProxy) {
1235 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1236 any(ActorSelection.class), eqDataExists());
1238 transactionProxy.exists(TestModel.TEST_PATH);
1240 transactionProxy.exists(TestModel.TEST_PATH);
1246 public void testExistsThrottlingWhenShardNotFound(){
1248 completeOperation(new TransactionProxyOperation() {
1250 public void run(TransactionProxy transactionProxy) {
1251 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1252 any(ActorSelection.class), eqDataExists());
1254 transactionProxy.exists(TestModel.TEST_PATH);
1256 transactionProxy.exists(TestModel.TEST_PATH);
1263 public void testExistsCompletion(){
1264 completeOperation(new TransactionProxyOperation() {
1266 public void run(TransactionProxy transactionProxy) {
1267 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
1268 any(ActorSelection.class), eqDataExists());
1270 transactionProxy.exists(TestModel.TEST_PATH);
1272 transactionProxy.exists(TestModel.TEST_PATH);
1279 public void testExistsCompletionForLocalShard(){
1280 final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1281 completeOperationLocal(new TransactionProxyOperation() {
1283 public void run(TransactionProxy transactionProxy) {
1284 transactionProxy.exists(TestModel.TEST_PATH);
1286 transactionProxy.exists(TestModel.TEST_PATH);
1288 }, createDataTree(nodeToRead));
1293 public void testExistsCompletionForLocalShardWhenExceptionOccurs(){
1294 completeOperationLocal(new TransactionProxyOperation() {
1296 public void run(TransactionProxy transactionProxy) {
1297 transactionProxy.exists(TestModel.TEST_PATH);
1299 transactionProxy.exists(TestModel.TEST_PATH);
1301 }, createDataTree());
1305 public void testReadyThrottling(){
1307 throttleOperation(new TransactionProxyOperation() {
1309 public void run(TransactionProxy transactionProxy) {
1310 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1312 expectBatchedModifications(1);
1314 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1316 transactionProxy.ready();
1322 public void testReadyThrottlingWithTwoTransactionContexts(){
1323 throttleOperation(new TransactionProxyOperation() {
1325 public void run(TransactionProxy transactionProxy) {
1326 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1327 NormalizedNode<?, ?> carsNode = ImmutableNodes.containerNode(CarsModel.BASE_QNAME);
1329 expectBatchedModifications(2);
1331 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1332 any(ActorSelection.class), any(ReadyTransaction.class));
1334 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1336 // Trying to write to Cars will cause another transaction context to get created
1337 transactionProxy.write(CarsModel.BASE_PATH, carsNode);
1339 // Now ready should block for both transaction contexts
1340 transactionProxy.ready();
1342 }, 1, true, TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis()) * 2);
1345 private void testModificationOperationBatching(TransactionType type) throws Exception {
1346 int shardBatchedModificationCount = 3;
1347 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1349 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), type);
1351 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1353 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1354 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1356 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1357 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1359 YangInstanceIdentifier writePath3 = TestModel.INNER_LIST_PATH;
1360 NormalizedNode<?, ?> writeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1362 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1363 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1365 YangInstanceIdentifier mergePath2 = TestModel.OUTER_LIST_PATH;
1366 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1368 YangInstanceIdentifier mergePath3 = TestModel.INNER_LIST_PATH;
1369 NormalizedNode<?, ?> mergeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1371 YangInstanceIdentifier deletePath1 = TestModel.TEST_PATH;
1372 YangInstanceIdentifier deletePath2 = TestModel.OUTER_LIST_PATH;
1374 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, type);
1376 transactionProxy.write(writePath1, writeNode1);
1377 transactionProxy.write(writePath2, writeNode2);
1378 transactionProxy.delete(deletePath1);
1379 transactionProxy.merge(mergePath1, mergeNode1);
1380 transactionProxy.merge(mergePath2, mergeNode2);
1381 transactionProxy.write(writePath3, writeNode3);
1382 transactionProxy.merge(mergePath3, mergeNode3);
1383 transactionProxy.delete(deletePath2);
1385 // This sends the last batch.
1386 transactionProxy.ready();
1388 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1389 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1391 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1392 new WriteModification(writePath2, writeNode2), new DeleteModification(deletePath1));
1394 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1395 new MergeModification(mergePath2, mergeNode2), new WriteModification(writePath3, writeNode3));
1397 verifyBatchedModifications(batchedModifications.get(2), true, true,
1398 new MergeModification(mergePath3, mergeNode3), new DeleteModification(deletePath2));
1400 assertEquals("getTotalMessageCount", 3, batchedModifications.get(2).getTotalMessagesSent());
1404 public void testReadWriteModificationOperationBatching() throws Throwable {
1405 testModificationOperationBatching(READ_WRITE);
1409 public void testWriteOnlyModificationOperationBatching() throws Throwable {
1410 testModificationOperationBatching(WRITE_ONLY);
1414 public void testOptimizedWriteOnlyModificationOperationBatching() throws Throwable {
1415 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
1416 testModificationOperationBatching(WRITE_ONLY);
1420 public void testModificationOperationBatchingWithInterleavedReads() throws Throwable {
1422 int shardBatchedModificationCount = 10;
1423 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1425 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
1427 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1429 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1430 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1432 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1433 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1435 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1436 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1438 YangInstanceIdentifier mergePath2 = TestModel.INNER_LIST_PATH;
1439 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1441 YangInstanceIdentifier deletePath = TestModel.OUTER_LIST_PATH;
1443 doReturn(readSerializedDataReply(writeNode2)).when(mockActorContext).executeOperationAsync(
1444 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1446 doReturn(readSerializedDataReply(mergeNode2)).when(mockActorContext).executeOperationAsync(
1447 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1449 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
1450 eq(actorSelection(actorRef)), eqSerializedDataExists());
1452 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
1454 transactionProxy.write(writePath1, writeNode1);
1455 transactionProxy.write(writePath2, writeNode2);
1457 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(writePath2).
1458 get(5, TimeUnit.SECONDS);
1460 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1461 assertEquals("Response NormalizedNode", writeNode2, readOptional.get());
1463 transactionProxy.merge(mergePath1, mergeNode1);
1464 transactionProxy.merge(mergePath2, mergeNode2);
1466 readOptional = transactionProxy.read(mergePath2).get(5, TimeUnit.SECONDS);
1468 transactionProxy.delete(deletePath);
1470 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
1471 assertEquals("Exists response", true, exists);
1473 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1474 assertEquals("Response NormalizedNode", mergeNode2, readOptional.get());
1476 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1477 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1479 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1480 new WriteModification(writePath2, writeNode2));
1482 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1483 new MergeModification(mergePath2, mergeNode2));
1485 verifyBatchedModifications(batchedModifications.get(2), false, new DeleteModification(deletePath));
1487 InOrder inOrder = Mockito.inOrder(mockActorContext);
1488 inOrder.verify(mockActorContext).executeOperationAsync(
1489 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1491 inOrder.verify(mockActorContext).executeOperationAsync(
1492 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1494 inOrder.verify(mockActorContext).executeOperationAsync(
1495 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1497 inOrder.verify(mockActorContext).executeOperationAsync(
1498 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1500 inOrder.verify(mockActorContext).executeOperationAsync(
1501 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1503 inOrder.verify(mockActorContext).executeOperationAsync(
1504 eq(actorSelection(actorRef)), eqSerializedDataExists());
1508 public void testReadRoot() throws ReadFailedException, InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
1510 SchemaContext schemaContext = SchemaContextHelper.full();
1511 Configuration configuration = mock(Configuration.class);
1512 doReturn(configuration).when(mockActorContext).getConfiguration();
1513 doReturn(schemaContext).when(mockActorContext).getSchemaContext();
1514 doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames();
1516 NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1517 NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
1519 setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext));
1520 setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext));
1522 doReturn(memberName).when(mockActorContext).getCurrentMemberName();
1524 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
1526 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
1528 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
1529 YangInstanceIdentifier.builder().build()).get(5, TimeUnit.SECONDS);
1531 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1533 NormalizedNode<?, ?> normalizedNode = readOptional.get();
1535 assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
1537 Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
1539 for(NormalizedNode<?,?> node : collection){
1540 assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
1543 assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
1544 NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null);
1546 assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME));
1548 assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
1549 NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
1551 assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME));
1555 private void setUpReadData(String shardName, NormalizedNode<?, ?> expectedNode) {
1556 ActorSystem actorSystem = getSystem();
1557 ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
1559 doReturn(getSystem().actorSelection(shardActorRef.path())).
1560 when(mockActorContext).actorSelection(shardActorRef.path().toString());
1562 doReturn(primaryShardInfoReply(getSystem(), shardActorRef)).
1563 when(mockActorContext).findPrimaryShardAsync(eq(shardName));
1565 doReturn(true).when(mockActorContext).isPathLocal(shardActorRef.path().toString());
1567 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
1569 doReturn(actorSystem.actorSelection(txActorRef.path())).
1570 when(mockActorContext).actorSelection(txActorRef.path().toString());
1572 doReturn(Futures.successful(createTransactionReply(txActorRef, DataStoreVersions.CURRENT_VERSION))).when(mockActorContext).
1573 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
1574 eqCreateTransaction(memberName, TransactionType.READ_ONLY), any(Timeout.class));
1576 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
1577 eq(actorSelection(txActorRef)), eqSerializedReadData(YangInstanceIdentifier.builder().build()));