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;
23 import akka.actor.ActorRef;
24 import akka.actor.ActorSelection;
25 import akka.actor.Props;
26 import akka.actor.Status.Failure;
27 import akka.dispatch.Dispatchers;
28 import akka.dispatch.OnComplete;
29 import akka.japi.Creator;
30 import akka.pattern.Patterns;
31 import akka.persistence.SaveSnapshotSuccess;
32 import akka.testkit.TestActorRef;
33 import akka.util.Timeout;
34 import com.google.common.base.Stopwatch;
35 import com.google.common.base.Throwables;
36 import com.google.common.util.concurrent.Uninterruptibles;
37 import java.io.IOException;
38 import java.util.Collections;
39 import java.util.HashSet;
42 import java.util.concurrent.CountDownLatch;
43 import java.util.concurrent.TimeUnit;
44 import java.util.concurrent.atomic.AtomicBoolean;
45 import java.util.concurrent.atomic.AtomicReference;
46 import org.junit.Test;
47 import org.mockito.InOrder;
48 import org.opendaylight.controller.cluster.DataPersistenceProvider;
49 import org.opendaylight.controller.cluster.DelegatingPersistentDataProvider;
50 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
51 import org.opendaylight.controller.cluster.access.concepts.MemberName;
52 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
53 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
54 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
55 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
56 import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction;
57 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
58 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
59 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
60 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
61 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
62 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration;
63 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistrationReply;
64 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
65 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
66 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
67 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
68 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
69 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
70 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
71 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
72 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
73 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
74 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply;
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.WriteModification;
79 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
80 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
81 import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
82 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
83 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListenerReply;
84 import org.opendaylight.controller.cluster.raft.RaftActorContext;
85 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
86 import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot;
87 import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
88 import org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus;
89 import org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow;
90 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
91 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
92 import org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState;
93 import org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState;
94 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
95 import org.opendaylight.controller.cluster.raft.messages.ServerRemoved;
96 import org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries;
97 import org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry;
98 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
99 import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy;
100 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
101 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
102 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
103 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
104 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
105 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
106 import org.opendaylight.yangtools.concepts.Identifier;
107 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
108 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
109 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
110 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
111 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
112 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
113 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
114 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
115 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
116 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
117 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
118 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
119 import scala.concurrent.Await;
120 import scala.concurrent.Future;
121 import scala.concurrent.duration.FiniteDuration;
123 public class ShardTest extends AbstractShardTest {
124 private static final String DUMMY_DATA = "Dummy data as snapshot sequence number is set to 0 in "
125 + "InMemorySnapshotStore and journal recovery seq number will start from 1";
128 public void testRegisterDataTreeChangeListener() throws Exception {
129 new ShardTestKit(getSystem()) {
131 final TestActorRef<Shard> shard = actorFactory.createTestActor(
132 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
133 "testRegisterDataTreeChangeListener");
135 waitUntilLeader(shard);
137 shard.tell(new UpdateSchemaContext(SchemaContextHelper.full()), ActorRef.noSender());
139 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
140 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener,
141 TestModel.TEST_PATH), "testRegisterDataTreeChangeListener-DataTreeChangeListener");
143 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, false), getRef());
145 final RegisterDataTreeNotificationListenerReply reply = expectMsgClass(duration("3 seconds"),
146 RegisterDataTreeNotificationListenerReply.class);
147 final String replyPath = reply.getListenerRegistrationPath().toString();
148 assertTrue("Incorrect reply path: " + replyPath,
149 replyPath.matches("akka:\\/\\/test\\/user\\/testRegisterDataTreeChangeListener\\/\\$.*"));
151 final YangInstanceIdentifier path = TestModel.TEST_PATH;
152 writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
154 listener.waitForChangeEvents();
159 @SuppressWarnings("serial")
161 public void testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration() throws Exception {
162 final CountDownLatch onFirstElectionTimeout = new CountDownLatch(1);
163 final CountDownLatch onChangeListenerRegistered = new CountDownLatch(1);
164 final Creator<Shard> creator = new Creator<Shard>() {
165 boolean firstElectionTimeout = true;
168 public Shard create() throws Exception {
169 return new Shard(newShardBuilder()) {
171 public void handleCommand(final Object message) {
172 if (message instanceof ElectionTimeout && firstElectionTimeout) {
173 firstElectionTimeout = false;
174 final ActorRef self = getSelf();
176 Uninterruptibles.awaitUninterruptibly(
177 onChangeListenerRegistered, 5, TimeUnit.SECONDS);
178 self.tell(message, self);
181 onFirstElectionTimeout.countDown();
183 super.handleCommand(message);
190 setupInMemorySnapshotStore();
192 final YangInstanceIdentifier path = TestModel.TEST_PATH;
193 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
194 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener, path),
195 "testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration-DataChangeListener");
197 final TestActorRef<Shard> shard = actorFactory.createTestActor(
198 Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
199 "testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration");
201 new ShardTestKit(getSystem()) {
203 assertEquals("Got first ElectionTimeout", true, onFirstElectionTimeout.await(5, TimeUnit.SECONDS));
205 shard.tell(new RegisterDataTreeChangeListener(path, dclActor, false), getRef());
206 final RegisterDataTreeNotificationListenerReply reply = expectMsgClass(duration("5 seconds"),
207 RegisterDataTreeNotificationListenerReply.class);
208 assertNotNull("getListenerRegistratioznPath", reply.getListenerRegistrationPath());
210 shard.tell(FindLeader.INSTANCE, getRef());
211 final FindLeaderReply findLeadeReply = expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
212 assertFalse("Expected the shard not to be the leader", findLeadeReply.getLeaderActor().isPresent());
214 onChangeListenerRegistered.countDown();
216 // TODO: investigate why we do not receive data chage events
217 listener.waitForChangeEvents();
223 public void testCreateTransaction() {
224 new ShardTestKit(getSystem()) {
226 final ActorRef shard = actorFactory.createActor(newShardProps(), "testCreateTransaction");
228 waitUntilLeader(shard);
230 shard.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
232 shard.tell(new CreateTransaction(nextTransactionId(), TransactionType.READ_ONLY.ordinal(),
233 DataStoreVersions.CURRENT_VERSION).toSerializable(), getRef());
235 final CreateTransactionReply reply = expectMsgClass(duration("3 seconds"),
236 CreateTransactionReply.class);
238 final String path = reply.getTransactionPath().toString();
239 assertTrue("Unexpected transaction path " + path, path.startsWith(String.format(
240 "akka://test/user/testCreateTransaction/shard-%s-%s:ShardTransactionTest@0:",
241 shardID.getShardName(), shardID.getMemberName().getName())));
247 public void testCreateTransactionOnChain() {
248 new ShardTestKit(getSystem()) {
250 final ActorRef shard = actorFactory.createActor(newShardProps(), "testCreateTransactionOnChain");
252 waitUntilLeader(shard);
254 shard.tell(new CreateTransaction(nextTransactionId(), TransactionType.READ_ONLY.ordinal(),
255 DataStoreVersions.CURRENT_VERSION).toSerializable(), getRef());
257 final CreateTransactionReply reply = expectMsgClass(duration("3 seconds"),
258 CreateTransactionReply.class);
260 final String path = reply.getTransactionPath().toString();
261 assertTrue("Unexpected transaction path " + path, path.startsWith(String.format(
262 "akka://test/user/testCreateTransactionOnChain/shard-%s-%s:ShardTransactionTest@0:",
263 shardID.getShardName(), shardID.getMemberName().getName())));
269 public void testPeerAddressResolved() throws Exception {
270 new ShardTestKit(getSystem()) {
272 final ShardIdentifier peerID = ShardIdentifier.create("inventory", MemberName.forName("member-2"),
274 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardBuilder()
275 .peerAddresses(Collections.<String, String>singletonMap(peerID.toString(), null))
276 .props().withDispatcher(Dispatchers.DefaultDispatcherId()), "testPeerAddressResolved");
278 final String address = "akka://foobar";
279 shard.tell(new PeerAddressResolved(peerID.toString(), address), ActorRef.noSender());
281 shard.tell(GetOnDemandRaftState.INSTANCE, getRef());
282 final OnDemandRaftState state = expectMsgClass(OnDemandRaftState.class);
283 assertEquals("getPeerAddress", address, state.getPeerAddresses().get(peerID.toString()));
289 public void testApplySnapshot() throws Exception {
291 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps()
292 .withDispatcher(Dispatchers.DefaultDispatcherId()), "testApplySnapshot");
294 ShardTestKit.waitUntilLeader(shard);
296 final DataTree store = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
299 final ContainerNode container = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
300 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
301 .withChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).addChild(
302 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1)).build()).build();
304 writeToStore(store, TestModel.TEST_PATH, container);
306 final YangInstanceIdentifier root = YangInstanceIdentifier.EMPTY;
307 final NormalizedNode<?,?> expected = readStore(store, root);
309 final Snapshot snapshot = Snapshot.create(
310 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(expected)),
311 Collections.<ReplicatedLogEntry>emptyList(), 1, 2, 3, 4, -1, null, null);
313 shard.tell(new ApplySnapshot(snapshot), ActorRef.noSender());
315 final Stopwatch sw = Stopwatch.createStarted();
316 while (sw.elapsed(TimeUnit.SECONDS) <= 5) {
317 Uninterruptibles.sleepUninterruptibly(75, TimeUnit.MILLISECONDS);
320 assertEquals("Root node", expected, readStore(shard, root));
322 } catch (final AssertionError e) {
327 fail("Snapshot was not applied");
331 public void testApplyState() throws Exception {
332 final TestActorRef<Shard> shard = actorFactory.createTestActor(
333 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testApplyState");
335 ShardTestKit.waitUntilLeader(shard);
337 final DataTree store = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
340 final DataTreeModification writeMod = store.takeSnapshot().newModification();
341 final ContainerNode node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
342 writeMod.write(TestModel.TEST_PATH, node);
345 final TransactionIdentifier tx = nextTransactionId();
346 shard.underlyingActor().applyState(null, null, payloadForModification(store, writeMod, tx));
348 final Stopwatch sw = Stopwatch.createStarted();
349 while (sw.elapsed(TimeUnit.SECONDS) <= 5) {
350 Uninterruptibles.sleepUninterruptibly(75, TimeUnit.MILLISECONDS);
352 final NormalizedNode<?,?> actual = readStore(shard, TestModel.TEST_PATH);
353 if (actual != null) {
354 assertEquals("Applied state", node, actual);
359 fail("State was not applied");
363 public void testDataTreeCandidateRecovery() throws Exception {
364 // Set up the InMemorySnapshotStore.
365 final DataTree source = setupInMemorySnapshotStore();
367 final DataTreeModification writeMod = source.takeSnapshot().newModification();
368 writeMod.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
370 InMemoryJournal.addEntry(shardID.toString(), 0, DUMMY_DATA);
372 // Set up the InMemoryJournal.
373 InMemoryJournal.addEntry(shardID.toString(), 1, new SimpleReplicatedLogEntry(0, 1,
374 payloadForModification(source, writeMod, nextTransactionId())));
376 final int nListEntries = 16;
377 final Set<Integer> listEntryKeys = new HashSet<>();
379 // Add some ModificationPayload entries
380 for (int i = 1; i <= nListEntries; i++) {
381 listEntryKeys.add(Integer.valueOf(i));
383 final YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
384 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i).build();
386 final DataTreeModification mod = source.takeSnapshot().newModification();
387 mod.merge(path, ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i));
390 InMemoryJournal.addEntry(shardID.toString(), i + 1, new SimpleReplicatedLogEntry(i, 1,
391 payloadForModification(source, mod, nextTransactionId())));
394 InMemoryJournal.addEntry(shardID.toString(), nListEntries + 2,
395 new ApplyJournalEntries(nListEntries));
397 testRecovery(listEntryKeys);
401 @SuppressWarnings("checkstyle:IllegalCatch")
402 public void testConcurrentThreePhaseCommits() throws Exception {
403 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
404 final CountDownLatch commitLatch = new CountDownLatch(2);
406 final long timeoutSec = 5;
407 final FiniteDuration duration = FiniteDuration.create(timeoutSec, TimeUnit.SECONDS);
408 final Timeout timeout = new Timeout(duration);
410 final TestActorRef<Shard> shard = actorFactory.createTestActor(
411 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
412 "testConcurrentThreePhaseCommits");
414 class OnFutureComplete extends OnComplete<Object> {
415 private final Class<?> expRespType;
417 OnFutureComplete(final Class<?> expRespType) {
418 this.expRespType = expRespType;
422 public void onComplete(final Throwable error, final Object resp) {
424 caughtEx.set(new AssertionError(getClass().getSimpleName() + " failure", error));
427 assertEquals("Commit response type", expRespType, resp.getClass());
429 } catch (final Exception e) {
435 void onSuccess(final Object resp) throws Exception {
439 class OnCommitFutureComplete extends OnFutureComplete {
440 OnCommitFutureComplete() {
441 super(CommitTransactionReply.class);
445 public void onComplete(final Throwable error, final Object resp) {
446 super.onComplete(error, resp);
447 commitLatch.countDown();
451 class OnCanCommitFutureComplete extends OnFutureComplete {
452 private final TransactionIdentifier transactionID;
454 OnCanCommitFutureComplete(final TransactionIdentifier transactionID) {
455 super(CanCommitTransactionReply.class);
456 this.transactionID = transactionID;
460 void onSuccess(final Object resp) throws Exception {
461 final CanCommitTransactionReply canCommitReply =
462 CanCommitTransactionReply.fromSerializable(resp);
463 assertEquals("Can commit", true, canCommitReply.getCanCommit());
465 final Future<Object> commitFuture = Patterns.ask(shard,
466 new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), timeout);
467 commitFuture.onComplete(new OnCommitFutureComplete(), getSystem().dispatcher());
471 new ShardTestKit(getSystem()) {
473 waitUntilLeader(shard);
475 final TransactionIdentifier transactionID1 = nextTransactionId();
476 final TransactionIdentifier transactionID2 = nextTransactionId();
477 final TransactionIdentifier transactionID3 = nextTransactionId();
479 final Map<TransactionIdentifier, CapturingShardDataTreeCohort> cohortMap = setupCohortDecorator(
480 shard.underlyingActor(), transactionID1, transactionID2, transactionID3);
481 final CapturingShardDataTreeCohort cohort1 = cohortMap.get(transactionID1);
482 final CapturingShardDataTreeCohort cohort2 = cohortMap.get(transactionID2);
483 final CapturingShardDataTreeCohort cohort3 = cohortMap.get(transactionID3);
485 shard.tell(prepareBatchedModifications(transactionID1, TestModel.TEST_PATH,
486 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
487 final ReadyTransactionReply readyReply = ReadyTransactionReply
488 .fromSerializable(expectMsgClass(duration, ReadyTransactionReply.class));
489 assertEquals("Cohort path", shard.path().toString(), readyReply.getCohortPath());
490 // Send the CanCommitTransaction message for the first Tx.
492 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
493 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
494 .fromSerializable(expectMsgClass(duration, CanCommitTransactionReply.class));
495 assertEquals("Can commit", true, canCommitReply.getCanCommit());
497 // Ready 2 more Tx's.
499 shard.tell(prepareBatchedModifications(transactionID2, TestModel.OUTER_LIST_PATH,
500 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false), getRef());
501 expectMsgClass(duration, ReadyTransactionReply.class);
504 prepareBatchedModifications(transactionID3,
505 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
506 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
507 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), false),
509 expectMsgClass(duration, ReadyTransactionReply.class);
511 // Send the CanCommitTransaction message for the next 2 Tx's.
512 // These should get queued and
513 // processed after the first Tx completes.
515 final Future<Object> canCommitFuture1 = Patterns.ask(shard,
516 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
518 final Future<Object> canCommitFuture2 = Patterns.ask(shard,
519 new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), timeout);
521 // Send the CommitTransaction message for the first Tx. After it
522 // completes, it should
523 // trigger the 2nd Tx to proceed which should in turn then
526 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
527 expectMsgClass(duration, CommitTransactionReply.class);
529 // Wait for the next 2 Tx's to complete.
531 canCommitFuture1.onComplete(new OnCanCommitFutureComplete(transactionID2), getSystem().dispatcher());
533 canCommitFuture2.onComplete(new OnCanCommitFutureComplete(transactionID3), getSystem().dispatcher());
535 final boolean done = commitLatch.await(timeoutSec, TimeUnit.SECONDS);
537 final Throwable t = caughtEx.get();
539 Throwables.propagateIfPossible(t, Exception.class);
540 throw new RuntimeException(t);
543 assertEquals("Commits complete", true, done);
545 // final InOrder inOrder = inOrder(cohort1.getCanCommit(), cohort1.getPreCommit(), cohort1.getCommit(),
546 // cohort2.getCanCommit(), cohort2.getPreCommit(), cohort2.getCommit(), cohort3.getCanCommit(),
547 // cohort3.getPreCommit(), cohort3.getCommit());
548 // inOrder.verify(cohort1.getCanCommit()).onSuccess(any(Void.class));
549 // inOrder.verify(cohort1.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
550 // inOrder.verify(cohort2.getCanCommit()).onSuccess(any(Void.class));
551 // inOrder.verify(cohort2.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
552 // inOrder.verify(cohort3.getCanCommit()).onSuccess(any(Void.class));
553 // inOrder.verify(cohort3.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
554 // inOrder.verify(cohort1.getCommit()).onSuccess(any(UnsignedLong.class));
555 // inOrder.verify(cohort2.getCommit()).onSuccess(any(UnsignedLong.class));
556 // inOrder.verify(cohort3.getCommit()).onSuccess(any(UnsignedLong.class));
558 // Verify data in the data store.
560 verifyOuterListEntry(shard, 1);
562 verifyLastApplied(shard, 5);
568 public void testBatchedModificationsWithNoCommitOnReady() throws Exception {
569 new ShardTestKit(getSystem()) {
571 final TestActorRef<Shard> shard = actorFactory.createTestActor(
572 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
573 "testBatchedModificationsWithNoCommitOnReady");
575 waitUntilLeader(shard);
577 final TransactionIdentifier transactionID = nextTransactionId();
578 final FiniteDuration duration = duration("5 seconds");
580 // Send a BatchedModifications to start a transaction.
582 shard.tell(newBatchedModifications(transactionID, TestModel.TEST_PATH,
583 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false, false, 1), getRef());
584 expectMsgClass(duration, BatchedModificationsReply.class);
586 // Send a couple more BatchedModifications.
589 newBatchedModifications(transactionID, TestModel.OUTER_LIST_PATH,
590 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false, false, 2),
592 expectMsgClass(duration, BatchedModificationsReply.class);
594 shard.tell(newBatchedModifications(transactionID,
595 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
596 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
597 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), true, false, 3),
599 expectMsgClass(duration, ReadyTransactionReply.class);
601 // Send the CanCommitTransaction message.
603 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
604 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
605 .fromSerializable(expectMsgClass(duration, CanCommitTransactionReply.class));
606 assertEquals("Can commit", true, canCommitReply.getCanCommit());
608 // Send the CommitTransaction message.
610 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
611 expectMsgClass(duration, CommitTransactionReply.class);
613 // Verify data in the data store.
615 verifyOuterListEntry(shard, 1);
621 public void testBatchedModificationsWithCommitOnReady() throws Exception {
622 new ShardTestKit(getSystem()) {
624 final TestActorRef<Shard> shard = actorFactory.createTestActor(
625 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
626 "testBatchedModificationsWithCommitOnReady");
628 waitUntilLeader(shard);
630 final TransactionIdentifier transactionID = nextTransactionId();
631 final FiniteDuration duration = duration("5 seconds");
633 // Send a BatchedModifications to start a transaction.
635 shard.tell(newBatchedModifications(transactionID, TestModel.TEST_PATH,
636 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false, false, 1), getRef());
637 expectMsgClass(duration, BatchedModificationsReply.class);
639 // Send a couple more BatchedModifications.
641 shard.tell(newBatchedModifications(transactionID, TestModel.OUTER_LIST_PATH,
642 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false, false, 2),
644 expectMsgClass(duration, BatchedModificationsReply.class);
646 shard.tell(newBatchedModifications(transactionID,
647 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
648 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
649 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), true, true, 3),
652 expectMsgClass(duration, CommitTransactionReply.class);
654 // Verify data in the data store.
656 verifyOuterListEntry(shard, 1);
661 @Test(expected = IllegalStateException.class)
662 public void testBatchedModificationsReadyWithIncorrectTotalMessageCount() throws Exception {
663 new ShardTestKit(getSystem()) {
665 final TestActorRef<Shard> shard = actorFactory.createTestActor(
666 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
667 "testBatchedModificationsReadyWithIncorrectTotalMessageCount");
669 waitUntilLeader(shard);
671 final TransactionIdentifier transactionID = nextTransactionId();
672 final BatchedModifications batched = new BatchedModifications(transactionID,
673 DataStoreVersions.CURRENT_VERSION);
674 batched.setReady(true);
675 batched.setTotalMessagesSent(2);
677 shard.tell(batched, getRef());
679 final Failure failure = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
681 if (failure != null) {
682 Throwables.propagateIfPossible(failure.cause(), Exception.class);
683 throw new RuntimeException(failure.cause());
690 public void testBatchedModificationsWithOperationFailure() throws Exception {
691 new ShardTestKit(getSystem()) {
693 final TestActorRef<Shard> shard = actorFactory.createTestActor(
694 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
695 "testBatchedModificationsWithOperationFailure");
697 waitUntilLeader(shard);
699 // Test merge with invalid data. An exception should occur when
700 // the merge is applied. Note that
701 // write will not validate the children for performance reasons.
703 final TransactionIdentifier transactionID = nextTransactionId();
705 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
706 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
707 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
709 BatchedModifications batched = new BatchedModifications(transactionID, CURRENT_VERSION);
710 batched.addModification(new MergeModification(TestModel.TEST_PATH, invalidData));
711 shard.tell(batched, getRef());
712 Failure failure = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
714 final Throwable cause = failure.cause();
716 batched = new BatchedModifications(transactionID, DataStoreVersions.CURRENT_VERSION);
717 batched.setReady(true);
718 batched.setTotalMessagesSent(2);
720 shard.tell(batched, getRef());
722 failure = expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
723 assertEquals("Failure cause", cause, failure.cause());
729 public void testBatchedModificationsOnTransactionChain() throws Exception {
730 new ShardTestKit(getSystem()) {
732 final TestActorRef<Shard> shard = actorFactory.createTestActor(
733 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
734 "testBatchedModificationsOnTransactionChain");
736 waitUntilLeader(shard);
738 final LocalHistoryIdentifier historyId = nextHistoryId();
739 final TransactionIdentifier transactionID1 = new TransactionIdentifier(historyId, 0);
740 final TransactionIdentifier transactionID2 = new TransactionIdentifier(historyId, 1);
742 final FiniteDuration duration = duration("5 seconds");
744 // Send a BatchedModifications to start a chained write
745 // transaction and ready it.
747 final ContainerNode containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
748 final YangInstanceIdentifier path = TestModel.TEST_PATH;
749 shard.tell(newBatchedModifications(transactionID1, path, containerNode, true, false, 1), getRef());
750 expectMsgClass(duration, ReadyTransactionReply.class);
752 // Create a read Tx on the same chain.
754 shard.tell(new CreateTransaction(transactionID2, TransactionType.READ_ONLY.ordinal(),
755 DataStoreVersions.CURRENT_VERSION).toSerializable(), getRef());
757 final CreateTransactionReply createReply = expectMsgClass(duration("3 seconds"),
758 CreateTransactionReply.class);
760 getSystem().actorSelection(createReply.getTransactionPath())
761 .tell(new ReadData(path, DataStoreVersions.CURRENT_VERSION), getRef());
762 final ReadDataReply readReply = expectMsgClass(duration("3 seconds"), ReadDataReply.class);
763 assertEquals("Read node", containerNode, readReply.getNormalizedNode());
765 // Commit the write transaction.
767 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
768 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
769 .fromSerializable(expectMsgClass(duration, CanCommitTransactionReply.class));
770 assertEquals("Can commit", true, canCommitReply.getCanCommit());
772 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
773 expectMsgClass(duration, CommitTransactionReply.class);
775 // Verify data in the data store.
777 final NormalizedNode<?, ?> actualNode = readStore(shard, path);
778 assertEquals("Stored node", containerNode, actualNode);
784 public void testOnBatchedModificationsWhenNotLeader() {
785 final AtomicBoolean overrideLeaderCalls = new AtomicBoolean();
786 new ShardTestKit(getSystem()) {
788 final Creator<Shard> creator = new Creator<Shard>() {
789 private static final long serialVersionUID = 1L;
792 public Shard create() throws Exception {
793 return new Shard(newShardBuilder()) {
795 protected boolean isLeader() {
796 return overrideLeaderCalls.get() ? false : super.isLeader();
800 public ActorSelection getLeader() {
801 return overrideLeaderCalls.get() ? getSystem().actorSelection(getRef().path())
808 final TestActorRef<Shard> shard = actorFactory.createTestActor(Props
809 .create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
810 "testOnBatchedModificationsWhenNotLeader");
812 waitUntilLeader(shard);
814 overrideLeaderCalls.set(true);
816 final BatchedModifications batched = new BatchedModifications(nextTransactionId(),
817 DataStoreVersions.CURRENT_VERSION);
819 shard.tell(batched, ActorRef.noSender());
821 expectMsgEquals(batched);
827 public void testTransactionMessagesWithNoLeader() {
828 new ShardTestKit(getSystem()) {
830 dataStoreContextBuilder.customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName())
831 .shardHeartbeatIntervalInMillis(50).shardElectionTimeoutFactor(1);
832 final TestActorRef<Shard> shard = actorFactory.createTestActor(
833 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
834 "testTransactionMessagesWithNoLeader");
836 waitUntilNoLeader(shard);
838 final TransactionIdentifier txId = nextTransactionId();
839 shard.tell(new BatchedModifications(txId, DataStoreVersions.CURRENT_VERSION), getRef());
840 Failure failure = expectMsgClass(Failure.class);
841 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
843 shard.tell(prepareForwardedReadyTransaction(shard, txId, TestModel.TEST_PATH,
844 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), getRef());
845 failure = expectMsgClass(Failure.class);
846 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
848 shard.tell(new ReadyLocalTransaction(txId, mock(DataTreeModification.class), true), getRef());
849 failure = expectMsgClass(Failure.class);
850 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
856 public void testReadyWithReadWriteImmediateCommit() throws Exception {
857 testReadyWithImmediateCommit(true);
861 public void testReadyWithWriteOnlyImmediateCommit() throws Exception {
862 testReadyWithImmediateCommit(false);
865 private void testReadyWithImmediateCommit(final boolean readWrite) throws Exception {
866 new ShardTestKit(getSystem()) {
868 final TestActorRef<Shard> shard = actorFactory.createTestActor(
869 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
870 "testReadyWithImmediateCommit-" + readWrite);
872 waitUntilLeader(shard);
874 final TransactionIdentifier transactionID = nextTransactionId();
875 final NormalizedNode<?, ?> containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
877 shard.tell(prepareForwardedReadyTransaction(shard, transactionID, TestModel.TEST_PATH,
878 containerNode, true), getRef());
880 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH, containerNode, true),
884 expectMsgClass(duration("5 seconds"), CommitTransactionReply.class);
886 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.TEST_PATH);
887 assertEquals(TestModel.TEST_QNAME.getLocalName(), containerNode, actualNode);
893 public void testReadyLocalTransactionWithImmediateCommit() throws Exception {
894 new ShardTestKit(getSystem()) {
896 final TestActorRef<Shard> shard = actorFactory.createTestActor(
897 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
898 "testReadyLocalTransactionWithImmediateCommit");
900 waitUntilLeader(shard);
902 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
904 final DataTreeModification modification = dataStore.newModification();
906 final ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
907 new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
908 final MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
909 new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
911 final TransactionIdentifier txId = nextTransactionId();
912 modification.ready();
913 final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(txId, modification, true);
915 shard.tell(readyMessage, getRef());
917 expectMsgClass(CommitTransactionReply.class);
919 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.OUTER_LIST_PATH);
920 assertEquals(TestModel.OUTER_LIST_QNAME.getLocalName(), mergeData, actualNode);
926 public void testReadyLocalTransactionWithThreePhaseCommit() throws Exception {
927 new ShardTestKit(getSystem()) {
929 final TestActorRef<Shard> shard = actorFactory.createTestActor(
930 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
931 "testReadyLocalTransactionWithThreePhaseCommit");
933 waitUntilLeader(shard);
935 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
937 final DataTreeModification modification = dataStore.newModification();
939 final ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
940 new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
941 final MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
942 new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
944 final TransactionIdentifier txId = nextTransactionId();
945 modification.ready();
946 final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(txId, modification, false);
948 shard.tell(readyMessage, getRef());
950 expectMsgClass(ReadyTransactionReply.class);
952 // Send the CanCommitTransaction message.
954 shard.tell(new CanCommitTransaction(txId, CURRENT_VERSION).toSerializable(), getRef());
955 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
956 .fromSerializable(expectMsgClass(CanCommitTransactionReply.class));
957 assertEquals("Can commit", true, canCommitReply.getCanCommit());
959 // Send the CanCommitTransaction message.
961 shard.tell(new CommitTransaction(txId, CURRENT_VERSION).toSerializable(), getRef());
962 expectMsgClass(CommitTransactionReply.class);
964 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.OUTER_LIST_PATH);
965 assertEquals(TestModel.OUTER_LIST_QNAME.getLocalName(), mergeData, actualNode);
971 public void testReadWriteCommitWithPersistenceDisabled() throws Exception {
972 dataStoreContextBuilder.persistent(false);
973 new ShardTestKit(getSystem()) {
975 final TestActorRef<Shard> shard = actorFactory.createTestActor(
976 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
977 "testCommitWithPersistenceDisabled");
979 waitUntilLeader(shard);
981 // Setup a simulated transactions with a mock cohort.
983 final FiniteDuration duration = duration("5 seconds");
985 final TransactionIdentifier transactionID = nextTransactionId();
986 final NormalizedNode<?, ?> containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
987 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH, containerNode, false),
989 expectMsgClass(duration, ReadyTransactionReply.class);
991 // Send the CanCommitTransaction message.
993 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
994 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
995 .fromSerializable(expectMsgClass(duration, CanCommitTransactionReply.class));
996 assertEquals("Can commit", true, canCommitReply.getCanCommit());
998 // Send the CanCommitTransaction message.
1000 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1001 expectMsgClass(duration, CommitTransactionReply.class);
1003 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.TEST_PATH);
1004 assertEquals(TestModel.TEST_QNAME.getLocalName(), containerNode, actualNode);
1010 public void testReadWriteCommitWhenTransactionHasModifications() throws Exception {
1011 testCommitWhenTransactionHasModifications(true);
1015 public void testWriteOnlyCommitWhenTransactionHasModifications() throws Exception {
1016 testCommitWhenTransactionHasModifications(false);
1019 private void testCommitWhenTransactionHasModifications(final boolean readWrite) throws Exception {
1020 new ShardTestKit(getSystem()) {
1022 final DataTree dataTree = createDelegatingMockDataTree();
1023 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1024 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1025 "testCommitWhenTransactionHasModifications-" + readWrite);
1027 waitUntilLeader(shard);
1029 final FiniteDuration duration = duration("5 seconds");
1030 final TransactionIdentifier transactionID = nextTransactionId();
1033 shard.tell(prepareForwardedReadyTransaction(shard, transactionID, TestModel.TEST_PATH,
1034 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
1036 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH,
1037 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
1040 expectMsgClass(duration, ReadyTransactionReply.class);
1042 // Send the CanCommitTransaction message.
1044 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1045 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
1046 .fromSerializable(expectMsgClass(duration, CanCommitTransactionReply.class));
1047 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1049 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1050 expectMsgClass(duration, CommitTransactionReply.class);
1052 final InOrder inOrder = inOrder(dataTree);
1053 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1054 inOrder.verify(dataTree).prepare(any(DataTreeModification.class));
1055 inOrder.verify(dataTree).commit(any(DataTreeCandidate.class));
1057 // Purge request is scheduled as asynchronous, wait for two heartbeats to let it propagate into
1059 Thread.sleep(HEARTBEAT_MILLIS * 2);
1061 shard.tell(Shard.GET_SHARD_MBEAN_MESSAGE, getRef());
1062 final ShardStats shardStats = expectMsgClass(duration, ShardStats.class);
1064 // Use MBean for verification
1065 // Committed transaction count should increase as usual
1066 assertEquals(1, shardStats.getCommittedTransactionsCount());
1068 // Commit index should advance as we do not have an empty
1070 assertEquals(1, shardStats.getCommitIndex());
1076 public void testCommitPhaseFailure() throws Exception {
1077 new ShardTestKit(getSystem()) {
1079 final DataTree dataTree = createDelegatingMockDataTree();
1080 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1081 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1082 "testCommitPhaseFailure");
1084 waitUntilLeader(shard);
1086 final FiniteDuration duration = duration("5 seconds");
1087 final Timeout timeout = new Timeout(duration);
1089 // Setup 2 simulated transactions with mock cohorts. The first
1093 doThrow(new RuntimeException("mock commit failure")).when(dataTree)
1094 .commit(any(DataTreeCandidate.class));
1096 final TransactionIdentifier transactionID1 = nextTransactionId();
1097 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1098 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1099 expectMsgClass(duration, ReadyTransactionReply.class);
1101 final TransactionIdentifier transactionID2 = nextTransactionId();
1102 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1103 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1104 expectMsgClass(duration, ReadyTransactionReply.class);
1106 // Send the CanCommitTransaction message for the first Tx.
1108 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1109 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
1110 .fromSerializable(expectMsgClass(duration, CanCommitTransactionReply.class));
1111 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1113 // Send the CanCommitTransaction message for the 2nd Tx. This
1114 // should get queued and
1115 // processed after the first Tx completes.
1117 final Future<Object> canCommitFuture = Patterns.ask(shard,
1118 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1120 // Send the CommitTransaction message for the first Tx. This
1121 // should send back an error
1122 // and trigger the 2nd Tx to proceed.
1124 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1125 expectMsgClass(duration, akka.actor.Status.Failure.class);
1127 // Wait for the 2nd Tx to complete the canCommit phase.
1129 final CountDownLatch latch = new CountDownLatch(1);
1130 canCommitFuture.onComplete(new OnComplete<Object>() {
1132 public void onComplete(final Throwable failure, final Object resp) {
1135 }, getSystem().dispatcher());
1137 assertEquals("2nd CanCommit complete", true, latch.await(5, TimeUnit.SECONDS));
1139 final InOrder inOrder = inOrder(dataTree);
1140 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1141 inOrder.verify(dataTree).prepare(any(DataTreeModification.class));
1143 // FIXME: this invocation is done on the result of validate(). To test it, we need to make sure mock
1144 // validate performs wrapping and we capture that mock
1145 // inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1147 inOrder.verify(dataTree).commit(any(DataTreeCandidate.class));
1153 public void testPreCommitPhaseFailure() throws Exception {
1154 new ShardTestKit(getSystem()) {
1156 final DataTree dataTree = createDelegatingMockDataTree();
1157 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1158 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1159 "testPreCommitPhaseFailure");
1161 waitUntilLeader(shard);
1163 final FiniteDuration duration = duration("5 seconds");
1164 final Timeout timeout = new Timeout(duration);
1166 doThrow(new RuntimeException("mock preCommit failure")).when(dataTree)
1167 .prepare(any(DataTreeModification.class));
1169 final TransactionIdentifier transactionID1 = nextTransactionId();
1170 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1171 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1172 expectMsgClass(duration, ReadyTransactionReply.class);
1174 final TransactionIdentifier transactionID2 = nextTransactionId();
1175 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1176 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1177 expectMsgClass(duration, ReadyTransactionReply.class);
1179 // Send the CanCommitTransaction message for the first Tx.
1181 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1182 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
1183 .fromSerializable(expectMsgClass(duration, CanCommitTransactionReply.class));
1184 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1186 // Send the CanCommitTransaction message for the 2nd Tx. This
1187 // should get queued and
1188 // processed after the first Tx completes.
1190 final Future<Object> canCommitFuture = Patterns.ask(shard,
1191 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1193 // Send the CommitTransaction message for the first Tx. This
1194 // should send back an error
1195 // and trigger the 2nd Tx to proceed.
1197 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1198 expectMsgClass(duration, akka.actor.Status.Failure.class);
1200 // Wait for the 2nd Tx to complete the canCommit phase.
1202 final CountDownLatch latch = new CountDownLatch(1);
1203 canCommitFuture.onComplete(new OnComplete<Object>() {
1205 public void onComplete(final Throwable failure, final Object resp) {
1208 }, getSystem().dispatcher());
1210 assertEquals("2nd CanCommit complete", true, latch.await(5, TimeUnit.SECONDS));
1212 final InOrder inOrder = inOrder(dataTree);
1213 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1214 inOrder.verify(dataTree).prepare(any(DataTreeModification.class));
1215 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1221 public void testCanCommitPhaseFailure() throws Exception {
1222 new ShardTestKit(getSystem()) {
1224 final DataTree dataTree = createDelegatingMockDataTree();
1225 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1226 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1227 "testCanCommitPhaseFailure");
1229 waitUntilLeader(shard);
1231 final FiniteDuration duration = duration("5 seconds");
1232 final TransactionIdentifier transactionID1 = nextTransactionId();
1234 doThrow(new DataValidationFailedException(YangInstanceIdentifier.EMPTY, "mock canCommit failure"))
1235 .doNothing().when(dataTree).validate(any(DataTreeModification.class));
1237 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1238 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1239 expectMsgClass(duration, ReadyTransactionReply.class);
1241 // Send the CanCommitTransaction message.
1243 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1244 expectMsgClass(duration, akka.actor.Status.Failure.class);
1246 // Send another can commit to ensure the failed one got cleaned
1249 final TransactionIdentifier transactionID2 = nextTransactionId();
1250 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1251 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1252 expectMsgClass(duration, ReadyTransactionReply.class);
1254 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1255 final CanCommitTransactionReply reply = CanCommitTransactionReply
1256 .fromSerializable(expectMsgClass(CanCommitTransactionReply.class));
1257 assertEquals("getCanCommit", true, reply.getCanCommit());
1263 public void testImmediateCommitWithCanCommitPhaseFailure() throws Exception {
1264 testImmediateCommitWithCanCommitPhaseFailure(true);
1265 testImmediateCommitWithCanCommitPhaseFailure(false);
1268 private void testImmediateCommitWithCanCommitPhaseFailure(final boolean readWrite) throws Exception {
1269 new ShardTestKit(getSystem()) {
1271 final DataTree dataTree = createDelegatingMockDataTree();
1272 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1273 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1274 "testImmediateCommitWithCanCommitPhaseFailure-" + readWrite);
1276 waitUntilLeader(shard);
1278 doThrow(new DataValidationFailedException(YangInstanceIdentifier.EMPTY, "mock canCommit failure"))
1279 .doNothing().when(dataTree).validate(any(DataTreeModification.class));
1281 final FiniteDuration duration = duration("5 seconds");
1283 final TransactionIdentifier transactionID1 = nextTransactionId();
1286 shard.tell(prepareForwardedReadyTransaction(shard, transactionID1, TestModel.TEST_PATH,
1287 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), getRef());
1289 shard.tell(prepareBatchedModifications(transactionID1, TestModel.TEST_PATH,
1290 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), getRef());
1293 expectMsgClass(duration, akka.actor.Status.Failure.class);
1295 // Send another can commit to ensure the failed one got cleaned
1298 final TransactionIdentifier transactionID2 = nextTransactionId();
1300 shard.tell(prepareForwardedReadyTransaction(shard, transactionID2, TestModel.TEST_PATH,
1301 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), getRef());
1303 shard.tell(prepareBatchedModifications(transactionID2, TestModel.TEST_PATH,
1304 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), getRef());
1307 expectMsgClass(duration, CommitTransactionReply.class);
1313 public void testAbortWithCommitPending() throws Exception {
1314 new ShardTestKit(getSystem()) {
1316 final Creator<Shard> creator = () -> new Shard(newShardBuilder()) {
1318 void persistPayload(final Identifier id, final Payload payload,
1319 final boolean batchHint) {
1320 // Simulate an AbortTransaction message occurring during
1321 // replication, after
1322 // persisting and before finishing the commit to the
1325 doAbortTransaction(id, null);
1326 super.persistPayload(id, payload, batchHint);
1330 final TestActorRef<Shard> shard = actorFactory.createTestActor(Props
1331 .create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
1332 "testAbortWithCommitPending");
1334 waitUntilLeader(shard);
1336 final FiniteDuration duration = duration("5 seconds");
1338 final TransactionIdentifier transactionID = nextTransactionId();
1340 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH,
1341 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
1342 expectMsgClass(duration, ReadyTransactionReply.class);
1344 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1345 expectMsgClass(duration, CanCommitTransactionReply.class);
1347 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), getRef());
1348 expectMsgClass(duration, CommitTransactionReply.class);
1350 final NormalizedNode<?, ?> node = readStore(shard, TestModel.TEST_PATH);
1352 // Since we're simulating an abort occurring during replication
1353 // and before finish commit,
1354 // the data should still get written to the in-memory store
1355 // since we've gotten past
1356 // canCommit and preCommit and persisted the data.
1357 assertNotNull(TestModel.TEST_QNAME.getLocalName() + " not found", node);
1363 public void testTransactionCommitTimeout() throws Exception {
1364 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1365 new ShardTestKit(getSystem()) {
1367 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1368 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1369 "testTransactionCommitTimeout");
1371 waitUntilLeader(shard);
1373 final FiniteDuration duration = duration("5 seconds");
1375 writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1376 writeToStore(shard, TestModel.OUTER_LIST_PATH,
1377 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1379 // Ready 2 Tx's - the first will timeout
1381 final TransactionIdentifier transactionID1 = nextTransactionId();
1383 prepareBatchedModifications(transactionID1,
1384 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1385 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
1386 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), false),
1388 expectMsgClass(duration, ReadyTransactionReply.class);
1390 final TransactionIdentifier transactionID2 = nextTransactionId();
1391 final YangInstanceIdentifier listNodePath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1392 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build();
1394 prepareBatchedModifications(transactionID2, listNodePath,
1395 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2), false),
1397 expectMsgClass(duration, ReadyTransactionReply.class);
1399 // canCommit 1st Tx. We don't send the commit so it should
1402 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1403 expectMsgClass(duration, CanCommitTransactionReply.class);
1405 // canCommit the 2nd Tx - it should complete after the 1st Tx
1408 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1409 expectMsgClass(duration, CanCommitTransactionReply.class);
1411 // Try to commit the 1st Tx - should fail as it's not the
1414 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1415 expectMsgClass(duration, akka.actor.Status.Failure.class);
1417 // Commit the 2nd Tx.
1419 shard.tell(new CommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1420 expectMsgClass(duration, CommitTransactionReply.class);
1422 final NormalizedNode<?, ?> node = readStore(shard, listNodePath);
1423 assertNotNull(listNodePath + " not found", node);
1430 // public void testTransactionCommitQueueCapacityExceeded() throws Throwable {
1431 // dataStoreContextBuilder.shardTransactionCommitQueueCapacity(2);
1433 // new ShardTestKit(getSystem()) {{
1434 // final TestActorRef<Shard> shard = actorFactory.createTestActor(
1435 // newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1436 // "testTransactionCommitQueueCapacityExceeded");
1438 // waitUntilLeader(shard);
1440 // final FiniteDuration duration = duration("5 seconds");
1442 // final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1444 // final TransactionIdentifier transactionID1 = nextTransactionId();
1445 // final MutableCompositeModification modification1 = new MutableCompositeModification();
1446 // final ShardDataTreeCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
1447 // TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), transactionID1,
1450 // final TransactionIdentifier transactionID2 = nextTransactionId();
1451 // final MutableCompositeModification modification2 = new MutableCompositeModification();
1452 // final ShardDataTreeCohort cohort2 = setupMockWriteTransaction("cohort2", dataStore,
1453 // TestModel.OUTER_LIST_PATH,
1454 // ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), transactionID2,
1457 // final TransactionIdentifier transactionID3 = nextTransactionId();
1458 // final MutableCompositeModification modification3 = new MutableCompositeModification();
1459 // final ShardDataTreeCohort cohort3 = setupMockWriteTransaction("cohort3", dataStore,
1460 // TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), transactionID3,
1463 // // Ready the Tx's
1465 // shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort1, transactionID1,
1466 // modification1), getRef());
1467 // expectMsgClass(duration, ReadyTransactionReply.class);
1469 // shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort2, transactionID2,
1470 // modification2), getRef());
1471 // expectMsgClass(duration, ReadyTransactionReply.class);
1473 // // The 3rd Tx should exceed queue capacity and fail.
1475 // shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort3, transactionID3,
1476 // modification3), getRef());
1477 // expectMsgClass(duration, akka.actor.Status.Failure.class);
1479 // // canCommit 1st Tx.
1481 // shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1482 // expectMsgClass(duration, CanCommitTransactionReply.class);
1484 // // canCommit the 2nd Tx - it should get queued.
1486 // shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1488 // // canCommit the 3rd Tx - should exceed queue capacity and fail.
1490 // shard.tell(new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), getRef());
1491 // expectMsgClass(duration, akka.actor.Status.Failure.class);
1496 public void testTransactionCommitWithPriorExpiredCohortEntries() throws Exception {
1497 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1498 new ShardTestKit(getSystem()) {
1500 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1501 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1502 "testTransactionCommitWithPriorExpiredCohortEntries");
1504 waitUntilLeader(shard);
1506 final FiniteDuration duration = duration("5 seconds");
1508 final TransactionIdentifier transactionID1 = nextTransactionId();
1509 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1510 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1511 expectMsgClass(duration, ReadyTransactionReply.class);
1513 final TransactionIdentifier transactionID2 = nextTransactionId();
1514 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1515 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1516 expectMsgClass(duration, ReadyTransactionReply.class);
1518 final TransactionIdentifier transactionID3 = nextTransactionId();
1519 shard.tell(newBatchedModifications(transactionID3, TestModel.TEST_PATH,
1520 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1521 expectMsgClass(duration, ReadyTransactionReply.class);
1523 // All Tx's are readied. We'll send canCommit for the last one
1524 // but not the others. The others
1525 // should expire from the queue and the last one should be
1528 shard.tell(new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), getRef());
1529 expectMsgClass(duration, CanCommitTransactionReply.class);
1535 public void testTransactionCommitWithSubsequentExpiredCohortEntry() throws Exception {
1536 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1537 new ShardTestKit(getSystem()) {
1539 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1540 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1541 "testTransactionCommitWithSubsequentExpiredCohortEntry");
1543 waitUntilLeader(shard);
1545 final FiniteDuration duration = duration("5 seconds");
1547 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1549 final TransactionIdentifier transactionID1 = nextTransactionId();
1550 shard.tell(prepareBatchedModifications(transactionID1, TestModel.TEST_PATH,
1551 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
1552 expectMsgClass(duration, ReadyTransactionReply.class);
1554 // CanCommit the first Tx so it's the current in-progress Tx.
1556 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1557 expectMsgClass(duration, CanCommitTransactionReply.class);
1559 // Ready the second Tx.
1561 final TransactionIdentifier transactionID2 = nextTransactionId();
1562 shard.tell(prepareBatchedModifications(transactionID2, TestModel.TEST_PATH,
1563 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), getRef());
1564 expectMsgClass(duration, ReadyTransactionReply.class);
1566 // Ready the third Tx.
1568 final TransactionIdentifier transactionID3 = nextTransactionId();
1569 final DataTreeModification modification3 = dataStore.newModification();
1570 new WriteModification(TestModel.TEST2_PATH, ImmutableNodes.containerNode(TestModel.TEST2_QNAME))
1571 .apply(modification3);
1572 modification3.ready();
1573 final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(transactionID3, modification3,
1575 shard.tell(readyMessage, getRef());
1577 // Commit the first Tx. After completing, the second should
1578 // expire from the queue and the third
1581 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1582 expectMsgClass(duration, CommitTransactionReply.class);
1584 // Expect commit reply from the third Tx.
1586 expectMsgClass(duration, CommitTransactionReply.class);
1588 final NormalizedNode<?, ?> node = readStore(shard, TestModel.TEST2_PATH);
1589 assertNotNull(TestModel.TEST2_PATH + " not found", node);
1595 public void testCanCommitBeforeReadyFailure() throws Exception {
1596 new ShardTestKit(getSystem()) {
1598 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1599 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1600 "testCanCommitBeforeReadyFailure");
1602 shard.tell(new CanCommitTransaction(nextTransactionId(), CURRENT_VERSION).toSerializable(), getRef());
1603 expectMsgClass(duration("5 seconds"), akka.actor.Status.Failure.class);
1609 public void testAbortAfterCanCommit() throws Exception {
1610 new ShardTestKit(getSystem()) {
1612 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1613 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testAbortAfterCanCommit");
1615 waitUntilLeader(shard);
1617 final FiniteDuration duration = duration("5 seconds");
1618 final Timeout timeout = new Timeout(duration);
1620 // Ready 2 transactions - the first one will be aborted.
1622 final TransactionIdentifier transactionID1 = nextTransactionId();
1623 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1624 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1625 expectMsgClass(duration, ReadyTransactionReply.class);
1627 final TransactionIdentifier transactionID2 = nextTransactionId();
1628 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1629 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1630 expectMsgClass(duration, ReadyTransactionReply.class);
1632 // Send the CanCommitTransaction message for the first Tx.
1634 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1635 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
1636 .fromSerializable(expectMsgClass(duration, CanCommitTransactionReply.class));
1637 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1639 // Send the CanCommitTransaction message for the 2nd Tx. This
1640 // should get queued and
1641 // processed after the first Tx completes.
1643 final Future<Object> canCommitFuture = Patterns.ask(shard,
1644 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1646 // Send the AbortTransaction message for the first Tx. This
1647 // should trigger the 2nd
1650 shard.tell(new AbortTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1651 expectMsgClass(duration, AbortTransactionReply.class);
1653 // Wait for the 2nd Tx to complete the canCommit phase.
1655 canCommitReply = (CanCommitTransactionReply) Await.result(canCommitFuture, duration);
1656 assertEquals("Can commit", true, canCommitReply.getCanCommit());
1662 public void testAbortAfterReady() throws Exception {
1663 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1664 new ShardTestKit(getSystem()) {
1666 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1667 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testAbortAfterReady");
1669 waitUntilLeader(shard);
1671 final FiniteDuration duration = duration("5 seconds");
1675 final TransactionIdentifier transactionID1 = nextTransactionId();
1676 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1677 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1678 expectMsgClass(duration, ReadyTransactionReply.class);
1680 // Send the AbortTransaction message.
1682 shard.tell(new AbortTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1683 expectMsgClass(duration, AbortTransactionReply.class);
1685 assertEquals("getPendingTxCommitQueueSize", 0, shard.underlyingActor().getPendingTxCommitQueueSize());
1687 // Now send CanCommitTransaction - should fail.
1689 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1690 final Throwable failure = expectMsgClass(duration, akka.actor.Status.Failure.class).cause();
1691 assertTrue("Failure type", failure instanceof IllegalStateException);
1693 // Ready and CanCommit another and verify success.
1695 final TransactionIdentifier transactionID2 = nextTransactionId();
1696 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1697 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1698 expectMsgClass(duration, ReadyTransactionReply.class);
1700 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1701 expectMsgClass(duration, CanCommitTransactionReply.class);
1707 public void testAbortQueuedTransaction() throws Exception {
1708 new ShardTestKit(getSystem()) {
1710 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1711 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testAbortAfterReady");
1713 waitUntilLeader(shard);
1715 final FiniteDuration duration = duration("5 seconds");
1719 final TransactionIdentifier transactionID1 = nextTransactionId();
1720 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1721 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1722 expectMsgClass(duration, ReadyTransactionReply.class);
1724 final TransactionIdentifier transactionID2 = nextTransactionId();
1725 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1726 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), getRef());
1727 expectMsgClass(duration, ReadyTransactionReply.class);
1729 final TransactionIdentifier transactionID3 = nextTransactionId();
1731 newBatchedModifications(transactionID3, TestModel.OUTER_LIST_PATH,
1732 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), true, false, 1),
1734 expectMsgClass(duration, ReadyTransactionReply.class);
1736 // Abort the second tx while it's queued.
1738 shard.tell(new AbortTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1739 expectMsgClass(duration, AbortTransactionReply.class);
1741 // Commit the other 2.
1743 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1744 expectMsgClass(duration, CanCommitTransactionReply.class);
1746 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1747 expectMsgClass(duration, CommitTransactionReply.class);
1749 shard.tell(new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), getRef());
1750 expectMsgClass(duration, CanCommitTransactionReply.class);
1752 shard.tell(new CommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), getRef());
1753 expectMsgClass(duration, CommitTransactionReply.class);
1755 assertEquals("getPendingTxCommitQueueSize", 0, shard.underlyingActor().getPendingTxCommitQueueSize());
1761 public void testCreateSnapshotWithNonPersistentData() throws Exception {
1762 testCreateSnapshot(false, "testCreateSnapshotWithNonPersistentData");
1766 public void testCreateSnapshot() throws Exception {
1767 testCreateSnapshot(true, "testCreateSnapshot");
1770 private void testCreateSnapshot(final boolean persistent, final String shardActorName) throws Exception {
1771 final AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(1));
1773 final AtomicReference<Object> savedSnapshot = new AtomicReference<>();
1774 class TestPersistentDataProvider extends DelegatingPersistentDataProvider {
1775 TestPersistentDataProvider(final DataPersistenceProvider delegate) {
1780 public void saveSnapshot(final Object obj) {
1781 savedSnapshot.set(obj);
1782 super.saveSnapshot(obj);
1786 dataStoreContextBuilder.persistent(persistent);
1788 class TestShard extends Shard {
1790 protected TestShard(final AbstractBuilder<?, ?> builder) {
1792 setPersistence(new TestPersistentDataProvider(super.persistence()));
1796 public void handleCommand(final Object message) {
1797 super.handleCommand(message);
1799 // XXX: commit_snapshot equality check references RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT
1800 if (message instanceof SaveSnapshotSuccess || "commit_snapshot".equals(message.toString())) {
1801 latch.get().countDown();
1806 public RaftActorContext getRaftActorContext() {
1807 return super.getRaftActorContext();
1811 new ShardTestKit(getSystem()) {
1813 final Creator<Shard> creator = () -> new TestShard(newShardBuilder());
1815 final TestActorRef<Shard> shard = actorFactory.createTestActor(Props
1816 .create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
1819 waitUntilLeader(shard);
1820 writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1822 final NormalizedNode<?, ?> expectedRoot = readStore(shard, YangInstanceIdentifier.EMPTY);
1824 // Trigger creation of a snapshot by ensuring
1825 final RaftActorContext raftActorContext = ((TestShard) shard.underlyingActor()).getRaftActorContext();
1826 raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
1827 awaitAndValidateSnapshot(expectedRoot);
1829 raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
1830 awaitAndValidateSnapshot(expectedRoot);
1833 private void awaitAndValidateSnapshot(final NormalizedNode<?, ?> expectedRoot)
1834 throws InterruptedException, IOException {
1835 assertEquals("Snapshot saved", true, latch.get().await(5, TimeUnit.SECONDS));
1837 assertTrue("Invalid saved snapshot " + savedSnapshot.get(), savedSnapshot.get() instanceof Snapshot);
1839 verifySnapshot((Snapshot) savedSnapshot.get(), expectedRoot);
1841 latch.set(new CountDownLatch(1));
1842 savedSnapshot.set(null);
1845 private void verifySnapshot(final Snapshot snapshot, final NormalizedNode<?, ?> expectedRoot)
1846 throws IOException {
1847 final NormalizedNode<?, ?> actual = ((ShardSnapshotState)snapshot.getState()).getSnapshot()
1848 .getRootNode().get();
1849 assertEquals("Root node", expectedRoot, actual);
1855 * This test simply verifies that the applySnapShot logic will work.
1858 public void testInMemoryDataTreeRestore() throws ReadFailedException, DataValidationFailedException {
1859 final DataTree store = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
1862 final DataTreeModification putTransaction = store.takeSnapshot().newModification();
1863 putTransaction.write(TestModel.TEST_PATH,
1864 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1865 commitTransaction(store, putTransaction);
1868 final NormalizedNode<?, ?> expected = readStore(store, YangInstanceIdentifier.EMPTY);
1870 final DataTreeModification writeTransaction = store.takeSnapshot().newModification();
1872 writeTransaction.delete(YangInstanceIdentifier.EMPTY);
1873 writeTransaction.write(YangInstanceIdentifier.EMPTY, expected);
1875 commitTransaction(store, writeTransaction);
1877 final NormalizedNode<?, ?> actual = readStore(store, YangInstanceIdentifier.EMPTY);
1879 assertEquals(expected, actual);
1883 public void testRecoveryApplicable() {
1885 final DatastoreContext persistentContext = DatastoreContext.newBuilder()
1886 .shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(true).build();
1888 final Props persistentProps = Shard.builder().id(shardID).datastoreContext(persistentContext)
1889 .schemaContextProvider(() -> SCHEMA_CONTEXT).props();
1891 final DatastoreContext nonPersistentContext = DatastoreContext.newBuilder()
1892 .shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(false).build();
1894 final Props nonPersistentProps = Shard.builder().id(shardID).datastoreContext(nonPersistentContext)
1895 .schemaContextProvider(() -> SCHEMA_CONTEXT).props();
1897 new ShardTestKit(getSystem()) {
1899 final TestActorRef<Shard> shard1 = actorFactory.createTestActor(persistentProps, "testPersistence1");
1901 assertTrue("Recovery Applicable", shard1.underlyingActor().persistence().isRecoveryApplicable());
1903 final TestActorRef<Shard> shard2 = actorFactory.createTestActor(nonPersistentProps, "testPersistence2");
1905 assertFalse("Recovery Not Applicable", shard2.underlyingActor().persistence().isRecoveryApplicable());
1911 public void testOnDatastoreContext() {
1912 new ShardTestKit(getSystem()) {
1914 dataStoreContextBuilder.persistent(true);
1916 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps(),
1917 "testOnDatastoreContext");
1919 assertEquals("isRecoveryApplicable", true,
1920 shard.underlyingActor().persistence().isRecoveryApplicable());
1922 waitUntilLeader(shard);
1924 shard.tell(dataStoreContextBuilder.persistent(false).build(), ActorRef.noSender());
1926 assertEquals("isRecoveryApplicable", false,
1927 shard.underlyingActor().persistence().isRecoveryApplicable());
1929 shard.tell(dataStoreContextBuilder.persistent(true).build(), ActorRef.noSender());
1931 assertEquals("isRecoveryApplicable", true,
1932 shard.underlyingActor().persistence().isRecoveryApplicable());
1938 public void testRegisterRoleChangeListener() throws Exception {
1939 new ShardTestKit(getSystem()) {
1941 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1942 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1943 "testRegisterRoleChangeListener");
1945 waitUntilLeader(shard);
1947 final ActorRef listener = getSystem().actorOf(MessageCollectorActor.props());
1949 shard.tell(new RegisterRoleChangeListener(), listener);
1951 MessageCollectorActor.expectFirstMatching(listener, RegisterRoleChangeListenerReply.class);
1953 ShardLeaderStateChanged leaderStateChanged = MessageCollectorActor.expectFirstMatching(listener,
1954 ShardLeaderStateChanged.class);
1955 assertEquals("getLocalShardDataTree present", true,
1956 leaderStateChanged.getLocalShardDataTree().isPresent());
1957 assertSame("getLocalShardDataTree", shard.underlyingActor().getDataStore().getDataTree(),
1958 leaderStateChanged.getLocalShardDataTree().get());
1960 MessageCollectorActor.clearMessages(listener);
1962 // Force a leader change
1964 shard.tell(new RequestVote(10000, "member2", 50, 50), getRef());
1966 leaderStateChanged = MessageCollectorActor.expectFirstMatching(listener,
1967 ShardLeaderStateChanged.class);
1968 assertEquals("getLocalShardDataTree present", false,
1969 leaderStateChanged.getLocalShardDataTree().isPresent());
1975 public void testFollowerInitialSyncStatus() throws Exception {
1976 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1977 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1978 "testFollowerInitialSyncStatus");
1980 shard.underlyingActor().handleNonRaftCommand(new FollowerInitialSyncUpStatus(false,
1981 "member-1-shard-inventory-operational"));
1983 assertEquals(false, shard.underlyingActor().getShardMBean().getFollowerInitialSyncStatus());
1985 shard.underlyingActor().handleNonRaftCommand(new FollowerInitialSyncUpStatus(true,
1986 "member-1-shard-inventory-operational"));
1988 assertEquals(true, shard.underlyingActor().getShardMBean().getFollowerInitialSyncStatus());
1992 public void testClusteredDataTreeChangeListenerWithDelayedRegistration() throws Exception {
1993 new ShardTestKit(getSystem()) {
1995 final String testName = "testClusteredDataTreeChangeListenerWithDelayedRegistration";
1996 dataStoreContextBuilder.shardElectionTimeoutFactor(1000)
1997 .customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
1999 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
2000 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener,
2001 TestModel.TEST_PATH), actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
2003 setupInMemorySnapshotStore();
2005 final TestActorRef<Shard> shard = actorFactory.createTestActor(
2006 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
2007 actorFactory.generateActorId(testName + "-shard"));
2009 waitUntilNoLeader(shard);
2011 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), getRef());
2012 final RegisterDataTreeNotificationListenerReply reply = expectMsgClass(duration("5 seconds"),
2013 RegisterDataTreeNotificationListenerReply.class);
2014 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
2016 shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build())
2017 .customRaftPolicyImplementation(null).build(), ActorRef.noSender());
2019 listener.waitForChangeEvents();
2025 public void testClusteredDataTreeChangeListenerWithDelayedRegistrationClosed() throws Exception {
2026 new ShardTestKit(getSystem()) {
2028 final String testName = "testClusteredDataTreeChangeListenerWithDelayedRegistrationClosed";
2029 dataStoreContextBuilder.shardElectionTimeoutFactor(1000)
2030 .customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
2032 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(0);
2033 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener,
2034 TestModel.TEST_PATH), actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
2036 setupInMemorySnapshotStore();
2038 final TestActorRef<Shard> shard = actorFactory.createTestActor(
2039 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
2040 actorFactory.generateActorId(testName + "-shard"));
2042 waitUntilNoLeader(shard);
2044 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), getRef());
2045 final RegisterDataTreeNotificationListenerReply reply = expectMsgClass(duration("5 seconds"),
2046 RegisterDataTreeNotificationListenerReply.class);
2047 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
2049 final ActorSelection regActor = getSystem().actorSelection(reply.getListenerRegistrationPath());
2050 regActor.tell(CloseDataTreeNotificationListenerRegistration.getInstance(), getRef());
2051 expectMsgClass(CloseDataTreeNotificationListenerRegistrationReply.class);
2053 shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build())
2054 .customRaftPolicyImplementation(null).build(), ActorRef.noSender());
2056 listener.expectNoMoreChanges("Received unexpected change after close");
2062 public void testClusteredDataTreeChangeListenerRegistration() throws Exception {
2063 new ShardTestKit(getSystem()) {
2065 final String testName = "testClusteredDataTreeChangeListenerRegistration";
2066 final ShardIdentifier followerShardID = ShardIdentifier.create("inventory",
2067 MemberName.forName(actorFactory.generateActorId(testName + "-follower")), "config");
2069 final ShardIdentifier leaderShardID = ShardIdentifier.create("inventory",
2070 MemberName.forName(actorFactory.generateActorId(testName + "-leader")), "config");
2072 final TestActorRef<Shard> followerShard = actorFactory
2073 .createTestActor(Shard.builder().id(followerShardID)
2074 .datastoreContext(dataStoreContextBuilder.shardElectionTimeoutFactor(1000).build())
2075 .peerAddresses(Collections.singletonMap(leaderShardID.toString(),
2076 "akka://test/user/" + leaderShardID.toString()))
2077 .schemaContextProvider(() -> SCHEMA_CONTEXT).props()
2078 .withDispatcher(Dispatchers.DefaultDispatcherId()), followerShardID.toString());
2080 final TestActorRef<Shard> leaderShard = actorFactory
2081 .createTestActor(Shard.builder().id(leaderShardID).datastoreContext(newDatastoreContext())
2082 .peerAddresses(Collections.singletonMap(followerShardID.toString(),
2083 "akka://test/user/" + followerShardID.toString()))
2084 .schemaContextProvider(() -> SCHEMA_CONTEXT).props()
2085 .withDispatcher(Dispatchers.DefaultDispatcherId()), leaderShardID.toString());
2087 leaderShard.tell(TimeoutNow.INSTANCE, ActorRef.noSender());
2088 final String leaderPath = waitUntilLeader(followerShard);
2089 assertEquals("Shard leader path", leaderShard.path().toString(), leaderPath);
2091 final YangInstanceIdentifier path = TestModel.TEST_PATH;
2092 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
2093 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener, path),
2094 actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
2096 followerShard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), getRef());
2097 final RegisterDataTreeNotificationListenerReply reply = expectMsgClass(duration("5 seconds"),
2098 RegisterDataTreeNotificationListenerReply.class);
2099 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
2101 writeToStore(followerShard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
2103 listener.waitForChangeEvents();
2109 public void testServerRemoved() throws Exception {
2110 final TestActorRef<MessageCollectorActor> parent = actorFactory.createTestActor(MessageCollectorActor.props()
2111 .withDispatcher(Dispatchers.DefaultDispatcherId()));
2113 final ActorRef shard = parent.underlyingActor().context().actorOf(
2114 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
2115 "testServerRemoved");
2117 shard.tell(new ServerRemoved("test"), ActorRef.noSender());
2119 MessageCollectorActor.expectFirstMatching(parent, ServerRemoved.class);