1 package org.opendaylight.controller.cluster.datastore;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertSame;
6 import static org.junit.Assert.assertTrue;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.never;
9 import static org.mockito.Mockito.times;
10 import static org.mockito.Mockito.verify;
11 import static org.mockito.Mockito.when;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSystem;
14 import akka.actor.AddressFromURIString;
15 import akka.actor.Props;
16 import akka.cluster.Cluster;
17 import akka.cluster.ClusterEvent;
18 import akka.dispatch.Dispatchers;
19 import akka.japi.Creator;
20 import akka.pattern.Patterns;
21 import akka.persistence.RecoveryCompleted;
22 import akka.testkit.JavaTestKit;
23 import akka.testkit.TestActorRef;
24 import akka.util.Timeout;
25 import com.google.common.base.Optional;
26 import com.google.common.collect.ImmutableMap;
27 import com.google.common.collect.ImmutableSet;
28 import com.google.common.collect.Sets;
29 import com.google.common.util.concurrent.Uninterruptibles;
30 import com.typesafe.config.ConfigFactory;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.HashSet;
35 import java.util.List;
38 import java.util.concurrent.CountDownLatch;
39 import java.util.concurrent.TimeUnit;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.opendaylight.controller.cluster.DataPersistenceProvider;
46 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
47 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
48 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
49 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
50 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
51 import org.opendaylight.controller.cluster.datastore.messages.ActorInitialized;
52 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
53 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
54 import org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound;
55 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
56 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
57 import org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound;
58 import org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged;
59 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
60 import org.opendaylight.controller.cluster.datastore.utils.MessageCollectorActor;
61 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
62 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
63 import org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache;
64 import org.opendaylight.controller.cluster.notifications.LeaderStateChanged;
65 import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
66 import org.opendaylight.controller.cluster.notifications.RoleChangeNotification;
67 import org.opendaylight.controller.cluster.raft.RaftState;
68 import org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus;
69 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
70 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
71 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
72 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
73 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
74 import scala.concurrent.Await;
75 import scala.concurrent.Future;
76 import scala.concurrent.duration.FiniteDuration;
78 public class ShardManagerTest extends AbstractActorTest {
79 private static int ID_COUNTER = 1;
81 private final String shardMrgIDSuffix = "config" + ID_COUNTER++;
82 private final String shardMgrID = "shard-manager-" + shardMrgIDSuffix;
85 private static CountDownLatch ready;
87 private static TestActorRef<MessageCollectorActor> mockShardActor;
89 private final DatastoreContext.Builder datastoreContextBuilder = DatastoreContext.newBuilder().
90 dataStoreType(shardMrgIDSuffix).shardInitializationTimeout(600, TimeUnit.MILLISECONDS);
92 private static ActorRef newMockShardActor(ActorSystem system, String shardName, String memberName) {
93 String name = new ShardIdentifier(shardName, memberName,"config").toString();
94 return TestActorRef.create(system, Props.create(MessageCollectorActor.class), name);
97 private final PrimaryShardInfoFutureCache primaryShardInfoCache = new PrimaryShardInfoFutureCache();
100 public void setUp() {
101 MockitoAnnotations.initMocks(this);
103 InMemoryJournal.clear();
105 if(mockShardActor == null) {
106 String name = new ShardIdentifier(Shard.DEFAULT_NAME, "member-1", "config").toString();
107 mockShardActor = TestActorRef.create(getSystem(), Props.create(MessageCollectorActor.class), name);
110 mockShardActor.underlyingActor().clear();
114 public void tearDown() {
115 InMemoryJournal.clear();
118 private Props newShardMgrProps(boolean persistent) {
119 return ShardManager.props(new MockClusterWrapper(), new MockConfiguration(),
120 datastoreContextBuilder.persistent(persistent).build(), ready, primaryShardInfoCache);
123 private Props newPropsShardMgrWithMockShardActor() {
124 return newPropsShardMgrWithMockShardActor("shardManager", mockShardActor, new MockClusterWrapper(),
125 new MockConfiguration());
128 private Props newPropsShardMgrWithMockShardActor(final String name, final ActorRef shardActor,
129 final ClusterWrapper clusterWrapper, final Configuration config) {
130 Creator<ShardManager> creator = new Creator<ShardManager>() {
131 private static final long serialVersionUID = 1L;
133 public ShardManager create() throws Exception {
134 return new ForwardingShardManager(clusterWrapper, config, datastoreContextBuilder.build(),
135 ready, name, shardActor, primaryShardInfoCache);
139 return Props.create(new DelegatingShardManagerCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId());
143 public void testOnReceiveFindPrimaryForNonExistentShard() throws Exception {
144 new JavaTestKit(getSystem()) {{
145 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
147 shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
149 shardManager.tell(new FindPrimary("non-existent", false), getRef());
151 expectMsgClass(duration("5 seconds"), PrimaryNotFoundException.class);
156 public void testOnReceiveFindPrimaryForLocalLeaderShard() throws Exception {
157 new JavaTestKit(getSystem()) {{
158 String memberId = "member-1-shard-default-" + shardMrgIDSuffix;
160 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
162 shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
163 shardManager.tell(new ActorInitialized(), mockShardActor);
165 DataTree mockDataTree = mock(DataTree.class);
166 shardManager.tell(new ShardLeaderStateChanged(memberId, memberId, Optional.of(mockDataTree)), getRef());
168 MessageCollectorActor.expectFirstMatching(mockShardActor, RegisterRoleChangeListener.class);
169 shardManager.tell((new RoleChangeNotification(memberId, RaftState.Candidate.name(),
170 RaftState.Leader.name())), mockShardActor);
172 shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
174 LocalPrimaryShardFound primaryFound = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
175 assertTrue("Unexpected primary path " + primaryFound.getPrimaryPath(),
176 primaryFound.getPrimaryPath().contains("member-1-shard-default"));
177 assertSame("getLocalShardDataTree", mockDataTree, primaryFound.getLocalShardDataTree() );
182 public void testOnReceiveFindPrimaryForNonLocalLeaderShardBeforeMemberUp() throws Exception {
183 new JavaTestKit(getSystem()) {{
184 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
186 shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
187 shardManager.tell(new ActorInitialized(), mockShardActor);
189 String memberId2 = "member-2-shard-default-" + shardMrgIDSuffix;
190 String memberId1 = "member-1-shard-default-" + shardMrgIDSuffix;
191 shardManager.tell(new RoleChangeNotification(memberId1,
192 RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor);
193 shardManager.tell(new LeaderStateChanged(memberId1, memberId2), mockShardActor);
195 shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
197 expectMsgClass(duration("5 seconds"), NoShardLeaderException.class);
202 public void testOnReceiveFindPrimaryForNonLocalLeaderShard() throws Exception {
203 new JavaTestKit(getSystem()) {{
204 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
206 shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
207 shardManager.tell(new ActorInitialized(), mockShardActor);
209 String memberId2 = "member-2-shard-default-" + shardMrgIDSuffix;
210 MockClusterWrapper.sendMemberUp(shardManager, "member-2", getRef().path().toString());
212 String memberId1 = "member-1-shard-default-" + shardMrgIDSuffix;
213 shardManager.tell(new RoleChangeNotification(memberId1,
214 RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor);
215 shardManager.tell(new ShardLeaderStateChanged(memberId1, memberId2, Optional.<DataTree>absent()), mockShardActor);
217 shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
219 RemotePrimaryShardFound primaryFound = expectMsgClass(duration("5 seconds"), RemotePrimaryShardFound.class);
220 assertTrue("Unexpected primary path " + primaryFound.getPrimaryPath(),
221 primaryFound.getPrimaryPath().contains("member-2-shard-default"));
226 public void testOnReceiveFindPrimaryForUninitializedShard() throws Exception {
227 new JavaTestKit(getSystem()) {{
228 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
230 shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
232 expectMsgClass(duration("5 seconds"), NotInitializedException.class);
237 public void testOnReceiveFindPrimaryForInitializedShardWithNoRole() throws Exception {
238 new JavaTestKit(getSystem()) {{
239 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
241 shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
242 shardManager.tell(new ActorInitialized(), mockShardActor);
244 shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
246 expectMsgClass(duration("5 seconds"), NoShardLeaderException.class);
251 public void testOnReceiveFindPrimaryForFollowerShardWithNoInitialLeaderId() throws Exception {
252 new JavaTestKit(getSystem()) {{
253 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
255 shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
256 shardManager.tell(new ActorInitialized(), mockShardActor);
258 String memberId = "member-1-shard-default-" + shardMrgIDSuffix;
259 shardManager.tell(new RoleChangeNotification(memberId,
260 RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor);
262 shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
264 expectMsgClass(duration("5 seconds"), NoShardLeaderException.class);
266 DataTree mockDataTree = mock(DataTree.class);
267 shardManager.tell(new ShardLeaderStateChanged(memberId, memberId, Optional.of(mockDataTree)), mockShardActor);
269 shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
271 LocalPrimaryShardFound primaryFound = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
272 assertTrue("Unexpected primary path " + primaryFound.getPrimaryPath(),
273 primaryFound.getPrimaryPath().contains("member-1-shard-default"));
274 assertSame("getLocalShardDataTree", mockDataTree, primaryFound.getLocalShardDataTree() );
279 public void testOnReceiveFindPrimaryWaitForShardLeader() throws Exception {
280 new JavaTestKit(getSystem()) {{
281 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
283 shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
285 // We're passing waitUntilInitialized = true to FindPrimary so the response should be
286 // delayed until we send ActorInitialized and RoleChangeNotification.
287 shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, true), getRef());
289 expectNoMsg(FiniteDuration.create(150, TimeUnit.MILLISECONDS));
291 shardManager.tell(new ActorInitialized(), mockShardActor);
293 expectNoMsg(FiniteDuration.create(150, TimeUnit.MILLISECONDS));
295 String memberId = "member-1-shard-default-" + shardMrgIDSuffix;
296 shardManager.tell(new RoleChangeNotification(memberId,
297 RaftState.Candidate.name(), RaftState.Leader.name()), mockShardActor);
299 expectNoMsg(FiniteDuration.create(150, TimeUnit.MILLISECONDS));
301 DataTree mockDataTree = mock(DataTree.class);
302 shardManager.tell(new ShardLeaderStateChanged(memberId, memberId, Optional.of(mockDataTree)), mockShardActor);
304 LocalPrimaryShardFound primaryFound = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
305 assertTrue("Unexpected primary path " + primaryFound.getPrimaryPath(),
306 primaryFound.getPrimaryPath().contains("member-1-shard-default"));
307 assertSame("getLocalShardDataTree", mockDataTree, primaryFound.getLocalShardDataTree() );
309 expectNoMsg(FiniteDuration.create(200, TimeUnit.MILLISECONDS));
314 public void testOnReceiveFindPrimaryWaitForReadyWithUninitializedShard() throws Exception {
315 new JavaTestKit(getSystem()) {{
316 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
318 shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
320 shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, true), getRef());
322 expectMsgClass(duration("2 seconds"), NotInitializedException.class);
324 shardManager.tell(new ActorInitialized(), mockShardActor);
326 expectNoMsg(FiniteDuration.create(200, TimeUnit.MILLISECONDS));
331 public void testOnReceiveFindPrimaryWaitForReadyWithCandidateShard() throws Exception {
332 new JavaTestKit(getSystem()) {{
333 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
335 shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
336 shardManager.tell(new ActorInitialized(), mockShardActor);
337 shardManager.tell(new RoleChangeNotification("member-1-shard-default-" + shardMrgIDSuffix,
338 null, RaftState.Candidate.name()), mockShardActor);
340 shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, true), getRef());
342 expectMsgClass(duration("2 seconds"), NoShardLeaderException.class);
347 public void testOnReceiveFindPrimaryWaitForReadyWithNoRoleShard() throws Exception {
348 new JavaTestKit(getSystem()) {{
349 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
351 shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
352 shardManager.tell(new ActorInitialized(), mockShardActor);
354 shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, true), getRef());
356 expectMsgClass(duration("2 seconds"), NoShardLeaderException.class);
361 public void testOnReceiveFindPrimaryForRemoteShard() throws Exception {
362 String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
364 // Create an ActorSystem ShardManager actor for member-1.
366 final ActorSystem system1 = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
367 Cluster.get(system1).join(AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2558"));
369 ActorRef mockShardActor1 = newMockShardActor(system1, Shard.DEFAULT_NAME, "member-1");
371 final TestActorRef<ForwardingShardManager> shardManager1 = TestActorRef.create(system1,
372 newPropsShardMgrWithMockShardActor("shardManager1", mockShardActor1, new ClusterWrapperImpl(system1),
373 new MockConfiguration()), shardManagerID);
375 // Create an ActorSystem ShardManager actor for member-2.
377 final ActorSystem system2 = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
379 Cluster.get(system2).join(AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2558"));
381 final ActorRef mockShardActor2 = newMockShardActor(system2, "astronauts", "member-2");
383 MockConfiguration mockConfig2 = new MockConfiguration(ImmutableMap.<String, List<String>>builder().
384 put("default", Arrays.asList("member-1", "member-2")).
385 put("astronauts", Arrays.asList("member-2")).build());
387 final TestActorRef<ForwardingShardManager> shardManager2 = TestActorRef.create(system2,
388 newPropsShardMgrWithMockShardActor("shardManager2", mockShardActor2, new ClusterWrapperImpl(system2),
389 mockConfig2), shardManagerID);
391 new JavaTestKit(system1) {{
393 shardManager1.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
394 shardManager2.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
396 shardManager2.tell(new ActorInitialized(), mockShardActor2);
398 String memberId2 = "member-2-shard-astronauts-" + shardMrgIDSuffix;
399 shardManager2.tell(new ShardLeaderStateChanged(memberId2, memberId2,
400 Optional.of(mock(DataTree.class))), mockShardActor2);
401 shardManager2.tell(new RoleChangeNotification(memberId2,
402 RaftState.Candidate.name(), RaftState.Leader.name()), mockShardActor2);
404 shardManager1.underlyingActor().waitForMemberUp();
406 shardManager1.tell(new FindPrimary("astronauts", false), getRef());
408 RemotePrimaryShardFound found = expectMsgClass(duration("5 seconds"), RemotePrimaryShardFound.class);
409 String path = found.getPrimaryPath();
410 assertTrue("Unexpected primary path " + path, path.contains("member-2-shard-astronauts-config"));
412 shardManager2.underlyingActor().verifyFindPrimary();
414 Cluster.get(system2).down(AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2558"));
416 shardManager1.underlyingActor().waitForMemberRemoved();
418 shardManager1.tell(new FindPrimary("astronauts", false), getRef());
420 expectMsgClass(duration("5 seconds"), PrimaryNotFoundException.class);
423 JavaTestKit.shutdownActorSystem(system1);
424 JavaTestKit.shutdownActorSystem(system2);
428 public void testShardAvailabilityOnChangeOfMemberReachability() throws Exception {
429 String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
431 // Create an ActorSystem ShardManager actor for member-1.
433 final ActorSystem system1 = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
434 Cluster.get(system1).join(AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2558"));
436 final ActorRef mockShardActor1 = newMockShardActor(system1, Shard.DEFAULT_NAME, "member-1");
438 final TestActorRef<ForwardingShardManager> shardManager1 = TestActorRef.create(system1,
439 newPropsShardMgrWithMockShardActor("shardManager1", mockShardActor1, new ClusterWrapperImpl(system1),
440 new MockConfiguration()), shardManagerID);
442 // Create an ActorSystem ShardManager actor for member-2.
444 final ActorSystem system2 = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
446 Cluster.get(system2).join(AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2558"));
448 final ActorRef mockShardActor2 = newMockShardActor(system2, Shard.DEFAULT_NAME, "member-2");
450 MockConfiguration mockConfig2 = new MockConfiguration(ImmutableMap.<String, List<String>>builder().
451 put("default", Arrays.asList("member-1", "member-2")).build());
453 final TestActorRef<ForwardingShardManager> shardManager2 = TestActorRef.create(system2,
454 newPropsShardMgrWithMockShardActor("shardManager2", mockShardActor2, new ClusterWrapperImpl(system2),
455 mockConfig2), shardManagerID);
457 new JavaTestKit(system1) {{
459 shardManager1.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
460 shardManager2.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
461 shardManager1.tell(new ActorInitialized(), mockShardActor1);
462 shardManager2.tell(new ActorInitialized(), mockShardActor2);
464 String memberId2 = "member-2-shard-default-" + shardMrgIDSuffix;
465 String memberId1 = "member-1-shard-default-" + shardMrgIDSuffix;
466 shardManager1.tell(new ShardLeaderStateChanged(memberId1, memberId2,
467 Optional.of(mock(DataTree.class))), mockShardActor1);
468 shardManager1.tell(new RoleChangeNotification(memberId1,
469 RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor1);
470 shardManager2.tell(new ShardLeaderStateChanged(memberId2, memberId2, Optional.of(mock(DataTree.class))),
472 shardManager2.tell(new RoleChangeNotification(memberId2,
473 RaftState.Candidate.name(), RaftState.Leader.name()), mockShardActor2);
474 shardManager1.underlyingActor().waitForMemberUp();
476 shardManager1.tell(new FindPrimary("default", true), getRef());
478 RemotePrimaryShardFound found = expectMsgClass(duration("5 seconds"), RemotePrimaryShardFound.class);
479 String path = found.getPrimaryPath();
480 assertTrue("Unexpected primary path " + path, path.contains("member-2-shard-default-config"));
482 shardManager1.underlyingActor().onReceiveCommand(MockClusterWrapper.
483 createUnreachableMember("member-2", "akka.tcp://cluster-test@127.0.0.1:2558"));
485 shardManager1.underlyingActor().waitForUnreachableMember();
487 shardManager1.tell(new FindPrimary("default", true), getRef());
489 expectMsgClass(duration("5 seconds"), NoShardLeaderException.class);
491 shardManager1.underlyingActor().onReceiveCommand(MockClusterWrapper.
492 createReachableMember("member-2", "akka.tcp://cluster-test@127.0.0.1:2558"));
494 shardManager1.underlyingActor().waitForReachableMember();
496 shardManager1.tell(new FindPrimary("default", true), getRef());
498 RemotePrimaryShardFound found1 = expectMsgClass(duration("5 seconds"), RemotePrimaryShardFound.class);
499 String path1 = found1.getPrimaryPath();
500 assertTrue("Unexpected primary path " + path1, path1.contains("member-2-shard-default-config"));
504 JavaTestKit.shutdownActorSystem(system1);
505 JavaTestKit.shutdownActorSystem(system2);
509 public void testShardAvailabilityChangeOnMemberUnreachableAndLeadershipChange() throws Exception {
510 String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
512 // Create an ActorSystem ShardManager actor for member-1.
514 final ActorSystem system1 = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
515 Cluster.get(system1).join(AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2558"));
517 final ActorRef mockShardActor1 = newMockShardActor(system1, Shard.DEFAULT_NAME, "member-1");
519 final TestActorRef<ForwardingShardManager> shardManager1 = TestActorRef.create(system1,
520 newPropsShardMgrWithMockShardActor("shardManager1", mockShardActor1, new ClusterWrapperImpl(system1),
521 new MockConfiguration()), shardManagerID);
523 // Create an ActorSystem ShardManager actor for member-2.
525 final ActorSystem system2 = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
527 Cluster.get(system2).join(AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2558"));
529 final ActorRef mockShardActor2 = newMockShardActor(system2, Shard.DEFAULT_NAME, "member-2");
531 MockConfiguration mockConfig2 = new MockConfiguration(ImmutableMap.<String, List<String>>builder().
532 put("default", Arrays.asList("member-1", "member-2")).build());
534 final TestActorRef<ForwardingShardManager> shardManager2 = TestActorRef.create(system2,
535 newPropsShardMgrWithMockShardActor("shardManager2", mockShardActor2, new ClusterWrapperImpl(system2),
536 mockConfig2), shardManagerID);
538 new JavaTestKit(system1) {{
540 shardManager1.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
541 shardManager2.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
542 shardManager1.tell(new ActorInitialized(), mockShardActor1);
543 shardManager2.tell(new ActorInitialized(), mockShardActor2);
545 String memberId2 = "member-2-shard-default-" + shardMrgIDSuffix;
546 String memberId1 = "member-1-shard-default-" + shardMrgIDSuffix;
547 shardManager1.tell(new ShardLeaderStateChanged(memberId1, memberId2,
548 Optional.of(mock(DataTree.class))), mockShardActor1);
549 shardManager1.tell(new RoleChangeNotification(memberId1,
550 RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor1);
551 shardManager2.tell(new ShardLeaderStateChanged(memberId2, memberId2, Optional.of(mock(DataTree.class))),
553 shardManager2.tell(new RoleChangeNotification(memberId2,
554 RaftState.Candidate.name(), RaftState.Leader.name()), mockShardActor2);
555 shardManager1.underlyingActor().waitForMemberUp();
557 shardManager1.tell(new FindPrimary("default", true), getRef());
559 RemotePrimaryShardFound found = expectMsgClass(duration("5 seconds"), RemotePrimaryShardFound.class);
560 String path = found.getPrimaryPath();
561 assertTrue("Unexpected primary path " + path, path.contains("member-2-shard-default-config"));
563 shardManager1.underlyingActor().onReceiveCommand(MockClusterWrapper.
564 createUnreachableMember("member-2", "akka.tcp://cluster-test@127.0.0.1:2558"));
566 shardManager1.underlyingActor().waitForUnreachableMember();
568 shardManager1.tell(new FindPrimary("default", true), getRef());
570 expectMsgClass(duration("5 seconds"), NoShardLeaderException.class);
572 shardManager1.tell(new ShardLeaderStateChanged(memberId1, memberId1, Optional.of(mock(DataTree.class))),
574 shardManager1.tell(new RoleChangeNotification(memberId1,
575 RaftState.Follower.name(), RaftState.Leader.name()), mockShardActor1);
577 shardManager1.tell(new FindPrimary("default", true), getRef());
579 LocalPrimaryShardFound found1 = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
580 String path1 = found1.getPrimaryPath();
581 assertTrue("Unexpected primary path " + path1, path1.contains("member-1-shard-default-config"));
585 JavaTestKit.shutdownActorSystem(system1);
586 JavaTestKit.shutdownActorSystem(system2);
591 public void testOnReceiveFindLocalShardForNonExistentShard() throws Exception {
592 new JavaTestKit(getSystem()) {{
593 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
595 shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
597 shardManager.tell(new FindLocalShard("non-existent", false), getRef());
599 LocalShardNotFound notFound = expectMsgClass(duration("5 seconds"), LocalShardNotFound.class);
601 assertEquals("getShardName", "non-existent", notFound.getShardName());
606 public void testOnReceiveFindLocalShardForExistentShard() throws Exception {
607 new JavaTestKit(getSystem()) {{
608 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
610 shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
611 shardManager.tell(new ActorInitialized(), mockShardActor);
613 shardManager.tell(new FindLocalShard(Shard.DEFAULT_NAME, false), getRef());
615 LocalShardFound found = expectMsgClass(duration("5 seconds"), LocalShardFound.class);
617 assertTrue("Found path contains " + found.getPath().path().toString(),
618 found.getPath().path().toString().contains("member-1-shard-default-config"));
623 public void testOnReceiveFindLocalShardForNotInitializedShard() throws Exception {
624 new JavaTestKit(getSystem()) {{
625 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
627 shardManager.tell(new FindLocalShard(Shard.DEFAULT_NAME, false), getRef());
629 expectMsgClass(duration("5 seconds"), NotInitializedException.class);
634 public void testOnReceiveFindLocalShardWaitForShardInitialized() throws Exception {
635 new JavaTestKit(getSystem()) {{
636 final ActorRef shardManager = getSystem().actorOf(newPropsShardMgrWithMockShardActor());
638 shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
640 // We're passing waitUntilInitialized = true to FindLocalShard so the response should be
641 // delayed until we send ActorInitialized.
642 Future<Object> future = Patterns.ask(shardManager, new FindLocalShard(Shard.DEFAULT_NAME, true),
643 new Timeout(5, TimeUnit.SECONDS));
645 shardManager.tell(new ActorInitialized(), mockShardActor);
647 Object resp = Await.result(future, duration("5 seconds"));
648 assertTrue("Expected: LocalShardFound, Actual: " + resp, resp instanceof LocalShardFound);
653 public void testOnRecoveryJournalIsCleaned() {
654 InMemoryJournal.addEntry(shardMgrID, 1L, new ShardManager.SchemaContextModules(
655 ImmutableSet.of("foo")));
656 InMemoryJournal.addEntry(shardMgrID, 2L, new ShardManager.SchemaContextModules(
657 ImmutableSet.of("bar")));
658 InMemoryJournal.addDeleteMessagesCompleteLatch(shardMgrID);
660 new JavaTestKit(getSystem()) {{
661 TestActorRef<TestShardManager> shardManager = TestActorRef.create(getSystem(),
662 Props.create(new TestShardManagerCreator(shardMrgIDSuffix)));
664 shardManager.underlyingActor().waitForRecoveryComplete();
665 InMemoryJournal.waitForDeleteMessagesComplete(shardMgrID);
667 // Journal entries up to the last one should've been deleted
668 Map<Long, Object> journal = InMemoryJournal.get(shardMgrID);
669 synchronized (journal) {
670 assertEquals("Journal size", 1, journal.size());
671 assertEquals("Journal entry seq #", Long.valueOf(2), journal.keySet().iterator().next());
677 public void testOnRecoveryPreviouslyKnownModulesAreDiscovered() throws Exception {
678 final ImmutableSet<String> persistedModules = ImmutableSet.of("foo", "bar");
679 InMemoryJournal.addEntry(shardMgrID, 1L, new ShardManager.SchemaContextModules(
681 new JavaTestKit(getSystem()) {{
682 TestActorRef<TestShardManager> shardManager = TestActorRef.create(getSystem(),
683 Props.create(new TestShardManagerCreator(shardMrgIDSuffix)));
685 shardManager.underlyingActor().waitForRecoveryComplete();
687 Collection<String> knownModules = shardManager.underlyingActor().getKnownModules();
689 assertEquals("getKnownModules", persistedModules, Sets.newHashSet(knownModules));
694 public void testOnUpdateSchemaContextUpdateKnownModulesIfTheyContainASuperSetOfTheKnownModules()
696 new JavaTestKit(getSystem()) {{
697 final TestActorRef<ShardManager> shardManager =
698 TestActorRef.create(getSystem(), newShardMgrProps(true));
700 assertEquals("getKnownModules size", 0, shardManager.underlyingActor().getKnownModules().size());
702 ModuleIdentifier foo = mock(ModuleIdentifier.class);
703 when(foo.getNamespace()).thenReturn(new URI("foo"));
705 Set<ModuleIdentifier> moduleIdentifierSet = new HashSet<>();
706 moduleIdentifierSet.add(foo);
708 SchemaContext schemaContext = mock(SchemaContext.class);
709 when(schemaContext.getAllModuleIdentifiers()).thenReturn(moduleIdentifierSet);
711 shardManager.underlyingActor().onReceiveCommand(new UpdateSchemaContext(schemaContext));
713 assertEquals("getKnownModules", Sets.newHashSet("foo"),
714 Sets.newHashSet(shardManager.underlyingActor().getKnownModules()));
716 ModuleIdentifier bar = mock(ModuleIdentifier.class);
717 when(bar.getNamespace()).thenReturn(new URI("bar"));
719 moduleIdentifierSet.add(bar);
721 shardManager.underlyingActor().onReceiveCommand(new UpdateSchemaContext(schemaContext));
723 assertEquals("getKnownModules", Sets.newHashSet("foo", "bar"),
724 Sets.newHashSet(shardManager.underlyingActor().getKnownModules()));
729 public void testOnUpdateSchemaContextDoNotUpdateKnownModulesIfTheyDoNotContainASuperSetOfKnownModules()
731 new JavaTestKit(getSystem()) {{
732 final TestActorRef<ShardManager> shardManager =
733 TestActorRef.create(getSystem(), newShardMgrProps(true));
735 SchemaContext schemaContext = mock(SchemaContext.class);
736 Set<ModuleIdentifier> moduleIdentifierSet = new HashSet<>();
738 ModuleIdentifier foo = mock(ModuleIdentifier.class);
739 when(foo.getNamespace()).thenReturn(new URI("foo"));
741 moduleIdentifierSet.add(foo);
743 when(schemaContext.getAllModuleIdentifiers()).thenReturn(moduleIdentifierSet);
745 shardManager.underlyingActor().onReceiveCommand(new UpdateSchemaContext(schemaContext));
747 assertEquals("getKnownModules", Sets.newHashSet("foo"),
748 Sets.newHashSet(shardManager.underlyingActor().getKnownModules()));
750 //Create a completely different SchemaContext with only the bar module in it
751 //schemaContext = mock(SchemaContext.class);
752 moduleIdentifierSet.clear();
753 ModuleIdentifier bar = mock(ModuleIdentifier.class);
754 when(bar.getNamespace()).thenReturn(new URI("bar"));
756 moduleIdentifierSet.add(bar);
758 shardManager.underlyingActor().onReceiveCommand(new UpdateSchemaContext(schemaContext));
760 assertEquals("getKnownModules", Sets.newHashSet("foo"),
761 Sets.newHashSet(shardManager.underlyingActor().getKnownModules()));
767 public void testRecoveryApplicable(){
768 new JavaTestKit(getSystem()) {
770 final Props persistentProps = newShardMgrProps(true);
771 final TestActorRef<ShardManager> persistentShardManager =
772 TestActorRef.create(getSystem(), persistentProps);
774 DataPersistenceProvider dataPersistenceProvider1 = persistentShardManager.underlyingActor().getDataPersistenceProvider();
776 assertTrue("Recovery Applicable", dataPersistenceProvider1.isRecoveryApplicable());
778 final Props nonPersistentProps = newShardMgrProps(false);
779 final TestActorRef<ShardManager> nonPersistentShardManager =
780 TestActorRef.create(getSystem(), nonPersistentProps);
782 DataPersistenceProvider dataPersistenceProvider2 = nonPersistentShardManager.underlyingActor().getDataPersistenceProvider();
784 assertFalse("Recovery Not Applicable", dataPersistenceProvider2.isRecoveryApplicable());
792 public void testOnUpdateSchemaContextUpdateKnownModulesCallsDataPersistenceProvider()
794 final CountDownLatch persistLatch = new CountDownLatch(1);
795 final Creator<ShardManager> creator = new Creator<ShardManager>() {
796 private static final long serialVersionUID = 1L;
798 public ShardManager create() throws Exception {
799 return new ShardManager(new MockClusterWrapper(), new MockConfiguration(), DatastoreContext.newBuilder().build(),
800 ready, new PrimaryShardInfoFutureCache()) {
802 protected DataPersistenceProvider createDataPersistenceProvider(boolean persistent) {
803 DataPersistenceProviderMonitor dataPersistenceProviderMonitor
804 = new DataPersistenceProviderMonitor();
805 dataPersistenceProviderMonitor.setPersistLatch(persistLatch);
806 return dataPersistenceProviderMonitor;
812 new JavaTestKit(getSystem()) {{
814 final TestActorRef<ShardManager> shardManager =
815 TestActorRef.create(getSystem(), Props.create(new DelegatingShardManagerCreator(creator)));
817 ModuleIdentifier foo = mock(ModuleIdentifier.class);
818 when(foo.getNamespace()).thenReturn(new URI("foo"));
820 Set<ModuleIdentifier> moduleIdentifierSet = new HashSet<>();
821 moduleIdentifierSet.add(foo);
823 SchemaContext schemaContext = mock(SchemaContext.class);
824 when(schemaContext.getAllModuleIdentifiers()).thenReturn(moduleIdentifierSet);
826 shardManager.underlyingActor().onReceiveCommand(new UpdateSchemaContext(schemaContext));
828 assertEquals("Persisted", true,
829 Uninterruptibles.awaitUninterruptibly(persistLatch, 5, TimeUnit.SECONDS));
835 public void testRoleChangeNotificationAndShardLeaderStateChangedReleaseReady() throws Exception {
836 new JavaTestKit(getSystem()) {
838 TestActorRef<ShardManager> shardManager = TestActorRef.create(getSystem(), newShardMgrProps(true));
840 String memberId = "member-1-shard-default-" + shardMrgIDSuffix;
841 shardManager.underlyingActor().onReceiveCommand(new RoleChangeNotification(
842 memberId, RaftState.Candidate.name(), RaftState.Leader.name()));
844 verify(ready, never()).countDown();
846 shardManager.underlyingActor().onReceiveCommand(new ShardLeaderStateChanged(memberId, memberId,
847 Optional.of(mock(DataTree.class))));
849 verify(ready, times(1)).countDown();
855 public void testRoleChangeNotificationToFollowerWithShardLeaderStateChangedReleaseReady() throws Exception {
856 new JavaTestKit(getSystem()) {
858 TestActorRef<ShardManager> shardManager = TestActorRef.create(getSystem(), newShardMgrProps(true));
860 String memberId = "member-1-shard-default-" + shardMrgIDSuffix;
861 shardManager.underlyingActor().onReceiveCommand(new RoleChangeNotification(
862 memberId, null, RaftState.Follower.name()));
864 verify(ready, never()).countDown();
866 shardManager.underlyingActor().onReceiveCommand(MockClusterWrapper.createMemberUp("member-2", getRef().path().toString()));
868 shardManager.underlyingActor().onReceiveCommand(new ShardLeaderStateChanged(memberId,
869 "member-2-shard-default-" + shardMrgIDSuffix, Optional.of(mock(DataTree.class))));
871 verify(ready, times(1)).countDown();
877 public void testReadyCountDownForMemberUpAfterLeaderStateChanged() throws Exception {
878 new JavaTestKit(getSystem()) {
880 TestActorRef<ShardManager> shardManager = TestActorRef.create(getSystem(), newShardMgrProps(true));
882 String memberId = "member-1-shard-default-" + shardMrgIDSuffix;
883 shardManager.underlyingActor().onReceiveCommand(new RoleChangeNotification(
884 memberId, null, RaftState.Follower.name()));
886 verify(ready, never()).countDown();
888 shardManager.underlyingActor().onReceiveCommand(new ShardLeaderStateChanged(memberId,
889 "member-2-shard-default-" + shardMrgIDSuffix, Optional.of(mock(DataTree.class))));
891 shardManager.underlyingActor().onReceiveCommand(MockClusterWrapper.createMemberUp("member-2", getRef().path().toString()));
893 verify(ready, times(1)).countDown();
899 public void testRoleChangeNotificationDoNothingForUnknownShard() throws Exception {
900 new JavaTestKit(getSystem()) {
902 TestActorRef<ShardManager> shardManager = TestActorRef.create(getSystem(), newShardMgrProps(true));
904 shardManager.underlyingActor().onReceiveCommand(new RoleChangeNotification(
905 "unknown", RaftState.Candidate.name(), RaftState.Leader.name()));
907 verify(ready, never()).countDown();
914 public void testByDefaultSyncStatusIsFalse() throws Exception{
915 final Props persistentProps = newShardMgrProps(true);
916 final TestActorRef<ShardManager> shardManager =
917 TestActorRef.create(getSystem(), persistentProps);
919 ShardManager shardManagerActor = shardManager.underlyingActor();
921 assertEquals(false, shardManagerActor.getMBean().getSyncStatus());
925 public void testWhenShardIsLeaderSyncStatusIsTrue() throws Exception{
926 final Props persistentProps = ShardManager.props(
927 new MockClusterWrapper(),
928 new MockConfiguration(),
929 DatastoreContext.newBuilder().persistent(true).build(), ready, primaryShardInfoCache);
930 final TestActorRef<ShardManager> shardManager =
931 TestActorRef.create(getSystem(), persistentProps);
933 ShardManager shardManagerActor = shardManager.underlyingActor();
934 shardManagerActor.onReceiveCommand(new RoleChangeNotification("member-1-shard-default-unknown",
935 RaftState.Follower.name(), RaftState.Leader.name()));
937 assertEquals(true, shardManagerActor.getMBean().getSyncStatus());
941 public void testWhenShardIsCandidateSyncStatusIsFalse() throws Exception{
942 final Props persistentProps = newShardMgrProps(true);
943 final TestActorRef<ShardManager> shardManager =
944 TestActorRef.create(getSystem(), persistentProps);
946 ShardManager shardManagerActor = shardManager.underlyingActor();
947 shardManagerActor.onReceiveCommand(new RoleChangeNotification("member-1-shard-default-unknown",
948 RaftState.Follower.name(), RaftState.Candidate.name()));
950 assertEquals(false, shardManagerActor.getMBean().getSyncStatus());
952 // Send a FollowerInitialSyncStatus with status = true for the replica whose current state is candidate
953 shardManagerActor.onReceiveCommand(new FollowerInitialSyncUpStatus(true, "member-1-shard-default-unknown"));
955 assertEquals(false, shardManagerActor.getMBean().getSyncStatus());
959 public void testWhenShardIsFollowerSyncStatusDependsOnFollowerInitialSyncStatus() throws Exception{
960 final Props persistentProps = ShardManager.props(
961 new MockClusterWrapper(),
962 new MockConfiguration(),
963 DatastoreContext.newBuilder().persistent(true).build(), ready, primaryShardInfoCache);
964 final TestActorRef<ShardManager> shardManager =
965 TestActorRef.create(getSystem(), persistentProps);
967 ShardManager shardManagerActor = shardManager.underlyingActor();
968 shardManagerActor.onReceiveCommand(new RoleChangeNotification("member-1-shard-default-unknown",
969 RaftState.Candidate.name(), RaftState.Follower.name()));
971 // Initially will be false
972 assertEquals(false, shardManagerActor.getMBean().getSyncStatus());
974 // Send status true will make sync status true
975 shardManagerActor.onReceiveCommand(new FollowerInitialSyncUpStatus(true, "member-1-shard-default-unknown"));
977 assertEquals(true, shardManagerActor.getMBean().getSyncStatus());
979 // Send status false will make sync status false
980 shardManagerActor.onReceiveCommand(new FollowerInitialSyncUpStatus(false, "member-1-shard-default-unknown"));
982 assertEquals(false, shardManagerActor.getMBean().getSyncStatus());
987 public void testWhenMultipleShardsPresentSyncStatusMustBeTrueForAllShards() throws Exception{
988 final Props persistentProps = ShardManager.props(
989 new MockClusterWrapper(),
990 new MockConfiguration() {
992 public List<String> getMemberShardNames(String memberName) {
993 return Arrays.asList("default", "astronauts");
996 DatastoreContext.newBuilder().persistent(true).build(), ready, primaryShardInfoCache);
997 final TestActorRef<ShardManager> shardManager =
998 TestActorRef.create(getSystem(), persistentProps);
1000 ShardManager shardManagerActor = shardManager.underlyingActor();
1002 // Initially will be false
1003 assertEquals(false, shardManagerActor.getMBean().getSyncStatus());
1005 // Make default shard leader
1006 shardManagerActor.onReceiveCommand(new RoleChangeNotification("member-1-shard-default-unknown",
1007 RaftState.Follower.name(), RaftState.Leader.name()));
1009 // default = Leader, astronauts is unknown so sync status remains false
1010 assertEquals(false, shardManagerActor.getMBean().getSyncStatus());
1012 // Make astronauts shard leader as well
1013 shardManagerActor.onReceiveCommand(new RoleChangeNotification("member-1-shard-astronauts-unknown",
1014 RaftState.Follower.name(), RaftState.Leader.name()));
1016 // Now sync status should be true
1017 assertEquals(true, shardManagerActor.getMBean().getSyncStatus());
1019 // Make astronauts a Follower
1020 shardManagerActor.onReceiveCommand(new RoleChangeNotification("member-1-shard-astronauts-unknown",
1021 RaftState.Leader.name(), RaftState.Follower.name()));
1023 // Sync status is not true
1024 assertEquals(false, shardManagerActor.getMBean().getSyncStatus());
1026 // Make the astronauts follower sync status true
1027 shardManagerActor.onReceiveCommand(new FollowerInitialSyncUpStatus(true, "member-1-shard-astronauts-unknown"));
1029 // Sync status is now true
1030 assertEquals(true, shardManagerActor.getMBean().getSyncStatus());
1034 private static class TestShardManager extends ShardManager {
1035 private final CountDownLatch recoveryComplete = new CountDownLatch(1);
1037 TestShardManager(String shardMrgIDSuffix) {
1038 super(new MockClusterWrapper(), new MockConfiguration(),
1039 DatastoreContext.newBuilder().dataStoreType(shardMrgIDSuffix).build(), ready,
1040 new PrimaryShardInfoFutureCache());
1044 public void handleRecover(Object message) throws Exception {
1046 super.handleRecover(message);
1048 if(message instanceof RecoveryCompleted) {
1049 recoveryComplete.countDown();
1054 void waitForRecoveryComplete() {
1055 assertEquals("Recovery complete", true,
1056 Uninterruptibles.awaitUninterruptibly(recoveryComplete, 5, TimeUnit.SECONDS));
1060 @SuppressWarnings("serial")
1061 static class TestShardManagerCreator implements Creator<TestShardManager> {
1062 String shardMrgIDSuffix;
1064 TestShardManagerCreator(String shardMrgIDSuffix) {
1065 this.shardMrgIDSuffix = shardMrgIDSuffix;
1069 public TestShardManager create() throws Exception {
1070 return new TestShardManager(shardMrgIDSuffix);
1075 private static class DelegatingShardManagerCreator implements Creator<ShardManager> {
1076 private static final long serialVersionUID = 1L;
1077 private final Creator<ShardManager> delegate;
1079 public DelegatingShardManagerCreator(Creator<ShardManager> delegate) {
1080 this.delegate = delegate;
1084 public ShardManager create() throws Exception {
1085 return delegate.create();
1089 private static class ForwardingShardManager extends ShardManager {
1090 private CountDownLatch findPrimaryMessageReceived = new CountDownLatch(1);
1091 private CountDownLatch memberUpReceived = new CountDownLatch(1);
1092 private CountDownLatch memberRemovedReceived = new CountDownLatch(1);
1093 private CountDownLatch memberUnreachableReceived = new CountDownLatch(1);
1094 private CountDownLatch memberReachableReceived = new CountDownLatch(1);
1095 private final ActorRef shardActor;
1096 private final String name;
1098 protected ForwardingShardManager(ClusterWrapper cluster, Configuration configuration,
1099 DatastoreContext datastoreContext, CountDownLatch waitTillReadyCountdownLatch, String name,
1100 ActorRef shardActor, PrimaryShardInfoFutureCache primaryShardInfoCache) {
1101 super(cluster, configuration, datastoreContext, waitTillReadyCountdownLatch, primaryShardInfoCache);
1102 this.shardActor = shardActor;
1107 public void handleCommand(Object message) throws Exception {
1109 super.handleCommand(message);
1111 if(message instanceof FindPrimary) {
1112 findPrimaryMessageReceived.countDown();
1113 } else if(message instanceof ClusterEvent.MemberUp) {
1114 String role = ((ClusterEvent.MemberUp)message).member().roles().head();
1115 if(!getCluster().getCurrentMemberName().equals(role)) {
1116 memberUpReceived.countDown();
1118 } else if(message instanceof ClusterEvent.MemberRemoved) {
1119 String role = ((ClusterEvent.MemberRemoved)message).member().roles().head();
1120 if(!getCluster().getCurrentMemberName().equals(role)) {
1121 memberRemovedReceived.countDown();
1123 } else if(message instanceof ClusterEvent.UnreachableMember) {
1124 String role = ((ClusterEvent.UnreachableMember)message).member().roles().head();
1125 if(!getCluster().getCurrentMemberName().equals(role)) {
1126 memberUnreachableReceived.countDown();
1128 } else if(message instanceof ClusterEvent.ReachableMember) {
1129 String role = ((ClusterEvent.ReachableMember)message).member().roles().head();
1130 if(!getCluster().getCurrentMemberName().equals(role)) {
1131 memberReachableReceived.countDown();
1138 public String persistenceId() {
1143 protected ActorRef newShardActor(SchemaContext schemaContext, ShardInformation info) {
1147 void waitForMemberUp() {
1148 assertEquals("MemberUp received", true,
1149 Uninterruptibles.awaitUninterruptibly(memberUpReceived, 5, TimeUnit.SECONDS));
1150 memberUpReceived = new CountDownLatch(1);
1153 void waitForMemberRemoved() {
1154 assertEquals("MemberRemoved received", true,
1155 Uninterruptibles.awaitUninterruptibly(memberRemovedReceived, 5, TimeUnit.SECONDS));
1156 memberRemovedReceived = new CountDownLatch(1);
1159 void waitForUnreachableMember() {
1160 assertEquals("UnreachableMember received", true,
1161 Uninterruptibles.awaitUninterruptibly(memberUnreachableReceived, 5, TimeUnit.SECONDS
1163 memberUnreachableReceived = new CountDownLatch(1);
1166 void waitForReachableMember() {
1167 assertEquals("ReachableMember received", true,
1168 Uninterruptibles.awaitUninterruptibly(memberReachableReceived, 5, TimeUnit.SECONDS));
1169 memberReachableReceived = new CountDownLatch(1);
1172 void verifyFindPrimary() {
1173 assertEquals("FindPrimary received", true,
1174 Uninterruptibles.awaitUninterruptibly(findPrimaryMessageReceived, 5, TimeUnit.SECONDS));
1175 findPrimaryMessageReceived = new CountDownLatch(1);