2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.eq;
15 import static org.mockito.Matchers.isA;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.never;
18 import static org.mockito.Mockito.timeout;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21 import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_WRITE;
22 import static org.opendaylight.controller.cluster.datastore.TransactionType.WRITE_ONLY;
23 import akka.actor.ActorRef;
24 import akka.util.Timeout;
25 import java.util.concurrent.CountDownLatch;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.atomic.AtomicReference;
28 import java.util.function.Function;
29 import org.junit.Assert;
30 import org.junit.Test;
31 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
32 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
33 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
34 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
35 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
36 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
37 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
38 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
39 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
42 import scala.concurrent.Promise;
44 public class TransactionChainProxyTest extends AbstractTransactionProxyTest {
45 private LocalHistoryIdentifier historyId;
50 historyId = MockIdentifiers.historyIdentifier(TransactionChainProxyTest.class, memberName);
53 @SuppressWarnings("resource")
55 public void testNewReadOnlyTransaction() {
57 DOMStoreTransaction dst = new TransactionChainProxy(mockComponentFactory, historyId).newReadOnlyTransaction();
58 Assert.assertTrue(dst instanceof DOMStoreReadTransaction);
62 @SuppressWarnings("resource")
64 public void testNewReadWriteTransaction() {
65 DOMStoreTransaction dst = new TransactionChainProxy(mockComponentFactory, historyId).newReadWriteTransaction();
66 Assert.assertTrue(dst instanceof DOMStoreReadWriteTransaction);
70 @SuppressWarnings("resource")
72 public void testNewWriteOnlyTransaction() {
73 DOMStoreTransaction dst = new TransactionChainProxy(mockComponentFactory, historyId).newWriteOnlyTransaction();
74 Assert.assertTrue(dst instanceof DOMStoreWriteTransaction);
79 public void testClose() {
80 new TransactionChainProxy(mockComponentFactory, historyId).close();
82 verify(mockActorContext, times(1)).broadcast(any(Function.class), any(Class.class));
86 public void testRateLimitingUsedInReadWriteTxCreation() {
87 try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) {
89 txChainProxy.newReadWriteTransaction();
91 verify(mockActorContext, times(1)).acquireTxCreationPermit();
96 public void testRateLimitingUsedInWriteOnlyTxCreation() {
97 try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) {
99 txChainProxy.newWriteOnlyTransaction();
101 verify(mockActorContext, times(1)).acquireTxCreationPermit();
106 public void testRateLimitingNotUsedInReadOnlyTxCreation() {
107 try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) {
109 txChainProxy.newReadOnlyTransaction();
111 verify(mockActorContext, times(0)).acquireTxCreationPermit();
116 * Tests 2 successive chained write-only transactions and verifies the second transaction isn't
117 * initiated until the first one completes its read future.
120 public void testChainedWriteOnlyTransactions() throws Exception {
121 dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
123 try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) {
125 ActorRef txActorRef1 = setupActorContextWithoutInitialCreateTransaction(getSystem());
127 Promise<Object> batchedReplyPromise1 = akka.dispatch.Futures.promise();
128 doReturn(batchedReplyPromise1.future()).when(mockActorContext).executeOperationAsync(
129 eq(actorSelection(txActorRef1)), isA(BatchedModifications.class), any(Timeout.class));
131 DOMStoreWriteTransaction writeTx1 = txChainProxy.newWriteOnlyTransaction();
133 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
134 writeTx1.write(TestModel.TEST_PATH, writeNode1);
138 verify(mockActorContext, times(1)).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
140 verifyOneBatchedModification(txActorRef1, new WriteModification(TestModel.TEST_PATH, writeNode1), true);
142 ActorRef txActorRef2 = setupActorContextWithoutInitialCreateTransaction(getSystem());
144 expectBatchedModifications(txActorRef2, 1);
146 final NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
148 final DOMStoreWriteTransaction writeTx2 = txChainProxy.newWriteOnlyTransaction();
150 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
151 final CountDownLatch write2Complete = new CountDownLatch(1);
156 writeTx2.write(TestModel.OUTER_LIST_PATH, writeNode2);
157 } catch (Exception e) {
160 write2Complete.countDown();
165 assertEquals("Tx 2 write should've completed", true, write2Complete.await(5, TimeUnit.SECONDS));
167 if (caughtEx.get() != null) {
168 throw caughtEx.get();
172 verify(mockActorContext, times(1)).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
173 } catch (AssertionError e) {
174 fail("Tx 2 should not have initiated until the Tx 1's ready future completed");
177 batchedReplyPromise1.success(readyTxReply(txActorRef1.path().toString()).value().get().get());
179 // Tx 2 should've proceeded to find the primary shard.
180 verify(mockActorContext, timeout(5000).times(2)).findPrimaryShardAsync(
181 eq(DefaultShardStrategy.DEFAULT_SHARD));
186 * Tests 2 successive chained read-write transactions and verifies the second transaction isn't
187 * initiated until the first one completes its read future.
190 public void testChainedReadWriteTransactions() throws Exception {
191 try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) {
193 ActorRef txActorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
195 expectBatchedModifications(txActorRef1, 1);
197 Promise<Object> readyReplyPromise1 = akka.dispatch.Futures.promise();
198 doReturn(readyReplyPromise1.future()).when(mockActorContext).executeOperationAsync(
199 eq(actorSelection(txActorRef1)), isA(BatchedModifications.class), any(Timeout.class));
201 DOMStoreWriteTransaction writeTx1 = txChainProxy.newReadWriteTransaction();
203 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
204 writeTx1.write(TestModel.TEST_PATH, writeNode1);
208 verifyOneBatchedModification(txActorRef1, new WriteModification(TestModel.TEST_PATH, writeNode1), true);
210 String tx2MemberName = "mock-member";
211 ActorRef shardActorRef2 = setupActorContextWithoutInitialCreateTransaction(getSystem());
212 ActorRef txActorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE,
213 DataStoreVersions.CURRENT_VERSION, tx2MemberName, shardActorRef2);
215 expectBatchedModifications(txActorRef2, 1);
217 final NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
219 final DOMStoreWriteTransaction writeTx2 = txChainProxy.newReadWriteTransaction();
221 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
222 final CountDownLatch write2Complete = new CountDownLatch(1);
227 writeTx2.write(TestModel.OUTER_LIST_PATH, writeNode2);
228 } catch (Exception e) {
231 write2Complete.countDown();
236 assertEquals("Tx 2 write should've completed", true, write2Complete.await(5, TimeUnit.SECONDS));
238 if (caughtEx.get() != null) {
239 throw caughtEx.get();
243 verify(mockActorContext, never()).executeOperationAsync(
244 eq(getSystem().actorSelection(shardActorRef2.path())),
245 eqCreateTransaction(tx2MemberName, READ_WRITE));
246 } catch (AssertionError e) {
247 fail("Tx 2 should not have initiated until the Tx 1's ready future completed");
250 readyReplyPromise1.success(readyTxReply(txActorRef1.path().toString()).value().get().get());
252 verify(mockActorContext, timeout(5000)).executeOperationAsync(
253 eq(getSystem().actorSelection(shardActorRef2.path())),
254 eqCreateTransaction(tx2MemberName, READ_WRITE), any(Timeout.class));
258 @Test(expected = IllegalStateException.class)
259 public void testChainedWriteTransactionsWithPreviousTxNotReady() {
260 ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
262 expectBatchedModifications(actorRef, 1);
264 try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) {
266 DOMStoreWriteTransaction writeTx1 = txChainProxy.newWriteOnlyTransaction();
268 NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
269 writeTx1.write(TestModel.TEST_PATH, writeNode1);
271 NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
273 txChainProxy.newWriteOnlyTransaction();