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
8 package org.opendaylight.controller.cluster.datastore;
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.CoreMatchers.endsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertSame;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.doThrow;
21 import static org.mockito.Mockito.inOrder;
22 import static org.mockito.Mockito.mock;
23 import static org.opendaylight.controller.cluster.datastore.DataStoreVersions.CURRENT_VERSION;
25 import akka.actor.ActorRef;
26 import akka.actor.ActorSelection;
27 import akka.actor.Props;
28 import akka.actor.Status.Failure;
29 import akka.dispatch.Dispatchers;
30 import akka.dispatch.OnComplete;
31 import akka.japi.Creator;
32 import akka.pattern.Patterns;
33 import akka.persistence.SaveSnapshotSuccess;
34 import akka.testkit.TestActorRef;
35 import akka.util.Timeout;
36 import com.google.common.base.Stopwatch;
37 import com.google.common.base.Throwables;
38 import com.google.common.util.concurrent.Uninterruptibles;
39 import java.time.Duration;
40 import java.util.Collections;
41 import java.util.HashSet;
43 import java.util.Optional;
45 import java.util.concurrent.CountDownLatch;
46 import java.util.concurrent.TimeUnit;
47 import java.util.concurrent.atomic.AtomicBoolean;
48 import java.util.concurrent.atomic.AtomicReference;
49 import org.junit.Test;
50 import org.mockito.InOrder;
51 import org.opendaylight.controller.cluster.DataPersistenceProvider;
52 import org.opendaylight.controller.cluster.DelegatingPersistentDataProvider;
53 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
54 import org.opendaylight.controller.cluster.access.concepts.MemberName;
55 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
56 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
57 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
58 import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction;
59 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
60 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
61 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
62 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
63 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
64 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration;
65 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistrationReply;
66 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
67 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
68 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
69 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
70 import org.opendaylight.controller.cluster.datastore.messages.PeerAddressResolved;
71 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
72 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
73 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
74 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
75 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
76 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply;
77 import org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged;
78 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
79 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
80 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
81 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
82 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
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.base.messages.ApplySnapshot;
89 import org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout;
90 import org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus;
91 import org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow;
92 import org.opendaylight.controller.cluster.raft.client.messages.FindLeader;
93 import org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply;
94 import org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState;
95 import org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState;
96 import org.opendaylight.controller.cluster.raft.messages.Payload;
97 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
98 import org.opendaylight.controller.cluster.raft.messages.ServerRemoved;
99 import org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries;
100 import org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry;
101 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
102 import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy;
103 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
104 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
105 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
106 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
107 import org.opendaylight.yangtools.concepts.Identifier;
108 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
109 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
110 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
111 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
112 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
113 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
114 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
115 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
116 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
117 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
118 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
119 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
120 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
121 import scala.concurrent.Await;
122 import scala.concurrent.Future;
123 import scala.concurrent.duration.FiniteDuration;
125 public class ShardTest extends AbstractShardTest {
126 private static final String DUMMY_DATA = "Dummy data as snapshot sequence number is set to 0 in "
127 + "InMemorySnapshotStore and journal recovery seq number will start from 1";
130 public void testRegisterDataTreeChangeListener() throws Exception {
131 final ShardTestKit testKit = new ShardTestKit(getSystem());
132 final TestActorRef<Shard> shard = actorFactory.createTestActor(
133 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
134 "testRegisterDataTreeChangeListener");
136 ShardTestKit.waitUntilLeader(shard);
138 shard.tell(new UpdateSchemaContext(SchemaContextHelper.full()), ActorRef.noSender());
140 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
141 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener,
142 TestModel.TEST_PATH), "testRegisterDataTreeChangeListener-DataTreeChangeListener");
144 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, false), testKit.getRef());
146 final RegisterDataTreeNotificationListenerReply reply = testKit.expectMsgClass(Duration.ofSeconds(3),
147 RegisterDataTreeNotificationListenerReply.class);
148 final String replyPath = reply.getListenerRegistrationPath().toString();
149 assertTrue("Incorrect reply path: " + replyPath,
150 replyPath.matches("akka:\\/\\/test\\/user\\/testRegisterDataTreeChangeListener\\/\\$.*"));
152 final YangInstanceIdentifier path = TestModel.TEST_PATH;
153 writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
155 listener.waitForChangeEvents();
156 listener.verifyOnInitialDataEvent();
158 final MockDataTreeChangeListener listener2 = new MockDataTreeChangeListener(1);
159 final ActorRef dclActor2 = actorFactory.createActor(DataTreeChangeListenerActor.props(listener2,
160 TestModel.TEST_PATH), "testRegisterDataTreeChangeListener-DataTreeChangeListener2");
162 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor2, false), testKit.getRef());
164 testKit.expectMsgClass(Duration.ofSeconds(3), RegisterDataTreeNotificationListenerReply.class);
166 listener2.waitForChangeEvents();
167 listener2.verifyNoOnInitialDataEvent();
170 @SuppressWarnings("serial")
172 public void testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration() throws Exception {
173 final CountDownLatch onFirstElectionTimeout = new CountDownLatch(1);
174 final CountDownLatch onChangeListenerRegistered = new CountDownLatch(1);
175 final Creator<Shard> creator = new Creator<>() {
176 boolean firstElectionTimeout = true;
179 public Shard create() {
180 return new Shard(newShardBuilder()) {
182 public void handleCommand(final Object message) {
183 if (message instanceof ElectionTimeout && firstElectionTimeout) {
184 firstElectionTimeout = false;
185 final ActorRef self = getSelf();
187 Uninterruptibles.awaitUninterruptibly(
188 onChangeListenerRegistered, 5, TimeUnit.SECONDS);
189 self.tell(message, self);
192 onFirstElectionTimeout.countDown();
194 super.handleCommand(message);
201 setupInMemorySnapshotStore();
203 final YangInstanceIdentifier path = TestModel.TEST_PATH;
204 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
205 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener, path),
206 "testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration-DataChangeListener");
208 final TestActorRef<Shard> shard = actorFactory.createTestActor(Props.create(Shard.class,
209 new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
210 "testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration");
212 final ShardTestKit testKit = new ShardTestKit(getSystem());
213 assertTrue("Got first ElectionTimeout", onFirstElectionTimeout.await(5, TimeUnit.SECONDS));
215 shard.tell(new RegisterDataTreeChangeListener(path, dclActor, false), testKit.getRef());
216 final RegisterDataTreeNotificationListenerReply reply = testKit.expectMsgClass(Duration.ofSeconds(5),
217 RegisterDataTreeNotificationListenerReply.class);
218 assertNotNull("getListenerRegistratioznPath", reply.getListenerRegistrationPath());
220 shard.tell(FindLeader.INSTANCE, testKit.getRef());
221 final FindLeaderReply findLeadeReply = testKit.expectMsgClass(Duration.ofSeconds(5), FindLeaderReply.class);
222 assertFalse("Expected the shard not to be the leader", findLeadeReply.getLeaderActor().isPresent());
224 onChangeListenerRegistered.countDown();
226 // TODO: investigate why we do not receive data chage events
227 listener.waitForChangeEvents();
231 public void testCreateTransaction() {
232 final ShardTestKit testKit = new ShardTestKit(getSystem());
233 final ActorRef shard = actorFactory.createActor(newShardProps(), "testCreateTransaction");
235 ShardTestKit.waitUntilLeader(shard);
237 shard.tell(new UpdateSchemaContext(TestModel.createTestContext()), testKit.getRef());
239 shard.tell(new CreateTransaction(nextTransactionId(), TransactionType.READ_ONLY.ordinal(),
240 DataStoreVersions.CURRENT_VERSION).toSerializable(), testKit.getRef());
242 final CreateTransactionReply reply = testKit.expectMsgClass(Duration.ofSeconds(3),
243 CreateTransactionReply.class);
245 final String path = reply.getTransactionPath().toString();
246 assertThat(path, containsString(String.format("/user/testCreateTransaction/shard-%s-%s:ShardTransactionTest@0:",
247 shardID.getShardName(), shardID.getMemberName().getName())));
251 public void testCreateTransactionOnChain() {
252 final ShardTestKit testKit = new ShardTestKit(getSystem());
253 final ActorRef shard = actorFactory.createActor(newShardProps(), "testCreateTransactionOnChain");
255 ShardTestKit.waitUntilLeader(shard);
257 shard.tell(new CreateTransaction(nextTransactionId(), TransactionType.READ_ONLY.ordinal(),
258 DataStoreVersions.CURRENT_VERSION).toSerializable(), testKit.getRef());
260 final CreateTransactionReply reply = testKit.expectMsgClass(Duration.ofSeconds(3),
261 CreateTransactionReply.class);
263 final String path = reply.getTransactionPath().toString();
264 assertThat(path, containsString(String.format(
265 "/user/testCreateTransactionOnChain/shard-%s-%s:ShardTransactionTest@0:",
266 shardID.getShardName(), shardID.getMemberName().getName())));
270 public void testPeerAddressResolved() {
271 final ShardTestKit testKit = 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, testKit.getRef());
282 final OnDemandRaftState state = testKit.expectMsgClass(OnDemandRaftState.class);
283 assertEquals("getPeerAddress", address, state.getPeerAddresses().get(peerID.toString()));
287 public void testApplySnapshot() throws Exception {
289 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps()
290 .withDispatcher(Dispatchers.DefaultDispatcherId()), "testApplySnapshot");
292 ShardTestKit.waitUntilLeader(shard);
294 final DataTree store = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
297 final ContainerNode container = Builders.containerBuilder()
298 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
299 .withChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
300 .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1))
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(new ShardSnapshotState(new MetadataShardDataTreeSnapshot(expected)),
310 Collections.emptyList(), 1, 2, 3, 4, -1, null, null);
312 shard.tell(new ApplySnapshot(snapshot), ActorRef.noSender());
314 final Stopwatch sw = Stopwatch.createStarted();
315 while (sw.elapsed(TimeUnit.SECONDS) <= 5) {
316 Uninterruptibles.sleepUninterruptibly(75, TimeUnit.MILLISECONDS);
319 assertEquals("Root node", expected, readStore(shard, root));
321 } catch (final AssertionError e) {
326 fail("Snapshot was not applied");
330 public void testApplyState() throws Exception {
331 final TestActorRef<Shard> shard = actorFactory.createTestActor(
332 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testApplyState");
334 ShardTestKit.waitUntilLeader(shard);
336 final DataTree store = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
339 final DataTreeModification writeMod = store.takeSnapshot().newModification();
340 final ContainerNode node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
341 writeMod.write(TestModel.TEST_PATH, node);
344 final TransactionIdentifier tx = nextTransactionId();
345 shard.underlyingActor().applyState(null, null, payloadForModification(store, writeMod, tx));
347 final Stopwatch sw = Stopwatch.createStarted();
348 while (sw.elapsed(TimeUnit.SECONDS) <= 5) {
349 Uninterruptibles.sleepUninterruptibly(75, TimeUnit.MILLISECONDS);
351 final NormalizedNode actual = readStore(shard, TestModel.TEST_PATH);
352 if (actual != null) {
353 assertEquals("Applied state", node, actual);
358 fail("State was not applied");
362 public void testDataTreeCandidateRecovery() throws Exception {
363 // Set up the InMemorySnapshotStore.
364 final DataTree source = setupInMemorySnapshotStore();
366 final DataTreeModification writeMod = source.takeSnapshot().newModification();
367 writeMod.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
369 InMemoryJournal.addEntry(shardID.toString(), 0, DUMMY_DATA);
371 // Set up the InMemoryJournal.
372 InMemoryJournal.addEntry(shardID.toString(), 1, new SimpleReplicatedLogEntry(0, 1,
373 payloadForModification(source, writeMod, nextTransactionId())));
375 final int nListEntries = 16;
376 final Set<Integer> listEntryKeys = new HashSet<>();
378 // Add some ModificationPayload entries
379 for (int i = 1; i <= nListEntries; i++) {
380 listEntryKeys.add(i);
382 final YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
383 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i).build();
385 final DataTreeModification mod = source.takeSnapshot().newModification();
386 mod.merge(path, ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i));
389 InMemoryJournal.addEntry(shardID.toString(), i + 1, new SimpleReplicatedLogEntry(i, 1,
390 payloadForModification(source, mod, nextTransactionId())));
393 InMemoryJournal.addEntry(shardID.toString(), nListEntries + 2,
394 new ApplyJournalEntries(nListEntries));
396 testRecovery(listEntryKeys, true);
400 @SuppressWarnings("checkstyle:IllegalCatch")
401 public void testConcurrentThreePhaseCommits() throws Exception {
402 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
403 final CountDownLatch commitLatch = new CountDownLatch(2);
405 final long timeoutSec = 5;
406 final Duration duration = Duration.ofSeconds(timeoutSec);
407 final Timeout timeout = Timeout.create(duration);
409 final TestActorRef<Shard> shard = actorFactory.createTestActor(
410 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
411 "testConcurrentThreePhaseCommits");
413 class OnFutureComplete extends OnComplete<Object> {
414 private final Class<?> expRespType;
416 OnFutureComplete(final Class<?> expRespType) {
417 this.expRespType = expRespType;
421 public void onComplete(final Throwable error, final Object resp) {
423 caughtEx.set(new AssertionError(getClass().getSimpleName() + " failure", error));
426 assertEquals("Commit response type", expRespType, resp.getClass());
428 } catch (final Exception e) {
434 void onSuccess(final Object resp) {
438 class OnCommitFutureComplete extends OnFutureComplete {
439 OnCommitFutureComplete() {
440 super(CommitTransactionReply.class);
444 public void onComplete(final Throwable error, final Object resp) {
445 super.onComplete(error, resp);
446 commitLatch.countDown();
450 class OnCanCommitFutureComplete extends OnFutureComplete {
451 private final TransactionIdentifier transactionID;
453 OnCanCommitFutureComplete(final TransactionIdentifier transactionID) {
454 super(CanCommitTransactionReply.class);
455 this.transactionID = transactionID;
459 void onSuccess(final Object resp) {
460 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(resp);
461 assertTrue("Can commit", canCommitReply.getCanCommit());
463 final Future<Object> commitFuture = Patterns.ask(shard,
464 new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), timeout);
465 commitFuture.onComplete(new OnCommitFutureComplete(), getSystem().dispatcher());
469 final ShardTestKit testKit = new ShardTestKit(getSystem());
470 ShardTestKit.waitUntilLeader(shard);
472 final TransactionIdentifier transactionID1 = nextTransactionId();
473 final TransactionIdentifier transactionID2 = nextTransactionId();
474 final TransactionIdentifier transactionID3 = nextTransactionId();
476 final Map<TransactionIdentifier, CapturingShardDataTreeCohort> cohortMap = setupCohortDecorator(
477 shard.underlyingActor(), transactionID1, transactionID2, transactionID3);
478 final CapturingShardDataTreeCohort cohort1 = cohortMap.get(transactionID1);
479 final CapturingShardDataTreeCohort cohort2 = cohortMap.get(transactionID2);
480 final CapturingShardDataTreeCohort cohort3 = cohortMap.get(transactionID3);
482 shard.tell(prepareBatchedModifications(transactionID1, TestModel.TEST_PATH,
483 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), testKit.getRef());
484 final ReadyTransactionReply readyReply = ReadyTransactionReply
485 .fromSerializable(testKit.expectMsgClass(duration, ReadyTransactionReply.class));
487 String pathSuffix = shard.path().toString().replaceFirst("akka://test", "");
488 assertThat(readyReply.getCohortPath(), endsWith(pathSuffix));
489 // Send the CanCommitTransaction message for the first Tx.
491 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
492 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
493 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
494 assertTrue("Can commit", canCommitReply.getCanCommit());
496 // Ready 2 more Tx's.
498 shard.tell(prepareBatchedModifications(transactionID2, TestModel.OUTER_LIST_PATH,
499 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false), testKit.getRef());
500 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
503 prepareBatchedModifications(transactionID3,
504 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
505 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
506 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), false), testKit.getRef());
507 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
509 // Send the CanCommitTransaction message for the next 2 Tx's.
510 // These should get queued and
511 // processed after the first Tx completes.
513 final Future<Object> canCommitFuture1 = Patterns.ask(shard,
514 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
516 final Future<Object> canCommitFuture2 = Patterns.ask(shard,
517 new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), timeout);
519 // Send the CommitTransaction message for the first Tx. After it
520 // completes, it should
521 // trigger the 2nd Tx to proceed which should in turn then
524 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
525 testKit.expectMsgClass(duration, CommitTransactionReply.class);
527 // Wait for the next 2 Tx's to complete.
529 canCommitFuture1.onComplete(new OnCanCommitFutureComplete(transactionID2), getSystem().dispatcher());
531 canCommitFuture2.onComplete(new OnCanCommitFutureComplete(transactionID3), getSystem().dispatcher());
533 final boolean done = commitLatch.await(timeoutSec, TimeUnit.SECONDS);
535 final Throwable t = caughtEx.get();
537 Throwables.propagateIfPossible(t, Exception.class);
538 throw new RuntimeException(t);
541 assertTrue("Commits complete", done);
543 // final InOrder inOrder = inOrder(cohort1.getCanCommit(), cohort1.getPreCommit(), cohort1.getCommit(),
544 // cohort2.getCanCommit(), cohort2.getPreCommit(), cohort2.getCommit(), cohort3.getCanCommit(),
545 // cohort3.getPreCommit(), cohort3.getCommit());
546 // inOrder.verify(cohort1.getCanCommit()).onSuccess(any(Void.class));
547 // inOrder.verify(cohort1.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
548 // inOrder.verify(cohort2.getCanCommit()).onSuccess(any(Void.class));
549 // inOrder.verify(cohort2.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
550 // inOrder.verify(cohort3.getCanCommit()).onSuccess(any(Void.class));
551 // inOrder.verify(cohort3.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
552 // inOrder.verify(cohort1.getCommit()).onSuccess(any(UnsignedLong.class));
553 // inOrder.verify(cohort2.getCommit()).onSuccess(any(UnsignedLong.class));
554 // inOrder.verify(cohort3.getCommit()).onSuccess(any(UnsignedLong.class));
556 // Verify data in the data store.
558 verifyOuterListEntry(shard, 1);
560 verifyLastApplied(shard, 3);
564 public void testBatchedModificationsWithNoCommitOnReady() {
565 final ShardTestKit testKit = new ShardTestKit(getSystem());
566 final TestActorRef<Shard> shard = actorFactory.createTestActor(
567 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
568 "testBatchedModificationsWithNoCommitOnReady");
570 ShardTestKit.waitUntilLeader(shard);
572 final TransactionIdentifier transactionID = nextTransactionId();
573 final Duration duration = Duration.ofSeconds(5);
575 // Send a BatchedModifications to start a transaction.
577 shard.tell(newBatchedModifications(transactionID, TestModel.TEST_PATH,
578 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false, false, 1), testKit.getRef());
579 testKit.expectMsgClass(duration, BatchedModificationsReply.class);
581 // Send a couple more BatchedModifications.
583 shard.tell(newBatchedModifications(transactionID, TestModel.OUTER_LIST_PATH,
584 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false, false, 2),
586 testKit.expectMsgClass(duration, BatchedModificationsReply.class);
588 shard.tell(newBatchedModifications(transactionID,
589 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
590 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
591 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), true, false, 3),
593 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
595 // Send the CanCommitTransaction message.
597 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
598 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
599 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
600 assertTrue("Can commit", canCommitReply.getCanCommit());
602 // Send the CommitTransaction message.
604 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
605 testKit.expectMsgClass(duration, CommitTransactionReply.class);
607 // Verify data in the data store.
609 verifyOuterListEntry(shard, 1);
613 public void testBatchedModificationsWithCommitOnReady() {
614 final ShardTestKit testKit = new ShardTestKit(getSystem());
615 final TestActorRef<Shard> shard = actorFactory.createTestActor(
616 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
617 "testBatchedModificationsWithCommitOnReady");
619 ShardTestKit.waitUntilLeader(shard);
621 final TransactionIdentifier transactionID = nextTransactionId();
622 final Duration duration = Duration.ofSeconds(5);
624 // Send a BatchedModifications to start a transaction.
626 shard.tell(newBatchedModifications(transactionID, TestModel.TEST_PATH,
627 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false, false, 1), testKit.getRef());
628 testKit.expectMsgClass(duration, BatchedModificationsReply.class);
630 // Send a couple more BatchedModifications.
632 shard.tell(newBatchedModifications(transactionID, TestModel.OUTER_LIST_PATH,
633 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false, false, 2),
635 testKit.expectMsgClass(duration, BatchedModificationsReply.class);
637 shard.tell(newBatchedModifications(transactionID,
638 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
639 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
640 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), true, true, 3),
643 testKit.expectMsgClass(duration, CommitTransactionReply.class);
645 // Verify data in the data store.
646 verifyOuterListEntry(shard, 1);
649 @Test(expected = IllegalStateException.class)
650 public void testBatchedModificationsReadyWithIncorrectTotalMessageCount() throws Exception {
651 final ShardTestKit testKit = new ShardTestKit(getSystem());
652 final TestActorRef<Shard> shard = actorFactory.createTestActor(
653 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
654 "testBatchedModificationsReadyWithIncorrectTotalMessageCount");
656 ShardTestKit.waitUntilLeader(shard);
658 final TransactionIdentifier transactionID = nextTransactionId();
659 final BatchedModifications batched = new BatchedModifications(transactionID,
660 DataStoreVersions.CURRENT_VERSION);
662 batched.setTotalMessagesSent(2);
664 shard.tell(batched, testKit.getRef());
666 final Failure failure = testKit.expectMsgClass(Duration.ofSeconds(5), Failure.class);
668 if (failure != null) {
669 Throwables.propagateIfPossible(failure.cause(), Exception.class);
670 throw new RuntimeException(failure.cause());
675 public void testBatchedModificationsWithOperationFailure() {
676 final ShardTestKit testKit = new ShardTestKit(getSystem());
677 final TestActorRef<Shard> shard = actorFactory.createTestActor(
678 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
679 "testBatchedModificationsWithOperationFailure");
681 ShardTestKit.waitUntilLeader(shard);
683 // Test merge with invalid data. An exception should occur when
684 // the merge is applied. Note that
685 // write will not validate the children for performance reasons.
687 final TransactionIdentifier transactionID = nextTransactionId();
689 final ContainerNode invalidData = Builders.containerBuilder()
690 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
691 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk"))
694 BatchedModifications batched = new BatchedModifications(transactionID, CURRENT_VERSION);
695 batched.addModification(new MergeModification(TestModel.TEST_PATH, invalidData));
696 shard.tell(batched, testKit.getRef());
697 Failure failure = testKit.expectMsgClass(Duration.ofSeconds(5), akka.actor.Status.Failure.class);
699 final Throwable cause = failure.cause();
701 batched = new BatchedModifications(transactionID, DataStoreVersions.CURRENT_VERSION);
703 batched.setTotalMessagesSent(2);
705 shard.tell(batched, testKit.getRef());
707 failure = testKit.expectMsgClass(Duration.ofSeconds(5), akka.actor.Status.Failure.class);
708 assertEquals("Failure cause", cause, failure.cause());
712 public void testBatchedModificationsOnTransactionChain() {
713 final ShardTestKit testKit = new ShardTestKit(getSystem());
714 final TestActorRef<Shard> shard = actorFactory.createTestActor(
715 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
716 "testBatchedModificationsOnTransactionChain");
718 ShardTestKit.waitUntilLeader(shard);
720 final LocalHistoryIdentifier historyId = nextHistoryId();
721 final TransactionIdentifier transactionID1 = new TransactionIdentifier(historyId, 0);
722 final TransactionIdentifier transactionID2 = new TransactionIdentifier(historyId, 1);
724 final Duration duration = Duration.ofSeconds(5);
726 // Send a BatchedModifications to start a chained write
727 // transaction and ready it.
729 final ContainerNode containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
730 final YangInstanceIdentifier path = TestModel.TEST_PATH;
731 shard.tell(newBatchedModifications(transactionID1, path, containerNode, true, false, 1), testKit.getRef());
732 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
734 // Create a read Tx on the same chain.
736 shard.tell(new CreateTransaction(transactionID2, TransactionType.READ_ONLY.ordinal(),
737 DataStoreVersions.CURRENT_VERSION).toSerializable(), testKit.getRef());
739 final CreateTransactionReply createReply = testKit.expectMsgClass(Duration.ofSeconds(3),
740 CreateTransactionReply.class);
742 getSystem().actorSelection(createReply.getTransactionPath())
743 .tell(new ReadData(path, DataStoreVersions.CURRENT_VERSION), testKit.getRef());
744 final ReadDataReply readReply = testKit.expectMsgClass(Duration.ofSeconds(3), ReadDataReply.class);
745 assertEquals("Read node", containerNode, readReply.getNormalizedNode());
747 // Commit the write transaction.
749 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
750 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
751 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
752 assertTrue("Can commit", canCommitReply.getCanCommit());
754 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
755 testKit.expectMsgClass(duration, CommitTransactionReply.class);
757 // Verify data in the data store.
759 final NormalizedNode actualNode = readStore(shard, path);
760 assertEquals("Stored node", containerNode, actualNode);
764 public void testOnBatchedModificationsWhenNotLeader() {
765 final AtomicBoolean overrideLeaderCalls = new AtomicBoolean();
766 final ShardTestKit testKit = new ShardTestKit(getSystem());
767 final Creator<Shard> creator = new Creator<>() {
768 private static final long serialVersionUID = 1L;
771 public Shard create() {
772 return new Shard(newShardBuilder()) {
774 protected boolean isLeader() {
775 return overrideLeaderCalls.get() ? false : super.isLeader();
779 public ActorSelection getLeader() {
780 return overrideLeaderCalls.get() ? getSystem().actorSelection(testKit.getRef().path())
787 final TestActorRef<Shard> shard = actorFactory.createTestActor(Props.create(Shard.class,
788 new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
789 "testOnBatchedModificationsWhenNotLeader");
791 ShardTestKit.waitUntilLeader(shard);
793 overrideLeaderCalls.set(true);
795 final BatchedModifications batched = new BatchedModifications(nextTransactionId(),
796 DataStoreVersions.CURRENT_VERSION);
798 shard.tell(batched, ActorRef.noSender());
800 testKit.expectMsgEquals(batched);
804 public void testTransactionMessagesWithNoLeader() {
805 final ShardTestKit testKit = new ShardTestKit(getSystem());
806 dataStoreContextBuilder.customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName())
807 .shardHeartbeatIntervalInMillis(50).shardElectionTimeoutFactor(1);
808 final TestActorRef<Shard> shard = actorFactory.createTestActor(
809 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
810 "testTransactionMessagesWithNoLeader");
812 testKit.waitUntilNoLeader(shard);
814 final TransactionIdentifier txId = nextTransactionId();
815 shard.tell(new BatchedModifications(txId, DataStoreVersions.CURRENT_VERSION), testKit.getRef());
816 Failure failure = testKit.expectMsgClass(Failure.class);
817 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
819 shard.tell(prepareForwardedReadyTransaction(shard, txId, TestModel.TEST_PATH,
820 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), testKit.getRef());
821 failure = testKit.expectMsgClass(Failure.class);
822 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
824 shard.tell(new ReadyLocalTransaction(txId, mock(DataTreeModification.class), true, Optional.empty()),
826 failure = testKit.expectMsgClass(Failure.class);
827 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
831 public void testReadyWithReadWriteImmediateCommit() {
832 testReadyWithImmediateCommit(true);
836 public void testReadyWithWriteOnlyImmediateCommit() {
837 testReadyWithImmediateCommit(false);
840 private void testReadyWithImmediateCommit(final boolean readWrite) {
841 final ShardTestKit testKit = new ShardTestKit(getSystem());
842 final TestActorRef<Shard> shard = actorFactory.createTestActor(
843 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
844 "testReadyWithImmediateCommit-" + readWrite);
846 ShardTestKit.waitUntilLeader(shard);
848 final TransactionIdentifier transactionID = nextTransactionId();
849 final NormalizedNode containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
851 shard.tell(prepareForwardedReadyTransaction(shard, transactionID, TestModel.TEST_PATH, containerNode, true),
854 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH, containerNode, true),
858 testKit.expectMsgClass(Duration.ofSeconds(5), CommitTransactionReply.class);
860 final NormalizedNode actualNode = readStore(shard, TestModel.TEST_PATH);
861 assertEquals(TestModel.TEST_QNAME.getLocalName(), containerNode, actualNode);
865 public void testReadyLocalTransactionWithImmediateCommit() {
866 final ShardTestKit testKit = new ShardTestKit(getSystem());
867 final TestActorRef<Shard> shard = actorFactory.createTestActor(
868 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
869 "testReadyLocalTransactionWithImmediateCommit");
871 ShardTestKit.waitUntilLeader(shard);
873 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
875 final DataTreeModification modification = dataStore.newModification();
877 final ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
878 new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
879 final MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
880 .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 42))
882 new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
884 final TransactionIdentifier txId = nextTransactionId();
885 modification.ready();
886 final ReadyLocalTransaction readyMessage =
887 new ReadyLocalTransaction(txId, modification, true, Optional.empty());
889 shard.tell(readyMessage, testKit.getRef());
891 testKit.expectMsgClass(CommitTransactionReply.class);
893 final NormalizedNode actualNode = readStore(shard, TestModel.OUTER_LIST_PATH);
894 assertEquals(TestModel.OUTER_LIST_QNAME.getLocalName(), mergeData, actualNode);
898 public void testReadyLocalTransactionWithThreePhaseCommit() {
899 final ShardTestKit testKit = new ShardTestKit(getSystem());
900 final TestActorRef<Shard> shard = actorFactory.createTestActor(
901 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
902 "testReadyLocalTransactionWithThreePhaseCommit");
904 ShardTestKit.waitUntilLeader(shard);
906 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
908 final DataTreeModification modification = dataStore.newModification();
910 final ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
911 new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
912 final MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
913 .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 42))
915 new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
917 final TransactionIdentifier txId = nextTransactionId();
918 modification.ready();
919 final ReadyLocalTransaction readyMessage =
920 new ReadyLocalTransaction(txId, modification, false, Optional.empty());
922 shard.tell(readyMessage, testKit.getRef());
924 testKit.expectMsgClass(ReadyTransactionReply.class);
926 // Send the CanCommitTransaction message.
928 shard.tell(new CanCommitTransaction(txId, CURRENT_VERSION).toSerializable(), testKit.getRef());
929 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
930 .fromSerializable(testKit.expectMsgClass(CanCommitTransactionReply.class));
931 assertTrue("Can commit", canCommitReply.getCanCommit());
933 // Send the CanCommitTransaction message.
935 shard.tell(new CommitTransaction(txId, CURRENT_VERSION).toSerializable(), testKit.getRef());
936 testKit.expectMsgClass(CommitTransactionReply.class);
938 final NormalizedNode actualNode = readStore(shard, TestModel.OUTER_LIST_PATH);
939 assertEquals(TestModel.OUTER_LIST_QNAME.getLocalName(), mergeData, actualNode);
943 public void testReadWriteCommitWithPersistenceDisabled() {
944 dataStoreContextBuilder.persistent(false);
945 final ShardTestKit testKit = new ShardTestKit(getSystem());
946 final TestActorRef<Shard> shard = actorFactory.createTestActor(
947 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
948 "testCommitWithPersistenceDisabled");
950 ShardTestKit.waitUntilLeader(shard);
952 // Setup a simulated transactions with a mock cohort.
954 final Duration duration = Duration.ofSeconds(5);
956 final TransactionIdentifier transactionID = nextTransactionId();
957 final NormalizedNode containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
958 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH, containerNode, false),
960 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
962 // Send the CanCommitTransaction message.
964 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
965 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
966 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
967 assertTrue("Can commit", canCommitReply.getCanCommit());
969 // Send the CanCommitTransaction message.
971 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
972 testKit.expectMsgClass(duration, CommitTransactionReply.class);
974 final NormalizedNode actualNode = readStore(shard, TestModel.TEST_PATH);
975 assertEquals(TestModel.TEST_QNAME.getLocalName(), containerNode, actualNode);
979 public void testReadWriteCommitWhenTransactionHasModifications() throws Exception {
980 testCommitWhenTransactionHasModifications(true);
984 public void testWriteOnlyCommitWhenTransactionHasModifications() throws Exception {
985 testCommitWhenTransactionHasModifications(false);
988 private void testCommitWhenTransactionHasModifications(final boolean readWrite) throws Exception {
989 final ShardTestKit testKit = new ShardTestKit(getSystem());
990 final DataTree dataTree = createDelegatingMockDataTree();
991 final TestActorRef<Shard> shard = actorFactory.createTestActor(
992 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
993 "testCommitWhenTransactionHasModifications-" + readWrite);
995 ShardTestKit.waitUntilLeader(shard);
997 final Duration duration = Duration.ofSeconds(5);
998 final TransactionIdentifier transactionID = nextTransactionId();
1001 shard.tell(prepareForwardedReadyTransaction(shard, transactionID, TestModel.TEST_PATH,
1002 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), testKit.getRef());
1004 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH,
1005 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), testKit.getRef());
1008 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1010 // Send the CanCommitTransaction message.
1012 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
1013 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
1014 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
1015 assertTrue("Can commit", canCommitReply.getCanCommit());
1017 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
1018 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1020 final InOrder inOrder = inOrder(dataTree);
1021 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1022 inOrder.verify(dataTree).prepare(any(DataTreeModification.class));
1023 inOrder.verify(dataTree).commit(any(DataTreeCandidate.class));
1025 // Purge request is scheduled as asynchronous, wait for two heartbeats to let it propagate into
1027 Thread.sleep(HEARTBEAT_MILLIS * 2);
1029 shard.tell(Shard.GET_SHARD_MBEAN_MESSAGE, testKit.getRef());
1030 final ShardStats shardStats = testKit.expectMsgClass(duration, ShardStats.class);
1032 // Use MBean for verification
1033 // Committed transaction count should increase as usual
1034 assertEquals(1, shardStats.getCommittedTransactionsCount());
1036 // Commit index should advance 1 to account for disabling metadata
1037 assertEquals(1, shardStats.getCommitIndex());
1041 public void testCommitPhaseFailure() throws Exception {
1042 final ShardTestKit testKit = new ShardTestKit(getSystem());
1043 final DataTree dataTree = createDelegatingMockDataTree();
1044 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1045 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1046 "testCommitPhaseFailure");
1048 ShardTestKit.waitUntilLeader(shard);
1050 final Duration duration = Duration.ofSeconds(5);
1051 final Timeout timeout = Timeout.create(duration);
1053 // Setup 2 simulated transactions with mock cohorts. The first
1057 doThrow(new RuntimeException("mock commit failure")).when(dataTree)
1058 .commit(any(DataTreeCandidate.class));
1060 final TransactionIdentifier transactionID1 = nextTransactionId();
1061 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1062 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1063 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1065 final TransactionIdentifier transactionID2 = nextTransactionId();
1066 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1067 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1068 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1070 // Send the CanCommitTransaction message for the first Tx.
1072 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1073 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
1074 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
1075 assertTrue("Can commit", canCommitReply.getCanCommit());
1077 // Send the CanCommitTransaction message for the 2nd Tx. This
1078 // should get queued and
1079 // processed after the first Tx completes.
1081 final Future<Object> canCommitFuture = Patterns.ask(shard,
1082 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1084 // Send the CommitTransaction message for the first Tx. This
1085 // should send back an error
1086 // and trigger the 2nd Tx to proceed.
1088 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1089 testKit.expectMsgClass(duration, akka.actor.Status.Failure.class);
1091 // Wait for the 2nd Tx to complete the canCommit phase.
1093 final CountDownLatch latch = new CountDownLatch(1);
1094 canCommitFuture.onComplete(new OnComplete<>() {
1096 public void onComplete(final Throwable failure, final Object resp) {
1099 }, getSystem().dispatcher());
1101 assertTrue("2nd CanCommit complete", latch.await(5, TimeUnit.SECONDS));
1103 final InOrder inOrder = inOrder(dataTree);
1104 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1105 inOrder.verify(dataTree).prepare(any(DataTreeModification.class));
1107 // FIXME: this invocation is done on the result of validate(). To test it, we need to make sure mock
1108 // validate performs wrapping and we capture that mock
1109 // inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1111 inOrder.verify(dataTree).commit(any(DataTreeCandidate.class));
1115 public void testPreCommitPhaseFailure() throws Exception {
1116 final ShardTestKit testKit = new ShardTestKit(getSystem());
1117 final DataTree dataTree = createDelegatingMockDataTree();
1118 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1119 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1120 "testPreCommitPhaseFailure");
1122 ShardTestKit.waitUntilLeader(shard);
1124 final Duration duration = Duration.ofSeconds(5);
1125 final Timeout timeout = Timeout.create(duration);
1127 doThrow(new RuntimeException("mock preCommit failure")).when(dataTree)
1128 .prepare(any(DataTreeModification.class));
1130 final TransactionIdentifier transactionID1 = nextTransactionId();
1131 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1132 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1133 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1135 final TransactionIdentifier transactionID2 = nextTransactionId();
1136 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1137 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1138 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1140 // Send the CanCommitTransaction message for the first Tx.
1142 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1143 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
1144 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
1145 assertTrue("Can commit", canCommitReply.getCanCommit());
1147 // Send the CanCommitTransaction message for the 2nd Tx. This
1148 // should get queued and
1149 // processed after the first Tx completes.
1151 final Future<Object> canCommitFuture = Patterns.ask(shard,
1152 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1154 // Send the CommitTransaction message for the first Tx. This
1155 // should send back an error
1156 // and trigger the 2nd Tx to proceed.
1158 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1159 testKit.expectMsgClass(duration, akka.actor.Status.Failure.class);
1161 // Wait for the 2nd Tx to complete the canCommit phase.
1163 final CountDownLatch latch = new CountDownLatch(1);
1164 canCommitFuture.onComplete(new OnComplete<>() {
1166 public void onComplete(final Throwable failure, final Object resp) {
1169 }, getSystem().dispatcher());
1171 assertTrue("2nd CanCommit complete", latch.await(5, TimeUnit.SECONDS));
1173 final InOrder inOrder = inOrder(dataTree);
1174 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1175 inOrder.verify(dataTree).prepare(any(DataTreeModification.class));
1176 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1180 public void testCanCommitPhaseFailure() throws Exception {
1181 final ShardTestKit testKit = new ShardTestKit(getSystem());
1182 final DataTree dataTree = createDelegatingMockDataTree();
1183 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1184 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1185 "testCanCommitPhaseFailure");
1187 ShardTestKit.waitUntilLeader(shard);
1189 final Duration duration = Duration.ofSeconds(5);
1190 final TransactionIdentifier transactionID1 = nextTransactionId();
1192 doThrow(new DataValidationFailedException(YangInstanceIdentifier.empty(), "mock canCommit failure"))
1193 .doNothing().when(dataTree).validate(any(DataTreeModification.class));
1195 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1196 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1197 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1199 // Send the CanCommitTransaction message.
1201 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1202 testKit.expectMsgClass(duration, akka.actor.Status.Failure.class);
1204 // Send another can commit to ensure the failed one got cleaned
1207 final TransactionIdentifier transactionID2 = nextTransactionId();
1208 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1209 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1210 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1212 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), testKit.getRef());
1213 final CanCommitTransactionReply reply = CanCommitTransactionReply
1214 .fromSerializable(testKit.expectMsgClass(CanCommitTransactionReply.class));
1215 assertTrue("getCanCommit", reply.getCanCommit());
1219 public void testImmediateCommitWithCanCommitPhaseFailure() throws Exception {
1220 testImmediateCommitWithCanCommitPhaseFailure(true);
1221 testImmediateCommitWithCanCommitPhaseFailure(false);
1224 private void testImmediateCommitWithCanCommitPhaseFailure(final boolean readWrite) throws Exception {
1225 final ShardTestKit testKit = new ShardTestKit(getSystem());
1226 final DataTree dataTree = createDelegatingMockDataTree();
1227 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1228 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1229 "testImmediateCommitWithCanCommitPhaseFailure-" + readWrite);
1231 ShardTestKit.waitUntilLeader(shard);
1233 doThrow(new DataValidationFailedException(YangInstanceIdentifier.empty(), "mock canCommit failure"))
1234 .doNothing().when(dataTree).validate(any(DataTreeModification.class));
1236 final Duration duration = Duration.ofSeconds(5);
1238 final TransactionIdentifier transactionID1 = nextTransactionId();
1241 shard.tell(prepareForwardedReadyTransaction(shard, transactionID1, TestModel.TEST_PATH,
1242 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), testKit.getRef());
1244 shard.tell(prepareBatchedModifications(transactionID1, TestModel.TEST_PATH,
1245 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), testKit.getRef());
1248 testKit.expectMsgClass(duration, akka.actor.Status.Failure.class);
1250 // Send another can commit to ensure the failed one got cleaned
1253 final TransactionIdentifier transactionID2 = nextTransactionId();
1255 shard.tell(prepareForwardedReadyTransaction(shard, transactionID2, TestModel.TEST_PATH,
1256 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), testKit.getRef());
1258 shard.tell(prepareBatchedModifications(transactionID2, TestModel.TEST_PATH,
1259 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), testKit.getRef());
1262 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1266 public void testAbortWithCommitPending() {
1267 final ShardTestKit testKit = new ShardTestKit(getSystem());
1268 final Creator<Shard> creator = () -> new Shard(newShardBuilder()) {
1270 void persistPayload(final Identifier id, final Payload payload, final boolean batchHint) {
1271 // Simulate an AbortTransaction message occurring during
1272 // replication, after
1273 // persisting and before finishing the commit to the
1276 doAbortTransaction(id, null);
1277 super.persistPayload(id, payload, batchHint);
1281 final TestActorRef<Shard> shard = actorFactory.createTestActor(Props.create(Shard.class,
1282 new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
1283 "testAbortWithCommitPending");
1285 ShardTestKit.waitUntilLeader(shard);
1287 final Duration duration = Duration.ofSeconds(5);
1289 final TransactionIdentifier transactionID = nextTransactionId();
1291 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH,
1292 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), testKit.getRef());
1293 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1295 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
1296 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1298 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
1299 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1301 final NormalizedNode node = readStore(shard, TestModel.TEST_PATH);
1303 // Since we're simulating an abort occurring during replication
1304 // and before finish commit,
1305 // the data should still get written to the in-memory store
1306 // since we've gotten past
1307 // canCommit and preCommit and persisted the data.
1308 assertNotNull(TestModel.TEST_QNAME.getLocalName() + " not found", node);
1312 public void testTransactionCommitTimeout() throws Exception {
1313 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1314 final ShardTestKit testKit = new ShardTestKit(getSystem());
1315 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1316 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1317 "testTransactionCommitTimeout");
1319 ShardTestKit.waitUntilLeader(shard);
1321 final Duration duration = Duration.ofSeconds(5);
1323 writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1324 writeToStore(shard, TestModel.OUTER_LIST_PATH,
1325 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1327 // Ready 2 Tx's - the first will timeout
1329 final TransactionIdentifier transactionID1 = nextTransactionId();
1331 prepareBatchedModifications(transactionID1,
1332 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1333 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
1334 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), false),
1336 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1338 final TransactionIdentifier transactionID2 = nextTransactionId();
1339 final YangInstanceIdentifier listNodePath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1340 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build();
1342 prepareBatchedModifications(transactionID2, listNodePath,
1343 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2), false), testKit.getRef());
1344 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1346 // canCommit 1st Tx. We don't send the commit so it should
1349 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1350 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1352 // canCommit the 2nd Tx - it should complete after the 1st Tx
1355 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), testKit.getRef());
1356 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1358 // Try to commit the 1st Tx - should fail as it's not the
1361 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1362 testKit.expectMsgClass(duration, akka.actor.Status.Failure.class);
1364 // Commit the 2nd Tx.
1366 shard.tell(new CommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), testKit.getRef());
1367 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1369 final NormalizedNode node = readStore(shard, listNodePath);
1370 assertNotNull(listNodePath + " not found", node);
1375 // public void testTransactionCommitQueueCapacityExceeded() throws Throwable {
1376 // dataStoreContextBuilder.shardTransactionCommitQueueCapacity(2);
1378 // new ShardTestKit(getSystem()) {{
1379 // final TestActorRef<Shard> shard = actorFactory.createTestActor(
1380 // newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1381 // "testTransactionCommitQueueCapacityExceeded");
1383 // waitUntilLeader(shard);
1385 // final FiniteDuration duration = duration("5 seconds");
1387 // final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1389 // final TransactionIdentifier transactionID1 = nextTransactionId();
1390 // final MutableCompositeModification modification1 = new MutableCompositeModification();
1391 // final ShardDataTreeCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
1392 // TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), transactionID1,
1395 // final TransactionIdentifier transactionID2 = nextTransactionId();
1396 // final MutableCompositeModification modification2 = new MutableCompositeModification();
1397 // final ShardDataTreeCohort cohort2 = setupMockWriteTransaction("cohort2", dataStore,
1398 // TestModel.OUTER_LIST_PATH,
1399 // ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), transactionID2,
1402 // final TransactionIdentifier transactionID3 = nextTransactionId();
1403 // final MutableCompositeModification modification3 = new MutableCompositeModification();
1404 // final ShardDataTreeCohort cohort3 = setupMockWriteTransaction("cohort3", dataStore,
1405 // TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), transactionID3,
1408 // // Ready the Tx's
1410 // shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort1, transactionID1,
1411 // modification1), getRef());
1412 // expectMsgClass(duration, ReadyTransactionReply.class);
1414 // shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort2, transactionID2,
1415 // modification2), getRef());
1416 // expectMsgClass(duration, ReadyTransactionReply.class);
1418 // // The 3rd Tx should exceed queue capacity and fail.
1420 // shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort3, transactionID3,
1421 // modification3), getRef());
1422 // expectMsgClass(duration, akka.actor.Status.Failure.class);
1424 // // canCommit 1st Tx.
1426 // shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1427 // expectMsgClass(duration, CanCommitTransactionReply.class);
1429 // // canCommit the 2nd Tx - it should get queued.
1431 // shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1433 // // canCommit the 3rd Tx - should exceed queue capacity and fail.
1435 // shard.tell(new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), getRef());
1436 // expectMsgClass(duration, akka.actor.Status.Failure.class);
1441 public void testTransactionCommitWithPriorExpiredCohortEntries() {
1442 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1443 final ShardTestKit testKit = new ShardTestKit(getSystem());
1444 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1445 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1446 "testTransactionCommitWithPriorExpiredCohortEntries");
1448 ShardTestKit.waitUntilLeader(shard);
1450 final Duration duration = Duration.ofSeconds(5);
1452 final TransactionIdentifier transactionID1 = nextTransactionId();
1453 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1454 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1455 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1457 final TransactionIdentifier transactionID2 = nextTransactionId();
1458 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1459 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1460 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1462 final TransactionIdentifier transactionID3 = nextTransactionId();
1463 shard.tell(newBatchedModifications(transactionID3, TestModel.TEST_PATH,
1464 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1465 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1467 // All Tx's are readied. We'll send canCommit for the last one
1468 // but not the others. The others
1469 // should expire from the queue and the last one should be
1472 shard.tell(new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), testKit.getRef());
1473 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1477 public void testTransactionCommitWithSubsequentExpiredCohortEntry() {
1478 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1479 final ShardTestKit testKit = new ShardTestKit(getSystem());
1480 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1481 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1482 "testTransactionCommitWithSubsequentExpiredCohortEntry");
1484 ShardTestKit.waitUntilLeader(shard);
1486 final Duration duration = Duration.ofSeconds(5);
1488 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1490 final TransactionIdentifier transactionID1 = nextTransactionId();
1491 shard.tell(prepareBatchedModifications(transactionID1, TestModel.TEST_PATH,
1492 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), testKit.getRef());
1493 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1495 // CanCommit the first Tx so it's the current in-progress Tx.
1497 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1498 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1500 // Ready the second Tx.
1502 final TransactionIdentifier transactionID2 = nextTransactionId();
1503 shard.tell(prepareBatchedModifications(transactionID2, TestModel.TEST_PATH,
1504 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), testKit.getRef());
1505 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1507 // Ready the third Tx.
1509 final TransactionIdentifier transactionID3 = nextTransactionId();
1510 final DataTreeModification modification3 = dataStore.newModification();
1511 new WriteModification(TestModel.TEST2_PATH, ImmutableNodes.containerNode(TestModel.TEST2_QNAME))
1512 .apply(modification3);
1513 modification3.ready();
1514 final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(transactionID3, modification3,
1515 true, Optional.empty());
1516 shard.tell(readyMessage, testKit.getRef());
1518 // Commit the first Tx. After completing, the second should
1519 // expire from the queue and the third
1522 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1523 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1525 // Expect commit reply from the third Tx.
1527 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1529 final NormalizedNode node = readStore(shard, TestModel.TEST2_PATH);
1530 assertNotNull(TestModel.TEST2_PATH + " not found", node);
1534 public void testCanCommitBeforeReadyFailure() {
1535 final ShardTestKit testKit = new ShardTestKit(getSystem());
1536 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1537 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1538 "testCanCommitBeforeReadyFailure");
1540 shard.tell(new CanCommitTransaction(nextTransactionId(), CURRENT_VERSION).toSerializable(), testKit.getRef());
1541 testKit.expectMsgClass(Duration.ofSeconds(5), akka.actor.Status.Failure.class);
1545 public void testAbortAfterCanCommit() throws Exception {
1546 final ShardTestKit testKit = new ShardTestKit(getSystem());
1547 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1548 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testAbortAfterCanCommit");
1550 ShardTestKit.waitUntilLeader(shard);
1552 final Duration duration = Duration.ofSeconds(5);
1553 final Timeout timeout = Timeout.create(duration);
1555 // Ready 2 transactions - the first one will be aborted.
1557 final TransactionIdentifier transactionID1 = nextTransactionId();
1558 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1559 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1560 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1562 final TransactionIdentifier transactionID2 = nextTransactionId();
1563 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1564 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1565 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1567 // Send the CanCommitTransaction message for the first Tx.
1569 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1570 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
1571 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
1572 assertTrue("Can commit", canCommitReply.getCanCommit());
1574 // Send the CanCommitTransaction message for the 2nd Tx. This
1575 // should get queued and
1576 // processed after the first Tx completes.
1578 final Future<Object> canCommitFuture = Patterns.ask(shard,
1579 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1581 // Send the AbortTransaction message for the first Tx. This
1582 // should trigger the 2nd
1585 shard.tell(new AbortTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1586 testKit.expectMsgClass(duration, AbortTransactionReply.class);
1588 // Wait for the 2nd Tx to complete the canCommit phase.
1590 canCommitReply = (CanCommitTransactionReply) Await.result(canCommitFuture,
1591 FiniteDuration.create(5, TimeUnit.SECONDS));
1592 assertTrue("Can commit", canCommitReply.getCanCommit());
1596 public void testAbortAfterReady() {
1597 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1598 final ShardTestKit testKit = new ShardTestKit(getSystem());
1599 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1600 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testAbortAfterReady");
1602 ShardTestKit.waitUntilLeader(shard);
1604 final Duration duration = Duration.ofSeconds(5);
1608 final TransactionIdentifier transactionID1 = nextTransactionId();
1609 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1610 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1611 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1613 // Send the AbortTransaction message.
1615 shard.tell(new AbortTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1616 testKit.expectMsgClass(duration, AbortTransactionReply.class);
1618 assertEquals("getPendingTxCommitQueueSize", 0, shard.underlyingActor().getPendingTxCommitQueueSize());
1620 // Now send CanCommitTransaction - should fail.
1622 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1623 final Throwable failure = testKit.expectMsgClass(duration, akka.actor.Status.Failure.class).cause();
1624 assertTrue("Failure type", failure instanceof IllegalStateException);
1626 // Ready and CanCommit another and verify success.
1628 final TransactionIdentifier transactionID2 = nextTransactionId();
1629 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1630 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1631 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1633 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), testKit.getRef());
1634 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1638 public void testAbortQueuedTransaction() {
1639 final ShardTestKit testKit = new ShardTestKit(getSystem());
1640 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1641 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testAbortAfterReady");
1643 ShardTestKit.waitUntilLeader(shard);
1645 final Duration duration = Duration.ofSeconds(5);
1649 final TransactionIdentifier transactionID1 = nextTransactionId();
1650 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1651 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1652 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1654 final TransactionIdentifier transactionID2 = nextTransactionId();
1655 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1656 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1657 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1659 final TransactionIdentifier transactionID3 = nextTransactionId();
1660 shard.tell(newBatchedModifications(transactionID3, TestModel.OUTER_LIST_PATH,
1661 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), true, false, 1), testKit.getRef());
1662 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1664 // Abort the second tx while it's queued.
1666 shard.tell(new AbortTransaction(transactionID2, CURRENT_VERSION).toSerializable(), testKit.getRef());
1667 testKit.expectMsgClass(duration, AbortTransactionReply.class);
1669 // Commit the other 2.
1671 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1672 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1674 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1675 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1677 shard.tell(new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), testKit.getRef());
1678 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1680 shard.tell(new CommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), testKit.getRef());
1681 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1683 assertEquals("getPendingTxCommitQueueSize", 0, shard.underlyingActor().getPendingTxCommitQueueSize());
1687 public void testCreateSnapshotWithNonPersistentData() throws Exception {
1688 testCreateSnapshot(false, "testCreateSnapshotWithNonPersistentData");
1692 public void testCreateSnapshot() throws Exception {
1693 testCreateSnapshot(true, "testCreateSnapshot");
1696 private void testCreateSnapshot(final boolean persistent, final String shardActorName) throws Exception {
1697 final AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(1));
1699 final AtomicReference<Object> savedSnapshot = new AtomicReference<>();
1700 class TestPersistentDataProvider extends DelegatingPersistentDataProvider {
1701 TestPersistentDataProvider(final DataPersistenceProvider delegate) {
1706 public void saveSnapshot(final Object obj) {
1707 savedSnapshot.set(obj);
1708 super.saveSnapshot(obj);
1712 dataStoreContextBuilder.persistent(persistent);
1714 final class TestShard extends Shard {
1716 TestShard(final AbstractBuilder<?, ?> builder) {
1718 setPersistence(new TestPersistentDataProvider(super.persistence()));
1722 public void handleCommand(final Object message) {
1723 super.handleCommand(message);
1725 // XXX: commit_snapshot equality check references RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT
1726 if (message instanceof SaveSnapshotSuccess || "commit_snapshot".equals(message.toString())) {
1727 latch.get().countDown();
1732 public RaftActorContext getRaftActorContext() {
1733 return super.getRaftActorContext();
1737 final Creator<Shard> creator = () -> new TestShard(newShardBuilder());
1739 final TestActorRef<Shard> shard = actorFactory.createTestActor(Props.create(Shard.class,
1740 new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()), shardActorName);
1742 ShardTestKit.waitUntilLeader(shard);
1743 writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1745 final NormalizedNode expectedRoot = readStore(shard, YangInstanceIdentifier.empty());
1747 // Trigger creation of a snapshot by ensuring
1748 final RaftActorContext raftActorContext = ((TestShard) shard.underlyingActor()).getRaftActorContext();
1749 raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
1750 awaitAndValidateSnapshot(latch, savedSnapshot, expectedRoot);
1752 raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
1753 awaitAndValidateSnapshot(latch, savedSnapshot, expectedRoot);
1756 private static void awaitAndValidateSnapshot(final AtomicReference<CountDownLatch> latch,
1757 final AtomicReference<Object> savedSnapshot, final NormalizedNode expectedRoot)
1758 throws InterruptedException {
1759 assertTrue("Snapshot saved", latch.get().await(5, TimeUnit.SECONDS));
1761 assertTrue("Invalid saved snapshot " + savedSnapshot.get(), savedSnapshot.get() instanceof Snapshot);
1763 verifySnapshot((Snapshot) savedSnapshot.get(), expectedRoot);
1765 latch.set(new CountDownLatch(1));
1766 savedSnapshot.set(null);
1769 private static void verifySnapshot(final Snapshot snapshot, final NormalizedNode expectedRoot) {
1770 final NormalizedNode actual = ((ShardSnapshotState)snapshot.getState()).getSnapshot().getRootNode().get();
1771 assertEquals("Root node", expectedRoot, actual);
1775 * This test simply verifies that the applySnapShot logic will work.
1778 public void testInMemoryDataTreeRestore() throws DataValidationFailedException {
1779 final DataTree store = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
1782 final DataTreeModification putTransaction = store.takeSnapshot().newModification();
1783 putTransaction.write(TestModel.TEST_PATH,
1784 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1785 commitTransaction(store, putTransaction);
1788 final NormalizedNode expected = readStore(store, YangInstanceIdentifier.empty());
1790 final DataTreeModification writeTransaction = store.takeSnapshot().newModification();
1792 writeTransaction.delete(YangInstanceIdentifier.empty());
1793 writeTransaction.write(YangInstanceIdentifier.empty(), expected);
1795 commitTransaction(store, writeTransaction);
1797 final NormalizedNode actual = readStore(store, YangInstanceIdentifier.empty());
1799 assertEquals(expected, actual);
1803 public void testRecoveryApplicable() {
1805 final DatastoreContext persistentContext = DatastoreContext.newBuilder()
1806 .shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(true).build();
1808 final Props persistentProps = Shard.builder().id(shardID).datastoreContext(persistentContext)
1809 .schemaContextProvider(() -> SCHEMA_CONTEXT).props();
1811 final DatastoreContext nonPersistentContext = DatastoreContext.newBuilder()
1812 .shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(false).build();
1814 final Props nonPersistentProps = Shard.builder().id(shardID).datastoreContext(nonPersistentContext)
1815 .schemaContextProvider(() -> SCHEMA_CONTEXT).props();
1817 final TestActorRef<Shard> shard1 = actorFactory.createTestActor(persistentProps, "testPersistence1");
1819 assertTrue("Recovery Applicable", shard1.underlyingActor().persistence().isRecoveryApplicable());
1821 final TestActorRef<Shard> shard2 = actorFactory.createTestActor(nonPersistentProps, "testPersistence2");
1823 assertFalse("Recovery Not Applicable", shard2.underlyingActor().persistence().isRecoveryApplicable());
1827 public void testOnDatastoreContext() {
1828 dataStoreContextBuilder.persistent(true);
1830 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps(), "testOnDatastoreContext");
1832 assertTrue("isRecoveryApplicable", shard.underlyingActor().persistence().isRecoveryApplicable());
1834 ShardTestKit.waitUntilLeader(shard);
1836 shard.tell(dataStoreContextBuilder.persistent(false).build(), ActorRef.noSender());
1838 assertFalse("isRecoveryApplicable", shard.underlyingActor().persistence().isRecoveryApplicable());
1840 shard.tell(dataStoreContextBuilder.persistent(true).build(), ActorRef.noSender());
1842 assertTrue("isRecoveryApplicable", shard.underlyingActor().persistence().isRecoveryApplicable());
1846 public void testRegisterRoleChangeListener() {
1847 final ShardTestKit testKit = new ShardTestKit(getSystem());
1848 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1849 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1850 "testRegisterRoleChangeListener");
1852 ShardTestKit.waitUntilLeader(shard);
1854 final ActorRef listener = getSystem().actorOf(MessageCollectorActor.props());
1856 shard.tell(new RegisterRoleChangeListener(), listener);
1858 MessageCollectorActor.expectFirstMatching(listener, RegisterRoleChangeListenerReply.class);
1860 ShardLeaderStateChanged leaderStateChanged = MessageCollectorActor.expectFirstMatching(listener,
1861 ShardLeaderStateChanged.class);
1862 assertTrue("getLocalShardDataTree present", leaderStateChanged.getLocalShardDataTree().isPresent());
1863 assertSame("getLocalShardDataTree", shard.underlyingActor().getDataStore().getDataTree(),
1864 leaderStateChanged.getLocalShardDataTree().get());
1866 MessageCollectorActor.clearMessages(listener);
1868 // Force a leader change
1870 shard.tell(new RequestVote(10000, "member2", 50, 50), testKit.getRef());
1872 leaderStateChanged = MessageCollectorActor.expectFirstMatching(listener, ShardLeaderStateChanged.class);
1873 assertFalse("getLocalShardDataTree present", leaderStateChanged.getLocalShardDataTree().isPresent());
1877 public void testFollowerInitialSyncStatus() {
1878 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1879 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1880 "testFollowerInitialSyncStatus");
1882 shard.underlyingActor().handleNonRaftCommand(new FollowerInitialSyncUpStatus(false,
1883 "member-1-shard-inventory-operational"));
1885 assertFalse(shard.underlyingActor().getShardMBean().getFollowerInitialSyncStatus());
1887 shard.underlyingActor().handleNonRaftCommand(new FollowerInitialSyncUpStatus(true,
1888 "member-1-shard-inventory-operational"));
1890 assertTrue(shard.underlyingActor().getShardMBean().getFollowerInitialSyncStatus());
1894 public void testClusteredDataTreeChangeListenerWithDelayedRegistration() throws Exception {
1895 final ShardTestKit testKit = new ShardTestKit(getSystem());
1896 final String testName = "testClusteredDataTreeChangeListenerWithDelayedRegistration";
1897 dataStoreContextBuilder.shardElectionTimeoutFactor(1000)
1898 .customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
1900 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
1901 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener,
1902 TestModel.TEST_PATH), actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
1904 setupInMemorySnapshotStore();
1906 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1907 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1908 actorFactory.generateActorId(testName + "-shard"));
1910 testKit.waitUntilNoLeader(shard);
1912 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), testKit.getRef());
1913 final RegisterDataTreeNotificationListenerReply reply = testKit.expectMsgClass(Duration.ofSeconds(5),
1914 RegisterDataTreeNotificationListenerReply.class);
1915 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
1917 shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build())
1918 .customRaftPolicyImplementation(null).build(), ActorRef.noSender());
1920 listener.waitForChangeEvents();
1924 public void testClusteredDataTreeChangeListenerWithDelayedRegistrationClosed() throws Exception {
1925 final ShardTestKit testKit = new ShardTestKit(getSystem());
1926 final String testName = "testClusteredDataTreeChangeListenerWithDelayedRegistrationClosed";
1927 dataStoreContextBuilder.shardElectionTimeoutFactor(1000)
1928 .customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
1930 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(0);
1931 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener,
1932 TestModel.TEST_PATH), actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
1934 setupInMemorySnapshotStore();
1936 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1937 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1938 actorFactory.generateActorId(testName + "-shard"));
1940 testKit.waitUntilNoLeader(shard);
1942 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), testKit.getRef());
1943 final RegisterDataTreeNotificationListenerReply reply = testKit.expectMsgClass(Duration.ofSeconds(5),
1944 RegisterDataTreeNotificationListenerReply.class);
1945 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
1947 final ActorSelection regActor = getSystem().actorSelection(reply.getListenerRegistrationPath());
1948 regActor.tell(CloseDataTreeNotificationListenerRegistration.getInstance(), testKit.getRef());
1949 testKit.expectMsgClass(CloseDataTreeNotificationListenerRegistrationReply.class);
1951 shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build())
1952 .customRaftPolicyImplementation(null).build(), ActorRef.noSender());
1954 listener.expectNoMoreChanges("Received unexpected change after close");
1958 public void testClusteredDataTreeChangeListenerRegistration() throws Exception {
1959 final ShardTestKit testKit = new ShardTestKit(getSystem());
1960 final String testName = "testClusteredDataTreeChangeListenerRegistration";
1961 final ShardIdentifier followerShardID = ShardIdentifier.create("inventory",
1962 MemberName.forName(actorFactory.generateActorId(testName + "-follower")), "config");
1964 final ShardIdentifier leaderShardID = ShardIdentifier.create("inventory",
1965 MemberName.forName(actorFactory.generateActorId(testName + "-leader")), "config");
1967 final TestActorRef<Shard> followerShard = actorFactory
1968 .createTestActor(Shard.builder().id(followerShardID)
1969 .datastoreContext(dataStoreContextBuilder.shardElectionTimeoutFactor(1000).build())
1970 .peerAddresses(Collections.singletonMap(leaderShardID.toString(),
1971 "akka://test/user/" + leaderShardID.toString()))
1972 .schemaContextProvider(() -> SCHEMA_CONTEXT).props()
1973 .withDispatcher(Dispatchers.DefaultDispatcherId()), followerShardID.toString());
1975 final TestActorRef<Shard> leaderShard = actorFactory
1976 .createTestActor(Shard.builder().id(leaderShardID).datastoreContext(newDatastoreContext())
1977 .peerAddresses(Collections.singletonMap(followerShardID.toString(),
1978 "akka://test/user/" + followerShardID.toString()))
1979 .schemaContextProvider(() -> SCHEMA_CONTEXT).props()
1980 .withDispatcher(Dispatchers.DefaultDispatcherId()), leaderShardID.toString());
1982 leaderShard.tell(TimeoutNow.INSTANCE, ActorRef.noSender());
1983 final String leaderPath = ShardTestKit.waitUntilLeader(followerShard);
1984 assertEquals("Shard leader path", leaderShard.path().toString(), leaderPath);
1986 final YangInstanceIdentifier path = TestModel.TEST_PATH;
1987 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
1988 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener, path),
1989 actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
1991 followerShard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), testKit.getRef());
1992 final RegisterDataTreeNotificationListenerReply reply = testKit.expectMsgClass(Duration.ofSeconds(5),
1993 RegisterDataTreeNotificationListenerReply.class);
1994 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
1996 writeToStore(followerShard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1998 listener.waitForChangeEvents();
2002 public void testServerRemoved() {
2003 final TestActorRef<MessageCollectorActor> parent = actorFactory.createTestActor(MessageCollectorActor.props()
2004 .withDispatcher(Dispatchers.DefaultDispatcherId()));
2006 final ActorRef shard = parent.underlyingActor().context().actorOf(
2007 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
2008 "testServerRemoved");
2010 shard.tell(new ServerRemoved("test"), ActorRef.noSender());
2012 MessageCollectorActor.expectFirstMatching(parent, ServerRemoved.class);