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.exceptions.NoShardLeaderException;
47 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
48 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
49 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
50 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
51 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
52 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
53 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
54 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
55 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
56 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
57 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
58 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
59 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
60 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest;
61 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
62 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
63 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
64 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
65 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
66 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
67 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
68 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
69 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
70 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
71 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
72 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
73 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
74 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
75 import scala.concurrent.Promise;
77 @SuppressWarnings("resource")
78 public class TransactionProxyTest extends AbstractTransactionProxyTest {
80 @SuppressWarnings("serial")
81 static class TestException extends RuntimeException {
84 static interface Invoker {
85 CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception;
89 public void testRead() throws Exception {
90 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
92 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
94 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
95 eq(actorSelection(actorRef)), eqSerializedReadData());
97 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
98 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
100 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
102 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
104 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
105 eq(actorSelection(actorRef)), eqSerializedReadData());
107 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
109 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
111 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
114 @Test(expected = ReadFailedException.class)
115 public void testReadWithInvalidReplyMessageType() throws Exception {
116 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
118 doReturn(Futures.successful(new Object())).when(mockActorContext).
119 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
121 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
123 transactionProxy.read(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
126 @Test(expected = TestException.class)
127 public void testReadWithAsyncRemoteOperatonFailure() throws Throwable {
128 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
130 doReturn(Futures.failed(new TestException())).when(mockActorContext).
131 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
133 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
135 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
138 private void testExceptionOnInitialCreateTransaction(Exception exToThrow, Invoker invoker)
140 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
142 if (exToThrow instanceof PrimaryNotFoundException) {
143 doReturn(Futures.failed(exToThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
145 doReturn(primaryShardInfoReply(getSystem(), actorRef)).
146 when(mockActorContext).findPrimaryShardAsync(anyString());
149 doReturn(Futures.failed(exToThrow)).when(mockActorContext).executeOperationAsync(
150 any(ActorSelection.class), any(), any(Timeout.class));
152 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
154 propagateReadFailedExceptionCause(invoker.invoke(transactionProxy));
157 private void testReadWithExceptionOnInitialCreateTransaction(Exception exToThrow) throws Throwable {
158 testExceptionOnInitialCreateTransaction(exToThrow, new Invoker() {
160 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
161 return proxy.read(TestModel.TEST_PATH);
166 @Test(expected = PrimaryNotFoundException.class)
167 public void testReadWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
168 testReadWithExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"));
171 @Test(expected = TimeoutException.class)
172 public void testReadWhenATimeoutExceptionIsThrown() throws Throwable {
173 testReadWithExceptionOnInitialCreateTransaction(new TimeoutException("test",
174 new Exception("reason")));
177 @Test(expected = TestException.class)
178 public void testReadWhenAnyOtherExceptionIsThrown() throws Throwable {
179 testReadWithExceptionOnInitialCreateTransaction(new TestException());
183 public void testReadWithPriorRecordingOperationSuccessful() throws Throwable {
184 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
186 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
188 expectBatchedModifications(actorRef, 1);
190 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
191 eq(actorSelection(actorRef)), eqSerializedReadData());
193 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
195 transactionProxy.write(TestModel.TEST_PATH, expectedNode);
197 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
198 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
200 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
201 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
203 InOrder inOrder = Mockito.inOrder(mockActorContext);
204 inOrder.verify(mockActorContext).executeOperationAsync(
205 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
207 inOrder.verify(mockActorContext).executeOperationAsync(
208 eq(actorSelection(actorRef)), eqSerializedReadData());
211 @Test(expected=IllegalStateException.class)
212 public void testReadPreConditionCheck() {
213 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
214 transactionProxy.read(TestModel.TEST_PATH);
217 @Test(expected=IllegalArgumentException.class)
218 public void testInvalidCreateTransactionReply() throws Throwable {
219 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
221 doReturn(getSystem().actorSelection(actorRef.path())).when(mockActorContext).
222 actorSelection(actorRef.path().toString());
224 doReturn(primaryShardInfoReply(getSystem(), actorRef)).
225 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
227 doReturn(Futures.successful(new Object())).when(mockActorContext).executeOperationAsync(
228 eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_ONLY),
231 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
233 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
237 public void testExists() throws Exception {
238 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
240 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
242 doReturn(dataExistsSerializedReply(false)).when(mockActorContext).executeOperationAsync(
243 eq(actorSelection(actorRef)), eqSerializedDataExists());
245 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
247 assertEquals("Exists response", false, exists);
249 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
250 eq(actorSelection(actorRef)), eqSerializedDataExists());
252 exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
254 assertEquals("Exists response", true, exists);
257 @Test(expected = PrimaryNotFoundException.class)
258 public void testExistsWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
259 testExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"), new Invoker() {
261 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
262 return proxy.exists(TestModel.TEST_PATH);
267 @Test(expected = ReadFailedException.class)
268 public void testExistsWithInvalidReplyMessageType() throws Exception {
269 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
271 doReturn(Futures.successful(new Object())).when(mockActorContext).
272 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
274 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
276 transactionProxy.exists(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
279 @Test(expected = TestException.class)
280 public void testExistsWithAsyncRemoteOperatonFailure() throws Throwable {
281 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
283 doReturn(Futures.failed(new TestException())).when(mockActorContext).
284 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
286 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
288 propagateReadFailedExceptionCause(transactionProxy.exists(TestModel.TEST_PATH));
292 public void testExistsWithPriorRecordingOperationSuccessful() throws Throwable {
293 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
295 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
297 expectBatchedModifications(actorRef, 1);
299 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
300 eq(actorSelection(actorRef)), eqSerializedDataExists());
302 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
304 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
306 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
308 assertEquals("Exists response", true, exists);
310 InOrder inOrder = Mockito.inOrder(mockActorContext);
311 inOrder.verify(mockActorContext).executeOperationAsync(
312 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
314 inOrder.verify(mockActorContext).executeOperationAsync(
315 eq(actorSelection(actorRef)), eqSerializedDataExists());
318 @Test(expected=IllegalStateException.class)
319 public void testExistsPreConditionCheck() {
320 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
321 transactionProxy.exists(TestModel.TEST_PATH);
325 public void testWrite() throws Exception {
326 dataStoreContextBuilder.shardBatchedModificationCount(1);
327 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
329 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
331 expectBatchedModifications(actorRef, 1);
333 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
335 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
337 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), false);
341 public void testWriteAfterAsyncRead() throws Throwable {
342 ActorRef actorRef = setupActorContextWithoutInitialCreateTransaction(getSystem(), DefaultShardStrategy.DEFAULT_SHARD);
344 Promise<Object> createTxPromise = akka.dispatch.Futures.promise();
345 doReturn(createTxPromise).when(mockActorContext).executeOperationAsync(
346 eq(getSystem().actorSelection(actorRef.path())),
347 eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
349 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
350 eq(actorSelection(actorRef)), eqSerializedReadData());
352 expectBatchedModificationsReady(actorRef);
354 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
356 final TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
358 final CountDownLatch readComplete = new CountDownLatch(1);
359 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
360 com.google.common.util.concurrent.Futures.addCallback(transactionProxy.read(TestModel.TEST_PATH),
361 new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
363 public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
365 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
366 } catch (Exception e) {
369 readComplete.countDown();
374 public void onFailure(Throwable t) {
376 readComplete.countDown();
380 createTxPromise.success(createTransactionReply(actorRef, DataStoreVersions.CURRENT_VERSION));
382 Uninterruptibles.awaitUninterruptibly(readComplete, 5, TimeUnit.SECONDS);
384 if(caughtEx.get() != null) {
385 throw caughtEx.get();
388 // This sends the batched modification.
389 transactionProxy.ready();
391 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), true);
394 @Test(expected=IllegalStateException.class)
395 public void testWritePreConditionCheck() {
396 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
397 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
400 @Test(expected=IllegalStateException.class)
401 public void testWriteAfterReadyPreConditionCheck() {
402 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
404 transactionProxy.ready();
406 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
410 public void testMerge() throws Exception {
411 dataStoreContextBuilder.shardBatchedModificationCount(1);
412 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
414 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
416 expectBatchedModifications(actorRef, 1);
418 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
420 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
422 verifyOneBatchedModification(actorRef, new MergeModification(TestModel.TEST_PATH, nodeToWrite), false);
426 public void testDelete() throws Exception {
427 dataStoreContextBuilder.shardBatchedModificationCount(1);
428 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
430 expectBatchedModifications(actorRef, 1);
432 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
434 transactionProxy.delete(TestModel.TEST_PATH);
436 verifyOneBatchedModification(actorRef, new DeleteModification(TestModel.TEST_PATH), false);
440 public void testReadWrite() throws Exception {
441 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
443 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
445 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
446 eq(actorSelection(actorRef)), eqSerializedReadData());
448 expectBatchedModifications(actorRef, 1);
450 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
452 transactionProxy.read(TestModel.TEST_PATH);
454 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
456 transactionProxy.read(TestModel.TEST_PATH);
458 transactionProxy.read(TestModel.TEST_PATH);
460 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
461 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
463 verifyBatchedModifications(batchedModifications.get(0), false,
464 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
468 public void testReadyWithReadWrite() throws Exception {
469 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
471 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
473 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
474 eq(actorSelection(actorRef)), eqSerializedReadData());
476 expectBatchedModificationsReady(actorRef, true);
478 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
480 transactionProxy.read(TestModel.TEST_PATH);
482 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
484 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
486 assertTrue(ready instanceof SingleCommitCohortProxy);
488 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
490 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
491 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
493 verifyBatchedModifications(batchedModifications.get(0), true, true,
494 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
496 assertEquals("getTotalMessageCount", 1, batchedModifications.get(0).getTotalMessagesSent());
500 public void testReadyWithNoModifications() throws Exception {
501 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
503 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
504 eq(actorSelection(actorRef)), eqSerializedReadData());
506 expectBatchedModificationsReady(actorRef, true);
508 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
510 transactionProxy.read(TestModel.TEST_PATH);
512 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
514 assertTrue(ready instanceof SingleCommitCohortProxy);
516 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
518 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
519 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
521 verifyBatchedModifications(batchedModifications.get(0), true, true);
525 public void testReadyWithMultipleShardWrites() throws Exception {
526 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
528 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
530 expectBatchedModificationsReady(actorRef1);
531 expectBatchedModificationsReady(actorRef2);
533 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
535 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
536 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
538 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
540 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
542 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef1),
543 actorSelection(actorRef2));
547 public void testReadyWithWriteOnlyAndLastBatchPending() throws Exception {
548 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
550 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
552 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
554 expectBatchedModificationsReady(actorRef, true);
556 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
558 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
560 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
562 assertTrue(ready instanceof SingleCommitCohortProxy);
564 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
566 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
567 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
569 verifyBatchedModifications(batchedModifications.get(0), true, true,
570 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
572 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
573 isA(ReadyTransaction.SERIALIZABLE_CLASS));
577 public void testReadyWithWriteOnlyAndLastBatchEmpty() throws Exception {
578 dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
579 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
581 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
583 expectBatchedModificationsReady(actorRef, true);
585 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
587 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
589 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
591 assertTrue(ready instanceof SingleCommitCohortProxy);
593 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
595 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
596 assertEquals("Captured BatchedModifications count", 2, batchedModifications.size());
598 verifyBatchedModifications(batchedModifications.get(0), false,
599 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
601 verifyBatchedModifications(batchedModifications.get(1), true, true);
603 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
604 isA(ReadyTransaction.SERIALIZABLE_CLASS));
608 public void testReadyWithReplyFailure() throws Exception {
609 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
611 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
613 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
615 expectFailedBatchedModifications(actorRef);
617 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
619 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
621 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
623 assertTrue(ready instanceof SingleCommitCohortProxy);
625 verifyCohortFutures((SingleCommitCohortProxy)ready, TestException.class);
629 public void testReadyWithDebugContextEnabled() throws Exception {
630 dataStoreContextBuilder.transactionDebugContextEnabled(true);
632 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
634 expectBatchedModificationsReady(actorRef, true);
636 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
638 transactionProxy.merge(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
640 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
642 assertTrue(ready instanceof DebugThreePhaseCommitCohort);
644 verifyCohortFutures((DebugThreePhaseCommitCohort)ready, new CommitTransactionReply().toSerializable());
647 private void testWriteOnlyTxWithFindPrimaryShardFailure(Exception toThrow) throws Exception {
648 doReturn(Futures.failed(toThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
650 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
652 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
654 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
656 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
658 transactionProxy.delete(TestModel.TEST_PATH);
660 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
662 assertTrue(ready instanceof SingleCommitCohortProxy);
664 verifyCohortFutures((SingleCommitCohortProxy)ready, toThrow.getClass());
668 public void testWriteOnlyTxWithPrimaryNotFoundException() throws Exception {
669 testWriteOnlyTxWithFindPrimaryShardFailure(new PrimaryNotFoundException("mock"));
673 public void testWriteOnlyTxWithNotInitializedException() throws Exception {
674 testWriteOnlyTxWithFindPrimaryShardFailure(new NotInitializedException("mock"));
678 public void testWriteOnlyTxWithNoShardLeaderException() throws Exception {
679 testWriteOnlyTxWithFindPrimaryShardFailure(new NoShardLeaderException("mock"));
683 public void testReadyWithInvalidReplyMessageType() throws Exception {
684 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
685 ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
687 ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
689 doReturn(Futures.successful(new Object())).when(mockActorContext).
690 executeOperationAsync(eq(actorSelection(actorRef1)), isA(BatchedModifications.class));
692 expectBatchedModificationsReady(actorRef2);
694 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
696 transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
697 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
699 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
701 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
703 verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef2),
704 IllegalArgumentException.class);
708 public void testGetIdentifier() {
709 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
710 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
712 Object id = transactionProxy.getIdentifier();
713 assertNotNull("getIdentifier returned null", id);
714 assertTrue("Invalid identifier: " + id, id.toString().startsWith(memberName));
718 public void testClose() throws Exception{
719 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
721 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
722 eq(actorSelection(actorRef)), eqSerializedReadData());
724 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
726 transactionProxy.read(TestModel.TEST_PATH);
728 transactionProxy.close();
730 verify(mockActorContext).sendOperationAsync(
731 eq(actorSelection(actorRef)), isA(CloseTransaction.SERIALIZABLE_CLASS));
736 * Method to test a local Tx actor. The Tx paths are matched to decide if the
737 * Tx actor is local or not. This is done by mocking the Tx actor path
738 * and the caller paths and ensuring that the paths have the remote-address format
740 * Note: Since the default akka provider for test is not a RemoteActorRefProvider,
741 * the paths returned for the actors for all the tests are not qualified remote paths.
742 * Hence are treated as non-local/remote actors. In short, all tests except
743 * few below run for remote actors
748 public void testLocalTxActorRead() throws Exception {
749 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
750 doReturn(true).when(mockActorContext).isPathLocal(anyString());
752 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
754 // negative test case with null as the reply
755 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
756 any(ActorSelection.class), eqReadData());
758 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
759 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
761 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
763 // test case with node as read data reply
764 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
766 doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
767 any(ActorSelection.class), eqReadData());
769 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
771 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
773 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
775 // test for local data exists
776 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
777 any(ActorSelection.class), eqDataExists());
779 boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
781 assertEquals("Exists response", true, exists);
785 public void testLocalTxActorReady() throws Exception {
786 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
787 doReturn(true).when(mockActorContext).isPathLocal(anyString());
789 expectBatchedModificationsReady(actorRef, true);
791 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
793 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
794 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
796 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
798 assertTrue(ready instanceof SingleCommitCohortProxy);
800 verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
803 private static interface TransactionProxyOperation {
804 void run(TransactionProxy transactionProxy);
807 private void throttleOperation(TransactionProxyOperation operation) {
808 throttleOperation(operation, 1, true);
811 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound){
812 throttleOperation(operation, outstandingOpsLimit, shardFound, TimeUnit.MILLISECONDS.toNanos(
813 mockActorContext.getDatastoreContext().getOperationTimeoutInMillis()));
816 private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef){
817 return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), DataStoreVersions.CURRENT_VERSION,
818 Optional.<DataTree>absent());
821 private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef, Optional<DataTree> dataTreeOptional){
822 return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), DataStoreVersions.CURRENT_VERSION,
827 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound, long expectedCompletionTime){
828 ActorSystem actorSystem = getSystem();
829 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
831 // Note that we setting batchedModificationCount to one less than what we need because in TransactionProxy
832 // we now allow one extra permit to be allowed for ready
833 doReturn(dataStoreContextBuilder.operationTimeoutInSeconds(2).
834 shardBatchedModificationCount(outstandingOpsLimit-1).build()).when(mockActorContext).getDatastoreContext();
836 doReturn(actorSystem.actorSelection(shardActorRef.path())).
837 when(mockActorContext).actorSelection(shardActorRef.path().toString());
840 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
841 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
842 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
843 when(mockActorContext).findPrimaryShardAsync(eq("cars"));
846 doReturn(Futures.failed(new Exception("not found")))
847 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
850 String actorPath = "akka.tcp://system@127.0.0.1:2550/user/tx-actor";
852 doReturn(incompleteFuture()).when(mockActorContext).
853 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
854 eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
856 doReturn(true).when(mockActorContext).isPathLocal(actorPath);
858 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
860 long start = System.nanoTime();
862 operation.run(transactionProxy);
864 long end = System.nanoTime();
866 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
867 expectedCompletionTime, (end-start)),
868 ((end - start) > expectedCompletionTime) && ((end - start) < expectedCompletionTime*2));
872 private void completeOperation(TransactionProxyOperation operation){
873 completeOperation(operation, true);
876 private void completeOperation(TransactionProxyOperation operation, boolean shardFound){
877 ActorSystem actorSystem = getSystem();
878 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
880 doReturn(actorSystem.actorSelection(shardActorRef.path())).
881 when(mockActorContext).actorSelection(shardActorRef.path().toString());
884 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
885 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
887 doReturn(Futures.failed(new PrimaryNotFoundException("test")))
888 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
891 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
892 String actorPath = txActorRef.path().toString();
893 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
894 setTransactionId("txn-1").setTransactionActorPath(actorPath).
895 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
897 doReturn(actorSystem.actorSelection(actorPath)).when(mockActorContext).actorSelection(actorPath);
899 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
900 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
901 eqCreateTransaction(memberName, READ_WRITE), any(Timeout.class));
903 doReturn(true).when(mockActorContext).isPathLocal(anyString());
905 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
907 long start = System.nanoTime();
909 operation.run(transactionProxy);
911 long end = System.nanoTime();
913 long expected = TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis());
914 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
915 expected, (end-start)), (end - start) <= expected);
918 private void completeOperationLocal(TransactionProxyOperation operation, Optional<DataTree> dataTreeOptional){
919 ActorSystem actorSystem = getSystem();
920 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
922 doReturn(actorSystem.actorSelection(shardActorRef.path())).
923 when(mockActorContext).actorSelection(shardActorRef.path().toString());
925 doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef, dataTreeOptional))).
926 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
928 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
930 long start = System.nanoTime();
932 operation.run(transactionProxy);
934 long end = System.nanoTime();
936 long expected = TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis());
937 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
938 expected, (end-start)), (end - start) <= expected);
941 private Optional<DataTree> createDataTree(){
942 DataTree dataTree = mock(DataTree.class);
943 Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
944 DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
945 DataTreeModification dataTreeModification = mock(DataTreeModification.class);
947 doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
948 doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
950 return dataTreeOptional;
953 private Optional<DataTree> createDataTree(NormalizedNode readResponse){
954 DataTree dataTree = mock(DataTree.class);
955 Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
956 DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
957 DataTreeModification dataTreeModification = mock(DataTreeModification.class);
959 doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
960 doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
961 doReturn(Optional.of(readResponse)).when(dataTreeModification).readNode(any(YangInstanceIdentifier.class));
963 return dataTreeOptional;
968 public void testWriteCompletionForLocalShard(){
969 completeOperationLocal(new TransactionProxyOperation() {
971 public void run(TransactionProxy transactionProxy) {
972 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
974 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
976 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
979 }, createDataTree());
983 public void testWriteThrottlingWhenShardFound(){
984 throttleOperation(new TransactionProxyOperation() {
986 public void run(TransactionProxy transactionProxy) {
987 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
989 expectIncompleteBatchedModifications();
991 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
993 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
999 public void testWriteThrottlingWhenShardNotFound(){
1000 // Confirm that there is no throttling when the Shard is not found
1001 completeOperation(new TransactionProxyOperation() {
1003 public void run(TransactionProxy transactionProxy) {
1004 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1006 expectBatchedModifications(2);
1008 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1010 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1018 public void testWriteCompletion(){
1019 completeOperation(new TransactionProxyOperation() {
1021 public void run(TransactionProxy transactionProxy) {
1022 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1024 expectBatchedModifications(2);
1026 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1028 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1034 public void testMergeThrottlingWhenShardFound(){
1035 throttleOperation(new TransactionProxyOperation() {
1037 public void run(TransactionProxy transactionProxy) {
1038 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1040 expectIncompleteBatchedModifications();
1042 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1044 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1050 public void testMergeThrottlingWhenShardNotFound(){
1051 completeOperation(new TransactionProxyOperation() {
1053 public void run(TransactionProxy transactionProxy) {
1054 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1056 expectBatchedModifications(2);
1058 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1060 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1066 public void testMergeCompletion(){
1067 completeOperation(new TransactionProxyOperation() {
1069 public void run(TransactionProxy transactionProxy) {
1070 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1072 expectBatchedModifications(2);
1074 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1076 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1083 public void testMergeCompletionForLocalShard(){
1084 completeOperationLocal(new TransactionProxyOperation() {
1086 public void run(TransactionProxy transactionProxy) {
1087 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1089 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1091 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1094 }, createDataTree());
1099 public void testDeleteThrottlingWhenShardFound(){
1101 throttleOperation(new TransactionProxyOperation() {
1103 public void run(TransactionProxy transactionProxy) {
1104 expectIncompleteBatchedModifications();
1106 transactionProxy.delete(TestModel.TEST_PATH);
1108 transactionProxy.delete(TestModel.TEST_PATH);
1115 public void testDeleteThrottlingWhenShardNotFound(){
1117 completeOperation(new TransactionProxyOperation() {
1119 public void run(TransactionProxy transactionProxy) {
1120 expectBatchedModifications(2);
1122 transactionProxy.delete(TestModel.TEST_PATH);
1124 transactionProxy.delete(TestModel.TEST_PATH);
1130 public void testDeleteCompletionForLocalShard(){
1131 completeOperationLocal(new TransactionProxyOperation() {
1133 public void run(TransactionProxy transactionProxy) {
1135 transactionProxy.delete(TestModel.TEST_PATH);
1137 transactionProxy.delete(TestModel.TEST_PATH);
1139 }, createDataTree());
1144 public void testDeleteCompletion(){
1145 completeOperation(new TransactionProxyOperation() {
1147 public void run(TransactionProxy transactionProxy) {
1148 expectBatchedModifications(2);
1150 transactionProxy.delete(TestModel.TEST_PATH);
1152 transactionProxy.delete(TestModel.TEST_PATH);
1159 public void testReadThrottlingWhenShardFound(){
1161 throttleOperation(new TransactionProxyOperation() {
1163 public void run(TransactionProxy transactionProxy) {
1164 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1165 any(ActorSelection.class), eqReadData());
1167 transactionProxy.read(TestModel.TEST_PATH);
1169 transactionProxy.read(TestModel.TEST_PATH);
1175 public void testReadThrottlingWhenShardNotFound(){
1177 completeOperation(new TransactionProxyOperation() {
1179 public void run(TransactionProxy transactionProxy) {
1180 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1181 any(ActorSelection.class), eqReadData());
1183 transactionProxy.read(TestModel.TEST_PATH);
1185 transactionProxy.read(TestModel.TEST_PATH);
1192 public void testReadCompletion(){
1193 completeOperation(new TransactionProxyOperation() {
1195 public void run(TransactionProxy transactionProxy) {
1196 NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1198 doReturn(readDataReply(nodeToRead)).when(mockActorContext).executeOperationAsync(
1199 any(ActorSelection.class), eqReadData());
1201 transactionProxy.read(TestModel.TEST_PATH);
1203 transactionProxy.read(TestModel.TEST_PATH);
1210 public void testReadCompletionForLocalShard(){
1211 final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1212 completeOperationLocal(new TransactionProxyOperation() {
1214 public void run(TransactionProxy transactionProxy) {
1215 transactionProxy.read(TestModel.TEST_PATH);
1217 transactionProxy.read(TestModel.TEST_PATH);
1219 }, createDataTree(nodeToRead));
1224 public void testReadCompletionForLocalShardWhenExceptionOccurs(){
1225 completeOperationLocal(new TransactionProxyOperation() {
1227 public void run(TransactionProxy transactionProxy) {
1228 transactionProxy.read(TestModel.TEST_PATH);
1230 transactionProxy.read(TestModel.TEST_PATH);
1232 }, createDataTree());
1237 public void testExistsThrottlingWhenShardFound(){
1239 throttleOperation(new TransactionProxyOperation() {
1241 public void run(TransactionProxy transactionProxy) {
1242 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1243 any(ActorSelection.class), eqDataExists());
1245 transactionProxy.exists(TestModel.TEST_PATH);
1247 transactionProxy.exists(TestModel.TEST_PATH);
1253 public void testExistsThrottlingWhenShardNotFound(){
1255 completeOperation(new TransactionProxyOperation() {
1257 public void run(TransactionProxy transactionProxy) {
1258 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1259 any(ActorSelection.class), eqDataExists());
1261 transactionProxy.exists(TestModel.TEST_PATH);
1263 transactionProxy.exists(TestModel.TEST_PATH);
1270 public void testExistsCompletion(){
1271 completeOperation(new TransactionProxyOperation() {
1273 public void run(TransactionProxy transactionProxy) {
1274 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
1275 any(ActorSelection.class), eqDataExists());
1277 transactionProxy.exists(TestModel.TEST_PATH);
1279 transactionProxy.exists(TestModel.TEST_PATH);
1286 public void testExistsCompletionForLocalShard(){
1287 final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1288 completeOperationLocal(new TransactionProxyOperation() {
1290 public void run(TransactionProxy transactionProxy) {
1291 transactionProxy.exists(TestModel.TEST_PATH);
1293 transactionProxy.exists(TestModel.TEST_PATH);
1295 }, createDataTree(nodeToRead));
1300 public void testExistsCompletionForLocalShardWhenExceptionOccurs(){
1301 completeOperationLocal(new TransactionProxyOperation() {
1303 public void run(TransactionProxy transactionProxy) {
1304 transactionProxy.exists(TestModel.TEST_PATH);
1306 transactionProxy.exists(TestModel.TEST_PATH);
1308 }, createDataTree());
1312 public void testReadyThrottling(){
1314 throttleOperation(new TransactionProxyOperation() {
1316 public void run(TransactionProxy transactionProxy) {
1317 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1319 expectBatchedModifications(1);
1321 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1323 transactionProxy.ready();
1329 public void testReadyThrottlingWithTwoTransactionContexts(){
1330 throttleOperation(new TransactionProxyOperation() {
1332 public void run(TransactionProxy transactionProxy) {
1333 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1334 NormalizedNode<?, ?> carsNode = ImmutableNodes.containerNode(CarsModel.BASE_QNAME);
1336 expectBatchedModifications(2);
1338 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1339 any(ActorSelection.class), any(ReadyTransaction.class));
1341 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1343 // Trying to write to Cars will cause another transaction context to get created
1344 transactionProxy.write(CarsModel.BASE_PATH, carsNode);
1346 // Now ready should block for both transaction contexts
1347 transactionProxy.ready();
1349 }, 1, true, TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis()) * 2);
1352 private void testModificationOperationBatching(TransactionType type) throws Exception {
1353 int shardBatchedModificationCount = 3;
1354 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1356 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), type);
1358 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1360 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1361 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1363 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1364 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1366 YangInstanceIdentifier writePath3 = TestModel.INNER_LIST_PATH;
1367 NormalizedNode<?, ?> writeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1369 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1370 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1372 YangInstanceIdentifier mergePath2 = TestModel.OUTER_LIST_PATH;
1373 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1375 YangInstanceIdentifier mergePath3 = TestModel.INNER_LIST_PATH;
1376 NormalizedNode<?, ?> mergeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1378 YangInstanceIdentifier deletePath1 = TestModel.TEST_PATH;
1379 YangInstanceIdentifier deletePath2 = TestModel.OUTER_LIST_PATH;
1381 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, type);
1383 transactionProxy.write(writePath1, writeNode1);
1384 transactionProxy.write(writePath2, writeNode2);
1385 transactionProxy.delete(deletePath1);
1386 transactionProxy.merge(mergePath1, mergeNode1);
1387 transactionProxy.merge(mergePath2, mergeNode2);
1388 transactionProxy.write(writePath3, writeNode3);
1389 transactionProxy.merge(mergePath3, mergeNode3);
1390 transactionProxy.delete(deletePath2);
1392 // This sends the last batch.
1393 transactionProxy.ready();
1395 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1396 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1398 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1399 new WriteModification(writePath2, writeNode2), new DeleteModification(deletePath1));
1401 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1402 new MergeModification(mergePath2, mergeNode2), new WriteModification(writePath3, writeNode3));
1404 verifyBatchedModifications(batchedModifications.get(2), true, true,
1405 new MergeModification(mergePath3, mergeNode3), new DeleteModification(deletePath2));
1407 assertEquals("getTotalMessageCount", 3, batchedModifications.get(2).getTotalMessagesSent());
1411 public void testReadWriteModificationOperationBatching() throws Throwable {
1412 testModificationOperationBatching(READ_WRITE);
1416 public void testWriteOnlyModificationOperationBatching() throws Throwable {
1417 testModificationOperationBatching(WRITE_ONLY);
1421 public void testOptimizedWriteOnlyModificationOperationBatching() throws Throwable {
1422 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
1423 testModificationOperationBatching(WRITE_ONLY);
1427 public void testModificationOperationBatchingWithInterleavedReads() throws Throwable {
1429 int shardBatchedModificationCount = 10;
1430 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1432 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
1434 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1436 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1437 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1439 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1440 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1442 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1443 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1445 YangInstanceIdentifier mergePath2 = TestModel.INNER_LIST_PATH;
1446 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1448 YangInstanceIdentifier deletePath = TestModel.OUTER_LIST_PATH;
1450 doReturn(readSerializedDataReply(writeNode2)).when(mockActorContext).executeOperationAsync(
1451 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1453 doReturn(readSerializedDataReply(mergeNode2)).when(mockActorContext).executeOperationAsync(
1454 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1456 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
1457 eq(actorSelection(actorRef)), eqSerializedDataExists());
1459 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
1461 transactionProxy.write(writePath1, writeNode1);
1462 transactionProxy.write(writePath2, writeNode2);
1464 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(writePath2).
1465 get(5, TimeUnit.SECONDS);
1467 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1468 assertEquals("Response NormalizedNode", writeNode2, readOptional.get());
1470 transactionProxy.merge(mergePath1, mergeNode1);
1471 transactionProxy.merge(mergePath2, mergeNode2);
1473 readOptional = transactionProxy.read(mergePath2).get(5, TimeUnit.SECONDS);
1475 transactionProxy.delete(deletePath);
1477 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
1478 assertEquals("Exists response", true, exists);
1480 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1481 assertEquals("Response NormalizedNode", mergeNode2, readOptional.get());
1483 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1484 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1486 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1487 new WriteModification(writePath2, writeNode2));
1489 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1490 new MergeModification(mergePath2, mergeNode2));
1492 verifyBatchedModifications(batchedModifications.get(2), false, new DeleteModification(deletePath));
1494 InOrder inOrder = Mockito.inOrder(mockActorContext);
1495 inOrder.verify(mockActorContext).executeOperationAsync(
1496 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1498 inOrder.verify(mockActorContext).executeOperationAsync(
1499 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1501 inOrder.verify(mockActorContext).executeOperationAsync(
1502 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1504 inOrder.verify(mockActorContext).executeOperationAsync(
1505 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1507 inOrder.verify(mockActorContext).executeOperationAsync(
1508 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1510 inOrder.verify(mockActorContext).executeOperationAsync(
1511 eq(actorSelection(actorRef)), eqSerializedDataExists());
1515 public void testReadRoot() throws ReadFailedException, InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
1517 SchemaContext schemaContext = SchemaContextHelper.full();
1518 Configuration configuration = mock(Configuration.class);
1519 doReturn(configuration).when(mockActorContext).getConfiguration();
1520 doReturn(schemaContext).when(mockActorContext).getSchemaContext();
1521 doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames();
1523 NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1524 NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
1526 setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext));
1527 setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext));
1529 doReturn(memberName).when(mockActorContext).getCurrentMemberName();
1531 doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
1533 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
1535 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
1536 YangInstanceIdentifier.builder().build()).get(5, TimeUnit.SECONDS);
1538 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1540 NormalizedNode<?, ?> normalizedNode = readOptional.get();
1542 assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
1544 Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
1546 for(NormalizedNode<?,?> node : collection){
1547 assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
1550 assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
1551 NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null);
1553 assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME));
1555 assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
1556 NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
1558 assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME));
1562 private void setUpReadData(String shardName, NormalizedNode<?, ?> expectedNode) {
1563 ActorSystem actorSystem = getSystem();
1564 ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
1566 doReturn(getSystem().actorSelection(shardActorRef.path())).
1567 when(mockActorContext).actorSelection(shardActorRef.path().toString());
1569 doReturn(primaryShardInfoReply(getSystem(), shardActorRef)).
1570 when(mockActorContext).findPrimaryShardAsync(eq(shardName));
1572 doReturn(true).when(mockActorContext).isPathLocal(shardActorRef.path().toString());
1574 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
1576 doReturn(actorSystem.actorSelection(txActorRef.path())).
1577 when(mockActorContext).actorSelection(txActorRef.path().toString());
1579 doReturn(Futures.successful(createTransactionReply(txActorRef, DataStoreVersions.CURRENT_VERSION))).when(mockActorContext).
1580 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
1581 eqCreateTransaction(memberName, TransactionType.READ_ONLY), any(Timeout.class));
1583 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
1584 eq(actorSelection(txActorRef)), eqSerializedReadData(YangInstanceIdentifier.builder().build()));