1 package org.opendaylight.controller.cluster.datastore;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertNull;
7 import static org.junit.Assert.assertSame;
8 import static org.junit.Assert.assertTrue;
9 import static org.mockito.Mockito.doReturn;
10 import static org.mockito.Mockito.inOrder;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.reset;
13 import static org.opendaylight.controller.cluster.datastore.DataStoreVersions.CURRENT_VERSION;
14 import akka.actor.ActorRef;
15 import akka.actor.ActorSelection;
16 import akka.actor.PoisonPill;
17 import akka.actor.Props;
18 import akka.actor.Status.Failure;
19 import akka.dispatch.Dispatchers;
20 import akka.dispatch.OnComplete;
21 import akka.japi.Creator;
22 import akka.pattern.Patterns;
23 import akka.persistence.SaveSnapshotSuccess;
24 import akka.testkit.TestActorRef;
25 import akka.util.Timeout;
26 import com.google.common.base.Function;
27 import com.google.common.base.Optional;
28 import com.google.common.util.concurrent.Futures;
29 import com.google.common.util.concurrent.ListenableFuture;
30 import com.google.common.util.concurrent.Uninterruptibles;
31 import java.io.IOException;
32 import java.util.Collections;
33 import java.util.HashSet;
36 import java.util.concurrent.CountDownLatch;
37 import java.util.concurrent.TimeUnit;
38 import java.util.concurrent.atomic.AtomicBoolean;
39 import java.util.concurrent.atomic.AtomicReference;
40 import org.junit.Test;
41 import org.mockito.InOrder;
42 import org.opendaylight.controller.cluster.DataPersistenceProvider;
43 import org.opendaylight.controller.cluster.DelegatingPersistentDataProvider;
44 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
45 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
46 import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction;
47 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
48 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
49 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
50 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
51 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
52 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
53 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
54 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
55 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
56 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
57 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
58 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
59 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
60 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
61 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
62 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
63 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
64 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
65 import org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged;
66 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
67 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
68 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
69 import org.opendaylight.controller.cluster.datastore.modification.Modification;
70 import org.opendaylight.controller.cluster.datastore.modification.ModificationPayload;
71 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
72 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
73 import org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener;
74 import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
75 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
76 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
77 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply;
78 import org.opendaylight.controller.cluster.raft.RaftActorContext;
79 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
80 import org.opendaylight.controller.cluster.raft.ReplicatedLogImplEntry;
81 import org.opendaylight.controller.cluster.raft.Snapshot;
82 import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries;
83 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
84 import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
85 import org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus;
86 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
87 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
88 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
89 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
90 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
91 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
92 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
93 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
94 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
95 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
96 import org.opendaylight.controller.protobuff.messages.cohort3pc.ThreePhaseCommitCohortMessages;
97 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
98 import org.opendaylight.yangtools.yang.common.QName;
99 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
100 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
101 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
102 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
103 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
104 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
105 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
106 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
107 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
108 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
109 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
110 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
111 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
112 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
113 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
114 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
115 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
116 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
117 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
118 import scala.concurrent.Await;
119 import scala.concurrent.Future;
120 import scala.concurrent.duration.FiniteDuration;
122 public class ShardTest extends AbstractShardTest {
123 private static final QName CARS_QNAME = QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test:cars", "2014-03-13", "cars");
125 private static final String DUMMY_DATA = "Dummy data as snapshot sequence number is set to 0 in InMemorySnapshotStore and journal recovery seq number will start from 1";
127 final CountDownLatch recoveryComplete = new CountDownLatch(1);
129 protected Props newShardPropsWithRecoveryComplete() {
131 final Creator<Shard> creator = new Creator<Shard>() {
133 public Shard create() throws Exception {
134 return new Shard(shardID, Collections.<String,String>emptyMap(),
135 newDatastoreContext(), SCHEMA_CONTEXT) {
137 protected void onRecoveryComplete() {
139 super.onRecoveryComplete();
141 recoveryComplete.countDown();
147 return Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId());
151 public void testRegisterChangeListener() throws Exception {
152 new ShardTestKit(getSystem()) {{
153 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
154 newShardProps(), "testRegisterChangeListener");
156 waitUntilLeader(shard);
158 shard.tell(new UpdateSchemaContext(SchemaContextHelper.full()), ActorRef.noSender());
160 final MockDataChangeListener listener = new MockDataChangeListener(1);
161 final ActorRef dclActor = getSystem().actorOf(DataChangeListener.props(listener),
162 "testRegisterChangeListener-DataChangeListener");
164 shard.tell(new RegisterChangeListener(TestModel.TEST_PATH,
165 dclActor, AsyncDataBroker.DataChangeScope.BASE), getRef());
167 final RegisterChangeListenerReply reply = expectMsgClass(duration("3 seconds"),
168 RegisterChangeListenerReply.class);
169 final String replyPath = reply.getListenerRegistrationPath().toString();
170 assertTrue("Incorrect reply path: " + replyPath, replyPath.matches(
171 "akka:\\/\\/test\\/user\\/testRegisterChangeListener\\/\\$.*"));
173 final YangInstanceIdentifier path = TestModel.TEST_PATH;
174 writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
176 listener.waitForChangeEvents(path);
178 dclActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
179 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
183 @SuppressWarnings("serial")
185 public void testChangeListenerNotifiedWhenNotTheLeaderOnRegistration() throws Exception {
186 // This test tests the timing window in which a change listener is registered before the
187 // shard becomes the leader. We verify that the listener is registered and notified of the
188 // existing data when the shard becomes the leader.
189 new ShardTestKit(getSystem()) {{
190 // For this test, we want to send the RegisterChangeListener message after the shard
191 // has recovered from persistence and before it becomes the leader. So we subclass
192 // Shard to override onReceiveCommand and, when the first ElectionTimeout is received,
193 // we know that the shard has been initialized to a follower and has started the
194 // election process. The following 2 CountDownLatches are used to coordinate the
195 // ElectionTimeout with the sending of the RegisterChangeListener message.
196 final CountDownLatch onFirstElectionTimeout = new CountDownLatch(1);
197 final CountDownLatch onChangeListenerRegistered = new CountDownLatch(1);
198 final Creator<Shard> creator = new Creator<Shard>() {
199 boolean firstElectionTimeout = true;
202 public Shard create() throws Exception {
203 // Use a non persistent provider because this test actually invokes persist on the journal
204 // this will cause all other messages to not be queued properly after that.
205 // The basic issue is that you cannot use TestActorRef with a persistent actor (at least when
206 // it does do a persist)
207 return new Shard(shardID, Collections.<String,String>emptyMap(),
208 dataStoreContextBuilder.persistent(false).build(), SCHEMA_CONTEXT) {
210 public void onReceiveCommand(final Object message) throws Exception {
211 if(message instanceof ElectionTimeout && firstElectionTimeout) {
212 // Got the first ElectionTimeout. We don't forward it to the
213 // base Shard yet until we've sent the RegisterChangeListener
214 // message. So we signal the onFirstElectionTimeout latch to tell
215 // the main thread to send the RegisterChangeListener message and
216 // start a thread to wait on the onChangeListenerRegistered latch,
217 // which the main thread signals after it has sent the message.
218 // After the onChangeListenerRegistered is triggered, we send the
219 // original ElectionTimeout message to proceed with the election.
220 firstElectionTimeout = false;
221 final ActorRef self = getSelf();
225 Uninterruptibles.awaitUninterruptibly(
226 onChangeListenerRegistered, 5, TimeUnit.SECONDS);
227 self.tell(message, self);
231 onFirstElectionTimeout.countDown();
233 super.onReceiveCommand(message);
240 final MockDataChangeListener listener = new MockDataChangeListener(1);
241 final ActorRef dclActor = getSystem().actorOf(DataChangeListener.props(listener),
242 "testRegisterChangeListenerWhenNotLeaderInitially-DataChangeListener");
244 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
245 Props.create(new DelegatingShardCreator(creator)),
246 "testRegisterChangeListenerWhenNotLeaderInitially");
248 // Write initial data into the in-memory store.
249 final YangInstanceIdentifier path = TestModel.TEST_PATH;
250 writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
252 // Wait until the shard receives the first ElectionTimeout message.
253 assertEquals("Got first ElectionTimeout", true,
254 onFirstElectionTimeout.await(5, TimeUnit.SECONDS));
256 // Now send the RegisterChangeListener and wait for the reply.
257 shard.tell(new RegisterChangeListener(path, dclActor,
258 AsyncDataBroker.DataChangeScope.SUBTREE), getRef());
260 final RegisterChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
261 RegisterChangeListenerReply.class);
262 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
264 // Sanity check - verify the shard is not the leader yet.
265 shard.tell(new FindLeader(), getRef());
266 final FindLeaderReply findLeadeReply =
267 expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
268 assertNull("Expected the shard not to be the leader", findLeadeReply.getLeaderActor());
270 // Signal the onChangeListenerRegistered latch to tell the thread above to proceed
271 // with the election process.
272 onChangeListenerRegistered.countDown();
274 // Wait for the shard to become the leader and notify our listener with the existing
275 // data in the store.
276 listener.waitForChangeEvents(path);
278 dclActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
279 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
284 public void testRegisterDataTreeChangeListener() throws Exception {
285 new ShardTestKit(getSystem()) {{
286 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
287 newShardProps(), "testRegisterDataTreeChangeListener");
289 waitUntilLeader(shard);
291 shard.tell(new UpdateSchemaContext(SchemaContextHelper.full()), ActorRef.noSender());
293 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
294 final ActorRef dclActor = getSystem().actorOf(DataTreeChangeListenerActor.props(listener),
295 "testRegisterDataTreeChangeListener-DataTreeChangeListener");
297 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor), getRef());
299 final RegisterDataTreeChangeListenerReply reply = expectMsgClass(duration("3 seconds"),
300 RegisterDataTreeChangeListenerReply.class);
301 final String replyPath = reply.getListenerRegistrationPath().toString();
302 assertTrue("Incorrect reply path: " + replyPath, replyPath.matches(
303 "akka:\\/\\/test\\/user\\/testRegisterDataTreeChangeListener\\/\\$.*"));
305 final YangInstanceIdentifier path = TestModel.TEST_PATH;
306 writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
308 listener.waitForChangeEvents();
310 dclActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
311 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
315 @SuppressWarnings("serial")
317 public void testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration() throws Exception {
318 new ShardTestKit(getSystem()) {{
319 final CountDownLatch onFirstElectionTimeout = new CountDownLatch(1);
320 final CountDownLatch onChangeListenerRegistered = new CountDownLatch(1);
321 final Creator<Shard> creator = new Creator<Shard>() {
322 boolean firstElectionTimeout = true;
325 public Shard create() throws Exception {
326 return new Shard(shardID, Collections.<String,String>emptyMap(),
327 dataStoreContextBuilder.persistent(false).build(), SCHEMA_CONTEXT) {
329 public void onReceiveCommand(final Object message) throws Exception {
330 if(message instanceof ElectionTimeout && firstElectionTimeout) {
331 firstElectionTimeout = false;
332 final ActorRef self = getSelf();
336 Uninterruptibles.awaitUninterruptibly(
337 onChangeListenerRegistered, 5, TimeUnit.SECONDS);
338 self.tell(message, self);
342 onFirstElectionTimeout.countDown();
344 super.onReceiveCommand(message);
351 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
352 final ActorRef dclActor = getSystem().actorOf(DataTreeChangeListenerActor.props(listener),
353 "testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration-DataChangeListener");
355 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
356 Props.create(new DelegatingShardCreator(creator)),
357 "testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration");
359 final YangInstanceIdentifier path = TestModel.TEST_PATH;
360 writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
362 assertEquals("Got first ElectionTimeout", true,
363 onFirstElectionTimeout.await(5, TimeUnit.SECONDS));
365 shard.tell(new RegisterDataTreeChangeListener(path, dclActor), getRef());
366 final RegisterDataTreeChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
367 RegisterDataTreeChangeListenerReply.class);
368 assertNotNull("getListenerRegistratioznPath", reply.getListenerRegistrationPath());
370 shard.tell(new FindLeader(), getRef());
371 final FindLeaderReply findLeadeReply =
372 expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
373 assertNull("Expected the shard not to be the leader", findLeadeReply.getLeaderActor());
375 writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
377 onChangeListenerRegistered.countDown();
379 // TODO: investigate why we do not receive data chage events
380 listener.waitForChangeEvents();
382 dclActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
383 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
388 public void testCreateTransaction(){
389 new ShardTestKit(getSystem()) {{
390 final ActorRef shard = getSystem().actorOf(newShardProps(), "testCreateTransaction");
392 waitUntilLeader(shard);
394 shard.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
396 shard.tell(new CreateTransaction("txn-1",
397 TransactionType.READ_ONLY.ordinal() ).toSerializable(), getRef());
399 final CreateTransactionReply reply = expectMsgClass(duration("3 seconds"),
400 CreateTransactionReply.class);
402 final String path = reply.getTransactionActorPath().toString();
403 assertTrue("Unexpected transaction path " + path,
404 path.contains("akka://test/user/testCreateTransaction/shard-txn-1"));
406 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
411 public void testCreateTransactionOnChain(){
412 new ShardTestKit(getSystem()) {{
413 final ActorRef shard = getSystem().actorOf(newShardProps(), "testCreateTransactionOnChain");
415 waitUntilLeader(shard);
417 shard.tell(new CreateTransaction("txn-1",
418 TransactionType.READ_ONLY.ordinal() , "foobar").toSerializable(),
421 final CreateTransactionReply reply = expectMsgClass(duration("3 seconds"),
422 CreateTransactionReply.class);
424 final String path = reply.getTransactionActorPath().toString();
425 assertTrue("Unexpected transaction path " + path,
426 path.contains("akka://test/user/testCreateTransactionOnChain/shard-txn-1"));
428 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
432 @SuppressWarnings("serial")
434 public void testPeerAddressResolved() throws Exception {
435 new ShardTestKit(getSystem()) {{
436 final CountDownLatch recoveryComplete = new CountDownLatch(1);
437 class TestShard extends Shard {
439 super(shardID, Collections.<String, String>singletonMap(shardID.toString(), null),
440 newDatastoreContext(), SCHEMA_CONTEXT);
443 Map<String, String> getPeerAddresses() {
444 return getRaftActorContext().getPeerAddresses();
448 protected void onRecoveryComplete() {
450 super.onRecoveryComplete();
452 recoveryComplete.countDown();
457 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
458 Props.create(new DelegatingShardCreator(new Creator<Shard>() {
460 public TestShard create() throws Exception {
461 return new TestShard();
463 })), "testPeerAddressResolved");
465 //waitUntilLeader(shard);
466 assertEquals("Recovery complete", true,
467 Uninterruptibles.awaitUninterruptibly(recoveryComplete, 5, TimeUnit.SECONDS));
469 final String address = "akka://foobar";
470 shard.underlyingActor().onReceiveCommand(new PeerAddressResolved(shardID.toString(), address));
472 assertEquals("getPeerAddresses", address,
473 ((TestShard)shard.underlyingActor()).getPeerAddresses().get(shardID.toString()));
475 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
480 public void testApplySnapshot() throws Exception {
481 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardProps(),
482 "testApplySnapshot");
484 final DataTree store = InMemoryDataTreeFactory.getInstance().create();
485 store.setSchemaContext(SCHEMA_CONTEXT);
487 final ContainerNode container = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
488 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
489 withChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).addChild(
490 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1)).build()).build();
492 writeToStore(store, TestModel.TEST_PATH, container);
494 final YangInstanceIdentifier root = YangInstanceIdentifier.builder().build();
495 final NormalizedNode<?,?> expected = readStore(store, root);
497 final Snapshot snapshot = Snapshot.create(SerializationUtils.serializeNormalizedNode(expected),
498 Collections.<ReplicatedLogEntry>emptyList(), 1, 2, 3, 4);
500 shard.underlyingActor().getRaftActorSnapshotCohort().applySnapshot(snapshot.getState());
502 final NormalizedNode<?,?> actual = readStore(shard, root);
504 assertEquals("Root node", expected, actual);
506 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
510 public void testApplyState() throws Exception {
512 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardProps(), "testApplyState");
514 final NormalizedNode<?, ?> node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
516 final ApplyState applyState = new ApplyState(null, "test", new ReplicatedLogImplEntry(1, 2,
517 newModificationPayload(new WriteModification(TestModel.TEST_PATH, node))));
519 shard.underlyingActor().onReceiveCommand(applyState);
521 final NormalizedNode<?,?> actual = readStore(shard, TestModel.TEST_PATH);
522 assertEquals("Applied state", node, actual);
524 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
528 public void testApplyStateWithCandidatePayload() throws Exception {
530 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardPropsWithRecoveryComplete(), "testApplyState");
532 recoveryComplete.await(5, TimeUnit.SECONDS);
534 final NormalizedNode<?, ?> node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
535 final DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(TestModel.TEST_PATH, node);
537 final ApplyState applyState = new ApplyState(null, "test", new ReplicatedLogImplEntry(1, 2,
538 DataTreeCandidatePayload.create(candidate)));
540 shard.underlyingActor().onReceiveCommand(applyState);
542 final NormalizedNode<?,?> actual = readStore(shard, TestModel.TEST_PATH);
543 assertEquals("Applied state", node, actual);
545 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
548 DataTree setupInMemorySnapshotStore() throws DataValidationFailedException {
549 final DataTree testStore = InMemoryDataTreeFactory.getInstance().create();
550 testStore.setSchemaContext(SCHEMA_CONTEXT);
552 writeToStore(testStore, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
554 final NormalizedNode<?, ?> root = readStore(testStore, YangInstanceIdentifier.builder().build());
556 InMemorySnapshotStore.addSnapshot(shardID.toString(), Snapshot.create(
557 SerializationUtils.serializeNormalizedNode(root),
558 Collections.<ReplicatedLogEntry>emptyList(), 0, 1, -1, -1));
562 private static DataTreeCandidatePayload payloadForModification(final DataTree source, final DataTreeModification mod) throws DataValidationFailedException {
563 source.validate(mod);
564 final DataTreeCandidate candidate = source.prepare(mod);
565 source.commit(candidate);
566 return DataTreeCandidatePayload.create(candidate);
570 public void testDataTreeCandidateRecovery() throws Exception {
571 // Set up the InMemorySnapshotStore.
572 final DataTree source = setupInMemorySnapshotStore();
574 final DataTreeModification writeMod = source.takeSnapshot().newModification();
575 writeMod.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
577 InMemoryJournal.addEntry(shardID.toString(), 0, DUMMY_DATA);
579 // Set up the InMemoryJournal.
580 InMemoryJournal.addEntry(shardID.toString(), 1, new ReplicatedLogImplEntry(0, 1, payloadForModification(source, writeMod)));
582 final int nListEntries = 16;
583 final Set<Integer> listEntryKeys = new HashSet<>();
585 // Add some ModificationPayload entries
586 for (int i = 1; i <= nListEntries; i++) {
587 listEntryKeys.add(Integer.valueOf(i));
589 final YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
590 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i).build();
592 final DataTreeModification mod = source.takeSnapshot().newModification();
593 mod.merge(path, ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i));
595 InMemoryJournal.addEntry(shardID.toString(), i+1, new ReplicatedLogImplEntry(i, 1,
596 payloadForModification(source, mod)));
599 InMemoryJournal.addEntry(shardID.toString(), nListEntries + 2,
600 new ApplyJournalEntries(nListEntries));
602 testRecovery(listEntryKeys);
606 public void testModicationRecovery() throws Exception {
608 // Set up the InMemorySnapshotStore.
609 setupInMemorySnapshotStore();
611 // Set up the InMemoryJournal.
613 InMemoryJournal.addEntry(shardID.toString(), 0, DUMMY_DATA);
615 InMemoryJournal.addEntry(shardID.toString(), 1, new ReplicatedLogImplEntry(0, 1, newModificationPayload(
616 new WriteModification(TestModel.OUTER_LIST_PATH,
617 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build()))));
619 final int nListEntries = 16;
620 final Set<Integer> listEntryKeys = new HashSet<>();
622 // Add some ModificationPayload entries
623 for(int i = 1; i <= nListEntries; i++) {
624 listEntryKeys.add(Integer.valueOf(i));
625 final YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
626 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i).build();
627 final Modification mod = new MergeModification(path,
628 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i));
629 InMemoryJournal.addEntry(shardID.toString(), i + 1, new ReplicatedLogImplEntry(i, 1,
630 newModificationPayload(mod)));
633 InMemoryJournal.addEntry(shardID.toString(), nListEntries + 2,
634 new ApplyJournalEntries(nListEntries));
636 testRecovery(listEntryKeys);
639 private static ModificationPayload newModificationPayload(final Modification... mods) throws IOException {
640 final MutableCompositeModification compMod = new MutableCompositeModification();
641 for(final Modification mod: mods) {
642 compMod.addModification(mod);
645 return new ModificationPayload(compMod);
649 public void testConcurrentThreePhaseCommits() throws Throwable {
650 new ShardTestKit(getSystem()) {{
651 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
652 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
653 "testConcurrentThreePhaseCommits");
655 waitUntilLeader(shard);
657 // Setup 3 simulated transactions with mock cohorts backed by real cohorts.
659 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
661 final String transactionID1 = "tx1";
662 final MutableCompositeModification modification1 = new MutableCompositeModification();
663 final ShardDataTreeCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
664 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification1);
666 final String transactionID2 = "tx2";
667 final MutableCompositeModification modification2 = new MutableCompositeModification();
668 final ShardDataTreeCohort cohort2 = setupMockWriteTransaction("cohort2", dataStore,
669 TestModel.OUTER_LIST_PATH,
670 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(),
673 final String transactionID3 = "tx3";
674 final MutableCompositeModification modification3 = new MutableCompositeModification();
675 final ShardDataTreeCohort cohort3 = setupMockWriteTransaction("cohort3", dataStore,
676 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
677 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
678 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1),
681 final long timeoutSec = 5;
682 final FiniteDuration duration = FiniteDuration.create(timeoutSec, TimeUnit.SECONDS);
683 final Timeout timeout = new Timeout(duration);
685 // Simulate the ForwardedReadyTransaction message for the first Tx that would be sent
686 // by the ShardTransaction.
688 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
689 cohort1, modification1, true, false), getRef());
690 final ReadyTransactionReply readyReply = ReadyTransactionReply.fromSerializable(
691 expectMsgClass(duration, ReadyTransactionReply.class));
692 assertEquals("Cohort path", shard.path().toString(), readyReply.getCohortPath());
694 // Send the CanCommitTransaction message for the first Tx.
696 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
697 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
698 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
699 assertEquals("Can commit", true, canCommitReply.getCanCommit());
701 // Send the ForwardedReadyTransaction for the next 2 Tx's.
703 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
704 cohort2, modification2, true, false), getRef());
705 expectMsgClass(duration, ReadyTransactionReply.class);
707 shard.tell(new ForwardedReadyTransaction(transactionID3, CURRENT_VERSION,
708 cohort3, modification3, true, false), getRef());
709 expectMsgClass(duration, ReadyTransactionReply.class);
711 // Send the CanCommitTransaction message for the next 2 Tx's. These should get queued and
712 // processed after the first Tx completes.
714 final Future<Object> canCommitFuture1 = Patterns.ask(shard,
715 new CanCommitTransaction(transactionID2).toSerializable(), timeout);
717 final Future<Object> canCommitFuture2 = Patterns.ask(shard,
718 new CanCommitTransaction(transactionID3).toSerializable(), timeout);
720 // Send the CommitTransaction message for the first Tx. After it completes, it should
721 // trigger the 2nd Tx to proceed which should in turn then trigger the 3rd.
723 shard.tell(new CommitTransaction(transactionID1).toSerializable(), getRef());
724 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
726 // Wait for the next 2 Tx's to complete.
728 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
729 final CountDownLatch commitLatch = new CountDownLatch(2);
731 class OnFutureComplete extends OnComplete<Object> {
732 private final Class<?> expRespType;
734 OnFutureComplete(final Class<?> expRespType) {
735 this.expRespType = expRespType;
739 public void onComplete(final Throwable error, final Object resp) {
741 caughtEx.set(new AssertionError(getClass().getSimpleName() + " failure", error));
744 assertEquals("Commit response type", expRespType, resp.getClass());
746 } catch (final Exception e) {
752 void onSuccess(final Object resp) throws Exception {
756 class OnCommitFutureComplete extends OnFutureComplete {
757 OnCommitFutureComplete() {
758 super(CommitTransactionReply.SERIALIZABLE_CLASS);
762 public void onComplete(final Throwable error, final Object resp) {
763 super.onComplete(error, resp);
764 commitLatch.countDown();
768 class OnCanCommitFutureComplete extends OnFutureComplete {
769 private final String transactionID;
771 OnCanCommitFutureComplete(final String transactionID) {
772 super(CanCommitTransactionReply.SERIALIZABLE_CLASS);
773 this.transactionID = transactionID;
777 void onSuccess(final Object resp) throws Exception {
778 final CanCommitTransactionReply canCommitReply =
779 CanCommitTransactionReply.fromSerializable(resp);
780 assertEquals("Can commit", true, canCommitReply.getCanCommit());
782 final Future<Object> commitFuture = Patterns.ask(shard,
783 new CommitTransaction(transactionID).toSerializable(), timeout);
784 commitFuture.onComplete(new OnCommitFutureComplete(), getSystem().dispatcher());
788 canCommitFuture1.onComplete(new OnCanCommitFutureComplete(transactionID2),
789 getSystem().dispatcher());
791 canCommitFuture2.onComplete(new OnCanCommitFutureComplete(transactionID3),
792 getSystem().dispatcher());
794 final boolean done = commitLatch.await(timeoutSec, TimeUnit.SECONDS);
796 if(caughtEx.get() != null) {
797 throw caughtEx.get();
800 assertEquals("Commits complete", true, done);
802 final InOrder inOrder = inOrder(cohort1, cohort2, cohort3);
803 inOrder.verify(cohort1).canCommit();
804 inOrder.verify(cohort1).preCommit();
805 inOrder.verify(cohort1).commit();
806 inOrder.verify(cohort2).canCommit();
807 inOrder.verify(cohort2).preCommit();
808 inOrder.verify(cohort2).commit();
809 inOrder.verify(cohort3).canCommit();
810 inOrder.verify(cohort3).preCommit();
811 inOrder.verify(cohort3).commit();
813 // Verify data in the data store.
815 verifyOuterListEntry(shard, 1);
817 verifyLastApplied(shard, 2);
819 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
823 private static BatchedModifications newBatchedModifications(final String transactionID, final YangInstanceIdentifier path,
824 final NormalizedNode<?, ?> data, final boolean ready, final boolean doCommitOnReady, final int messagesSent) {
825 return newBatchedModifications(transactionID, null, path, data, ready, doCommitOnReady, messagesSent);
828 private static BatchedModifications newBatchedModifications(final String transactionID, final String transactionChainID,
829 final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final boolean ready, final boolean doCommitOnReady,
830 final int messagesSent) {
831 final BatchedModifications batched = new BatchedModifications(transactionID, CURRENT_VERSION, transactionChainID);
832 batched.addModification(new WriteModification(path, data));
833 batched.setReady(ready);
834 batched.setDoCommitOnReady(doCommitOnReady);
835 batched.setTotalMessagesSent(messagesSent);
840 public void testBatchedModificationsWithNoCommitOnReady() throws Throwable {
841 new ShardTestKit(getSystem()) {{
842 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
843 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
844 "testBatchedModificationsWithNoCommitOnReady");
846 waitUntilLeader(shard);
848 final String transactionID = "tx";
849 final FiniteDuration duration = duration("5 seconds");
851 final AtomicReference<ShardDataTreeCohort> mockCohort = new AtomicReference<>();
852 final ShardCommitCoordinator.CohortDecorator cohortDecorator = new ShardCommitCoordinator.CohortDecorator() {
854 public ShardDataTreeCohort decorate(final String txID, final ShardDataTreeCohort actual) {
855 if(mockCohort.get() == null) {
856 mockCohort.set(createDelegatingMockCohort("cohort", actual));
859 return mockCohort.get();
863 shard.underlyingActor().getCommitCoordinator().setCohortDecorator(cohortDecorator);
865 // Send a BatchedModifications to start a transaction.
867 shard.tell(newBatchedModifications(transactionID, TestModel.TEST_PATH,
868 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false, false, 1), getRef());
869 expectMsgClass(duration, BatchedModificationsReply.class);
871 // Send a couple more BatchedModifications.
873 shard.tell(newBatchedModifications(transactionID, TestModel.OUTER_LIST_PATH,
874 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false, false, 2), getRef());
875 expectMsgClass(duration, BatchedModificationsReply.class);
877 shard.tell(newBatchedModifications(transactionID, YangInstanceIdentifier.builder(
878 TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
879 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), true, false, 3), getRef());
880 expectMsgClass(duration, ReadyTransactionReply.class);
882 // Send the CanCommitTransaction message.
884 shard.tell(new CanCommitTransaction(transactionID).toSerializable(), getRef());
885 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
886 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
887 assertEquals("Can commit", true, canCommitReply.getCanCommit());
889 // Send the CanCommitTransaction message.
891 shard.tell(new CommitTransaction(transactionID).toSerializable(), getRef());
892 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
894 final InOrder inOrder = inOrder(mockCohort.get());
895 inOrder.verify(mockCohort.get()).canCommit();
896 inOrder.verify(mockCohort.get()).preCommit();
897 inOrder.verify(mockCohort.get()).commit();
899 // Verify data in the data store.
901 verifyOuterListEntry(shard, 1);
903 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
908 public void testBatchedModificationsWithCommitOnReady() throws Throwable {
909 new ShardTestKit(getSystem()) {{
910 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
911 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
912 "testBatchedModificationsWithCommitOnReady");
914 waitUntilLeader(shard);
916 final String transactionID = "tx";
917 final FiniteDuration duration = duration("5 seconds");
919 final AtomicReference<ShardDataTreeCohort> mockCohort = new AtomicReference<>();
920 final ShardCommitCoordinator.CohortDecorator cohortDecorator = new ShardCommitCoordinator.CohortDecorator() {
922 public ShardDataTreeCohort decorate(final String txID, final ShardDataTreeCohort actual) {
923 if(mockCohort.get() == null) {
924 mockCohort.set(createDelegatingMockCohort("cohort", actual));
927 return mockCohort.get();
931 shard.underlyingActor().getCommitCoordinator().setCohortDecorator(cohortDecorator);
933 // Send a BatchedModifications to start a transaction.
935 shard.tell(newBatchedModifications(transactionID, TestModel.TEST_PATH,
936 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false, false, 1), getRef());
937 expectMsgClass(duration, BatchedModificationsReply.class);
939 // Send a couple more BatchedModifications.
941 shard.tell(newBatchedModifications(transactionID, TestModel.OUTER_LIST_PATH,
942 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false, false, 2), getRef());
943 expectMsgClass(duration, BatchedModificationsReply.class);
945 shard.tell(newBatchedModifications(transactionID, YangInstanceIdentifier.builder(
946 TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
947 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), true, true, 3), getRef());
949 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
951 final InOrder inOrder = inOrder(mockCohort.get());
952 inOrder.verify(mockCohort.get()).canCommit();
953 inOrder.verify(mockCohort.get()).preCommit();
954 inOrder.verify(mockCohort.get()).commit();
956 // Verify data in the data store.
958 verifyOuterListEntry(shard, 1);
960 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
964 @Test(expected=IllegalStateException.class)
965 public void testBatchedModificationsReadyWithIncorrectTotalMessageCount() throws Throwable {
966 new ShardTestKit(getSystem()) {{
967 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
968 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
969 "testBatchedModificationsReadyWithIncorrectTotalMessageCount");
971 waitUntilLeader(shard);
973 final String transactionID = "tx1";
974 final BatchedModifications batched = new BatchedModifications(transactionID, DataStoreVersions.CURRENT_VERSION, null);
975 batched.setReady(true);
976 batched.setTotalMessagesSent(2);
978 shard.tell(batched, getRef());
980 final Failure failure = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
982 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
984 if(failure != null) {
985 throw failure.cause();
991 public void testBatchedModificationsWithOperationFailure() throws Throwable {
992 new ShardTestKit(getSystem()) {{
993 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
994 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
995 "testBatchedModificationsWithOperationFailure");
997 waitUntilLeader(shard);
999 // Test merge with invalid data. An exception should occur when the merge is applied. Note that
1000 // write will not validate the children for performance reasons.
1002 String transactionID = "tx1";
1004 ContainerNode invalidData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
1005 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
1006 withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1008 BatchedModifications batched = new BatchedModifications(transactionID, CURRENT_VERSION, null);
1009 batched.addModification(new MergeModification(TestModel.TEST_PATH, invalidData));
1010 shard.tell(batched, getRef());
1011 Failure failure = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
1013 Throwable cause = failure.cause();
1015 batched = new BatchedModifications(transactionID, DataStoreVersions.CURRENT_VERSION, null);
1016 batched.setReady(true);
1017 batched.setTotalMessagesSent(2);
1019 shard.tell(batched, getRef());
1021 failure = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
1022 assertEquals("Failure cause", cause, failure.cause());
1024 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1028 @SuppressWarnings("unchecked")
1029 private static void verifyOuterListEntry(final TestActorRef<Shard> shard, final Object expIDValue) throws Exception {
1030 final NormalizedNode<?, ?> outerList = readStore(shard, TestModel.OUTER_LIST_PATH);
1031 assertNotNull(TestModel.OUTER_LIST_QNAME.getLocalName() + " not found", outerList);
1032 assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " value is not Iterable",
1033 outerList.getValue() instanceof Iterable);
1034 final Object entry = ((Iterable<Object>)outerList.getValue()).iterator().next();
1035 assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " entry is not MapEntryNode",
1036 entry instanceof MapEntryNode);
1037 final MapEntryNode mapEntry = (MapEntryNode)entry;
1038 final Optional<DataContainerChild<? extends PathArgument, ?>> idLeaf =
1039 mapEntry.getChild(new YangInstanceIdentifier.NodeIdentifier(TestModel.ID_QNAME));
1040 assertTrue("Missing leaf " + TestModel.ID_QNAME.getLocalName(), idLeaf.isPresent());
1041 assertEquals(TestModel.ID_QNAME.getLocalName() + " value", expIDValue, idLeaf.get().getValue());
1045 public void testBatchedModificationsOnTransactionChain() throws Throwable {
1046 new ShardTestKit(getSystem()) {{
1047 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1048 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1049 "testBatchedModificationsOnTransactionChain");
1051 waitUntilLeader(shard);
1053 final String transactionChainID = "txChain";
1054 final String transactionID1 = "tx1";
1055 final String transactionID2 = "tx2";
1057 final FiniteDuration duration = duration("5 seconds");
1059 // Send a BatchedModifications to start a chained write transaction and ready it.
1061 final ContainerNode containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1062 final YangInstanceIdentifier path = TestModel.TEST_PATH;
1063 shard.tell(newBatchedModifications(transactionID1, transactionChainID, path,
1064 containerNode, true, false, 1), getRef());
1065 expectMsgClass(duration, ReadyTransactionReply.class);
1067 // Create a read Tx on the same chain.
1069 shard.tell(new CreateTransaction(transactionID2, TransactionType.READ_ONLY.ordinal() ,
1070 transactionChainID).toSerializable(), getRef());
1072 final CreateTransactionReply createReply = expectMsgClass(duration("3 seconds"), CreateTransactionReply.class);
1074 getSystem().actorSelection(createReply.getTransactionActorPath()).tell(new ReadData(path), getRef());
1075 final ReadDataReply readReply = expectMsgClass(duration("3 seconds"), ReadDataReply.class);
1076 assertEquals("Read node", containerNode, readReply.getNormalizedNode());
1078 // Commit the write transaction.
1080 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
1081 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1082 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
1083 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1085 shard.tell(new CommitTransaction(transactionID1).toSerializable(), getRef());
1086 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
1088 // Verify data in the data store.
1090 final NormalizedNode<?, ?> actualNode = readStore(shard, path);
1091 assertEquals("Stored node", containerNode, actualNode);
1093 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1098 public void testOnBatchedModificationsWhenNotLeader() {
1099 final AtomicBoolean overrideLeaderCalls = new AtomicBoolean();
1100 new ShardTestKit(getSystem()) {{
1101 final Creator<Shard> creator = new Creator<Shard>() {
1102 private static final long serialVersionUID = 1L;
1105 public Shard create() throws Exception {
1106 return new Shard(shardID, Collections.<String,String>emptyMap(),
1107 newDatastoreContext(), SCHEMA_CONTEXT) {
1109 protected boolean isLeader() {
1110 return overrideLeaderCalls.get() ? false : super.isLeader();
1114 protected ActorSelection getLeader() {
1115 return overrideLeaderCalls.get() ? getSystem().actorSelection(getRef().path()) :
1122 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1123 Props.create(new DelegatingShardCreator(creator)), "testOnBatchedModificationsWhenNotLeader");
1125 waitUntilLeader(shard);
1127 overrideLeaderCalls.set(true);
1129 final BatchedModifications batched = new BatchedModifications("tx", DataStoreVersions.CURRENT_VERSION, "");
1131 shard.tell(batched, ActorRef.noSender());
1133 expectMsgEquals(batched);
1135 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1140 public void testForwardedReadyTransactionWithImmediateCommit() throws Exception{
1141 new ShardTestKit(getSystem()) {{
1142 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1143 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1144 "testForwardedReadyTransactionWithImmediateCommit");
1146 waitUntilLeader(shard);
1148 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1150 final String transactionID = "tx1";
1151 final MutableCompositeModification modification = new MutableCompositeModification();
1152 final NormalizedNode<?, ?> containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1153 final ShardDataTreeCohort cohort = setupMockWriteTransaction("cohort", dataStore,
1154 TestModel.TEST_PATH, containerNode, modification);
1156 final FiniteDuration duration = duration("5 seconds");
1158 // Simulate the ForwardedReadyTransaction messages that would be sent
1159 // by the ShardTransaction.
1161 shard.tell(new ForwardedReadyTransaction(transactionID, CURRENT_VERSION,
1162 cohort, modification, true, true), getRef());
1164 expectMsgClass(duration, ThreePhaseCommitCohortMessages.CommitTransactionReply.class);
1166 final InOrder inOrder = inOrder(cohort);
1167 inOrder.verify(cohort).canCommit();
1168 inOrder.verify(cohort).preCommit();
1169 inOrder.verify(cohort).commit();
1171 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.TEST_PATH);
1172 assertEquals(TestModel.TEST_QNAME.getLocalName(), containerNode, actualNode);
1174 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1179 public void testReadyLocalTransactionWithImmediateCommit() throws Exception{
1180 new ShardTestKit(getSystem()) {{
1181 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1182 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1183 "testReadyLocalTransactionWithImmediateCommit");
1185 waitUntilLeader(shard);
1187 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1189 final DataTreeModification modification = dataStore.getDataTree().takeSnapshot().newModification();
1191 final ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1192 new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
1193 final MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
1194 new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
1196 final String txId = "tx1";
1197 modification.ready();
1198 final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(txId, modification, true);
1200 shard.tell(readyMessage, getRef());
1202 expectMsgClass(CommitTransactionReply.SERIALIZABLE_CLASS);
1204 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.OUTER_LIST_PATH);
1205 assertEquals(TestModel.OUTER_LIST_QNAME.getLocalName(), mergeData, actualNode);
1207 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1212 public void testReadyLocalTransactionWithThreePhaseCommit() throws Exception{
1213 new ShardTestKit(getSystem()) {{
1214 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1215 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1216 "testReadyLocalTransactionWithThreePhaseCommit");
1218 waitUntilLeader(shard);
1220 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1222 final DataTreeModification modification = dataStore.getDataTree().takeSnapshot().newModification();
1224 final ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1225 new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
1226 final MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
1227 new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
1229 final String txId = "tx1";
1230 modification.ready();
1231 final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(txId, modification, false);
1233 shard.tell(readyMessage, getRef());
1235 expectMsgClass(ReadyTransactionReply.class);
1237 // Send the CanCommitTransaction message.
1239 shard.tell(new CanCommitTransaction(txId).toSerializable(), getRef());
1240 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1241 expectMsgClass(CanCommitTransactionReply.SERIALIZABLE_CLASS));
1242 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1244 // Send the CanCommitTransaction message.
1246 shard.tell(new CommitTransaction(txId).toSerializable(), getRef());
1247 expectMsgClass(CommitTransactionReply.SERIALIZABLE_CLASS);
1249 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.OUTER_LIST_PATH);
1250 assertEquals(TestModel.OUTER_LIST_QNAME.getLocalName(), mergeData, actualNode);
1252 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1257 public void testCommitWithPersistenceDisabled() throws Throwable {
1258 dataStoreContextBuilder.persistent(false);
1259 new ShardTestKit(getSystem()) {{
1260 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1261 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1262 "testCommitWithPersistenceDisabled");
1264 waitUntilLeader(shard);
1266 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1268 // Setup a simulated transactions with a mock cohort.
1270 final String transactionID = "tx";
1271 final MutableCompositeModification modification = new MutableCompositeModification();
1272 final NormalizedNode<?, ?> containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1273 final ShardDataTreeCohort cohort = setupMockWriteTransaction("cohort", dataStore,
1274 TestModel.TEST_PATH, containerNode, modification);
1276 final FiniteDuration duration = duration("5 seconds");
1278 // Simulate the ForwardedReadyTransaction messages that would be sent
1279 // by the ShardTransaction.
1281 shard.tell(new ForwardedReadyTransaction(transactionID, CURRENT_VERSION,
1282 cohort, modification, true, false), getRef());
1283 expectMsgClass(duration, ReadyTransactionReply.class);
1285 // Send the CanCommitTransaction message.
1287 shard.tell(new CanCommitTransaction(transactionID).toSerializable(), getRef());
1288 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1289 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
1290 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1292 // Send the CanCommitTransaction message.
1294 shard.tell(new CommitTransaction(transactionID).toSerializable(), getRef());
1295 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
1297 final InOrder inOrder = inOrder(cohort);
1298 inOrder.verify(cohort).canCommit();
1299 inOrder.verify(cohort).preCommit();
1300 inOrder.verify(cohort).commit();
1302 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.TEST_PATH);
1303 assertEquals(TestModel.TEST_QNAME.getLocalName(), containerNode, actualNode);
1305 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1309 private static DataTreeCandidateTip mockCandidate(final String name) {
1310 final DataTreeCandidateTip mockCandidate = mock(DataTreeCandidateTip.class, name);
1311 final DataTreeCandidateNode mockCandidateNode = mock(DataTreeCandidateNode.class, name + "-node");
1312 doReturn(ModificationType.WRITE).when(mockCandidateNode).getModificationType();
1313 doReturn(Optional.of(ImmutableNodes.containerNode(CARS_QNAME))).when(mockCandidateNode).getDataAfter();
1314 doReturn(YangInstanceIdentifier.builder().build()).when(mockCandidate).getRootPath();
1315 doReturn(mockCandidateNode).when(mockCandidate).getRootNode();
1316 return mockCandidate;
1319 private static DataTreeCandidateTip mockUnmodifiedCandidate(final String name) {
1320 final DataTreeCandidateTip mockCandidate = mock(DataTreeCandidateTip.class, name);
1321 final DataTreeCandidateNode mockCandidateNode = mock(DataTreeCandidateNode.class, name + "-node");
1322 doReturn(ModificationType.UNMODIFIED).when(mockCandidateNode).getModificationType();
1323 doReturn(YangInstanceIdentifier.builder().build()).when(mockCandidate).getRootPath();
1324 doReturn(mockCandidateNode).when(mockCandidate).getRootNode();
1325 return mockCandidate;
1329 public void testCommitWhenTransactionHasNoModifications(){
1330 // Note that persistence is enabled which would normally result in the entry getting written to the journal
1331 // but here that need not happen
1332 new ShardTestKit(getSystem()) {
1334 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1335 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1336 "testCommitWhenTransactionHasNoModifications");
1338 waitUntilLeader(shard);
1340 final String transactionID = "tx1";
1341 final MutableCompositeModification modification = new MutableCompositeModification();
1342 final ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class, "cohort1");
1343 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1344 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).preCommit();
1345 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).commit();
1346 doReturn(mockUnmodifiedCandidate("cohort1-candidate")).when(cohort).getCandidate();
1348 final FiniteDuration duration = duration("5 seconds");
1350 // Simulate the ForwardedReadyTransaction messages that would be sent
1351 // by the ShardTransaction.
1353 shard.tell(new ForwardedReadyTransaction(transactionID, CURRENT_VERSION,
1354 cohort, modification, true, false), getRef());
1355 expectMsgClass(duration, ReadyTransactionReply.class);
1357 // Send the CanCommitTransaction message.
1359 shard.tell(new CanCommitTransaction(transactionID).toSerializable(), getRef());
1360 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1361 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
1362 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1364 shard.tell(new CommitTransaction(transactionID).toSerializable(), getRef());
1365 expectMsgClass(duration, ThreePhaseCommitCohortMessages.CommitTransactionReply.class);
1367 final InOrder inOrder = inOrder(cohort);
1368 inOrder.verify(cohort).canCommit();
1369 inOrder.verify(cohort).preCommit();
1370 inOrder.verify(cohort).commit();
1372 shard.tell(Shard.GET_SHARD_MBEAN_MESSAGE, getRef());
1373 final ShardStats shardStats = expectMsgClass(duration, ShardStats.class);
1375 // Use MBean for verification
1376 // Committed transaction count should increase as usual
1377 assertEquals(1,shardStats.getCommittedTransactionsCount());
1379 // Commit index should not advance because this does not go into the journal
1380 assertEquals(-1, shardStats.getCommitIndex());
1382 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1389 public void testCommitWhenTransactionHasModifications(){
1390 new ShardTestKit(getSystem()) {
1392 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1393 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1394 "testCommitWhenTransactionHasModifications");
1396 waitUntilLeader(shard);
1398 final String transactionID = "tx1";
1399 final MutableCompositeModification modification = new MutableCompositeModification();
1400 modification.addModification(new DeleteModification(YangInstanceIdentifier.builder().build()));
1401 final ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class, "cohort1");
1402 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1403 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).preCommit();
1404 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).commit();
1405 doReturn(mockCandidate("cohort1-candidate")).when(cohort).getCandidate();
1407 final FiniteDuration duration = duration("5 seconds");
1409 // Simulate the ForwardedReadyTransaction messages that would be sent
1410 // by the ShardTransaction.
1412 shard.tell(new ForwardedReadyTransaction(transactionID, CURRENT_VERSION,
1413 cohort, modification, true, false), getRef());
1414 expectMsgClass(duration, ReadyTransactionReply.class);
1416 // Send the CanCommitTransaction message.
1418 shard.tell(new CanCommitTransaction(transactionID).toSerializable(), getRef());
1419 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1420 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
1421 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1423 shard.tell(new CommitTransaction(transactionID).toSerializable(), getRef());
1424 expectMsgClass(duration, ThreePhaseCommitCohortMessages.CommitTransactionReply.class);
1426 final InOrder inOrder = inOrder(cohort);
1427 inOrder.verify(cohort).canCommit();
1428 inOrder.verify(cohort).preCommit();
1429 inOrder.verify(cohort).commit();
1431 shard.tell(Shard.GET_SHARD_MBEAN_MESSAGE, getRef());
1432 final ShardStats shardStats = expectMsgClass(duration, ShardStats.class);
1434 // Use MBean for verification
1435 // Committed transaction count should increase as usual
1436 assertEquals(1, shardStats.getCommittedTransactionsCount());
1438 // Commit index should advance as we do not have an empty modification
1439 assertEquals(0, shardStats.getCommitIndex());
1441 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1448 public void testCommitPhaseFailure() throws Throwable {
1449 new ShardTestKit(getSystem()) {{
1450 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1451 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1452 "testCommitPhaseFailure");
1454 waitUntilLeader(shard);
1456 // Setup 2 simulated transactions with mock cohorts. The first one fails in the
1459 final String transactionID1 = "tx1";
1460 final MutableCompositeModification modification1 = new MutableCompositeModification();
1461 final ShardDataTreeCohort cohort1 = mock(ShardDataTreeCohort.class, "cohort1");
1462 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort1).canCommit();
1463 doReturn(Futures.immediateFuture(null)).when(cohort1).preCommit();
1464 doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock"))).when(cohort1).commit();
1465 doReturn(mockCandidate("cohort1-candidate")).when(cohort1).getCandidate();
1467 final String transactionID2 = "tx2";
1468 final MutableCompositeModification modification2 = new MutableCompositeModification();
1469 final ShardDataTreeCohort cohort2 = mock(ShardDataTreeCohort.class, "cohort2");
1470 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort2).canCommit();
1472 final FiniteDuration duration = duration("5 seconds");
1473 final Timeout timeout = new Timeout(duration);
1475 // Simulate the ForwardedReadyTransaction messages that would be sent
1476 // by the ShardTransaction.
1478 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1479 cohort1, modification1, true, false), getRef());
1480 expectMsgClass(duration, ReadyTransactionReply.class);
1482 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1483 cohort2, modification2, true, false), getRef());
1484 expectMsgClass(duration, ReadyTransactionReply.class);
1486 // Send the CanCommitTransaction message for the first Tx.
1488 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
1489 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1490 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
1491 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1493 // Send the CanCommitTransaction message for the 2nd Tx. This should get queued and
1494 // processed after the first Tx completes.
1496 final Future<Object> canCommitFuture = Patterns.ask(shard,
1497 new CanCommitTransaction(transactionID2).toSerializable(), timeout);
1499 // Send the CommitTransaction message for the first Tx. This should send back an error
1500 // and trigger the 2nd Tx to proceed.
1502 shard.tell(new CommitTransaction(transactionID1).toSerializable(), getRef());
1503 expectMsgClass(duration, akka.actor.Status.Failure.class);
1505 // Wait for the 2nd Tx to complete the canCommit phase.
1507 final CountDownLatch latch = new CountDownLatch(1);
1508 canCommitFuture.onComplete(new OnComplete<Object>() {
1510 public void onComplete(final Throwable t, final Object resp) {
1513 }, getSystem().dispatcher());
1515 assertEquals("2nd CanCommit complete", true, latch.await(5, TimeUnit.SECONDS));
1517 final InOrder inOrder = inOrder(cohort1, cohort2);
1518 inOrder.verify(cohort1).canCommit();
1519 inOrder.verify(cohort1).preCommit();
1520 inOrder.verify(cohort1).commit();
1521 inOrder.verify(cohort2).canCommit();
1523 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1528 public void testPreCommitPhaseFailure() throws Throwable {
1529 new ShardTestKit(getSystem()) {{
1530 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1531 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1532 "testPreCommitPhaseFailure");
1534 waitUntilLeader(shard);
1536 final String transactionID1 = "tx1";
1537 final MutableCompositeModification modification1 = new MutableCompositeModification();
1538 final ShardDataTreeCohort cohort1 = mock(ShardDataTreeCohort.class, "cohort1");
1539 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort1).canCommit();
1540 doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock"))).when(cohort1).preCommit();
1542 final String transactionID2 = "tx2";
1543 final MutableCompositeModification modification2 = new MutableCompositeModification();
1544 final ShardDataTreeCohort cohort2 = mock(ShardDataTreeCohort.class, "cohort2");
1545 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort2).canCommit();
1547 final FiniteDuration duration = duration("5 seconds");
1548 final Timeout timeout = new Timeout(duration);
1550 // Simulate the ForwardedReadyTransaction messages that would be sent
1551 // by the ShardTransaction.
1553 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1554 cohort1, modification1, true, false), getRef());
1555 expectMsgClass(duration, ReadyTransactionReply.class);
1557 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1558 cohort2, modification2, true, false), getRef());
1559 expectMsgClass(duration, ReadyTransactionReply.class);
1561 // Send the CanCommitTransaction message for the first Tx.
1563 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
1564 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1565 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
1566 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1568 // Send the CanCommitTransaction message for the 2nd Tx. This should get queued and
1569 // processed after the first Tx completes.
1571 final Future<Object> canCommitFuture = Patterns.ask(shard,
1572 new CanCommitTransaction(transactionID2).toSerializable(), timeout);
1574 // Send the CommitTransaction message for the first Tx. This should send back an error
1575 // and trigger the 2nd Tx to proceed.
1577 shard.tell(new CommitTransaction(transactionID1).toSerializable(), getRef());
1578 expectMsgClass(duration, akka.actor.Status.Failure.class);
1580 // Wait for the 2nd Tx to complete the canCommit phase.
1582 final CountDownLatch latch = new CountDownLatch(1);
1583 canCommitFuture.onComplete(new OnComplete<Object>() {
1585 public void onComplete(final Throwable t, final Object resp) {
1588 }, getSystem().dispatcher());
1590 assertEquals("2nd CanCommit complete", true, latch.await(5, TimeUnit.SECONDS));
1592 final InOrder inOrder = inOrder(cohort1, cohort2);
1593 inOrder.verify(cohort1).canCommit();
1594 inOrder.verify(cohort1).preCommit();
1595 inOrder.verify(cohort2).canCommit();
1597 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1602 public void testCanCommitPhaseFailure() throws Throwable {
1603 new ShardTestKit(getSystem()) {{
1604 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1605 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1606 "testCanCommitPhaseFailure");
1608 waitUntilLeader(shard);
1610 final FiniteDuration duration = duration("5 seconds");
1612 final String transactionID1 = "tx1";
1613 final MutableCompositeModification modification = new MutableCompositeModification();
1614 final ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class, "cohort1");
1615 doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock"))).when(cohort).canCommit();
1617 // Simulate the ForwardedReadyTransaction messages that would be sent
1618 // by the ShardTransaction.
1620 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1621 cohort, modification, true, false), getRef());
1622 expectMsgClass(duration, ReadyTransactionReply.class);
1624 // Send the CanCommitTransaction message.
1626 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
1627 expectMsgClass(duration, akka.actor.Status.Failure.class);
1629 // Send another can commit to ensure the failed one got cleaned up.
1633 final String transactionID2 = "tx2";
1634 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1636 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1637 cohort, modification, true, false), getRef());
1638 expectMsgClass(duration, ReadyTransactionReply.class);
1640 shard.tell(new CanCommitTransaction(transactionID2).toSerializable(), getRef());
1641 final CanCommitTransactionReply reply = CanCommitTransactionReply.fromSerializable(
1642 expectMsgClass(CanCommitTransactionReply.SERIALIZABLE_CLASS));
1643 assertEquals("getCanCommit", true, reply.getCanCommit());
1645 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1650 public void testCanCommitPhaseFalseResponse() throws Throwable {
1651 new ShardTestKit(getSystem()) {{
1652 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1653 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1654 "testCanCommitPhaseFalseResponse");
1656 waitUntilLeader(shard);
1658 final FiniteDuration duration = duration("5 seconds");
1660 final String transactionID1 = "tx1";
1661 final MutableCompositeModification modification = new MutableCompositeModification();
1662 final ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class, "cohort1");
1663 doReturn(Futures.immediateFuture(Boolean.FALSE)).when(cohort).canCommit();
1665 // Simulate the ForwardedReadyTransaction messages that would be sent
1666 // by the ShardTransaction.
1668 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1669 cohort, modification, true, false), getRef());
1670 expectMsgClass(duration, ReadyTransactionReply.class);
1672 // Send the CanCommitTransaction message.
1674 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
1675 CanCommitTransactionReply reply = CanCommitTransactionReply.fromSerializable(
1676 expectMsgClass(CanCommitTransactionReply.SERIALIZABLE_CLASS));
1677 assertEquals("getCanCommit", false, reply.getCanCommit());
1679 // Send another can commit to ensure the failed one got cleaned up.
1683 final String transactionID2 = "tx2";
1684 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1686 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1687 cohort, modification, true, false), getRef());
1688 expectMsgClass(duration, ReadyTransactionReply.class);
1690 shard.tell(new CanCommitTransaction(transactionID2).toSerializable(), getRef());
1691 reply = CanCommitTransactionReply.fromSerializable(
1692 expectMsgClass(CanCommitTransactionReply.SERIALIZABLE_CLASS));
1693 assertEquals("getCanCommit", true, reply.getCanCommit());
1695 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1700 public void testImmediateCommitWithCanCommitPhaseFailure() throws Throwable {
1701 new ShardTestKit(getSystem()) {{
1702 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1703 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1704 "testImmediateCommitWithCanCommitPhaseFailure");
1706 waitUntilLeader(shard);
1708 final FiniteDuration duration = duration("5 seconds");
1710 final String transactionID1 = "tx1";
1711 final MutableCompositeModification modification = new MutableCompositeModification();
1712 final ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class, "cohort1");
1713 doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock"))).when(cohort).canCommit();
1715 // Simulate the ForwardedReadyTransaction messages that would be sent
1716 // by the ShardTransaction.
1718 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1719 cohort, modification, true, true), getRef());
1721 expectMsgClass(duration, akka.actor.Status.Failure.class);
1723 // Send another can commit to ensure the failed one got cleaned up.
1727 final String transactionID2 = "tx2";
1728 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1729 doReturn(Futures.immediateFuture(null)).when(cohort).preCommit();
1730 doReturn(Futures.immediateFuture(null)).when(cohort).commit();
1731 final DataTreeCandidateTip candidate = mock(DataTreeCandidateTip.class);
1732 final DataTreeCandidateNode candidateRoot = mock(DataTreeCandidateNode.class);
1733 doReturn(ModificationType.UNMODIFIED).when(candidateRoot).getModificationType();
1734 doReturn(candidateRoot).when(candidate).getRootNode();
1735 doReturn(candidate).when(cohort).getCandidate();
1737 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1738 cohort, modification, true, true), getRef());
1740 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
1742 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1747 public void testImmediateCommitWithCanCommitPhaseFalseResponse() throws Throwable {
1748 new ShardTestKit(getSystem()) {{
1749 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1750 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1751 "testImmediateCommitWithCanCommitPhaseFalseResponse");
1753 waitUntilLeader(shard);
1755 final FiniteDuration duration = duration("5 seconds");
1757 final String transactionID = "tx1";
1758 final MutableCompositeModification modification = new MutableCompositeModification();
1759 final ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class, "cohort1");
1760 doReturn(Futures.immediateFuture(Boolean.FALSE)).when(cohort).canCommit();
1762 // Simulate the ForwardedReadyTransaction messages that would be sent
1763 // by the ShardTransaction.
1765 shard.tell(new ForwardedReadyTransaction(transactionID, CURRENT_VERSION,
1766 cohort, modification, true, true), getRef());
1768 expectMsgClass(duration, akka.actor.Status.Failure.class);
1770 // Send another can commit to ensure the failed one got cleaned up.
1774 final String transactionID2 = "tx2";
1775 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1776 doReturn(Futures.immediateFuture(null)).when(cohort).preCommit();
1777 doReturn(Futures.immediateFuture(null)).when(cohort).commit();
1778 final DataTreeCandidateTip candidate = mock(DataTreeCandidateTip.class);
1779 final DataTreeCandidateNode candidateRoot = mock(DataTreeCandidateNode.class);
1780 doReturn(ModificationType.UNMODIFIED).when(candidateRoot).getModificationType();
1781 doReturn(candidateRoot).when(candidate).getRootNode();
1782 doReturn(candidate).when(cohort).getCandidate();
1784 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1785 cohort, modification, true, true), getRef());
1787 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
1789 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1794 public void testAbortBeforeFinishCommit() throws Throwable {
1795 new ShardTestKit(getSystem()) {{
1796 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1797 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1798 "testAbortBeforeFinishCommit");
1800 waitUntilLeader(shard);
1802 final FiniteDuration duration = duration("5 seconds");
1803 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1805 final String transactionID = "tx1";
1806 final Function<ShardDataTreeCohort, ListenableFuture<Void>> preCommit =
1807 new Function<ShardDataTreeCohort, ListenableFuture<Void>>() {
1809 public ListenableFuture<Void> apply(final ShardDataTreeCohort cohort) {
1810 final ListenableFuture<Void> preCommitFuture = cohort.preCommit();
1812 // Simulate an AbortTransaction message occurring during replication, after
1813 // persisting and before finishing the commit to the in-memory store.
1814 // We have no followers so due to optimizations in the RaftActor, it does not
1815 // attempt replication and thus we can't send an AbortTransaction message b/c
1816 // it would be processed too late after CommitTransaction completes. So we'll
1817 // simulate an AbortTransaction message occurring during replication by calling
1818 // the shard directly.
1820 shard.underlyingActor().doAbortTransaction(transactionID, null);
1822 return preCommitFuture;
1826 final MutableCompositeModification modification = new MutableCompositeModification();
1827 final ShardDataTreeCohort cohort = setupMockWriteTransaction("cohort1", dataStore,
1828 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME),
1829 modification, preCommit);
1831 shard.tell(new ForwardedReadyTransaction(transactionID, CURRENT_VERSION,
1832 cohort, modification, true, false), getRef());
1833 expectMsgClass(duration, ReadyTransactionReply.class);
1835 shard.tell(new CanCommitTransaction(transactionID).toSerializable(), getRef());
1836 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1837 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
1838 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1840 shard.tell(new CommitTransaction(transactionID).toSerializable(), getRef());
1841 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
1843 final NormalizedNode<?, ?> node = readStore(shard, TestModel.TEST_PATH);
1845 // Since we're simulating an abort occurring during replication and before finish commit,
1846 // the data should still get written to the in-memory store since we've gotten past
1847 // canCommit and preCommit and persisted the data.
1848 assertNotNull(TestModel.TEST_QNAME.getLocalName() + " not found", node);
1850 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1855 public void testTransactionCommitTimeout() throws Throwable {
1856 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1858 new ShardTestKit(getSystem()) {{
1859 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1860 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1861 "testTransactionCommitTimeout");
1863 waitUntilLeader(shard);
1865 final FiniteDuration duration = duration("5 seconds");
1867 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1869 writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1870 writeToStore(shard, TestModel.OUTER_LIST_PATH,
1871 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1873 // Create 1st Tx - will timeout
1875 final String transactionID1 = "tx1";
1876 final MutableCompositeModification modification1 = new MutableCompositeModification();
1877 final ShardDataTreeCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
1878 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1879 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
1880 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1),
1885 final String transactionID2 = "tx3";
1886 final MutableCompositeModification modification2 = new MutableCompositeModification();
1887 final YangInstanceIdentifier listNodePath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1888 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build();
1889 final ShardDataTreeCohort cohort2 = setupMockWriteTransaction("cohort3", dataStore,
1891 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2),
1896 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1897 cohort1, modification1, true, false), getRef());
1898 expectMsgClass(duration, ReadyTransactionReply.class);
1900 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1901 cohort2, modification2, true, false), getRef());
1902 expectMsgClass(duration, ReadyTransactionReply.class);
1904 // canCommit 1st Tx. We don't send the commit so it should timeout.
1906 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
1907 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS);
1909 // canCommit the 2nd Tx - it should complete after the 1st Tx times out.
1911 shard.tell(new CanCommitTransaction(transactionID2).toSerializable(), getRef());
1912 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS);
1914 // Try to commit the 1st Tx - should fail as it's not the current Tx.
1916 shard.tell(new CommitTransaction(transactionID1).toSerializable(), getRef());
1917 expectMsgClass(duration, akka.actor.Status.Failure.class);
1919 // Commit the 2nd Tx.
1921 shard.tell(new CommitTransaction(transactionID2).toSerializable(), getRef());
1922 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
1924 final NormalizedNode<?, ?> node = readStore(shard, listNodePath);
1925 assertNotNull(listNodePath + " not found", node);
1927 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1932 public void testTransactionCommitQueueCapacityExceeded() throws Throwable {
1933 dataStoreContextBuilder.shardTransactionCommitQueueCapacity(2);
1935 new ShardTestKit(getSystem()) {{
1936 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1937 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1938 "testTransactionCommitQueueCapacityExceeded");
1940 waitUntilLeader(shard);
1942 final FiniteDuration duration = duration("5 seconds");
1944 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1946 final String transactionID1 = "tx1";
1947 final MutableCompositeModification modification1 = new MutableCompositeModification();
1948 final ShardDataTreeCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
1949 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification1);
1951 final String transactionID2 = "tx2";
1952 final MutableCompositeModification modification2 = new MutableCompositeModification();
1953 final ShardDataTreeCohort cohort2 = setupMockWriteTransaction("cohort2", dataStore,
1954 TestModel.OUTER_LIST_PATH,
1955 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(),
1958 final String transactionID3 = "tx3";
1959 final MutableCompositeModification modification3 = new MutableCompositeModification();
1960 final ShardDataTreeCohort cohort3 = setupMockWriteTransaction("cohort3", dataStore,
1961 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification3);
1965 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1966 cohort1, modification1, true, false), getRef());
1967 expectMsgClass(duration, ReadyTransactionReply.class);
1969 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1970 cohort2, modification2, true, false), getRef());
1971 expectMsgClass(duration, ReadyTransactionReply.class);
1973 // The 3rd Tx should exceed queue capacity and fail.
1975 shard.tell(new ForwardedReadyTransaction(transactionID3, CURRENT_VERSION,
1976 cohort3, modification3, true, false), getRef());
1977 expectMsgClass(duration, akka.actor.Status.Failure.class);
1979 // canCommit 1st Tx.
1981 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
1982 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS);
1984 // canCommit the 2nd Tx - it should get queued.
1986 shard.tell(new CanCommitTransaction(transactionID2).toSerializable(), getRef());
1988 // canCommit the 3rd Tx - should exceed queue capacity and fail.
1990 shard.tell(new CanCommitTransaction(transactionID3).toSerializable(), getRef());
1991 expectMsgClass(duration, akka.actor.Status.Failure.class);
1993 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1998 public void testTransactionCommitWithPriorExpiredCohortEntries() throws Throwable {
1999 dataStoreContextBuilder.shardCommitQueueExpiryTimeoutInMillis(1300).shardTransactionCommitTimeoutInSeconds(1);
2001 new ShardTestKit(getSystem()) {{
2002 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
2003 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
2004 "testTransactionCommitWithPriorExpiredCohortEntries");
2006 waitUntilLeader(shard);
2008 final FiniteDuration duration = duration("5 seconds");
2010 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
2012 final String transactionID1 = "tx1";
2013 final MutableCompositeModification modification1 = new MutableCompositeModification();
2014 final ShardDataTreeCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
2015 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification1);
2017 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
2018 cohort1, modification1, true, false), getRef());
2019 expectMsgClass(duration, ReadyTransactionReply.class);
2021 final String transactionID2 = "tx2";
2022 final MutableCompositeModification modification2 = new MutableCompositeModification();
2023 final ShardDataTreeCohort cohort2 = setupMockWriteTransaction("cohort2", dataStore,
2024 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification2);
2026 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
2027 cohort2, modification2, true, false), getRef());
2028 expectMsgClass(duration, ReadyTransactionReply.class);
2030 final String transactionID3 = "tx3";
2031 final MutableCompositeModification modification3 = new MutableCompositeModification();
2032 final ShardDataTreeCohort cohort3 = setupMockWriteTransaction("cohort3", dataStore,
2033 TestModel.TEST2_PATH, ImmutableNodes.containerNode(TestModel.TEST2_QNAME), modification3);
2035 shard.tell(new ForwardedReadyTransaction(transactionID3, CURRENT_VERSION,
2036 cohort3, modification3, true, false), getRef());
2037 expectMsgClass(duration, ReadyTransactionReply.class);
2039 // All Tx's are readied. We'll send canCommit for the last one but not the others. The others
2040 // should expire from the queue and the last one should be processed.
2042 shard.tell(new CanCommitTransaction(transactionID3).toSerializable(), getRef());
2043 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS);
2045 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
2050 public void testTransactionCommitWithSubsequentExpiredCohortEntry() throws Throwable {
2051 dataStoreContextBuilder.shardCommitQueueExpiryTimeoutInMillis(1300).shardTransactionCommitTimeoutInSeconds(1);
2053 new ShardTestKit(getSystem()) {{
2054 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
2055 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
2056 "testTransactionCommitWithSubsequentExpiredCohortEntry");
2058 waitUntilLeader(shard);
2060 final FiniteDuration duration = duration("5 seconds");
2062 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
2064 final String transactionID1 = "tx1";
2065 final MutableCompositeModification modification1 = new MutableCompositeModification();
2066 final ShardDataTreeCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
2067 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification1);
2069 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
2070 cohort1, modification1, true, false), getRef());
2071 expectMsgClass(duration, ReadyTransactionReply.class);
2073 // CanCommit the first one so it's the current in-progress CohortEntry.
2075 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
2076 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS);
2078 // Ready the second Tx.
2080 final String transactionID2 = "tx2";
2081 final MutableCompositeModification modification2 = new MutableCompositeModification();
2082 final ShardDataTreeCohort cohort2 = setupMockWriteTransaction("cohort2", dataStore,
2083 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification2);
2085 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
2086 cohort2, modification2, true, false), getRef());
2087 expectMsgClass(duration, ReadyTransactionReply.class);
2089 // Ready the third Tx.
2091 final String transactionID3 = "tx3";
2092 final DataTreeModification modification3 = dataStore.getDataTree().takeSnapshot().newModification();
2093 new WriteModification(TestModel.TEST2_PATH, ImmutableNodes.containerNode(TestModel.TEST2_QNAME))
2094 .apply(modification3);
2095 modification3.ready();
2096 final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(transactionID3, modification3, true);
2098 shard.tell(readyMessage, getRef());
2100 // Commit the first Tx. After completing, the second should expire from the queue and the third
2103 shard.tell(new CommitTransaction(transactionID1).toSerializable(), getRef());
2104 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
2106 // Expect commit reply from the third Tx.
2108 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
2110 final NormalizedNode<?, ?> node = readStore(shard, TestModel.TEST2_PATH);
2111 assertNotNull(TestModel.TEST2_PATH + " not found", node);
2113 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
2118 public void testCanCommitBeforeReadyFailure() throws Throwable {
2119 new ShardTestKit(getSystem()) {{
2120 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
2121 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
2122 "testCanCommitBeforeReadyFailure");
2124 shard.tell(new CanCommitTransaction("tx").toSerializable(), getRef());
2125 expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
2127 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
2132 public void testAbortTransaction() throws Throwable {
2133 new ShardTestKit(getSystem()) {{
2134 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
2135 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
2136 "testAbortTransaction");
2138 waitUntilLeader(shard);
2140 // Setup 2 simulated transactions with mock cohorts. The first one will be aborted.
2142 final String transactionID1 = "tx1";
2143 final MutableCompositeModification modification1 = new MutableCompositeModification();
2144 final ShardDataTreeCohort cohort1 = mock(ShardDataTreeCohort.class, "cohort1");
2145 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort1).canCommit();
2146 doReturn(Futures.immediateFuture(null)).when(cohort1).abort();
2148 final String transactionID2 = "tx2";
2149 final MutableCompositeModification modification2 = new MutableCompositeModification();
2150 final ShardDataTreeCohort cohort2 = mock(ShardDataTreeCohort.class, "cohort2");
2151 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort2).canCommit();
2153 final FiniteDuration duration = duration("5 seconds");
2154 final Timeout timeout = new Timeout(duration);
2156 // Simulate the ForwardedReadyTransaction messages that would be sent
2157 // by the ShardTransaction.
2159 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
2160 cohort1, modification1, true, false), getRef());
2161 expectMsgClass(duration, ReadyTransactionReply.class);
2163 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
2164 cohort2, modification2, true, false), getRef());
2165 expectMsgClass(duration, ReadyTransactionReply.class);
2167 // Send the CanCommitTransaction message for the first Tx.
2169 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
2170 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
2171 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
2172 assertEquals("Can commit", true, canCommitReply.getCanCommit());
2174 // Send the CanCommitTransaction message for the 2nd Tx. This should get queued and
2175 // processed after the first Tx completes.
2177 final Future<Object> canCommitFuture = Patterns.ask(shard,
2178 new CanCommitTransaction(transactionID2).toSerializable(), timeout);
2180 // Send the AbortTransaction message for the first Tx. This should trigger the 2nd
2183 shard.tell(new AbortTransaction(transactionID1).toSerializable(), getRef());
2184 expectMsgClass(duration, AbortTransactionReply.SERIALIZABLE_CLASS);
2186 // Wait for the 2nd Tx to complete the canCommit phase.
2188 Await.ready(canCommitFuture, duration);
2190 final InOrder inOrder = inOrder(cohort1, cohort2);
2191 inOrder.verify(cohort1).canCommit();
2192 inOrder.verify(cohort2).canCommit();
2194 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
2199 public void testCreateSnapshot() throws Exception {
2200 testCreateSnapshot(true, "testCreateSnapshot");
2204 public void testCreateSnapshotWithNonPersistentData() throws Exception {
2205 testCreateSnapshot(false, "testCreateSnapshotWithNonPersistentData");
2208 @SuppressWarnings("serial")
2209 public void testCreateSnapshot(final boolean persistent, final String shardActorName) throws Exception{
2211 final AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(1));
2213 final AtomicReference<Object> savedSnapshot = new AtomicReference<>();
2214 class TestPersistentDataProvider extends DelegatingPersistentDataProvider {
2215 TestPersistentDataProvider(final DataPersistenceProvider delegate) {
2220 public void saveSnapshot(final Object o) {
2221 savedSnapshot.set(o);
2222 super.saveSnapshot(o);
2226 dataStoreContextBuilder.persistent(persistent);
2228 new ShardTestKit(getSystem()) {{
2229 class TestShard extends Shard {
2231 protected TestShard(final ShardIdentifier name, final Map<String, String> peerAddresses,
2232 final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
2233 super(name, peerAddresses, datastoreContext, schemaContext);
2234 setPersistence(new TestPersistentDataProvider(super.persistence()));
2238 public void handleCommand(final Object message) {
2239 super.handleCommand(message);
2241 if (message instanceof SaveSnapshotSuccess || message.equals("commit_snapshot")) {
2242 latch.get().countDown();
2247 public RaftActorContext getRaftActorContext() {
2248 return super.getRaftActorContext();
2252 final Creator<Shard> creator = new Creator<Shard>() {
2254 public Shard create() throws Exception {
2255 return new TestShard(shardID, Collections.<String,String>emptyMap(),
2256 newDatastoreContext(), SCHEMA_CONTEXT);
2260 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
2261 Props.create(new DelegatingShardCreator(creator)), shardActorName);
2263 waitUntilLeader(shard);
2265 writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
2267 final NormalizedNode<?,?> expectedRoot = readStore(shard, YangInstanceIdentifier.builder().build());
2269 // Trigger creation of a snapshot by ensuring
2270 final RaftActorContext raftActorContext = ((TestShard) shard.underlyingActor()).getRaftActorContext();
2271 raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
2273 assertEquals("Snapshot saved", true, latch.get().await(5, TimeUnit.SECONDS));
2275 assertTrue("Invalid saved snapshot " + savedSnapshot.get(),
2276 savedSnapshot.get() instanceof Snapshot);
2278 verifySnapshot((Snapshot)savedSnapshot.get(), expectedRoot);
2280 latch.set(new CountDownLatch(1));
2281 savedSnapshot.set(null);
2283 raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
2285 assertEquals("Snapshot saved", true, latch.get().await(5, TimeUnit.SECONDS));
2287 assertTrue("Invalid saved snapshot " + savedSnapshot.get(),
2288 savedSnapshot.get() instanceof Snapshot);
2290 verifySnapshot((Snapshot)savedSnapshot.get(), expectedRoot);
2292 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
2295 private void verifySnapshot(final Snapshot snapshot, final NormalizedNode<?,?> expectedRoot) {
2297 final NormalizedNode<?, ?> actual = SerializationUtils.deserializeNormalizedNode(snapshot.getState());
2298 assertEquals("Root node", expectedRoot, actual);
2304 * This test simply verifies that the applySnapShot logic will work
2305 * @throws ReadFailedException
2306 * @throws DataValidationFailedException
2309 public void testInMemoryDataTreeRestore() throws ReadFailedException, DataValidationFailedException {
2310 final DataTree store = InMemoryDataTreeFactory.getInstance().create();
2311 store.setSchemaContext(SCHEMA_CONTEXT);
2313 final DataTreeModification putTransaction = store.takeSnapshot().newModification();
2314 putTransaction.write(TestModel.TEST_PATH,
2315 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
2316 commitTransaction(store, putTransaction);
2319 final NormalizedNode<?, ?> expected = readStore(store, YangInstanceIdentifier.builder().build());
2321 final DataTreeModification writeTransaction = store.takeSnapshot().newModification();
2323 writeTransaction.delete(YangInstanceIdentifier.builder().build());
2324 writeTransaction.write(YangInstanceIdentifier.builder().build(), expected);
2326 commitTransaction(store, writeTransaction);
2328 final NormalizedNode<?, ?> actual = readStore(store, YangInstanceIdentifier.builder().build());
2330 assertEquals(expected, actual);
2334 public void testRecoveryApplicable(){
2336 final DatastoreContext persistentContext = DatastoreContext.newBuilder().
2337 shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(true).build();
2339 final Props persistentProps = Shard.props(shardID, Collections.<String, String>emptyMap(),
2340 persistentContext, SCHEMA_CONTEXT);
2342 final DatastoreContext nonPersistentContext = DatastoreContext.newBuilder().
2343 shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(false).build();
2345 final Props nonPersistentProps = Shard.props(shardID, Collections.<String, String>emptyMap(),
2346 nonPersistentContext, SCHEMA_CONTEXT);
2348 new ShardTestKit(getSystem()) {{
2349 final TestActorRef<Shard> shard1 = TestActorRef.create(getSystem(),
2350 persistentProps, "testPersistence1");
2352 assertTrue("Recovery Applicable", shard1.underlyingActor().persistence().isRecoveryApplicable());
2354 shard1.tell(PoisonPill.getInstance(), ActorRef.noSender());
2356 final TestActorRef<Shard> shard2 = TestActorRef.create(getSystem(),
2357 nonPersistentProps, "testPersistence2");
2359 assertFalse("Recovery Not Applicable", shard2.underlyingActor().persistence().isRecoveryApplicable());
2361 shard2.tell(PoisonPill.getInstance(), ActorRef.noSender());
2368 public void testOnDatastoreContext() {
2369 new ShardTestKit(getSystem()) {{
2370 dataStoreContextBuilder.persistent(true);
2372 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardProps(), "testOnDatastoreContext");
2374 assertEquals("isRecoveryApplicable", true,
2375 shard.underlyingActor().persistence().isRecoveryApplicable());
2377 waitUntilLeader(shard);
2379 shard.tell(dataStoreContextBuilder.persistent(false).build(), ActorRef.noSender());
2381 assertEquals("isRecoveryApplicable", false,
2382 shard.underlyingActor().persistence().isRecoveryApplicable());
2384 shard.tell(dataStoreContextBuilder.persistent(true).build(), ActorRef.noSender());
2386 assertEquals("isRecoveryApplicable", true,
2387 shard.underlyingActor().persistence().isRecoveryApplicable());
2389 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
2394 public void testRegisterRoleChangeListener() throws Exception {
2395 new ShardTestKit(getSystem()) {
2397 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
2398 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
2399 "testRegisterRoleChangeListener");
2401 waitUntilLeader(shard);
2403 final TestActorRef<MessageCollectorActor> listener =
2404 TestActorRef.create(getSystem(), Props.create(MessageCollectorActor.class));
2406 shard.tell(new RegisterRoleChangeListener(), listener);
2408 MessageCollectorActor.expectFirstMatching(listener, RegisterRoleChangeListenerReply.class);
2410 ShardLeaderStateChanged leaderStateChanged = MessageCollectorActor.expectFirstMatching(listener,
2411 ShardLeaderStateChanged.class);
2412 assertEquals("getLocalShardDataTree present", true,
2413 leaderStateChanged.getLocalShardDataTree().isPresent());
2414 assertSame("getLocalShardDataTree", shard.underlyingActor().getDataStore().getDataTree(),
2415 leaderStateChanged.getLocalShardDataTree().get());
2417 MessageCollectorActor.clearMessages(listener);
2419 // Force a leader change
2421 shard.tell(new RequestVote(10000, "member2", 50, 50), getRef());
2423 leaderStateChanged = MessageCollectorActor.expectFirstMatching(listener,
2424 ShardLeaderStateChanged.class);
2425 assertEquals("getLocalShardDataTree present", false,
2426 leaderStateChanged.getLocalShardDataTree().isPresent());
2428 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
2434 public void testFollowerInitialSyncStatus() throws Exception {
2435 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
2436 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
2437 "testFollowerInitialSyncStatus");
2439 shard.underlyingActor().onReceiveCommand(new FollowerInitialSyncUpStatus(false, "member-1-shard-inventory-operational"));
2441 assertEquals(false, shard.underlyingActor().getShardMBean().getFollowerInitialSyncStatus());
2443 shard.underlyingActor().onReceiveCommand(new FollowerInitialSyncUpStatus(true, "member-1-shard-inventory-operational"));
2445 assertEquals(true, shard.underlyingActor().getShardMBean().getFollowerInitialSyncStatus());
2447 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
2450 private static void commitTransaction(final DataTree store, final DataTreeModification modification) throws DataValidationFailedException {
2451 modification.ready();
2452 store.validate(modification);
2453 store.commit(store.prepare(modification));