BUG 3249 : Operation Limiter not release on completion of operation
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / TransactionProxyTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.Matchers.any;
7 import static org.mockito.Matchers.anyString;
8 import static org.mockito.Matchers.eq;
9 import static org.mockito.Matchers.isA;
10 import static org.mockito.Mockito.doReturn;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.never;
13 import static org.mockito.Mockito.verify;
14 import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_ONLY;
15 import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_WRITE;
16 import static org.opendaylight.controller.cluster.datastore.TransactionType.WRITE_ONLY;
17 import akka.actor.ActorRef;
18 import akka.actor.ActorSelection;
19 import akka.actor.ActorSystem;
20 import akka.actor.Props;
21 import akka.dispatch.Futures;
22 import com.google.common.base.Optional;
23 import com.google.common.collect.Sets;
24 import com.google.common.util.concurrent.CheckedFuture;
25 import com.google.common.util.concurrent.FutureCallback;
26 import com.google.common.util.concurrent.Uninterruptibles;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.atomic.AtomicReference;
33 import org.junit.Assert;
34 import org.junit.Test;
35 import org.mockito.InOrder;
36 import org.mockito.Mockito;
37 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
38 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
39 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
40 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
41 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
42 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
43 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
44 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
45 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
46 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
47 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
48 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
49 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
50 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
51 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest;
52 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
53 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
54 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
55 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
56 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
57 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
58 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
59 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
62 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
63 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
64 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
65 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
66 import scala.concurrent.Promise;
67
68 @SuppressWarnings("resource")
69 public class TransactionProxyTest extends AbstractTransactionProxyTest {
70
71     @SuppressWarnings("serial")
72     static class TestException extends RuntimeException {
73     }
74
75     static interface Invoker {
76         CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception;
77     }
78
79     @Test
80     public void testRead() throws Exception {
81         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
82
83         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
84
85         doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
86                 eq(actorSelection(actorRef)), eqSerializedReadData());
87
88         Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
89                 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
90
91         assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
92
93         NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
94
95         doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
96                 eq(actorSelection(actorRef)), eqSerializedReadData());
97
98         readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
99
100         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
101
102         assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
103     }
104
105     @Test(expected = ReadFailedException.class)
106     public void testReadWithInvalidReplyMessageType() throws Exception {
107         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
108
109         doReturn(Futures.successful(new Object())).when(mockActorContext).
110                 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
111
112         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
113
114         transactionProxy.read(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
115     }
116
117     @Test(expected = TestException.class)
118     public void testReadWithAsyncRemoteOperatonFailure() throws Throwable {
119         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
120
121         doReturn(Futures.failed(new TestException())).when(mockActorContext).
122                 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
123
124         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
125
126         propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
127     }
128
129     private void testExceptionOnInitialCreateTransaction(Exception exToThrow, Invoker invoker)
130             throws Throwable {
131         ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
132
133         if (exToThrow instanceof PrimaryNotFoundException) {
134             doReturn(Futures.failed(exToThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
135         } else {
136             doReturn(primaryShardInfoReply(getSystem(), actorRef)).
137                     when(mockActorContext).findPrimaryShardAsync(anyString());
138         }
139
140         doReturn(Futures.failed(exToThrow)).when(mockActorContext).executeOperationAsync(
141                 any(ActorSelection.class), any());
142
143         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
144
145         propagateReadFailedExceptionCause(invoker.invoke(transactionProxy));
146     }
147
148     private void testReadWithExceptionOnInitialCreateTransaction(Exception exToThrow) throws Throwable {
149         testExceptionOnInitialCreateTransaction(exToThrow, new Invoker() {
150             @Override
151             public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
152                 return proxy.read(TestModel.TEST_PATH);
153             }
154         });
155     }
156
157     @Test(expected = PrimaryNotFoundException.class)
158     public void testReadWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
159         testReadWithExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"));
160     }
161
162     @Test(expected = TimeoutException.class)
163     public void testReadWhenATimeoutExceptionIsThrown() throws Throwable {
164         testReadWithExceptionOnInitialCreateTransaction(new TimeoutException("test",
165                 new Exception("reason")));
166     }
167
168     @Test(expected = TestException.class)
169     public void testReadWhenAnyOtherExceptionIsThrown() throws Throwable {
170         testReadWithExceptionOnInitialCreateTransaction(new TestException());
171     }
172
173     @Test
174     public void testReadWithPriorRecordingOperationSuccessful() throws Throwable {
175         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
176
177         NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
178
179         expectBatchedModifications(actorRef, 1);
180
181         doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
182                 eq(actorSelection(actorRef)), eqSerializedReadData());
183
184         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
185
186         transactionProxy.write(TestModel.TEST_PATH, expectedNode);
187
188         Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
189                 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
190
191         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
192         assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
193
194         InOrder inOrder = Mockito.inOrder(mockActorContext);
195         inOrder.verify(mockActorContext).executeOperationAsync(
196                 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
197
198         inOrder.verify(mockActorContext).executeOperationAsync(
199                 eq(actorSelection(actorRef)), eqSerializedReadData());
200     }
201
202     @Test(expected=IllegalStateException.class)
203     public void testReadPreConditionCheck() {
204         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
205         transactionProxy.read(TestModel.TEST_PATH);
206     }
207
208     @Test(expected=IllegalArgumentException.class)
209     public void testInvalidCreateTransactionReply() throws Throwable {
210         ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
211
212         doReturn(getSystem().actorSelection(actorRef.path())).when(mockActorContext).
213             actorSelection(actorRef.path().toString());
214
215         doReturn(primaryShardInfoReply(getSystem(), actorRef)).
216             when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
217
218         doReturn(Futures.successful(new Object())).when(mockActorContext).executeOperationAsync(
219             eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_ONLY));
220
221         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
222
223         propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
224     }
225
226     @Test
227     public void testExists() throws Exception {
228         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
229
230         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
231
232         doReturn(dataExistsSerializedReply(false)).when(mockActorContext).executeOperationAsync(
233                 eq(actorSelection(actorRef)), eqSerializedDataExists());
234
235         Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
236
237         assertEquals("Exists response", false, exists);
238
239         doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
240                 eq(actorSelection(actorRef)), eqSerializedDataExists());
241
242         exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
243
244         assertEquals("Exists response", true, exists);
245     }
246
247     @Test(expected = PrimaryNotFoundException.class)
248     public void testExistsWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
249         testExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"), new Invoker() {
250             @Override
251             public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
252                 return proxy.exists(TestModel.TEST_PATH);
253             }
254         });
255     }
256
257     @Test(expected = ReadFailedException.class)
258     public void testExistsWithInvalidReplyMessageType() throws Exception {
259         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
260
261         doReturn(Futures.successful(new Object())).when(mockActorContext).
262                 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
263
264         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
265
266         transactionProxy.exists(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
267     }
268
269     @Test(expected = TestException.class)
270     public void testExistsWithAsyncRemoteOperatonFailure() throws Throwable {
271         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
272
273         doReturn(Futures.failed(new TestException())).when(mockActorContext).
274                 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
275
276         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
277
278         propagateReadFailedExceptionCause(transactionProxy.exists(TestModel.TEST_PATH));
279     }
280
281     @Test
282     public void testExistsWithPriorRecordingOperationSuccessful() throws Throwable {
283         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
284
285         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
286
287         expectBatchedModifications(actorRef, 1);
288
289         doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
290                 eq(actorSelection(actorRef)), eqSerializedDataExists());
291
292         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
293
294         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
295
296         Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
297
298         assertEquals("Exists response", true, exists);
299
300         InOrder inOrder = Mockito.inOrder(mockActorContext);
301         inOrder.verify(mockActorContext).executeOperationAsync(
302                 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
303
304         inOrder.verify(mockActorContext).executeOperationAsync(
305                 eq(actorSelection(actorRef)), eqSerializedDataExists());
306     }
307
308     @Test(expected=IllegalStateException.class)
309     public void testExistsPreConditionCheck() {
310         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
311         transactionProxy.exists(TestModel.TEST_PATH);
312     }
313
314     @Test
315     public void testWrite() throws Exception {
316         dataStoreContextBuilder.shardBatchedModificationCount(1);
317         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
318
319         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
320
321         expectBatchedModifications(actorRef, 1);
322
323         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
324
325         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
326
327         verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), false);
328     }
329
330     @Test
331     public void testWriteAfterAsyncRead() throws Throwable {
332         ActorRef actorRef = setupActorContextWithoutInitialCreateTransaction(getSystem(), DefaultShardStrategy.DEFAULT_SHARD);
333
334         Promise<Object> createTxPromise = akka.dispatch.Futures.promise();
335         doReturn(createTxPromise).when(mockActorContext).executeOperationAsync(
336                 eq(getSystem().actorSelection(actorRef.path())),
337                 eqCreateTransaction(memberName, READ_WRITE));
338
339         doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
340                 eq(actorSelection(actorRef)), eqSerializedReadData());
341
342         expectBatchedModificationsReady(actorRef);
343
344         final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
345
346         final TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
347
348         final CountDownLatch readComplete = new CountDownLatch(1);
349         final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
350         com.google.common.util.concurrent.Futures.addCallback(transactionProxy.read(TestModel.TEST_PATH),
351                 new  FutureCallback<Optional<NormalizedNode<?, ?>>>() {
352                     @Override
353                     public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
354                         try {
355                             transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
356                         } catch (Exception e) {
357                             caughtEx.set(e);
358                         } finally {
359                             readComplete.countDown();
360                         }
361                     }
362
363                     @Override
364                     public void onFailure(Throwable t) {
365                         caughtEx.set(t);
366                         readComplete.countDown();
367                     }
368                 });
369
370         createTxPromise.success(createTransactionReply(actorRef, DataStoreVersions.CURRENT_VERSION));
371
372         Uninterruptibles.awaitUninterruptibly(readComplete, 5, TimeUnit.SECONDS);
373
374         if(caughtEx.get() != null) {
375             throw caughtEx.get();
376         }
377
378         // This sends the batched modification.
379         transactionProxy.ready();
380
381         verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), true);
382     }
383
384     @Test(expected=IllegalStateException.class)
385     public void testWritePreConditionCheck() {
386         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
387         transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
388     }
389
390     @Test(expected=IllegalStateException.class)
391     public void testWriteAfterReadyPreConditionCheck() {
392         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
393
394         transactionProxy.ready();
395
396         transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
397     }
398
399     @Test
400     public void testMerge() throws Exception {
401         dataStoreContextBuilder.shardBatchedModificationCount(1);
402         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
403
404         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
405
406         expectBatchedModifications(actorRef, 1);
407
408         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
409
410         transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
411
412         verifyOneBatchedModification(actorRef, new MergeModification(TestModel.TEST_PATH, nodeToWrite), false);
413     }
414
415     @Test
416     public void testDelete() throws Exception {
417         dataStoreContextBuilder.shardBatchedModificationCount(1);
418         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
419
420         expectBatchedModifications(actorRef, 1);
421
422         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
423
424         transactionProxy.delete(TestModel.TEST_PATH);
425
426         verifyOneBatchedModification(actorRef, new DeleteModification(TestModel.TEST_PATH), false);
427     }
428
429     @Test
430     public void testReadWrite() throws Exception {
431         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
432
433         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
434
435         doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
436                 eq(actorSelection(actorRef)), eqSerializedReadData());
437
438         expectBatchedModifications(actorRef, 1);
439
440         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
441
442         transactionProxy.read(TestModel.TEST_PATH);
443
444         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
445
446         transactionProxy.read(TestModel.TEST_PATH);
447
448         transactionProxy.read(TestModel.TEST_PATH);
449
450         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
451         assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
452
453         verifyBatchedModifications(batchedModifications.get(0), false,
454                 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
455     }
456
457     @Test
458     public void testReadyWithReadWrite() throws Exception {
459         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
460
461         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
462
463         doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
464                 eq(actorSelection(actorRef)), eqSerializedReadData());
465
466         expectBatchedModificationsReady(actorRef, true);
467
468         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
469
470         transactionProxy.read(TestModel.TEST_PATH);
471
472         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
473
474         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
475
476         assertTrue(ready instanceof SingleCommitCohortProxy);
477
478         verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
479
480         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
481         assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
482
483         verifyBatchedModifications(batchedModifications.get(0), true, true,
484                 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
485
486         assertEquals("getTotalMessageCount", 1, batchedModifications.get(0).getTotalMessagesSent());
487     }
488
489     @Test
490     public void testReadyWithNoModifications() throws Exception {
491         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
492
493         doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
494                 eq(actorSelection(actorRef)), eqSerializedReadData());
495
496         expectBatchedModificationsReady(actorRef, true);
497
498         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
499
500         transactionProxy.read(TestModel.TEST_PATH);
501
502         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
503
504         assertTrue(ready instanceof SingleCommitCohortProxy);
505
506         verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
507
508         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
509         assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
510
511         verifyBatchedModifications(batchedModifications.get(0), true, true);
512     }
513
514     @Test
515     public void testReadyWithMultipleShardWrites() throws Exception {
516         ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
517
518         ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
519
520         expectBatchedModificationsReady(actorRef1);
521         expectBatchedModificationsReady(actorRef2);
522
523         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
524
525         transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
526         transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
527
528         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
529
530         assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
531
532         verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef1),
533                 actorSelection(actorRef2));
534     }
535
536     @Test
537     public void testReadyWithWriteOnlyAndLastBatchPending() throws Exception {
538         dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
539
540         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
541
542         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
543
544         expectBatchedModificationsReady(actorRef, true);
545
546         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
547
548         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
549
550         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
551
552         assertTrue(ready instanceof SingleCommitCohortProxy);
553
554         verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
555
556         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
557         assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
558
559         verifyBatchedModifications(batchedModifications.get(0), true, true,
560                 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
561
562         verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
563                 isA(ReadyTransaction.SERIALIZABLE_CLASS));
564     }
565
566     @Test
567     public void testReadyWithWriteOnlyAndLastBatchEmpty() throws Exception {
568         dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
569         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
570
571         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
572
573         expectBatchedModificationsReady(actorRef, true);
574
575         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
576
577         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
578
579         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
580
581         assertTrue(ready instanceof SingleCommitCohortProxy);
582
583         verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
584
585         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
586         assertEquals("Captured BatchedModifications count", 2, batchedModifications.size());
587
588         verifyBatchedModifications(batchedModifications.get(0), false,
589                 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
590
591         verifyBatchedModifications(batchedModifications.get(1), true, true);
592
593         verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
594                 isA(ReadyTransaction.SERIALIZABLE_CLASS));
595     }
596
597     @Test
598     public void testReadyWithReplyFailure() throws Exception {
599         dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
600
601         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
602
603         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
604
605         expectFailedBatchedModifications(actorRef);
606
607         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
608
609         transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
610
611         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
612
613         assertTrue(ready instanceof SingleCommitCohortProxy);
614
615         verifyCohortFutures((SingleCommitCohortProxy)ready, TestException.class);
616     }
617
618     private void testWriteOnlyTxWithFindPrimaryShardFailure(Exception toThrow) throws Exception {
619         doReturn(Futures.failed(toThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
620
621         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
622
623         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
624
625         transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
626
627         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
628
629         transactionProxy.delete(TestModel.TEST_PATH);
630
631         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
632
633         assertTrue(ready instanceof SingleCommitCohortProxy);
634
635         verifyCohortFutures((SingleCommitCohortProxy)ready, toThrow.getClass());
636     }
637
638     @Test
639     public void testWriteOnlyTxWithPrimaryNotFoundException() throws Exception {
640         testWriteOnlyTxWithFindPrimaryShardFailure(new PrimaryNotFoundException("mock"));
641     }
642
643     @Test
644     public void testWriteOnlyTxWithNotInitializedException() throws Exception {
645         testWriteOnlyTxWithFindPrimaryShardFailure(new NotInitializedException("mock"));
646     }
647
648     @Test
649     public void testWriteOnlyTxWithNoShardLeaderException() throws Exception {
650         testWriteOnlyTxWithFindPrimaryShardFailure(new NoShardLeaderException("mock"));
651     }
652
653     @Test
654     public void testReadyWithInvalidReplyMessageType() throws Exception {
655         dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
656         ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
657
658         ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
659
660         doReturn(Futures.successful(new Object())).when(mockActorContext).
661                 executeOperationAsync(eq(actorSelection(actorRef1)), isA(BatchedModifications.class));
662
663         expectBatchedModificationsReady(actorRef2);
664
665         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
666
667         transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
668         transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
669
670         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
671
672         assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
673
674         verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef2),
675                 IllegalArgumentException.class);
676     }
677
678     @Test
679     public void testGetIdentifier() {
680         setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
681         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
682
683         Object id = transactionProxy.getIdentifier();
684         assertNotNull("getIdentifier returned null", id);
685         assertTrue("Invalid identifier: " + id, id.toString().startsWith(memberName));
686     }
687
688     @Test
689     public void testClose() throws Exception{
690         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
691
692         doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
693                 eq(actorSelection(actorRef)), eqSerializedReadData());
694
695         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
696
697         transactionProxy.read(TestModel.TEST_PATH);
698
699         transactionProxy.close();
700
701         verify(mockActorContext).sendOperationAsync(
702                 eq(actorSelection(actorRef)), isA(CloseTransaction.SERIALIZABLE_CLASS));
703     }
704
705
706     /**
707      * Method to test a local Tx actor. The Tx paths are matched to decide if the
708      * Tx actor is local or not. This is done by mocking the Tx actor path
709      * and the caller paths and ensuring that the paths have the remote-address format
710      *
711      * Note: Since the default akka provider for test is not a RemoteActorRefProvider,
712      * the paths returned for the actors for all the tests are not qualified remote paths.
713      * Hence are treated as non-local/remote actors. In short, all tests except
714      * few below run for remote actors
715      *
716      * @throws Exception
717      */
718     @Test
719     public void testLocalTxActorRead() throws Exception {
720         setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
721         doReturn(true).when(mockActorContext).isPathLocal(anyString());
722
723         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
724
725         // negative test case with null as the reply
726         doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
727             any(ActorSelection.class), eqReadData());
728
729         Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
730             TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
731
732         assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
733
734         // test case with node as read data reply
735         NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
736
737         doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
738             any(ActorSelection.class), eqReadData());
739
740         readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
741
742         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
743
744         assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
745
746         // test for local data exists
747         doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
748             any(ActorSelection.class), eqDataExists());
749
750         boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
751
752         assertEquals("Exists response", true, exists);
753     }
754
755     @Test
756     public void testLocalTxActorReady() throws Exception {
757         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
758         doReturn(true).when(mockActorContext).isPathLocal(anyString());
759
760         expectBatchedModificationsReady(actorRef, true);
761
762         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
763
764         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
765         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
766
767         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
768
769         assertTrue(ready instanceof SingleCommitCohortProxy);
770
771         verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
772     }
773
774     private static interface TransactionProxyOperation {
775         void run(TransactionProxy transactionProxy);
776     }
777
778     private void throttleOperation(TransactionProxyOperation operation) {
779         throttleOperation(operation, 1, true);
780     }
781
782     private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef){
783         return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), Optional.<DataTree>absent());
784     }
785
786     private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef, Optional<DataTree> dataTreeOptional){
787         return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), dataTreeOptional);
788     }
789
790
791     private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound){
792         ActorSystem actorSystem = getSystem();
793         ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
794
795         doReturn(outstandingOpsLimit).when(mockActorContext).getTransactionOutstandingOperationLimit();
796
797         doReturn(actorSystem.actorSelection(shardActorRef.path())).
798                 when(mockActorContext).actorSelection(shardActorRef.path().toString());
799
800         if(shardFound) {
801             doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
802                     when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
803         } else {
804             doReturn(Futures.failed(new Exception("not found")))
805                     .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
806         }
807
808         String actorPath = "akka.tcp://system@127.0.0.1:2550/user/tx-actor";
809         CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
810                 setTransactionId("txn-1").setTransactionActorPath(actorPath).
811                 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
812
813         doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
814                 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
815                         eqCreateTransaction(memberName, READ_WRITE));
816
817         doReturn(true).when(mockActorContext).isPathLocal(actorPath);
818
819         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
820
821         long start = System.nanoTime();
822
823         operation.run(transactionProxy);
824
825         long end = System.nanoTime();
826
827         long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
828         Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
829                 expected, (end-start)), (end - start) > expected);
830
831     }
832
833     private void completeOperation(TransactionProxyOperation operation){
834         completeOperation(operation, true);
835     }
836
837     private void completeOperation(TransactionProxyOperation operation, boolean shardFound){
838         ActorSystem actorSystem = getSystem();
839         ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
840
841         doReturn(1).when(mockActorContext).getTransactionOutstandingOperationLimit();
842
843         doReturn(actorSystem.actorSelection(shardActorRef.path())).
844                 when(mockActorContext).actorSelection(shardActorRef.path().toString());
845
846         if(shardFound) {
847             doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
848                     when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
849         } else {
850             doReturn(Futures.failed(new PrimaryNotFoundException("test")))
851                     .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
852         }
853
854         ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
855         String actorPath = txActorRef.path().toString();
856         CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
857                 setTransactionId("txn-1").setTransactionActorPath(actorPath).
858                 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
859
860         doReturn(actorSystem.actorSelection(actorPath)).when(mockActorContext).actorSelection(actorPath);
861
862         doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
863                 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
864                         eqCreateTransaction(memberName, READ_WRITE));
865
866         doReturn(true).when(mockActorContext).isPathLocal(anyString());
867
868         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
869
870         long start = System.nanoTime();
871
872         operation.run(transactionProxy);
873
874         long end = System.nanoTime();
875
876         long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
877         Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
878                 expected, (end-start)), (end - start) <= expected);
879     }
880
881     private void completeOperationLocal(TransactionProxyOperation operation, Optional<DataTree> dataTreeOptional){
882         ActorSystem actorSystem = getSystem();
883         ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
884
885         doReturn(1).when(mockActorContext).getTransactionOutstandingOperationLimit();
886
887         doReturn(actorSystem.actorSelection(shardActorRef.path())).
888                 when(mockActorContext).actorSelection(shardActorRef.path().toString());
889
890         doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef, dataTreeOptional))).
891                 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
892
893         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
894
895         long start = System.nanoTime();
896
897         operation.run(transactionProxy);
898
899         long end = System.nanoTime();
900
901         long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
902         Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
903                 expected, (end-start)), (end - start) <= expected);
904     }
905
906     private Optional<DataTree> createDataTree(){
907         DataTree dataTree = mock(DataTree.class);
908         Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
909         DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
910         DataTreeModification dataTreeModification = mock(DataTreeModification.class);
911
912         doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
913         doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
914
915         return dataTreeOptional;
916     }
917
918     private Optional<DataTree> createDataTree(NormalizedNode readResponse){
919         DataTree dataTree = mock(DataTree.class);
920         Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
921         DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
922         DataTreeModification dataTreeModification = mock(DataTreeModification.class);
923
924         doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
925         doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
926         doReturn(Optional.of(readResponse)).when(dataTreeModification).readNode(any(YangInstanceIdentifier.class));
927
928         return dataTreeOptional;
929     }
930
931
932     @Test
933     public void testWriteCompletionForLocalShard(){
934         dataStoreContextBuilder.shardBatchedModificationCount(1);
935         completeOperationLocal(new TransactionProxyOperation() {
936             @Override
937             public void run(TransactionProxy transactionProxy) {
938                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
939
940                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
941
942                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
943
944             }
945         }, createDataTree());
946     }
947
948     @Test
949     public void testWriteThrottlingWhenShardFound(){
950         dataStoreContextBuilder.shardBatchedModificationCount(1);
951         throttleOperation(new TransactionProxyOperation() {
952             @Override
953             public void run(TransactionProxy transactionProxy) {
954                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
955
956                 expectIncompleteBatchedModifications();
957
958                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
959
960                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
961             }
962         });
963     }
964
965     @Test
966     public void testWriteThrottlingWhenShardNotFound(){
967         // Confirm that there is no throttling when the Shard is not found
968         dataStoreContextBuilder.shardBatchedModificationCount(1);
969         completeOperation(new TransactionProxyOperation() {
970             @Override
971             public void run(TransactionProxy transactionProxy) {
972                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
973
974                 expectBatchedModifications(2);
975
976                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
977
978                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
979             }
980         }, false);
981
982     }
983
984
985     @Test
986     public void testWriteCompletion(){
987         dataStoreContextBuilder.shardBatchedModificationCount(1);
988         completeOperation(new TransactionProxyOperation() {
989             @Override
990             public void run(TransactionProxy transactionProxy) {
991                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
992
993                 expectBatchedModifications(2);
994
995                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
996
997                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
998             }
999         });
1000     }
1001
1002     @Test
1003     public void testMergeThrottlingWhenShardFound(){
1004         dataStoreContextBuilder.shardBatchedModificationCount(1);
1005         throttleOperation(new TransactionProxyOperation() {
1006             @Override
1007             public void run(TransactionProxy transactionProxy) {
1008                 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1009
1010                 expectIncompleteBatchedModifications();
1011
1012                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1013
1014                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1015             }
1016         });
1017     }
1018
1019     @Test
1020     public void testMergeThrottlingWhenShardNotFound(){
1021         dataStoreContextBuilder.shardBatchedModificationCount(1);
1022         completeOperation(new TransactionProxyOperation() {
1023             @Override
1024             public void run(TransactionProxy transactionProxy) {
1025                 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1026
1027                 expectBatchedModifications(2);
1028
1029                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1030
1031                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1032             }
1033         }, false);
1034     }
1035
1036     @Test
1037     public void testMergeCompletion(){
1038         dataStoreContextBuilder.shardBatchedModificationCount(1);
1039         completeOperation(new TransactionProxyOperation() {
1040             @Override
1041             public void run(TransactionProxy transactionProxy) {
1042                 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1043
1044                 expectBatchedModifications(2);
1045
1046                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1047
1048                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1049             }
1050         });
1051
1052     }
1053
1054     @Test
1055     public void testMergeCompletionForLocalShard(){
1056         dataStoreContextBuilder.shardBatchedModificationCount(1);
1057         completeOperationLocal(new TransactionProxyOperation() {
1058             @Override
1059             public void run(TransactionProxy transactionProxy) {
1060                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1061
1062                 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1063
1064                 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1065
1066             }
1067         }, createDataTree());
1068     }
1069
1070
1071     @Test
1072     public void testDeleteThrottlingWhenShardFound(){
1073
1074         throttleOperation(new TransactionProxyOperation() {
1075             @Override
1076             public void run(TransactionProxy transactionProxy) {
1077                 expectIncompleteBatchedModifications();
1078
1079                 transactionProxy.delete(TestModel.TEST_PATH);
1080
1081                 transactionProxy.delete(TestModel.TEST_PATH);
1082             }
1083         });
1084     }
1085
1086
1087     @Test
1088     public void testDeleteThrottlingWhenShardNotFound(){
1089
1090         completeOperation(new TransactionProxyOperation() {
1091             @Override
1092             public void run(TransactionProxy transactionProxy) {
1093                 expectBatchedModifications(2);
1094
1095                 transactionProxy.delete(TestModel.TEST_PATH);
1096
1097                 transactionProxy.delete(TestModel.TEST_PATH);
1098             }
1099         }, false);
1100     }
1101
1102     @Test
1103     public void testDeleteCompletionForLocalShard(){
1104         dataStoreContextBuilder.shardBatchedModificationCount(1);
1105         completeOperationLocal(new TransactionProxyOperation() {
1106             @Override
1107             public void run(TransactionProxy transactionProxy) {
1108
1109                 transactionProxy.delete(TestModel.TEST_PATH);
1110
1111                 transactionProxy.delete(TestModel.TEST_PATH);
1112             }
1113         }, createDataTree());
1114
1115     }
1116
1117     @Test
1118     public void testDeleteCompletion(){
1119         dataStoreContextBuilder.shardBatchedModificationCount(1);
1120         completeOperation(new TransactionProxyOperation() {
1121             @Override
1122             public void run(TransactionProxy transactionProxy) {
1123                 expectBatchedModifications(2);
1124
1125                 transactionProxy.delete(TestModel.TEST_PATH);
1126
1127                 transactionProxy.delete(TestModel.TEST_PATH);
1128             }
1129         });
1130
1131     }
1132
1133     @Test
1134     public void testReadThrottlingWhenShardFound(){
1135
1136         throttleOperation(new TransactionProxyOperation() {
1137             @Override
1138             public void run(TransactionProxy transactionProxy) {
1139                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1140                         any(ActorSelection.class), eqReadData());
1141
1142                 transactionProxy.read(TestModel.TEST_PATH);
1143
1144                 transactionProxy.read(TestModel.TEST_PATH);
1145             }
1146         });
1147     }
1148
1149     @Test
1150     public void testReadThrottlingWhenShardNotFound(){
1151
1152         completeOperation(new TransactionProxyOperation() {
1153             @Override
1154             public void run(TransactionProxy transactionProxy) {
1155                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1156                         any(ActorSelection.class), eqReadData());
1157
1158                 transactionProxy.read(TestModel.TEST_PATH);
1159
1160                 transactionProxy.read(TestModel.TEST_PATH);
1161             }
1162         }, false);
1163     }
1164
1165
1166     @Test
1167     public void testReadCompletion(){
1168         completeOperation(new TransactionProxyOperation() {
1169             @Override
1170             public void run(TransactionProxy transactionProxy) {
1171                 NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1172
1173                 doReturn(readDataReply(nodeToRead)).when(mockActorContext).executeOperationAsync(
1174                         any(ActorSelection.class), eqReadData());
1175
1176                 transactionProxy.read(TestModel.TEST_PATH);
1177
1178                 transactionProxy.read(TestModel.TEST_PATH);
1179             }
1180         });
1181
1182     }
1183
1184     @Test
1185     public void testReadCompletionForLocalShard(){
1186         final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1187         completeOperationLocal(new TransactionProxyOperation() {
1188             @Override
1189             public void run(TransactionProxy transactionProxy) {
1190                 transactionProxy.read(TestModel.TEST_PATH);
1191
1192                 transactionProxy.read(TestModel.TEST_PATH);
1193             }
1194         }, createDataTree(nodeToRead));
1195
1196     }
1197
1198     @Test
1199     public void testReadCompletionForLocalShardWhenExceptionOccurs(){
1200         completeOperationLocal(new TransactionProxyOperation() {
1201             @Override
1202             public void run(TransactionProxy transactionProxy) {
1203                 transactionProxy.read(TestModel.TEST_PATH);
1204
1205                 transactionProxy.read(TestModel.TEST_PATH);
1206             }
1207         }, createDataTree());
1208
1209     }
1210
1211     @Test
1212     public void testExistsThrottlingWhenShardFound(){
1213
1214         throttleOperation(new TransactionProxyOperation() {
1215             @Override
1216             public void run(TransactionProxy transactionProxy) {
1217                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1218                         any(ActorSelection.class), eqDataExists());
1219
1220                 transactionProxy.exists(TestModel.TEST_PATH);
1221
1222                 transactionProxy.exists(TestModel.TEST_PATH);
1223             }
1224         });
1225     }
1226
1227     @Test
1228     public void testExistsThrottlingWhenShardNotFound(){
1229
1230         completeOperation(new TransactionProxyOperation() {
1231             @Override
1232             public void run(TransactionProxy transactionProxy) {
1233                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1234                         any(ActorSelection.class), eqDataExists());
1235
1236                 transactionProxy.exists(TestModel.TEST_PATH);
1237
1238                 transactionProxy.exists(TestModel.TEST_PATH);
1239             }
1240         }, false);
1241     }
1242
1243
1244     @Test
1245     public void testExistsCompletion(){
1246         completeOperation(new TransactionProxyOperation() {
1247             @Override
1248             public void run(TransactionProxy transactionProxy) {
1249                 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
1250                         any(ActorSelection.class), eqDataExists());
1251
1252                 transactionProxy.exists(TestModel.TEST_PATH);
1253
1254                 transactionProxy.exists(TestModel.TEST_PATH);
1255             }
1256         });
1257
1258     }
1259
1260     @Test
1261     public void testExistsCompletionForLocalShard(){
1262         final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1263         completeOperationLocal(new TransactionProxyOperation() {
1264             @Override
1265             public void run(TransactionProxy transactionProxy) {
1266                 transactionProxy.exists(TestModel.TEST_PATH);
1267
1268                 transactionProxy.exists(TestModel.TEST_PATH);
1269             }
1270         }, createDataTree(nodeToRead));
1271
1272     }
1273
1274     @Test
1275     public void testExistsCompletionForLocalShardWhenExceptionOccurs(){
1276         completeOperationLocal(new TransactionProxyOperation() {
1277             @Override
1278             public void run(TransactionProxy transactionProxy) {
1279                 transactionProxy.exists(TestModel.TEST_PATH);
1280
1281                 transactionProxy.exists(TestModel.TEST_PATH);
1282             }
1283         }, createDataTree());
1284
1285     }
1286     @Test
1287     public void testReadyThrottling(){
1288
1289         throttleOperation(new TransactionProxyOperation() {
1290             @Override
1291             public void run(TransactionProxy transactionProxy) {
1292                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1293
1294                 expectBatchedModifications(1);
1295
1296                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1297                         any(ActorSelection.class), any(ReadyTransaction.class));
1298
1299                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1300
1301                 transactionProxy.ready();
1302             }
1303         });
1304     }
1305
1306     @Test
1307     public void testReadyThrottlingWithTwoTransactionContexts(){
1308
1309         throttleOperation(new TransactionProxyOperation() {
1310             @Override
1311             public void run(TransactionProxy transactionProxy) {
1312                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1313                 NormalizedNode<?, ?> carsNode = ImmutableNodes.containerNode(CarsModel.BASE_QNAME);
1314
1315                 expectBatchedModifications(2);
1316
1317                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1318                         any(ActorSelection.class), any(ReadyTransaction.class));
1319
1320                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1321
1322                 transactionProxy.write(TestModel.TEST_PATH, carsNode);
1323
1324                 transactionProxy.ready();
1325             }
1326         }, 2, true);
1327     }
1328
1329     private void testModificationOperationBatching(TransactionType type) throws Exception {
1330         int shardBatchedModificationCount = 3;
1331         dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1332
1333         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), type);
1334
1335         expectBatchedModifications(actorRef, shardBatchedModificationCount);
1336
1337         YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1338         NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1339
1340         YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1341         NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1342
1343         YangInstanceIdentifier writePath3 = TestModel.INNER_LIST_PATH;
1344         NormalizedNode<?, ?> writeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1345
1346         YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1347         NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1348
1349         YangInstanceIdentifier mergePath2 = TestModel.OUTER_LIST_PATH;
1350         NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1351
1352         YangInstanceIdentifier mergePath3 = TestModel.INNER_LIST_PATH;
1353         NormalizedNode<?, ?> mergeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1354
1355         YangInstanceIdentifier deletePath1 = TestModel.TEST_PATH;
1356         YangInstanceIdentifier deletePath2 = TestModel.OUTER_LIST_PATH;
1357
1358         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, type);
1359
1360         transactionProxy.write(writePath1, writeNode1);
1361         transactionProxy.write(writePath2, writeNode2);
1362         transactionProxy.delete(deletePath1);
1363         transactionProxy.merge(mergePath1, mergeNode1);
1364         transactionProxy.merge(mergePath2, mergeNode2);
1365         transactionProxy.write(writePath3, writeNode3);
1366         transactionProxy.merge(mergePath3, mergeNode3);
1367         transactionProxy.delete(deletePath2);
1368
1369         // This sends the last batch.
1370         transactionProxy.ready();
1371
1372         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1373         assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1374
1375         verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1376                 new WriteModification(writePath2, writeNode2), new DeleteModification(deletePath1));
1377
1378         verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1379                 new MergeModification(mergePath2, mergeNode2), new WriteModification(writePath3, writeNode3));
1380
1381         verifyBatchedModifications(batchedModifications.get(2), true, true,
1382                 new MergeModification(mergePath3, mergeNode3), new DeleteModification(deletePath2));
1383
1384         assertEquals("getTotalMessageCount", 3, batchedModifications.get(2).getTotalMessagesSent());
1385     }
1386
1387     @Test
1388     public void testReadWriteModificationOperationBatching() throws Throwable {
1389         testModificationOperationBatching(READ_WRITE);
1390     }
1391
1392     @Test
1393     public void testWriteOnlyModificationOperationBatching() throws Throwable {
1394         testModificationOperationBatching(WRITE_ONLY);
1395     }
1396
1397     @Test
1398     public void testOptimizedWriteOnlyModificationOperationBatching() throws Throwable {
1399         dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
1400         testModificationOperationBatching(WRITE_ONLY);
1401     }
1402
1403     @Test
1404     public void testModificationOperationBatchingWithInterleavedReads() throws Throwable {
1405
1406         int shardBatchedModificationCount = 10;
1407         dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1408
1409         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
1410
1411         expectBatchedModifications(actorRef, shardBatchedModificationCount);
1412
1413         YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1414         NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1415
1416         YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1417         NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1418
1419         YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1420         NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1421
1422         YangInstanceIdentifier mergePath2 = TestModel.INNER_LIST_PATH;
1423         NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1424
1425         YangInstanceIdentifier deletePath = TestModel.OUTER_LIST_PATH;
1426
1427         doReturn(readSerializedDataReply(writeNode2)).when(mockActorContext).executeOperationAsync(
1428                 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1429
1430         doReturn(readSerializedDataReply(mergeNode2)).when(mockActorContext).executeOperationAsync(
1431                 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1432
1433         doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
1434                 eq(actorSelection(actorRef)), eqSerializedDataExists());
1435
1436         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
1437
1438         transactionProxy.write(writePath1, writeNode1);
1439         transactionProxy.write(writePath2, writeNode2);
1440
1441         Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(writePath2).
1442                 get(5, TimeUnit.SECONDS);
1443
1444         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1445         assertEquals("Response NormalizedNode", writeNode2, readOptional.get());
1446
1447         transactionProxy.merge(mergePath1, mergeNode1);
1448         transactionProxy.merge(mergePath2, mergeNode2);
1449
1450         readOptional = transactionProxy.read(mergePath2).get(5, TimeUnit.SECONDS);
1451
1452         transactionProxy.delete(deletePath);
1453
1454         Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
1455         assertEquals("Exists response", true, exists);
1456
1457         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1458         assertEquals("Response NormalizedNode", mergeNode2, readOptional.get());
1459
1460         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1461         assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1462
1463         verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1464                 new WriteModification(writePath2, writeNode2));
1465
1466         verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1467                 new MergeModification(mergePath2, mergeNode2));
1468
1469         verifyBatchedModifications(batchedModifications.get(2), false, new DeleteModification(deletePath));
1470
1471         InOrder inOrder = Mockito.inOrder(mockActorContext);
1472         inOrder.verify(mockActorContext).executeOperationAsync(
1473                 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1474
1475         inOrder.verify(mockActorContext).executeOperationAsync(
1476                 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1477
1478         inOrder.verify(mockActorContext).executeOperationAsync(
1479                 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1480
1481         inOrder.verify(mockActorContext).executeOperationAsync(
1482                 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1483
1484         inOrder.verify(mockActorContext).executeOperationAsync(
1485                 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1486
1487         inOrder.verify(mockActorContext).executeOperationAsync(
1488                 eq(actorSelection(actorRef)), eqSerializedDataExists());
1489     }
1490
1491     @Test
1492     public void testReadRoot() throws ReadFailedException, InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
1493
1494         SchemaContext schemaContext = SchemaContextHelper.full();
1495         Configuration configuration = mock(Configuration.class);
1496         doReturn(configuration).when(mockActorContext).getConfiguration();
1497         doReturn(schemaContext).when(mockActorContext).getSchemaContext();
1498         doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames();
1499
1500         NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1501         NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
1502
1503         setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext));
1504         setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext));
1505
1506         doReturn(memberName).when(mockActorContext).getCurrentMemberName();
1507
1508         doReturn(10).when(mockActorContext).getTransactionOutstandingOperationLimit();
1509
1510         doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
1511
1512         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
1513
1514         Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
1515                 YangInstanceIdentifier.builder().build()).get(5, TimeUnit.SECONDS);
1516
1517         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1518
1519         NormalizedNode<?, ?> normalizedNode = readOptional.get();
1520
1521         assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
1522
1523         Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
1524
1525         for(NormalizedNode<?,?> node : collection){
1526             assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
1527         }
1528
1529         assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
1530                 NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null);
1531
1532         assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME));
1533
1534         assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
1535                 NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
1536
1537         assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME));
1538     }
1539
1540
1541     private void setUpReadData(String shardName, NormalizedNode<?, ?> expectedNode) {
1542         ActorSystem actorSystem = getSystem();
1543         ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
1544
1545         doReturn(getSystem().actorSelection(shardActorRef.path())).
1546                 when(mockActorContext).actorSelection(shardActorRef.path().toString());
1547
1548         doReturn(primaryShardInfoReply(getSystem(), shardActorRef)).
1549                 when(mockActorContext).findPrimaryShardAsync(eq(shardName));
1550
1551         doReturn(true).when(mockActorContext).isPathLocal(shardActorRef.path().toString());
1552
1553         ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
1554
1555         doReturn(actorSystem.actorSelection(txActorRef.path())).
1556                 when(mockActorContext).actorSelection(txActorRef.path().toString());
1557
1558         doReturn(Futures.successful(createTransactionReply(txActorRef, DataStoreVersions.CURRENT_VERSION))).when(mockActorContext).
1559                 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
1560                         eqCreateTransaction(memberName, TransactionType.READ_ONLY));
1561
1562         doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
1563                 eq(actorSelection(txActorRef)), eqSerializedReadData(YangInstanceIdentifier.builder().build()));
1564     }
1565 }