Browse Source

rule-engine: don't init rule chains on partition change when rule engine execution is disabled

TenantActor.onPartitionChangeMsg initialized rule chains for any managed
tenant whose chains were not initialized yet, ignoring the API usage
state. TenantActor.init and the API_USAGE_STATE lifecycle handler both
respect isReExecEnabled(), so tenants with rule-engine execution
disabled by API limits had no rule chains until the first partition
change, which then materialized their whole rule-chain/rule-node actor
tree on every rule-engine node (and failed to start script nodes when
script execution is disabled too).

Apply the same isReExecEnabled() gate on the partition-change path and
cover it with two TenantActorTest cases.
pull/16343/head
Sergey Matvienko 2 months ago
parent
commit
1eb8305d0f
  1. 2
      application/src/main/java/org/thingsboard/server/actors/tenant/TenantActor.java
  2. 31
      application/src/test/java/org/thingsboard/server/actors/tenant/TenantActorTest.java

2
application/src/main/java/org/thingsboard/server/actors/tenant/TenantActor.java

@ -285,7 +285,7 @@ public class TenantActor extends RuleChainManagerActor {
log.info("[{}] Failed to init CF Actor.", tenantId, e);
}
}
if (!ruleChainsInitialized) {
if (!ruleChainsInitialized && getApiUsageState().isReExecEnabled()) {
log.info("Tenant {} is now managed by this service, initializing rule chains", tenantId);
initRuleChains();
}

31
application/src/test/java/org/thingsboard/server/actors/tenant/TenantActorTest.java

@ -17,13 +17,17 @@ import org.thingsboard.server.actors.ruleChain.RuleChainToRuleChainMsg;
import org.thingsboard.server.actors.service.DefaultActorService;
import org.thingsboard.server.actors.shared.RuleChainErrorActor;
import org.thingsboard.server.common.data.ApiUsageState;
import org.thingsboard.server.common.data.ApiUsageStateValue;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.RuleChainId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
import org.thingsboard.server.common.data.rule.RuleChain;
import org.thingsboard.server.common.data.rule.RuleChainType;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg;
import org.thingsboard.server.common.msg.queue.PartitionChangeMsg;
import org.thingsboard.server.common.msg.queue.ServiceType;
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo;
import org.thingsboard.server.common.msg.rule.engine.DeviceDeleteMsg;
@ -99,6 +103,33 @@ public class TenantActorTest {
verify(deviceActorRef, never()).tellWithHighPriority(any());
}
@Test
public void partitionChangeDoesNotInitRuleChainsWhenRuleEngineExecutionDisabled() throws Exception {
ApiUsageState disabledState = new ApiUsageState();
disabledState.setReExecState(ApiUsageStateValue.DISABLED);
when(systemContext.getApiUsageStateService().getApiUsageState(tenantId)).thenReturn(disabledState);
when(ruleChainService.findTenantRuleChainsByType(eq(tenantId), eq(RuleChainType.CORE), any())).thenReturn(PageData.emptyPageData());
tenantActor.init(mock(TbActorCtx.class));
tenantActor.doProcess(new PartitionChangeMsg(ServiceType.TB_RULE_ENGINE));
verify(ruleChainService, never()).findTenantRuleChainsByType(any(), any(), any());
}
@Test
public void partitionChangeInitsRuleChainsWhenTenantBecomesManagedAndRuleEngineExecutionEnabled() throws Exception {
when(partitionService.isManagedByCurrentService(tenantId)).thenReturn(false);
when(ruleChainService.findTenantRuleChainsByType(eq(tenantId), eq(RuleChainType.CORE), any())).thenReturn(PageData.emptyPageData());
tenantActor.init(mock(TbActorCtx.class));
verify(ruleChainService, never()).findTenantRuleChainsByType(any(), any(), any());
when(partitionService.isManagedByCurrentService(tenantId)).thenReturn(true);
tenantActor.doProcess(new PartitionChangeMsg(ServiceType.TB_RULE_ENGINE));
verify(ruleChainService).findTenantRuleChainsByType(eq(tenantId), eq(RuleChainType.CORE), any());
}
@Test
public void ruleChainErrorActorTest() throws Exception {
TbActorSystemSettings settings = new TbActorSystemSettings(0, 0, 0);

Loading…
Cancel
Save