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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.doThrow;
18 import static org.mockito.Mockito.inOrder;
19 import static org.mockito.Mockito.mock;
20 import static org.opendaylight.controller.cluster.datastore.DataStoreVersions.CURRENT_VERSION;
22 import akka.actor.ActorRef;
23 import akka.actor.ActorSelection;
24 import akka.actor.Props;
25 import akka.actor.Status.Failure;
26 import akka.dispatch.Dispatchers;
27 import akka.dispatch.OnComplete;
28 import akka.japi.Creator;
29 import akka.pattern.Patterns;
30 import akka.persistence.SaveSnapshotSuccess;
31 import akka.testkit.TestActorRef;
32 import akka.util.Timeout;
33 import com.google.common.base.Stopwatch;
34 import com.google.common.base.Throwables;
35 import com.google.common.util.concurrent.Uninterruptibles;
36 import java.time.Duration;
37 import java.util.Collections;
38 import java.util.HashSet;
40 import java.util.Optional;
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.yangtools.concepts.Identifier;
106 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
107 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
108 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
109 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
110 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
111 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
112 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
113 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
114 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
115 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
116 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
117 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
118 import scala.concurrent.Await;
119 import scala.concurrent.Future;
120 import scala.concurrent.duration.FiniteDuration;
122 public class ShardTest extends AbstractShardTest {
123 private static final String DUMMY_DATA = "Dummy data as snapshot sequence number is set to 0 in "
124 + "InMemorySnapshotStore and journal recovery seq number will start from 1";
127 public void testRegisterDataTreeChangeListener() throws Exception {
128 final ShardTestKit testKit = new ShardTestKit(getSystem());
129 final TestActorRef<Shard> shard = actorFactory.createTestActor(
130 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
131 "testRegisterDataTreeChangeListener");
133 ShardTestKit.waitUntilLeader(shard);
135 shard.tell(new UpdateSchemaContext(SchemaContextHelper.full()), ActorRef.noSender());
137 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
138 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener,
139 TestModel.TEST_PATH), "testRegisterDataTreeChangeListener-DataTreeChangeListener");
141 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, false), testKit.getRef());
143 final RegisterDataTreeNotificationListenerReply reply = testKit.expectMsgClass(Duration.ofSeconds(3),
144 RegisterDataTreeNotificationListenerReply.class);
145 final String replyPath = reply.getListenerRegistrationPath().toString();
146 assertTrue("Incorrect reply path: " + replyPath,
147 replyPath.matches("akka:\\/\\/test\\/user\\/testRegisterDataTreeChangeListener\\/\\$.*"));
149 final YangInstanceIdentifier path = TestModel.TEST_PATH;
150 writeToStore(shard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
152 listener.waitForChangeEvents();
153 listener.verifyOnInitialDataEvent();
155 final MockDataTreeChangeListener listener2 = new MockDataTreeChangeListener(1);
156 final ActorRef dclActor2 = actorFactory.createActor(DataTreeChangeListenerActor.props(listener2,
157 TestModel.TEST_PATH), "testRegisterDataTreeChangeListener-DataTreeChangeListener2");
159 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor2, false), testKit.getRef());
161 testKit.expectMsgClass(Duration.ofSeconds(3), RegisterDataTreeNotificationListenerReply.class);
163 listener2.waitForChangeEvents();
164 listener2.verifyNoOnInitialDataEvent();
167 @SuppressWarnings("serial")
169 public void testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration() throws Exception {
170 final CountDownLatch onFirstElectionTimeout = new CountDownLatch(1);
171 final CountDownLatch onChangeListenerRegistered = new CountDownLatch(1);
172 final Creator<Shard> creator = new Creator<Shard>() {
173 boolean firstElectionTimeout = true;
176 public Shard create() {
177 return new Shard(newShardBuilder()) {
179 public void handleCommand(final Object message) {
180 if (message instanceof ElectionTimeout && firstElectionTimeout) {
181 firstElectionTimeout = false;
182 final ActorRef self = getSelf();
184 Uninterruptibles.awaitUninterruptibly(
185 onChangeListenerRegistered, 5, TimeUnit.SECONDS);
186 self.tell(message, self);
189 onFirstElectionTimeout.countDown();
191 super.handleCommand(message);
198 setupInMemorySnapshotStore();
200 final YangInstanceIdentifier path = TestModel.TEST_PATH;
201 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
202 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener, path),
203 "testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration-DataChangeListener");
205 final TestActorRef<Shard> shard = actorFactory.createTestActor(
206 Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
207 "testDataTreeChangeListenerNotifiedWhenNotTheLeaderOnRegistration");
209 final ShardTestKit testKit = new ShardTestKit(getSystem());
210 assertTrue("Got first ElectionTimeout", onFirstElectionTimeout.await(5, TimeUnit.SECONDS));
212 shard.tell(new RegisterDataTreeChangeListener(path, dclActor, false), testKit.getRef());
213 final RegisterDataTreeNotificationListenerReply reply = testKit.expectMsgClass(Duration.ofSeconds(5),
214 RegisterDataTreeNotificationListenerReply.class);
215 assertNotNull("getListenerRegistratioznPath", reply.getListenerRegistrationPath());
217 shard.tell(FindLeader.INSTANCE, testKit.getRef());
218 final FindLeaderReply findLeadeReply = testKit.expectMsgClass(Duration.ofSeconds(5), FindLeaderReply.class);
219 assertFalse("Expected the shard not to be the leader", findLeadeReply.getLeaderActor().isPresent());
221 onChangeListenerRegistered.countDown();
223 // TODO: investigate why we do not receive data chage events
224 listener.waitForChangeEvents();
228 public void testCreateTransaction() {
229 final ShardTestKit testKit = new ShardTestKit(getSystem());
230 final ActorRef shard = actorFactory.createActor(newShardProps(), "testCreateTransaction");
232 ShardTestKit.waitUntilLeader(shard);
234 shard.tell(new UpdateSchemaContext(TestModel.createTestContext()), testKit.getRef());
236 shard.tell(new CreateTransaction(nextTransactionId(), TransactionType.READ_ONLY.ordinal(),
237 DataStoreVersions.CURRENT_VERSION).toSerializable(), testKit.getRef());
239 final CreateTransactionReply reply = testKit.expectMsgClass(Duration.ofSeconds(3),
240 CreateTransactionReply.class);
242 final String path = reply.getTransactionPath().toString();
243 assertTrue("Unexpected transaction path " + path, path.startsWith(String.format(
244 "akka://test/user/testCreateTransaction/shard-%s-%s:ShardTransactionTest@0:",
245 shardID.getShardName(), shardID.getMemberName().getName())));
249 public void testCreateTransactionOnChain() {
250 final ShardTestKit testKit = new ShardTestKit(getSystem());
251 final ActorRef shard = actorFactory.createActor(newShardProps(), "testCreateTransactionOnChain");
253 ShardTestKit.waitUntilLeader(shard);
255 shard.tell(new CreateTransaction(nextTransactionId(), TransactionType.READ_ONLY.ordinal(),
256 DataStoreVersions.CURRENT_VERSION).toSerializable(), testKit.getRef());
258 final CreateTransactionReply reply = testKit.expectMsgClass(Duration.ofSeconds(3),
259 CreateTransactionReply.class);
261 final String path = reply.getTransactionPath().toString();
262 assertTrue("Unexpected transaction path " + path, path.startsWith(String.format(
263 "akka://test/user/testCreateTransactionOnChain/shard-%s-%s:ShardTransactionTest@0:",
264 shardID.getShardName(), shardID.getMemberName().getName())));
268 public void testPeerAddressResolved() {
269 final ShardTestKit testKit = new ShardTestKit(getSystem());
270 final ShardIdentifier peerID = ShardIdentifier.create("inventory", MemberName.forName("member-2"),
272 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardBuilder()
273 .peerAddresses(Collections.<String, String>singletonMap(peerID.toString(), null))
274 .props().withDispatcher(Dispatchers.DefaultDispatcherId()), "testPeerAddressResolved");
276 final String address = "akka://foobar";
277 shard.tell(new PeerAddressResolved(peerID.toString(), address), ActorRef.noSender());
279 shard.tell(GetOnDemandRaftState.INSTANCE, testKit.getRef());
280 final OnDemandRaftState state = testKit.expectMsgClass(OnDemandRaftState.class);
281 assertEquals("getPeerAddress", address, state.getPeerAddresses().get(peerID.toString()));
285 public void testApplySnapshot() throws Exception {
287 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps()
288 .withDispatcher(Dispatchers.DefaultDispatcherId()), "testApplySnapshot");
290 ShardTestKit.waitUntilLeader(shard);
292 final DataTree store = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
295 final ContainerNode container = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
296 new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
297 .withChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).addChild(
298 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1)).build()).build();
300 writeToStore(store, TestModel.TEST_PATH, container);
302 final YangInstanceIdentifier root = YangInstanceIdentifier.EMPTY;
303 final NormalizedNode<?,?> expected = readStore(store, root);
305 final Snapshot snapshot = Snapshot.create(new ShardSnapshotState(new MetadataShardDataTreeSnapshot(expected)),
306 Collections.emptyList(), 1, 2, 3, 4, -1, null, null);
308 shard.tell(new ApplySnapshot(snapshot), ActorRef.noSender());
310 final Stopwatch sw = Stopwatch.createStarted();
311 while (sw.elapsed(TimeUnit.SECONDS) <= 5) {
312 Uninterruptibles.sleepUninterruptibly(75, TimeUnit.MILLISECONDS);
315 assertEquals("Root node", expected, readStore(shard, root));
317 } catch (final AssertionError e) {
322 fail("Snapshot was not applied");
326 public void testApplyState() throws Exception {
327 final TestActorRef<Shard> shard = actorFactory.createTestActor(
328 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testApplyState");
330 ShardTestKit.waitUntilLeader(shard);
332 final DataTree store = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
335 final DataTreeModification writeMod = store.takeSnapshot().newModification();
336 final ContainerNode node = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
337 writeMod.write(TestModel.TEST_PATH, node);
340 final TransactionIdentifier tx = nextTransactionId();
341 shard.underlyingActor().applyState(null, null, payloadForModification(store, writeMod, tx));
343 final Stopwatch sw = Stopwatch.createStarted();
344 while (sw.elapsed(TimeUnit.SECONDS) <= 5) {
345 Uninterruptibles.sleepUninterruptibly(75, TimeUnit.MILLISECONDS);
347 final NormalizedNode<?,?> actual = readStore(shard, TestModel.TEST_PATH);
348 if (actual != null) {
349 assertEquals("Applied state", node, actual);
354 fail("State was not applied");
358 public void testDataTreeCandidateRecovery() throws Exception {
359 // Set up the InMemorySnapshotStore.
360 final DataTree source = setupInMemorySnapshotStore();
362 final DataTreeModification writeMod = source.takeSnapshot().newModification();
363 writeMod.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
365 InMemoryJournal.addEntry(shardID.toString(), 0, DUMMY_DATA);
367 // Set up the InMemoryJournal.
368 InMemoryJournal.addEntry(shardID.toString(), 1, new SimpleReplicatedLogEntry(0, 1,
369 payloadForModification(source, writeMod, nextTransactionId())));
371 final int nListEntries = 16;
372 final Set<Integer> listEntryKeys = new HashSet<>();
374 // Add some ModificationPayload entries
375 for (int i = 1; i <= nListEntries; i++) {
376 listEntryKeys.add(Integer.valueOf(i));
378 final YangInstanceIdentifier path = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
379 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i).build();
381 final DataTreeModification mod = source.takeSnapshot().newModification();
382 mod.merge(path, ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i));
385 InMemoryJournal.addEntry(shardID.toString(), i + 1, new SimpleReplicatedLogEntry(i, 1,
386 payloadForModification(source, mod, nextTransactionId())));
389 InMemoryJournal.addEntry(shardID.toString(), nListEntries + 2,
390 new ApplyJournalEntries(nListEntries));
392 testRecovery(listEntryKeys);
396 @SuppressWarnings("checkstyle:IllegalCatch")
397 public void testConcurrentThreePhaseCommits() throws Exception {
398 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
399 final CountDownLatch commitLatch = new CountDownLatch(2);
401 final long timeoutSec = 5;
402 final Duration duration = Duration.ofSeconds(timeoutSec);
403 final Timeout timeout = Timeout.create(duration);
405 final TestActorRef<Shard> shard = actorFactory.createTestActor(
406 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
407 "testConcurrentThreePhaseCommits");
409 class OnFutureComplete extends OnComplete<Object> {
410 private final Class<?> expRespType;
412 OnFutureComplete(final Class<?> expRespType) {
413 this.expRespType = expRespType;
417 public void onComplete(final Throwable error, final Object resp) {
419 caughtEx.set(new AssertionError(getClass().getSimpleName() + " failure", error));
422 assertEquals("Commit response type", expRespType, resp.getClass());
424 } catch (final Exception e) {
430 void onSuccess(final Object resp) {
434 class OnCommitFutureComplete extends OnFutureComplete {
435 OnCommitFutureComplete() {
436 super(CommitTransactionReply.class);
440 public void onComplete(final Throwable error, final Object resp) {
441 super.onComplete(error, resp);
442 commitLatch.countDown();
446 class OnCanCommitFutureComplete extends OnFutureComplete {
447 private final TransactionIdentifier transactionID;
449 OnCanCommitFutureComplete(final TransactionIdentifier transactionID) {
450 super(CanCommitTransactionReply.class);
451 this.transactionID = transactionID;
455 void onSuccess(final Object resp) {
456 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(resp);
457 assertTrue("Can commit", canCommitReply.getCanCommit());
459 final Future<Object> commitFuture = Patterns.ask(shard,
460 new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), timeout);
461 commitFuture.onComplete(new OnCommitFutureComplete(), getSystem().dispatcher());
465 final ShardTestKit testKit = new ShardTestKit(getSystem());
466 ShardTestKit.waitUntilLeader(shard);
468 final TransactionIdentifier transactionID1 = nextTransactionId();
469 final TransactionIdentifier transactionID2 = nextTransactionId();
470 final TransactionIdentifier transactionID3 = nextTransactionId();
472 final Map<TransactionIdentifier, CapturingShardDataTreeCohort> cohortMap = setupCohortDecorator(
473 shard.underlyingActor(), transactionID1, transactionID2, transactionID3);
474 final CapturingShardDataTreeCohort cohort1 = cohortMap.get(transactionID1);
475 final CapturingShardDataTreeCohort cohort2 = cohortMap.get(transactionID2);
476 final CapturingShardDataTreeCohort cohort3 = cohortMap.get(transactionID3);
478 shard.tell(prepareBatchedModifications(transactionID1, TestModel.TEST_PATH,
479 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), testKit.getRef());
480 final ReadyTransactionReply readyReply = ReadyTransactionReply
481 .fromSerializable(testKit.expectMsgClass(duration, ReadyTransactionReply.class));
482 assertEquals("Cohort path", shard.path().toString(), readyReply.getCohortPath());
483 // Send the CanCommitTransaction message for the first Tx.
485 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
486 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
487 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
488 assertTrue("Can commit", canCommitReply.getCanCommit());
490 // Ready 2 more Tx's.
492 shard.tell(prepareBatchedModifications(transactionID2, TestModel.OUTER_LIST_PATH,
493 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false), testKit.getRef());
494 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
497 prepareBatchedModifications(transactionID3,
498 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
499 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
500 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), false), testKit.getRef());
501 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
503 // Send the CanCommitTransaction message for the next 2 Tx's.
504 // These should get queued and
505 // processed after the first Tx completes.
507 final Future<Object> canCommitFuture1 = Patterns.ask(shard,
508 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
510 final Future<Object> canCommitFuture2 = Patterns.ask(shard,
511 new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), timeout);
513 // Send the CommitTransaction message for the first Tx. After it
514 // completes, it should
515 // trigger the 2nd Tx to proceed which should in turn then
518 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
519 testKit.expectMsgClass(duration, CommitTransactionReply.class);
521 // Wait for the next 2 Tx's to complete.
523 canCommitFuture1.onComplete(new OnCanCommitFutureComplete(transactionID2), getSystem().dispatcher());
525 canCommitFuture2.onComplete(new OnCanCommitFutureComplete(transactionID3), getSystem().dispatcher());
527 final boolean done = commitLatch.await(timeoutSec, TimeUnit.SECONDS);
529 final Throwable t = caughtEx.get();
531 Throwables.propagateIfPossible(t, Exception.class);
532 throw new RuntimeException(t);
535 assertTrue("Commits complete", done);
537 // final InOrder inOrder = inOrder(cohort1.getCanCommit(), cohort1.getPreCommit(), cohort1.getCommit(),
538 // cohort2.getCanCommit(), cohort2.getPreCommit(), cohort2.getCommit(), cohort3.getCanCommit(),
539 // cohort3.getPreCommit(), cohort3.getCommit());
540 // inOrder.verify(cohort1.getCanCommit()).onSuccess(any(Void.class));
541 // inOrder.verify(cohort1.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
542 // inOrder.verify(cohort2.getCanCommit()).onSuccess(any(Void.class));
543 // inOrder.verify(cohort2.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
544 // inOrder.verify(cohort3.getCanCommit()).onSuccess(any(Void.class));
545 // inOrder.verify(cohort3.getPreCommit()).onSuccess(any(DataTreeCandidate.class));
546 // inOrder.verify(cohort1.getCommit()).onSuccess(any(UnsignedLong.class));
547 // inOrder.verify(cohort2.getCommit()).onSuccess(any(UnsignedLong.class));
548 // inOrder.verify(cohort3.getCommit()).onSuccess(any(UnsignedLong.class));
550 // Verify data in the data store.
552 verifyOuterListEntry(shard, 1);
554 verifyLastApplied(shard, 3);
558 public void testBatchedModificationsWithNoCommitOnReady() {
559 final ShardTestKit testKit = new ShardTestKit(getSystem());
560 final TestActorRef<Shard> shard = actorFactory.createTestActor(
561 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
562 "testBatchedModificationsWithNoCommitOnReady");
564 ShardTestKit.waitUntilLeader(shard);
566 final TransactionIdentifier transactionID = nextTransactionId();
567 final Duration duration = Duration.ofSeconds(5);
569 // Send a BatchedModifications to start a transaction.
571 shard.tell(newBatchedModifications(transactionID, TestModel.TEST_PATH,
572 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false, false, 1), testKit.getRef());
573 testKit.expectMsgClass(duration, BatchedModificationsReply.class);
575 // Send a couple more BatchedModifications.
577 shard.tell(newBatchedModifications(transactionID, TestModel.OUTER_LIST_PATH,
578 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false, false, 2),
580 testKit.expectMsgClass(duration, BatchedModificationsReply.class);
582 shard.tell(newBatchedModifications(transactionID,
583 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
584 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
585 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), true, false, 3),
587 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
589 // Send the CanCommitTransaction message.
591 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
592 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
593 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
594 assertTrue("Can commit", canCommitReply.getCanCommit());
596 // Send the CommitTransaction message.
598 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
599 testKit.expectMsgClass(duration, CommitTransactionReply.class);
601 // Verify data in the data store.
603 verifyOuterListEntry(shard, 1);
607 public void testBatchedModificationsWithCommitOnReady() {
608 final ShardTestKit testKit = new ShardTestKit(getSystem());
609 final TestActorRef<Shard> shard = actorFactory.createTestActor(
610 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
611 "testBatchedModificationsWithCommitOnReady");
613 ShardTestKit.waitUntilLeader(shard);
615 final TransactionIdentifier transactionID = nextTransactionId();
616 final Duration duration = Duration.ofSeconds(5);
618 // Send a BatchedModifications to start a transaction.
620 shard.tell(newBatchedModifications(transactionID, TestModel.TEST_PATH,
621 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false, false, 1), testKit.getRef());
622 testKit.expectMsgClass(duration, BatchedModificationsReply.class);
624 // Send a couple more BatchedModifications.
626 shard.tell(newBatchedModifications(transactionID, TestModel.OUTER_LIST_PATH,
627 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), false, false, 2),
629 testKit.expectMsgClass(duration, BatchedModificationsReply.class);
631 shard.tell(newBatchedModifications(transactionID,
632 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
633 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
634 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), true, true, 3),
637 testKit.expectMsgClass(duration, CommitTransactionReply.class);
639 // Verify data in the data store.
640 verifyOuterListEntry(shard, 1);
643 @Test(expected = IllegalStateException.class)
644 public void testBatchedModificationsReadyWithIncorrectTotalMessageCount() throws Exception {
645 final ShardTestKit testKit = new ShardTestKit(getSystem());
646 final TestActorRef<Shard> shard = actorFactory.createTestActor(
647 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
648 "testBatchedModificationsReadyWithIncorrectTotalMessageCount");
650 ShardTestKit.waitUntilLeader(shard);
652 final TransactionIdentifier transactionID = nextTransactionId();
653 final BatchedModifications batched = new BatchedModifications(transactionID,
654 DataStoreVersions.CURRENT_VERSION);
656 batched.setTotalMessagesSent(2);
658 shard.tell(batched, testKit.getRef());
660 final Failure failure = testKit.expectMsgClass(Duration.ofSeconds(5), Failure.class);
662 if (failure != null) {
663 Throwables.propagateIfPossible(failure.cause(), Exception.class);
664 throw new RuntimeException(failure.cause());
669 public void testBatchedModificationsWithOperationFailure() {
670 final ShardTestKit testKit = new ShardTestKit(getSystem());
671 final TestActorRef<Shard> shard = actorFactory.createTestActor(
672 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
673 "testBatchedModificationsWithOperationFailure");
675 ShardTestKit.waitUntilLeader(shard);
677 // Test merge with invalid data. An exception should occur when
678 // the merge is applied. Note that
679 // write will not validate the children for performance reasons.
681 final TransactionIdentifier transactionID = nextTransactionId();
683 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
684 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
685 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
687 BatchedModifications batched = new BatchedModifications(transactionID, CURRENT_VERSION);
688 batched.addModification(new MergeModification(TestModel.TEST_PATH, invalidData));
689 shard.tell(batched, testKit.getRef());
690 Failure failure = testKit.expectMsgClass(Duration.ofSeconds(5), akka.actor.Status.Failure.class);
692 final Throwable cause = failure.cause();
694 batched = new BatchedModifications(transactionID, DataStoreVersions.CURRENT_VERSION);
696 batched.setTotalMessagesSent(2);
698 shard.tell(batched, testKit.getRef());
700 failure = testKit.expectMsgClass(Duration.ofSeconds(5), akka.actor.Status.Failure.class);
701 assertEquals("Failure cause", cause, failure.cause());
705 public void testBatchedModificationsOnTransactionChain() {
706 final ShardTestKit testKit = new ShardTestKit(getSystem());
707 final TestActorRef<Shard> shard = actorFactory.createTestActor(
708 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
709 "testBatchedModificationsOnTransactionChain");
711 ShardTestKit.waitUntilLeader(shard);
713 final LocalHistoryIdentifier historyId = nextHistoryId();
714 final TransactionIdentifier transactionID1 = new TransactionIdentifier(historyId, 0);
715 final TransactionIdentifier transactionID2 = new TransactionIdentifier(historyId, 1);
717 final Duration duration = Duration.ofSeconds(5);
719 // Send a BatchedModifications to start a chained write
720 // transaction and ready it.
722 final ContainerNode containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
723 final YangInstanceIdentifier path = TestModel.TEST_PATH;
724 shard.tell(newBatchedModifications(transactionID1, path, containerNode, true, false, 1), testKit.getRef());
725 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
727 // Create a read Tx on the same chain.
729 shard.tell(new CreateTransaction(transactionID2, TransactionType.READ_ONLY.ordinal(),
730 DataStoreVersions.CURRENT_VERSION).toSerializable(), testKit.getRef());
732 final CreateTransactionReply createReply = testKit.expectMsgClass(Duration.ofSeconds(3),
733 CreateTransactionReply.class);
735 getSystem().actorSelection(createReply.getTransactionPath())
736 .tell(new ReadData(path, DataStoreVersions.CURRENT_VERSION), testKit.getRef());
737 final ReadDataReply readReply = testKit.expectMsgClass(Duration.ofSeconds(3), ReadDataReply.class);
738 assertEquals("Read node", containerNode, readReply.getNormalizedNode());
740 // Commit the write transaction.
742 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
743 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
744 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
745 assertTrue("Can commit", canCommitReply.getCanCommit());
747 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
748 testKit.expectMsgClass(duration, CommitTransactionReply.class);
750 // Verify data in the data store.
752 final NormalizedNode<?, ?> actualNode = readStore(shard, path);
753 assertEquals("Stored node", containerNode, actualNode);
757 public void testOnBatchedModificationsWhenNotLeader() {
758 final AtomicBoolean overrideLeaderCalls = new AtomicBoolean();
759 final ShardTestKit testKit = new ShardTestKit(getSystem());
760 final Creator<Shard> creator = new Creator<Shard>() {
761 private static final long serialVersionUID = 1L;
764 public Shard create() {
765 return new Shard(newShardBuilder()) {
767 protected boolean isLeader() {
768 return overrideLeaderCalls.get() ? false : super.isLeader();
772 public ActorSelection getLeader() {
773 return overrideLeaderCalls.get() ? getSystem().actorSelection(testKit.getRef().path())
780 final TestActorRef<Shard> shard = actorFactory.createTestActor(Props
781 .create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
782 "testOnBatchedModificationsWhenNotLeader");
784 ShardTestKit.waitUntilLeader(shard);
786 overrideLeaderCalls.set(true);
788 final BatchedModifications batched = new BatchedModifications(nextTransactionId(),
789 DataStoreVersions.CURRENT_VERSION);
791 shard.tell(batched, ActorRef.noSender());
793 testKit.expectMsgEquals(batched);
797 public void testTransactionMessagesWithNoLeader() {
798 final ShardTestKit testKit = new ShardTestKit(getSystem());
799 dataStoreContextBuilder.customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName())
800 .shardHeartbeatIntervalInMillis(50).shardElectionTimeoutFactor(1);
801 final TestActorRef<Shard> shard = actorFactory.createTestActor(
802 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
803 "testTransactionMessagesWithNoLeader");
805 testKit.waitUntilNoLeader(shard);
807 final TransactionIdentifier txId = nextTransactionId();
808 shard.tell(new BatchedModifications(txId, DataStoreVersions.CURRENT_VERSION), testKit.getRef());
809 Failure failure = testKit.expectMsgClass(Failure.class);
810 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
812 shard.tell(prepareForwardedReadyTransaction(shard, txId, TestModel.TEST_PATH,
813 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), testKit.getRef());
814 failure = testKit.expectMsgClass(Failure.class);
815 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
817 shard.tell(new ReadyLocalTransaction(txId, mock(DataTreeModification.class), true, Optional.empty()),
819 failure = testKit.expectMsgClass(Failure.class);
820 assertEquals("Failure cause type", NoShardLeaderException.class, failure.cause().getClass());
824 public void testReadyWithReadWriteImmediateCommit() {
825 testReadyWithImmediateCommit(true);
829 public void testReadyWithWriteOnlyImmediateCommit() {
830 testReadyWithImmediateCommit(false);
833 private void testReadyWithImmediateCommit(final boolean readWrite) {
834 final ShardTestKit testKit = new ShardTestKit(getSystem());
835 final TestActorRef<Shard> shard = actorFactory.createTestActor(
836 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
837 "testReadyWithImmediateCommit-" + readWrite);
839 ShardTestKit.waitUntilLeader(shard);
841 final TransactionIdentifier transactionID = nextTransactionId();
842 final NormalizedNode<?, ?> containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
844 shard.tell(prepareForwardedReadyTransaction(shard, transactionID, TestModel.TEST_PATH, containerNode, true),
847 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH, containerNode, true),
851 testKit.expectMsgClass(Duration.ofSeconds(5), CommitTransactionReply.class);
853 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.TEST_PATH);
854 assertEquals(TestModel.TEST_QNAME.getLocalName(), containerNode, actualNode);
858 public void testReadyLocalTransactionWithImmediateCommit() {
859 final ShardTestKit testKit = new ShardTestKit(getSystem());
860 final TestActorRef<Shard> shard = actorFactory.createTestActor(
861 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
862 "testReadyLocalTransactionWithImmediateCommit");
864 ShardTestKit.waitUntilLeader(shard);
866 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
868 final DataTreeModification modification = dataStore.newModification();
870 final ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
871 new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
872 final MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
873 .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 42))
875 new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
877 final TransactionIdentifier txId = nextTransactionId();
878 modification.ready();
879 final ReadyLocalTransaction readyMessage =
880 new ReadyLocalTransaction(txId, modification, true, Optional.empty());
882 shard.tell(readyMessage, testKit.getRef());
884 testKit.expectMsgClass(CommitTransactionReply.class);
886 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.OUTER_LIST_PATH);
887 assertEquals(TestModel.OUTER_LIST_QNAME.getLocalName(), mergeData, actualNode);
891 public void testReadyLocalTransactionWithThreePhaseCommit() {
892 final ShardTestKit testKit = new ShardTestKit(getSystem());
893 final TestActorRef<Shard> shard = actorFactory.createTestActor(
894 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
895 "testReadyLocalTransactionWithThreePhaseCommit");
897 ShardTestKit.waitUntilLeader(shard);
899 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
901 final DataTreeModification modification = dataStore.newModification();
903 final ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
904 new WriteModification(TestModel.TEST_PATH, writeData).apply(modification);
905 final MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
906 .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 42))
908 new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification);
910 final TransactionIdentifier txId = nextTransactionId();
911 modification.ready();
912 final ReadyLocalTransaction readyMessage =
913 new ReadyLocalTransaction(txId, modification, false, Optional.empty());
915 shard.tell(readyMessage, testKit.getRef());
917 testKit.expectMsgClass(ReadyTransactionReply.class);
919 // Send the CanCommitTransaction message.
921 shard.tell(new CanCommitTransaction(txId, CURRENT_VERSION).toSerializable(), testKit.getRef());
922 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
923 .fromSerializable(testKit.expectMsgClass(CanCommitTransactionReply.class));
924 assertTrue("Can commit", canCommitReply.getCanCommit());
926 // Send the CanCommitTransaction message.
928 shard.tell(new CommitTransaction(txId, CURRENT_VERSION).toSerializable(), testKit.getRef());
929 testKit.expectMsgClass(CommitTransactionReply.class);
931 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.OUTER_LIST_PATH);
932 assertEquals(TestModel.OUTER_LIST_QNAME.getLocalName(), mergeData, actualNode);
936 public void testReadWriteCommitWithPersistenceDisabled() {
937 dataStoreContextBuilder.persistent(false);
938 final ShardTestKit testKit = new ShardTestKit(getSystem());
939 final TestActorRef<Shard> shard = actorFactory.createTestActor(
940 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
941 "testCommitWithPersistenceDisabled");
943 ShardTestKit.waitUntilLeader(shard);
945 // Setup a simulated transactions with a mock cohort.
947 final Duration duration = Duration.ofSeconds(5);
949 final TransactionIdentifier transactionID = nextTransactionId();
950 final NormalizedNode<?, ?> containerNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
951 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH, containerNode, false),
953 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
955 // Send the CanCommitTransaction message.
957 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
958 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
959 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
960 assertTrue("Can commit", canCommitReply.getCanCommit());
962 // Send the CanCommitTransaction message.
964 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
965 testKit.expectMsgClass(duration, CommitTransactionReply.class);
967 final NormalizedNode<?, ?> actualNode = readStore(shard, TestModel.TEST_PATH);
968 assertEquals(TestModel.TEST_QNAME.getLocalName(), containerNode, actualNode);
972 public void testReadWriteCommitWhenTransactionHasModifications() throws Exception {
973 testCommitWhenTransactionHasModifications(true);
977 public void testWriteOnlyCommitWhenTransactionHasModifications() throws Exception {
978 testCommitWhenTransactionHasModifications(false);
981 private void testCommitWhenTransactionHasModifications(final boolean readWrite) throws Exception {
982 final ShardTestKit testKit = new ShardTestKit(getSystem());
983 final DataTree dataTree = createDelegatingMockDataTree();
984 final TestActorRef<Shard> shard = actorFactory.createTestActor(
985 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
986 "testCommitWhenTransactionHasModifications-" + readWrite);
988 ShardTestKit.waitUntilLeader(shard);
990 final Duration duration = Duration.ofSeconds(5);
991 final TransactionIdentifier transactionID = nextTransactionId();
994 shard.tell(prepareForwardedReadyTransaction(shard, transactionID, TestModel.TEST_PATH,
995 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), testKit.getRef());
997 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH,
998 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), testKit.getRef());
1001 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1003 // Send the CanCommitTransaction message.
1005 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
1006 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
1007 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
1008 assertTrue("Can commit", canCommitReply.getCanCommit());
1010 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
1011 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1013 final InOrder inOrder = inOrder(dataTree);
1014 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1015 inOrder.verify(dataTree).prepare(any(DataTreeModification.class));
1016 inOrder.verify(dataTree).commit(any(DataTreeCandidate.class));
1018 // Purge request is scheduled as asynchronous, wait for two heartbeats to let it propagate into
1020 Thread.sleep(HEARTBEAT_MILLIS * 2);
1022 shard.tell(Shard.GET_SHARD_MBEAN_MESSAGE, testKit.getRef());
1023 final ShardStats shardStats = testKit.expectMsgClass(duration, ShardStats.class);
1025 // Use MBean for verification
1026 // Committed transaction count should increase as usual
1027 assertEquals(1, shardStats.getCommittedTransactionsCount());
1029 // Commit index should advance 1 to account for disabling metadata
1030 assertEquals(1, shardStats.getCommitIndex());
1034 public void testCommitPhaseFailure() throws Exception {
1035 final ShardTestKit testKit = new ShardTestKit(getSystem());
1036 final DataTree dataTree = createDelegatingMockDataTree();
1037 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1038 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1039 "testCommitPhaseFailure");
1041 ShardTestKit.waitUntilLeader(shard);
1043 final Duration duration = Duration.ofSeconds(5);
1044 final Timeout timeout = Timeout.create(duration);
1046 // Setup 2 simulated transactions with mock cohorts. The first
1050 doThrow(new RuntimeException("mock commit failure")).when(dataTree)
1051 .commit(any(DataTreeCandidate.class));
1053 final TransactionIdentifier transactionID1 = nextTransactionId();
1054 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1055 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1056 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1058 final TransactionIdentifier transactionID2 = nextTransactionId();
1059 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1060 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1061 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1063 // Send the CanCommitTransaction message for the first Tx.
1065 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1066 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
1067 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
1068 assertTrue("Can commit", canCommitReply.getCanCommit());
1070 // Send the CanCommitTransaction message for the 2nd Tx. This
1071 // should get queued and
1072 // processed after the first Tx completes.
1074 final Future<Object> canCommitFuture = Patterns.ask(shard,
1075 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1077 // Send the CommitTransaction message for the first Tx. This
1078 // should send back an error
1079 // and trigger the 2nd Tx to proceed.
1081 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1082 testKit.expectMsgClass(duration, akka.actor.Status.Failure.class);
1084 // Wait for the 2nd Tx to complete the canCommit phase.
1086 final CountDownLatch latch = new CountDownLatch(1);
1087 canCommitFuture.onComplete(new OnComplete<Object>() {
1089 public void onComplete(final Throwable failure, final Object resp) {
1092 }, getSystem().dispatcher());
1094 assertTrue("2nd CanCommit complete", latch.await(5, TimeUnit.SECONDS));
1096 final InOrder inOrder = inOrder(dataTree);
1097 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1098 inOrder.verify(dataTree).prepare(any(DataTreeModification.class));
1100 // FIXME: this invocation is done on the result of validate(). To test it, we need to make sure mock
1101 // validate performs wrapping and we capture that mock
1102 // inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1104 inOrder.verify(dataTree).commit(any(DataTreeCandidate.class));
1108 public void testPreCommitPhaseFailure() throws Exception {
1109 final ShardTestKit testKit = new ShardTestKit(getSystem());
1110 final DataTree dataTree = createDelegatingMockDataTree();
1111 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1112 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1113 "testPreCommitPhaseFailure");
1115 ShardTestKit.waitUntilLeader(shard);
1117 final Duration duration = Duration.ofSeconds(5);
1118 final Timeout timeout = Timeout.create(duration);
1120 doThrow(new RuntimeException("mock preCommit failure")).when(dataTree)
1121 .prepare(any(DataTreeModification.class));
1123 final TransactionIdentifier transactionID1 = nextTransactionId();
1124 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1125 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1126 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1128 final TransactionIdentifier transactionID2 = nextTransactionId();
1129 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1130 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1131 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1133 // Send the CanCommitTransaction message for the first Tx.
1135 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1136 final CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
1137 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
1138 assertTrue("Can commit", canCommitReply.getCanCommit());
1140 // Send the CanCommitTransaction message for the 2nd Tx. This
1141 // should get queued and
1142 // processed after the first Tx completes.
1144 final Future<Object> canCommitFuture = Patterns.ask(shard,
1145 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1147 // Send the CommitTransaction message for the first Tx. This
1148 // should send back an error
1149 // and trigger the 2nd Tx to proceed.
1151 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1152 testKit.expectMsgClass(duration, akka.actor.Status.Failure.class);
1154 // Wait for the 2nd Tx to complete the canCommit phase.
1156 final CountDownLatch latch = new CountDownLatch(1);
1157 canCommitFuture.onComplete(new OnComplete<Object>() {
1159 public void onComplete(final Throwable failure, final Object resp) {
1162 }, getSystem().dispatcher());
1164 assertTrue("2nd CanCommit complete", latch.await(5, TimeUnit.SECONDS));
1166 final InOrder inOrder = inOrder(dataTree);
1167 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1168 inOrder.verify(dataTree).prepare(any(DataTreeModification.class));
1169 inOrder.verify(dataTree).validate(any(DataTreeModification.class));
1173 public void testCanCommitPhaseFailure() throws Exception {
1174 final ShardTestKit testKit = new ShardTestKit(getSystem());
1175 final DataTree dataTree = createDelegatingMockDataTree();
1176 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1177 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1178 "testCanCommitPhaseFailure");
1180 ShardTestKit.waitUntilLeader(shard);
1182 final Duration duration = Duration.ofSeconds(5);
1183 final TransactionIdentifier transactionID1 = nextTransactionId();
1185 doThrow(new DataValidationFailedException(YangInstanceIdentifier.EMPTY, "mock canCommit failure"))
1186 .doNothing().when(dataTree).validate(any(DataTreeModification.class));
1188 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1189 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1190 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1192 // Send the CanCommitTransaction message.
1194 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1195 testKit.expectMsgClass(duration, akka.actor.Status.Failure.class);
1197 // Send another can commit to ensure the failed one got cleaned
1200 final TransactionIdentifier transactionID2 = nextTransactionId();
1201 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1202 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1203 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1205 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), testKit.getRef());
1206 final CanCommitTransactionReply reply = CanCommitTransactionReply
1207 .fromSerializable(testKit.expectMsgClass(CanCommitTransactionReply.class));
1208 assertTrue("getCanCommit", reply.getCanCommit());
1212 public void testImmediateCommitWithCanCommitPhaseFailure() throws Exception {
1213 testImmediateCommitWithCanCommitPhaseFailure(true);
1214 testImmediateCommitWithCanCommitPhaseFailure(false);
1217 private void testImmediateCommitWithCanCommitPhaseFailure(final boolean readWrite) throws Exception {
1218 final ShardTestKit testKit = new ShardTestKit(getSystem());
1219 final DataTree dataTree = createDelegatingMockDataTree();
1220 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1221 newShardBuilder().dataTree(dataTree).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1222 "testImmediateCommitWithCanCommitPhaseFailure-" + readWrite);
1224 ShardTestKit.waitUntilLeader(shard);
1226 doThrow(new DataValidationFailedException(YangInstanceIdentifier.EMPTY, "mock canCommit failure"))
1227 .doNothing().when(dataTree).validate(any(DataTreeModification.class));
1229 final Duration duration = Duration.ofSeconds(5);
1231 final TransactionIdentifier transactionID1 = nextTransactionId();
1234 shard.tell(prepareForwardedReadyTransaction(shard, transactionID1, TestModel.TEST_PATH,
1235 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), testKit.getRef());
1237 shard.tell(prepareBatchedModifications(transactionID1, TestModel.TEST_PATH,
1238 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), testKit.getRef());
1241 testKit.expectMsgClass(duration, akka.actor.Status.Failure.class);
1243 // Send another can commit to ensure the failed one got cleaned
1246 final TransactionIdentifier transactionID2 = nextTransactionId();
1248 shard.tell(prepareForwardedReadyTransaction(shard, transactionID2, TestModel.TEST_PATH,
1249 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), testKit.getRef());
1251 shard.tell(prepareBatchedModifications(transactionID2, TestModel.TEST_PATH,
1252 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true), testKit.getRef());
1255 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1259 public void testAbortWithCommitPending() {
1260 final ShardTestKit testKit = new ShardTestKit(getSystem());
1261 final Creator<Shard> creator = () -> new Shard(newShardBuilder()) {
1263 void persistPayload(final Identifier id, final Payload payload,
1264 final boolean batchHint) {
1265 // Simulate an AbortTransaction message occurring during
1266 // replication, after
1267 // persisting and before finishing the commit to the
1270 doAbortTransaction(id, null);
1271 super.persistPayload(id, payload, batchHint);
1275 final TestActorRef<Shard> shard = actorFactory.createTestActor(Props
1276 .create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
1277 "testAbortWithCommitPending");
1279 ShardTestKit.waitUntilLeader(shard);
1281 final Duration duration = Duration.ofSeconds(5);
1283 final TransactionIdentifier transactionID = nextTransactionId();
1285 shard.tell(prepareBatchedModifications(transactionID, TestModel.TEST_PATH,
1286 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), testKit.getRef());
1287 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1289 shard.tell(new CanCommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
1290 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1292 shard.tell(new CommitTransaction(transactionID, CURRENT_VERSION).toSerializable(), testKit.getRef());
1293 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1295 final NormalizedNode<?, ?> node = readStore(shard, TestModel.TEST_PATH);
1297 // Since we're simulating an abort occurring during replication
1298 // and before finish commit,
1299 // the data should still get written to the in-memory store
1300 // since we've gotten past
1301 // canCommit and preCommit and persisted the data.
1302 assertNotNull(TestModel.TEST_QNAME.getLocalName() + " not found", node);
1306 public void testTransactionCommitTimeout() throws Exception {
1307 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1308 final ShardTestKit testKit = new ShardTestKit(getSystem());
1309 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1310 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1311 "testTransactionCommitTimeout");
1313 ShardTestKit.waitUntilLeader(shard);
1315 final Duration duration = Duration.ofSeconds(5);
1317 writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1318 writeToStore(shard, TestModel.OUTER_LIST_PATH,
1319 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1321 // Ready 2 Tx's - the first will timeout
1323 final TransactionIdentifier transactionID1 = nextTransactionId();
1325 prepareBatchedModifications(transactionID1,
1326 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1327 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build(),
1328 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1), false),
1330 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1332 final TransactionIdentifier transactionID2 = nextTransactionId();
1333 final YangInstanceIdentifier listNodePath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1334 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build();
1336 prepareBatchedModifications(transactionID2, listNodePath,
1337 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2), false), testKit.getRef());
1338 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1340 // canCommit 1st Tx. We don't send the commit so it should
1343 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1344 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1346 // canCommit the 2nd Tx - it should complete after the 1st Tx
1349 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), testKit.getRef());
1350 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1352 // Try to commit the 1st Tx - should fail as it's not the
1355 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1356 testKit.expectMsgClass(duration, akka.actor.Status.Failure.class);
1358 // Commit the 2nd Tx.
1360 shard.tell(new CommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), testKit.getRef());
1361 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1363 final NormalizedNode<?, ?> node = readStore(shard, listNodePath);
1364 assertNotNull(listNodePath + " not found", node);
1369 // public void testTransactionCommitQueueCapacityExceeded() throws Throwable {
1370 // dataStoreContextBuilder.shardTransactionCommitQueueCapacity(2);
1372 // new ShardTestKit(getSystem()) {{
1373 // final TestActorRef<Shard> shard = actorFactory.createTestActor(
1374 // newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1375 // "testTransactionCommitQueueCapacityExceeded");
1377 // waitUntilLeader(shard);
1379 // final FiniteDuration duration = duration("5 seconds");
1381 // final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1383 // final TransactionIdentifier transactionID1 = nextTransactionId();
1384 // final MutableCompositeModification modification1 = new MutableCompositeModification();
1385 // final ShardDataTreeCohort cohort1 = setupMockWriteTransaction("cohort1", dataStore,
1386 // TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), transactionID1,
1389 // final TransactionIdentifier transactionID2 = nextTransactionId();
1390 // final MutableCompositeModification modification2 = new MutableCompositeModification();
1391 // final ShardDataTreeCohort cohort2 = setupMockWriteTransaction("cohort2", dataStore,
1392 // TestModel.OUTER_LIST_PATH,
1393 // ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), transactionID2,
1396 // final TransactionIdentifier transactionID3 = nextTransactionId();
1397 // final MutableCompositeModification modification3 = new MutableCompositeModification();
1398 // final ShardDataTreeCohort cohort3 = setupMockWriteTransaction("cohort3", dataStore,
1399 // TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME), transactionID3,
1402 // // Ready the Tx's
1404 // shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort1, transactionID1,
1405 // modification1), getRef());
1406 // expectMsgClass(duration, ReadyTransactionReply.class);
1408 // shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort2, transactionID2,
1409 // modification2), getRef());
1410 // expectMsgClass(duration, ReadyTransactionReply.class);
1412 // // The 3rd Tx should exceed queue capacity and fail.
1414 // shard.tell(prepareReadyTransactionMessage(false, shard.underlyingActor(), cohort3, transactionID3,
1415 // modification3), getRef());
1416 // expectMsgClass(duration, akka.actor.Status.Failure.class);
1418 // // canCommit 1st Tx.
1420 // shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), getRef());
1421 // expectMsgClass(duration, CanCommitTransactionReply.class);
1423 // // canCommit the 2nd Tx - it should get queued.
1425 // shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), getRef());
1427 // // canCommit the 3rd Tx - should exceed queue capacity and fail.
1429 // shard.tell(new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), getRef());
1430 // expectMsgClass(duration, akka.actor.Status.Failure.class);
1435 public void testTransactionCommitWithPriorExpiredCohortEntries() {
1436 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1437 final ShardTestKit testKit = new ShardTestKit(getSystem());
1438 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1439 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1440 "testTransactionCommitWithPriorExpiredCohortEntries");
1442 ShardTestKit.waitUntilLeader(shard);
1444 final Duration duration = Duration.ofSeconds(5);
1446 final TransactionIdentifier transactionID1 = nextTransactionId();
1447 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1448 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1449 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1451 final TransactionIdentifier transactionID2 = nextTransactionId();
1452 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1453 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1454 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1456 final TransactionIdentifier transactionID3 = nextTransactionId();
1457 shard.tell(newBatchedModifications(transactionID3, TestModel.TEST_PATH,
1458 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1459 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1461 // All Tx's are readied. We'll send canCommit for the last one
1462 // but not the others. The others
1463 // should expire from the queue and the last one should be
1466 shard.tell(new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), testKit.getRef());
1467 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1471 public void testTransactionCommitWithSubsequentExpiredCohortEntry() {
1472 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1473 final ShardTestKit testKit = new ShardTestKit(getSystem());
1474 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1475 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1476 "testTransactionCommitWithSubsequentExpiredCohortEntry");
1478 ShardTestKit.waitUntilLeader(shard);
1480 final Duration duration = Duration.ofSeconds(5);
1482 final ShardDataTree dataStore = shard.underlyingActor().getDataStore();
1484 final TransactionIdentifier transactionID1 = nextTransactionId();
1485 shard.tell(prepareBatchedModifications(transactionID1, TestModel.TEST_PATH,
1486 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), testKit.getRef());
1487 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1489 // CanCommit the first Tx so it's the current in-progress Tx.
1491 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1492 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1494 // Ready the second Tx.
1496 final TransactionIdentifier transactionID2 = nextTransactionId();
1497 shard.tell(prepareBatchedModifications(transactionID2, TestModel.TEST_PATH,
1498 ImmutableNodes.containerNode(TestModel.TEST_QNAME), false), testKit.getRef());
1499 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1501 // Ready the third Tx.
1503 final TransactionIdentifier transactionID3 = nextTransactionId();
1504 final DataTreeModification modification3 = dataStore.newModification();
1505 new WriteModification(TestModel.TEST2_PATH, ImmutableNodes.containerNode(TestModel.TEST2_QNAME))
1506 .apply(modification3);
1507 modification3.ready();
1508 final ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(transactionID3, modification3,
1509 true, Optional.empty());
1510 shard.tell(readyMessage, testKit.getRef());
1512 // Commit the first Tx. After completing, the second should
1513 // expire from the queue and the third
1516 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1517 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1519 // Expect commit reply from the third Tx.
1521 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1523 final NormalizedNode<?, ?> node = readStore(shard, TestModel.TEST2_PATH);
1524 assertNotNull(TestModel.TEST2_PATH + " not found", node);
1528 public void testCanCommitBeforeReadyFailure() {
1529 final ShardTestKit testKit = new ShardTestKit(getSystem());
1530 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1531 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1532 "testCanCommitBeforeReadyFailure");
1534 shard.tell(new CanCommitTransaction(nextTransactionId(), CURRENT_VERSION).toSerializable(), testKit.getRef());
1535 testKit.expectMsgClass(Duration.ofSeconds(5), akka.actor.Status.Failure.class);
1539 public void testAbortAfterCanCommit() throws Exception {
1540 final ShardTestKit testKit = new ShardTestKit(getSystem());
1541 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1542 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testAbortAfterCanCommit");
1544 ShardTestKit.waitUntilLeader(shard);
1546 final Duration duration = Duration.ofSeconds(5);
1547 final Timeout timeout = Timeout.create(duration);
1549 // Ready 2 transactions - the first one will be aborted.
1551 final TransactionIdentifier transactionID1 = nextTransactionId();
1552 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1553 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1554 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1556 final TransactionIdentifier transactionID2 = nextTransactionId();
1557 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1558 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1559 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1561 // Send the CanCommitTransaction message for the first Tx.
1563 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1564 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply
1565 .fromSerializable(testKit.expectMsgClass(duration, CanCommitTransactionReply.class));
1566 assertTrue("Can commit", canCommitReply.getCanCommit());
1568 // Send the CanCommitTransaction message for the 2nd Tx. This
1569 // should get queued and
1570 // processed after the first Tx completes.
1572 final Future<Object> canCommitFuture = Patterns.ask(shard,
1573 new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), timeout);
1575 // Send the AbortTransaction message for the first Tx. This
1576 // should trigger the 2nd
1579 shard.tell(new AbortTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1580 testKit.expectMsgClass(duration, AbortTransactionReply.class);
1582 // Wait for the 2nd Tx to complete the canCommit phase.
1584 canCommitReply = (CanCommitTransactionReply) Await.result(canCommitFuture,
1585 FiniteDuration.create(5, TimeUnit.SECONDS));
1586 assertTrue("Can commit", canCommitReply.getCanCommit());
1590 public void testAbortAfterReady() {
1591 dataStoreContextBuilder.shardTransactionCommitTimeoutInSeconds(1);
1592 final ShardTestKit testKit = new ShardTestKit(getSystem());
1593 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1594 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testAbortAfterReady");
1596 ShardTestKit.waitUntilLeader(shard);
1598 final Duration duration = Duration.ofSeconds(5);
1602 final TransactionIdentifier transactionID1 = nextTransactionId();
1603 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1604 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1605 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1607 // Send the AbortTransaction message.
1609 shard.tell(new AbortTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1610 testKit.expectMsgClass(duration, AbortTransactionReply.class);
1612 assertEquals("getPendingTxCommitQueueSize", 0, shard.underlyingActor().getPendingTxCommitQueueSize());
1614 // Now send CanCommitTransaction - should fail.
1616 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1617 final Throwable failure = testKit.expectMsgClass(duration, akka.actor.Status.Failure.class).cause();
1618 assertTrue("Failure type", failure instanceof IllegalStateException);
1620 // Ready and CanCommit another and verify success.
1622 final TransactionIdentifier transactionID2 = nextTransactionId();
1623 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1624 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1625 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1627 shard.tell(new CanCommitTransaction(transactionID2, CURRENT_VERSION).toSerializable(), testKit.getRef());
1628 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1632 public void testAbortQueuedTransaction() {
1633 final ShardTestKit testKit = new ShardTestKit(getSystem());
1634 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1635 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testAbortAfterReady");
1637 ShardTestKit.waitUntilLeader(shard);
1639 final Duration duration = Duration.ofSeconds(5);
1643 final TransactionIdentifier transactionID1 = nextTransactionId();
1644 shard.tell(newBatchedModifications(transactionID1, TestModel.TEST_PATH,
1645 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1646 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1648 final TransactionIdentifier transactionID2 = nextTransactionId();
1649 shard.tell(newBatchedModifications(transactionID2, TestModel.TEST_PATH,
1650 ImmutableNodes.containerNode(TestModel.TEST_QNAME), true, false, 1), testKit.getRef());
1651 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1653 final TransactionIdentifier transactionID3 = nextTransactionId();
1654 shard.tell(newBatchedModifications(transactionID3, TestModel.OUTER_LIST_PATH,
1655 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(), true, false, 1), testKit.getRef());
1656 testKit.expectMsgClass(duration, ReadyTransactionReply.class);
1658 // Abort the second tx while it's queued.
1660 shard.tell(new AbortTransaction(transactionID2, CURRENT_VERSION).toSerializable(), testKit.getRef());
1661 testKit.expectMsgClass(duration, AbortTransactionReply.class);
1663 // Commit the other 2.
1665 shard.tell(new CanCommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1666 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1668 shard.tell(new CommitTransaction(transactionID1, CURRENT_VERSION).toSerializable(), testKit.getRef());
1669 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1671 shard.tell(new CanCommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), testKit.getRef());
1672 testKit.expectMsgClass(duration, CanCommitTransactionReply.class);
1674 shard.tell(new CommitTransaction(transactionID3, CURRENT_VERSION).toSerializable(), testKit.getRef());
1675 testKit.expectMsgClass(duration, CommitTransactionReply.class);
1677 assertEquals("getPendingTxCommitQueueSize", 0, shard.underlyingActor().getPendingTxCommitQueueSize());
1681 public void testCreateSnapshotWithNonPersistentData() throws Exception {
1682 testCreateSnapshot(false, "testCreateSnapshotWithNonPersistentData");
1686 public void testCreateSnapshot() throws Exception {
1687 testCreateSnapshot(true, "testCreateSnapshot");
1690 private void testCreateSnapshot(final boolean persistent, final String shardActorName) throws Exception {
1691 final AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(1));
1693 final AtomicReference<Object> savedSnapshot = new AtomicReference<>();
1694 class TestPersistentDataProvider extends DelegatingPersistentDataProvider {
1695 TestPersistentDataProvider(final DataPersistenceProvider delegate) {
1700 public void saveSnapshot(final Object obj) {
1701 savedSnapshot.set(obj);
1702 super.saveSnapshot(obj);
1706 dataStoreContextBuilder.persistent(persistent);
1708 class TestShard extends Shard {
1710 protected TestShard(final AbstractBuilder<?, ?> builder) {
1712 setPersistence(new TestPersistentDataProvider(super.persistence()));
1716 public void handleCommand(final Object message) {
1717 super.handleCommand(message);
1719 // XXX: commit_snapshot equality check references RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT
1720 if (message instanceof SaveSnapshotSuccess || "commit_snapshot".equals(message.toString())) {
1721 latch.get().countDown();
1726 public RaftActorContext getRaftActorContext() {
1727 return super.getRaftActorContext();
1731 final Creator<Shard> creator = () -> new TestShard(newShardBuilder());
1733 final TestActorRef<Shard> shard = actorFactory.createTestActor(Props
1734 .create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()),
1737 ShardTestKit.waitUntilLeader(shard);
1738 writeToStore(shard, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1740 final NormalizedNode<?, ?> expectedRoot = readStore(shard, YangInstanceIdentifier.EMPTY);
1742 // Trigger creation of a snapshot by ensuring
1743 final RaftActorContext raftActorContext = ((TestShard) shard.underlyingActor()).getRaftActorContext();
1744 raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
1745 awaitAndValidateSnapshot(latch, savedSnapshot, expectedRoot);
1747 raftActorContext.getSnapshotManager().capture(mock(ReplicatedLogEntry.class), -1);
1748 awaitAndValidateSnapshot(latch, savedSnapshot, expectedRoot);
1751 private static void awaitAndValidateSnapshot(final AtomicReference<CountDownLatch> latch,
1752 final AtomicReference<Object> savedSnapshot, final NormalizedNode<?, ?> expectedRoot)
1753 throws InterruptedException {
1754 assertTrue("Snapshot saved", latch.get().await(5, TimeUnit.SECONDS));
1756 assertTrue("Invalid saved snapshot " + savedSnapshot.get(), savedSnapshot.get() instanceof Snapshot);
1758 verifySnapshot((Snapshot) savedSnapshot.get(), expectedRoot);
1760 latch.set(new CountDownLatch(1));
1761 savedSnapshot.set(null);
1764 private static void verifySnapshot(final Snapshot snapshot, final NormalizedNode<?, ?> expectedRoot) {
1765 final NormalizedNode<?, ?> actual = ((ShardSnapshotState)snapshot.getState()).getSnapshot().getRootNode().get();
1766 assertEquals("Root node", expectedRoot, actual);
1770 * This test simply verifies that the applySnapShot logic will work.
1773 public void testInMemoryDataTreeRestore() throws DataValidationFailedException {
1774 final DataTree store = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
1777 final DataTreeModification putTransaction = store.takeSnapshot().newModification();
1778 putTransaction.write(TestModel.TEST_PATH,
1779 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1780 commitTransaction(store, putTransaction);
1783 final NormalizedNode<?, ?> expected = readStore(store, YangInstanceIdentifier.EMPTY);
1785 final DataTreeModification writeTransaction = store.takeSnapshot().newModification();
1787 writeTransaction.delete(YangInstanceIdentifier.EMPTY);
1788 writeTransaction.write(YangInstanceIdentifier.EMPTY, expected);
1790 commitTransaction(store, writeTransaction);
1792 final NormalizedNode<?, ?> actual = readStore(store, YangInstanceIdentifier.EMPTY);
1794 assertEquals(expected, actual);
1798 public void testRecoveryApplicable() {
1800 final DatastoreContext persistentContext = DatastoreContext.newBuilder()
1801 .shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(true).build();
1803 final Props persistentProps = Shard.builder().id(shardID).datastoreContext(persistentContext)
1804 .schemaContextProvider(() -> SCHEMA_CONTEXT).props();
1806 final DatastoreContext nonPersistentContext = DatastoreContext.newBuilder()
1807 .shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).persistent(false).build();
1809 final Props nonPersistentProps = Shard.builder().id(shardID).datastoreContext(nonPersistentContext)
1810 .schemaContextProvider(() -> SCHEMA_CONTEXT).props();
1812 final TestActorRef<Shard> shard1 = actorFactory.createTestActor(persistentProps, "testPersistence1");
1814 assertTrue("Recovery Applicable", shard1.underlyingActor().persistence().isRecoveryApplicable());
1816 final TestActorRef<Shard> shard2 = actorFactory.createTestActor(nonPersistentProps, "testPersistence2");
1818 assertFalse("Recovery Not Applicable", shard2.underlyingActor().persistence().isRecoveryApplicable());
1822 public void testOnDatastoreContext() {
1823 dataStoreContextBuilder.persistent(true);
1825 final TestActorRef<Shard> shard = actorFactory.createTestActor(newShardProps(), "testOnDatastoreContext");
1827 assertTrue("isRecoveryApplicable", shard.underlyingActor().persistence().isRecoveryApplicable());
1829 ShardTestKit.waitUntilLeader(shard);
1831 shard.tell(dataStoreContextBuilder.persistent(false).build(), ActorRef.noSender());
1833 assertFalse("isRecoveryApplicable", shard.underlyingActor().persistence().isRecoveryApplicable());
1835 shard.tell(dataStoreContextBuilder.persistent(true).build(), ActorRef.noSender());
1837 assertTrue("isRecoveryApplicable", shard.underlyingActor().persistence().isRecoveryApplicable());
1841 public void testRegisterRoleChangeListener() {
1842 final ShardTestKit testKit = new ShardTestKit(getSystem());
1843 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1844 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1845 "testRegisterRoleChangeListener");
1847 ShardTestKit.waitUntilLeader(shard);
1849 final ActorRef listener = getSystem().actorOf(MessageCollectorActor.props());
1851 shard.tell(new RegisterRoleChangeListener(), listener);
1853 MessageCollectorActor.expectFirstMatching(listener, RegisterRoleChangeListenerReply.class);
1855 ShardLeaderStateChanged leaderStateChanged = MessageCollectorActor.expectFirstMatching(listener,
1856 ShardLeaderStateChanged.class);
1857 assertTrue("getLocalShardDataTree present", leaderStateChanged.getLocalShardDataTree().isPresent());
1858 assertSame("getLocalShardDataTree", shard.underlyingActor().getDataStore().getDataTree(),
1859 leaderStateChanged.getLocalShardDataTree().get());
1861 MessageCollectorActor.clearMessages(listener);
1863 // Force a leader change
1865 shard.tell(new RequestVote(10000, "member2", 50, 50), testKit.getRef());
1867 leaderStateChanged = MessageCollectorActor.expectFirstMatching(listener, ShardLeaderStateChanged.class);
1868 assertFalse("getLocalShardDataTree present", leaderStateChanged.getLocalShardDataTree().isPresent());
1872 public void testFollowerInitialSyncStatus() {
1873 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1874 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
1875 "testFollowerInitialSyncStatus");
1877 shard.underlyingActor().handleNonRaftCommand(new FollowerInitialSyncUpStatus(false,
1878 "member-1-shard-inventory-operational"));
1880 assertFalse(shard.underlyingActor().getShardMBean().getFollowerInitialSyncStatus());
1882 shard.underlyingActor().handleNonRaftCommand(new FollowerInitialSyncUpStatus(true,
1883 "member-1-shard-inventory-operational"));
1885 assertTrue(shard.underlyingActor().getShardMBean().getFollowerInitialSyncStatus());
1889 public void testClusteredDataTreeChangeListenerWithDelayedRegistration() throws Exception {
1890 final ShardTestKit testKit = new ShardTestKit(getSystem());
1891 final String testName = "testClusteredDataTreeChangeListenerWithDelayedRegistration";
1892 dataStoreContextBuilder.shardElectionTimeoutFactor(1000)
1893 .customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
1895 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
1896 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener,
1897 TestModel.TEST_PATH), actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
1899 setupInMemorySnapshotStore();
1901 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1902 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1903 actorFactory.generateActorId(testName + "-shard"));
1905 testKit.waitUntilNoLeader(shard);
1907 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), testKit.getRef());
1908 final RegisterDataTreeNotificationListenerReply reply = testKit.expectMsgClass(Duration.ofSeconds(5),
1909 RegisterDataTreeNotificationListenerReply.class);
1910 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
1912 shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build())
1913 .customRaftPolicyImplementation(null).build(), ActorRef.noSender());
1915 listener.waitForChangeEvents();
1919 public void testClusteredDataTreeChangeListenerWithDelayedRegistrationClosed() throws Exception {
1920 final ShardTestKit testKit = new ShardTestKit(getSystem());
1921 final String testName = "testClusteredDataTreeChangeListenerWithDelayedRegistrationClosed";
1922 dataStoreContextBuilder.shardElectionTimeoutFactor(1000)
1923 .customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
1925 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(0);
1926 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener,
1927 TestModel.TEST_PATH), actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
1929 setupInMemorySnapshotStore();
1931 final TestActorRef<Shard> shard = actorFactory.createTestActor(
1932 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
1933 actorFactory.generateActorId(testName + "-shard"));
1935 testKit.waitUntilNoLeader(shard);
1937 shard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), testKit.getRef());
1938 final RegisterDataTreeNotificationListenerReply reply = testKit.expectMsgClass(Duration.ofSeconds(5),
1939 RegisterDataTreeNotificationListenerReply.class);
1940 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
1942 final ActorSelection regActor = getSystem().actorSelection(reply.getListenerRegistrationPath());
1943 regActor.tell(CloseDataTreeNotificationListenerRegistration.getInstance(), testKit.getRef());
1944 testKit.expectMsgClass(CloseDataTreeNotificationListenerRegistrationReply.class);
1946 shard.tell(DatastoreContext.newBuilderFrom(dataStoreContextBuilder.build())
1947 .customRaftPolicyImplementation(null).build(), ActorRef.noSender());
1949 listener.expectNoMoreChanges("Received unexpected change after close");
1953 public void testClusteredDataTreeChangeListenerRegistration() throws Exception {
1954 final ShardTestKit testKit = new ShardTestKit(getSystem());
1955 final String testName = "testClusteredDataTreeChangeListenerRegistration";
1956 final ShardIdentifier followerShardID = ShardIdentifier.create("inventory",
1957 MemberName.forName(actorFactory.generateActorId(testName + "-follower")), "config");
1959 final ShardIdentifier leaderShardID = ShardIdentifier.create("inventory",
1960 MemberName.forName(actorFactory.generateActorId(testName + "-leader")), "config");
1962 final TestActorRef<Shard> followerShard = actorFactory
1963 .createTestActor(Shard.builder().id(followerShardID)
1964 .datastoreContext(dataStoreContextBuilder.shardElectionTimeoutFactor(1000).build())
1965 .peerAddresses(Collections.singletonMap(leaderShardID.toString(),
1966 "akka://test/user/" + leaderShardID.toString()))
1967 .schemaContextProvider(() -> SCHEMA_CONTEXT).props()
1968 .withDispatcher(Dispatchers.DefaultDispatcherId()), followerShardID.toString());
1970 final TestActorRef<Shard> leaderShard = actorFactory
1971 .createTestActor(Shard.builder().id(leaderShardID).datastoreContext(newDatastoreContext())
1972 .peerAddresses(Collections.singletonMap(followerShardID.toString(),
1973 "akka://test/user/" + followerShardID.toString()))
1974 .schemaContextProvider(() -> SCHEMA_CONTEXT).props()
1975 .withDispatcher(Dispatchers.DefaultDispatcherId()), leaderShardID.toString());
1977 leaderShard.tell(TimeoutNow.INSTANCE, ActorRef.noSender());
1978 final String leaderPath = ShardTestKit.waitUntilLeader(followerShard);
1979 assertEquals("Shard leader path", leaderShard.path().toString(), leaderPath);
1981 final YangInstanceIdentifier path = TestModel.TEST_PATH;
1982 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
1983 final ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener, path),
1984 actorFactory.generateActorId(testName + "-DataTreeChangeListener"));
1986 followerShard.tell(new RegisterDataTreeChangeListener(TestModel.TEST_PATH, dclActor, true), testKit.getRef());
1987 final RegisterDataTreeNotificationListenerReply reply = testKit.expectMsgClass(Duration.ofSeconds(5),
1988 RegisterDataTreeNotificationListenerReply.class);
1989 assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
1991 writeToStore(followerShard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1993 listener.waitForChangeEvents();
1997 public void testServerRemoved() {
1998 final TestActorRef<MessageCollectorActor> parent = actorFactory.createTestActor(MessageCollectorActor.props()
1999 .withDispatcher(Dispatchers.DefaultDispatcherId()));
2001 final ActorRef shard = parent.underlyingActor().context().actorOf(
2002 newShardBuilder().props().withDispatcher(Dispatchers.DefaultDispatcherId()),
2003 "testServerRemoved");
2005 shard.tell(new ServerRemoved("test"), ActorRef.noSender());
2007 MessageCollectorActor.expectFirstMatching(parent, ServerRemoved.class);