2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertSame;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.inOrder;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.reset;
21 import static org.mockito.Mockito.verify;
22 import static org.opendaylight.controller.cluster.datastore.DataStoreVersions.CURRENT_VERSION;
23 import akka.actor.ActorRef;
24 import akka.actor.ActorSelection;
25 import akka.actor.Props;
26 import akka.actor.Status.Failure;
27 import akka.dispatch.Dispatchers;
28 import akka.dispatch.OnComplete;
29 import akka.japi.Creator;
30 import akka.pattern.Patterns;
31 import akka.persistence.SaveSnapshotSuccess;
32 import akka.testkit.TestActorRef;
33 import akka.util.Timeout;
34 import com.google.common.base.Function;
35 import com.google.common.base.Stopwatch;
36 import com.google.common.util.concurrent.Futures;
37 import com.google.common.util.concurrent.ListenableFuture;
38 import com.google.common.util.concurrent.Uninterruptibles;
39 import java.util.Collections;
40 import java.util.HashSet;
42 import java.util.concurrent.CountDownLatch;
43 import java.util.concurrent.TimeUnit;
44 import java.util.concurrent.atomic.AtomicBoolean;
45 import java.util.concurrent.atomic.AtomicReference;
46 import org.junit.Test;
47 import org.mockito.InOrder;
48 import org.opendaylight.controller.cluster.DataPersistenceProvider;
49 import org.opendaylight.controller.cluster.DelegatingPersistentDataProvider;
50 import org.opendaylight.controller.cluster.access.concepts.MemberName;
51 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
52 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
53 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
54 import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction;
55 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
56 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
57 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
58 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
59 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
60 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
61 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
62 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
63 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
64 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
65 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
66 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
67 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
68 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
69 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
70 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
71 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
72 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
73 import org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged;
74 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
75 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
76 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
77 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
78 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
79 import org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener;
80 import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
81 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
82 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
83 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply;
84 import org.opendaylight.controller.cluster.raft.RaftActorContext;
85 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
86 import org.opendaylight.controller.cluster.raft.ReplicatedLogImplEntry;
87 import org.opendaylight.controller.cluster.raft.Snapshot;
88 import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries;
89 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
90 import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
91 import org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus;
92 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
93 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
94 import org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState;
95 import org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState;
96 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
97 import org.opendaylight.controller.cluster.raft.messages.ServerRemoved;
98 import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy;
99 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
100 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
101 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
102 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
103 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
104 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
105 import org.opendaylight.yangtools.util.StringIdentifier;
106 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
107 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
108 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
109 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
110 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
111 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
112 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
113 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
114 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
115 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
116 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
117 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
118 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
119 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
120 import scala.concurrent.Await;
121 import scala.concurrent.Future;
122 import scala.concurrent.duration.FiniteDuration;
124 public class ShardTest extends AbstractShardTest {
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";
128 public void testRegisterChangeListener() throws Exception {
129 new ShardTestKit(getSystem()) {{
130 final TestActorRef<Shard> shard = actorFactory.createTestActor(
131 newShardProps(), "testRegisterChangeListener");
133 waitUntilLeader(shard);
135 shard.tell(new UpdateSchemaContext(SchemaContextHelper.full()), ActorRef.noSender());
137 final MockDataChangeListener listener = new MockDataChangeListener(1);
138 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
139 "testRegisterChangeListener-DataChangeListener");
141 shard.tell(new RegisterChangeListener(TestModel.TEST_PATH,
142 dclActor, AsyncDataBroker.DataChangeScope.BASE, true), getRef());
144 final RegisterChangeListenerReply reply = expectMsgClass(duration("3 seconds"),
145 RegisterChangeListenerReply.class);
146 final String replyPath = reply.getListenerRegistrationPath().toString();
147 assertTrue("Incorrect reply path: " + replyPath, replyPath.matches(
148 "akka:\\/\\/test\\/user\\/testRegisterChangeListener\\/\\$.*"));
150 final YangInstanceIdentifier path = TestModel.TEST_PATH;
151 writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
153 listener.waitForChangeEvents(path);
157 @SuppressWarnings("serial")
159 public void testChangeListenerNotifiedWhenNotTheLeaderOnRegistration() throws Exception {
160 // This test tests the timing window in which a change listener is registered before the
161 // shard becomes the leader. We verify that the listener is registered and notified of the
162 // existing data when the shard becomes the leader.
163 new ShardTestKit(getSystem()) {{
164 // For this test, we want to send the RegisterChangeListener message after the shard
165 // has recovered from persistence and before it becomes the leader. So we subclass
166 // Shard to override onReceiveCommand and, when the first ElectionTimeout is received,
167 // we know that the shard has been initialized to a follower and has started the
168 // election process. The following 2 CountDownLatches are used to coordinate the
169 // ElectionTimeout with the sending of the RegisterChangeListener message.
170 final CountDownLatch onFirstElectionTimeout = new CountDownLatch(1);
171 final CountDownLatch onChangeListenerRegistered = new CountDownLatch(1);
172 final Creator<Shard> creator = new Creator<Shard>() {
173 boolean firstElectionTimeout = true;
176 public Shard create() throws Exception {
177 // Use a non persistent provider because this test actually invokes persist on the journal
178 // this will cause all other messages to not be queued properly after that.
179 // The basic issue is that you cannot use TestActorRef with a persistent actor (at least when
180 // it does do a persist)
181 return new Shard(newShardBuilder()) {
183 public void handleCommand(final Object message) {
184 if(message instanceof ElectionTimeout && firstElectionTimeout) {
185 // Got the first ElectionTimeout. We don't forward it to the
186 // base Shard yet until we've sent the RegisterChangeListener
187 // message. So we signal the onFirstElectionTimeout latch to tell
188 // the main thread to send the RegisterChangeListener message and
189 // start a thread to wait on the onChangeListenerRegistered latch,
190 // which the main thread signals after it has sent the message.
191 // After the onChangeListenerRegistered is triggered, we send the
192 // original ElectionTimeout message to proceed with the election.
193 firstElectionTimeout = false;
194 final ActorRef self = getSelf();
198 Uninterruptibles.awaitUninterruptibly(
199 onChangeListenerRegistered, 5, TimeUnit.SECONDS);
200 self.tell(message, self);
204 onFirstElectionTimeout.countDown();
206 super.handleCommand(message);
213 setupInMemorySnapshotStore();
215 final MockDataChangeListener listener = new MockDataChangeListener(1);
216 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
217 "testRegisterChangeListenerWhenNotLeaderInitially-DataChangeListener");
219 final TestActorRef<Shard> shard = actorFactory.createTestActor(
220 Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
221 "testRegisterChangeListenerWhenNotLeaderInitially");
223 final YangInstanceIdentifier path = TestModel.TEST_PATH;
225 // Wait until the shard receives the first ElectionTimeout message.
226 assertEquals("Got first ElectionTimeout", true,
227 onFirstElectionTimeout.await(5, TimeUnit.SECONDS));
229 // Now send the RegisterChangeListener and wait for the reply.
230 shard.tell(new RegisterChangeListener(path, dclActor,
231 AsyncDataBroker.DataChangeScope.SUBTREE, false), getRef());
233 final RegisterChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
234 RegisterChangeListenerReply.class);
235 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
237 // Sanity check - verify the shard is not the leader yet.
238 shard.tell(FindLeader.INSTANCE, getRef());
239 final FindLeaderReply findLeadeReply =
240 expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
241 assertFalse("Expected the shard not to be the leader", findLeadeReply.getLeaderActor().isPresent());
243 // Signal the onChangeListenerRegistered latch to tell the thread above to proceed
244 // with the election process.
245 onChangeListenerRegistered.countDown();
247 // Wait for the shard to become the leader and notify our listener with the existing
248 // data in the store.
249 listener.waitForChangeEvents(path);
254 public void testRegisterDataTreeChangeListener() throws Exception {
255 new ShardTestKit(getSystem()) {{
256 final TestActorRef<Shard> shard = actorFactory.createTestActor(
257 newShardProps(), "testRegisterDataTreeChangeListener");
259 waitUntilLeader(shard);
261 shard.tell(new UpdateSchemaContext(SchemaContextHelper.full()), ActorRef.noSender());
263 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
264 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener),
265 "testRegisterDataTreeChangeListener-DataTreeChangeListener");
267 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, false), getRef());
269 final RegisterDataTreeChangeListenerReply reply = expectMsgClass(duration("3 seconds"),
270 RegisterDataTreeChangeListenerReply.class);
271 final String replyPath = reply.getListenerRegistrationPath().toString();
272 assertTrue("Incorrect reply path: " + replyPath, replyPath.matches(
273 "akka:\\/\\/test\\/user\\/testRegisterDataTreeChangeListener\\/\\$.*"));
275 final YangInstanceIdentifier path = TestModel.TEST_PATH;
276 writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
278 listener.waitForChangeEvents();
282 @SuppressWarnings("serial")
284 public void testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration() throws Exception {
285 new ShardTestKit(getSystem()) {{
286 final CountDownLatch onFirstElectionTimeout = new CountDownLatch(1);
287 final CountDownLatch onChangeListenerRegistered = new CountDownLatch(1);
288 final Creator<Shard> creator = new Creator<Shard>() {
289 boolean firstElectionTimeout = true;
292 public Shard create() throws Exception {
293 return new Shard(newShardBuilder()) {
295 public void handleCommand(final Object message) {
296 if(message instanceof ElectionTimeout && firstElectionTimeout) {
297 firstElectionTimeout = false;
298 final ActorRef self = getSelf();
302 Uninterruptibles.awaitUninterruptibly(
303 onChangeListenerRegistered, 5, TimeUnit.SECONDS);
304 self.tell(message, self);
308 onFirstElectionTimeout.countDown();
310 super.handleCommand(message);
317 setupInMemorySnapshotStore();
319 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
320 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener),
321 "testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration-DataChangeListener");
323 final TestActorRef<Shard> shard = actorFactory.createTestActor(
324 Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
325 "testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration");
327 final YangInstanceIdentifier path = TestModel.TEST_PATH;
329 assertEquals("Got first ElectionTimeout", true,
330 onFirstElectionTimeout.await(5, TimeUnit.SECONDS));
332 shard.tell(new RegisterDataTreeChangeListener(path, dclActor, false), getRef());
333 final RegisterDataTreeChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
334 RegisterDataTreeChangeListenerReply.class);
335 assertNotNull("getListenerRegistratioznPath", reply.getListenerRegistrationPath());
337 shard.tell(FindLeader.INSTANCE, getRef());
338 final FindLeaderReply findLeadeReply =
339 expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
340 assertFalse("Expected the shard not to be the leader", findLeadeReply.getLeaderActor().isPresent());
343 onChangeListenerRegistered.countDown();
345 // TODO: investigate why we do not receive data chage events
346 listener.waitForChangeEvents();
351 public void testCreateTransaction(){
352 new ShardTestKit(getSystem()) {{
353 final ActorRef shard = actorFactory.createActor(newShardProps(), "testCreateTransaction");
355 waitUntilLeader(shard);
357 shard.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
359 shard.tell(new CreateTransaction("txn-1", TransactionType.READ_ONLY.ordinal(), null,
360 DataStoreVersions.CURRENT_VERSION).toSerializable(), getRef());
362 final CreateTransactionReply reply = expectMsgClass(duration("3 seconds"),
363 CreateTransactionReply.class);
365 final String path = reply.getTransactionPath().toString();
366 assertTrue("Unexpected transaction path " + path,
367 path.contains("akka://test/user/testCreateTransaction/shard-txn-1"));
372 public void testCreateTransactionOnChain(){
373 new ShardTestKit(getSystem()) {{
374 final ActorRef shard = actorFactory.createActor(newShardProps(), "testCreateTransactionOnChain");
376 waitUntilLeader(shard);
378 shard.tell(new CreateTransaction("txn-1",TransactionType.READ_ONLY.ordinal(), "foobar",
379 DataStoreVersions.CURRENT_VERSION).toSerializable(), getRef());
381 final CreateTransactionReply reply = expectMsgClass(duration("3 seconds"),
382 CreateTransactionReply.class);
384 final String path = reply.getTransactionPath().toString();
385 assertTrue("Unexpected transaction path " + path,
386 path.contains("akka://test/user/testCreateTransactionOnChain/shard-txn-1"));
391 public void testPeerAddressResolved() throws Exception {
392 new ShardTestKit(getSystem()) {{
393 ShardIdentifier peerID = ShardIdentifier.builder().memberName(MemberName.forName("member-2"))
394 .shardName("inventory").type("config").build();
395 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardBuilder().
396 peerAddresses(Collections.<String, String>singletonMap(peerID.toString(), null)).props().
397 withDispatcher(Dispatchers.DefaultDispatcherId()), "testPeerAddressResolved");
399 final String address = "akka://foobar";
400 shard.tell(new PeerAddressResolved(peerID.toString(), address), ActorRef.noSender());
402 shard.tell(GetOnDemandRaftState.INSTANCE, getRef());
403 OnDemandRaftState state = expectMsgClass(OnDemandRaftState.class);
404 assertEquals("getPeerAddress", address, state.getPeerAddresses().get(peerID.toString()));
409 public void testApplySnapshot() throws Exception {
411 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps(), "testApplySnapshot");
413 ShardTestKit.waitUntilLeader(shard);
415 final DataTree store = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
416 store.setSchemaContext(SCHEMA_CONTEXT);
418 final ContainerNode container = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
419 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
420 withChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).addChild(
421 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1)).build()).build();
423 writeToStore(store, TestModel.TEST_PATH, container);
425 final YangInstanceIdentifier root = YangInstanceIdentifier.builder().build();
426 final NormalizedNode<?,?> expected = readStore(store, root);
428 final Snapshot snapshot = Snapshot.create(SerializationUtils.serializeNormalizedNode(expected),
429 Collections.<ReplicatedLogEntry>emptyList(), 1, 2, 3, 4);
431 shard.underlyingActor().getRaftActorSnapshotCohort().applySnapshot(snapshot.getState());
433 final NormalizedNode<?,?> actual = readStore(shard, root);
435 assertEquals("Root node", expected, actual);
439 public void testApplyState() throws Exception {
440 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps(), "testApplyState");
442 ShardTestKit.waitUntilLeader(shard);
444 final DataTree source = setupInMemorySnapshotStore();
445 final DataTreeModification writeMod = source.takeSnapshot().newModification();
446 ContainerNode node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
447 writeMod.write(TestModel.TEST_PATH, node);
450 final ApplyState applyState = new ApplyState(null, new StringIdentifier("test"),
451 new ReplicatedLogImplEntry(1, 2, payloadForModification(source, writeMod)));
453 shard.tell(applyState, shard);
455 Stopwatch sw = Stopwatch.createStarted();
456 while(sw.elapsed(TimeUnit.SECONDS) <= 5) {
457 Uninterruptibles.sleepUninterruptibly(75, TimeUnit.MILLISECONDS);
459 final NormalizedNode<?,?> actual = readStore(shard, TestModel.TEST_PATH);
461 assertEquals("Applied state", node, actual);
466 fail("State was not applied");
470 public void testDataTreeCandidateRecovery() throws Exception {
471 // Set up the InMemorySnapshotStore.
472 final DataTree source = setupInMemorySnapshotStore();
474 final DataTreeModification writeMod = source.takeSnapshot().newModification();
475 writeMod.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
477 InMemoryJournal.addEntry(shardID.toString(), 0, DUMMY_DATA);
479 // Set up the InMemoryJournal.
480 InMemoryJournal.addEntry(shardID.toString(), 1, new ReplicatedLogImplEntry(0, 1, payloadForModification(source, writeMod)));
482 final int nListEntries = 16;
483 final Set<Integer> listEntryKeys = new HashSet<>();
485 // Add some ModificationPayload entries
486 for (int i = 1; i <= nListEntries; i++) {
487 listEntryKeys.add(Integer.valueOf(i));
489 final YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
490 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i).build();
492 final DataTreeModification mod = source.takeSnapshot().newModification();
493 mod.merge(path, ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i));
495 InMemoryJournal.addEntry(shardID.toString(), i+1, new ReplicatedLogImplEntry(i, 1,
496 payloadForModification(source, mod)));
499 InMemoryJournal.addEntry(shardID.toString(), nListEntries + 2,
500 new ApplyJournalEntries(nListEntries));
502 testRecovery(listEntryKeys);
506 public void testConcurrentThreePhaseCommits() throws Throwable {
507 new ShardTestKit(getSystem()) {{
508 final TestActorRef<Shard> shard = actorFactory.createTestActor(
509 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
510 "testConcurrentThreePhaseCommits");
512 waitUntilLeader(shard);
514 // Setup 3 simulated transactions with mock cohorts backed by real cohorts.
516 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
518 final String transactionID1 = "tx1";
519 final MutableCompositeModification modification1 = new MutableCompositeModification();
520 final ShardDataTreeCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
521 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification1);
523 final String transactionID2 = "tx2";
524 final MutableCompositeModification modification2 = new MutableCompositeModification();
525 final ShardDataTreeCohort cohort2 = setupMockWriteTransaction("cohort2", dataStore,
526 TestModel.OUTER_LIST_PATH,
527 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(),
530 final String transactionID3 = "tx3";
531 final MutableCompositeModification modification3 = new MutableCompositeModification();
532 final ShardDataTreeCohort cohort3 = setupMockWriteTransaction("cohort3", dataStore,
533 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
534 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
535 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1),
538 final long timeoutSec = 5;
539 final FiniteDuration duration = FiniteDuration.create(timeoutSec, TimeUnit.SECONDS);
540 final Timeout timeout = new Timeout(duration);
542 shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort1, transactionID1, modification1), getRef());
543 final ReadyTransactionReply readyReply = ReadyTransactionReply.fromSerializable(
544 expectMsgClass(duration, ReadyTransactionReply.class));
545 assertEquals("Cohort path", shard.path().toString(), readyReply.getCohortPath());
547 // Send the CanCommitTransaction message for the first Tx.
549 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
550 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
551 expectMsgClass(duration, CanCommitTransactionReply.class));
552 assertEquals("Can commit", true, canCommitReply.getCanCommit());
554 shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort2, transactionID2, modification2), getRef());
555 expectMsgClass(duration, ReadyTransactionReply.class);
557 shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort3, transactionID3, modification3), getRef());
558 expectMsgClass(duration, ReadyTransactionReply.class);
560 // Send the CanCommitTransaction message for the next 2 Tx's. These should get queued and
561 // processed after the first Tx completes.
563 final Future<Object> canCommitFuture1 = Patterns.ask(shard,
564 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
566 final Future<Object> canCommitFuture2 = Patterns.ask(shard,
567 new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), timeout);
569 // Send the CommitTransaction message for the first Tx. After it completes, it should
570 // trigger the 2nd Tx to proceed which should in turn then trigger the 3rd.
572 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
573 expectMsgClass(duration, CommitTransactionReply.class);
575 // Wait for the next 2 Tx's to complete.
577 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
578 final CountDownLatch commitLatch = new CountDownLatch(2);
580 class OnFutureComplete extends OnComplete<Object> {
581 private final Class<?> expRespType;
583 OnFutureComplete(final Class<?> expRespType) {
584 this.expRespType = expRespType;
588 public void onComplete(final Throwable error, final Object resp) {
590 caughtEx.set(new AssertionError(getClass().getSimpleName() + " failure", error));
593 assertEquals("Commit response type", expRespType, resp.getClass());
595 } catch (final Exception e) {
601 void onSuccess(final Object resp) throws Exception {
605 class OnCommitFutureComplete extends OnFutureComplete {
606 OnCommitFutureComplete() {
607 super(CommitTransactionReply.class);
611 public void onComplete(final Throwable error, final Object resp) {
612 super.onComplete(error, resp);
613 commitLatch.countDown();
617 class OnCanCommitFutureComplete extends OnFutureComplete {
618 private final String transactionID;
620 OnCanCommitFutureComplete(final String transactionID) {
621 super(CanCommitTransactionReply.class);
622 this.transactionID = transactionID;
626 void onSuccess(final Object resp) throws Exception {
627 final CanCommitTransactionReply canCommitReply =
628 CanCommitTransactionReply.fromSerializable(resp);
629 assertEquals("Can commit", true, canCommitReply.getCanCommit());
631 final Future<Object> commitFuture = Patterns.ask(shard,
632 new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), timeout);
633 commitFuture.onComplete(new OnCommitFutureComplete(), getSystem().dispatcher());
637 canCommitFuture1.onComplete(new OnCanCommitFutureComplete(transactionID2),
638 getSystem().dispatcher());
640 canCommitFuture2.onComplete(new OnCanCommitFutureComplete(transactionID3),
641 getSystem().dispatcher());
643 final boolean done = commitLatch.await(timeoutSec, TimeUnit.SECONDS);
645 if(caughtEx.get() != null) {
646 throw caughtEx.get();
649 assertEquals("Commits complete", true, done);
651 final InOrder inOrder = inOrder(cohort1, cohort2, cohort3);
652 inOrder.verify(cohort1).canCommit();
653 inOrder.verify(cohort1).preCommit();
654 inOrder.verify(cohort1).commit();
655 inOrder.verify(cohort2).canCommit();
656 inOrder.verify(cohort2).preCommit();
657 inOrder.verify(cohort2).commit();
658 inOrder.verify(cohort3).canCommit();
659 inOrder.verify(cohort3).preCommit();
660 inOrder.verify(cohort3).commit();
662 // Verify data in the data store.
664 verifyOuterListEntry(shard, 1);
666 verifyLastApplied(shard, 2);
671 public void testBatchedModificationsWithNoCommitOnReady() throws Throwable {
672 new ShardTestKit(getSystem()) {{
673 final TestActorRef<Shard> shard = actorFactory.createTestActor(
674 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
675 "testBatchedModificationsWithNoCommitOnReady");
677 waitUntilLeader(shard);
679 final String transactionID = "tx";
680 final FiniteDuration duration = duration("5 seconds");
682 final AtomicReference<ShardDataTreeCohort> mockCohort = new AtomicReference<>();
683 final ShardCommitCoordinator.CohortDecorator cohortDecorator = (txID, actual) -> {
684 if(mockCohort.get() == null) {
685 mockCohort.set(createDelegatingMockCohort("cohort", actual));
688 return mockCohort.get();
691 shard.underlyingActor().getCommitCoordinator().setCohortDecorator(cohortDecorator);
693 // Send a BatchedModifications to start a transaction.
695 shard.tell(newBatchedModifications(transactionID, TestModel.TEST_PATH,
696 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false, false, 1), getRef());
697 expectMsgClass(duration, BatchedModificationsReply.class);
699 // Send a couple more BatchedModifications.
701 shard.tell(newBatchedModifications(transactionID, TestModel.OUTER_LIST_PATH,
702 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false, false, 2), getRef());
703 expectMsgClass(duration, BatchedModificationsReply.class);
705 shard.tell(newBatchedModifications(transactionID, YangInstanceIdentifier.builder(
706 TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
707 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), true, false, 3), getRef());
708 expectMsgClass(duration, ReadyTransactionReply.class);
710 // Send the CanCommitTransaction message.
712 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
713 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
714 expectMsgClass(duration, CanCommitTransactionReply.class));
715 assertEquals("Can commit", true, canCommitReply.getCanCommit());
717 // Send the CanCommitTransaction message.
719 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
720 expectMsgClass(duration, CommitTransactionReply.class);
722 final InOrder inOrder = inOrder(mockCohort.get());
723 inOrder.verify(mockCohort.get()).canCommit();
724 inOrder.verify(mockCohort.get()).preCommit();
725 inOrder.verify(mockCohort.get()).commit();
727 // Verify data in the data store.
729 verifyOuterListEntry(shard, 1);
734 public void testBatchedModificationsWithCommitOnReady() throws Throwable {
735 new ShardTestKit(getSystem()) {{
736 final TestActorRef<Shard> shard = actorFactory.createTestActor(
737 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
738 "testBatchedModificationsWithCommitOnReady");
740 waitUntilLeader(shard);
742 final String transactionID = "tx";
743 final FiniteDuration duration = duration("5 seconds");
745 final AtomicReference<ShardDataTreeCohort> mockCohort = new AtomicReference<>();
746 final ShardCommitCoordinator.CohortDecorator cohortDecorator = (txID, actual) -> {
747 if(mockCohort.get() == null) {
748 mockCohort.set(createDelegatingMockCohort("cohort", actual));
751 return mockCohort.get();
754 shard.underlyingActor().getCommitCoordinator().setCohortDecorator(cohortDecorator);
756 // Send a BatchedModifications to start a transaction.
758 shard.tell(newBatchedModifications(transactionID, TestModel.TEST_PATH,
759 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false, false, 1), getRef());
760 expectMsgClass(duration, BatchedModificationsReply.class);
762 // Send a couple more BatchedModifications.
764 shard.tell(newBatchedModifications(transactionID, TestModel.OUTER_LIST_PATH,
765 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false, false, 2), getRef());
766 expectMsgClass(duration, BatchedModificationsReply.class);
768 shard.tell(newBatchedModifications(transactionID, YangInstanceIdentifier.builder(
769 TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
770 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), true, true, 3), getRef());
772 expectMsgClass(duration, CommitTransactionReply.class);
774 final InOrder inOrder = inOrder(mockCohort.get());
775 inOrder.verify(mockCohort.get()).canCommit();
776 inOrder.verify(mockCohort.get()).preCommit();
777 inOrder.verify(mockCohort.get()).commit();
779 // Verify data in the data store.
781 verifyOuterListEntry(shard, 1);
785 @Test(expected=IllegalStateException.class)
786 public void testBatchedModificationsReadyWithIncorrectTotalMessageCount() throws Throwable {
787 new ShardTestKit(getSystem()) {{
788 final TestActorRef<Shard> shard = actorFactory.createTestActor(
789 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
790 "testBatchedModificationsReadyWithIncorrectTotalMessageCount");
792 waitUntilLeader(shard);
794 final String transactionID = "tx1";
795 final BatchedModifications batched = new BatchedModifications(transactionID, DataStoreVersions.CURRENT_VERSION, null);
796 batched.setReady(true);
797 batched.setTotalMessagesSent(2);
799 shard.tell(batched, getRef());
801 final Failure failure = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
803 if(failure != null) {
804 throw failure.cause();
810 public void testBatchedModificationsWithOperationFailure() throws Throwable {
811 new ShardTestKit(getSystem()) {{
812 final TestActorRef<Shard> shard = actorFactory.createTestActor(
813 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
814 "testBatchedModificationsWithOperationFailure");
816 waitUntilLeader(shard);
818 // Test merge with invalid data. An exception should occur when the merge is applied. Note that
819 // write will not validate the children for performance reasons.
821 String transactionID = "tx1";
823 ContainerNode invalidData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
824 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
825 withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
827 BatchedModifications batched = new BatchedModifications(transactionID, CURRENT_VERSION, null);
828 batched.addModification(new MergeModification(TestModel.TEST_PATH, invalidData));
829 shard.tell(batched, getRef());
830 Failure failure = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
832 Throwable cause = failure.cause();
834 batched = new BatchedModifications(transactionID, DataStoreVersions.CURRENT_VERSION, null);
835 batched.setReady(true);
836 batched.setTotalMessagesSent(2);
838 shard.tell(batched, getRef());
840 failure = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
841 assertEquals("Failure cause", cause, failure.cause());
846 public void testBatchedModificationsOnTransactionChain() throws Throwable {
847 new ShardTestKit(getSystem()) {{
848 final TestActorRef<Shard> shard = actorFactory.createTestActor(
849 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
850 "testBatchedModificationsOnTransactionChain");
852 waitUntilLeader(shard);
854 final String transactionChainID = "txChain";
855 final String transactionID1 = "tx1";
856 final String transactionID2 = "tx2";
858 final FiniteDuration duration = duration("5 seconds");
860 // Send a BatchedModifications to start a chained write transaction and ready it.
862 final ContainerNode containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
863 final YangInstanceIdentifier path = TestModel.TEST_PATH;
864 shard.tell(newBatchedModifications(transactionID1, transactionChainID, path,
865 containerNode, true, false, 1), getRef());
866 expectMsgClass(duration, ReadyTransactionReply.class);
868 // Create a read Tx on the same chain.
870 shard.tell(new CreateTransaction(transactionID2, TransactionType.READ_ONLY.ordinal(),
871 transactionChainID, DataStoreVersions.CURRENT_VERSION).toSerializable(), getRef());
873 final CreateTransactionReply createReply = expectMsgClass(duration("3 seconds"), CreateTransactionReply.class);
875 getSystem().actorSelection(createReply.getTransactionPath()).tell(
876 new ReadData(path, DataStoreVersions.CURRENT_VERSION), getRef());
877 final ReadDataReply readReply = expectMsgClass(duration("3 seconds"), ReadDataReply.class);
878 assertEquals("Read node", containerNode, readReply.getNormalizedNode());
880 // Commit the write transaction.
882 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
883 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
884 expectMsgClass(duration, CanCommitTransactionReply.class));
885 assertEquals("Can commit", true, canCommitReply.getCanCommit());
887 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
888 expectMsgClass(duration, CommitTransactionReply.class);
890 // Verify data in the data store.
892 final NormalizedNode<?, ?> actualNode = readStore(shard, path);
893 assertEquals("Stored node", containerNode, actualNode);
898 public void testOnBatchedModificationsWhenNotLeader() {
899 final AtomicBoolean overrideLeaderCalls = new AtomicBoolean();
900 new ShardTestKit(getSystem()) {{
901 final Creator<Shard> creator = new Creator<Shard>() {
902 private static final long serialVersionUID = 1L;
905 public Shard create() throws Exception {
906 return new Shard(newShardBuilder()) {
908 protected boolean isLeader() {
909 return overrideLeaderCalls.get() ? false : super.isLeader();
913 protected ActorSelection getLeader() {
914 return overrideLeaderCalls.get() ? getSystem().actorSelection(getRef().path()) :
921 final TestActorRef<Shard> shard = actorFactory.createTestActor(
922 Props.create(new DelegatingShardCreator(creator)), "testOnBatchedModificationsWhenNotLeader");
924 waitUntilLeader(shard);
926 overrideLeaderCalls.set(true);
928 final BatchedModifications batched = new BatchedModifications("tx", DataStoreVersions.CURRENT_VERSION, "");
930 shard.tell(batched, ActorRef.noSender());
932 expectMsgEquals(batched);
937 public void testTransactionMessagesWithNoLeader() {
938 new ShardTestKit(getSystem()) {{
939 dataStoreContextBuilder.customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName()).
940 shardHeartbeatIntervalInMillis(50).shardElectionTimeoutFactor(1);
941 final TestActorRef<Shard> shard = actorFactory.createTestActor(
942 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
943 "testTransactionMessagesWithNoLeader");
945 waitUntilNoLeader(shard);
947 shard.tell(new BatchedModifications("tx", DataStoreVersions.CURRENT_VERSION, ""), getRef());
948 Failure failure = expectMsgClass(Failure.class);
949 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
951 shard.tell(prepareForwardedReadyTransaction(mock(ShardDataTreeCohort.class), "tx",
952 DataStoreVersions.CURRENT_VERSION, true), getRef());
953 failure = expectMsgClass(Failure.class);
954 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
956 shard.tell(new ReadyLocalTransaction("tx", mock(DataTreeModification.class), true), getRef());
957 failure = expectMsgClass(Failure.class);
958 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
963 public void testReadyWithReadWriteImmediateCommit() throws Exception{
964 testReadyWithImmediateCommit(true);
968 public void testReadyWithWriteOnlyImmediateCommit() throws Exception{
969 testReadyWithImmediateCommit(false);
972 private void testReadyWithImmediateCommit(final boolean readWrite) throws Exception{
973 new ShardTestKit(getSystem()) {{
974 final TestActorRef<Shard> shard = actorFactory.createTestActor(
975 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
976 "testReadyWithImmediateCommit-" + readWrite);
978 waitUntilLeader(shard);
980 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
982 final String transactionID = "tx1";
983 final MutableCompositeModification modification = new MutableCompositeModification();
984 final NormalizedNode<?, ?> containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
985 final ShardDataTreeCohort cohort = setupMockWriteTransaction("cohort", dataStore,
986 TestModel.TEST_PATH, containerNode, modification);
988 final FiniteDuration duration = duration("5 seconds");
990 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort, transactionID, modification, true), getRef());
992 expectMsgClass(duration, CommitTransactionReply.class);
994 final InOrder inOrder = inOrder(cohort);
995 inOrder.verify(cohort).canCommit();
996 inOrder.verify(cohort).preCommit();
997 inOrder.verify(cohort).commit();
999 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.TEST_PATH);
1000 assertEquals(TestModel.TEST_QNAME.getLocalName(), containerNode, actualNode);
1005 public void testReadyLocalTransactionWithImmediateCommit() throws Exception{
1006 new ShardTestKit(getSystem()) {{
1007 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1008 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1009 "testReadyLocalTransactionWithImmediateCommit");
1011 waitUntilLeader(shard);
1013 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1015 final DataTreeModification modification = dataStore.newModification();
1017 final ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1018 new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
1019 final MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
1020 new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
1022 final String txId = "tx1";
1023 modification.ready();
1024 final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(txId, modification, true);
1026 shard.tell(readyMessage, getRef());
1028 expectMsgClass(CommitTransactionReply.class);
1030 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.OUTER_LIST_PATH);
1031 assertEquals(TestModel.OUTER_LIST_QNAME.getLocalName(), mergeData, actualNode);
1036 public void testReadyLocalTransactionWithThreePhaseCommit() throws Exception{
1037 new ShardTestKit(getSystem()) {{
1038 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1039 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1040 "testReadyLocalTransactionWithThreePhaseCommit");
1042 waitUntilLeader(shard);
1044 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1046 final DataTreeModification modification = dataStore.newModification();
1048 final ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1049 new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
1050 final MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
1051 new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
1053 final String txId = "tx1";
1054 modification.ready();
1055 final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(txId, modification, false);
1057 shard.tell(readyMessage, getRef());
1059 expectMsgClass(ReadyTransactionReply.class);
1061 // Send the CanCommitTransaction message.
1063 shard.tell(new CanCommitTransaction(txId, CURRENT_VERSION).toSerializable(), getRef());
1064 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1065 expectMsgClass(CanCommitTransactionReply.class));
1066 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1068 // Send the CanCommitTransaction message.
1070 shard.tell(new CommitTransaction(txId, CURRENT_VERSION).toSerializable(), getRef());
1071 expectMsgClass(CommitTransactionReply.class);
1073 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.OUTER_LIST_PATH);
1074 assertEquals(TestModel.OUTER_LIST_QNAME.getLocalName(), mergeData, actualNode);
1079 public void testReadWriteCommitWithPersistenceDisabled() throws Throwable {
1080 testCommitWithPersistenceDisabled(true);
1084 public void testWriteOnlyCommitWithPersistenceDisabled() throws Throwable {
1085 testCommitWithPersistenceDisabled(true);
1088 private void testCommitWithPersistenceDisabled(final boolean readWrite) throws Throwable {
1089 dataStoreContextBuilder.persistent(false);
1090 new ShardTestKit(getSystem()) {{
1091 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1092 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1093 "testCommitWithPersistenceDisabled-" + readWrite);
1095 waitUntilLeader(shard);
1097 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1099 // Setup a simulated transactions with a mock cohort.
1101 final String transactionID = "tx";
1102 final MutableCompositeModification modification = new MutableCompositeModification();
1103 final NormalizedNode<?, ?> containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1104 final ShardDataTreeCohort cohort = setupMockWriteTransaction("cohort", dataStore,
1105 TestModel.TEST_PATH, containerNode, modification);
1107 final FiniteDuration duration = duration("5 seconds");
1109 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort, transactionID, modification), getRef());
1110 expectMsgClass(duration, ReadyTransactionReply.class);
1112 // Send the CanCommitTransaction message.
1114 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1115 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1116 expectMsgClass(duration, CanCommitTransactionReply.class));
1117 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1119 // Send the CanCommitTransaction message.
1121 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1122 expectMsgClass(duration, CommitTransactionReply.class);
1124 final InOrder inOrder = inOrder(cohort);
1125 inOrder.verify(cohort).canCommit();
1126 inOrder.verify(cohort).preCommit();
1127 inOrder.verify(cohort).commit();
1129 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.TEST_PATH);
1130 assertEquals(TestModel.TEST_QNAME.getLocalName(), containerNode, actualNode);
1135 public void testReadWriteCommitWhenTransactionHasNoModifications() {
1136 testCommitWhenTransactionHasNoModifications(true);
1140 public void testWriteOnlyCommitWhenTransactionHasNoModifications() {
1141 testCommitWhenTransactionHasNoModifications(false);
1144 private void testCommitWhenTransactionHasNoModifications(final boolean readWrite){
1145 // Note that persistence is enabled which would normally result in the entry getting written to the journal
1146 // but here that need not happen
1147 new ShardTestKit(getSystem()) {
1149 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1150 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1151 "testCommitWhenTransactionHasNoModifications-" + readWrite);
1153 waitUntilLeader(shard);
1155 final String transactionID = "tx1";
1156 final MutableCompositeModification modification = new MutableCompositeModification();
1157 final ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class, "cohort1");
1158 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1159 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).preCommit();
1160 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).commit();
1161 doReturn(mockUnmodifiedCandidate("cohort1-candidate")).when(cohort).getCandidate();
1163 final FiniteDuration duration = duration("5 seconds");
1165 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort, transactionID, modification), getRef());
1166 expectMsgClass(duration, ReadyTransactionReply.class);
1168 // Send the CanCommitTransaction message.
1170 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1171 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1172 expectMsgClass(duration, CanCommitTransactionReply.class));
1173 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1175 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1176 expectMsgClass(duration, CommitTransactionReply.class);
1178 final InOrder inOrder = inOrder(cohort);
1179 inOrder.verify(cohort).canCommit();
1180 inOrder.verify(cohort).preCommit();
1181 inOrder.verify(cohort).commit();
1183 shard.tell(Shard.GET_SHARD_MBEAN_MESSAGE, getRef());
1184 final ShardStats shardStats = expectMsgClass(duration, ShardStats.class);
1186 // Use MBean for verification
1187 // Committed transaction count should increase as usual
1188 assertEquals(1,shardStats.getCommittedTransactionsCount());
1190 // Commit index should not advance because this does not go into the journal
1191 assertEquals(-1, shardStats.getCommitIndex());
1197 public void testReadWriteCommitWhenTransactionHasModifications() {
1198 testCommitWhenTransactionHasModifications(true);
1202 public void testWriteOnlyCommitWhenTransactionHasModifications() {
1203 testCommitWhenTransactionHasModifications(false);
1206 private void testCommitWhenTransactionHasModifications(final boolean readWrite){
1207 new ShardTestKit(getSystem()) {
1209 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1210 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1211 "testCommitWhenTransactionHasModifications-" + readWrite);
1213 waitUntilLeader(shard);
1215 final String transactionID = "tx1";
1216 final MutableCompositeModification modification = new MutableCompositeModification();
1217 modification.addModification(new DeleteModification(YangInstanceIdentifier.builder().build()));
1218 final ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class, "cohort1");
1219 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1220 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).preCommit();
1221 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).commit();
1222 doReturn(mockCandidate("cohort1-candidate")).when(cohort).getCandidate();
1224 final FiniteDuration duration = duration("5 seconds");
1226 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort, transactionID, modification), getRef());
1227 expectMsgClass(duration, ReadyTransactionReply.class);
1229 // Send the CanCommitTransaction message.
1231 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1232 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1233 expectMsgClass(duration, CanCommitTransactionReply.class));
1234 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1236 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1237 expectMsgClass(duration, CommitTransactionReply.class);
1239 final InOrder inOrder = inOrder(cohort);
1240 inOrder.verify(cohort).canCommit();
1241 inOrder.verify(cohort).preCommit();
1242 inOrder.verify(cohort).commit();
1244 shard.tell(Shard.GET_SHARD_MBEAN_MESSAGE, getRef());
1245 final ShardStats shardStats = expectMsgClass(duration, ShardStats.class);
1247 // Use MBean for verification
1248 // Committed transaction count should increase as usual
1249 assertEquals(1, shardStats.getCommittedTransactionsCount());
1251 // Commit index should advance as we do not have an empty modification
1252 assertEquals(0, shardStats.getCommitIndex());
1258 public void testCommitPhaseFailure() throws Throwable {
1259 testCommitPhaseFailure(true);
1260 testCommitPhaseFailure(false);
1263 private void testCommitPhaseFailure(final boolean readWrite) throws Throwable {
1264 new ShardTestKit(getSystem()) {{
1265 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1266 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1267 "testCommitPhaseFailure-" + readWrite);
1269 waitUntilLeader(shard);
1271 // Setup 2 simulated transactions with mock cohorts. The first one fails in the
1274 final String transactionID1 = "tx1";
1275 final MutableCompositeModification modification1 = new MutableCompositeModification();
1276 final ShardDataTreeCohort cohort1 = mock(ShardDataTreeCohort.class, "cohort1");
1277 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort1).canCommit();
1278 doReturn(Futures.immediateFuture(null)).when(cohort1).preCommit();
1279 doReturn(Futures.immediateFailedFuture(new RuntimeException("mock"))).when(cohort1).commit();
1280 doReturn(mockCandidate("cohort1-candidate")).when(cohort1).getCandidate();
1282 final String transactionID2 = "tx2";
1283 final MutableCompositeModification modification2 = new MutableCompositeModification();
1284 final ShardDataTreeCohort cohort2 = mock(ShardDataTreeCohort.class, "cohort2");
1285 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort2).canCommit();
1287 final FiniteDuration duration = duration("5 seconds");
1288 final Timeout timeout = new Timeout(duration);
1290 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort1, transactionID1, modification1), getRef());
1291 expectMsgClass(duration, ReadyTransactionReply.class);
1293 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort2, transactionID2, modification2), getRef());
1294 expectMsgClass(duration, ReadyTransactionReply.class);
1296 // Send the CanCommitTransaction message for the first Tx.
1298 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1299 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1300 expectMsgClass(duration, CanCommitTransactionReply.class));
1301 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1303 // Send the CanCommitTransaction message for the 2nd Tx. This should get queued and
1304 // processed after the first Tx completes.
1306 final Future<Object> canCommitFuture = Patterns.ask(shard,
1307 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1309 // Send the CommitTransaction message for the first Tx. This should send back an error
1310 // and trigger the 2nd Tx to proceed.
1312 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1313 expectMsgClass(duration, akka.actor.Status.Failure.class);
1315 // Wait for the 2nd Tx to complete the canCommit phase.
1317 final CountDownLatch latch = new CountDownLatch(1);
1318 canCommitFuture.onComplete(new OnComplete<Object>() {
1320 public void onComplete(final Throwable t, final Object resp) {
1323 }, getSystem().dispatcher());
1325 assertEquals("2nd CanCommit complete", true, latch.await(5, TimeUnit.SECONDS));
1327 final InOrder inOrder = inOrder(cohort1, cohort2);
1328 inOrder.verify(cohort1).canCommit();
1329 inOrder.verify(cohort1).preCommit();
1330 inOrder.verify(cohort1).commit();
1331 inOrder.verify(cohort2).canCommit();
1336 public void testPreCommitPhaseFailure() throws Throwable {
1337 testPreCommitPhaseFailure(true);
1338 testPreCommitPhaseFailure(false);
1341 private void testPreCommitPhaseFailure(final boolean readWrite) throws Throwable {
1342 new ShardTestKit(getSystem()) {{
1343 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1344 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1345 "testPreCommitPhaseFailure-" + readWrite);
1347 waitUntilLeader(shard);
1349 final String transactionID1 = "tx1";
1350 final MutableCompositeModification modification1 = new MutableCompositeModification();
1351 final ShardDataTreeCohort cohort1 = mock(ShardDataTreeCohort.class, "cohort1");
1352 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort1).canCommit();
1353 doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock"))).when(cohort1).preCommit();
1355 final String transactionID2 = "tx2";
1356 final MutableCompositeModification modification2 = new MutableCompositeModification();
1357 final ShardDataTreeCohort cohort2 = mock(ShardDataTreeCohort.class, "cohort2");
1358 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort2).canCommit();
1360 final FiniteDuration duration = duration("5 seconds");
1361 final Timeout timeout = new Timeout(duration);
1363 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort1, transactionID1, modification1), getRef());
1364 expectMsgClass(duration, ReadyTransactionReply.class);
1366 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort2, transactionID2, modification2), getRef());
1367 expectMsgClass(duration, ReadyTransactionReply.class);
1369 // Send the CanCommitTransaction message for the first Tx.
1371 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1372 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1373 expectMsgClass(duration, CanCommitTransactionReply.class));
1374 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1376 // Send the CanCommitTransaction message for the 2nd Tx. This should get queued and
1377 // processed after the first Tx completes.
1379 final Future<Object> canCommitFuture = Patterns.ask(shard,
1380 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1382 // Send the CommitTransaction message for the first Tx. This should send back an error
1383 // and trigger the 2nd Tx to proceed.
1385 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1386 expectMsgClass(duration, akka.actor.Status.Failure.class);
1388 // Wait for the 2nd Tx to complete the canCommit phase.
1390 final CountDownLatch latch = new CountDownLatch(1);
1391 canCommitFuture.onComplete(new OnComplete<Object>() {
1393 public void onComplete(final Throwable t, final Object resp) {
1396 }, getSystem().dispatcher());
1398 assertEquals("2nd CanCommit complete", true, latch.await(5, TimeUnit.SECONDS));
1400 final InOrder inOrder = inOrder(cohort1, cohort2);
1401 inOrder.verify(cohort1).canCommit();
1402 inOrder.verify(cohort1).preCommit();
1403 inOrder.verify(cohort2).canCommit();
1408 public void testCanCommitPhaseFailure() throws Throwable {
1409 testCanCommitPhaseFailure(true);
1410 testCanCommitPhaseFailure(false);
1413 private void testCanCommitPhaseFailure(final boolean readWrite) throws Throwable {
1414 new ShardTestKit(getSystem()) {{
1415 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1416 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1417 "testCanCommitPhaseFailure-" + readWrite);
1419 waitUntilLeader(shard);
1421 final FiniteDuration duration = duration("5 seconds");
1423 final String transactionID1 = "tx1";
1424 final MutableCompositeModification modification = new MutableCompositeModification();
1425 final ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class, "cohort1");
1426 doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock"))).when(cohort).canCommit();
1428 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort, transactionID1, modification), getRef());
1429 expectMsgClass(duration, ReadyTransactionReply.class);
1431 // Send the CanCommitTransaction message.
1433 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1434 expectMsgClass(duration, akka.actor.Status.Failure.class);
1436 // Send another can commit to ensure the failed one got cleaned up.
1440 final String transactionID2 = "tx2";
1441 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1443 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort, transactionID2, modification), getRef());
1444 expectMsgClass(duration, ReadyTransactionReply.class);
1446 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1447 final CanCommitTransactionReply reply = CanCommitTransactionReply.fromSerializable(
1448 expectMsgClass(CanCommitTransactionReply.class));
1449 assertEquals("getCanCommit", true, reply.getCanCommit());
1454 public void testCanCommitPhaseFalseResponse() throws Throwable {
1455 testCanCommitPhaseFalseResponse(true);
1456 testCanCommitPhaseFalseResponse(false);
1459 private void testCanCommitPhaseFalseResponse(final boolean readWrite) throws Throwable {
1460 new ShardTestKit(getSystem()) {{
1461 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1462 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1463 "testCanCommitPhaseFalseResponse-" + readWrite);
1465 waitUntilLeader(shard);
1467 final FiniteDuration duration = duration("5 seconds");
1469 final String transactionID1 = "tx1";
1470 final MutableCompositeModification modification = new MutableCompositeModification();
1471 final ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class, "cohort1");
1472 doReturn(Futures.immediateFuture(Boolean.FALSE)).when(cohort).canCommit();
1474 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort, transactionID1, modification), getRef());
1475 expectMsgClass(duration, ReadyTransactionReply.class);
1477 // Send the CanCommitTransaction message.
1479 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1480 CanCommitTransactionReply reply = CanCommitTransactionReply.fromSerializable(
1481 expectMsgClass(CanCommitTransactionReply.class));
1482 assertEquals("getCanCommit", false, reply.getCanCommit());
1484 // Send another can commit to ensure the failed one got cleaned up.
1488 final String transactionID2 = "tx2";
1489 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1491 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort, transactionID2, modification), getRef());
1492 expectMsgClass(duration, ReadyTransactionReply.class);
1494 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1495 reply = CanCommitTransactionReply.fromSerializable(
1496 expectMsgClass(CanCommitTransactionReply.class));
1497 assertEquals("getCanCommit", true, reply.getCanCommit());
1502 public void testImmediateCommitWithCanCommitPhaseFailure() throws Throwable {
1503 testImmediateCommitWithCanCommitPhaseFailure(true);
1504 testImmediateCommitWithCanCommitPhaseFailure(false);
1507 private void testImmediateCommitWithCanCommitPhaseFailure(final boolean readWrite) throws Throwable {
1508 new ShardTestKit(getSystem()) {{
1509 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1510 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1511 "testImmediateCommitWithCanCommitPhaseFailure-" + readWrite);
1513 waitUntilLeader(shard);
1515 final FiniteDuration duration = duration("5 seconds");
1517 final String transactionID1 = "tx1";
1518 final MutableCompositeModification modification = new MutableCompositeModification();
1519 final ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class, "cohort1");
1520 doReturn(Futures.immediateFailedFuture(new IllegalStateException("mock"))).when(cohort).canCommit();
1522 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort, transactionID1, modification, true), getRef());
1524 expectMsgClass(duration, akka.actor.Status.Failure.class);
1526 // Send another can commit to ensure the failed one got cleaned up.
1530 final String transactionID2 = "tx2";
1531 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1532 doReturn(Futures.immediateFuture(null)).when(cohort).preCommit();
1533 doReturn(Futures.immediateFuture(null)).when(cohort).commit();
1534 final DataTreeCandidateTip candidate = mock(DataTreeCandidateTip.class);
1535 final DataTreeCandidateNode candidateRoot = mock(DataTreeCandidateNode.class);
1536 doReturn(ModificationType.UNMODIFIED).when(candidateRoot).getModificationType();
1537 doReturn(candidateRoot).when(candidate).getRootNode();
1538 doReturn(YangInstanceIdentifier.EMPTY).when(candidate).getRootPath();
1539 doReturn(candidate).when(cohort).getCandidate();
1541 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort, transactionID2, modification, true), getRef());
1543 expectMsgClass(duration, CommitTransactionReply.class);
1548 public void testImmediateCommitWithCanCommitPhaseFalseResponse() throws Throwable {
1549 testImmediateCommitWithCanCommitPhaseFalseResponse(true);
1550 testImmediateCommitWithCanCommitPhaseFalseResponse(false);
1553 private void testImmediateCommitWithCanCommitPhaseFalseResponse(final boolean readWrite) throws Throwable {
1554 new ShardTestKit(getSystem()) {{
1555 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1556 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1557 "testImmediateCommitWithCanCommitPhaseFalseResponse-" + readWrite);
1559 waitUntilLeader(shard);
1561 final FiniteDuration duration = duration("5 seconds");
1563 final String transactionID = "tx1";
1564 final MutableCompositeModification modification = new MutableCompositeModification();
1565 final ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class, "cohort1");
1566 doReturn(Futures.immediateFuture(Boolean.FALSE)).when(cohort).canCommit();
1568 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort, transactionID, modification, true), getRef());
1570 expectMsgClass(duration, akka.actor.Status.Failure.class);
1572 // Send another can commit to ensure the failed one got cleaned up.
1576 final String transactionID2 = "tx2";
1577 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort).canCommit();
1578 doReturn(Futures.immediateFuture(null)).when(cohort).preCommit();
1579 doReturn(Futures.immediateFuture(null)).when(cohort).commit();
1580 final DataTreeCandidateTip candidate = mock(DataTreeCandidateTip.class);
1581 final DataTreeCandidateNode candidateRoot = mock(DataTreeCandidateNode.class);
1582 doReturn(ModificationType.UNMODIFIED).when(candidateRoot).getModificationType();
1583 doReturn(candidateRoot).when(candidate).getRootNode();
1584 doReturn(YangInstanceIdentifier.EMPTY).when(candidate).getRootPath();
1585 doReturn(candidate).when(cohort).getCandidate();
1587 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort, transactionID2, modification, true), getRef());
1589 expectMsgClass(duration, CommitTransactionReply.class);
1594 public void testAbortBeforeFinishCommit() throws Throwable {
1595 testAbortBeforeFinishCommit(true);
1596 testAbortBeforeFinishCommit(false);
1599 private void testAbortBeforeFinishCommit(final boolean readWrite) throws Throwable {
1600 new ShardTestKit(getSystem()) {{
1601 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1602 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1603 "testAbortBeforeFinishCommit-" + readWrite);
1605 waitUntilLeader(shard);
1607 final FiniteDuration duration = duration("5 seconds");
1608 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1610 final String transactionID = "tx1";
1611 final Function<ShardDataTreeCohort, ListenableFuture<Void>> preCommit =
1613 final ListenableFuture<Void> preCommitFuture = cohort.preCommit();
1615 // Simulate an AbortTransaction message occurring during replication, after
1616 // persisting and before finishing the commit to the in-memory store.
1617 // We have no followers so due to optimizations in the RaftActor, it does not
1618 // attempt replication and thus we can't send an AbortTransaction message b/c
1619 // it would be processed too late after CommitTransaction completes. So we'll
1620 // simulate an AbortTransaction message occurring during replication by calling
1621 // the shard directly.
1623 shard.underlyingActor().doAbortTransaction(transactionID, null);
1625 return preCommitFuture;
1628 final MutableCompositeModification modification = new MutableCompositeModification();
1629 final ShardDataTreeCohort cohort = setupMockWriteTransaction("cohort1", dataStore,
1630 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME),
1631 modification, preCommit);
1633 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort, transactionID, modification), getRef());
1634 expectMsgClass(duration, ReadyTransactionReply.class);
1636 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1637 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1638 expectMsgClass(duration, CanCommitTransactionReply.class));
1639 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1641 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1642 expectMsgClass(duration, CommitTransactionReply.class);
1644 final NormalizedNode<?, ?> node = readStore(shard, TestModel.TEST_PATH);
1646 // Since we're simulating an abort occurring during replication and before finish commit,
1647 // the data should still get written to the in-memory store since we've gotten past
1648 // canCommit and preCommit and persisted the data.
1649 assertNotNull(TestModel.TEST_QNAME.getLocalName() + " not found", node);
1654 public void testTransactionCommitTimeout() throws Throwable {
1655 testTransactionCommitTimeout(true);
1656 testTransactionCommitTimeout(false);
1659 private void testTransactionCommitTimeout(final boolean readWrite) throws Throwable {
1660 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1662 new ShardTestKit(getSystem()) {{
1663 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1664 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1665 "testTransactionCommitTimeout-" + readWrite);
1667 waitUntilLeader(shard);
1669 final FiniteDuration duration = duration("5 seconds");
1671 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1673 writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1674 writeToStore(shard, TestModel.OUTER_LIST_PATH,
1675 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1677 // Create 1st Tx - will timeout
1679 final String transactionID1 = "tx1";
1680 final MutableCompositeModification modification1 = new MutableCompositeModification();
1681 final ShardDataTreeCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
1682 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1683 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
1684 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1),
1689 final String transactionID2 = "tx3";
1690 final MutableCompositeModification modification2 = new MutableCompositeModification();
1691 final YangInstanceIdentifier listNodePath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1692 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build();
1693 final ShardDataTreeCohort cohort2 = setupMockWriteTransaction("cohort3", dataStore,
1695 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2),
1700 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort1, transactionID1, modification1), getRef());
1701 expectMsgClass(duration, ReadyTransactionReply.class);
1703 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort2, transactionID2, modification2), getRef());
1704 expectMsgClass(duration, ReadyTransactionReply.class);
1706 // canCommit 1st Tx. We don't send the commit so it should timeout.
1708 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1709 expectMsgClass(duration, CanCommitTransactionReply.class);
1711 // canCommit the 2nd Tx - it should complete after the 1st Tx times out.
1713 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1714 expectMsgClass(duration, CanCommitTransactionReply.class);
1716 // Try to commit the 1st Tx - should fail as it's not the current Tx.
1718 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1719 expectMsgClass(duration, akka.actor.Status.Failure.class);
1721 // Commit the 2nd Tx.
1723 shard.tell(new CommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1724 expectMsgClass(duration, CommitTransactionReply.class);
1726 final NormalizedNode<?, ?> node = readStore(shard, listNodePath);
1727 assertNotNull(listNodePath + " not found", node);
1732 public void testTransactionCommitQueueCapacityExceeded() throws Throwable {
1733 dataStoreContextBuilder.shardTransactionCommitQueueCapacity(2);
1735 new ShardTestKit(getSystem()) {{
1736 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1737 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1738 "testTransactionCommitQueueCapacityExceeded");
1740 waitUntilLeader(shard);
1742 final FiniteDuration duration = duration("5 seconds");
1744 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1746 final String transactionID1 = "tx1";
1747 final MutableCompositeModification modification1 = new MutableCompositeModification();
1748 final ShardDataTreeCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
1749 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification1);
1751 final String transactionID2 = "tx2";
1752 final MutableCompositeModification modification2 = new MutableCompositeModification();
1753 final ShardDataTreeCohort cohort2 = setupMockWriteTransaction("cohort2", dataStore,
1754 TestModel.OUTER_LIST_PATH,
1755 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(),
1758 final String transactionID3 = "tx3";
1759 final MutableCompositeModification modification3 = new MutableCompositeModification();
1760 final ShardDataTreeCohort cohort3 = setupMockWriteTransaction("cohort3", dataStore,
1761 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification3);
1765 shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort1, transactionID1, modification1), getRef());
1766 expectMsgClass(duration, ReadyTransactionReply.class);
1768 shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort2, transactionID2, modification2), getRef());
1769 expectMsgClass(duration, ReadyTransactionReply.class);
1771 // The 3rd Tx should exceed queue capacity and fail.
1773 shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort3, transactionID3, modification3), getRef());
1774 expectMsgClass(duration, akka.actor.Status.Failure.class);
1776 // canCommit 1st Tx.
1778 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1779 expectMsgClass(duration, CanCommitTransactionReply.class);
1781 // canCommit the 2nd Tx - it should get queued.
1783 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1785 // canCommit the 3rd Tx - should exceed queue capacity and fail.
1787 shard.tell(new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), getRef());
1788 expectMsgClass(duration, akka.actor.Status.Failure.class);
1793 public void testTransactionCommitWithPriorExpiredCohortEntries() throws Throwable {
1794 dataStoreContextBuilder.shardCommitQueueExpiryTimeoutInMillis(1300).shardTransactionCommitTimeoutInSeconds(1);
1796 new ShardTestKit(getSystem()) {{
1797 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1798 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1799 "testTransactionCommitWithPriorExpiredCohortEntries");
1801 waitUntilLeader(shard);
1803 final FiniteDuration duration = duration("5 seconds");
1805 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1807 final String transactionID1 = "tx1";
1808 final MutableCompositeModification modification1 = new MutableCompositeModification();
1809 final ShardDataTreeCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
1810 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification1);
1812 shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort1, transactionID1, modification1), getRef());
1813 expectMsgClass(duration, ReadyTransactionReply.class);
1815 final String transactionID2 = "tx2";
1816 final MutableCompositeModification modification2 = new MutableCompositeModification();
1817 final ShardDataTreeCohort cohort2 = setupMockWriteTransaction("cohort2", dataStore,
1818 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification2);
1820 shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort2, transactionID2, modification2), getRef());
1821 expectMsgClass(duration, ReadyTransactionReply.class);
1823 final String transactionID3 = "tx3";
1824 final MutableCompositeModification modification3 = new MutableCompositeModification();
1825 final ShardDataTreeCohort cohort3 = setupMockWriteTransaction("cohort3", dataStore,
1826 TestModel.TEST2_PATH, ImmutableNodes.containerNode(TestModel.TEST2_QNAME), modification3);
1828 shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort3, transactionID3, modification3), getRef());
1829 expectMsgClass(duration, ReadyTransactionReply.class);
1831 // All Tx's are readied. We'll send canCommit for the last one but not the others. The others
1832 // should expire from the queue and the last one should be processed.
1834 shard.tell(new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), getRef());
1835 expectMsgClass(duration, CanCommitTransactionReply.class);
1840 public void testTransactionCommitWithSubsequentExpiredCohortEntry() throws Throwable {
1841 dataStoreContextBuilder.shardCommitQueueExpiryTimeoutInMillis(1300).shardTransactionCommitTimeoutInSeconds(1);
1843 new ShardTestKit(getSystem()) {{
1844 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1845 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1846 "testTransactionCommitWithSubsequentExpiredCohortEntry");
1848 waitUntilLeader(shard);
1850 final FiniteDuration duration = duration("5 seconds");
1852 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1854 final String transactionID1 = "tx1";
1855 final MutableCompositeModification modification1 = new MutableCompositeModification();
1856 final ShardDataTreeCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
1857 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification1);
1859 shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort1, transactionID1, modification1), getRef());
1860 expectMsgClass(duration, ReadyTransactionReply.class);
1862 // CanCommit the first one so it's the current in-progress CohortEntry.
1864 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1865 expectMsgClass(duration, CanCommitTransactionReply.class);
1867 // Ready the second Tx.
1869 final String transactionID2 = "tx2";
1870 final MutableCompositeModification modification2 = new MutableCompositeModification();
1871 final ShardDataTreeCohort cohort2 = setupMockWriteTransaction("cohort2", dataStore,
1872 TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), modification2);
1874 shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort2, transactionID2, modification2), getRef());
1875 expectMsgClass(duration, ReadyTransactionReply.class);
1877 // Ready the third Tx.
1879 final String transactionID3 = "tx3";
1880 final DataTreeModification modification3 = dataStore.newModification();
1881 new WriteModification(TestModel.TEST2_PATH, ImmutableNodes.containerNode(TestModel.TEST2_QNAME))
1882 .apply(modification3);
1883 modification3.ready();
1884 final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(transactionID3, modification3, true);
1886 shard.tell(readyMessage, getRef());
1888 // Commit the first Tx. After completing, the second should expire from the queue and the third
1891 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1892 expectMsgClass(duration, CommitTransactionReply.class);
1894 // Expect commit reply from the third Tx.
1896 expectMsgClass(duration, CommitTransactionReply.class);
1898 final NormalizedNode<?, ?> node = readStore(shard, TestModel.TEST2_PATH);
1899 assertNotNull(TestModel.TEST2_PATH + " not found", node);
1904 public void testCanCommitBeforeReadyFailure() throws Throwable {
1905 new ShardTestKit(getSystem()) {{
1906 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1907 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1908 "testCanCommitBeforeReadyFailure");
1910 shard.tell(new CanCommitTransaction("tx", CURRENT_VERSION).toSerializable(), getRef());
1911 expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
1916 public void testAbortCurrentTransaction() throws Throwable {
1917 testAbortCurrentTransaction(true);
1918 testAbortCurrentTransaction(false);
1921 private void testAbortCurrentTransaction(final boolean readWrite) throws Throwable {
1922 new ShardTestKit(getSystem()) {{
1923 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1924 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1925 "testAbortCurrentTransaction-" + readWrite);
1927 waitUntilLeader(shard);
1929 // Setup 2 simulated transactions with mock cohorts. The first one will be aborted.
1931 final String transactionID1 = "tx1";
1932 final MutableCompositeModification modification1 = new MutableCompositeModification();
1933 final ShardDataTreeCohort cohort1 = mock(ShardDataTreeCohort.class, "cohort1");
1934 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort1).canCommit();
1935 doReturn(Futures.immediateFuture(null)).when(cohort1).abort();
1937 final String transactionID2 = "tx2";
1938 final MutableCompositeModification modification2 = new MutableCompositeModification();
1939 final ShardDataTreeCohort cohort2 = mock(ShardDataTreeCohort.class, "cohort2");
1940 doReturn(Futures.immediateFuture(Boolean.TRUE)).when(cohort2).canCommit();
1942 final FiniteDuration duration = duration("5 seconds");
1943 final Timeout timeout = new Timeout(duration);
1945 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort1, transactionID1, modification1), getRef());
1946 expectMsgClass(duration, ReadyTransactionReply.class);
1948 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort2, transactionID2, modification2), getRef());
1949 expectMsgClass(duration, ReadyTransactionReply.class);
1951 // Send the CanCommitTransaction message for the first Tx.
1953 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1954 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1955 expectMsgClass(duration, CanCommitTransactionReply.class));
1956 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1958 // Send the CanCommitTransaction message for the 2nd Tx. This should get queued and
1959 // processed after the first Tx completes.
1961 final Future<Object> canCommitFuture = Patterns.ask(shard,
1962 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1964 // Send the AbortTransaction message for the first Tx. This should trigger the 2nd
1967 shard.tell(new AbortTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1968 expectMsgClass(duration, AbortTransactionReply.class);
1970 // Wait for the 2nd Tx to complete the canCommit phase.
1972 Await.ready(canCommitFuture, duration);
1974 final InOrder inOrder = inOrder(cohort1, cohort2);
1975 inOrder.verify(cohort1).canCommit();
1976 inOrder.verify(cohort2).canCommit();
1981 public void testAbortQueuedTransaction() throws Throwable {
1982 testAbortQueuedTransaction(true);
1983 testAbortQueuedTransaction(false);
1986 private void testAbortQueuedTransaction(final boolean readWrite) throws Throwable {
1987 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1988 new ShardTestKit(getSystem()) {{
1989 final AtomicReference<CountDownLatch> cleaupCheckLatch = new AtomicReference<>();
1990 @SuppressWarnings("serial")
1991 final Creator<Shard> creator = () -> new Shard(newShardBuilder()) {
1993 public void handleCommand(final Object message) {
1994 super.handleCommand(message);
1995 if(TX_COMMIT_TIMEOUT_CHECK_MESSAGE.equals(message)) {
1996 if(cleaupCheckLatch.get() != null) {
1997 cleaupCheckLatch.get().countDown();
2003 final TestActorRef<Shard> shard = actorFactory.createTestActor(
2004 Props.create(new DelegatingShardCreator(creator)).withDispatcher(
2005 Dispatchers.DefaultDispatcherId()), "testAbortQueuedTransaction-" + readWrite);
2007 waitUntilLeader(shard);
2009 final String transactionID = "tx1";
2011 final MutableCompositeModification modification = new MutableCompositeModification();
2012 final ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class, "cohort");
2013 doReturn(Futures.immediateFuture(null)).when(cohort).abort();
2015 final FiniteDuration duration = duration("5 seconds");
2019 shard.tell(prepareReadyTransactionMessage(readWrite, shard.underlyingActor(), cohort, transactionID, modification), getRef());
2020 expectMsgClass(duration, ReadyTransactionReply.class);
2022 assertEquals("getPendingTxCommitQueueSize", 1, shard.underlyingActor().getPendingTxCommitQueueSize());
2024 // Send the AbortTransaction message.
2026 shard.tell(new AbortTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
2027 expectMsgClass(duration, AbortTransactionReply.class);
2029 verify(cohort).abort();
2031 // Verify the tx cohort is removed from queue at the cleanup check interval.
2033 cleaupCheckLatch.set(new CountDownLatch(1));
2034 assertEquals("TX_COMMIT_TIMEOUT_CHECK_MESSAGE received", true,
2035 cleaupCheckLatch.get().await(5, TimeUnit.SECONDS));
2037 assertEquals("getPendingTxCommitQueueSize", 0, shard.underlyingActor().getPendingTxCommitQueueSize());
2039 // Now send CanCommitTransaction - should fail.
2041 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
2043 Throwable failure = expectMsgClass(duration, akka.actor.Status.Failure.class).cause();
2044 assertTrue("Failure type", failure instanceof IllegalStateException);
2049 public void testCreateSnapshot() throws Exception {
2050 testCreateSnapshot(true, "testCreateSnapshot");
2054 public void testCreateSnapshotWithNonPersistentData() throws Exception {
2055 testCreateSnapshot(false, "testCreateSnapshotWithNonPersistentData");
2058 @SuppressWarnings("serial")
2059 private void testCreateSnapshot(final boolean persistent, final String shardActorName) throws Exception{
2061 final AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(1));
2063 final AtomicReference<Object> savedSnapshot = new AtomicReference<>();
2064 class TestPersistentDataProvider extends DelegatingPersistentDataProvider {
2065 TestPersistentDataProvider(final DataPersistenceProvider delegate) {
2070 public void saveSnapshot(final Object o) {
2071 savedSnapshot.set(o);
2072 super.saveSnapshot(o);
2076 dataStoreContextBuilder.persistent(persistent);
2078 new ShardTestKit(getSystem()) {{
2079 class TestShard extends Shard {
2081 protected TestShard(AbstractBuilder<?, ?> builder) {
2083 setPersistence(new TestPersistentDataProvider(super.persistence()));
2087 public void handleCommand(final Object message) {
2088 super.handleCommand(message);
2090 // XXX: commit_snapshot equality check references RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT
2091 if (message instanceof SaveSnapshotSuccess || "commit_snapshot".equals(message.toString())) {
2092 latch.get().countDown();
2097 public RaftActorContext getRaftActorContext() {
2098 return super.getRaftActorContext();
2102 final Creator<Shard> creator = () -> new TestShard(newShardBuilder());
2104 final TestActorRef<Shard> shard = actorFactory.createTestActor(
2105 Props.create(new DelegatingShardCreator(creator)), shardActorName);
2107 waitUntilLeader(shard);
2108 writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
2110 final NormalizedNode<?,?> expectedRoot = readStore(shard, YangInstanceIdentifier.builder().build());
2112 // Trigger creation of a snapshot by ensuring
2113 final RaftActorContext raftActorContext = ((TestShard) shard.underlyingActor()).getRaftActorContext();
2114 raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
2115 awaitAndValidateSnapshot(expectedRoot);
2117 raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
2118 awaitAndValidateSnapshot(expectedRoot);
2121 private void awaitAndValidateSnapshot(NormalizedNode<?,?> expectedRoot) throws InterruptedException {
2122 assertEquals("Snapshot saved", true, latch.get().await(5, TimeUnit.SECONDS));
2124 assertTrue("Invalid saved snapshot " + savedSnapshot.get(),
2125 savedSnapshot.get() instanceof Snapshot);
2127 verifySnapshot((Snapshot)savedSnapshot.get(), expectedRoot);
2129 latch.set(new CountDownLatch(1));
2130 savedSnapshot.set(null);
2133 private void verifySnapshot(final Snapshot snapshot, final NormalizedNode<?,?> expectedRoot) {
2135 final NormalizedNode<?, ?> actual = SerializationUtils.deserializeNormalizedNode(snapshot.getState());
2136 assertEquals("Root node", expectedRoot, actual);
2142 * This test simply verifies that the applySnapShot logic will work
2143 * @throws ReadFailedException
2144 * @throws DataValidationFailedException
2147 public void testInMemoryDataTreeRestore() throws ReadFailedException, DataValidationFailedException {
2148 final DataTree store = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
2149 store.setSchemaContext(SCHEMA_CONTEXT);
2151 final DataTreeModification putTransaction = store.takeSnapshot().newModification();
2152 putTransaction.write(TestModel.TEST_PATH,
2153 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
2154 commitTransaction(store, putTransaction);
2157 final NormalizedNode<?, ?> expected = readStore(store, YangInstanceIdentifier.builder().build());
2159 final DataTreeModification writeTransaction = store.takeSnapshot().newModification();
2161 writeTransaction.delete(YangInstanceIdentifier.builder().build());
2162 writeTransaction.write(YangInstanceIdentifier.builder().build(), expected);
2164 commitTransaction(store, writeTransaction);
2166 final NormalizedNode<?, ?> actual = readStore(store, YangInstanceIdentifier.builder().build());
2168 assertEquals(expected, actual);
2172 public void testRecoveryApplicable(){
2174 final DatastoreContext persistentContext = DatastoreContext.newBuilder().
2175 shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(true).build();
2177 final Props persistentProps = Shard.builder().id(shardID).datastoreContext(persistentContext).
2178 schemaContext(SCHEMA_CONTEXT).props();
2180 final DatastoreContext nonPersistentContext = DatastoreContext.newBuilder().
2181 shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(false).build();
2183 final Props nonPersistentProps = Shard.builder().id(shardID).datastoreContext(nonPersistentContext).
2184 schemaContext(SCHEMA_CONTEXT).props();
2186 new ShardTestKit(getSystem()) {{
2187 final TestActorRef<Shard> shard1 = actorFactory.createTestActor(persistentProps, "testPersistence1");
2189 assertTrue("Recovery Applicable", shard1.underlyingActor().persistence().isRecoveryApplicable());
2191 final TestActorRef<Shard> shard2 = actorFactory.createTestActor(nonPersistentProps, "testPersistence2");
2193 assertFalse("Recovery Not Applicable", shard2.underlyingActor().persistence().isRecoveryApplicable());
2198 public void testOnDatastoreContext() {
2199 new ShardTestKit(getSystem()) {{
2200 dataStoreContextBuilder.persistent(true);
2202 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps(), "testOnDatastoreContext");
2204 assertEquals("isRecoveryApplicable", true,
2205 shard.underlyingActor().persistence().isRecoveryApplicable());
2207 waitUntilLeader(shard);
2209 shard.tell(dataStoreContextBuilder.persistent(false).build(), ActorRef.noSender());
2211 assertEquals("isRecoveryApplicable", false,
2212 shard.underlyingActor().persistence().isRecoveryApplicable());
2214 shard.tell(dataStoreContextBuilder.persistent(true).build(), ActorRef.noSender());
2216 assertEquals("isRecoveryApplicable", true,
2217 shard.underlyingActor().persistence().isRecoveryApplicable());
2222 public void testRegisterRoleChangeListener() throws Exception {
2223 new ShardTestKit(getSystem()) {
2225 final TestActorRef<Shard> shard = actorFactory.createTestActor(
2226 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
2227 "testRegisterRoleChangeListener");
2229 waitUntilLeader(shard);
2231 final TestActorRef<MessageCollectorActor> listener =
2232 TestActorRef.create(getSystem(), Props.create(MessageCollectorActor.class));
2234 shard.tell(new RegisterRoleChangeListener(), listener);
2236 MessageCollectorActor.expectFirstMatching(listener, RegisterRoleChangeListenerReply.class);
2238 ShardLeaderStateChanged leaderStateChanged = MessageCollectorActor.expectFirstMatching(listener,
2239 ShardLeaderStateChanged.class);
2240 assertEquals("getLocalShardDataTree present", true,
2241 leaderStateChanged.getLocalShardDataTree().isPresent());
2242 assertSame("getLocalShardDataTree", shard.underlyingActor().getDataStore().getDataTree(),
2243 leaderStateChanged.getLocalShardDataTree().get());
2245 MessageCollectorActor.clearMessages(listener);
2247 // Force a leader change
2249 shard.tell(new RequestVote(10000, "member2", 50, 50), getRef());
2251 leaderStateChanged = MessageCollectorActor.expectFirstMatching(listener,
2252 ShardLeaderStateChanged.class);
2253 assertEquals("getLocalShardDataTree present", false,
2254 leaderStateChanged.getLocalShardDataTree().isPresent());
2260 public void testFollowerInitialSyncStatus() throws Exception {
2261 final TestActorRef<Shard> shard = actorFactory.createTestActor(
2262 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
2263 "testFollowerInitialSyncStatus");
2265 shard.underlyingActor().handleNonRaftCommand(new FollowerInitialSyncUpStatus(false, "member-1-shard-inventory-operational"));
2267 assertEquals(false, shard.underlyingActor().getShardMBean().getFollowerInitialSyncStatus());
2269 shard.underlyingActor().handleNonRaftCommand(new FollowerInitialSyncUpStatus(true, "member-1-shard-inventory-operational"));
2271 assertEquals(true, shard.underlyingActor().getShardMBean().getFollowerInitialSyncStatus());
2275 public void testClusteredDataChangeListenerDelayedRegistration() throws Exception {
2276 new ShardTestKit(getSystem()) {{
2277 String testName = "testClusteredDataChangeListenerDelayedRegistration";
2278 dataStoreContextBuilder.shardElectionTimeoutFactor(1000).
2279 customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
2281 final MockDataChangeListener listener = new MockDataChangeListener(1);
2282 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
2283 actorFactory.generateActorId(testName + "-DataChangeListener"));
2285 setupInMemorySnapshotStore();
2287 final TestActorRef<Shard> shard = actorFactory.createTestActor(
2288 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
2289 actorFactory.generateActorId(testName + "-shard"));
2291 waitUntilNoLeader(shard);
2293 final YangInstanceIdentifier path = TestModel.TEST_PATH;
2295 shard.tell(new RegisterChangeListener(path, dclActor, AsyncDataBroker.DataChangeScope.BASE, true), getRef());
2296 final RegisterChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
2297 RegisterChangeListenerReply.class);
2298 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
2300 shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build()).
2301 customRaftPolicyImplementation(null).build(), ActorRef.noSender());
2303 listener.waitForChangeEvents();
2308 public void testClusteredDataChangeListenerRegistration() throws Exception {
2309 new ShardTestKit(getSystem()) {{
2310 String testName = "testClusteredDataChangeListenerRegistration";
2311 final ShardIdentifier followerShardID = ShardIdentifier.builder()
2312 .memberName(MemberName.forName(actorFactory.generateActorId(testName + "-follower")))
2313 .shardName("inventory").type("config").build();
2315 final ShardIdentifier leaderShardID = ShardIdentifier.builder()
2316 .memberName(MemberName.forName(actorFactory.generateActorId(testName + "-leader")))
2317 .shardName("inventory").type("config").build();
2319 final TestActorRef<Shard> followerShard = actorFactory.createTestActor(
2320 Shard.builder().id(followerShardID).
2321 datastoreContext(dataStoreContextBuilder.shardElectionTimeoutFactor(1000).build()).
2322 peerAddresses(Collections.singletonMap(leaderShardID.toString(),
2323 "akka://test/user/" + leaderShardID.toString())).schemaContext(SCHEMA_CONTEXT).props().
2324 withDispatcher(Dispatchers.DefaultDispatcherId()), followerShardID.toString());
2326 final TestActorRef<Shard> leaderShard = actorFactory.createTestActor(
2327 Shard.builder().id(leaderShardID).datastoreContext(newDatastoreContext()).
2328 peerAddresses(Collections.singletonMap(followerShardID.toString(),
2329 "akka://test/user/" + followerShardID.toString())).schemaContext(SCHEMA_CONTEXT).props().
2330 withDispatcher(Dispatchers.DefaultDispatcherId()), leaderShardID.toString());
2332 leaderShard.tell(ElectionTimeout.INSTANCE, ActorRef.noSender());
2333 String leaderPath = waitUntilLeader(followerShard);
2334 assertEquals("Shard leader path", leaderShard.path().toString(), leaderPath);
2336 final YangInstanceIdentifier path = TestModel.TEST_PATH;
2337 final MockDataChangeListener listener = new MockDataChangeListener(1);
2338 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
2339 actorFactory.generateActorId(testName + "-DataChangeListener"));
2341 followerShard.tell(new RegisterChangeListener(path, dclActor, AsyncDataBroker.DataChangeScope.BASE, true), getRef());
2342 final RegisterChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
2343 RegisterChangeListenerReply.class);
2344 assertNotNull("getListenerRegistratioznPath", reply.getListenerRegistrationPath());
2346 writeToStore(followerShard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
2348 listener.waitForChangeEvents();
2353 public void testClusteredDataTreeChangeListenerDelayedRegistration() throws Exception {
2354 new ShardTestKit(getSystem()) {{
2355 String testName = "testClusteredDataTreeChangeListenerDelayedRegistration";
2356 dataStoreContextBuilder.shardElectionTimeoutFactor(1000).
2357 customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
2359 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
2360 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener),
2361 actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
2363 setupInMemorySnapshotStore();
2365 final TestActorRef<Shard> shard = actorFactory.createTestActor(
2366 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
2367 actorFactory.generateActorId(testName + "-shard"));
2369 waitUntilNoLeader(shard);
2371 final YangInstanceIdentifier path = TestModel.TEST_PATH;
2373 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), getRef());
2374 final RegisterDataTreeChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
2375 RegisterDataTreeChangeListenerReply.class);
2376 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
2378 shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build()).
2379 customRaftPolicyImplementation(null).build(), ActorRef.noSender());
2381 listener.waitForChangeEvents();
2386 public void testClusteredDataTreeChangeListenerRegistration() throws Exception {
2387 new ShardTestKit(getSystem()) {{
2388 String testName = "testClusteredDataTreeChangeListenerRegistration";
2389 final ShardIdentifier followerShardID = ShardIdentifier.builder()
2390 .memberName(MemberName.forName(actorFactory.generateActorId(testName + "-follower")))
2391 .shardName("inventory").type("config").build();
2393 final ShardIdentifier leaderShardID = ShardIdentifier.builder()
2394 .memberName(MemberName.forName(actorFactory.generateActorId(testName + "-leader")))
2395 .shardName("inventory").type("config").build();
2397 final TestActorRef<Shard> followerShard = actorFactory.createTestActor(
2398 Shard.builder().id(followerShardID).
2399 datastoreContext(dataStoreContextBuilder.shardElectionTimeoutFactor(1000).build()).
2400 peerAddresses(Collections.singletonMap(leaderShardID.toString(),
2401 "akka://test/user/" + leaderShardID.toString())).schemaContext(SCHEMA_CONTEXT).props().
2402 withDispatcher(Dispatchers.DefaultDispatcherId()), followerShardID.toString());
2404 final TestActorRef<Shard> leaderShard = actorFactory.createTestActor(
2405 Shard.builder().id(leaderShardID).datastoreContext(newDatastoreContext()).
2406 peerAddresses(Collections.singletonMap(followerShardID.toString(),
2407 "akka://test/user/" + followerShardID.toString())).schemaContext(SCHEMA_CONTEXT).props().
2408 withDispatcher(Dispatchers.DefaultDispatcherId()), leaderShardID.toString());
2410 leaderShard.tell(ElectionTimeout.INSTANCE, ActorRef.noSender());
2411 String leaderPath = waitUntilLeader(followerShard);
2412 assertEquals("Shard leader path", leaderShard.path().toString(), leaderPath);
2414 final YangInstanceIdentifier path = TestModel.TEST_PATH;
2415 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
2416 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener),
2417 actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
2419 followerShard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), getRef());
2420 final RegisterDataTreeChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
2421 RegisterDataTreeChangeListenerReply.class);
2422 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
2424 writeToStore(followerShard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
2426 listener.waitForChangeEvents();
2431 public void testServerRemoved() throws Exception {
2432 final TestActorRef<MessageCollectorActor> parent = actorFactory.createTestActor(MessageCollectorActor.props());
2434 final ActorRef shard = parent.underlyingActor().context().actorOf(
2435 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
2436 "testServerRemoved");
2438 shard.tell(new ServerRemoved("test"), ActorRef.noSender());
2440 MessageCollectorActor.expectFirstMatching(parent, ServerRemoved.class);