Commit 25db0b73fd59c89b1c50c04ebf6d43e31f6072ef
1 parent
9be5c367
Improvements to Tenant Actor initialization
Showing
4 changed files
with
21 additions
and
19 deletions
... | ... | @@ -78,11 +78,13 @@ public class AppActor extends ContextAwareActor { |
78 | 78 | ruleManager.init(this.context()); |
79 | 79 | pluginManager.init(this.context()); |
80 | 80 | |
81 | - PageDataIterable<Tenant> tenantIterator = new PageDataIterable<>(link -> tenantService.findTenants(link), ENTITY_PACK_LIMIT); | |
82 | - for (Tenant tenant : tenantIterator) { | |
83 | - logger.debug("[{}] Creating tenant actor", tenant.getId()); | |
84 | - getOrCreateTenantActor(tenant.getId()); | |
85 | - logger.debug("Tenant actor created."); | |
81 | + if (systemContext.isTenantComponentsInitEnabled()) { | |
82 | + PageDataIterable<Tenant> tenantIterator = new PageDataIterable<>(tenantService::findTenants, ENTITY_PACK_LIMIT); | |
83 | + for (Tenant tenant : tenantIterator) { | |
84 | + logger.debug("[{}] Creating tenant actor", tenant.getId()); | |
85 | + getOrCreateTenantActor(tenant.getId()); | |
86 | + logger.debug("Tenant actor created."); | |
87 | + } | |
86 | 88 | } |
87 | 89 | |
88 | 90 | logger.info("Main system actor started."); |
... | ... | @@ -181,13 +183,8 @@ public class AppActor extends ContextAwareActor { |
181 | 183 | } |
182 | 184 | |
183 | 185 | private ActorRef getOrCreateTenantActor(TenantId tenantId) { |
184 | - ActorRef tenantActor = tenantActors.get(tenantId); | |
185 | - if (tenantActor == null) { | |
186 | - tenantActor = context().actorOf(Props.create(new TenantActor.ActorCreator(systemContext, tenantId)) | |
187 | - .withDispatcher(DefaultActorService.CORE_DISPATCHER_NAME), tenantId.toString()); | |
188 | - tenantActors.put(tenantId, tenantActor); | |
189 | - } | |
190 | - return tenantActor; | |
186 | + return tenantActors.computeIfAbsent(tenantId, k -> context().actorOf(Props.create(new TenantActor.ActorCreator(systemContext, tenantId)) | |
187 | + .withDispatcher(DefaultActorService.CORE_DISPATCHER_NAME), tenantId.toString())); | |
191 | 188 | } |
192 | 189 | |
193 | 190 | private void processTermination(Terminated message) { | ... | ... |
... | ... | @@ -44,7 +44,7 @@ public abstract class RuleManager { |
44 | 44 | protected final Map<RuleId, ActorRef> ruleActors; |
45 | 45 | protected final TenantId tenantId; |
46 | 46 | |
47 | - Map<RuleMetaData, RuleActorMetaData> ruleMap = new HashMap<>(); | |
47 | + private Map<RuleMetaData, RuleActorMetaData> ruleMap; | |
48 | 48 | private RuleActorChain ruleChain; |
49 | 49 | |
50 | 50 | public RuleManager(ActorSystemContext systemContext, TenantId tenantId) { |
... | ... | @@ -55,6 +55,10 @@ public abstract class RuleManager { |
55 | 55 | } |
56 | 56 | |
57 | 57 | public void init(ActorContext context) { |
58 | + doInit(context); | |
59 | + } | |
60 | + | |
61 | + private void doInit(ActorContext context) { | |
58 | 62 | PageDataIterable<RuleMetaData> ruleIterator = new PageDataIterable<>(getFetchRulesFunction(), |
59 | 63 | ContextAwareActor.ENTITY_PACK_LIMIT); |
60 | 64 | ruleMap = new HashMap<>(); |
... | ... | @@ -62,8 +66,7 @@ public abstract class RuleManager { |
62 | 66 | for (RuleMetaData rule : ruleIterator) { |
63 | 67 | log.debug("[{}] Creating rule actor {}", rule.getId(), rule); |
64 | 68 | ActorRef ref = getOrCreateRuleActor(context, rule.getId()); |
65 | - RuleActorMetaData actorMd = RuleActorMetaData.systemRule(rule.getId(), rule.getWeight(), ref); | |
66 | - ruleMap.put(rule, actorMd); | |
69 | + ruleMap.put(rule, RuleActorMetaData.systemRule(rule.getId(), rule.getWeight(), ref)); | |
67 | 70 | log.debug("[{}] Rule actor created.", rule.getId()); |
68 | 71 | } |
69 | 72 | |
... | ... | @@ -72,7 +75,7 @@ public abstract class RuleManager { |
72 | 75 | |
73 | 76 | public Optional<ActorRef> update(ActorContext context, RuleId ruleId, ComponentLifecycleEvent event) { |
74 | 77 | if (ruleMap == null) { |
75 | - init(context); | |
78 | + doInit(context); | |
76 | 79 | } |
77 | 80 | RuleMetaData rule; |
78 | 81 | if (event != ComponentLifecycleEvent.DELETED) { |
... | ... | @@ -114,8 +117,8 @@ public abstract class RuleManager { |
114 | 117 | } |
115 | 118 | |
116 | 119 | public RuleActorChain getRuleChain(ActorContext context) { |
117 | - if (ruleMap == null) { | |
118 | - init(context); | |
120 | + if (ruleChain == null) { | |
121 | + doInit(context); | |
119 | 122 | } |
120 | 123 | return ruleChain; |
121 | 124 | } | ... | ... |
... | ... | @@ -151,7 +151,8 @@ public class TenantActor extends ContextAwareActor { |
151 | 151 | private void process(RuleChainDeviceMsg msg) { |
152 | 152 | ToDeviceActorMsg toDeviceActorMsg = msg.getToDeviceActorMsg(); |
153 | 153 | ActorRef deviceActor = getOrCreateDeviceActor(toDeviceActorMsg.getDeviceId()); |
154 | - RuleActorChain chain = new ComplexRuleActorChain(msg.getRuleChain(), ruleManager.getRuleChain(this.context())); | |
154 | + RuleActorChain tenantChain = ruleManager.getRuleChain(this.context()); | |
155 | + RuleActorChain chain = new ComplexRuleActorChain(msg.getRuleChain(), tenantChain); | |
155 | 156 | deviceActor.tell(new RuleChainDeviceMsg(toDeviceActorMsg, chain), context().self()); |
156 | 157 | } |
157 | 158 | ... | ... |