use of org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound in project controller by opendaylight.
the class ActorContextTest method testBroadcast.
@Test
public void testBroadcast() {
new TestKit(getSystem()) {
{
ActorRef shardActorRef1 = getSystem().actorOf(MessageCollectorActor.props());
ActorRef shardActorRef2 = getSystem().actorOf(MessageCollectorActor.props());
TestActorRef<MockShardManager> shardManagerActorRef = TestActorRef.create(getSystem(), MockShardManager.props());
MockShardManager shardManagerActor = shardManagerActorRef.underlyingActor();
shardManagerActor.addFindPrimaryResp("shard1", new RemotePrimaryShardFound(shardActorRef1.path().toString(), DataStoreVersions.CURRENT_VERSION));
shardManagerActor.addFindPrimaryResp("shard2", new RemotePrimaryShardFound(shardActorRef2.path().toString(), DataStoreVersions.CURRENT_VERSION));
shardManagerActor.addFindPrimaryResp("shard3", new NoShardLeaderException("not found"));
Configuration mockConfig = mock(Configuration.class);
doReturn(Sets.newLinkedHashSet(Arrays.asList("shard1", "shard2", "shard3"))).when(mockConfig).getAllShardNames();
ActorContext actorContext = new ActorContext(getSystem(), shardManagerActorRef, mock(ClusterWrapper.class), mockConfig, DatastoreContext.newBuilder().shardInitializationTimeout(200, TimeUnit.MILLISECONDS).build(), new PrimaryShardInfoFutureCache());
actorContext.broadcast(v -> new TestMessage(), TestMessage.class);
MessageCollectorActor.expectFirstMatching(shardActorRef1, TestMessage.class);
MessageCollectorActor.expectFirstMatching(shardActorRef2, TestMessage.class);
}
};
}
use of org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound in project controller by opendaylight.
the class ShardManagerTest method testOnReceiveFindPrimaryForNonLocalLeaderShard.
@Test
public void testOnReceiveFindPrimaryForNonLocalLeaderShard() throws Exception {
LOG.info("testOnReceiveFindPrimaryForNonLocalLeaderShard starting");
new TestKit(getSystem()) {
{
final ActorRef shardManager = actorFactory.createActor(newPropsShardMgrWithMockShardActor());
shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager.tell(new ActorInitialized(), mockShardActor);
String memberId2 = "member-2-shard-default-" + shardMrgIDSuffix;
MockClusterWrapper.sendMemberUp(shardManager, "member-2", getRef().path().toString());
String memberId1 = "member-1-shard-default-" + shardMrgIDSuffix;
shardManager.tell(new RoleChangeNotification(memberId1, RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor);
short leaderVersion = DataStoreVersions.CURRENT_VERSION - 1;
shardManager.tell(new ShardLeaderStateChanged(memberId1, memberId2, leaderVersion), mockShardActor);
shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
RemotePrimaryShardFound primaryFound = expectMsgClass(duration("5 seconds"), RemotePrimaryShardFound.class);
assertTrue("Unexpected primary path " + primaryFound.getPrimaryPath(), primaryFound.getPrimaryPath().contains("member-2-shard-default"));
assertEquals("getPrimaryVersion", leaderVersion, primaryFound.getPrimaryVersion());
}
};
LOG.info("testOnReceiveFindPrimaryForNonLocalLeaderShard ending");
}
use of org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound in project controller by opendaylight.
the class ShardManagerTest method testShardAvailabilityOnChangeOfMemberReachability.
@Test
public void testShardAvailabilityOnChangeOfMemberReachability() throws Exception {
LOG.info("testShardAvailabilityOnChangeOfMemberReachability starting");
String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
// Create an ActorSystem ShardManager actor for member-1.
final ActorSystem system1 = newActorSystem("Member1");
Cluster.get(system1).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
final ActorRef mockShardActor1 = newMockShardActor(system1, Shard.DEFAULT_NAME, "member-1");
final TestActorRef<TestShardManager> shardManager1 = TestActorRef.create(system1, newTestShardMgrBuilder().shardActor(mockShardActor1).cluster(new ClusterWrapperImpl(system1)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
// Create an ActorSystem ShardManager actor for member-2.
final ActorSystem system2 = newActorSystem("Member2");
Cluster.get(system2).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
final ActorRef mockShardActor2 = newMockShardActor(system2, Shard.DEFAULT_NAME, "member-2");
MockConfiguration mockConfig2 = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("default", Arrays.asList("member-1", "member-2")).build());
final TestActorRef<TestShardManager> shardManager2 = TestActorRef.create(system2, newTestShardMgrBuilder(mockConfig2).shardActor(mockShardActor2).cluster(new ClusterWrapperImpl(system2)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
new TestKit(system1) {
{
shardManager1.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager2.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager1.tell(new ActorInitialized(), mockShardActor1);
shardManager2.tell(new ActorInitialized(), mockShardActor2);
String memberId2 = "member-2-shard-default-" + shardMrgIDSuffix;
String memberId1 = "member-1-shard-default-" + shardMrgIDSuffix;
shardManager1.tell(new ShardLeaderStateChanged(memberId1, memberId2, mock(DataTree.class), DataStoreVersions.CURRENT_VERSION), mockShardActor1);
shardManager1.tell(new RoleChangeNotification(memberId1, RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor1);
shardManager2.tell(new ShardLeaderStateChanged(memberId2, memberId2, mock(DataTree.class), DataStoreVersions.CURRENT_VERSION), mockShardActor2);
shardManager2.tell(new RoleChangeNotification(memberId2, RaftState.Candidate.name(), RaftState.Leader.name()), mockShardActor2);
shardManager1.underlyingActor().waitForMemberUp();
shardManager1.tell(new FindPrimary("default", true), getRef());
RemotePrimaryShardFound found = expectMsgClass(duration("5 seconds"), RemotePrimaryShardFound.class);
String path = found.getPrimaryPath();
assertTrue("Unexpected primary path " + path, path.contains("member-2-shard-default-config"));
shardManager1.tell(MockClusterWrapper.createUnreachableMember("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
shardManager1.underlyingActor().waitForUnreachableMember();
PeerDown peerDown = MessageCollectorActor.expectFirstMatching(mockShardActor1, PeerDown.class);
assertEquals("getMemberName", MEMBER_2, peerDown.getMemberName());
MessageCollectorActor.clearMessages(mockShardActor1);
shardManager1.tell(MockClusterWrapper.createMemberRemoved("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
MessageCollectorActor.expectFirstMatching(mockShardActor1, PeerDown.class);
shardManager1.tell(new FindPrimary("default", true), getRef());
expectMsgClass(duration("5 seconds"), NoShardLeaderException.class);
shardManager1.tell(MockClusterWrapper.createReachableMember("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
shardManager1.underlyingActor().waitForReachableMember();
PeerUp peerUp = MessageCollectorActor.expectFirstMatching(mockShardActor1, PeerUp.class);
assertEquals("getMemberName", MEMBER_2, peerUp.getMemberName());
MessageCollectorActor.clearMessages(mockShardActor1);
shardManager1.tell(new FindPrimary("default", true), getRef());
RemotePrimaryShardFound found1 = expectMsgClass(duration("5 seconds"), RemotePrimaryShardFound.class);
String path1 = found1.getPrimaryPath();
assertTrue("Unexpected primary path " + path1, path1.contains("member-2-shard-default-config"));
shardManager1.tell(MockClusterWrapper.createMemberUp("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
MessageCollectorActor.expectFirstMatching(mockShardActor1, PeerUp.class);
// Test FindPrimary wait succeeds after reachable member event.
shardManager1.tell(MockClusterWrapper.createUnreachableMember("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
shardManager1.underlyingActor().waitForUnreachableMember();
shardManager1.tell(new FindPrimary("default", true), getRef());
shardManager1.tell(MockClusterWrapper.createReachableMember("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
RemotePrimaryShardFound found2 = expectMsgClass(duration("5 seconds"), RemotePrimaryShardFound.class);
String path2 = found2.getPrimaryPath();
assertTrue("Unexpected primary path " + path2, path2.contains("member-2-shard-default-config"));
}
};
LOG.info("testShardAvailabilityOnChangeOfMemberReachability ending");
}
use of org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound in project controller by opendaylight.
the class ShardManager method onAddPrefixShardReplica.
private void onAddPrefixShardReplica(final AddPrefixShardReplica message) {
LOG.debug("{}: onAddPrefixShardReplica: {}", persistenceId(), message);
final ShardIdentifier shardId = getShardIdentifier(cluster.getCurrentMemberName(), ClusterUtils.getCleanShardName(message.getShardPrefix()));
final String shardName = shardId.getShardName();
// Create the localShard
if (schemaContext == null) {
String msg = String.format("No SchemaContext is available in order to create a local shard instance for %s", shardName);
LOG.debug("{}: {}", persistenceId(), msg);
getSender().tell(new Status.Failure(new IllegalStateException(msg)), getSelf());
return;
}
findPrimary(shardName, new AutoFindPrimaryFailureResponseHandler(getSender(), shardName, persistenceId(), getSelf()) {
@Override
public void onRemotePrimaryShardFound(final RemotePrimaryShardFound response) {
final RunnableMessage runnable = (RunnableMessage) () -> addPrefixShard(getShardName(), message.getShardPrefix(), response, getSender());
if (!isPreviousShardActorStopInProgress(getShardName(), runnable)) {
getSelf().tell(runnable, getTargetActor());
}
}
@Override
public void onLocalPrimaryFound(final LocalPrimaryShardFound message) {
sendLocalReplicaAlreadyExistsReply(getShardName(), getTargetActor());
}
});
}
use of org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound in project controller by opendaylight.
the class ShardManager method findPrimary.
private void findPrimary(final FindPrimary message) {
LOG.debug("{}: In findPrimary: {}", persistenceId(), message);
final String shardName = message.getShardName();
final boolean canReturnLocalShardState = !(message instanceof RemoteFindPrimary);
// First see if the there is a local replica for the shard
final ShardInformation info = localShards.get(shardName);
if (info != null && info.isActiveMember()) {
sendResponse(info, message.isWaitUntilReady(), true, () -> {
String primaryPath = info.getSerializedLeaderActor();
Object found = canReturnLocalShardState && info.isLeader() ? new LocalPrimaryShardFound(primaryPath, info.getLocalShardDataTree().get()) : new RemotePrimaryShardFound(primaryPath, info.getLeaderVersion());
LOG.debug("{}: Found primary for {}: {}", persistenceId(), shardName, found);
return found;
});
return;
}
final Collection<String> visitedAddresses;
if (message instanceof RemoteFindPrimary) {
visitedAddresses = ((RemoteFindPrimary) message).getVisitedAddresses();
} else {
visitedAddresses = new ArrayList<>(1);
}
visitedAddresses.add(peerAddressResolver.getShardManagerActorPathBuilder(cluster.getSelfAddress()).toString());
for (String address : peerAddressResolver.getShardManagerPeerActorAddresses()) {
if (visitedAddresses.contains(address)) {
continue;
}
LOG.debug("{}: findPrimary for {} forwarding to remote ShardManager {}, visitedAddresses: {}", persistenceId(), shardName, address, visitedAddresses);
getContext().actorSelection(address).forward(new RemoteFindPrimary(shardName, message.isWaitUntilReady(), visitedAddresses), getContext());
return;
}
LOG.debug("{}: No shard found for {}", persistenceId(), shardName);
getSender().tell(new PrimaryNotFoundException(String.format("No primary shard found for %s.", shardName)), getSelf());
}
Aggregations