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.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Mockito.doThrow;
16 import akka.actor.ActorRef;
17 import akka.actor.Props;
18 import akka.actor.Status.Failure;
19 import akka.actor.Terminated;
20 import akka.testkit.JavaTestKit;
21 import akka.testkit.TestActorRef;
22 import java.util.concurrent.TimeUnit;
23 import org.junit.Ignore;
24 import org.junit.Test;
25 import org.mockito.InOrder;
26 import org.mockito.Mockito;
27 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
28 import org.opendaylight.controller.cluster.datastore.exceptions.UnknownMessageException;
29 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
30 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
31 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
32 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
33 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
34 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
35 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
36 import org.opendaylight.controller.cluster.datastore.messages.CreateSnapshot;
37 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
38 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
39 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
40 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
41 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
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.utils.SerializationUtils;
46 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
47 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
52 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
53 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
54 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
55 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
57 public class ShardTransactionTest extends AbstractActorTest {
59 private static final SchemaContext testSchemaContext = TestModel.createTestContext();
60 private static final TransactionType RO = TransactionType.READ_ONLY;
61 private static final TransactionType RW = TransactionType.READ_WRITE;
62 private static final TransactionType WO = TransactionType.WRITE_ONLY;
64 private static final ShardIdentifier SHARD_IDENTIFIER =
65 ShardIdentifier.create("inventory", MEMBER_NAME, "config");
68 private DatastoreContext datastoreContext = DatastoreContext.newBuilder().build();
70 private final ShardStats shardStats = new ShardStats(SHARD_IDENTIFIER.toString(), "DataStore");
72 private final ShardDataTree store = new ShardDataTree(testSchemaContext, TreeType.OPERATIONAL);
74 private ActorRef createShard() {
75 ActorRef shard = getSystem().actorOf(Shard.builder().id(SHARD_IDENTIFIER).datastoreContext(datastoreContext).
76 schemaContext(TestModel.createTestContext()).props());
77 ShardTestKit.waitUntilLeader(shard);
81 private ActorRef newTransactionActor(TransactionType type, AbstractShardDataTreeTransaction<?> transaction, String name) {
82 return newTransactionActor(type, transaction, null, name);
85 private ActorRef newTransactionActor(TransactionType type, AbstractShardDataTreeTransaction<?> transaction, ActorRef shard, String name) {
86 Props props = ShardTransaction.props(type, transaction, shard != null ? shard : createShard(),
87 datastoreContext, shardStats);
88 return getSystem().actorOf(props, name);
91 private ReadOnlyShardDataTreeTransaction readOnlyTransaction() {
92 return store.newReadOnlyTransaction(nextTransactionId());
95 private ReadWriteShardDataTreeTransaction readWriteTransaction() {
96 return store.newReadWriteTransaction(nextTransactionId());
100 public void testOnReceiveReadData() throws Exception {
101 new JavaTestKit(getSystem()) {{
102 final ActorRef shard = createShard();
104 testOnReceiveReadData(newTransactionActor(RO, readOnlyTransaction(), shard, "testReadDataRO"));
106 testOnReceiveReadData(newTransactionActor(RW, readWriteTransaction(), shard, "testReadDataRW"));
109 private void testOnReceiveReadData(final ActorRef transaction) {
110 transaction.tell(new ReadData(YangInstanceIdentifier.EMPTY,
111 DataStoreVersions.CURRENT_VERSION), getRef());
113 ReadDataReply reply = expectMsgClass(duration("5 seconds"), ReadDataReply.class);
115 assertNotNull(reply.getNormalizedNode());
120 public void testOnReceiveReadDataWhenDataNotFound() throws Exception {
121 new JavaTestKit(getSystem()) {{
122 final ActorRef shard = createShard();
124 testOnReceiveReadDataWhenDataNotFound(newTransactionActor(
125 RO, readOnlyTransaction(), shard, "testReadDataWhenDataNotFoundRO"));
127 testOnReceiveReadDataWhenDataNotFound(newTransactionActor(
128 RW, readWriteTransaction(), shard, "testReadDataWhenDataNotFoundRW"));
131 private void testOnReceiveReadDataWhenDataNotFound(final ActorRef transaction) {
132 transaction.tell(new ReadData(TestModel.TEST_PATH, DataStoreVersions.CURRENT_VERSION),getRef());
134 ReadDataReply reply = expectMsgClass(duration("5 seconds"), ReadDataReply.class);
136 assertTrue(reply.getNormalizedNode() == null);
141 public void testOnReceiveDataExistsPositive() throws Exception {
142 new JavaTestKit(getSystem()) {{
143 final ActorRef shard = createShard();
145 testOnReceiveDataExistsPositive(newTransactionActor(RO, readOnlyTransaction(), shard,
146 "testDataExistsPositiveRO"));
148 testOnReceiveDataExistsPositive(newTransactionActor(RW, readWriteTransaction(), shard,
149 "testDataExistsPositiveRW"));
152 private void testOnReceiveDataExistsPositive(final ActorRef transaction) {
153 transaction.tell(new DataExists(YangInstanceIdentifier.EMPTY,
154 DataStoreVersions.CURRENT_VERSION), getRef());
156 DataExistsReply reply = expectMsgClass(duration("5 seconds"), DataExistsReply.class);
158 assertTrue(reply.exists());
163 public void testOnReceiveDataExistsNegative() throws Exception {
164 new JavaTestKit(getSystem()) {{
165 final ActorRef shard = createShard();
167 testOnReceiveDataExistsNegative(newTransactionActor(RO, readOnlyTransaction(), shard,
168 "testDataExistsNegativeRO"));
170 testOnReceiveDataExistsNegative(newTransactionActor(RW, readWriteTransaction(), shard,
171 "testDataExistsNegativeRW"));
174 private void testOnReceiveDataExistsNegative(final ActorRef transaction) {
175 transaction.tell(new DataExists(TestModel.TEST_PATH, DataStoreVersions.CURRENT_VERSION),getRef());
177 DataExistsReply reply = expectMsgClass(duration("5 seconds"), DataExistsReply.class);
179 assertFalse(reply.exists());
184 public void testOnReceiveBatchedModifications() throws Exception {
185 new JavaTestKit(getSystem()) {{
187 ShardDataTreeTransactionParent parent = Mockito.mock(ShardDataTreeTransactionParent.class);
188 DataTreeModification mockModification = Mockito.mock(DataTreeModification.class);
189 ReadWriteShardDataTreeTransaction mockWriteTx = new ReadWriteShardDataTreeTransaction(parent,
190 nextTransactionId(), mockModification);
191 final ActorRef transaction = newTransactionActor(RW, mockWriteTx, "testOnReceiveBatchedModifications");
193 YangInstanceIdentifier writePath = TestModel.TEST_PATH;
194 NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
195 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
196 withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
198 YangInstanceIdentifier mergePath = TestModel.OUTER_LIST_PATH;
199 NormalizedNode<?, ?> mergeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
200 new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME)).build();
202 YangInstanceIdentifier deletePath = TestModel.TEST_PATH;
204 BatchedModifications batched = new BatchedModifications(nextTransactionId(), DataStoreVersions.CURRENT_VERSION);
205 batched.addModification(new WriteModification(writePath, writeData));
206 batched.addModification(new MergeModification(mergePath, mergeData));
207 batched.addModification(new DeleteModification(deletePath));
209 transaction.tell(batched, getRef());
211 BatchedModificationsReply reply = expectMsgClass(duration("5 seconds"), BatchedModificationsReply.class);
212 assertEquals("getNumBatched", 3, reply.getNumBatched());
214 InOrder inOrder = Mockito.inOrder(mockModification);
215 inOrder.verify(mockModification).write(writePath, writeData);
216 inOrder.verify(mockModification).merge(mergePath, mergeData);
217 inOrder.verify(mockModification).delete(deletePath);
222 public void testOnReceiveBatchedModificationsReadyWithoutImmediateCommit() throws Exception {
223 new JavaTestKit(getSystem()) {{
225 final ActorRef transaction = newTransactionActor(WO, readWriteTransaction(),
226 "testOnReceiveBatchedModificationsReadyWithoutImmediateCommit");
228 JavaTestKit watcher = new JavaTestKit(getSystem());
229 watcher.watch(transaction);
231 YangInstanceIdentifier writePath = TestModel.TEST_PATH;
232 NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
233 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
234 withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
236 final TransactionIdentifier tx1 = nextTransactionId();
237 BatchedModifications batched = new BatchedModifications(tx1, DataStoreVersions.CURRENT_VERSION);
238 batched.addModification(new WriteModification(writePath, writeData));
240 transaction.tell(batched, getRef());
241 BatchedModificationsReply reply = expectMsgClass(duration("5 seconds"), BatchedModificationsReply.class);
242 assertEquals("getNumBatched", 1, reply.getNumBatched());
244 batched = new BatchedModifications(tx1, DataStoreVersions.CURRENT_VERSION);
245 batched.setReady(true);
246 batched.setTotalMessagesSent(2);
248 transaction.tell(batched, getRef());
249 expectMsgClass(duration("5 seconds"), ReadyTransactionReply.class);
250 watcher.expectMsgClass(duration("5 seconds"), Terminated.class);
255 public void testOnReceiveBatchedModificationsReadyWithImmediateCommit() throws Exception {
256 new JavaTestKit(getSystem()) {{
258 final ActorRef transaction = newTransactionActor(WO, readWriteTransaction(),
259 "testOnReceiveBatchedModificationsReadyWithImmediateCommit");
261 JavaTestKit watcher = new JavaTestKit(getSystem());
262 watcher.watch(transaction);
264 YangInstanceIdentifier writePath = TestModel.TEST_PATH;
265 NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
266 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
267 withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
269 BatchedModifications batched = new BatchedModifications(nextTransactionId(), DataStoreVersions.CURRENT_VERSION);
270 batched.addModification(new WriteModification(writePath, writeData));
271 batched.setReady(true);
272 batched.setDoCommitOnReady(true);
273 batched.setTotalMessagesSent(1);
275 transaction.tell(batched, getRef());
276 expectMsgClass(duration("5 seconds"), CommitTransactionReply.class);
277 watcher.expectMsgClass(duration("5 seconds"), Terminated.class);
281 @Test(expected=TestException.class)
282 public void testOnReceiveBatchedModificationsFailure() throws Throwable {
283 new JavaTestKit(getSystem()) {{
285 ShardDataTreeTransactionParent parent = Mockito.mock(ShardDataTreeTransactionParent.class);
286 DataTreeModification mockModification = Mockito.mock(DataTreeModification.class);
287 ReadWriteShardDataTreeTransaction mockWriteTx = new ReadWriteShardDataTreeTransaction(parent,
288 nextTransactionId(), mockModification);
289 final ActorRef transaction = newTransactionActor(RW, mockWriteTx,
290 "testOnReceiveBatchedModificationsFailure");
292 JavaTestKit watcher = new JavaTestKit(getSystem());
293 watcher.watch(transaction);
295 YangInstanceIdentifier path = TestModel.TEST_PATH;
296 ContainerNode node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
298 doThrow(new TestException()).when(mockModification).write(path, node);
300 final TransactionIdentifier tx1 = nextTransactionId();
301 BatchedModifications batched = new BatchedModifications(tx1, DataStoreVersions.CURRENT_VERSION);
302 batched.addModification(new WriteModification(path, node));
304 transaction.tell(batched, getRef());
305 expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
307 batched = new BatchedModifications(tx1, DataStoreVersions.CURRENT_VERSION);
308 batched.setReady(true);
309 batched.setTotalMessagesSent(2);
311 transaction.tell(batched, getRef());
312 Failure failure = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
313 watcher.expectMsgClass(duration("5 seconds"), Terminated.class);
315 if(failure != null) {
316 throw failure.cause();
321 @Test(expected=IllegalStateException.class)
322 public void testOnReceiveBatchedModificationsReadyWithIncorrectTotalMessageCount() throws Throwable {
323 new JavaTestKit(getSystem()) {{
325 final ActorRef transaction = newTransactionActor(WO, readWriteTransaction(),
326 "testOnReceiveBatchedModificationsReadyWithIncorrectTotalMessageCount");
328 JavaTestKit watcher = new JavaTestKit(getSystem());
329 watcher.watch(transaction);
331 BatchedModifications batched = new BatchedModifications(nextTransactionId(), DataStoreVersions.CURRENT_VERSION);
332 batched.setReady(true);
333 batched.setTotalMessagesSent(2);
335 transaction.tell(batched, getRef());
337 Failure failure = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
338 watcher.expectMsgClass(duration("5 seconds"), Terminated.class);
340 if(failure != null) {
341 throw failure.cause();
347 public void testOnReceiveCreateSnapshot() throws Exception {
348 new JavaTestKit(getSystem()) {{
349 ShardTest.writeToStore(store.getDataTree(), TestModel.TEST_PATH,
350 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
352 NormalizedNode<?,?> expectedRoot = ShardTest.readStore(store.getDataTree(),
353 YangInstanceIdentifier.EMPTY);
355 final ActorRef transaction = newTransactionActor(TransactionType.READ_ONLY, readOnlyTransaction(),
356 "testOnReceiveCreateSnapshot");
360 transaction.tell(CreateSnapshot.INSTANCE, getRef());
362 CaptureSnapshotReply reply = expectMsgClass(duration("3 seconds"), CaptureSnapshotReply.class);
364 assertNotNull("getSnapshot is null", reply.getSnapshot());
366 NormalizedNode<?,?> actualRoot = SerializationUtils.deserializeNormalizedNode(
367 reply.getSnapshot());
369 assertEquals("Root node", expectedRoot, actualRoot);
371 expectTerminated(duration("3 seconds"), transaction);
376 public void testReadWriteTxOnReceiveCloseTransaction() throws Exception {
377 new JavaTestKit(getSystem()) {{
378 final ActorRef transaction = newTransactionActor(RW, readWriteTransaction(),
379 "testReadWriteTxOnReceiveCloseTransaction");
383 transaction.tell(new CloseTransaction().toSerializable(), getRef());
385 expectMsgClass(duration("3 seconds"), CloseTransactionReply.class);
386 expectTerminated(duration("3 seconds"), transaction);
391 public void testWriteOnlyTxOnReceiveCloseTransaction() throws Exception {
392 new JavaTestKit(getSystem()) {{
393 final ActorRef transaction = newTransactionActor(WO, readWriteTransaction(),
394 "testWriteTxOnReceiveCloseTransaction");
398 transaction.tell(new CloseTransaction().toSerializable(), getRef());
400 expectMsgClass(duration("3 seconds"), CloseTransactionReply.class);
401 expectTerminated(duration("3 seconds"), transaction);
406 public void testReadOnlyTxOnReceiveCloseTransaction() throws Exception {
407 new JavaTestKit(getSystem()) {{
408 final ActorRef transaction = newTransactionActor(TransactionType.READ_ONLY, readOnlyTransaction(),
409 "testReadOnlyTxOnReceiveCloseTransaction");
413 transaction.tell(new CloseTransaction().toSerializable(), getRef());
415 expectMsgClass(duration("3 seconds"), Terminated.class);
419 // Unknown operations are being logged
421 @Test(expected=UnknownMessageException.class)
422 public void testNegativePerformingWriteOperationOnReadTransaction() throws Exception {
423 final ActorRef shard = createShard();
424 final Props props = ShardTransaction.props(TransactionType.READ_ONLY, readOnlyTransaction(), shard,
425 datastoreContext, shardStats);
426 final TestActorRef<ShardTransaction> transaction = TestActorRef.apply(props,getSystem());
428 transaction.receive(new BatchedModifications(nextTransactionId(), DataStoreVersions.CURRENT_VERSION),
429 ActorRef.noSender());
433 public void testShardTransactionInactivity() {
435 datastoreContext = DatastoreContext.newBuilder().shardTransactionIdleTimeout(
436 500, TimeUnit.MILLISECONDS).build();
438 new JavaTestKit(getSystem()) {{
439 final ActorRef transaction = newTransactionActor(RW, readWriteTransaction(),
440 "testShardTransactionInactivity");
444 expectMsgClass(duration("3 seconds"), Terminated.class);
447 public static class TestException extends RuntimeException {
448 private static final long serialVersionUID = 1L;