1 package org.opendaylight.controller.cluster.datastore;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.junit.Assert.fail;
7 import static org.mockito.Matchers.any;
8 import static org.mockito.Matchers.anyString;
9 import static org.mockito.Matchers.eq;
10 import static org.mockito.Matchers.isA;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.never;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType.READ_ONLY;
16 import static org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType.READ_WRITE;
17 import static org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType.WRITE_ONLY;
18 import akka.actor.ActorRef;
19 import akka.actor.ActorSelection;
20 import akka.actor.ActorSystem;
21 import akka.actor.Props;
22 import akka.dispatch.Futures;
23 import com.google.common.base.Optional;
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.List;
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.atomic.AtomicReference;
31 import org.junit.Assert;
32 import org.junit.Test;
33 import org.mockito.InOrder;
34 import org.mockito.Mockito;
35 import org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType;
36 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
37 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
38 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
39 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
40 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
41 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
42 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
43 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
44 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
45 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
46 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
47 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
48 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
49 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
50 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
51 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
54 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
55 import scala.concurrent.Await;
56 import scala.concurrent.Future;
57 import scala.concurrent.Promise;
58 import scala.concurrent.duration.Duration;
60 @SuppressWarnings("resource")
61 public class TransactionProxyTest extends AbstractTransactionProxyTest {
63 @SuppressWarnings("serial")
64 static class TestException extends RuntimeException {
67 static interface Invoker {
68 CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception;
72 public void testRead() throws Exception {
73 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
75 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
77 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
78 eq(actorSelection(actorRef)), eqSerializedReadData());
80 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
81 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
83 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
85 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
87 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
88 eq(actorSelection(actorRef)), eqSerializedReadData());
90 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
92 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
94 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
97 @Test(expected = ReadFailedException.class)
98 public void testReadWithInvalidReplyMessageType() throws Exception {
99 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
101 doReturn(Futures.successful(new Object())).when(mockActorContext).
102 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
104 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
106 transactionProxy.read(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
109 @Test(expected = TestException.class)
110 public void testReadWithAsyncRemoteOperatonFailure() throws Throwable {
111 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
113 doReturn(Futures.failed(new TestException())).when(mockActorContext).
114 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
116 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
118 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
121 private void testExceptionOnInitialCreateTransaction(Exception exToThrow, Invoker invoker)
123 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
125 if (exToThrow instanceof PrimaryNotFoundException) {
126 doReturn(Futures.failed(exToThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
128 doReturn(Futures.successful(getSystem().actorSelection(actorRef.path()))).
129 when(mockActorContext).findPrimaryShardAsync(anyString());
132 doReturn(Futures.failed(exToThrow)).when(mockActorContext).executeOperationAsync(
133 any(ActorSelection.class), any());
135 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
137 propagateReadFailedExceptionCause(invoker.invoke(transactionProxy));
140 private void testReadWithExceptionOnInitialCreateTransaction(Exception exToThrow) throws Throwable {
141 testExceptionOnInitialCreateTransaction(exToThrow, new Invoker() {
143 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
144 return proxy.read(TestModel.TEST_PATH);
149 @Test(expected = PrimaryNotFoundException.class)
150 public void testReadWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
151 testReadWithExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"));
154 @Test(expected = TimeoutException.class)
155 public void testReadWhenATimeoutExceptionIsThrown() throws Throwable {
156 testReadWithExceptionOnInitialCreateTransaction(new TimeoutException("test",
157 new Exception("reason")));
160 @Test(expected = TestException.class)
161 public void testReadWhenAnyOtherExceptionIsThrown() throws Throwable {
162 testReadWithExceptionOnInitialCreateTransaction(new TestException());
165 @Test(expected = TestException.class)
166 public void testReadWithPriorRecordingOperationFailure() throws Throwable {
167 doReturn(dataStoreContextBuilder.shardBatchedModificationCount(2).build()).
168 when(mockActorContext).getDatastoreContext();
170 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
172 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
174 expectFailedBatchedModifications(actorRef);
176 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
177 eq(actorSelection(actorRef)), eqSerializedReadData());
179 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
181 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
183 transactionProxy.delete(TestModel.TEST_PATH);
186 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
188 verify(mockActorContext, times(0)).executeOperationAsync(
189 eq(actorSelection(actorRef)), eqSerializedReadData());
194 public void testReadWithPriorRecordingOperationSuccessful() throws Throwable {
195 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
197 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
199 expectBatchedModifications(actorRef, 1);
201 doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
202 eq(actorSelection(actorRef)), eqSerializedReadData());
204 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
206 transactionProxy.write(TestModel.TEST_PATH, expectedNode);
208 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
209 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
211 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
212 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
214 InOrder inOrder = Mockito.inOrder(mockActorContext);
215 inOrder.verify(mockActorContext).executeOperationAsync(
216 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
218 inOrder.verify(mockActorContext).executeOperationAsync(
219 eq(actorSelection(actorRef)), eqSerializedReadData());
222 @Test(expected=IllegalStateException.class)
223 public void testReadPreConditionCheck() {
224 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
225 transactionProxy.read(TestModel.TEST_PATH);
228 @Test(expected=IllegalArgumentException.class)
229 public void testInvalidCreateTransactionReply() throws Throwable {
230 ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
232 doReturn(getSystem().actorSelection(actorRef.path())).when(mockActorContext).
233 actorSelection(actorRef.path().toString());
235 doReturn(Futures.successful(getSystem().actorSelection(actorRef.path()))).
236 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
238 doReturn(Futures.successful(new Object())).when(mockActorContext).executeOperationAsync(
239 eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_ONLY));
241 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
243 propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
247 public void testExists() throws Exception {
248 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
250 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
252 doReturn(dataExistsSerializedReply(false)).when(mockActorContext).executeOperationAsync(
253 eq(actorSelection(actorRef)), eqSerializedDataExists());
255 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
257 assertEquals("Exists response", false, exists);
259 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
260 eq(actorSelection(actorRef)), eqSerializedDataExists());
262 exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
264 assertEquals("Exists response", true, exists);
267 @Test(expected = PrimaryNotFoundException.class)
268 public void testExistsWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
269 testExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"), new Invoker() {
271 public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
272 return proxy.exists(TestModel.TEST_PATH);
277 @Test(expected = ReadFailedException.class)
278 public void testExistsWithInvalidReplyMessageType() throws Exception {
279 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
281 doReturn(Futures.successful(new Object())).when(mockActorContext).
282 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
284 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext,
287 transactionProxy.exists(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
290 @Test(expected = TestException.class)
291 public void testExistsWithAsyncRemoteOperatonFailure() throws Throwable {
292 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
294 doReturn(Futures.failed(new TestException())).when(mockActorContext).
295 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
297 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
299 propagateReadFailedExceptionCause(transactionProxy.exists(TestModel.TEST_PATH));
302 @Test(expected = TestException.class)
303 public void testExistsWithPriorRecordingOperationFailure() throws Throwable {
304 doReturn(dataStoreContextBuilder.shardBatchedModificationCount(2).build()).
305 when(mockActorContext).getDatastoreContext();
307 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
309 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
311 expectFailedBatchedModifications(actorRef);
313 doReturn(dataExistsSerializedReply(false)).when(mockActorContext).executeOperationAsync(
314 eq(actorSelection(actorRef)), eqSerializedDataExists());
316 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext,
319 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
321 transactionProxy.delete(TestModel.TEST_PATH);
324 propagateReadFailedExceptionCause(transactionProxy.exists(TestModel.TEST_PATH));
326 verify(mockActorContext, times(0)).executeOperationAsync(
327 eq(actorSelection(actorRef)), eqSerializedDataExists());
332 public void testExistsWithPriorRecordingOperationSuccessful() throws Throwable {
333 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
335 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
337 expectBatchedModifications(actorRef, 1);
339 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
340 eq(actorSelection(actorRef)), eqSerializedDataExists());
342 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
344 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
346 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
348 assertEquals("Exists response", true, exists);
350 InOrder inOrder = Mockito.inOrder(mockActorContext);
351 inOrder.verify(mockActorContext).executeOperationAsync(
352 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
354 inOrder.verify(mockActorContext).executeOperationAsync(
355 eq(actorSelection(actorRef)), eqSerializedDataExists());
358 @Test(expected=IllegalStateException.class)
359 public void testExistsPreConditionCheck() {
360 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
361 transactionProxy.exists(TestModel.TEST_PATH);
364 private void verifyRecordingOperationFutures(List<Future<Object>> futures,
365 Class<?>... expResultTypes) throws Exception {
366 assertEquals("getRecordingOperationFutures size", expResultTypes.length, futures.size());
369 for( Future<Object> future: futures) {
370 assertNotNull("Recording operation Future is null", future);
372 Class<?> expResultType = expResultTypes[i++];
373 if(Throwable.class.isAssignableFrom(expResultType)) {
375 Await.result(future, Duration.create(5, TimeUnit.SECONDS));
376 fail("Expected exception from recording operation Future");
377 } catch(Exception e) {
381 assertEquals(String.format("Recording operation %d Future result type", i +1 ), expResultType,
382 Await.result(future, Duration.create(5, TimeUnit.SECONDS)).getClass());
388 public void testWrite() throws Exception {
389 dataStoreContextBuilder.shardBatchedModificationCount(1);
390 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
392 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
394 expectBatchedModifications(actorRef, 1);
396 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
398 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
400 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), false);
404 public void testWriteAfterAsyncRead() throws Throwable {
405 ActorRef actorRef = setupActorContextWithoutInitialCreateTransaction(getSystem());
407 Promise<Object> createTxPromise = akka.dispatch.Futures.promise();
408 doReturn(createTxPromise).when(mockActorContext).executeOperationAsync(
409 eq(getSystem().actorSelection(actorRef.path())),
410 eqCreateTransaction(memberName, READ_WRITE));
412 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
413 eq(actorSelection(actorRef)), eqSerializedReadData());
415 expectBatchedModifications(actorRef, 1);
416 expectReadyTransaction(actorRef);
418 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
420 final TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
422 final CountDownLatch readComplete = new CountDownLatch(1);
423 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
424 com.google.common.util.concurrent.Futures.addCallback(transactionProxy.read(TestModel.TEST_PATH),
425 new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
427 public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
429 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
430 } catch (Exception e) {
433 readComplete.countDown();
438 public void onFailure(Throwable t) {
440 readComplete.countDown();
444 createTxPromise.success(createTransactionReply(actorRef, DataStoreVersions.CURRENT_VERSION));
446 Uninterruptibles.awaitUninterruptibly(readComplete, 5, TimeUnit.SECONDS);
448 if(caughtEx.get() != null) {
449 throw caughtEx.get();
452 // This sends the batched modification.
453 transactionProxy.ready();
455 verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), false);
457 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures(),
458 BatchedModificationsReply.class);
461 @Test(expected=IllegalStateException.class)
462 public void testWritePreConditionCheck() {
463 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_ONLY);
464 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
467 @Test(expected=IllegalStateException.class)
468 public void testWriteAfterReadyPreConditionCheck() {
469 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
471 transactionProxy.ready();
473 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
477 public void testMerge() throws Exception {
478 dataStoreContextBuilder.shardBatchedModificationCount(1);
479 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
481 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
483 expectBatchedModifications(actorRef, 1);
485 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
487 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
489 verifyOneBatchedModification(actorRef, new MergeModification(TestModel.TEST_PATH, nodeToWrite), false);
493 public void testDelete() throws Exception {
494 dataStoreContextBuilder.shardBatchedModificationCount(1);
495 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
497 expectBatchedModifications(actorRef, 1);
499 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
501 transactionProxy.delete(TestModel.TEST_PATH);
503 verifyOneBatchedModification(actorRef, new DeleteModification(TestModel.TEST_PATH), false);
507 public void testReadyWithReadWrite() throws Exception {
508 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
510 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
512 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
513 eq(actorSelection(actorRef)), eqSerializedReadData());
515 expectBatchedModifications(actorRef, 1);
516 expectReadyTransaction(actorRef);
518 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
520 transactionProxy.read(TestModel.TEST_PATH);
522 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
524 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
526 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
528 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
530 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures(),
531 BatchedModificationsReply.class);
533 verifyCohortFutures(proxy, getSystem().actorSelection(actorRef.path()));
535 verify(mockActorContext).executeOperationAsync(eq(actorSelection(actorRef)),
536 isA(BatchedModifications.class));
538 verify(mockActorContext).executeOperationAsync(eq(actorSelection(actorRef)),
539 isA(ReadyTransaction.SERIALIZABLE_CLASS));
543 public void testReadyWithWriteOnlyAndLastBatchPending() throws Exception {
544 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
546 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
548 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
550 expectBatchedModificationsReady(actorRef, 1);
552 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
554 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
556 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
558 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
560 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
562 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures());
564 verifyCohortFutures(proxy, getSystem().actorSelection(actorRef.path()));
566 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
567 assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
569 verifyBatchedModifications(batchedModifications.get(0), 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, 1);
585 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
587 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
589 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
591 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
593 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
595 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures(),
596 BatchedModificationsReply.class);
598 verifyCohortFutures(proxy, getSystem().actorSelection(actorRef.path()));
600 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
601 assertEquals("Captured BatchedModifications count", 2, batchedModifications.size());
603 verifyBatchedModifications(batchedModifications.get(0), false,
604 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
606 verifyBatchedModifications(batchedModifications.get(1), true);
608 verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
609 isA(ReadyTransaction.SERIALIZABLE_CLASS));
613 public void testReadyWithRecordingOperationFailure() throws Exception {
614 dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
616 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
618 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
620 expectFailedBatchedModifications(actorRef);
622 doReturn(false).when(mockActorContext).isPathLocal(actorRef.path().toString());
624 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
626 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
628 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
630 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
632 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
634 verifyCohortFutures(proxy, TestException.class);
636 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures(), TestException.class);
640 public void testReadyWithReplyFailure() throws Exception {
641 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
643 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
645 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
647 expectFailedBatchedModifications(actorRef);
649 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
651 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
653 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
655 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
657 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
659 verifyCohortFutures(proxy, TestException.class);
663 public void testReadyWithInitialCreateTransactionFailure() throws Exception {
665 doReturn(Futures.failed(new PrimaryNotFoundException("mock"))).when(
666 mockActorContext).findPrimaryShardAsync(anyString());
668 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
670 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
672 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
674 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
676 transactionProxy.delete(TestModel.TEST_PATH);
678 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
680 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
682 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
684 verifyCohortFutures(proxy, PrimaryNotFoundException.class);
688 public void testReadyWithInvalidReplyMessageType() throws Exception {
689 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
690 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
692 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
694 //expectBatchedModifications(actorRef, 1);
696 doReturn(Futures.successful(new Object())).when(mockActorContext).
697 executeOperationAsync(eq(actorSelection(actorRef)),
698 isA(BatchedModifications.class));
700 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, WRITE_ONLY);
702 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
704 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
706 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
708 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
710 verifyCohortFutures(proxy, IllegalArgumentException.class);
714 public void testGetIdentifier() {
715 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
716 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext,
717 TransactionProxy.TransactionType.READ_ONLY);
719 Object id = transactionProxy.getIdentifier();
720 assertNotNull("getIdentifier returned null", id);
721 assertTrue("Invalid identifier: " + id, id.toString().startsWith(memberName));
725 public void testClose() throws Exception{
726 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
728 doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
729 eq(actorSelection(actorRef)), eqSerializedReadData());
731 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
733 transactionProxy.read(TestModel.TEST_PATH);
735 transactionProxy.close();
737 verify(mockActorContext).sendOperationAsync(
738 eq(actorSelection(actorRef)), isA(CloseTransaction.SERIALIZABLE_CLASS));
743 * Method to test a local Tx actor. The Tx paths are matched to decide if the
744 * Tx actor is local or not. This is done by mocking the Tx actor path
745 * and the caller paths and ensuring that the paths have the remote-address format
747 * Note: Since the default akka provider for test is not a RemoteActorRefProvider,
748 * the paths returned for the actors for all the tests are not qualified remote paths.
749 * Hence are treated as non-local/remote actors. In short, all tests except
750 * few below run for remote actors
755 public void testLocalTxActorRead() throws Exception {
756 setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
757 doReturn(true).when(mockActorContext).isPathLocal(anyString());
759 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext,READ_ONLY);
761 // negative test case with null as the reply
762 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
763 any(ActorSelection.class), eqReadData());
765 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
766 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
768 assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
770 // test case with node as read data reply
771 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
773 doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
774 any(ActorSelection.class), eqReadData());
776 readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
778 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
780 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
782 // test for local data exists
783 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
784 any(ActorSelection.class), eqDataExists());
786 boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
788 assertEquals("Exists response", true, exists);
792 public void testLocalTxActorReady() throws Exception {
793 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
794 doReturn(true).when(mockActorContext).isPathLocal(anyString());
796 doReturn(batchedModificationsReply(1)).when(mockActorContext).executeOperationAsync(
797 any(ActorSelection.class), isA(BatchedModifications.class));
799 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
801 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
802 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
805 doReturn(readyTxReply(actorRef.path().toString())).when(mockActorContext).executeOperationAsync(
806 eq(actorSelection(actorRef)), isA(ReadyTransaction.class));
808 DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
810 assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
812 ThreePhaseCommitCohortProxy proxy = (ThreePhaseCommitCohortProxy) ready;
814 verifyCohortFutures(proxy, getSystem().actorSelection(actorRef.path()));
817 private static interface TransactionProxyOperation {
818 void run(TransactionProxy transactionProxy);
821 private void throttleOperation(TransactionProxyOperation operation) {
822 throttleOperation(operation, 1, true);
825 private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound){
826 ActorSystem actorSystem = getSystem();
827 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
829 doReturn(outstandingOpsLimit).when(mockActorContext).getTransactionOutstandingOperationLimit();
831 doReturn(actorSystem.actorSelection(shardActorRef.path())).
832 when(mockActorContext).actorSelection(shardActorRef.path().toString());
835 doReturn(Futures.successful(actorSystem.actorSelection(shardActorRef.path()))).
836 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
838 doReturn(Futures.failed(new Exception("not found")))
839 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
842 String actorPath = "akka.tcp://system@127.0.0.1:2550/user/tx-actor";
843 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
844 setTransactionId("txn-1").setTransactionActorPath(actorPath).
845 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
847 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
848 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
849 eqCreateTransaction(memberName, READ_WRITE));
851 doReturn(true).when(mockActorContext).isPathLocal(actorPath);
853 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
855 long start = System.nanoTime();
857 operation.run(transactionProxy);
859 long end = System.nanoTime();
861 long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
862 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
863 expected, (end-start)), (end - start) > expected);
867 private void completeOperation(TransactionProxyOperation operation){
868 completeOperation(operation, true);
871 private void completeOperation(TransactionProxyOperation operation, boolean shardFound){
872 ActorSystem actorSystem = getSystem();
873 ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
875 doReturn(1).when(mockActorContext).getTransactionOutstandingOperationLimit();
877 doReturn(actorSystem.actorSelection(shardActorRef.path())).
878 when(mockActorContext).actorSelection(shardActorRef.path().toString());
881 doReturn(Futures.successful(actorSystem.actorSelection(shardActorRef.path()))).
882 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
884 doReturn(Futures.failed(new PrimaryNotFoundException("test")))
885 .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
888 ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
889 String actorPath = txActorRef.path().toString();
890 CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
891 setTransactionId("txn-1").setTransactionActorPath(actorPath).
892 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
894 doReturn(actorSystem.actorSelection(actorPath)).when(mockActorContext).actorSelection(actorPath);
896 doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
897 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
898 eqCreateTransaction(memberName, READ_WRITE));
900 doReturn(true).when(mockActorContext).isPathLocal(anyString());
902 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
904 long start = System.nanoTime();
906 operation.run(transactionProxy);
908 long end = System.nanoTime();
910 long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
911 Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
912 expected, (end-start)), (end - start) <= expected);
915 public void testWriteThrottling(boolean shardFound){
917 throttleOperation(new TransactionProxyOperation() {
919 public void run(TransactionProxy transactionProxy) {
920 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
922 expectBatchedModifications(2);
924 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
926 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
932 public void testWriteThrottlingWhenShardFound(){
933 dataStoreContextBuilder.shardBatchedModificationCount(1);
934 throttleOperation(new TransactionProxyOperation() {
936 public void run(TransactionProxy transactionProxy) {
937 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
939 expectIncompleteBatchedModifications();
941 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
943 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
949 public void testWriteThrottlingWhenShardNotFound(){
950 // Confirm that there is no throttling when the Shard is not found
951 dataStoreContextBuilder.shardBatchedModificationCount(1);
952 completeOperation(new TransactionProxyOperation() {
954 public void run(TransactionProxy transactionProxy) {
955 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
957 expectBatchedModifications(2);
959 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
961 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
969 public void testWriteCompletion(){
970 dataStoreContextBuilder.shardBatchedModificationCount(1);
971 completeOperation(new TransactionProxyOperation() {
973 public void run(TransactionProxy transactionProxy) {
974 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
976 expectBatchedModifications(2);
978 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
980 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
986 public void testMergeThrottlingWhenShardFound(){
987 dataStoreContextBuilder.shardBatchedModificationCount(1);
988 throttleOperation(new TransactionProxyOperation() {
990 public void run(TransactionProxy transactionProxy) {
991 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
993 expectIncompleteBatchedModifications();
995 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
997 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1003 public void testMergeThrottlingWhenShardNotFound(){
1004 dataStoreContextBuilder.shardBatchedModificationCount(1);
1005 completeOperation(new TransactionProxyOperation() {
1007 public void run(TransactionProxy transactionProxy) {
1008 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1010 expectBatchedModifications(2);
1012 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1014 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1020 public void testMergeCompletion(){
1021 dataStoreContextBuilder.shardBatchedModificationCount(1);
1022 completeOperation(new TransactionProxyOperation() {
1024 public void run(TransactionProxy transactionProxy) {
1025 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1027 expectBatchedModifications(2);
1029 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1031 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1038 public void testDeleteThrottlingWhenShardFound(){
1040 throttleOperation(new TransactionProxyOperation() {
1042 public void run(TransactionProxy transactionProxy) {
1043 expectIncompleteBatchedModifications();
1045 transactionProxy.delete(TestModel.TEST_PATH);
1047 transactionProxy.delete(TestModel.TEST_PATH);
1054 public void testDeleteThrottlingWhenShardNotFound(){
1056 completeOperation(new TransactionProxyOperation() {
1058 public void run(TransactionProxy transactionProxy) {
1059 expectBatchedModifications(2);
1061 transactionProxy.delete(TestModel.TEST_PATH);
1063 transactionProxy.delete(TestModel.TEST_PATH);
1069 public void testDeleteCompletion(){
1070 dataStoreContextBuilder.shardBatchedModificationCount(1);
1071 completeOperation(new TransactionProxyOperation() {
1073 public void run(TransactionProxy transactionProxy) {
1074 expectBatchedModifications(2);
1076 transactionProxy.delete(TestModel.TEST_PATH);
1078 transactionProxy.delete(TestModel.TEST_PATH);
1085 public void testReadThrottlingWhenShardFound(){
1087 throttleOperation(new TransactionProxyOperation() {
1089 public void run(TransactionProxy transactionProxy) {
1090 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1091 any(ActorSelection.class), eqReadData());
1093 transactionProxy.read(TestModel.TEST_PATH);
1095 transactionProxy.read(TestModel.TEST_PATH);
1101 public void testReadThrottlingWhenShardNotFound(){
1103 completeOperation(new TransactionProxyOperation() {
1105 public void run(TransactionProxy transactionProxy) {
1106 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1107 any(ActorSelection.class), eqReadData());
1109 transactionProxy.read(TestModel.TEST_PATH);
1111 transactionProxy.read(TestModel.TEST_PATH);
1118 public void testReadCompletion(){
1119 completeOperation(new TransactionProxyOperation() {
1121 public void run(TransactionProxy transactionProxy) {
1122 NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1124 doReturn(readDataReply(nodeToRead)).when(mockActorContext).executeOperationAsync(
1125 any(ActorSelection.class), eqReadData());
1127 transactionProxy.read(TestModel.TEST_PATH);
1129 transactionProxy.read(TestModel.TEST_PATH);
1136 public void testExistsThrottlingWhenShardFound(){
1138 throttleOperation(new TransactionProxyOperation() {
1140 public void run(TransactionProxy transactionProxy) {
1141 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1142 any(ActorSelection.class), eqDataExists());
1144 transactionProxy.exists(TestModel.TEST_PATH);
1146 transactionProxy.exists(TestModel.TEST_PATH);
1152 public void testExistsThrottlingWhenShardNotFound(){
1154 completeOperation(new TransactionProxyOperation() {
1156 public void run(TransactionProxy transactionProxy) {
1157 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1158 any(ActorSelection.class), eqDataExists());
1160 transactionProxy.exists(TestModel.TEST_PATH);
1162 transactionProxy.exists(TestModel.TEST_PATH);
1169 public void testExistsCompletion(){
1170 completeOperation(new TransactionProxyOperation() {
1172 public void run(TransactionProxy transactionProxy) {
1173 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
1174 any(ActorSelection.class), eqDataExists());
1176 transactionProxy.exists(TestModel.TEST_PATH);
1178 transactionProxy.exists(TestModel.TEST_PATH);
1185 public void testReadyThrottling(){
1187 throttleOperation(new TransactionProxyOperation() {
1189 public void run(TransactionProxy transactionProxy) {
1190 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1192 expectBatchedModifications(1);
1194 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1195 any(ActorSelection.class), any(ReadyTransaction.class));
1197 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1199 transactionProxy.ready();
1205 public void testReadyThrottlingWithTwoTransactionContexts(){
1207 throttleOperation(new TransactionProxyOperation() {
1209 public void run(TransactionProxy transactionProxy) {
1210 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1211 NormalizedNode<?, ?> carsNode = ImmutableNodes.containerNode(CarsModel.BASE_QNAME);
1213 expectBatchedModifications(2);
1215 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1216 any(ActorSelection.class), any(ReadyTransaction.class));
1218 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1220 transactionProxy.write(TestModel.TEST_PATH, carsNode);
1222 transactionProxy.ready();
1227 private void testModificationOperationBatching(TransactionType type) throws Exception {
1228 int shardBatchedModificationCount = 3;
1229 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1231 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), type);
1233 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1235 expectReadyTransaction(actorRef);
1237 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1238 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1240 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1241 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1243 YangInstanceIdentifier writePath3 = TestModel.INNER_LIST_PATH;
1244 NormalizedNode<?, ?> writeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1246 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1247 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1249 YangInstanceIdentifier mergePath2 = TestModel.OUTER_LIST_PATH;
1250 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1252 YangInstanceIdentifier mergePath3 = TestModel.INNER_LIST_PATH;
1253 NormalizedNode<?, ?> mergeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1255 YangInstanceIdentifier deletePath1 = TestModel.TEST_PATH;
1256 YangInstanceIdentifier deletePath2 = TestModel.OUTER_LIST_PATH;
1258 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, type);
1260 transactionProxy.write(writePath1, writeNode1);
1261 transactionProxy.write(writePath2, writeNode2);
1262 transactionProxy.delete(deletePath1);
1263 transactionProxy.merge(mergePath1, mergeNode1);
1264 transactionProxy.merge(mergePath2, mergeNode2);
1265 transactionProxy.write(writePath3, writeNode3);
1266 transactionProxy.merge(mergePath3, mergeNode3);
1267 transactionProxy.delete(deletePath2);
1269 // This sends the last batch.
1270 transactionProxy.ready();
1272 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1273 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1275 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1276 new WriteModification(writePath2, writeNode2), new DeleteModification(deletePath1));
1278 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1279 new MergeModification(mergePath2, mergeNode2), new WriteModification(writePath3, writeNode3));
1281 boolean optimizedWriteOnly = type == WRITE_ONLY && dataStoreContextBuilder.build().isWriteOnlyTransactionOptimizationsEnabled();
1282 verifyBatchedModifications(batchedModifications.get(2), optimizedWriteOnly, new MergeModification(mergePath3, mergeNode3),
1283 new DeleteModification(deletePath2));
1285 if(optimizedWriteOnly) {
1286 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures(),
1287 BatchedModificationsReply.class, BatchedModificationsReply.class);
1289 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures(),
1290 BatchedModificationsReply.class, BatchedModificationsReply.class, BatchedModificationsReply.class);
1295 public void testReadWriteModificationOperationBatching() throws Throwable {
1296 testModificationOperationBatching(READ_WRITE);
1300 public void testWriteOnlyModificationOperationBatching() throws Throwable {
1301 testModificationOperationBatching(WRITE_ONLY);
1305 public void testOptimizedWriteOnlyModificationOperationBatching() throws Throwable {
1306 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
1307 testModificationOperationBatching(WRITE_ONLY);
1311 public void testModificationOperationBatchingWithInterleavedReads() throws Throwable {
1313 int shardBatchedModificationCount = 10;
1314 dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1316 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
1318 expectBatchedModifications(actorRef, shardBatchedModificationCount);
1320 YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1321 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1323 YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1324 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1326 YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1327 NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1329 YangInstanceIdentifier mergePath2 = TestModel.INNER_LIST_PATH;
1330 NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1332 YangInstanceIdentifier deletePath = TestModel.OUTER_LIST_PATH;
1334 doReturn(readSerializedDataReply(writeNode2)).when(mockActorContext).executeOperationAsync(
1335 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1337 doReturn(readSerializedDataReply(mergeNode2)).when(mockActorContext).executeOperationAsync(
1338 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1340 doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
1341 eq(actorSelection(actorRef)), eqSerializedDataExists());
1343 TransactionProxy transactionProxy = new TransactionProxy(mockActorContext, READ_WRITE);
1345 transactionProxy.write(writePath1, writeNode1);
1346 transactionProxy.write(writePath2, writeNode2);
1348 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(writePath2).
1349 get(5, TimeUnit.SECONDS);
1351 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1352 assertEquals("Response NormalizedNode", writeNode2, readOptional.get());
1354 transactionProxy.merge(mergePath1, mergeNode1);
1355 transactionProxy.merge(mergePath2, mergeNode2);
1357 readOptional = transactionProxy.read(mergePath2).get(5, TimeUnit.SECONDS);
1359 transactionProxy.delete(deletePath);
1361 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
1362 assertEquals("Exists response", true, exists);
1364 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1365 assertEquals("Response NormalizedNode", mergeNode2, readOptional.get());
1367 List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1368 assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1370 verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1371 new WriteModification(writePath2, writeNode2));
1373 verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1374 new MergeModification(mergePath2, mergeNode2));
1376 verifyBatchedModifications(batchedModifications.get(2), false, new DeleteModification(deletePath));
1378 InOrder inOrder = Mockito.inOrder(mockActorContext);
1379 inOrder.verify(mockActorContext).executeOperationAsync(
1380 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1382 inOrder.verify(mockActorContext).executeOperationAsync(
1383 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1385 inOrder.verify(mockActorContext).executeOperationAsync(
1386 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1388 inOrder.verify(mockActorContext).executeOperationAsync(
1389 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1391 inOrder.verify(mockActorContext).executeOperationAsync(
1392 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1394 inOrder.verify(mockActorContext).executeOperationAsync(
1395 eq(actorSelection(actorRef)), eqSerializedDataExists());
1397 verifyRecordingOperationFutures(transactionProxy.getRecordedOperationFutures(),
1398 BatchedModificationsReply.class, BatchedModificationsReply.class, BatchedModificationsReply.class);