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.mock;
20 import static org.mockito.Mockito.never;
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.datastore.config.Configuration;
47 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
48 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
49 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
50 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
51 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
52 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
53 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
54 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
55 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
56 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
57 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
58 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
59 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
60 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
61 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest;
62 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
63 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
64 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
65 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
66 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
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(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
96 eq(actorSelection(actorRef)), eqSerializedReadData());
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(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
106 eq(actorSelection(actorRef)), eqSerializedReadData());
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)), eqSerializedReadData());
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)), eqSerializedReadData());
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(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
192 eq(actorSelection(actorRef)), eqSerializedReadData());
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));
208 inOrder.verify(mockActorContext).executeOperationAsync(
209 eq(actorSelection(actorRef)), eqSerializedReadData());
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(dataExistsSerializedReply(false)).when(mockActorContext).executeOperationAsync(
244 eq(actorSelection(actorRef)), eqSerializedDataExists());
246 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
248 assertEquals("Exists response", false, exists);
250 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
251 eq(actorSelection(actorRef)), eqSerializedDataExists());
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)), eqSerializedDataExists());
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)), eqSerializedDataExists());
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(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
301 eq(actorSelection(actorRef)), eqSerializedDataExists());
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));
315 inOrder.verify(mockActorContext).executeOperationAsync(
316 eq(actorSelection(actorRef)), eqSerializedDataExists());
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(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
351 eq(actorSelection(actorRef)), eqSerializedReadData());
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(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
447 eq(actorSelection(actorRef)), eqSerializedReadData());
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(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
475 eq(actorSelection(actorRef)), eqSerializedReadData());
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(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
505 eq(actorSelection(actorRef)), eqSerializedReadData());
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));
573 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
574 isA(ReadyTransaction.SERIALIZABLE_CLASS));
578 public void testReadyWithWriteOnlyAndLastBatchEmpty() throws Exception {
579 dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
580 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
582 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
584 expectBatchedModificationsReady(actorRef, true);
586 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
588 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
590 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
592 assertTrue(ready instanceof SingleCommitCohortProxy);
594 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
596 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
597 assertEquals("Captured BatchedModifications count", 2, batchedModifications.size());
599 verifyBatchedModifications(batchedModifications.get(0), false,
600 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
602 verifyBatchedModifications(batchedModifications.get(1), true, true);
604 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
605 isA(ReadyTransaction.SERIALIZABLE_CLASS));
609 public void testReadyWithReplyFailure() throws Exception {
610 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
612 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
614 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
616 expectFailedBatchedModifications(actorRef);
618 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
620 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
622 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
624 assertTrue(ready instanceof SingleCommitCohortProxy);
626 verifyCohortFutures((SingleCommitCohortProxy)ready, TestException.class);
630 public void testReadyWithDebugContextEnabled() throws Exception {
631 dataStoreContextBuilder.transactionDebugContextEnabled(true);
633 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
635 expectBatchedModificationsReady(actorRef, true);
637 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
639 transactionProxy.merge(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
641 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
643 assertTrue(ready instanceof DebugThreePhaseCommitCohort);
645 verifyCohortFutures((DebugThreePhaseCommitCohort)ready, new CommitTransactionReply().toSerializable());
648 private void testWriteOnlyTxWithFindPrimaryShardFailure(Exception toThrow) throws Exception {
649 doReturn(Futures.failed(toThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
651 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
653 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
655 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
657 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
659 transactionProxy.delete(TestModel.TEST_PATH);
661 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
663 assertTrue(ready instanceof SingleCommitCohortProxy);
665 verifyCohortFutures((SingleCommitCohortProxy)ready, toThrow.getClass());
669 public void testWriteOnlyTxWithPrimaryNotFoundException() throws Exception {
670 testWriteOnlyTxWithFindPrimaryShardFailure(new PrimaryNotFoundException("mock"));
674 public void testWriteOnlyTxWithNotInitializedException() throws Exception {
675 testWriteOnlyTxWithFindPrimaryShardFailure(new NotInitializedException("mock"));
679 public void testWriteOnlyTxWithNoShardLeaderException() throws Exception {
680 testWriteOnlyTxWithFindPrimaryShardFailure(new NoShardLeaderException("mock"));
684 public void testReadyWithInvalidReplyMessageType() throws Exception {
685 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
686 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
688 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
690 doReturn(Futures.successful(new Object())).when(mockActorContext).
691 executeOperationAsync(eq(actorSelection(actorRef1)), isA(BatchedModifications.class));
693 expectBatchedModificationsReady(actorRef2);
695 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
697 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
698 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
700 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
702 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
704 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef2),
705 IllegalArgumentException.class);
709 public void testGetIdentifier() {
710 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
711 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
713 Object id = transactionProxy.getIdentifier();
714 assertNotNull("getIdentifier returned null", id);
715 assertTrue("Invalid identifier: " + id, id.toString().startsWith(memberName));
719 public void testClose() throws Exception{
720 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
722 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
723 eq(actorSelection(actorRef)), eqSerializedReadData());
725 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
727 transactionProxy.read(TestModel.TEST_PATH);
729 transactionProxy.close();
731 verify(mockActorContext).sendOperationAsync(
732 eq(actorSelection(actorRef)), isA(CloseTransaction.SERIALIZABLE_CLASS));
737 * Method to test a local Tx actor. The Tx paths are matched to decide if the
738 * Tx actor is local or not. This is done by mocking the Tx actor path
739 * and the caller paths and ensuring that the paths have the remote-address format
741 * Note: Since the default akka provider for test is not a RemoteActorRefProvider,
742 * the paths returned for the actors for all the tests are not qualified remote paths.
743 * Hence are treated as non-local/remote actors. In short, all tests except
744 * few below run for remote actors
749 public void testLocalTxActorRead() throws Exception {
750 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
751 doReturn(true).when(mockActorContext).isPathLocal(anyString());
753 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
755 // negative test case with null as the reply
756 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
757 any(ActorSelection.class), eqReadData());
759 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
760 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
762 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
764 // test case with node as read data reply
765 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
767 doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
768 any(ActorSelection.class), eqReadData());
770 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
772 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
774 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
776 // test for local data exists
777 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
778 any(ActorSelection.class), eqDataExists());
780 boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
782 assertEquals("Exists response", true, exists);
786 public void testLocalTxActorReady() throws Exception {
787 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
788 doReturn(true).when(mockActorContext).isPathLocal(anyString());
790 expectBatchedModificationsReady(actorRef, true);
792 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
794 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
795 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
797 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
799 assertTrue(ready instanceof SingleCommitCohortProxy);
801 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
804 private static interface TransactionProxyOperation {
805 void run(TransactionProxy transactionProxy);
808 private void throttleOperation(TransactionProxyOperation operation) {
809 throttleOperation(operation, 1, true);
812 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound){
813 throttleOperation(operation, outstandingOpsLimit, shardFound, TimeUnit.MILLISECONDS.toNanos(
814 mockActorContext.getDatastoreContext().getOperationTimeoutInMillis()));
817 private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef){
818 return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), DataStoreVersions.CURRENT_VERSION,
819 Optional.<DataTree>absent());
822 private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef, Optional<DataTree> dataTreeOptional){
823 return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), DataStoreVersions.CURRENT_VERSION,
828 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound, long expectedCompletionTime){
829 ActorSystem actorSystem = getSystem();
830 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
832 // Note that we setting batchedModificationCount to one less than what we need because in TransactionProxy
833 // we now allow one extra permit to be allowed for ready
834 doReturn(dataStoreContextBuilder.operationTimeoutInSeconds(2).
835 shardBatchedModificationCount(outstandingOpsLimit-1).build()).when(mockActorContext).getDatastoreContext();
837 doReturn(actorSystem.actorSelection(shardActorRef.path())).
838 when(mockActorContext).actorSelection(shardActorRef.path().toString());
841 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
842 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
843 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
844 when(mockActorContext).findPrimaryShardAsync(eq("cars"));
847 doReturn(Futures.failed(new Exception("not found")))
848 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
851 String actorPath = "akka.tcp://system@127.0.0.1:2550/user/tx-actor";
853 doReturn(incompleteFuture()).when(mockActorContext).
854 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
855 eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
857 doReturn(true).when(mockActorContext).isPathLocal(actorPath);
859 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
861 long start = System.nanoTime();
863 operation.run(transactionProxy);
865 long end = System.nanoTime();
867 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
868 expectedCompletionTime, (end-start)),
869 ((end - start) > expectedCompletionTime) && ((end - start) < expectedCompletionTime*2));
873 private void completeOperation(TransactionProxyOperation operation){
874 completeOperation(operation, true);
877 private void completeOperation(TransactionProxyOperation operation, boolean shardFound){
878 ActorSystem actorSystem = getSystem();
879 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
881 doReturn(actorSystem.actorSelection(shardActorRef.path())).
882 when(mockActorContext).actorSelection(shardActorRef.path().toString());
885 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
886 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
888 doReturn(Futures.failed(new PrimaryNotFoundException("test")))
889 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
892 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
893 String actorPath = txActorRef.path().toString();
894 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
895 setTransactionId("txn-1").setTransactionActorPath(actorPath).
896 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
898 doReturn(actorSystem.actorSelection(actorPath)).when(mockActorContext).actorSelection(actorPath);
900 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
901 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
902 eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
904 doReturn(true).when(mockActorContext).isPathLocal(anyString());
906 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
908 long start = System.nanoTime();
910 operation.run(transactionProxy);
912 long end = System.nanoTime();
914 long expected = TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis());
915 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
916 expected, (end-start)), (end - start) <= expected);
919 private void completeOperationLocal(TransactionProxyOperation operation, Optional<DataTree> dataTreeOptional){
920 ActorSystem actorSystem = getSystem();
921 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
923 doReturn(actorSystem.actorSelection(shardActorRef.path())).
924 when(mockActorContext).actorSelection(shardActorRef.path().toString());
926 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef, dataTreeOptional))).
927 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
929 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
931 long start = System.nanoTime();
933 operation.run(transactionProxy);
935 long end = System.nanoTime();
937 long expected = TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis());
938 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
939 expected, (end-start)), (end - start) <= expected);
942 private static Optional<DataTree> createDataTree(){
943 DataTree dataTree = mock(DataTree.class);
944 Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
945 DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
946 DataTreeModification dataTreeModification = mock(DataTreeModification.class);
948 doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
949 doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
951 return dataTreeOptional;
954 private static Optional<DataTree> createDataTree(NormalizedNode readResponse){
955 DataTree dataTree = mock(DataTree.class);
956 Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
957 DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
958 DataTreeModification dataTreeModification = mock(DataTreeModification.class);
960 doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
961 doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
962 doReturn(Optional.of(readResponse)).when(dataTreeModification).readNode(any(YangInstanceIdentifier.class));
964 return dataTreeOptional;
969 public void testWriteCompletionForLocalShard(){
970 completeOperationLocal(new TransactionProxyOperation() {
972 public void run(TransactionProxy transactionProxy) {
973 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
975 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
977 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
980 }, createDataTree());
984 public void testWriteThrottlingWhenShardFound(){
985 throttleOperation(new TransactionProxyOperation() {
987 public void run(TransactionProxy transactionProxy) {
988 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
990 expectIncompleteBatchedModifications();
992 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
994 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1000 public void testWriteThrottlingWhenShardNotFound(){
1001 // Confirm that there is no throttling when the Shard is not found
1002 completeOperation(new TransactionProxyOperation() {
1004 public void run(TransactionProxy transactionProxy) {
1005 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1007 expectBatchedModifications(2);
1009 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1011 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1019 public void testWriteCompletion(){
1020 completeOperation(new TransactionProxyOperation() {
1022 public void run(TransactionProxy transactionProxy) {
1023 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1025 expectBatchedModifications(2);
1027 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1029 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1035 public void testMergeThrottlingWhenShardFound(){
1036 throttleOperation(new TransactionProxyOperation() {
1038 public void run(TransactionProxy transactionProxy) {
1039 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1041 expectIncompleteBatchedModifications();
1043 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1045 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1051 public void testMergeThrottlingWhenShardNotFound(){
1052 completeOperation(new TransactionProxyOperation() {
1054 public void run(TransactionProxy transactionProxy) {
1055 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1057 expectBatchedModifications(2);
1059 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1061 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1067 public void testMergeCompletion(){
1068 completeOperation(new TransactionProxyOperation() {
1070 public void run(TransactionProxy transactionProxy) {
1071 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1073 expectBatchedModifications(2);
1075 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1077 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1084 public void testMergeCompletionForLocalShard(){
1085 completeOperationLocal(new TransactionProxyOperation() {
1087 public void run(TransactionProxy transactionProxy) {
1088 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1090 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1092 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1095 }, createDataTree());
1100 public void testDeleteThrottlingWhenShardFound(){
1102 throttleOperation(new TransactionProxyOperation() {
1104 public void run(TransactionProxy transactionProxy) {
1105 expectIncompleteBatchedModifications();
1107 transactionProxy.delete(TestModel.TEST_PATH);
1109 transactionProxy.delete(TestModel.TEST_PATH);
1116 public void testDeleteThrottlingWhenShardNotFound(){
1118 completeOperation(new TransactionProxyOperation() {
1120 public void run(TransactionProxy transactionProxy) {
1121 expectBatchedModifications(2);
1123 transactionProxy.delete(TestModel.TEST_PATH);
1125 transactionProxy.delete(TestModel.TEST_PATH);
1131 public void testDeleteCompletionForLocalShard(){
1132 completeOperationLocal(new TransactionProxyOperation() {
1134 public void run(TransactionProxy transactionProxy) {
1136 transactionProxy.delete(TestModel.TEST_PATH);
1138 transactionProxy.delete(TestModel.TEST_PATH);
1140 }, createDataTree());
1145 public void testDeleteCompletion(){
1146 completeOperation(new TransactionProxyOperation() {
1148 public void run(TransactionProxy transactionProxy) {
1149 expectBatchedModifications(2);
1151 transactionProxy.delete(TestModel.TEST_PATH);
1153 transactionProxy.delete(TestModel.TEST_PATH);
1160 public void testReadThrottlingWhenShardFound(){
1162 throttleOperation(new TransactionProxyOperation() {
1164 public void run(TransactionProxy transactionProxy) {
1165 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1166 any(ActorSelection.class), eqReadData());
1168 transactionProxy.read(TestModel.TEST_PATH);
1170 transactionProxy.read(TestModel.TEST_PATH);
1176 public void testReadThrottlingWhenShardNotFound(){
1178 completeOperation(new TransactionProxyOperation() {
1180 public void run(TransactionProxy transactionProxy) {
1181 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1182 any(ActorSelection.class), eqReadData());
1184 transactionProxy.read(TestModel.TEST_PATH);
1186 transactionProxy.read(TestModel.TEST_PATH);
1193 public void testReadCompletion(){
1194 completeOperation(new TransactionProxyOperation() {
1196 public void run(TransactionProxy transactionProxy) {
1197 NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1199 doReturn(readDataReply(nodeToRead)).when(mockActorContext).executeOperationAsync(
1200 any(ActorSelection.class), eqReadData());
1202 transactionProxy.read(TestModel.TEST_PATH);
1204 transactionProxy.read(TestModel.TEST_PATH);
1211 public void testReadCompletionForLocalShard(){
1212 final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1213 completeOperationLocal(new TransactionProxyOperation() {
1215 public void run(TransactionProxy transactionProxy) {
1216 transactionProxy.read(TestModel.TEST_PATH);
1218 transactionProxy.read(TestModel.TEST_PATH);
1220 }, createDataTree(nodeToRead));
1225 public void testReadCompletionForLocalShardWhenExceptionOccurs(){
1226 completeOperationLocal(new TransactionProxyOperation() {
1228 public void run(TransactionProxy transactionProxy) {
1229 transactionProxy.read(TestModel.TEST_PATH);
1231 transactionProxy.read(TestModel.TEST_PATH);
1233 }, createDataTree());
1238 public void testExistsThrottlingWhenShardFound(){
1240 throttleOperation(new TransactionProxyOperation() {
1242 public void run(TransactionProxy transactionProxy) {
1243 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1244 any(ActorSelection.class), eqDataExists());
1246 transactionProxy.exists(TestModel.TEST_PATH);
1248 transactionProxy.exists(TestModel.TEST_PATH);
1254 public void testExistsThrottlingWhenShardNotFound(){
1256 completeOperation(new TransactionProxyOperation() {
1258 public void run(TransactionProxy transactionProxy) {
1259 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1260 any(ActorSelection.class), eqDataExists());
1262 transactionProxy.exists(TestModel.TEST_PATH);
1264 transactionProxy.exists(TestModel.TEST_PATH);
1271 public void testExistsCompletion(){
1272 completeOperation(new TransactionProxyOperation() {
1274 public void run(TransactionProxy transactionProxy) {
1275 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
1276 any(ActorSelection.class), eqDataExists());
1278 transactionProxy.exists(TestModel.TEST_PATH);
1280 transactionProxy.exists(TestModel.TEST_PATH);
1287 public void testExistsCompletionForLocalShard(){
1288 final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1289 completeOperationLocal(new TransactionProxyOperation() {
1291 public void run(TransactionProxy transactionProxy) {
1292 transactionProxy.exists(TestModel.TEST_PATH);
1294 transactionProxy.exists(TestModel.TEST_PATH);
1296 }, createDataTree(nodeToRead));
1301 public void testExistsCompletionForLocalShardWhenExceptionOccurs(){
1302 completeOperationLocal(new TransactionProxyOperation() {
1304 public void run(TransactionProxy transactionProxy) {
1305 transactionProxy.exists(TestModel.TEST_PATH);
1307 transactionProxy.exists(TestModel.TEST_PATH);
1309 }, createDataTree());
1313 public void testReadyThrottling(){
1315 throttleOperation(new TransactionProxyOperation() {
1317 public void run(TransactionProxy transactionProxy) {
1318 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1320 expectBatchedModifications(1);
1322 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1324 transactionProxy.ready();
1330 public void testReadyThrottlingWithTwoTransactionContexts(){
1331 throttleOperation(new TransactionProxyOperation() {
1333 public void run(TransactionProxy transactionProxy) {
1334 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1335 NormalizedNode<?, ?> carsNode = ImmutableNodes.containerNode(CarsModel.BASE_QNAME);
1337 expectBatchedModifications(2);
1339 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1340 any(ActorSelection.class), any(ReadyTransaction.class));
1342 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1344 // Trying to write to Cars will cause another transaction context to get created
1345 transactionProxy.write(CarsModel.BASE_PATH, carsNode);
1347 // Now ready should block for both transaction contexts
1348 transactionProxy.ready();
1350 }, 1, true, TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis()) * 2);
1353 private void testModificationOperationBatching(TransactionType type) throws Exception {
1354 int shardBatchedModificationCount = 3;
1355 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1357 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), type);
1359 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1361 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1362 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1364 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1365 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1367 YangInstanceIdentifier writePath3 = TestModel.INNER_LIST_PATH;
1368 NormalizedNode<?, ?> writeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1370 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1371 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1373 YangInstanceIdentifier mergePath2 = TestModel.OUTER_LIST_PATH;
1374 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1376 YangInstanceIdentifier mergePath3 = TestModel.INNER_LIST_PATH;
1377 NormalizedNode<?, ?> mergeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1379 YangInstanceIdentifier deletePath1 = TestModel.TEST_PATH;
1380 YangInstanceIdentifier deletePath2 = TestModel.OUTER_LIST_PATH;
1382 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, type);
1384 transactionProxy.write(writePath1, writeNode1);
1385 transactionProxy.write(writePath2, writeNode2);
1386 transactionProxy.delete(deletePath1);
1387 transactionProxy.merge(mergePath1, mergeNode1);
1388 transactionProxy.merge(mergePath2, mergeNode2);
1389 transactionProxy.write(writePath3, writeNode3);
1390 transactionProxy.merge(mergePath3, mergeNode3);
1391 transactionProxy.delete(deletePath2);
1393 // This sends the last batch.
1394 transactionProxy.ready();
1396 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1397 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1399 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1400 new WriteModification(writePath2, writeNode2), new DeleteModification(deletePath1));
1402 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1403 new MergeModification(mergePath2, mergeNode2), new WriteModification(writePath3, writeNode3));
1405 verifyBatchedModifications(batchedModifications.get(2), true, true,
1406 new MergeModification(mergePath3, mergeNode3), new DeleteModification(deletePath2));
1408 assertEquals("getTotalMessageCount", 3, batchedModifications.get(2).getTotalMessagesSent());
1412 public void testReadWriteModificationOperationBatching() throws Throwable {
1413 testModificationOperationBatching(READ_WRITE);
1417 public void testWriteOnlyModificationOperationBatching() throws Throwable {
1418 testModificationOperationBatching(WRITE_ONLY);
1422 public void testOptimizedWriteOnlyModificationOperationBatching() throws Throwable {
1423 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
1424 testModificationOperationBatching(WRITE_ONLY);
1428 public void testModificationOperationBatchingWithInterleavedReads() throws Throwable {
1430 int shardBatchedModificationCount = 10;
1431 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1433 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
1435 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1437 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1438 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1440 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1441 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1443 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1444 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1446 YangInstanceIdentifier mergePath2 = TestModel.INNER_LIST_PATH;
1447 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1449 YangInstanceIdentifier deletePath = TestModel.OUTER_LIST_PATH;
1451 doReturn(readSerializedDataReply(writeNode2)).when(mockActorContext).executeOperationAsync(
1452 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1454 doReturn(readSerializedDataReply(mergeNode2)).when(mockActorContext).executeOperationAsync(
1455 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1457 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
1458 eq(actorSelection(actorRef)), eqSerializedDataExists());
1460 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
1462 transactionProxy.write(writePath1, writeNode1);
1463 transactionProxy.write(writePath2, writeNode2);
1465 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(writePath2).
1466 get(5, TimeUnit.SECONDS);
1468 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1469 assertEquals("Response NormalizedNode", writeNode2, readOptional.get());
1471 transactionProxy.merge(mergePath1, mergeNode1);
1472 transactionProxy.merge(mergePath2, mergeNode2);
1474 readOptional = transactionProxy.read(mergePath2).get(5, TimeUnit.SECONDS);
1476 transactionProxy.delete(deletePath);
1478 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
1479 assertEquals("Exists response", true, exists);
1481 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1482 assertEquals("Response NormalizedNode", mergeNode2, readOptional.get());
1484 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1485 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1487 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1488 new WriteModification(writePath2, writeNode2));
1490 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1491 new MergeModification(mergePath2, mergeNode2));
1493 verifyBatchedModifications(batchedModifications.get(2), false, new DeleteModification(deletePath));
1495 InOrder inOrder = Mockito.inOrder(mockActorContext);
1496 inOrder.verify(mockActorContext).executeOperationAsync(
1497 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1499 inOrder.verify(mockActorContext).executeOperationAsync(
1500 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1502 inOrder.verify(mockActorContext).executeOperationAsync(
1503 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1505 inOrder.verify(mockActorContext).executeOperationAsync(
1506 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1508 inOrder.verify(mockActorContext).executeOperationAsync(
1509 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1511 inOrder.verify(mockActorContext).executeOperationAsync(
1512 eq(actorSelection(actorRef)), eqSerializedDataExists());
1516 public void testReadRoot() throws ReadFailedException, InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
1518 SchemaContext schemaContext = SchemaContextHelper.full();
1519 Configuration configuration = mock(Configuration.class);
1520 doReturn(configuration).when(mockActorContext).getConfiguration();
1521 doReturn(schemaContext).when(mockActorContext).getSchemaContext();
1522 doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames();
1524 NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1525 NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
1527 setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext));
1528 setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext));
1530 doReturn(memberName).when(mockActorContext).getCurrentMemberName();
1532 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
1534 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
1536 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
1537 YangInstanceIdentifier.builder().build()).get(5, TimeUnit.SECONDS);
1539 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1541 NormalizedNode<?, ?> normalizedNode = readOptional.get();
1543 assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
1545 Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
1547 for(NormalizedNode<?,?> node : collection){
1548 assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
1551 assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
1552 NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null);
1554 assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME));
1556 assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
1557 NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
1559 assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME));
1563 private void setUpReadData(String shardName, NormalizedNode<?, ?> expectedNode) {
1564 ActorSystem actorSystem = getSystem();
1565 ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
1567 doReturn(getSystem().actorSelection(shardActorRef.path())).
1568 when(mockActorContext).actorSelection(shardActorRef.path().toString());
1570 doReturn(primaryShardInfoReply(getSystem(), shardActorRef)).
1571 when(mockActorContext).findPrimaryShardAsync(eq(shardName));
1573 doReturn(true).when(mockActorContext).isPathLocal(shardActorRef.path().toString());
1575 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
1577 doReturn(actorSystem.actorSelection(txActorRef.path())).
1578 when(mockActorContext).actorSelection(txActorRef.path().toString());
1580 doReturn(Futures.successful(createTransactionReply(txActorRef, DataStoreVersions.CURRENT_VERSION))).when(mockActorContext).
1581 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
1582 eqCreateTransaction(memberName, TransactionType.READ_ONLY), any(Timeout.class));
1584 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
1585 eq(actorSelection(txActorRef)), eqSerializedReadData(YangInstanceIdentifier.builder().build()));