Browse Source

Use the preloaded rule node only for the first rule node actor init

ComponentActor.init calls createProcessor on every init attempt: the
mailbox retries a failed start, and re-initializes an INIT_FAILED actor
(failed start, or failed restart after a configuration update) on the
next RuleNodeUpdatedMsg, reusing the same actor instance. Reusing the
snapshot from actor creation there would keep a stale configuration and
ignore later fixes, so later attempts read the rule node from the
database, as before.
pull/16344/head
Sergey Matvienko 2 months ago
parent
commit
3a587aec79
  1. 8
      application/src/main/java/org/thingsboard/server/actors/ruleChain/RuleNodeActor.java
  2. 19
      application/src/test/java/org/thingsboard/server/actors/ruleChain/RuleNodeActorMessageProcessorTest.java
  3. 56
      common/actor/src/test/java/org/thingsboard/server/actors/ActorSystemTest.java

8
application/src/main/java/org/thingsboard/server/actors/ruleChain/RuleNodeActor.java

@ -25,7 +25,7 @@ public class RuleNodeActor extends RuleEngineComponentActor<RuleNodeId, RuleNode
private final RuleChainId ruleChainId;
private final RuleNodeId ruleNodeId;
// Rule node already loaded by the parent rule chain actor; null means the processor reads it from the database
private final RuleNode preloadedRuleNode;
private RuleNode preloadedRuleNode;
private RuleNodeActor(ActorSystemContext systemContext, TenantId tenantId,
RuleChainId ruleChainId, String ruleChainName, RuleNodeId ruleNodeId, RuleNode preloadedRuleNode) {
@ -38,7 +38,11 @@ public class RuleNodeActor extends RuleEngineComponentActor<RuleNodeId, RuleNode
@Override
protected RuleNodeActorMessageProcessor createProcessor(TbActorCtx ctx) {
return new RuleNodeActorMessageProcessor(tenantId, this.ruleChainName, ruleNodeId, systemContext, ctx, preloadedRuleNode);
RuleNode ruleNode = preloadedRuleNode;
// Use the preloaded node for the first init only: init retries and re-inits after a failed start
// must read the current rule node, which the user may have fixed in the meantime
preloadedRuleNode = null;
return new RuleNodeActorMessageProcessor(tenantId, this.ruleChainName, ruleNodeId, systemContext, ctx, ruleNode);
}
@Override

19
application/src/test/java/org/thingsboard/server/actors/ruleChain/RuleNodeActorMessageProcessorTest.java

@ -74,6 +74,25 @@ public class RuleNodeActorMessageProcessorTest {
verify(ruleChainService).findRuleNodeById(tenantId, ruleNodeId);
}
@Test
public void actorUsesPreloadedRuleNodeOnlyForFirstInitAttempt() {
RuleNode preloaded = ruleNode();
RuleNode fixed = ruleNode();
fixed.setName("Save timeseries (fixed)");
when(ruleChainService.findRuleNodeById(tenantId, ruleNodeId)).thenReturn(fixed);
RuleNodeActor actor = (RuleNodeActor) new RuleNodeActor.ActorCreator(systemContext, tenantId,
preloaded.getRuleChainId(), "chain", ruleNodeId, preloaded).createActor();
RuleNodeActorMessageProcessor first = actor.createProcessor(mock(TbActorCtx.class));
verify(ruleChainService, never()).findRuleNodeById(any(), any());
assertThat(first.getComponentName()).isEqualTo(preloaded.getName());
// an init retry, or a re-init after a failed start, must pick up the current rule node
RuleNodeActorMessageProcessor retry = actor.createProcessor(mock(TbActorCtx.class));
verify(ruleChainService).findRuleNodeById(tenantId, ruleNodeId);
assertThat(retry.getComponentName()).isEqualTo(fixed.getName());
}
@Test
@SuppressWarnings("deprecation")
public void copyOfKeepsAllFieldsAndDetachesConfiguration() {

56
common/actor/src/test/java/org/thingsboard/server/actors/ActorSystemTest.java

@ -11,6 +11,10 @@ import org.junit.jupiter.api.Test;
import org.thingsboard.common.util.ThingsBoardExecutors;
import org.thingsboard.common.util.ThingsBoardThreadFactory;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.RuleNodeId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.msg.TbActorStopReason;
import org.thingsboard.server.common.msg.plugin.RuleNodeUpdatedMsg;
import java.util.ArrayList;
import java.util.List;
@ -204,6 +208,58 @@ public class ActorSystemTest {
Assertions.assertTrue(testCtx1.getLatch().await(3, TimeUnit.SECONDS));
}
@Test
public void testInitFailedActorIsReinitializedOnRuleNodeUpdate() throws InterruptedException {
executor = ThingsBoardExecutors.newWorkStealingPool(parallelism, getClass());
actorSystem.createDispatcher(ROOT_DISPATCHER, executor);
ActorTestCtx testCtx = getActorTestCtx(1);
RuleNodeId ruleNodeId = new RuleNodeId(UUID.randomUUID());
AtomicInteger createdActors = new AtomicInteger();
AtomicInteger initAttempts = new AtomicInteger();
CountDownLatch destroyed = new CountDownLatch(1);
// fails its first init without retries, as a rule node whose start or update failed; succeeds afterwards
TbActorRef actorRef = actorSystem.createRootActor(ROOT_DISPATCHER, new TbActorCreator() {
@Override
public TbActorId createActorId() {
return new TbEntityActorId(ruleNodeId);
}
@Override
public TbActor createActor() {
createdActors.incrementAndGet();
return new TestRootActor(createActorId(), testCtx) {
@Override
public void init(TbActorCtx ctx) throws TbActorException {
if (initAttempts.incrementAndGet() == 1) {
throw new TbActorException("Test init failure", new RuntimeException());
}
super.init(ctx);
}
@Override
public InitFailureStrategy onInitFailure(int attempt, Throwable t) {
return InitFailureStrategy.stop();
}
@Override
public void destroy(TbActorStopReason stopReason, Throwable cause) {
destroyed.countDown();
}
};
}
});
Assertions.assertTrue(destroyed.await(TIMEOUT_AWAIT_MAX_SEC, TimeUnit.SECONDS));
actorRef.tellWithHighPriority(new RuleNodeUpdatedMsg(TenantId.SYS_TENANT_ID, ruleNodeId));
Awaitility.await().atMost(TIMEOUT_AWAIT_MAX_SEC, TimeUnit.SECONDS).until(() -> initAttempts.get() == 2);
actorRef.tell(new IntTbActorMsg(42));
Assertions.assertTrue(testCtx.getLatch().await(TIMEOUT_AWAIT_MAX_SEC, TimeUnit.SECONDS));
// the same actor instance is initialized again, so state it kept from its creation is reused by the re-init
assertThat(createdActors.get()).isEqualTo(1);
}
public void testActorsAndMessages(int actorsCount, int msgNumber, int times) throws InterruptedException {
Random random = new Random();

Loading…
Cancel
Save