use of org.opendaylight.controller.cluster.datastore.DatastoreContext in project controller by opendaylight.
the class ShardManagerTest method testPerShardDatastoreContext.
@Test
public void testPerShardDatastoreContext() throws Exception {
LOG.info("testPerShardDatastoreContext starting");
final DatastoreContextFactory mockFactory = newDatastoreContextFactory(datastoreContextBuilder.shardElectionTimeoutFactor(5).build());
Mockito.doReturn(DatastoreContext.newBuilderFrom(datastoreContextBuilder.build()).shardElectionTimeoutFactor(6).build()).when(mockFactory).getShardDatastoreContext("default");
Mockito.doReturn(DatastoreContext.newBuilderFrom(datastoreContextBuilder.build()).shardElectionTimeoutFactor(7).build()).when(mockFactory).getShardDatastoreContext("topology");
final MockConfiguration mockConfig = new MockConfiguration() {
@Override
public Collection<String> getMemberShardNames(final MemberName memberName) {
return Arrays.asList("default", "topology");
}
@Override
public Collection<MemberName> getMembersFromShardName(final String shardName) {
return members("member-1");
}
};
final ActorRef defaultShardActor = actorFactory.createActor(MessageCollectorActor.props(), actorFactory.generateActorId("default"));
final ActorRef topologyShardActor = actorFactory.createActor(MessageCollectorActor.props(), actorFactory.generateActorId("topology"));
final Map<String, Entry<ActorRef, DatastoreContext>> shardInfoMap = Collections.synchronizedMap(new HashMap<String, Entry<ActorRef, DatastoreContext>>());
shardInfoMap.put("default", new AbstractMap.SimpleEntry<>(defaultShardActor, null));
shardInfoMap.put("topology", new AbstractMap.SimpleEntry<>(topologyShardActor, null));
final PrimaryShardInfoFutureCache primaryShardInfoCache = new PrimaryShardInfoFutureCache();
final CountDownLatch newShardActorLatch = new CountDownLatch(2);
class LocalShardManager extends ShardManager {
LocalShardManager(final AbstractShardManagerCreator<?> creator) {
super(creator);
}
@Override
protected ActorRef newShardActor(final ShardInformation info) {
Entry<ActorRef, DatastoreContext> entry = shardInfoMap.get(info.getShardName());
ActorRef ref = null;
if (entry != null) {
ref = entry.getKey();
entry.setValue(info.getDatastoreContext());
}
newShardActorLatch.countDown();
return ref;
}
}
final Creator<ShardManager> creator = new Creator<ShardManager>() {
private static final long serialVersionUID = 1L;
@Override
public ShardManager create() throws Exception {
return new LocalShardManager(new GenericCreator<>(LocalShardManager.class).datastoreContextFactory(mockFactory).primaryShardInfoCache(primaryShardInfoCache).configuration(mockConfig));
}
};
TestKit kit = new TestKit(getSystem());
final ActorRef shardManager = actorFactory.createActor(Props.create(new DelegatingShardManagerCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()));
shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), kit.getRef());
assertEquals("Shard actors created", true, newShardActorLatch.await(5, TimeUnit.SECONDS));
assertEquals("getShardElectionTimeoutFactor", 6, shardInfoMap.get("default").getValue().getShardElectionTimeoutFactor());
assertEquals("getShardElectionTimeoutFactor", 7, shardInfoMap.get("topology").getValue().getShardElectionTimeoutFactor());
DatastoreContextFactory newMockFactory = newDatastoreContextFactory(datastoreContextBuilder.shardElectionTimeoutFactor(5).build());
Mockito.doReturn(DatastoreContext.newBuilderFrom(datastoreContextBuilder.build()).shardElectionTimeoutFactor(66).build()).when(newMockFactory).getShardDatastoreContext("default");
Mockito.doReturn(DatastoreContext.newBuilderFrom(datastoreContextBuilder.build()).shardElectionTimeoutFactor(77).build()).when(newMockFactory).getShardDatastoreContext("topology");
shardManager.tell(newMockFactory, kit.getRef());
DatastoreContext newContext = MessageCollectorActor.expectFirstMatching(defaultShardActor, DatastoreContext.class);
assertEquals("getShardElectionTimeoutFactor", 66, newContext.getShardElectionTimeoutFactor());
newContext = MessageCollectorActor.expectFirstMatching(topologyShardActor, DatastoreContext.class);
assertEquals("getShardElectionTimeoutFactor", 77, newContext.getShardElectionTimeoutFactor());
LOG.info("testPerShardDatastoreContext ending");
}
use of org.opendaylight.controller.cluster.datastore.DatastoreContext in project controller by opendaylight.
the class ShardManagerTest method testOnCreateShard.
@Test
public void testOnCreateShard() {
LOG.info("testOnCreateShard starting");
new TestKit(getSystem()) {
{
datastoreContextBuilder.shardInitializationTimeout(1, TimeUnit.MINUTES).persistent(true);
ActorRef shardManager = actorFactory.createActor(newShardMgrProps(new ConfigurationImpl(new EmptyModuleShardConfigProvider())).withDispatcher(Dispatchers.DefaultDispatcherId()));
SchemaContext schemaContext = TestModel.createTestContext();
shardManager.tell(new UpdateSchemaContext(schemaContext), ActorRef.noSender());
DatastoreContext datastoreContext = DatastoreContext.newBuilder().shardElectionTimeoutFactor(100).persistent(false).build();
Shard.Builder shardBuilder = Shard.builder();
ModuleShardConfiguration config = new ModuleShardConfiguration(URI.create("foo-ns"), "foo-module", "foo", null, members("member-1", "member-5", "member-6"));
shardManager.tell(new CreateShard(config, shardBuilder, datastoreContext), getRef());
expectMsgClass(duration("5 seconds"), Success.class);
shardManager.tell(new FindLocalShard("foo", true), getRef());
expectMsgClass(duration("5 seconds"), LocalShardFound.class);
assertEquals("isRecoveryApplicable", false, shardBuilder.getDatastoreContext().isPersistent());
assertTrue("Epxected ShardPeerAddressResolver", shardBuilder.getDatastoreContext().getShardRaftConfig().getPeerAddressResolver() instanceof ShardPeerAddressResolver);
assertEquals("peerMembers", Sets.newHashSet(ShardIdentifier.create("foo", MemberName.forName("member-5"), shardMrgIDSuffix).toString(), ShardIdentifier.create("foo", MemberName.forName("member-6"), shardMrgIDSuffix).toString()), shardBuilder.getPeerAddresses().keySet());
assertEquals("ShardIdentifier", ShardIdentifier.create("foo", MEMBER_1, shardMrgIDSuffix), shardBuilder.getId());
assertSame("schemaContext", schemaContext, shardBuilder.getSchemaContext());
// Send CreateShard with same name - should return Success with
// a message.
shardManager.tell(new CreateShard(config, shardBuilder, null), getRef());
Success success = expectMsgClass(duration("5 seconds"), Success.class);
assertNotNull("Success status is null", success.status());
}
};
LOG.info("testOnCreateShard ending");
}
use of org.opendaylight.controller.cluster.datastore.DatastoreContext 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.DatastoreContext in project controller by opendaylight.
the class ShardManager method doCreateShard.
private void doCreateShard(final CreateShard createShard) {
final ModuleShardConfiguration moduleShardConfig = createShard.getModuleShardConfig();
final String shardName = moduleShardConfig.getShardName();
configuration.addModuleShardConfiguration(moduleShardConfig);
DatastoreContext shardDatastoreContext = createShard.getDatastoreContext();
if (shardDatastoreContext == null) {
shardDatastoreContext = newShardDatastoreContext(shardName);
} else {
shardDatastoreContext = DatastoreContext.newBuilderFrom(shardDatastoreContext).shardPeerAddressResolver(peerAddressResolver).build();
}
ShardIdentifier shardId = getShardIdentifier(cluster.getCurrentMemberName(), shardName);
boolean shardWasInRecoveredSnapshot = currentSnapshot != null && currentSnapshot.getShardList().contains(shardName);
Map<String, String> peerAddresses;
boolean isActiveMember;
if (shardWasInRecoveredSnapshot || configuration.getMembersFromShardName(shardName).contains(cluster.getCurrentMemberName())) {
peerAddresses = getPeerAddresses(shardName);
isActiveMember = true;
} else {
// The local member is not in the static shard member configuration and the shard did not
// previously exist (ie !shardWasInRecoveredSnapshot). In this case we'll create
// the shard with no peers and with elections disabled so it stays as follower. A
// subsequent AddServer request will be needed to make it an active member.
isActiveMember = false;
peerAddresses = Collections.emptyMap();
shardDatastoreContext = DatastoreContext.newBuilderFrom(shardDatastoreContext).customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName()).build();
}
LOG.debug("{} doCreateShard: shardId: {}, memberNames: {}, peerAddresses: {}, isActiveMember: {}", persistenceId(), shardId, moduleShardConfig.getShardMemberNames(), peerAddresses, isActiveMember);
ShardInformation info = new ShardInformation(shardName, shardId, peerAddresses, shardDatastoreContext, createShard.getShardBuilder(), peerAddressResolver);
info.setActiveMember(isActiveMember);
localShards.put(info.getShardName(), info);
if (schemaContext != null) {
info.setSchemaContext(schemaContext);
info.setActor(newShardActor(info));
}
}
use of org.opendaylight.controller.cluster.datastore.DatastoreContext in project controller by opendaylight.
the class ShardManager method addPrefixShard.
private void addPrefixShard(final String shardName, final YangInstanceIdentifier shardPrefix, final RemotePrimaryShardFound response, final ActorRef sender) {
if (isShardReplicaOperationInProgress(shardName, sender)) {
return;
}
shardReplicaOperationsInProgress.add(shardName);
final ShardInformation shardInfo;
final boolean removeShardOnFailure;
ShardInformation existingShardInfo = localShards.get(shardName);
if (existingShardInfo == null) {
removeShardOnFailure = true;
ShardIdentifier shardId = getShardIdentifier(cluster.getCurrentMemberName(), shardName);
final Builder builder = newShardDatastoreContextBuilder(shardName);
builder.storeRoot(shardPrefix).customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
DatastoreContext datastoreContext = builder.build();
shardInfo = new ShardInformation(shardName, shardId, getPeerAddresses(shardName), datastoreContext, Shard.builder(), peerAddressResolver);
shardInfo.setActiveMember(false);
shardInfo.setSchemaContext(schemaContext);
localShards.put(shardName, shardInfo);
shardInfo.setActor(newShardActor(shardInfo));
} else {
removeShardOnFailure = false;
shardInfo = existingShardInfo;
}
execAddShard(shardName, shardInfo, response, removeShardOnFailure, sender);
}
Aggregations