use of org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound in project controller by opendaylight.
the class ShardManagerTest method testOnReceiveFindPrimaryForLocalLeaderShard.
@Test
public void testOnReceiveFindPrimaryForLocalLeaderShard() throws Exception {
LOG.info("testOnReceiveFindPrimaryForLocalLeaderShard starting");
new TestKit(getSystem()) {
{
String memberId = "member-1-shard-default-" + shardMrgIDSuffix;
final ActorRef shardManager = actorFactory.createActor(newPropsShardMgrWithMockShardActor());
shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager.tell(new ActorInitialized(), mockShardActor);
DataTree mockDataTree = mock(DataTree.class);
shardManager.tell(new ShardLeaderStateChanged(memberId, memberId, mockDataTree, DataStoreVersions.CURRENT_VERSION), getRef());
MessageCollectorActor.expectFirstMatching(mockShardActor, RegisterRoleChangeListener.class);
shardManager.tell(new RoleChangeNotification(memberId, RaftState.Candidate.name(), RaftState.Leader.name()), mockShardActor);
shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
LocalPrimaryShardFound primaryFound = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
assertTrue("Unexpected primary path " + primaryFound.getPrimaryPath(), primaryFound.getPrimaryPath().contains("member-1-shard-default"));
assertSame("getLocalShardDataTree", mockDataTree, primaryFound.getLocalShardDataTree());
}
};
LOG.info("testOnReceiveFindPrimaryForLocalLeaderShard ending");
}
use of org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound in project controller by opendaylight.
the class ShardManagerTest method testOnReceiveFindPrimaryForFollowerShardWithNoInitialLeaderId.
@Test
public void testOnReceiveFindPrimaryForFollowerShardWithNoInitialLeaderId() throws Exception {
LOG.info("testOnReceiveFindPrimaryForFollowerShardWithNoInitialLeaderId starting");
new TestKit(getSystem()) {
{
final ActorRef shardManager = actorFactory.createActor(newPropsShardMgrWithMockShardActor());
shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
shardManager.tell(new ActorInitialized(), mockShardActor);
String memberId = "member-1-shard-default-" + shardMrgIDSuffix;
shardManager.tell(new RoleChangeNotification(memberId, RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor);
shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
expectMsgClass(duration("5 seconds"), NoShardLeaderException.class);
DataTree mockDataTree = mock(DataTree.class);
shardManager.tell(new ShardLeaderStateChanged(memberId, memberId, mockDataTree, DataStoreVersions.CURRENT_VERSION), mockShardActor);
shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
LocalPrimaryShardFound primaryFound = expectMsgClass(duration("5 seconds"), LocalPrimaryShardFound.class);
assertTrue("Unexpected primary path " + primaryFound.getPrimaryPath(), primaryFound.getPrimaryPath().contains("member-1-shard-default"));
assertSame("getLocalShardDataTree", mockDataTree, primaryFound.getLocalShardDataTree());
}
};
LOG.info("testOnReceiveFindPrimaryForFollowerShardWithNoInitialLeaderId starting");
}
use of org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound in project controller by opendaylight.
the class ActorContextTest method testFindPrimaryShardAsyncLocalPrimaryFound.
@Test
public void testFindPrimaryShardAsyncLocalPrimaryFound() throws Exception {
ActorRef shardManager = getSystem().actorOf(MessageCollectorActor.props());
DatastoreContext dataStoreContext = DatastoreContext.newBuilder().logicalStoreType(LogicalDatastoreType.CONFIGURATION).shardLeaderElectionTimeout(100, TimeUnit.MILLISECONDS).build();
final DataTree mockDataTree = Mockito.mock(DataTree.class);
final String expPrimaryPath = "akka://test-system/find-primary-shard";
ActorContext actorContext = new ActorContext(getSystem(), shardManager, mock(ClusterWrapper.class), mock(Configuration.class), dataStoreContext, new PrimaryShardInfoFutureCache()) {
@Override
protected Future<Object> doAsk(final ActorRef actorRef, final Object message, final Timeout timeout) {
return Futures.successful((Object) new LocalPrimaryShardFound(expPrimaryPath, mockDataTree));
}
};
Future<PrimaryShardInfo> foobar = actorContext.findPrimaryShardAsync("foobar");
PrimaryShardInfo actual = Await.result(foobar, Duration.apply(5000, TimeUnit.MILLISECONDS));
assertNotNull(actual);
assertEquals("LocalShardDataTree present", true, actual.getLocalShardDataTree().isPresent());
assertSame("LocalShardDataTree", mockDataTree, actual.getLocalShardDataTree().get());
assertTrue("Unexpected PrimaryShardActor path " + actual.getPrimaryShardActor().path(), expPrimaryPath.endsWith(actual.getPrimaryShardActor().pathString()));
assertEquals("getPrimaryShardVersion", DataStoreVersions.CURRENT_VERSION, actual.getPrimaryShardVersion());
Future<PrimaryShardInfo> cached = actorContext.getPrimaryShardInfoCache().getIfPresent("foobar");
PrimaryShardInfo cachedInfo = Await.result(cached, FiniteDuration.apply(1, TimeUnit.MILLISECONDS));
assertEquals(cachedInfo, actual);
actorContext.getPrimaryShardInfoCache().remove("foobar");
cached = actorContext.getPrimaryShardInfoCache().getIfPresent("foobar");
assertNull(cached);
}
use of org.opendaylight.controller.cluster.datastore.messages.LocalPrimaryShardFound 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.LocalPrimaryShardFound 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