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.assertTrue;
8 import static org.mockito.Mockito.doReturn;
9 import static org.mockito.Mockito.inOrder;
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.reset;
12 import static org.opendaylight.controller.cluster.datastore.DataStoreVersions.CURRENT_VERSION;
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSelection;
15 import akka.actor.PoisonPill;
16 import akka.actor.Props;
17 import akka.dispatch.Dispatchers;
18 import akka.dispatch.OnComplete;
19 import akka.japi.Creator;
20 import akka.pattern.Patterns;
21 import akka.persistence.SaveSnapshotSuccess;
22 import akka.testkit.TestActorRef;
23 import akka.util.Timeout;
24 import com.google.common.base.Function;
25 import com.google.common.base.Optional;
26 import com.google.common.util.concurrent.Futures;
27 import com.google.common.util.concurrent.ListenableFuture;
28 import com.google.common.util.concurrent.MoreExecutors;
29 import com.google.common.util.concurrent.Uninterruptibles;
30 import java.io.IOException;
31 import java.util.Collections;
32 import java.util.HashSet;
33 import java.util.List;
36 import java.util.concurrent.CountDownLatch;
37 import java.util.concurrent.ExecutionException;
38 import java.util.concurrent.TimeUnit;
39 import java.util.concurrent.atomic.AtomicBoolean;
40 import java.util.concurrent.atomic.AtomicReference;
41 import org.junit.Test;
42 import org.mockito.InOrder;
43 import org.opendaylight.controller.cluster.DataPersistenceProvider;
44 import org.opendaylight.controller.cluster.DelegatingPersistentDataProvider;
45 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
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.ReadyTransactionReply;
60 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
61 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
62 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
63 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
64 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
65 import org.opendaylight.controller.cluster.datastore.modification.Modification;
66 import org.opendaylight.controller.cluster.datastore.modification.ModificationPayload;
67 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
68 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
69 import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor;
70 import org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener;
71 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
72 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
73 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply;
74 import org.opendaylight.controller.cluster.raft.RaftActorContext;
75 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
76 import org.opendaylight.controller.cluster.raft.ReplicatedLogImplEntry;
77 import org.opendaylight.controller.cluster.raft.Snapshot;
78 import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries;
79 import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot;
80 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
81 import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
82 import org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus;
83 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
84 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
85 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
86 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
87 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
88 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
89 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
90 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
91 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
92 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
93 import org.opendaylight.controller.protobuff.messages.cohort3pc.ThreePhaseCommitCohortMessages;
94 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
95 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
96 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
97 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
98 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
99 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
100 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
101 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
102 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
103 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
104 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
105 import scala.concurrent.Await;
106 import scala.concurrent.Future;
107 import scala.concurrent.duration.FiniteDuration;
109 public class ShardTest extends AbstractShardTest {
112 public void testRegisterChangeListener() throws Exception {
113 new ShardTestKit(getSystem()) {{
114 TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
115 newShardProps(), "testRegisterChangeListener");
117 waitUntilLeader(shard);
119 shard.tell(new UpdateSchemaContext(SchemaContextHelper.full()), ActorRef.noSender());
121 MockDataChangeListener listener = new MockDataChangeListener(1);
122 ActorRef dclActor = getSystem().actorOf(DataChangeListener.props(listener),
123 "testRegisterChangeListener-DataChangeListener");
125 shard.tell(new RegisterChangeListener(TestModel.TEST_PATH,
126 dclActor, AsyncDataBroker.DataChangeScope.BASE), getRef());
128 RegisterChangeListenerReply reply = expectMsgClass(duration("3 seconds"),
129 RegisterChangeListenerReply.class);
130 String replyPath = reply.getListenerRegistrationPath().toString();
131 assertTrue("Incorrect reply path: " + replyPath, replyPath.matches(
132 "akka:\\/\\/test\\/user\\/testRegisterChangeListener\\/\\$.*"));
134 YangInstanceIdentifier path = TestModel.TEST_PATH;
135 writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
137 listener.waitForChangeEvents(path);
139 dclActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
140 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
144 @SuppressWarnings("serial")
146 public void testChangeListenerNotifiedWhenNotTheLeaderOnRegistration() throws Exception {
147 // This test tests the timing window in which a change listener is registered before the
148 // shard becomes the leader. We verify that the listener is registered and notified of the
149 // existing data when the shard becomes the leader.
150 new ShardTestKit(getSystem()) {{
151 // For this test, we want to send the RegisterChangeListener message after the shard
152 // has recovered from persistence and before it becomes the leader. So we subclass
153 // Shard to override onReceiveCommand and, when the first ElectionTimeout is received,
154 // we know that the shard has been initialized to a follower and has started the
155 // election process. The following 2 CountDownLatches are used to coordinate the
156 // ElectionTimeout with the sending of the RegisterChangeListener message.
157 final CountDownLatch onFirstElectionTimeout = new CountDownLatch(1);
158 final CountDownLatch onChangeListenerRegistered = new CountDownLatch(1);
159 Creator<Shard> creator = new Creator<Shard>() {
160 boolean firstElectionTimeout = true;
163 public Shard create() throws Exception {
164 // Use a non persistent provider because this test actually invokes persist on the journal
165 // this will cause all other messages to not be queued properly after that.
166 // The basic issue is that you cannot use TestActorRef with a persistent actor (at least when
167 // it does do a persist)
168 return new Shard(shardID, Collections.<String,String>emptyMap(),
169 dataStoreContextBuilder.persistent(false).build(), SCHEMA_CONTEXT) {
171 public void onReceiveCommand(final Object message) throws Exception {
172 if(message instanceof ElectionTimeout && firstElectionTimeout) {
173 // Got the first ElectionTimeout. We don't forward it to the
174 // base Shard yet until we've sent the RegisterChangeListener
175 // message. So we signal the onFirstElectionTimeout latch to tell
176 // the main thread to send the RegisterChangeListener message and
177 // start a thread to wait on the onChangeListenerRegistered latch,
178 // which the main thread signals after it has sent the message.
179 // After the onChangeListenerRegistered is triggered, we send the
180 // original ElectionTimeout message to proceed with the election.
181 firstElectionTimeout = false;
182 final ActorRef self = getSelf();
186 Uninterruptibles.awaitUninterruptibly(
187 onChangeListenerRegistered, 5, TimeUnit.SECONDS);
188 self.tell(message, self);
192 onFirstElectionTimeout.countDown();
194 super.onReceiveCommand(message);
201 MockDataChangeListener listener = new MockDataChangeListener(1);
202 ActorRef dclActor = getSystem().actorOf(DataChangeListener.props(listener),
203 "testRegisterChangeListenerWhenNotLeaderInitially-DataChangeListener");
205 TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
206 Props.create(new DelegatingShardCreator(creator)),
207 "testRegisterChangeListenerWhenNotLeaderInitially");
209 // Write initial data into the in-memory store.
210 YangInstanceIdentifier path = TestModel.TEST_PATH;
211 writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
213 // Wait until the shard receives the first ElectionTimeout message.
214 assertEquals("Got first ElectionTimeout", true,
215 onFirstElectionTimeout.await(5, TimeUnit.SECONDS));
217 // Now send the RegisterChangeListener and wait for the reply.
218 shard.tell(new RegisterChangeListener(path, dclActor,
219 AsyncDataBroker.DataChangeScope.SUBTREE), getRef());
221 RegisterChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
222 RegisterChangeListenerReply.class);
223 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
225 // Sanity check - verify the shard is not the leader yet.
226 shard.tell(new FindLeader(), getRef());
227 FindLeaderReply findLeadeReply =
228 expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
229 assertNull("Expected the shard not to be the leader", findLeadeReply.getLeaderActor());
231 // Signal the onChangeListenerRegistered latch to tell the thread above to proceed
232 // with the election process.
233 onChangeListenerRegistered.countDown();
235 // Wait for the shard to become the leader and notify our listener with the existing
236 // data in the store.
237 listener.waitForChangeEvents(path);
239 dclActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
240 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
245 public void testCreateTransaction(){
246 new ShardTestKit(getSystem()) {{
247 ActorRef shard = getSystem().actorOf(newShardProps(), "testCreateTransaction");
249 waitUntilLeader(shard);
251 shard.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
253 shard.tell(new CreateTransaction("txn-1",
254 TransactionProxy.TransactionType.READ_ONLY.ordinal() ).toSerializable(), getRef());
256 CreateTransactionReply reply = expectMsgClass(duration("3 seconds"),
257 CreateTransactionReply.class);
259 String path = reply.getTransactionActorPath().toString();
260 assertTrue("Unexpected transaction path " + path,
261 path.contains("akka://test/user/testCreateTransaction/shard-txn-1"));
263 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
268 public void testCreateTransactionOnChain(){
269 new ShardTestKit(getSystem()) {{
270 final ActorRef shard = getSystem().actorOf(newShardProps(), "testCreateTransactionOnChain");
272 waitUntilLeader(shard);
274 shard.tell(new CreateTransaction("txn-1",
275 TransactionProxy.TransactionType.READ_ONLY.ordinal() , "foobar").toSerializable(),
278 CreateTransactionReply reply = expectMsgClass(duration("3 seconds"),
279 CreateTransactionReply.class);
281 String path = reply.getTransactionActorPath().toString();
282 assertTrue("Unexpected transaction path " + path,
283 path.contains("akka://test/user/testCreateTransactionOnChain/shard-txn-1"));
285 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
289 @SuppressWarnings("serial")
291 public void testPeerAddressResolved() throws Exception {
292 new ShardTestKit(getSystem()) {{
293 final CountDownLatch recoveryComplete = new CountDownLatch(1);
294 class TestShard extends Shard {
296 super(shardID, Collections.<String, String>singletonMap(shardID.toString(), null),
297 newDatastoreContext(), SCHEMA_CONTEXT);
300 Map<String, String> getPeerAddresses() {
301 return getRaftActorContext().getPeerAddresses();
305 protected void onRecoveryComplete() {
307 super.onRecoveryComplete();
309 recoveryComplete.countDown();
314 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
315 Props.create(new DelegatingShardCreator(new Creator<Shard>() {
317 public TestShard create() throws Exception {
318 return new TestShard();
320 })), "testPeerAddressResolved");
322 //waitUntilLeader(shard);
323 assertEquals("Recovery complete", true,
324 Uninterruptibles.awaitUninterruptibly(recoveryComplete, 5, TimeUnit.SECONDS));
326 String address = "akka://foobar";
327 shard.underlyingActor().onReceiveCommand(new PeerAddressResolved(shardID.toString(), address));
329 assertEquals("getPeerAddresses", address,
330 ((TestShard)shard.underlyingActor()).getPeerAddresses().get(shardID.toString()));
332 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
337 public void testApplySnapshot() throws Exception {
338 TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardProps(),
339 "testApplySnapshot");
341 InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
342 store.onGlobalContextUpdated(SCHEMA_CONTEXT);
344 writeToStore(store, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
346 YangInstanceIdentifier root = YangInstanceIdentifier.builder().build();
347 NormalizedNode<?,?> expected = readStore(store, root);
349 ApplySnapshot applySnapshot = new ApplySnapshot(Snapshot.create(
350 SerializationUtils.serializeNormalizedNode(expected),
351 Collections.<ReplicatedLogEntry>emptyList(), 1, 2, 3, 4));
353 shard.underlyingActor().onReceiveCommand(applySnapshot);
355 NormalizedNode<?,?> actual = readStore(shard, root);
357 assertEquals("Root node", expected, actual);
359 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
363 public void testApplyState() throws Exception {
365 TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardProps(), "testApplyState");
367 NormalizedNode<?, ?> node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
369 ApplyState applyState = new ApplyState(null, "test", new ReplicatedLogImplEntry(1, 2,
370 newModificationPayload(new WriteModification(TestModel.TEST_PATH, node))));
372 shard.underlyingActor().onReceiveCommand(applyState);
374 NormalizedNode<?,?> actual = readStore(shard, TestModel.TEST_PATH);
375 assertEquals("Applied state", node, actual);
377 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
381 public void testRecovery() throws Exception {
383 // Set up the InMemorySnapshotStore.
385 InMemoryDOMDataStore testStore = InMemoryDOMDataStoreFactory.create("Test", null, null);
386 testStore.onGlobalContextUpdated(SCHEMA_CONTEXT);
388 writeToStore(testStore, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
390 NormalizedNode<?, ?> root = readStore(testStore, YangInstanceIdentifier.builder().build());
392 InMemorySnapshotStore.addSnapshot(shardID.toString(), Snapshot.create(
393 SerializationUtils.serializeNormalizedNode(root),
394 Collections.<ReplicatedLogEntry>emptyList(), 0, 1, -1, -1));
396 // Set up the InMemoryJournal.
398 InMemoryJournal.addEntry(shardID.toString(), 0, new ReplicatedLogImplEntry(0, 1, newModificationPayload(
399 new WriteModification(TestModel.OUTER_LIST_PATH,
400 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build()))));
402 int nListEntries = 16;
403 Set<Integer> listEntryKeys = new HashSet<>();
405 // Add some ModificationPayload entries
406 for(int i = 1; i <= nListEntries; i++) {
407 listEntryKeys.add(Integer.valueOf(i));
408 YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
409 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i).build();
410 Modification mod = new MergeModification(path,
411 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i));
412 InMemoryJournal.addEntry(shardID.toString(), i, new ReplicatedLogImplEntry(i, 1,
413 newModificationPayload(mod)));
416 InMemoryJournal.addEntry(shardID.toString(), nListEntries + 1,
417 new ApplyJournalEntries(nListEntries));
419 testRecovery(listEntryKeys);
422 private ModificationPayload newModificationPayload(final Modification... mods) throws IOException {
423 MutableCompositeModification compMod = new MutableCompositeModification();
424 for(Modification mod: mods) {
425 compMod.addModification(mod);
428 return new ModificationPayload(compMod);
431 @SuppressWarnings({ "unchecked" })
433 public void testConcurrentThreePhaseCommits() throws Throwable {
434 new ShardTestKit(getSystem()) {{
435 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
436 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
437 "testConcurrentThreePhaseCommits");
439 waitUntilLeader(shard);
441 // Setup 3 simulated transactions with mock cohorts backed by real cohorts.
443 InMemoryDOMDataStore dataStore = shard.underlyingActor().getDataStore();
445 String transactionID1 = "tx1";
446 MutableCompositeModification modification1 = new MutableCompositeModification();
447 DOMStoreThreePhaseCommitCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
448 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification1);
450 String transactionID2 = "tx2";
451 MutableCompositeModification modification2 = new MutableCompositeModification();
452 DOMStoreThreePhaseCommitCohort cohort2 = setupMockWriteTransaction("cohort2", dataStore,
453 TestModel.OUTER_LIST_PATH,
454 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(),
457 String transactionID3 = "tx3";
458 MutableCompositeModification modification3 = new MutableCompositeModification();
459 DOMStoreThreePhaseCommitCohort cohort3 = setupMockWriteTransaction("cohort3", dataStore,
460 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
461 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
462 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1),
466 final FiniteDuration duration = FiniteDuration.create(timeoutSec, TimeUnit.SECONDS);
467 final Timeout timeout = new Timeout(duration);
469 // Simulate the ForwardedReadyTransaction message for the first Tx that would be sent
470 // by the ShardTransaction.
472 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
473 cohort1, modification1, true, false), getRef());
474 ReadyTransactionReply readyReply = ReadyTransactionReply.fromSerializable(
475 expectMsgClass(duration, ReadyTransactionReply.class));
476 assertEquals("Cohort path", shard.path().toString(), readyReply.getCohortPath());
478 // Send the CanCommitTransaction message for the first Tx.
480 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
481 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
482 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
483 assertEquals("Can commit", true, canCommitReply.getCanCommit());
485 // Send the ForwardedReadyTransaction for the next 2 Tx's.
487 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
488 cohort2, modification2, true, false), getRef());
489 expectMsgClass(duration, ReadyTransactionReply.class);
491 shard.tell(new ForwardedReadyTransaction(transactionID3, CURRENT_VERSION,
492 cohort3, modification3, true, false), getRef());
493 expectMsgClass(duration, ReadyTransactionReply.class);
495 // Send the CanCommitTransaction message for the next 2 Tx's. These should get queued and
496 // processed after the first Tx completes.
498 Future<Object> canCommitFuture1 = Patterns.ask(shard,
499 new CanCommitTransaction(transactionID2).toSerializable(), timeout);
501 Future<Object> canCommitFuture2 = Patterns.ask(shard,
502 new CanCommitTransaction(transactionID3).toSerializable(), timeout);
504 // Send the CommitTransaction message for the first Tx. After it completes, it should
505 // trigger the 2nd Tx to proceed which should in turn then trigger the 3rd.
507 shard.tell(new CommitTransaction(transactionID1).toSerializable(), getRef());
508 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
510 // Wait for the next 2 Tx's to complete.
512 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
513 final CountDownLatch commitLatch = new CountDownLatch(2);
515 class OnFutureComplete extends OnComplete<Object> {
516 private final Class<?> expRespType;
518 OnFutureComplete(final Class<?> expRespType) {
519 this.expRespType = expRespType;
523 public void onComplete(final Throwable error, final Object resp) {
525 caughtEx.set(new AssertionError(getClass().getSimpleName() + " failure", error));
528 assertEquals("Commit response type", expRespType, resp.getClass());
530 } catch (Exception e) {
536 void onSuccess(final Object resp) throws Exception {
540 class OnCommitFutureComplete extends OnFutureComplete {
541 OnCommitFutureComplete() {
542 super(CommitTransactionReply.SERIALIZABLE_CLASS);
546 public void onComplete(final Throwable error, final Object resp) {
547 super.onComplete(error, resp);
548 commitLatch.countDown();
552 class OnCanCommitFutureComplete extends OnFutureComplete {
553 private final String transactionID;
555 OnCanCommitFutureComplete(final String transactionID) {
556 super(CanCommitTransactionReply.SERIALIZABLE_CLASS);
557 this.transactionID = transactionID;
561 void onSuccess(final Object resp) throws Exception {
562 CanCommitTransactionReply canCommitReply =
563 CanCommitTransactionReply.fromSerializable(resp);
564 assertEquals("Can commit", true, canCommitReply.getCanCommit());
566 Future<Object> commitFuture = Patterns.ask(shard,
567 new CommitTransaction(transactionID).toSerializable(), timeout);
568 commitFuture.onComplete(new OnCommitFutureComplete(), getSystem().dispatcher());
572 canCommitFuture1.onComplete(new OnCanCommitFutureComplete(transactionID2),
573 getSystem().dispatcher());
575 canCommitFuture2.onComplete(new OnCanCommitFutureComplete(transactionID3),
576 getSystem().dispatcher());
578 boolean done = commitLatch.await(timeoutSec, TimeUnit.SECONDS);
580 if(caughtEx.get() != null) {
581 throw caughtEx.get();
584 assertEquals("Commits complete", true, done);
586 InOrder inOrder = inOrder(cohort1, cohort2, cohort3);
587 inOrder.verify(cohort1).canCommit();
588 inOrder.verify(cohort1).preCommit();
589 inOrder.verify(cohort1).commit();
590 inOrder.verify(cohort2).canCommit();
591 inOrder.verify(cohort2).preCommit();
592 inOrder.verify(cohort2).commit();
593 inOrder.verify(cohort3).canCommit();
594 inOrder.verify(cohort3).preCommit();
595 inOrder.verify(cohort3).commit();
597 // Verify data in the data store.
599 verifyOuterListEntry(shard, 1);
601 verifyLastApplied(shard, 2);
603 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
607 private BatchedModifications newBatchedModifications(String transactionID, YangInstanceIdentifier path,
608 NormalizedNode<?, ?> data, boolean ready, boolean doCommitOnReady) {
609 return newBatchedModifications(transactionID, null, path, data, ready, doCommitOnReady);
612 private BatchedModifications newBatchedModifications(String transactionID, String transactionChainID,
613 YangInstanceIdentifier path, NormalizedNode<?, ?> data, boolean ready, boolean doCommitOnReady) {
614 BatchedModifications batched = new BatchedModifications(transactionID, CURRENT_VERSION, transactionChainID);
615 batched.addModification(new WriteModification(path, data));
616 batched.setReady(ready);
617 batched.setDoCommitOnReady(doCommitOnReady);
622 public void testBatchedModificationsWithNoCommitOnReady() throws Throwable {
623 new ShardTestKit(getSystem()) {{
624 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
625 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
626 "testBatchedModificationsWithNoCommitOnReady");
628 waitUntilLeader(shard);
630 final String transactionID = "tx";
631 FiniteDuration duration = duration("5 seconds");
633 final AtomicReference<DOMStoreThreePhaseCommitCohort> mockCohort = new AtomicReference<>();
634 ShardCommitCoordinator.CohortDecorator cohortDecorator = new ShardCommitCoordinator.CohortDecorator() {
636 public DOMStoreThreePhaseCommitCohort decorate(String txID, DOMStoreThreePhaseCommitCohort actual) {
637 if(mockCohort.get() == null) {
638 mockCohort.set(createDelegatingMockCohort("cohort", actual));
641 return mockCohort.get();
645 shard.underlyingActor().getCommitCoordinator().setCohortDecorator(cohortDecorator);
647 // Send a BatchedModifications to start a transaction.
649 shard.tell(newBatchedModifications(transactionID, TestModel.TEST_PATH,
650 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false, false), getRef());
651 expectMsgClass(duration, BatchedModificationsReply.class);
653 // Send a couple more BatchedModifications.
655 shard.tell(newBatchedModifications(transactionID, TestModel.OUTER_LIST_PATH,
656 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false, false), getRef());
657 expectMsgClass(duration, BatchedModificationsReply.class);
659 shard.tell(newBatchedModifications(transactionID, YangInstanceIdentifier.builder(
660 TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
661 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), true, false), getRef());
662 expectMsgClass(duration, ReadyTransactionReply.class);
664 // Send the CanCommitTransaction message.
666 shard.tell(new CanCommitTransaction(transactionID).toSerializable(), getRef());
667 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
668 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
669 assertEquals("Can commit", true, canCommitReply.getCanCommit());
671 // Send the CanCommitTransaction message.
673 shard.tell(new CommitTransaction(transactionID).toSerializable(), getRef());
674 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
676 InOrder inOrder = inOrder(mockCohort.get());
677 inOrder.verify(mockCohort.get()).canCommit();
678 inOrder.verify(mockCohort.get()).preCommit();
679 inOrder.verify(mockCohort.get()).commit();
681 // Verify data in the data store.
683 verifyOuterListEntry(shard, 1);
685 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
690 public void testBatchedModificationsWithCommitOnReady() throws Throwable {
691 new ShardTestKit(getSystem()) {{
692 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
693 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
694 "testBatchedModificationsWithCommitOnReady");
696 waitUntilLeader(shard);
698 final String transactionID = "tx";
699 FiniteDuration duration = duration("5 seconds");
701 final AtomicReference<DOMStoreThreePhaseCommitCohort> mockCohort = new AtomicReference<>();
702 ShardCommitCoordinator.CohortDecorator cohortDecorator = new ShardCommitCoordinator.CohortDecorator() {
704 public DOMStoreThreePhaseCommitCohort decorate(String txID, DOMStoreThreePhaseCommitCohort actual) {
705 if(mockCohort.get() == null) {
706 mockCohort.set(createDelegatingMockCohort("cohort", actual));
709 return mockCohort.get();
713 shard.underlyingActor().getCommitCoordinator().setCohortDecorator(cohortDecorator);
715 // Send a BatchedModifications to start a transaction.
717 shard.tell(newBatchedModifications(transactionID, TestModel.TEST_PATH,
718 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false, false), getRef());
719 expectMsgClass(duration, BatchedModificationsReply.class);
721 // Send a couple more BatchedModifications.
723 shard.tell(newBatchedModifications(transactionID, TestModel.OUTER_LIST_PATH,
724 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false, false), getRef());
725 expectMsgClass(duration, BatchedModificationsReply.class);
727 shard.tell(newBatchedModifications(transactionID, YangInstanceIdentifier.builder(
728 TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
729 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), true, true), getRef());
731 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
733 InOrder inOrder = inOrder(mockCohort.get());
734 inOrder.verify(mockCohort.get()).canCommit();
735 inOrder.verify(mockCohort.get()).preCommit();
736 inOrder.verify(mockCohort.get()).commit();
738 // Verify data in the data store.
740 verifyOuterListEntry(shard, 1);
742 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
746 @SuppressWarnings("unchecked")
747 private void verifyOuterListEntry(final TestActorRef<Shard> shard, Object expIDValue) throws Exception {
748 NormalizedNode<?, ?> outerList = readStore(shard, TestModel.OUTER_LIST_PATH);
749 assertNotNull(TestModel.OUTER_LIST_QNAME.getLocalName() + " not found", outerList);
750 assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " value is not Iterable",
751 outerList.getValue() instanceof Iterable);
752 Object entry = ((Iterable<Object>)outerList.getValue()).iterator().next();
753 assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " entry is not MapEntryNode",
754 entry instanceof MapEntryNode);
755 MapEntryNode mapEntry = (MapEntryNode)entry;
756 Optional<DataContainerChild<? extends PathArgument, ?>> idLeaf =
757 mapEntry.getChild(new YangInstanceIdentifier.NodeIdentifier(TestModel.ID_QNAME));
758 assertTrue("Missing leaf " + TestModel.ID_QNAME.getLocalName(), idLeaf.isPresent());
759 assertEquals(TestModel.ID_QNAME.getLocalName() + " value", expIDValue, idLeaf.get().getValue());
763 public void testBatchedModificationsOnTransactionChain() throws Throwable {
764 new ShardTestKit(getSystem()) {{
765 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
766 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
767 "testBatchedModificationsOnTransactionChain");
769 waitUntilLeader(shard);
771 String transactionChainID = "txChain";
772 String transactionID1 = "tx1";
773 String transactionID2 = "tx2";
775 FiniteDuration duration = duration("5 seconds");
777 // Send a BatchedModifications to start a chained write transaction and ready it.
779 ContainerNode containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
780 YangInstanceIdentifier path = TestModel.TEST_PATH;
781 shard.tell(newBatchedModifications(transactionID1, transactionChainID, path,
782 containerNode, true, false), getRef());
783 expectMsgClass(duration, ReadyTransactionReply.class);
785 // Create a read Tx on the same chain.
787 shard.tell(new CreateTransaction(transactionID2, TransactionProxy.TransactionType.READ_ONLY.ordinal() ,
788 transactionChainID).toSerializable(), getRef());
790 CreateTransactionReply createReply = expectMsgClass(duration("3 seconds"), CreateTransactionReply.class);
792 getSystem().actorSelection(createReply.getTransactionActorPath()).tell(new ReadData(path), getRef());
793 ReadDataReply readReply = expectMsgClass(duration("3 seconds"), ReadDataReply.class);
794 assertEquals("Read node", containerNode, readReply.getNormalizedNode());
796 // Commit the write transaction.
798 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
799 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
800 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
801 assertEquals("Can commit", true, canCommitReply.getCanCommit());
803 shard.tell(new CommitTransaction(transactionID1).toSerializable(), getRef());
804 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
806 // Verify data in the data store.
808 NormalizedNode<?, ?> actualNode = readStore(shard, path);
809 assertEquals("Stored node", containerNode, actualNode);
811 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
816 public void testOnBatchedModificationsWhenNotLeader() {
817 final AtomicBoolean overrideLeaderCalls = new AtomicBoolean();
818 new ShardTestKit(getSystem()) {{
819 Creator<Shard> creator = new Creator<Shard>() {
821 public Shard create() throws Exception {
822 return new Shard(shardID, Collections.<String,String>emptyMap(),
823 newDatastoreContext(), SCHEMA_CONTEXT) {
825 protected boolean isLeader() {
826 return overrideLeaderCalls.get() ? false : super.isLeader();
830 protected ActorSelection getLeader() {
831 return overrideLeaderCalls.get() ? getSystem().actorSelection(getRef().path()) :
838 TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
839 Props.create(new DelegatingShardCreator(creator)), "testOnBatchedModificationsWhenNotLeader");
841 waitUntilLeader(shard);
843 overrideLeaderCalls.set(true);
845 BatchedModifications batched = new BatchedModifications("tx", DataStoreVersions.CURRENT_VERSION, "");
847 shard.tell(batched, ActorRef.noSender());
849 expectMsgEquals(batched);
851 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
856 public void testForwardedReadyTransactionWithImmediateCommit() throws Exception{
857 new ShardTestKit(getSystem()) {{
858 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
859 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
860 "testForwardedReadyTransactionWithImmediateCommit");
862 waitUntilLeader(shard);
864 InMemoryDOMDataStore dataStore = shard.underlyingActor().getDataStore();
866 String transactionID = "tx1";
867 MutableCompositeModification modification = new MutableCompositeModification();
868 NormalizedNode<?, ?> containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
869 DOMStoreThreePhaseCommitCohort cohort = setupMockWriteTransaction("cohort", dataStore,
870 TestModel.TEST_PATH, containerNode, modification);
872 FiniteDuration duration = duration("5 seconds");
874 // Simulate the ForwardedReadyTransaction messages that would be sent
875 // by the ShardTransaction.
877 shard.tell(new ForwardedReadyTransaction(transactionID, CURRENT_VERSION,
878 cohort, modification, true, true), getRef());
880 expectMsgClass(duration, ThreePhaseCommitCohortMessages.CommitTransactionReply.class);
882 InOrder inOrder = inOrder(cohort);
883 inOrder.verify(cohort).canCommit();
884 inOrder.verify(cohort).preCommit();
885 inOrder.verify(cohort).commit();
887 NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.TEST_PATH);
888 assertEquals(TestModel.TEST_QNAME.getLocalName(), containerNode, actualNode);
890 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
895 public void testCommitWithPersistenceDisabled() throws Throwable {
896 dataStoreContextBuilder.persistent(false);
897 new ShardTestKit(getSystem()) {{
898 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
899 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
900 "testCommitWithPersistenceDisabled");
902 waitUntilLeader(shard);
904 InMemoryDOMDataStore dataStore = shard.underlyingActor().getDataStore();
906 // Setup a simulated transactions with a mock cohort.
908 String transactionID = "tx";
909 MutableCompositeModification modification = new MutableCompositeModification();
910 NormalizedNode<?, ?> containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
911 DOMStoreThreePhaseCommitCohort cohort = setupMockWriteTransaction("cohort", dataStore,
912 TestModel.TEST_PATH, containerNode, modification);
914 FiniteDuration duration = duration("5 seconds");
916 // Simulate the ForwardedReadyTransaction messages that would be sent
917 // by the ShardTransaction.
919 shard.tell(new ForwardedReadyTransaction(transactionID, CURRENT_VERSION,
920 cohort, modification, true, false), getRef());
921 expectMsgClass(duration, ReadyTransactionReply.class);
923 // Send the CanCommitTransaction message.
925 shard.tell(new CanCommitTransaction(transactionID).toSerializable(), getRef());
926 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
927 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
928 assertEquals("Can commit", true, canCommitReply.getCanCommit());
930 // Send the CanCommitTransaction message.
932 shard.tell(new CommitTransaction(transactionID).toSerializable(), getRef());
933 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
935 InOrder inOrder = inOrder(cohort);
936 inOrder.verify(cohort).canCommit();
937 inOrder.verify(cohort).preCommit();
938 inOrder.verify(cohort).commit();
940 NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.TEST_PATH);
941 assertEquals(TestModel.TEST_QNAME.getLocalName(), containerNode, actualNode);
943 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
948 public void testCommitWhenTransactionHasNoModifications(){
949 // Note that persistence is enabled which would normally result in the entry getting written to the journal
950 // but here that need not happen
951 new ShardTestKit(getSystem()) {
953 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
954 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
955 "testCommitWhenTransactionHasNoModifications");
957 waitUntilLeader(shard);
959 String transactionID = "tx1";
960 MutableCompositeModification modification = new MutableCompositeModification();
961 DOMStoreThreePhaseCommitCohort cohort = mock(DOMStoreThreePhaseCommitCohort.class, "cohort1");
962 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
963 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).preCommit();
964 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).commit();
966 FiniteDuration duration = duration("5 seconds");
968 // Simulate the ForwardedReadyTransaction messages that would be sent
969 // by the ShardTransaction.
971 shard.tell(new ForwardedReadyTransaction(transactionID, CURRENT_VERSION,
972 cohort, modification, true, false), getRef());
973 expectMsgClass(duration, ReadyTransactionReply.class);
975 // Send the CanCommitTransaction message.
977 shard.tell(new CanCommitTransaction(transactionID).toSerializable(), getRef());
978 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
979 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
980 assertEquals("Can commit", true, canCommitReply.getCanCommit());
982 shard.tell(new CommitTransaction(transactionID).toSerializable(), getRef());
983 expectMsgClass(duration, ThreePhaseCommitCohortMessages.CommitTransactionReply.class);
985 InOrder inOrder = inOrder(cohort);
986 inOrder.verify(cohort).canCommit();
987 inOrder.verify(cohort).preCommit();
988 inOrder.verify(cohort).commit();
990 // Use MBean for verification
991 // Committed transaction count should increase as usual
992 assertEquals(1,shard.underlyingActor().getShardMBean().getCommittedTransactionsCount());
994 // Commit index should not advance because this does not go into the journal
995 assertEquals(-1, shard.underlyingActor().getShardMBean().getCommitIndex());
997 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1004 public void testCommitWhenTransactionHasModifications(){
1005 new ShardTestKit(getSystem()) {
1007 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1008 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1009 "testCommitWhenTransactionHasModifications");
1011 waitUntilLeader(shard);
1013 String transactionID = "tx1";
1014 MutableCompositeModification modification = new MutableCompositeModification();
1015 modification.addModification(new DeleteModification(YangInstanceIdentifier.builder().build()));
1016 DOMStoreThreePhaseCommitCohort cohort = mock(DOMStoreThreePhaseCommitCohort.class, "cohort1");
1017 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1018 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).preCommit();
1019 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).commit();
1021 FiniteDuration duration = duration("5 seconds");
1023 // Simulate the ForwardedReadyTransaction messages that would be sent
1024 // by the ShardTransaction.
1026 shard.tell(new ForwardedReadyTransaction(transactionID, CURRENT_VERSION,
1027 cohort, modification, true, false), getRef());
1028 expectMsgClass(duration, ReadyTransactionReply.class);
1030 // Send the CanCommitTransaction message.
1032 shard.tell(new CanCommitTransaction(transactionID).toSerializable(), getRef());
1033 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1034 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
1035 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1037 shard.tell(new CommitTransaction(transactionID).toSerializable(), getRef());
1038 expectMsgClass(duration, ThreePhaseCommitCohortMessages.CommitTransactionReply.class);
1040 InOrder inOrder = inOrder(cohort);
1041 inOrder.verify(cohort).canCommit();
1042 inOrder.verify(cohort).preCommit();
1043 inOrder.verify(cohort).commit();
1045 // Use MBean for verification
1046 // Committed transaction count should increase as usual
1047 assertEquals(1, shard.underlyingActor().getShardMBean().getCommittedTransactionsCount());
1049 // Commit index should advance as we do not have an empty modification
1050 assertEquals(0, shard.underlyingActor().getShardMBean().getCommitIndex());
1052 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1059 public void testCommitPhaseFailure() throws Throwable {
1060 new ShardTestKit(getSystem()) {{
1061 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1062 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1063 "testCommitPhaseFailure");
1065 waitUntilLeader(shard);
1067 // Setup 2 simulated transactions with mock cohorts. The first one fails in the
1070 String transactionID1 = "tx1";
1071 MutableCompositeModification modification1 = new MutableCompositeModification();
1072 DOMStoreThreePhaseCommitCohort cohort1 = mock(DOMStoreThreePhaseCommitCohort.class, "cohort1");
1073 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort1).canCommit();
1074 doReturn(Futures.immediateFuture(null)).when(cohort1).preCommit();
1075 doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock"))).when(cohort1).commit();
1077 String transactionID2 = "tx2";
1078 MutableCompositeModification modification2 = new MutableCompositeModification();
1079 DOMStoreThreePhaseCommitCohort cohort2 = mock(DOMStoreThreePhaseCommitCohort.class, "cohort2");
1080 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort2).canCommit();
1082 FiniteDuration duration = duration("5 seconds");
1083 final Timeout timeout = new Timeout(duration);
1085 // Simulate the ForwardedReadyTransaction messages that would be sent
1086 // by the ShardTransaction.
1088 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1089 cohort1, modification1, true, false), getRef());
1090 expectMsgClass(duration, ReadyTransactionReply.class);
1092 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1093 cohort2, modification2, true, false), getRef());
1094 expectMsgClass(duration, ReadyTransactionReply.class);
1096 // Send the CanCommitTransaction message for the first Tx.
1098 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
1099 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1100 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
1101 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1103 // Send the CanCommitTransaction message for the 2nd Tx. This should get queued and
1104 // processed after the first Tx completes.
1106 Future<Object> canCommitFuture = Patterns.ask(shard,
1107 new CanCommitTransaction(transactionID2).toSerializable(), timeout);
1109 // Send the CommitTransaction message for the first Tx. This should send back an error
1110 // and trigger the 2nd Tx to proceed.
1112 shard.tell(new CommitTransaction(transactionID1).toSerializable(), getRef());
1113 expectMsgClass(duration, akka.actor.Status.Failure.class);
1115 // Wait for the 2nd Tx to complete the canCommit phase.
1117 final CountDownLatch latch = new CountDownLatch(1);
1118 canCommitFuture.onComplete(new OnComplete<Object>() {
1120 public void onComplete(final Throwable t, final Object resp) {
1123 }, getSystem().dispatcher());
1125 assertEquals("2nd CanCommit complete", true, latch.await(5, TimeUnit.SECONDS));
1127 InOrder inOrder = inOrder(cohort1, cohort2);
1128 inOrder.verify(cohort1).canCommit();
1129 inOrder.verify(cohort1).preCommit();
1130 inOrder.verify(cohort1).commit();
1131 inOrder.verify(cohort2).canCommit();
1133 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1138 public void testPreCommitPhaseFailure() throws Throwable {
1139 new ShardTestKit(getSystem()) {{
1140 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1141 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1142 "testPreCommitPhaseFailure");
1144 waitUntilLeader(shard);
1146 String transactionID1 = "tx1";
1147 MutableCompositeModification modification1 = new MutableCompositeModification();
1148 DOMStoreThreePhaseCommitCohort cohort1 = mock(DOMStoreThreePhaseCommitCohort.class, "cohort1");
1149 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort1).canCommit();
1150 doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock"))).when(cohort1).preCommit();
1152 String transactionID2 = "tx2";
1153 MutableCompositeModification modification2 = new MutableCompositeModification();
1154 DOMStoreThreePhaseCommitCohort cohort2 = mock(DOMStoreThreePhaseCommitCohort.class, "cohort2");
1155 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort2).canCommit();
1157 FiniteDuration duration = duration("5 seconds");
1158 final Timeout timeout = new Timeout(duration);
1160 // Simulate the ForwardedReadyTransaction messages that would be sent
1161 // by the ShardTransaction.
1163 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1164 cohort1, modification1, true, false), getRef());
1165 expectMsgClass(duration, ReadyTransactionReply.class);
1167 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1168 cohort2, modification2, true, false), getRef());
1169 expectMsgClass(duration, ReadyTransactionReply.class);
1171 // Send the CanCommitTransaction message for the first Tx.
1173 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
1174 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1175 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
1176 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1178 // Send the CanCommitTransaction message for the 2nd Tx. This should get queued and
1179 // processed after the first Tx completes.
1181 Future<Object> canCommitFuture = Patterns.ask(shard,
1182 new CanCommitTransaction(transactionID2).toSerializable(), timeout);
1184 // Send the CommitTransaction message for the first Tx. This should send back an error
1185 // and trigger the 2nd Tx to proceed.
1187 shard.tell(new CommitTransaction(transactionID1).toSerializable(), getRef());
1188 expectMsgClass(duration, akka.actor.Status.Failure.class);
1190 // Wait for the 2nd Tx to complete the canCommit phase.
1192 final CountDownLatch latch = new CountDownLatch(1);
1193 canCommitFuture.onComplete(new OnComplete<Object>() {
1195 public void onComplete(final Throwable t, final Object resp) {
1198 }, getSystem().dispatcher());
1200 assertEquals("2nd CanCommit complete", true, latch.await(5, TimeUnit.SECONDS));
1202 InOrder inOrder = inOrder(cohort1, cohort2);
1203 inOrder.verify(cohort1).canCommit();
1204 inOrder.verify(cohort1).preCommit();
1205 inOrder.verify(cohort2).canCommit();
1207 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1212 public void testCanCommitPhaseFailure() throws Throwable {
1213 new ShardTestKit(getSystem()) {{
1214 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1215 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1216 "testCanCommitPhaseFailure");
1218 waitUntilLeader(shard);
1220 final FiniteDuration duration = duration("5 seconds");
1222 String transactionID1 = "tx1";
1223 MutableCompositeModification modification = new MutableCompositeModification();
1224 DOMStoreThreePhaseCommitCohort cohort = mock(DOMStoreThreePhaseCommitCohort.class, "cohort1");
1225 doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock"))).when(cohort).canCommit();
1227 // Simulate the ForwardedReadyTransaction messages that would be sent
1228 // by the ShardTransaction.
1230 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1231 cohort, modification, true, false), getRef());
1232 expectMsgClass(duration, ReadyTransactionReply.class);
1234 // Send the CanCommitTransaction message.
1236 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
1237 expectMsgClass(duration, akka.actor.Status.Failure.class);
1239 // Send another can commit to ensure the failed one got cleaned up.
1243 String transactionID2 = "tx2";
1244 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1246 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1247 cohort, modification, true, false), getRef());
1248 expectMsgClass(duration, ReadyTransactionReply.class);
1250 shard.tell(new CanCommitTransaction(transactionID2).toSerializable(), getRef());
1251 CanCommitTransactionReply reply = CanCommitTransactionReply.fromSerializable(
1252 expectMsgClass(CanCommitTransactionReply.SERIALIZABLE_CLASS));
1253 assertEquals("getCanCommit", true, reply.getCanCommit());
1255 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1260 public void testCanCommitPhaseFalseResponse() throws Throwable {
1261 new ShardTestKit(getSystem()) {{
1262 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1263 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1264 "testCanCommitPhaseFalseResponse");
1266 waitUntilLeader(shard);
1268 final FiniteDuration duration = duration("5 seconds");
1270 String transactionID1 = "tx1";
1271 MutableCompositeModification modification = new MutableCompositeModification();
1272 DOMStoreThreePhaseCommitCohort cohort = mock(DOMStoreThreePhaseCommitCohort.class, "cohort1");
1273 doReturn(Futures.immediateFuture(Boolean.FALSE)).when(cohort).canCommit();
1275 // Simulate the ForwardedReadyTransaction messages that would be sent
1276 // by the ShardTransaction.
1278 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1279 cohort, modification, true, false), getRef());
1280 expectMsgClass(duration, ReadyTransactionReply.class);
1282 // Send the CanCommitTransaction message.
1284 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
1285 CanCommitTransactionReply reply = CanCommitTransactionReply.fromSerializable(
1286 expectMsgClass(CanCommitTransactionReply.SERIALIZABLE_CLASS));
1287 assertEquals("getCanCommit", false, reply.getCanCommit());
1289 // Send another can commit to ensure the failed one got cleaned up.
1293 String transactionID2 = "tx2";
1294 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1296 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1297 cohort, modification, true, false), getRef());
1298 expectMsgClass(duration, ReadyTransactionReply.class);
1300 shard.tell(new CanCommitTransaction(transactionID2).toSerializable(), getRef());
1301 reply = CanCommitTransactionReply.fromSerializable(
1302 expectMsgClass(CanCommitTransactionReply.SERIALIZABLE_CLASS));
1303 assertEquals("getCanCommit", true, reply.getCanCommit());
1305 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1310 public void testImmediateCommitWithCanCommitPhaseFailure() throws Throwable {
1311 new ShardTestKit(getSystem()) {{
1312 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1313 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1314 "testImmediateCommitWithCanCommitPhaseFailure");
1316 waitUntilLeader(shard);
1318 final FiniteDuration duration = duration("5 seconds");
1320 String transactionID1 = "tx1";
1321 MutableCompositeModification modification = new MutableCompositeModification();
1322 DOMStoreThreePhaseCommitCohort cohort = mock(DOMStoreThreePhaseCommitCohort.class, "cohort1");
1323 doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock"))).when(cohort).canCommit();
1325 // Simulate the ForwardedReadyTransaction messages that would be sent
1326 // by the ShardTransaction.
1328 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1329 cohort, modification, true, true), getRef());
1331 expectMsgClass(duration, akka.actor.Status.Failure.class);
1333 // Send another can commit to ensure the failed one got cleaned up.
1337 String transactionID2 = "tx2";
1338 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1339 doReturn(Futures.immediateFuture(null)).when(cohort).preCommit();
1340 doReturn(Futures.immediateFuture(null)).when(cohort).commit();
1342 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1343 cohort, modification, true, true), getRef());
1345 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
1347 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1352 public void testImmediateCommitWithCanCommitPhaseFalseResponse() throws Throwable {
1353 new ShardTestKit(getSystem()) {{
1354 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1355 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1356 "testImmediateCommitWithCanCommitPhaseFalseResponse");
1358 waitUntilLeader(shard);
1360 final FiniteDuration duration = duration("5 seconds");
1362 String transactionID = "tx1";
1363 MutableCompositeModification modification = new MutableCompositeModification();
1364 DOMStoreThreePhaseCommitCohort cohort = mock(DOMStoreThreePhaseCommitCohort.class, "cohort1");
1365 doReturn(Futures.immediateFuture(Boolean.FALSE)).when(cohort).canCommit();
1367 // Simulate the ForwardedReadyTransaction messages that would be sent
1368 // by the ShardTransaction.
1370 shard.tell(new ForwardedReadyTransaction(transactionID, CURRENT_VERSION,
1371 cohort, modification, true, true), getRef());
1373 expectMsgClass(duration, akka.actor.Status.Failure.class);
1375 // Send another can commit to ensure the failed one got cleaned up.
1379 String transactionID2 = "tx2";
1380 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1381 doReturn(Futures.immediateFuture(null)).when(cohort).preCommit();
1382 doReturn(Futures.immediateFuture(null)).when(cohort).commit();
1384 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1385 cohort, modification, true, true), getRef());
1387 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
1389 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1394 public void testAbortBeforeFinishCommit() throws Throwable {
1395 new ShardTestKit(getSystem()) {{
1396 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1397 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1398 "testAbortBeforeFinishCommit");
1400 waitUntilLeader(shard);
1402 final FiniteDuration duration = duration("5 seconds");
1403 InMemoryDOMDataStore dataStore = shard.underlyingActor().getDataStore();
1405 final String transactionID = "tx1";
1406 Function<DOMStoreThreePhaseCommitCohort,ListenableFuture<Void>> preCommit =
1407 new Function<DOMStoreThreePhaseCommitCohort,ListenableFuture<Void>>() {
1409 public ListenableFuture<Void> apply(final DOMStoreThreePhaseCommitCohort cohort) {
1410 ListenableFuture<Void> preCommitFuture = cohort.preCommit();
1412 // Simulate an AbortTransaction message occurring during replication, after
1413 // persisting and before finishing the commit to the in-memory store.
1414 // We have no followers so due to optimizations in the RaftActor, it does not
1415 // attempt replication and thus we can't send an AbortTransaction message b/c
1416 // it would be processed too late after CommitTransaction completes. So we'll
1417 // simulate an AbortTransaction message occurring during replication by calling
1418 // the shard directly.
1420 shard.underlyingActor().doAbortTransaction(transactionID, null);
1422 return preCommitFuture;
1426 MutableCompositeModification modification = new MutableCompositeModification();
1427 DOMStoreThreePhaseCommitCohort cohort = setupMockWriteTransaction("cohort1", dataStore,
1428 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME),
1429 modification, preCommit);
1431 shard.tell(new ForwardedReadyTransaction(transactionID, CURRENT_VERSION,
1432 cohort, modification, true, false), getRef());
1433 expectMsgClass(duration, ReadyTransactionReply.class);
1435 shard.tell(new CanCommitTransaction(transactionID).toSerializable(), getRef());
1436 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1437 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
1438 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1440 shard.tell(new CommitTransaction(transactionID).toSerializable(), getRef());
1441 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
1443 NormalizedNode<?, ?> node = readStore(shard, TestModel.TEST_PATH);
1445 // Since we're simulating an abort occurring during replication and before finish commit,
1446 // the data should still get written to the in-memory store since we've gotten past
1447 // canCommit and preCommit and persisted the data.
1448 assertNotNull(TestModel.TEST_QNAME.getLocalName() + " not found", node);
1450 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1455 public void testTransactionCommitTimeout() throws Throwable {
1456 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1458 new ShardTestKit(getSystem()) {{
1459 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1460 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1461 "testTransactionCommitTimeout");
1463 waitUntilLeader(shard);
1465 final FiniteDuration duration = duration("5 seconds");
1467 InMemoryDOMDataStore dataStore = shard.underlyingActor().getDataStore();
1469 writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1470 writeToStore(shard, TestModel.OUTER_LIST_PATH,
1471 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1473 // Create 1st Tx - will timeout
1475 String transactionID1 = "tx1";
1476 MutableCompositeModification modification1 = new MutableCompositeModification();
1477 DOMStoreThreePhaseCommitCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
1478 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1479 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
1480 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1),
1485 String transactionID2 = "tx3";
1486 MutableCompositeModification modification2 = new MutableCompositeModification();
1487 YangInstanceIdentifier listNodePath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1488 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build();
1489 DOMStoreThreePhaseCommitCohort cohort2 = setupMockWriteTransaction("cohort3", dataStore,
1491 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2),
1496 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1497 cohort1, modification1, true, false), getRef());
1498 expectMsgClass(duration, ReadyTransactionReply.class);
1500 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1501 cohort2, modification2, true, false), getRef());
1502 expectMsgClass(duration, ReadyTransactionReply.class);
1504 // canCommit 1st Tx. We don't send the commit so it should timeout.
1506 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
1507 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS);
1509 // canCommit the 2nd Tx - it should complete after the 1st Tx times out.
1511 shard.tell(new CanCommitTransaction(transactionID2).toSerializable(), getRef());
1512 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS);
1514 // Try to commit the 1st Tx - should fail as it's not the current Tx.
1516 shard.tell(new CommitTransaction(transactionID1).toSerializable(), getRef());
1517 expectMsgClass(duration, akka.actor.Status.Failure.class);
1519 // Commit the 2nd Tx.
1521 shard.tell(new CommitTransaction(transactionID2).toSerializable(), getRef());
1522 expectMsgClass(duration, CommitTransactionReply.SERIALIZABLE_CLASS);
1524 NormalizedNode<?, ?> node = readStore(shard, listNodePath);
1525 assertNotNull(listNodePath + " not found", node);
1527 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1532 public void testTransactionCommitQueueCapacityExceeded() throws Throwable {
1533 dataStoreContextBuilder.shardTransactionCommitQueueCapacity(1);
1535 new ShardTestKit(getSystem()) {{
1536 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1537 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1538 "testTransactionCommitQueueCapacityExceeded");
1540 waitUntilLeader(shard);
1542 final FiniteDuration duration = duration("5 seconds");
1544 InMemoryDOMDataStore dataStore = shard.underlyingActor().getDataStore();
1546 String transactionID1 = "tx1";
1547 MutableCompositeModification modification1 = new MutableCompositeModification();
1548 DOMStoreThreePhaseCommitCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
1549 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification1);
1551 String transactionID2 = "tx2";
1552 MutableCompositeModification modification2 = new MutableCompositeModification();
1553 DOMStoreThreePhaseCommitCohort cohort2 = setupMockWriteTransaction("cohort2", dataStore,
1554 TestModel.OUTER_LIST_PATH,
1555 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(),
1558 String transactionID3 = "tx3";
1559 MutableCompositeModification modification3 = new MutableCompositeModification();
1560 DOMStoreThreePhaseCommitCohort cohort3 = setupMockWriteTransaction("cohort3", dataStore,
1561 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification3);
1565 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1566 cohort1, modification1, true, false), getRef());
1567 expectMsgClass(duration, ReadyTransactionReply.class);
1569 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1570 cohort2, modification2, true, false), getRef());
1571 expectMsgClass(duration, ReadyTransactionReply.class);
1573 shard.tell(new ForwardedReadyTransaction(transactionID3, CURRENT_VERSION,
1574 cohort3, modification3, true, false), getRef());
1575 expectMsgClass(duration, ReadyTransactionReply.class);
1577 // canCommit 1st Tx.
1579 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
1580 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS);
1582 // canCommit the 2nd Tx - it should get queued.
1584 shard.tell(new CanCommitTransaction(transactionID2).toSerializable(), getRef());
1586 // canCommit the 3rd Tx - should exceed queue capacity and fail.
1588 shard.tell(new CanCommitTransaction(transactionID3).toSerializable(), getRef());
1589 expectMsgClass(duration, akka.actor.Status.Failure.class);
1591 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1596 public void testCanCommitBeforeReadyFailure() throws Throwable {
1597 new ShardTestKit(getSystem()) {{
1598 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1599 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1600 "testCanCommitBeforeReadyFailure");
1602 shard.tell(new CanCommitTransaction("tx").toSerializable(), getRef());
1603 expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
1605 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1610 public void testAbortTransaction() throws Throwable {
1611 new ShardTestKit(getSystem()) {{
1612 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1613 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1614 "testAbortTransaction");
1616 waitUntilLeader(shard);
1618 // Setup 2 simulated transactions with mock cohorts. The first one will be aborted.
1620 String transactionID1 = "tx1";
1621 MutableCompositeModification modification1 = new MutableCompositeModification();
1622 DOMStoreThreePhaseCommitCohort cohort1 = mock(DOMStoreThreePhaseCommitCohort.class, "cohort1");
1623 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort1).canCommit();
1624 doReturn(Futures.immediateFuture(null)).when(cohort1).abort();
1626 String transactionID2 = "tx2";
1627 MutableCompositeModification modification2 = new MutableCompositeModification();
1628 DOMStoreThreePhaseCommitCohort cohort2 = mock(DOMStoreThreePhaseCommitCohort.class, "cohort2");
1629 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort2).canCommit();
1631 FiniteDuration duration = duration("5 seconds");
1632 final Timeout timeout = new Timeout(duration);
1634 // Simulate the ForwardedReadyTransaction messages that would be sent
1635 // by the ShardTransaction.
1637 shard.tell(new ForwardedReadyTransaction(transactionID1, CURRENT_VERSION,
1638 cohort1, modification1, true, false), getRef());
1639 expectMsgClass(duration, ReadyTransactionReply.class);
1641 shard.tell(new ForwardedReadyTransaction(transactionID2, CURRENT_VERSION,
1642 cohort2, modification2, true, false), getRef());
1643 expectMsgClass(duration, ReadyTransactionReply.class);
1645 // Send the CanCommitTransaction message for the first Tx.
1647 shard.tell(new CanCommitTransaction(transactionID1).toSerializable(), getRef());
1648 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1649 expectMsgClass(duration, CanCommitTransactionReply.SERIALIZABLE_CLASS));
1650 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1652 // Send the CanCommitTransaction message for the 2nd Tx. This should get queued and
1653 // processed after the first Tx completes.
1655 Future<Object> canCommitFuture = Patterns.ask(shard,
1656 new CanCommitTransaction(transactionID2).toSerializable(), timeout);
1658 // Send the AbortTransaction message for the first Tx. This should trigger the 2nd
1661 shard.tell(new AbortTransaction(transactionID1).toSerializable(), getRef());
1662 expectMsgClass(duration, AbortTransactionReply.SERIALIZABLE_CLASS);
1664 // Wait for the 2nd Tx to complete the canCommit phase.
1666 Await.ready(canCommitFuture, duration);
1668 InOrder inOrder = inOrder(cohort1, cohort2);
1669 inOrder.verify(cohort1).canCommit();
1670 inOrder.verify(cohort2).canCommit();
1672 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1677 public void testCreateSnapshot() throws Exception {
1678 testCreateSnapshot(true, "testCreateSnapshot");
1682 public void testCreateSnapshotWithNonPersistentData() throws Exception {
1683 testCreateSnapshot(false, "testCreateSnapshotWithNonPersistentData");
1686 @SuppressWarnings("serial")
1687 public void testCreateSnapshot(final boolean persistent, final String shardActorName) throws Exception{
1689 final AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(1));
1691 final AtomicReference<Object> savedSnapshot = new AtomicReference<>();
1692 class TestPersistentDataProvider extends DelegatingPersistentDataProvider {
1693 TestPersistentDataProvider(DataPersistenceProvider delegate) {
1698 public void saveSnapshot(Object o) {
1699 savedSnapshot.set(o);
1700 super.saveSnapshot(o);
1704 dataStoreContextBuilder.persistent(persistent);
1706 new ShardTestKit(getSystem()) {{
1707 class TestShard extends Shard {
1709 protected TestShard(ShardIdentifier name, Map<String, String> peerAddresses,
1710 DatastoreContext datastoreContext, SchemaContext schemaContext) {
1711 super(name, peerAddresses, datastoreContext, schemaContext);
1712 setPersistence(new TestPersistentDataProvider(super.persistence()));
1716 public void handleCommand(Object message) {
1717 super.handleCommand(message);
1719 if (message instanceof SaveSnapshotSuccess || message.equals("commit_snapshot")) {
1720 latch.get().countDown();
1725 public RaftActorContext getRaftActorContext() {
1726 return super.getRaftActorContext();
1730 Creator<Shard> creator = new Creator<Shard>() {
1732 public Shard create() throws Exception {
1733 return new TestShard(shardID, Collections.<String,String>emptyMap(),
1734 newDatastoreContext(), SCHEMA_CONTEXT);
1738 TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1739 Props.create(new DelegatingShardCreator(creator)), shardActorName);
1741 waitUntilLeader(shard);
1743 writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1745 NormalizedNode<?,?> expectedRoot = readStore(shard, YangInstanceIdentifier.builder().build());
1747 // Trigger creation of a snapshot by ensuring
1748 RaftActorContext raftActorContext = ((TestShard) shard.underlyingActor()).getRaftActorContext();
1749 raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
1751 assertEquals("Snapshot saved", true, latch.get().await(5, TimeUnit.SECONDS));
1753 assertTrue("Invalid saved snapshot " + savedSnapshot.get(),
1754 savedSnapshot.get() instanceof Snapshot);
1756 verifySnapshot((Snapshot)savedSnapshot.get(), expectedRoot);
1758 latch.set(new CountDownLatch(1));
1759 savedSnapshot.set(null);
1761 raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
1763 assertEquals("Snapshot saved", true, latch.get().await(5, TimeUnit.SECONDS));
1765 assertTrue("Invalid saved snapshot " + savedSnapshot.get(),
1766 savedSnapshot.get() instanceof Snapshot);
1768 verifySnapshot((Snapshot)savedSnapshot.get(), expectedRoot);
1770 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1773 private void verifySnapshot(Snapshot snapshot, NormalizedNode<?,?> expectedRoot) {
1775 NormalizedNode<?, ?> actual = SerializationUtils.deserializeNormalizedNode(snapshot.getState());
1776 assertEquals("Root node", expectedRoot, actual);
1782 * This test simply verifies that the applySnapShot logic will work
1783 * @throws ReadFailedException
1786 public void testInMemoryDataStoreRestore() throws ReadFailedException {
1787 InMemoryDOMDataStore store = new InMemoryDOMDataStore("test", MoreExecutors.sameThreadExecutor());
1789 store.onGlobalContextUpdated(SCHEMA_CONTEXT);
1791 DOMStoreWriteTransaction putTransaction = store.newWriteOnlyTransaction();
1792 putTransaction.write(TestModel.TEST_PATH,
1793 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1794 commitTransaction(putTransaction);
1797 NormalizedNode<?, ?> expected = readStore(store);
1799 DOMStoreWriteTransaction writeTransaction = store.newWriteOnlyTransaction();
1801 writeTransaction.delete(YangInstanceIdentifier.builder().build());
1802 writeTransaction.write(YangInstanceIdentifier.builder().build(), expected);
1804 commitTransaction(writeTransaction);
1806 NormalizedNode<?, ?> actual = readStore(store);
1808 assertEquals(expected, actual);
1812 public void testRecoveryApplicable(){
1814 final DatastoreContext persistentContext = DatastoreContext.newBuilder().
1815 shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(true).build();
1817 final Props persistentProps = Shard.props(shardID, Collections.<String, String>emptyMap(),
1818 persistentContext, SCHEMA_CONTEXT);
1820 final DatastoreContext nonPersistentContext = DatastoreContext.newBuilder().
1821 shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(false).build();
1823 final Props nonPersistentProps = Shard.props(shardID, Collections.<String, String>emptyMap(),
1824 nonPersistentContext, SCHEMA_CONTEXT);
1826 new ShardTestKit(getSystem()) {{
1827 TestActorRef<Shard> shard1 = TestActorRef.create(getSystem(),
1828 persistentProps, "testPersistence1");
1830 assertTrue("Recovery Applicable", shard1.underlyingActor().persistence().isRecoveryApplicable());
1832 shard1.tell(PoisonPill.getInstance(), ActorRef.noSender());
1834 TestActorRef<Shard> shard2 = TestActorRef.create(getSystem(),
1835 nonPersistentProps, "testPersistence2");
1837 assertFalse("Recovery Not Applicable", shard2.underlyingActor().persistence().isRecoveryApplicable());
1839 shard2.tell(PoisonPill.getInstance(), ActorRef.noSender());
1846 public void testOnDatastoreContext() {
1847 new ShardTestKit(getSystem()) {{
1848 dataStoreContextBuilder.persistent(true);
1850 TestActorRef<Shard> shard = TestActorRef.create(getSystem(), newShardProps(), "testOnDatastoreContext");
1852 assertEquals("isRecoveryApplicable", true,
1853 shard.underlyingActor().persistence().isRecoveryApplicable());
1855 waitUntilLeader(shard);
1857 shard.tell(dataStoreContextBuilder.persistent(false).build(), ActorRef.noSender());
1859 assertEquals("isRecoveryApplicable", false,
1860 shard.underlyingActor().persistence().isRecoveryApplicable());
1862 shard.tell(dataStoreContextBuilder.persistent(true).build(), ActorRef.noSender());
1864 assertEquals("isRecoveryApplicable", true,
1865 shard.underlyingActor().persistence().isRecoveryApplicable());
1867 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1872 public void testRegisterRoleChangeListener() throws Exception {
1873 new ShardTestKit(getSystem()) {
1875 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1876 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1877 "testRegisterRoleChangeListener");
1879 waitUntilLeader(shard);
1881 TestActorRef<MessageCollectorActor> listener =
1882 TestActorRef.create(getSystem(), Props.create(MessageCollectorActor.class));
1884 shard.tell(new RegisterRoleChangeListener(), listener);
1886 // TODO: MessageCollectorActor exists as a test util in both the akka-raft and distributed-datastore
1887 // projects. Need to move it to commons as a regular utility and then we can get rid of this arbitrary
1889 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
1891 List<Object> allMatching = MessageCollectorActor.getAllMatching(listener, RegisterRoleChangeListenerReply.class);
1893 assertEquals(1, allMatching.size());
1899 public void testFollowerInitialSyncStatus() throws Exception {
1900 final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
1901 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1902 "testFollowerInitialSyncStatus");
1904 shard.underlyingActor().onReceiveCommand(new FollowerInitialSyncUpStatus(false, "member-1-shard-inventory-operational"));
1906 assertEquals(false, shard.underlyingActor().getShardMBean().getFollowerInitialSyncStatus());
1908 shard.underlyingActor().onReceiveCommand(new FollowerInitialSyncUpStatus(true, "member-1-shard-inventory-operational"));
1910 assertEquals(true, shard.underlyingActor().getShardMBean().getFollowerInitialSyncStatus());
1912 shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
1915 private void commitTransaction(final DOMStoreWriteTransaction transaction) {
1916 DOMStoreThreePhaseCommitCohort commitCohort = transaction.ready();
1917 ListenableFuture<Void> future =
1918 commitCohort.preCommit();
1921 future = commitCohort.commit();
1923 } catch (InterruptedException | ExecutionException e) {