Improvements to Tenant Actor initialization

This commit is contained in:
Andrew Shvayka 2017-08-28 14:00:05 +03:00
parent 9be5c367ce
commit 25db0b73fd
4 changed files with 21 additions and 19 deletions

View File

@ -78,12 +78,14 @@ public class AppActor extends ContextAwareActor {
ruleManager.init(this.context());
pluginManager.init(this.context());
PageDataIterable<Tenant> tenantIterator = new PageDataIterable<>(link -> tenantService.findTenants(link), ENTITY_PACK_LIMIT);
if (systemContext.isTenantComponentsInitEnabled()) {
PageDataIterable<Tenant> tenantIterator = new PageDataIterable<>(tenantService::findTenants, ENTITY_PACK_LIMIT);
for (Tenant tenant : tenantIterator) {
logger.debug("[{}] Creating tenant actor", tenant.getId());
getOrCreateTenantActor(tenant.getId());
logger.debug("Tenant actor created.");
}
}
logger.info("Main system actor started.");
} catch (Exception e) {
@ -181,13 +183,8 @@ public class AppActor extends ContextAwareActor {
}
private ActorRef getOrCreateTenantActor(TenantId tenantId) {
ActorRef tenantActor = tenantActors.get(tenantId);
if (tenantActor == null) {
tenantActor = context().actorOf(Props.create(new TenantActor.ActorCreator(systemContext, tenantId))
.withDispatcher(DefaultActorService.CORE_DISPATCHER_NAME), tenantId.toString());
tenantActors.put(tenantId, tenantActor);
}
return tenantActor;
return tenantActors.computeIfAbsent(tenantId, k -> context().actorOf(Props.create(new TenantActor.ActorCreator(systemContext, tenantId))
.withDispatcher(DefaultActorService.CORE_DISPATCHER_NAME), tenantId.toString()));
}
private void processTermination(Terminated message) {

View File

@ -44,7 +44,7 @@ public abstract class RuleManager {
protected final Map<RuleId, ActorRef> ruleActors;
protected final TenantId tenantId;
Map<RuleMetaData, RuleActorMetaData> ruleMap = new HashMap<>();
private Map<RuleMetaData, RuleActorMetaData> ruleMap;
private RuleActorChain ruleChain;
public RuleManager(ActorSystemContext systemContext, TenantId tenantId) {
@ -55,6 +55,10 @@ public abstract class RuleManager {
}
public void init(ActorContext context) {
doInit(context);
}
private void doInit(ActorContext context) {
PageDataIterable<RuleMetaData> ruleIterator = new PageDataIterable<>(getFetchRulesFunction(),
ContextAwareActor.ENTITY_PACK_LIMIT);
ruleMap = new HashMap<>();
@ -62,8 +66,7 @@ public abstract class RuleManager {
for (RuleMetaData rule : ruleIterator) {
log.debug("[{}] Creating rule actor {}", rule.getId(), rule);
ActorRef ref = getOrCreateRuleActor(context, rule.getId());
RuleActorMetaData actorMd = RuleActorMetaData.systemRule(rule.getId(), rule.getWeight(), ref);
ruleMap.put(rule, actorMd);
ruleMap.put(rule, RuleActorMetaData.systemRule(rule.getId(), rule.getWeight(), ref));
log.debug("[{}] Rule actor created.", rule.getId());
}
@ -72,7 +75,7 @@ public abstract class RuleManager {
public Optional<ActorRef> update(ActorContext context, RuleId ruleId, ComponentLifecycleEvent event) {
if (ruleMap == null) {
init(context);
doInit(context);
}
RuleMetaData rule;
if (event != ComponentLifecycleEvent.DELETED) {
@ -114,8 +117,8 @@ public abstract class RuleManager {
}
public RuleActorChain getRuleChain(ActorContext context) {
if (ruleMap == null) {
init(context);
if (ruleChain == null) {
doInit(context);
}
return ruleChain;
}

View File

@ -28,6 +28,7 @@ public class TenantRuleManager extends RuleManager {
super(systemContext, tenantId);
}
@Override
public void init(ActorContext context) {
if (systemContext.isTenantComponentsInitEnabled()) {
super.init(context);

View File

@ -151,7 +151,8 @@ public class TenantActor extends ContextAwareActor {
private void process(RuleChainDeviceMsg msg) {
ToDeviceActorMsg toDeviceActorMsg = msg.getToDeviceActorMsg();
ActorRef deviceActor = getOrCreateDeviceActor(toDeviceActorMsg.getDeviceId());
RuleActorChain chain = new ComplexRuleActorChain(msg.getRuleChain(), ruleManager.getRuleChain(this.context()));
RuleActorChain tenantChain = ruleManager.getRuleChain(this.context());
RuleActorChain chain = new ComplexRuleActorChain(msg.getRuleChain(), tenantChain);
deviceActor.tell(new RuleChainDeviceMsg(toDeviceActorMsg, chain), context().self());
}