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.Matchers.any;
18 import static org.mockito.Mockito.doThrow;
19 import static org.mockito.Mockito.inOrder;
20 import static org.mockito.Mockito.mock;
21 import static org.opendaylight.controller.cluster.datastore.DataStoreVersions.CURRENT_VERSION;
22 import akka.actor.ActorRef;
23 import akka.actor.ActorSelection;
24 import akka.actor.Props;
25 import akka.actor.Status.Failure;
26 import akka.dispatch.Dispatchers;
27 import akka.dispatch.OnComplete;
28 import akka.japi.Creator;
29 import akka.pattern.Patterns;
30 import akka.persistence.SaveSnapshotSuccess;
31 import akka.testkit.TestActorRef;
32 import akka.util.Timeout;
33 import com.google.common.base.Stopwatch;
34 import com.google.common.primitives.UnsignedLong;
35 import com.google.common.util.concurrent.Uninterruptibles;
36 import java.io.IOException;
37 import java.util.Collections;
38 import java.util.HashSet;
41 import java.util.concurrent.CountDownLatch;
42 import java.util.concurrent.TimeUnit;
43 import java.util.concurrent.atomic.AtomicBoolean;
44 import java.util.concurrent.atomic.AtomicReference;
45 import org.junit.Test;
46 import org.mockito.InOrder;
47 import org.opendaylight.controller.cluster.DataPersistenceProvider;
48 import org.opendaylight.controller.cluster.DelegatingPersistentDataProvider;
49 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
50 import org.opendaylight.controller.cluster.access.concepts.MemberName;
51 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
52 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
53 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
54 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
55 import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction;
56 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
57 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
58 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
59 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
60 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
61 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
62 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
63 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
64 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
65 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
66 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
67 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
68 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
69 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
70 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
71 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
72 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
73 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
74 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
75 import org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged;
76 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
77 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
78 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
79 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
80 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
81 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
82 import org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener;
83 import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
84 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
85 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply;
86 import org.opendaylight.controller.cluster.raft.RaftActorContext;
87 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
88 import org.opendaylight.controller.cluster.raft.ReplicatedLogImplEntry;
89 import org.opendaylight.controller.cluster.raft.Snapshot;
90 import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries;
91 import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot;
92 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
93 import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
94 import org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus;
95 import org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow;
96 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
97 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
98 import org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState;
99 import org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState;
100 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
101 import org.opendaylight.controller.cluster.raft.messages.ServerRemoved;
102 import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy;
103 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
104 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
105 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
106 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
107 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
108 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
109 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
110 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
111 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
112 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
113 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
114 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
115 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
116 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
117 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
118 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
119 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
120 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
121 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
122 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
123 import scala.concurrent.Await;
124 import scala.concurrent.Future;
125 import scala.concurrent.duration.FiniteDuration;
127 public class ShardTest extends AbstractShardTest {
128 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";
131 public void testRegisterChangeListener() throws Exception {
132 new ShardTestKit(getSystem()) {{
133 final TestActorRef<Shard> shard = actorFactory.createTestActor(
134 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testRegisterChangeListener");
136 waitUntilLeader(shard);
138 shard.tell(new UpdateSchemaContext(SchemaContextHelper.full()), ActorRef.noSender());
140 final MockDataChangeListener listener = new MockDataChangeListener(1);
141 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
142 "testRegisterChangeListener-DataChangeListener");
144 shard.tell(new RegisterChangeListener(TestModel.TEST_PATH,
145 dclActor, AsyncDataBroker.DataChangeScope.BASE, true), getRef());
147 final RegisterChangeListenerReply reply = expectMsgClass(duration("3 seconds"),
148 RegisterChangeListenerReply.class);
149 final String replyPath = reply.getListenerRegistrationPath().toString();
150 assertTrue("Incorrect reply path: " + replyPath, replyPath.matches(
151 "akka:\\/\\/test\\/user\\/testRegisterChangeListener\\/\\$.*"));
153 final YangInstanceIdentifier path = TestModel.TEST_PATH;
154 writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
156 listener.waitForChangeEvents(path);
160 @SuppressWarnings("serial")
162 public void testChangeListenerNotifiedWhenNotTheLeaderOnRegistration() throws Exception {
163 // This test tests the timing window in which a change listener is registered before the
164 // shard becomes the leader. We verify that the listener is registered and notified of the
165 // existing data when the shard becomes the leader.
166 new ShardTestKit(getSystem()) {{
167 // For this test, we want to send the RegisterChangeListener message after the shard
168 // has recovered from persistence and before it becomes the leader. So we subclass
169 // Shard to override onReceiveCommand and, when the first ElectionTimeout is received,
170 // we know that the shard has been initialized to a follower and has started the
171 // election process. The following 2 CountDownLatches are used to coordinate the
172 // ElectionTimeout with the sending of the RegisterChangeListener message.
173 final CountDownLatch onFirstElectionTimeout = new CountDownLatch(1);
174 final CountDownLatch onChangeListenerRegistered = new CountDownLatch(1);
175 final Creator<Shard> creator = new Creator<Shard>() {
176 boolean firstElectionTimeout = true;
179 public Shard create() throws Exception {
180 // Use a non persistent provider because this test actually invokes persist on the journal
181 // this will cause all other messages to not be queued properly after that.
182 // The basic issue is that you cannot use TestActorRef with a persistent actor (at least when
183 // it does do a persist)
184 return new Shard(newShardBuilder()) {
186 public void handleCommand(final Object message) {
187 if(message instanceof ElectionTimeout && firstElectionTimeout) {
188 // Got the first ElectionTimeout. We don't forward it to the
189 // base Shard yet until we've sent the RegisterChangeListener
190 // message. So we signal the onFirstElectionTimeout latch to tell
191 // the main thread to send the RegisterChangeListener message and
192 // start a thread to wait on the onChangeListenerRegistered latch,
193 // which the main thread signals after it has sent the message.
194 // After the onChangeListenerRegistered is triggered, we send the
195 // original ElectionTimeout message to proceed with the election.
196 firstElectionTimeout = false;
197 final ActorRef self = getSelf();
201 Uninterruptibles.awaitUninterruptibly(
202 onChangeListenerRegistered, 5, TimeUnit.SECONDS);
203 self.tell(message, self);
207 onFirstElectionTimeout.countDown();
209 super.handleCommand(message);
216 setupInMemorySnapshotStore();
218 final MockDataChangeListener listener = new MockDataChangeListener(1);
219 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
220 "testRegisterChangeListenerWhenNotLeaderInitially-DataChangeListener");
222 final TestActorRef<Shard> shard = actorFactory.createTestActor(
223 Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
224 "testRegisterChangeListenerWhenNotLeaderInitially");
226 final YangInstanceIdentifier path = TestModel.TEST_PATH;
228 // Wait until the shard receives the first ElectionTimeout message.
229 assertEquals("Got first ElectionTimeout", true,
230 onFirstElectionTimeout.await(5, TimeUnit.SECONDS));
232 // Now send the RegisterChangeListener and wait for the reply.
233 shard.tell(new RegisterChangeListener(path, dclActor,
234 AsyncDataBroker.DataChangeScope.SUBTREE, false), getRef());
236 final RegisterChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
237 RegisterChangeListenerReply.class);
238 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
240 // Sanity check - verify the shard is not the leader yet.
241 shard.tell(FindLeader.INSTANCE, getRef());
242 final FindLeaderReply findLeadeReply =
243 expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
244 assertFalse("Expected the shard not to be the leader", findLeadeReply.getLeaderActor().isPresent());
246 // Signal the onChangeListenerRegistered latch to tell the thread above to proceed
247 // with the election process.
248 onChangeListenerRegistered.countDown();
250 // Wait for the shard to become the leader and notify our listener with the existing
251 // data in the store.
252 listener.waitForChangeEvents(path);
257 public void testRegisterDataTreeChangeListener() throws Exception {
258 new ShardTestKit(getSystem()) {{
259 final TestActorRef<Shard> shard = actorFactory.createTestActor(
260 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testRegisterDataTreeChangeListener");
262 waitUntilLeader(shard);
264 shard.tell(new UpdateSchemaContext(SchemaContextHelper.full()), ActorRef.noSender());
266 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
267 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener),
268 "testRegisterDataTreeChangeListener-DataTreeChangeListener");
270 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, false), getRef());
272 final RegisterDataTreeChangeListenerReply reply = expectMsgClass(duration("3 seconds"),
273 RegisterDataTreeChangeListenerReply.class);
274 final String replyPath = reply.getListenerRegistrationPath().toString();
275 assertTrue("Incorrect reply path: " + replyPath, replyPath.matches(
276 "akka:\\/\\/test\\/user\\/testRegisterDataTreeChangeListener\\/\\$.*"));
278 final YangInstanceIdentifier path = TestModel.TEST_PATH;
279 writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
281 listener.waitForChangeEvents();
285 @SuppressWarnings("serial")
287 public void testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration() throws Exception {
288 new ShardTestKit(getSystem()) {{
289 final CountDownLatch onFirstElectionTimeout = new CountDownLatch(1);
290 final CountDownLatch onChangeListenerRegistered = new CountDownLatch(1);
291 final Creator<Shard> creator = new Creator<Shard>() {
292 boolean firstElectionTimeout = true;
295 public Shard create() throws Exception {
296 return new Shard(newShardBuilder()) {
298 public void handleCommand(final Object message) {
299 if(message instanceof ElectionTimeout && firstElectionTimeout) {
300 firstElectionTimeout = false;
301 final ActorRef self = getSelf();
305 Uninterruptibles.awaitUninterruptibly(
306 onChangeListenerRegistered, 5, TimeUnit.SECONDS);
307 self.tell(message, self);
311 onFirstElectionTimeout.countDown();
313 super.handleCommand(message);
320 setupInMemorySnapshotStore();
322 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
323 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener),
324 "testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration-DataChangeListener");
326 final TestActorRef<Shard> shard = actorFactory.createTestActor(
327 Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
328 "testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration");
330 final YangInstanceIdentifier path = TestModel.TEST_PATH;
332 assertEquals("Got first ElectionTimeout", true,
333 onFirstElectionTimeout.await(5, TimeUnit.SECONDS));
335 shard.tell(new RegisterDataTreeChangeListener(path, dclActor, false), getRef());
336 final RegisterDataTreeChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
337 RegisterDataTreeChangeListenerReply.class);
338 assertNotNull("getListenerRegistratioznPath", reply.getListenerRegistrationPath());
340 shard.tell(FindLeader.INSTANCE, getRef());
341 final FindLeaderReply findLeadeReply =
342 expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
343 assertFalse("Expected the shard not to be the leader", findLeadeReply.getLeaderActor().isPresent());
346 onChangeListenerRegistered.countDown();
348 // TODO: investigate why we do not receive data chage events
349 listener.waitForChangeEvents();
354 public void testCreateTransaction(){
355 new ShardTestKit(getSystem()) {{
356 final ActorRef shard = actorFactory.createActor(newShardProps(), "testCreateTransaction");
358 waitUntilLeader(shard);
360 shard.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
362 shard.tell(new CreateTransaction(nextTransactionId(), TransactionType.READ_ONLY.ordinal(),
363 DataStoreVersions.CURRENT_VERSION).toSerializable(), getRef());
365 final CreateTransactionReply reply = expectMsgClass(duration("3 seconds"),
366 CreateTransactionReply.class);
368 final String path = reply.getTransactionPath().toString();
369 assertTrue("Unexpected transaction path " + path,
370 path.startsWith("akka://test/user/testCreateTransaction/shard-member-1:ShardTransactionTest@0:"));
375 public void testCreateTransactionOnChain(){
376 new ShardTestKit(getSystem()) {{
377 final ActorRef shard = actorFactory.createActor(newShardProps(), "testCreateTransactionOnChain");
379 waitUntilLeader(shard);
381 shard.tell(new CreateTransaction(nextTransactionId(),TransactionType.READ_ONLY.ordinal(),
382 DataStoreVersions.CURRENT_VERSION).toSerializable(), getRef());
384 final CreateTransactionReply reply = expectMsgClass(duration("3 seconds"),
385 CreateTransactionReply.class);
387 final String path = reply.getTransactionPath().toString();
388 assertTrue("Unexpected transaction path " + path,
389 path.startsWith("akka://test/user/testCreateTransactionOnChain/shard-member-1:ShardTransactionTest@0:"));
394 public void testPeerAddressResolved() throws Exception {
395 new ShardTestKit(getSystem()) {{
396 ShardIdentifier peerID = ShardIdentifier.create("inventory", MemberName.forName("member-2"), "config");
397 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardBuilder().
398 peerAddresses(Collections.<String, String>singletonMap(peerID.toString(), null)).props().
399 withDispatcher(Dispatchers.DefaultDispatcherId()), "testPeerAddressResolved");
401 final String address = "akka://foobar";
402 shard.tell(new PeerAddressResolved(peerID.toString(), address), ActorRef.noSender());
404 shard.tell(GetOnDemandRaftState.INSTANCE, getRef());
405 OnDemandRaftState state = expectMsgClass(OnDemandRaftState.class);
406 assertEquals("getPeerAddress", address, state.getPeerAddresses().get(peerID.toString()));
411 public void testApplySnapshot() throws Exception {
413 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps().
414 withDispatcher(Dispatchers.DefaultDispatcherId()), "testApplySnapshot");
416 ShardTestKit.waitUntilLeader(shard);
418 final DataTree store = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
419 store.setSchemaContext(SCHEMA_CONTEXT);
421 final ContainerNode container = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
422 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
423 withChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).addChild(
424 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1)).build()).build();
426 writeToStore(store, TestModel.TEST_PATH, container);
428 final YangInstanceIdentifier root = YangInstanceIdentifier.EMPTY;
429 final NormalizedNode<?,?> expected = readStore(store, root);
431 final Snapshot snapshot = Snapshot.create(new MetadataShardDataTreeSnapshot(expected).serialize(),
432 Collections.<ReplicatedLogEntry>emptyList(), 1, 2, 3, 4);
434 shard.tell(new ApplySnapshot(snapshot), ActorRef.noSender());
436 Stopwatch sw = Stopwatch.createStarted();
437 while(sw.elapsed(TimeUnit.SECONDS) <= 5) {
438 Uninterruptibles.sleepUninterruptibly(75, TimeUnit.MILLISECONDS);
441 assertEquals("Root node", expected, readStore(shard, root));
443 } catch(AssertionError e) {
448 fail("Snapshot was not applied");
452 public void testApplyState() throws Exception {
453 final TestActorRef<Shard> shard = actorFactory.createTestActor(
454 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testApplyState");
456 ShardTestKit.waitUntilLeader(shard);
458 final DataTree source = setupInMemorySnapshotStore();
459 final DataTreeModification writeMod = source.takeSnapshot().newModification();
460 ContainerNode node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
461 writeMod.write(TestModel.TEST_PATH, node);
464 final TransactionIdentifier tx = nextTransactionId();
465 final ApplyState applyState = new ApplyState(null, tx,
466 new ReplicatedLogImplEntry(1, 2, payloadForModification(source, writeMod, tx)));
468 shard.tell(applyState, shard);
470 Stopwatch sw = Stopwatch.createStarted();
471 while(sw.elapsed(TimeUnit.SECONDS) <= 5) {
472 Uninterruptibles.sleepUninterruptibly(75, TimeUnit.MILLISECONDS);
474 final NormalizedNode<?,?> actual = readStore(shard, TestModel.TEST_PATH);
476 assertEquals("Applied state", node, actual);
481 fail("State was not applied");
485 public void testDataTreeCandidateRecovery() throws Exception {
486 // Set up the InMemorySnapshotStore.
487 final DataTree source = setupInMemorySnapshotStore();
489 final DataTreeModification writeMod = source.takeSnapshot().newModification();
490 writeMod.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
492 InMemoryJournal.addEntry(shardID.toString(), 0, DUMMY_DATA);
494 // Set up the InMemoryJournal.
495 InMemoryJournal.addEntry(shardID.toString(), 1, new ReplicatedLogImplEntry(0, 1,
496 payloadForModification(source, writeMod, nextTransactionId())));
498 final int nListEntries = 16;
499 final Set<Integer> listEntryKeys = new HashSet<>();
501 // Add some ModificationPayload entries
502 for (int i = 1; i <= nListEntries; i++) {
503 listEntryKeys.add(Integer.valueOf(i));
505 final YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
506 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i).build();
508 final DataTreeModification mod = source.takeSnapshot().newModification();
509 mod.merge(path, ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i));
512 InMemoryJournal.addEntry(shardID.toString(), i+1, new ReplicatedLogImplEntry(i, 1,
513 payloadForModification(source, mod, nextTransactionId())));
516 InMemoryJournal.addEntry(shardID.toString(), nListEntries + 2,
517 new ApplyJournalEntries(nListEntries));
519 testRecovery(listEntryKeys);
523 public void testConcurrentThreePhaseCommits() throws Throwable {
524 new ShardTestKit(getSystem()) {{
525 final TestActorRef<Shard> shard = actorFactory.createTestActor(
526 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
527 "testConcurrentThreePhaseCommits");
529 waitUntilLeader(shard);
531 final TransactionIdentifier transactionID1 = nextTransactionId();
532 final TransactionIdentifier transactionID2 = nextTransactionId();
533 final TransactionIdentifier transactionID3 = nextTransactionId();
535 Map<TransactionIdentifier, CapturingShardDataTreeCohort> cohortMap = setupCohortDecorator(
536 shard.underlyingActor(), transactionID1, transactionID2, transactionID3);
537 final CapturingShardDataTreeCohort cohort1 = cohortMap.get(transactionID1);
538 final CapturingShardDataTreeCohort cohort2 = cohortMap.get(transactionID2);
539 final CapturingShardDataTreeCohort cohort3 = cohortMap.get(transactionID3);
541 final long timeoutSec = 5;
542 final FiniteDuration duration = FiniteDuration.create(timeoutSec, TimeUnit.SECONDS);
543 final Timeout timeout = new Timeout(duration);
545 shard.tell(prepareBatchedModifications(transactionID1, TestModel.TEST_PATH,
546 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
547 final ReadyTransactionReply readyReply = ReadyTransactionReply.fromSerializable(
548 expectMsgClass(duration, ReadyTransactionReply.class));
549 assertEquals("Cohort path", shard.path().toString(), readyReply.getCohortPath());
551 // Send the CanCommitTransaction message for the first Tx.
553 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
554 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
555 expectMsgClass(duration, CanCommitTransactionReply.class));
556 assertEquals("Can commit", true, canCommitReply.getCanCommit());
558 // Ready 2 more Tx's.
560 shard.tell(prepareBatchedModifications(transactionID2, TestModel.OUTER_LIST_PATH,
561 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false), getRef());
562 expectMsgClass(duration, ReadyTransactionReply.class);
564 shard.tell(prepareBatchedModifications(transactionID3, YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
565 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
566 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), false), getRef());
567 expectMsgClass(duration, ReadyTransactionReply.class);
569 // Send the CanCommitTransaction message for the next 2 Tx's. These should get queued and
570 // processed after the first Tx completes.
572 final Future<Object> canCommitFuture1 = Patterns.ask(shard,
573 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
575 final Future<Object> canCommitFuture2 = Patterns.ask(shard,
576 new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), timeout);
578 // Send the CommitTransaction message for the first Tx. After it completes, it should
579 // trigger the 2nd Tx to proceed which should in turn then trigger the 3rd.
581 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
582 expectMsgClass(duration, CommitTransactionReply.class);
584 // Wait for the next 2 Tx's to complete.
586 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
587 final CountDownLatch commitLatch = new CountDownLatch(2);
589 class OnFutureComplete extends OnComplete<Object> {
590 private final Class<?> expRespType;
592 OnFutureComplete(final Class<?> expRespType) {
593 this.expRespType = expRespType;
597 public void onComplete(final Throwable error, final Object resp) {
599 caughtEx.set(new AssertionError(getClass().getSimpleName() + " failure", error));
602 assertEquals("Commit response type", expRespType, resp.getClass());
604 } catch (final Exception e) {
610 void onSuccess(final Object resp) throws Exception {
614 class OnCommitFutureComplete extends OnFutureComplete {
615 OnCommitFutureComplete() {
616 super(CommitTransactionReply.class);
620 public void onComplete(final Throwable error, final Object resp) {
621 super.onComplete(error, resp);
622 commitLatch.countDown();
626 class OnCanCommitFutureComplete extends OnFutureComplete {
627 private final TransactionIdentifier transactionID;
629 OnCanCommitFutureComplete(final TransactionIdentifier transactionID) {
630 super(CanCommitTransactionReply.class);
631 this.transactionID = transactionID;
635 void onSuccess(final Object resp) throws Exception {
636 final CanCommitTransactionReply canCommitReply =
637 CanCommitTransactionReply.fromSerializable(resp);
638 assertEquals("Can commit", true, canCommitReply.getCanCommit());
640 final Future<Object> commitFuture = Patterns.ask(shard,
641 new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), timeout);
642 commitFuture.onComplete(new OnCommitFutureComplete(), getSystem().dispatcher());
646 canCommitFuture1.onComplete(new OnCanCommitFutureComplete(transactionID2),
647 getSystem().dispatcher());
649 canCommitFuture2.onComplete(new OnCanCommitFutureComplete(transactionID3),
650 getSystem().dispatcher());
652 final boolean done = commitLatch.await(timeoutSec, TimeUnit.SECONDS);
654 if(caughtEx.get() != null) {
655 throw caughtEx.get();
658 assertEquals("Commits complete", true, done);
660 final InOrder inOrder = inOrder(cohort1.getCanCommit(), cohort1.getPreCommit(), cohort1.getCommit(),
661 cohort2.getCanCommit(), cohort2.getPreCommit(), cohort2.getCommit(), cohort3.getCanCommit(),
662 cohort3.getPreCommit(), cohort3.getCommit());
663 inOrder.verify(cohort1.getCanCommit()).onSuccess(any(Void.class));
664 inOrder.verify(cohort1.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
665 inOrder.verify(cohort1.getCommit()).onSuccess(any(UnsignedLong.class));
666 inOrder.verify(cohort2.getCanCommit()).onSuccess(any(Void.class));
667 inOrder.verify(cohort2.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
668 inOrder.verify(cohort2.getCommit()).onSuccess(any(UnsignedLong.class));
669 inOrder.verify(cohort3.getCanCommit()).onSuccess(any(Void.class));
670 inOrder.verify(cohort3.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
671 inOrder.verify(cohort3.getCommit()).onSuccess(any(UnsignedLong.class));
673 // Verify data in the data store.
675 verifyOuterListEntry(shard, 1);
677 verifyLastApplied(shard, 2);
682 public void testBatchedModificationsWithNoCommitOnReady() throws Throwable {
683 new ShardTestKit(getSystem()) {{
684 final TestActorRef<Shard> shard = actorFactory.createTestActor(
685 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
686 "testBatchedModificationsWithNoCommitOnReady");
688 waitUntilLeader(shard);
690 final TransactionIdentifier transactionID = nextTransactionId();
691 final FiniteDuration duration = duration("5 seconds");
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 CommitTransaction message.
719 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
720 expectMsgClass(duration, CommitTransactionReply.class);
722 // Verify data in the data store.
724 verifyOuterListEntry(shard, 1);
729 public void testBatchedModificationsWithCommitOnReady() throws Throwable {
730 new ShardTestKit(getSystem()) {{
731 final TestActorRef<Shard> shard = actorFactory.createTestActor(
732 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
733 "testBatchedModificationsWithCommitOnReady");
735 waitUntilLeader(shard);
737 final TransactionIdentifier transactionID = nextTransactionId();
738 final FiniteDuration duration = duration("5 seconds");
740 // Send a BatchedModifications to start a transaction.
742 shard.tell(newBatchedModifications(transactionID, TestModel.TEST_PATH,
743 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false, false, 1), getRef());
744 expectMsgClass(duration, BatchedModificationsReply.class);
746 // Send a couple more BatchedModifications.
748 shard.tell(newBatchedModifications(transactionID, TestModel.OUTER_LIST_PATH,
749 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false, false, 2), getRef());
750 expectMsgClass(duration, BatchedModificationsReply.class);
752 shard.tell(newBatchedModifications(transactionID, YangInstanceIdentifier.builder(
753 TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
754 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), true, true, 3), getRef());
756 expectMsgClass(duration, CommitTransactionReply.class);
758 // Verify data in the data store.
760 verifyOuterListEntry(shard, 1);
764 @Test(expected=IllegalStateException.class)
765 public void testBatchedModificationsReadyWithIncorrectTotalMessageCount() throws Throwable {
766 new ShardTestKit(getSystem()) {{
767 final TestActorRef<Shard> shard = actorFactory.createTestActor(
768 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
769 "testBatchedModificationsReadyWithIncorrectTotalMessageCount");
771 waitUntilLeader(shard);
773 final TransactionIdentifier transactionID = nextTransactionId();
774 final BatchedModifications batched = new BatchedModifications(transactionID, DataStoreVersions.CURRENT_VERSION);
775 batched.setReady(true);
776 batched.setTotalMessagesSent(2);
778 shard.tell(batched, getRef());
780 final Failure failure = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
782 if(failure != null) {
783 throw failure.cause();
789 public void testBatchedModificationsWithOperationFailure() throws Throwable {
790 new ShardTestKit(getSystem()) {{
791 final TestActorRef<Shard> shard = actorFactory.createTestActor(
792 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
793 "testBatchedModificationsWithOperationFailure");
795 waitUntilLeader(shard);
797 // Test merge with invalid data. An exception should occur when the merge is applied. Note that
798 // write will not validate the children for performance reasons.
800 TransactionIdentifier transactionID = nextTransactionId();
802 ContainerNode invalidData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
803 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME)).
804 withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
806 BatchedModifications batched = new BatchedModifications(transactionID, CURRENT_VERSION);
807 batched.addModification(new MergeModification(TestModel.TEST_PATH, invalidData));
808 shard.tell(batched, getRef());
809 Failure failure = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
811 Throwable cause = failure.cause();
813 batched = new BatchedModifications(transactionID, DataStoreVersions.CURRENT_VERSION);
814 batched.setReady(true);
815 batched.setTotalMessagesSent(2);
817 shard.tell(batched, getRef());
819 failure = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
820 assertEquals("Failure cause", cause, failure.cause());
825 public void testBatchedModificationsOnTransactionChain() throws Throwable {
826 new ShardTestKit(getSystem()) {{
827 final TestActorRef<Shard> shard = actorFactory.createTestActor(
828 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
829 "testBatchedModificationsOnTransactionChain");
831 waitUntilLeader(shard);
833 final LocalHistoryIdentifier historyId = nextHistoryId();
834 final TransactionIdentifier transactionID1 = new TransactionIdentifier(historyId, 0);
835 final TransactionIdentifier transactionID2 = new TransactionIdentifier(historyId, 1);
837 final FiniteDuration duration = duration("5 seconds");
839 // Send a BatchedModifications to start a chained write transaction and ready it.
841 final ContainerNode containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
842 final YangInstanceIdentifier path = TestModel.TEST_PATH;
843 shard.tell(newBatchedModifications(transactionID1, path, containerNode, true, false, 1), getRef());
844 expectMsgClass(duration, ReadyTransactionReply.class);
846 // Create a read Tx on the same chain.
848 shard.tell(new CreateTransaction(transactionID2, TransactionType.READ_ONLY.ordinal(),
849 DataStoreVersions.CURRENT_VERSION).toSerializable(), getRef());
851 final CreateTransactionReply createReply = expectMsgClass(duration("3 seconds"), CreateTransactionReply.class);
853 getSystem().actorSelection(createReply.getTransactionPath()).tell(
854 new ReadData(path, DataStoreVersions.CURRENT_VERSION), getRef());
855 final ReadDataReply readReply = expectMsgClass(duration("3 seconds"), ReadDataReply.class);
856 assertEquals("Read node", containerNode, readReply.getNormalizedNode());
858 // Commit the write transaction.
860 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
861 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
862 expectMsgClass(duration, CanCommitTransactionReply.class));
863 assertEquals("Can commit", true, canCommitReply.getCanCommit());
865 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
866 expectMsgClass(duration, CommitTransactionReply.class);
868 // Verify data in the data store.
870 final NormalizedNode<?, ?> actualNode = readStore(shard, path);
871 assertEquals("Stored node", containerNode, actualNode);
876 public void testOnBatchedModificationsWhenNotLeader() {
877 final AtomicBoolean overrideLeaderCalls = new AtomicBoolean();
878 new ShardTestKit(getSystem()) {{
879 final Creator<Shard> creator = new Creator<Shard>() {
880 private static final long serialVersionUID = 1L;
883 public Shard create() throws Exception {
884 return new Shard(newShardBuilder()) {
886 protected boolean isLeader() {
887 return overrideLeaderCalls.get() ? false : super.isLeader();
891 protected ActorSelection getLeader() {
892 return overrideLeaderCalls.get() ? getSystem().actorSelection(getRef().path()) :
899 final TestActorRef<Shard> shard = actorFactory.createTestActor(
900 Props.create(new DelegatingShardCreator(creator)).
901 withDispatcher(Dispatchers.DefaultDispatcherId()), "testOnBatchedModificationsWhenNotLeader");
903 waitUntilLeader(shard);
905 overrideLeaderCalls.set(true);
907 final BatchedModifications batched = new BatchedModifications(nextTransactionId(), DataStoreVersions.CURRENT_VERSION);
909 shard.tell(batched, ActorRef.noSender());
911 expectMsgEquals(batched);
916 public void testTransactionMessagesWithNoLeader() {
917 new ShardTestKit(getSystem()) {{
918 dataStoreContextBuilder.customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName()).
919 shardHeartbeatIntervalInMillis(50).shardElectionTimeoutFactor(1);
920 final TestActorRef<Shard> shard = actorFactory.createTestActor(
921 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
922 "testTransactionMessagesWithNoLeader");
924 waitUntilNoLeader(shard);
926 final TransactionIdentifier txId = nextTransactionId();
927 shard.tell(new BatchedModifications(txId, DataStoreVersions.CURRENT_VERSION), getRef());
928 Failure failure = expectMsgClass(Failure.class);
929 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
931 shard.tell(prepareForwardedReadyTransaction(shard, txId, TestModel.TEST_PATH,
932 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), getRef());
933 failure = expectMsgClass(Failure.class);
934 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
936 shard.tell(new ReadyLocalTransaction(txId, mock(DataTreeModification.class), true), getRef());
937 failure = expectMsgClass(Failure.class);
938 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
943 public void testReadyWithReadWriteImmediateCommit() throws Exception{
944 testReadyWithImmediateCommit(true);
948 public void testReadyWithWriteOnlyImmediateCommit() throws Exception{
949 testReadyWithImmediateCommit(false);
952 private void testReadyWithImmediateCommit(final boolean readWrite) throws Exception{
953 new ShardTestKit(getSystem()) {{
954 final TestActorRef<Shard> shard = actorFactory.createTestActor(
955 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
956 "testReadyWithImmediateCommit-" + readWrite);
958 waitUntilLeader(shard);
960 final TransactionIdentifier transactionID = nextTransactionId();
961 final NormalizedNode<?, ?> containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
963 shard.tell(prepareForwardedReadyTransaction(shard, transactionID, TestModel.TEST_PATH,
964 containerNode, true), getRef());
966 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH, containerNode, true), getRef());
969 expectMsgClass(duration("5 seconds"), CommitTransactionReply.class);
971 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.TEST_PATH);
972 assertEquals(TestModel.TEST_QNAME.getLocalName(), containerNode, actualNode);
977 public void testReadyLocalTransactionWithImmediateCommit() throws Exception{
978 new ShardTestKit(getSystem()) {{
979 final TestActorRef<Shard> shard = actorFactory.createTestActor(
980 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
981 "testReadyLocalTransactionWithImmediateCommit");
983 waitUntilLeader(shard);
985 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
987 final DataTreeModification modification = dataStore.newModification();
989 final ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
990 new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
991 final MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
992 new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
994 final TransactionIdentifier txId = nextTransactionId();
995 modification.ready();
996 final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(txId, modification, true);
998 shard.tell(readyMessage, getRef());
1000 expectMsgClass(CommitTransactionReply.class);
1002 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.OUTER_LIST_PATH);
1003 assertEquals(TestModel.OUTER_LIST_QNAME.getLocalName(), mergeData, actualNode);
1008 public void testReadyLocalTransactionWithThreePhaseCommit() throws Exception{
1009 new ShardTestKit(getSystem()) {{
1010 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1011 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1012 "testReadyLocalTransactionWithThreePhaseCommit");
1014 waitUntilLeader(shard);
1016 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1018 final DataTreeModification modification = dataStore.newModification();
1020 final ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1021 new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
1022 final MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
1023 new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
1025 final TransactionIdentifier txId = nextTransactionId();
1026 modification.ready();
1027 final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(txId, modification, false);
1029 shard.tell(readyMessage, getRef());
1031 expectMsgClass(ReadyTransactionReply.class);
1033 // Send the CanCommitTransaction message.
1035 shard.tell(new CanCommitTransaction(txId, CURRENT_VERSION).toSerializable(), getRef());
1036 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1037 expectMsgClass(CanCommitTransactionReply.class));
1038 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1040 // Send the CanCommitTransaction message.
1042 shard.tell(new CommitTransaction(txId, CURRENT_VERSION).toSerializable(), getRef());
1043 expectMsgClass(CommitTransactionReply.class);
1045 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.OUTER_LIST_PATH);
1046 assertEquals(TestModel.OUTER_LIST_QNAME.getLocalName(), mergeData, actualNode);
1051 public void testReadWriteCommitWithPersistenceDisabled() throws Throwable {
1052 dataStoreContextBuilder.persistent(false);
1053 new ShardTestKit(getSystem()) {{
1054 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1055 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1056 "testCommitWithPersistenceDisabled");
1058 waitUntilLeader(shard);
1060 // Setup a simulated transactions with a mock cohort.
1062 final FiniteDuration duration = duration("5 seconds");
1064 final TransactionIdentifier transactionID = nextTransactionId();
1065 final NormalizedNode<?, ?> containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1066 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH, containerNode, false), getRef());
1067 expectMsgClass(duration, ReadyTransactionReply.class);
1069 // Send the CanCommitTransaction message.
1071 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1072 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1073 expectMsgClass(duration, CanCommitTransactionReply.class));
1074 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1076 // Send the CanCommitTransaction message.
1078 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1079 expectMsgClass(duration, CommitTransactionReply.class);
1081 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.TEST_PATH);
1082 assertEquals(TestModel.TEST_QNAME.getLocalName(), containerNode, actualNode);
1087 public void testReadWriteCommitWhenTransactionHasNoModifications() {
1088 testCommitWhenTransactionHasNoModifications(true);
1092 public void testWriteOnlyCommitWhenTransactionHasNoModifications() {
1093 testCommitWhenTransactionHasNoModifications(false);
1096 private void testCommitWhenTransactionHasNoModifications(final boolean readWrite){
1097 // Note that persistence is enabled which would normally result in the entry getting written to the journal
1098 // but here that need not happen
1099 new ShardTestKit(getSystem()) {{
1100 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1101 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1102 "testCommitWhenTransactionHasNoModifications-" + readWrite);
1104 waitUntilLeader(shard);
1106 final TransactionIdentifier transactionID = nextTransactionId();
1108 final FiniteDuration duration = duration("5 seconds");
1111 ReadWriteShardDataTreeTransaction rwTx = shard.underlyingActor().getDataStore().
1112 newReadWriteTransaction(transactionID);
1113 shard.tell(new ForwardedReadyTransaction(transactionID, CURRENT_VERSION, rwTx, false), getRef());
1115 shard.tell(prepareBatchedModifications(transactionID, new MutableCompositeModification()), getRef());
1118 expectMsgClass(duration, ReadyTransactionReply.class);
1120 // Send the CanCommitTransaction message.
1122 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1123 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1124 expectMsgClass(duration, CanCommitTransactionReply.class));
1125 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1127 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1128 expectMsgClass(duration, CommitTransactionReply.class);
1130 shard.tell(Shard.GET_SHARD_MBEAN_MESSAGE, getRef());
1131 final ShardStats shardStats = expectMsgClass(duration, ShardStats.class);
1133 // Use MBean for verification
1134 // Committed transaction count should increase as usual
1135 assertEquals(1,shardStats.getCommittedTransactionsCount());
1137 // Commit index should not advance because this does not go into the journal
1138 assertEquals(-1, shardStats.getCommitIndex());
1143 public void testReadWriteCommitWhenTransactionHasModifications() throws Exception {
1144 testCommitWhenTransactionHasModifications(true);
1148 public void testWriteOnlyCommitWhenTransactionHasModifications() throws Exception {
1149 testCommitWhenTransactionHasModifications(false);
1152 private void testCommitWhenTransactionHasModifications(final boolean readWrite) throws Exception {
1153 new ShardTestKit(getSystem()) {{
1154 final TipProducingDataTree dataTree = createDelegatingMockDataTree();
1155 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1156 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1157 "testCommitWhenTransactionHasModifications-" + readWrite);
1159 waitUntilLeader(shard);
1161 final FiniteDuration duration = duration("5 seconds");
1162 final TransactionIdentifier transactionID = nextTransactionId();
1165 shard.tell(prepareForwardedReadyTransaction(shard, transactionID, TestModel.TEST_PATH,
1166 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
1168 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH,
1169 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
1172 expectMsgClass(duration, ReadyTransactionReply.class);
1174 // Send the CanCommitTransaction message.
1176 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1177 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1178 expectMsgClass(duration, CanCommitTransactionReply.class));
1179 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1181 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1182 expectMsgClass(duration, CommitTransactionReply.class);
1184 final InOrder inOrder = inOrder(dataTree);
1185 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1186 inOrder.verify(dataTree).prepare(any(DataTreeModification.class));
1187 inOrder.verify(dataTree).commit(any(DataTreeCandidate.class));
1189 shard.tell(Shard.GET_SHARD_MBEAN_MESSAGE, getRef());
1190 final ShardStats shardStats = expectMsgClass(duration, ShardStats.class);
1192 // Use MBean for verification
1193 // Committed transaction count should increase as usual
1194 assertEquals(1, shardStats.getCommittedTransactionsCount());
1196 // Commit index should advance as we do not have an empty modification
1197 assertEquals(0, shardStats.getCommitIndex());
1202 public void testCommitPhaseFailure() throws Throwable {
1203 new ShardTestKit(getSystem()) {{
1204 final TipProducingDataTree dataTree = createDelegatingMockDataTree();
1205 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1206 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1207 "testCommitPhaseFailure");
1209 waitUntilLeader(shard);
1211 final FiniteDuration duration = duration("5 seconds");
1212 final Timeout timeout = new Timeout(duration);
1214 // Setup 2 simulated transactions with mock cohorts. The first one fails in the
1217 doThrow(new RuntimeException("mock commit failure")).when(dataTree).commit(any(DataTreeCandidate.class));
1219 final TransactionIdentifier transactionID1 = nextTransactionId();
1220 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1221 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1222 expectMsgClass(duration, ReadyTransactionReply.class);
1224 final TransactionIdentifier transactionID2 = nextTransactionId();
1225 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1226 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1227 expectMsgClass(duration, ReadyTransactionReply.class);
1229 // Send the CanCommitTransaction message for the first Tx.
1231 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1232 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1233 expectMsgClass(duration, CanCommitTransactionReply.class));
1234 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1236 // Send the CanCommitTransaction message for the 2nd Tx. This should get queued and
1237 // processed after the first Tx completes.
1239 final Future<Object> canCommitFuture = Patterns.ask(shard,
1240 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1242 // Send the CommitTransaction message for the first Tx. This should send back an error
1243 // and trigger the 2nd Tx to proceed.
1245 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1246 expectMsgClass(duration, akka.actor.Status.Failure.class);
1248 // Wait for the 2nd Tx to complete the canCommit phase.
1250 final CountDownLatch latch = new CountDownLatch(1);
1251 canCommitFuture.onComplete(new OnComplete<Object>() {
1253 public void onComplete(final Throwable t, final Object resp) {
1256 }, getSystem().dispatcher());
1258 assertEquals("2nd CanCommit complete", true, latch.await(5, TimeUnit.SECONDS));
1260 final InOrder inOrder = inOrder(dataTree);
1261 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1262 inOrder.verify(dataTree).prepare(any(DataTreeModification.class));
1263 inOrder.verify(dataTree).commit(any(DataTreeCandidate.class));
1264 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1269 public void testPreCommitPhaseFailure() throws Throwable {
1270 new ShardTestKit(getSystem()) {{
1271 final TipProducingDataTree dataTree = createDelegatingMockDataTree();
1272 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1273 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1274 "testPreCommitPhaseFailure");
1276 waitUntilLeader(shard);
1278 final FiniteDuration duration = duration("5 seconds");
1279 final Timeout timeout = new Timeout(duration);
1281 doThrow(new RuntimeException("mock preCommit failure")).when(dataTree).prepare(any(DataTreeModification.class));
1283 final TransactionIdentifier transactionID1 = nextTransactionId();
1284 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1285 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1286 expectMsgClass(duration, ReadyTransactionReply.class);
1288 final TransactionIdentifier transactionID2 = nextTransactionId();
1289 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1290 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1291 expectMsgClass(duration, ReadyTransactionReply.class);
1293 // Send the CanCommitTransaction message for the first Tx.
1295 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1296 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1297 expectMsgClass(duration, CanCommitTransactionReply.class));
1298 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1300 // Send the CanCommitTransaction message for the 2nd Tx. This should get queued and
1301 // processed after the first Tx completes.
1303 final Future<Object> canCommitFuture = Patterns.ask(shard,
1304 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1306 // Send the CommitTransaction message for the first Tx. This should send back an error
1307 // and trigger the 2nd Tx to proceed.
1309 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1310 expectMsgClass(duration, akka.actor.Status.Failure.class);
1312 // Wait for the 2nd Tx to complete the canCommit phase.
1314 final CountDownLatch latch = new CountDownLatch(1);
1315 canCommitFuture.onComplete(new OnComplete<Object>() {
1317 public void onComplete(final Throwable t, final Object resp) {
1320 }, getSystem().dispatcher());
1322 assertEquals("2nd CanCommit complete", true, latch.await(5, TimeUnit.SECONDS));
1324 final InOrder inOrder = inOrder(dataTree);
1325 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1326 inOrder.verify(dataTree).prepare(any(DataTreeModification.class));
1327 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1332 public void testCanCommitPhaseFailure() throws Throwable {
1333 new ShardTestKit(getSystem()) {{
1334 final TipProducingDataTree dataTree = createDelegatingMockDataTree();
1335 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1336 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1337 "testCanCommitPhaseFailure");
1339 waitUntilLeader(shard);
1341 final FiniteDuration duration = duration("5 seconds");
1342 final TransactionIdentifier transactionID1 = nextTransactionId();
1344 doThrow(new DataValidationFailedException(YangInstanceIdentifier.EMPTY, "mock canCommit failure")).
1345 doNothing().when(dataTree).validate(any(DataTreeModification.class));
1347 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1348 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1349 expectMsgClass(duration, ReadyTransactionReply.class);
1351 // Send the CanCommitTransaction message.
1353 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1354 expectMsgClass(duration, akka.actor.Status.Failure.class);
1356 // Send another can commit to ensure the failed one got cleaned up.
1358 final TransactionIdentifier transactionID2 = nextTransactionId();
1359 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1360 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1361 expectMsgClass(duration, ReadyTransactionReply.class);
1363 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1364 final CanCommitTransactionReply reply = CanCommitTransactionReply.fromSerializable(
1365 expectMsgClass(CanCommitTransactionReply.class));
1366 assertEquals("getCanCommit", true, reply.getCanCommit());
1371 public void testImmediateCommitWithCanCommitPhaseFailure() throws Throwable {
1372 testImmediateCommitWithCanCommitPhaseFailure(true);
1373 testImmediateCommitWithCanCommitPhaseFailure(false);
1376 private void testImmediateCommitWithCanCommitPhaseFailure(final boolean readWrite) throws Throwable {
1377 new ShardTestKit(getSystem()) {{
1378 final TipProducingDataTree dataTree = createDelegatingMockDataTree();
1379 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1380 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1381 "testImmediateCommitWithCanCommitPhaseFailure-" + readWrite);
1383 waitUntilLeader(shard);
1385 doThrow(new DataValidationFailedException(YangInstanceIdentifier.EMPTY, "mock canCommit failure")).
1386 doNothing().when(dataTree).validate(any(DataTreeModification.class));
1388 final FiniteDuration duration = duration("5 seconds");
1390 final TransactionIdentifier transactionID1 = nextTransactionId();
1393 shard.tell(prepareForwardedReadyTransaction(shard, transactionID1, TestModel.TEST_PATH,
1394 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), getRef());
1396 shard.tell(prepareBatchedModifications(transactionID1, TestModel.TEST_PATH,
1397 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), getRef());
1400 expectMsgClass(duration, akka.actor.Status.Failure.class);
1402 // Send another can commit to ensure the failed one got cleaned up.
1404 final TransactionIdentifier transactionID2 = nextTransactionId();
1406 shard.tell(prepareForwardedReadyTransaction(shard, transactionID2, TestModel.TEST_PATH,
1407 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), getRef());
1409 shard.tell(prepareBatchedModifications(transactionID2, TestModel.TEST_PATH,
1410 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), getRef());
1413 expectMsgClass(duration, CommitTransactionReply.class);
1417 @SuppressWarnings("serial")
1419 public void testAbortWithCommitPending() throws Throwable {
1420 new ShardTestKit(getSystem()) {{
1421 final Creator<Shard> creator = new Creator<Shard>() {
1423 public Shard create() throws Exception {
1424 return new Shard(newShardBuilder()) {
1426 void persistPayload(final TransactionIdentifier transactionId, final Payload payload) {
1427 // Simulate an AbortTransaction message occurring during replication, after
1428 // persisting and before finishing the commit to the in-memory store.
1430 doAbortTransaction(transactionId, null);
1431 super.persistPayload(transactionId, payload);
1437 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1438 Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
1439 "testAbortWithCommitPending");
1441 waitUntilLeader(shard);
1443 final FiniteDuration duration = duration("5 seconds");
1445 final TransactionIdentifier transactionID = nextTransactionId();
1447 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH,
1448 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
1449 expectMsgClass(duration, ReadyTransactionReply.class);
1451 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1452 expectMsgClass(duration, CanCommitTransactionReply.class);
1454 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1455 expectMsgClass(duration, CommitTransactionReply.class);
1457 final NormalizedNode<?, ?> node = readStore(shard, TestModel.TEST_PATH);
1459 // Since we're simulating an abort occurring during replication and before finish commit,
1460 // the data should still get written to the in-memory store since we've gotten past
1461 // canCommit and preCommit and persisted the data.
1462 assertNotNull(TestModel.TEST_QNAME.getLocalName() + " not found", node);
1467 public void testTransactionCommitTimeout() throws Throwable {
1468 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1469 new ShardTestKit(getSystem()) {{
1470 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1471 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1472 "testTransactionCommitTimeout");
1474 waitUntilLeader(shard);
1476 final FiniteDuration duration = duration("5 seconds");
1478 writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1479 writeToStore(shard, TestModel.OUTER_LIST_PATH,
1480 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1482 // Ready 2 Tx's - the first will timeout
1484 final TransactionIdentifier transactionID1 = nextTransactionId();
1485 shard.tell(prepareBatchedModifications(transactionID1, YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1486 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
1487 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), false), getRef());
1488 expectMsgClass(duration, ReadyTransactionReply.class);
1490 final TransactionIdentifier transactionID2 = nextTransactionId();
1491 final YangInstanceIdentifier listNodePath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1492 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build();
1493 shard.tell(prepareBatchedModifications(transactionID2, listNodePath,
1494 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2), false), getRef());
1495 expectMsgClass(duration, ReadyTransactionReply.class);
1497 // canCommit 1st Tx. We don't send the commit so it should timeout.
1499 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1500 expectMsgClass(duration, CanCommitTransactionReply.class);
1502 // canCommit the 2nd Tx - it should complete after the 1st Tx times out.
1504 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1505 expectMsgClass(duration, CanCommitTransactionReply.class);
1507 // Try to commit the 1st Tx - should fail as it's not the current Tx.
1509 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1510 expectMsgClass(duration, akka.actor.Status.Failure.class);
1512 // Commit the 2nd Tx.
1514 shard.tell(new CommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1515 expectMsgClass(duration, CommitTransactionReply.class);
1517 final NormalizedNode<?, ?> node = readStore(shard, listNodePath);
1518 assertNotNull(listNodePath + " not found", node);
1524 // public void testTransactionCommitQueueCapacityExceeded() throws Throwable {
1525 // dataStoreContextBuilder.shardTransactionCommitQueueCapacity(2);
1527 // new ShardTestKit(getSystem()) {{
1528 // final TestActorRef<Shard> shard = actorFactory.createTestActor(
1529 // newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1530 // "testTransactionCommitQueueCapacityExceeded");
1532 // waitUntilLeader(shard);
1534 // final FiniteDuration duration = duration("5 seconds");
1536 // final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1538 // final TransactionIdentifier transactionID1 = nextTransactionId();
1539 // final MutableCompositeModification modification1 = new MutableCompositeModification();
1540 // final ShardDataTreeCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
1541 // TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), transactionID1,
1544 // final TransactionIdentifier transactionID2 = nextTransactionId();
1545 // final MutableCompositeModification modification2 = new MutableCompositeModification();
1546 // final ShardDataTreeCohort cohort2 = setupMockWriteTransaction("cohort2", dataStore,
1547 // TestModel.OUTER_LIST_PATH,
1548 // ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), transactionID2,
1551 // final TransactionIdentifier transactionID3 = nextTransactionId();
1552 // final MutableCompositeModification modification3 = new MutableCompositeModification();
1553 // final ShardDataTreeCohort cohort3 = setupMockWriteTransaction("cohort3", dataStore,
1554 // TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), transactionID3,
1557 // // Ready the Tx's
1559 // shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort1, transactionID1, modification1), getRef());
1560 // expectMsgClass(duration, ReadyTransactionReply.class);
1562 // shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort2, transactionID2, modification2), getRef());
1563 // expectMsgClass(duration, ReadyTransactionReply.class);
1565 // // The 3rd Tx should exceed queue capacity and fail.
1567 // shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort3, transactionID3, modification3), getRef());
1568 // expectMsgClass(duration, akka.actor.Status.Failure.class);
1570 // // canCommit 1st Tx.
1572 // shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1573 // expectMsgClass(duration, CanCommitTransactionReply.class);
1575 // // canCommit the 2nd Tx - it should get queued.
1577 // shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1579 // // canCommit the 3rd Tx - should exceed queue capacity and fail.
1581 // shard.tell(new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), getRef());
1582 // expectMsgClass(duration, akka.actor.Status.Failure.class);
1587 public void testTransactionCommitWithPriorExpiredCohortEntries() throws Throwable {
1588 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1589 new ShardTestKit(getSystem()) {{
1590 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1591 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1592 "testTransactionCommitWithPriorExpiredCohortEntries");
1594 waitUntilLeader(shard);
1596 final FiniteDuration duration = duration("5 seconds");
1598 final TransactionIdentifier transactionID1 = nextTransactionId();
1599 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1600 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1601 expectMsgClass(duration, ReadyTransactionReply.class);
1603 final TransactionIdentifier transactionID2 = nextTransactionId();
1604 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1605 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1606 expectMsgClass(duration, ReadyTransactionReply.class);
1608 final TransactionIdentifier transactionID3 = nextTransactionId();
1609 shard.tell(newBatchedModifications(transactionID3, TestModel.TEST_PATH,
1610 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1611 expectMsgClass(duration, ReadyTransactionReply.class);
1613 // All Tx's are readied. We'll send canCommit for the last one but not the others. The others
1614 // should expire from the queue and the last one should be processed.
1616 shard.tell(new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), getRef());
1617 expectMsgClass(duration, CanCommitTransactionReply.class);
1622 public void testTransactionCommitWithSubsequentExpiredCohortEntry() throws Throwable {
1623 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1624 new ShardTestKit(getSystem()) {{
1625 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1626 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1627 "testTransactionCommitWithSubsequentExpiredCohortEntry");
1629 waitUntilLeader(shard);
1631 final FiniteDuration duration = duration("5 seconds");
1633 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1635 final TransactionIdentifier transactionID1 = nextTransactionId();
1636 shard.tell(prepareBatchedModifications(transactionID1, TestModel.TEST_PATH,
1637 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
1638 expectMsgClass(duration, ReadyTransactionReply.class);
1640 // CanCommit the first Tx so it's the current in-progress Tx.
1642 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1643 expectMsgClass(duration, CanCommitTransactionReply.class);
1645 // Ready the second Tx.
1647 final TransactionIdentifier transactionID2 = nextTransactionId();
1648 shard.tell(prepareBatchedModifications(transactionID2, TestModel.TEST_PATH,
1649 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
1650 expectMsgClass(duration, ReadyTransactionReply.class);
1652 // Ready the third Tx.
1654 final TransactionIdentifier transactionID3 = nextTransactionId();
1655 final DataTreeModification modification3 = dataStore.newModification();
1656 new WriteModification(TestModel.TEST2_PATH, ImmutableNodes.containerNode(TestModel.TEST2_QNAME))
1657 .apply(modification3);
1658 modification3.ready();
1659 final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(transactionID3, modification3, true);
1660 shard.tell(readyMessage, getRef());
1662 // Commit the first Tx. After completing, the second should expire from the queue and the third
1665 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1666 expectMsgClass(duration, CommitTransactionReply.class);
1668 // Expect commit reply from the third Tx.
1670 expectMsgClass(duration, CommitTransactionReply.class);
1672 final NormalizedNode<?, ?> node = readStore(shard, TestModel.TEST2_PATH);
1673 assertNotNull(TestModel.TEST2_PATH + " not found", node);
1678 public void testCanCommitBeforeReadyFailure() throws Throwable {
1679 new ShardTestKit(getSystem()) {{
1680 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1681 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1682 "testCanCommitBeforeReadyFailure");
1684 shard.tell(new CanCommitTransaction(nextTransactionId(), CURRENT_VERSION).toSerializable(), getRef());
1685 expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
1690 public void testAbortAfterCanCommit() throws Throwable {
1691 new ShardTestKit(getSystem()) {{
1692 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1693 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1694 "testAbortAfterCanCommit");
1696 waitUntilLeader(shard);
1698 final FiniteDuration duration = duration("5 seconds");
1699 final Timeout timeout = new Timeout(duration);
1701 // Ready 2 transactions - the first one will be aborted.
1703 final TransactionIdentifier transactionID1 = nextTransactionId();
1704 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1705 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1706 expectMsgClass(duration, ReadyTransactionReply.class);
1708 final TransactionIdentifier transactionID2 = nextTransactionId();
1709 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1710 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1711 expectMsgClass(duration, ReadyTransactionReply.class);
1713 // Send the CanCommitTransaction message for the first Tx.
1715 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1716 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
1717 expectMsgClass(duration, CanCommitTransactionReply.class));
1718 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1720 // Send the CanCommitTransaction message for the 2nd Tx. This should get queued and
1721 // processed after the first Tx completes.
1723 final Future<Object> canCommitFuture = Patterns.ask(shard,
1724 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1726 // Send the AbortTransaction message for the first Tx. This should trigger the 2nd
1729 shard.tell(new AbortTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1730 expectMsgClass(duration, AbortTransactionReply.class);
1732 // Wait for the 2nd Tx to complete the canCommit phase.
1734 canCommitReply = (CanCommitTransactionReply) Await.result(canCommitFuture, duration);
1735 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1740 public void testAbortAfterReady() throws Throwable {
1741 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1742 new ShardTestKit(getSystem()) {{
1743 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1744 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testAbortAfterReady");
1746 waitUntilLeader(shard);
1748 final FiniteDuration duration = duration("5 seconds");
1752 final TransactionIdentifier transactionID1 = nextTransactionId();
1753 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1754 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1755 expectMsgClass(duration, ReadyTransactionReply.class);
1757 // Send the AbortTransaction message.
1759 shard.tell(new AbortTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1760 expectMsgClass(duration, AbortTransactionReply.class);
1762 assertEquals("getPendingTxCommitQueueSize", 0, shard.underlyingActor().getPendingTxCommitQueueSize());
1764 // Now send CanCommitTransaction - should fail.
1766 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1767 Throwable failure = expectMsgClass(duration, akka.actor.Status.Failure.class).cause();
1768 assertTrue("Failure type", failure instanceof IllegalStateException);
1770 // Ready and CanCommit another and verify success.
1772 final TransactionIdentifier transactionID2 = nextTransactionId();
1773 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1774 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1775 expectMsgClass(duration, ReadyTransactionReply.class);
1777 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1778 expectMsgClass(duration, CanCommitTransactionReply.class);
1783 public void testAbortQueuedTransaction() throws Throwable {
1784 new ShardTestKit(getSystem()) {{
1785 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1786 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testAbortAfterReady");
1788 waitUntilLeader(shard);
1790 final FiniteDuration duration = duration("5 seconds");
1794 final TransactionIdentifier transactionID1 = nextTransactionId();
1795 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1796 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1797 expectMsgClass(duration, ReadyTransactionReply.class);
1799 final TransactionIdentifier transactionID2 = nextTransactionId();
1800 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1801 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1802 expectMsgClass(duration, ReadyTransactionReply.class);
1804 final TransactionIdentifier transactionID3 = nextTransactionId();
1805 shard.tell(newBatchedModifications(transactionID3, TestModel.OUTER_LIST_PATH,
1806 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), true, false, 1), getRef());
1807 expectMsgClass(duration, ReadyTransactionReply.class);
1809 // Abort the second tx while it's queued.
1811 shard.tell(new AbortTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1812 expectMsgClass(duration, AbortTransactionReply.class);
1814 // Commit the other 2.
1816 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1817 expectMsgClass(duration, CanCommitTransactionReply.class);
1819 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1820 expectMsgClass(duration, CommitTransactionReply.class);
1822 shard.tell(new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), getRef());
1823 expectMsgClass(duration, CanCommitTransactionReply.class);
1825 shard.tell(new CommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), getRef());
1826 expectMsgClass(duration, CommitTransactionReply.class);
1828 assertEquals("getPendingTxCommitQueueSize", 0, shard.underlyingActor().getPendingTxCommitQueueSize());
1833 public void testCreateSnapshot() throws Exception {
1834 testCreateSnapshot(true, "testCreateSnapshot");
1838 public void testCreateSnapshotWithNonPersistentData() throws Exception {
1839 testCreateSnapshot(false, "testCreateSnapshotWithNonPersistentData");
1842 private void testCreateSnapshot(final boolean persistent, final String shardActorName) throws Exception{
1844 final AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(1));
1846 final AtomicReference<Object> savedSnapshot = new AtomicReference<>();
1847 class TestPersistentDataProvider extends DelegatingPersistentDataProvider {
1848 TestPersistentDataProvider(final DataPersistenceProvider delegate) {
1853 public void saveSnapshot(final Object o) {
1854 savedSnapshot.set(o);
1855 super.saveSnapshot(o);
1859 dataStoreContextBuilder.persistent(persistent);
1861 new ShardTestKit(getSystem()) {{
1862 class TestShard extends Shard {
1864 protected TestShard(final AbstractBuilder<?, ?> builder) {
1866 setPersistence(new TestPersistentDataProvider(super.persistence()));
1870 public void handleCommand(final Object message) {
1871 super.handleCommand(message);
1873 // XXX: commit_snapshot equality check references RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT
1874 if (message instanceof SaveSnapshotSuccess || "commit_snapshot".equals(message.toString())) {
1875 latch.get().countDown();
1880 public RaftActorContext getRaftActorContext() {
1881 return super.getRaftActorContext();
1885 final Creator<Shard> creator = () -> new TestShard(newShardBuilder());
1887 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1888 Props.create(new DelegatingShardCreator(creator)).
1889 withDispatcher(Dispatchers.DefaultDispatcherId()), shardActorName);
1891 waitUntilLeader(shard);
1892 writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1894 final NormalizedNode<?,?> expectedRoot = readStore(shard, YangInstanceIdentifier.EMPTY);
1896 // Trigger creation of a snapshot by ensuring
1897 final RaftActorContext raftActorContext = ((TestShard) shard.underlyingActor()).getRaftActorContext();
1898 raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
1899 awaitAndValidateSnapshot(expectedRoot);
1901 raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
1902 awaitAndValidateSnapshot(expectedRoot);
1905 private void awaitAndValidateSnapshot(final NormalizedNode<?,?> expectedRoot) throws InterruptedException, IOException {
1906 assertEquals("Snapshot saved", true, latch.get().await(5, TimeUnit.SECONDS));
1908 assertTrue("Invalid saved snapshot " + savedSnapshot.get(),
1909 savedSnapshot.get() instanceof Snapshot);
1911 verifySnapshot((Snapshot)savedSnapshot.get(), expectedRoot);
1913 latch.set(new CountDownLatch(1));
1914 savedSnapshot.set(null);
1917 private void verifySnapshot(final Snapshot snapshot, final NormalizedNode<?,?> expectedRoot) throws IOException {
1918 final NormalizedNode<?, ?> actual = ShardDataTreeSnapshot.deserialize(snapshot.getState()).getRootNode().get();
1919 assertEquals("Root node", expectedRoot, actual);
1924 * This test simply verifies that the applySnapShot logic will work
1925 * @throws ReadFailedException
1926 * @throws DataValidationFailedException
1929 public void testInMemoryDataTreeRestore() throws ReadFailedException, DataValidationFailedException {
1930 final DataTree store = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
1931 store.setSchemaContext(SCHEMA_CONTEXT);
1933 final DataTreeModification putTransaction = store.takeSnapshot().newModification();
1934 putTransaction.write(TestModel.TEST_PATH,
1935 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1936 commitTransaction(store, putTransaction);
1939 final NormalizedNode<?, ?> expected = readStore(store, YangInstanceIdentifier.EMPTY);
1941 final DataTreeModification writeTransaction = store.takeSnapshot().newModification();
1943 writeTransaction.delete(YangInstanceIdentifier.EMPTY);
1944 writeTransaction.write(YangInstanceIdentifier.EMPTY, expected);
1946 commitTransaction(store, writeTransaction);
1948 final NormalizedNode<?, ?> actual = readStore(store, YangInstanceIdentifier.EMPTY);
1950 assertEquals(expected, actual);
1954 public void testRecoveryApplicable(){
1956 final DatastoreContext persistentContext = DatastoreContext.newBuilder().
1957 shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(true).build();
1959 final Props persistentProps = Shard.builder().id(shardID).datastoreContext(persistentContext).
1960 schemaContext(SCHEMA_CONTEXT).props();
1962 final DatastoreContext nonPersistentContext = DatastoreContext.newBuilder().
1963 shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(false).build();
1965 final Props nonPersistentProps = Shard.builder().id(shardID).datastoreContext(nonPersistentContext).
1966 schemaContext(SCHEMA_CONTEXT).props();
1968 new ShardTestKit(getSystem()) {{
1969 final TestActorRef<Shard> shard1 = actorFactory.createTestActor(persistentProps, "testPersistence1");
1971 assertTrue("Recovery Applicable", shard1.underlyingActor().persistence().isRecoveryApplicable());
1973 final TestActorRef<Shard> shard2 = actorFactory.createTestActor(nonPersistentProps, "testPersistence2");
1975 assertFalse("Recovery Not Applicable", shard2.underlyingActor().persistence().isRecoveryApplicable());
1980 public void testOnDatastoreContext() {
1981 new ShardTestKit(getSystem()) {{
1982 dataStoreContextBuilder.persistent(true);
1984 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps(), "testOnDatastoreContext");
1986 assertEquals("isRecoveryApplicable", true,
1987 shard.underlyingActor().persistence().isRecoveryApplicable());
1989 waitUntilLeader(shard);
1991 shard.tell(dataStoreContextBuilder.persistent(false).build(), ActorRef.noSender());
1993 assertEquals("isRecoveryApplicable", false,
1994 shard.underlyingActor().persistence().isRecoveryApplicable());
1996 shard.tell(dataStoreContextBuilder.persistent(true).build(), ActorRef.noSender());
1998 assertEquals("isRecoveryApplicable", true,
1999 shard.underlyingActor().persistence().isRecoveryApplicable());
2004 public void testRegisterRoleChangeListener() throws Exception {
2005 new ShardTestKit(getSystem()) {
2007 final TestActorRef<Shard> shard = actorFactory.createTestActor(
2008 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
2009 "testRegisterRoleChangeListener");
2011 waitUntilLeader(shard);
2013 final TestActorRef<MessageCollectorActor> listener =
2014 TestActorRef.create(getSystem(), Props.create(MessageCollectorActor.class));
2016 shard.tell(new RegisterRoleChangeListener(), listener);
2018 MessageCollectorActor.expectFirstMatching(listener, RegisterRoleChangeListenerReply.class);
2020 ShardLeaderStateChanged leaderStateChanged = MessageCollectorActor.expectFirstMatching(listener,
2021 ShardLeaderStateChanged.class);
2022 assertEquals("getLocalShardDataTree present", true,
2023 leaderStateChanged.getLocalShardDataTree().isPresent());
2024 assertSame("getLocalShardDataTree", shard.underlyingActor().getDataStore().getDataTree(),
2025 leaderStateChanged.getLocalShardDataTree().get());
2027 MessageCollectorActor.clearMessages(listener);
2029 // Force a leader change
2031 shard.tell(new RequestVote(10000, "member2", 50, 50), getRef());
2033 leaderStateChanged = MessageCollectorActor.expectFirstMatching(listener,
2034 ShardLeaderStateChanged.class);
2035 assertEquals("getLocalShardDataTree present", false,
2036 leaderStateChanged.getLocalShardDataTree().isPresent());
2042 public void testFollowerInitialSyncStatus() throws Exception {
2043 final TestActorRef<Shard> shard = actorFactory.createTestActor(
2044 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
2045 "testFollowerInitialSyncStatus");
2047 shard.underlyingActor().handleNonRaftCommand(new FollowerInitialSyncUpStatus(false, "member-1-shard-inventory-operational"));
2049 assertEquals(false, shard.underlyingActor().getShardMBean().getFollowerInitialSyncStatus());
2051 shard.underlyingActor().handleNonRaftCommand(new FollowerInitialSyncUpStatus(true, "member-1-shard-inventory-operational"));
2053 assertEquals(true, shard.underlyingActor().getShardMBean().getFollowerInitialSyncStatus());
2057 public void testClusteredDataChangeListenerDelayedRegistration() throws Exception {
2058 new ShardTestKit(getSystem()) {{
2059 String testName = "testClusteredDataChangeListenerDelayedRegistration";
2060 dataStoreContextBuilder.shardElectionTimeoutFactor(1000).
2061 customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
2063 final MockDataChangeListener listener = new MockDataChangeListener(1);
2064 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
2065 actorFactory.generateActorId(testName + "-DataChangeListener"));
2067 setupInMemorySnapshotStore();
2069 final TestActorRef<Shard> shard = actorFactory.createTestActor(
2070 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
2071 actorFactory.generateActorId(testName + "-shard"));
2073 waitUntilNoLeader(shard);
2075 final YangInstanceIdentifier path = TestModel.TEST_PATH;
2077 shard.tell(new RegisterChangeListener(path, dclActor, AsyncDataBroker.DataChangeScope.BASE, true), getRef());
2078 final RegisterChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
2079 RegisterChangeListenerReply.class);
2080 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
2082 shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build()).
2083 customRaftPolicyImplementation(null).build(), ActorRef.noSender());
2085 listener.waitForChangeEvents();
2090 public void testClusteredDataChangeListenerRegistration() throws Exception {
2091 new ShardTestKit(getSystem()) {{
2092 String testName = "testClusteredDataChangeListenerRegistration";
2093 final ShardIdentifier followerShardID = ShardIdentifier.create("inventory",
2094 MemberName.forName(actorFactory.generateActorId(testName + "-follower")), "config");
2096 final ShardIdentifier leaderShardID = ShardIdentifier.create("inventory",
2097 MemberName.forName(actorFactory.generateActorId(testName + "-leader")), "config");
2099 final TestActorRef<Shard> followerShard = actorFactory.createTestActor(
2100 Shard.builder().id(followerShardID).
2101 datastoreContext(dataStoreContextBuilder.shardElectionTimeoutFactor(1000).build()).
2102 peerAddresses(Collections.singletonMap(leaderShardID.toString(),
2103 "akka://test/user/" + leaderShardID.toString())).schemaContext(SCHEMA_CONTEXT).props().
2104 withDispatcher(Dispatchers.DefaultDispatcherId()), followerShardID.toString());
2106 final TestActorRef<Shard> leaderShard = actorFactory.createTestActor(
2107 Shard.builder().id(leaderShardID).datastoreContext(newDatastoreContext()).
2108 peerAddresses(Collections.singletonMap(followerShardID.toString(),
2109 "akka://test/user/" + followerShardID.toString())).schemaContext(SCHEMA_CONTEXT).props().
2110 withDispatcher(Dispatchers.DefaultDispatcherId()), leaderShardID.toString());
2112 leaderShard.tell(TimeoutNow.INSTANCE, ActorRef.noSender());
2113 String leaderPath = waitUntilLeader(followerShard);
2114 assertEquals("Shard leader path", leaderShard.path().toString(), leaderPath);
2116 final YangInstanceIdentifier path = TestModel.TEST_PATH;
2117 final MockDataChangeListener listener = new MockDataChangeListener(1);
2118 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
2119 actorFactory.generateActorId(testName + "-DataChangeListener"));
2121 followerShard.tell(new RegisterChangeListener(path, dclActor, AsyncDataBroker.DataChangeScope.BASE, true), getRef());
2122 final RegisterChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
2123 RegisterChangeListenerReply.class);
2124 assertNotNull("getListenerRegistratioznPath", reply.getListenerRegistrationPath());
2126 writeToStore(followerShard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
2128 listener.waitForChangeEvents();
2133 public void testClusteredDataTreeChangeListenerDelayedRegistration() throws Exception {
2134 new ShardTestKit(getSystem()) {{
2135 String testName = "testClusteredDataTreeChangeListenerDelayedRegistration";
2136 dataStoreContextBuilder.shardElectionTimeoutFactor(1000).
2137 customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
2139 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
2140 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener),
2141 actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
2143 setupInMemorySnapshotStore();
2145 final TestActorRef<Shard> shard = actorFactory.createTestActor(
2146 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
2147 actorFactory.generateActorId(testName + "-shard"));
2149 waitUntilNoLeader(shard);
2151 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), getRef());
2152 final RegisterDataTreeChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
2153 RegisterDataTreeChangeListenerReply.class);
2154 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
2156 shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build()).
2157 customRaftPolicyImplementation(null).build(), ActorRef.noSender());
2159 listener.waitForChangeEvents();
2164 public void testClusteredDataTreeChangeListenerRegistration() throws Exception {
2165 new ShardTestKit(getSystem()) {{
2166 String testName = "testClusteredDataTreeChangeListenerRegistration";
2167 final ShardIdentifier followerShardID = ShardIdentifier.create("inventory",
2168 MemberName.forName(actorFactory.generateActorId(testName + "-follower")), "config");
2170 final ShardIdentifier leaderShardID = ShardIdentifier.create("inventory",
2171 MemberName.forName(actorFactory.generateActorId(testName + "-leader")), "config");
2173 final TestActorRef<Shard> followerShard = actorFactory.createTestActor(
2174 Shard.builder().id(followerShardID).
2175 datastoreContext(dataStoreContextBuilder.shardElectionTimeoutFactor(1000).build()).
2176 peerAddresses(Collections.singletonMap(leaderShardID.toString(),
2177 "akka://test/user/" + leaderShardID.toString())).schemaContext(SCHEMA_CONTEXT).props().
2178 withDispatcher(Dispatchers.DefaultDispatcherId()), followerShardID.toString());
2180 final TestActorRef<Shard> leaderShard = actorFactory.createTestActor(
2181 Shard.builder().id(leaderShardID).datastoreContext(newDatastoreContext()).
2182 peerAddresses(Collections.singletonMap(followerShardID.toString(),
2183 "akka://test/user/" + followerShardID.toString())).schemaContext(SCHEMA_CONTEXT).props().
2184 withDispatcher(Dispatchers.DefaultDispatcherId()), leaderShardID.toString());
2186 leaderShard.tell(TimeoutNow.INSTANCE, ActorRef.noSender());
2187 String leaderPath = waitUntilLeader(followerShard);
2188 assertEquals("Shard leader path", leaderShard.path().toString(), leaderPath);
2190 final YangInstanceIdentifier path = TestModel.TEST_PATH;
2191 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
2192 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener),
2193 actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
2195 followerShard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), getRef());
2196 final RegisterDataTreeChangeListenerReply reply = expectMsgClass(duration("5 seconds"),
2197 RegisterDataTreeChangeListenerReply.class);
2198 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
2200 writeToStore(followerShard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
2202 listener.waitForChangeEvents();
2207 public void testServerRemoved() throws Exception {
2208 final TestActorRef<MessageCollectorActor> parent = actorFactory.createTestActor(MessageCollectorActor.props());
2210 final ActorRef shard = parent.underlyingActor().context().actorOf(
2211 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
2212 "testServerRemoved");
2214 shard.tell(new ServerRemoved("test"), ActorRef.noSender());
2216 MessageCollectorActor.expectFirstMatching(parent, ServerRemoved.class);