Search in sources :

Example 1 with TbActorRef

use of org.thingsboard.server.actors.TbActorRef in project thingsboard by thingsboard.

the class DefaultActorService method initActorSystem.

@PostConstruct
public void initActorSystem() {
    log.info("Initializing actor system.");
    actorContext.setActorService(this);
    TbActorSystemSettings settings = new TbActorSystemSettings(actorThroughput, schedulerPoolSize, maxActorInitAttempts);
    system = new DefaultTbActorSystem(settings);
    system.createDispatcher(APP_DISPATCHER_NAME, initDispatcherExecutor(APP_DISPATCHER_NAME, appDispatcherSize));
    system.createDispatcher(TENANT_DISPATCHER_NAME, initDispatcherExecutor(TENANT_DISPATCHER_NAME, tenantDispatcherSize));
    system.createDispatcher(DEVICE_DISPATCHER_NAME, initDispatcherExecutor(DEVICE_DISPATCHER_NAME, deviceDispatcherSize));
    system.createDispatcher(RULE_DISPATCHER_NAME, initDispatcherExecutor(RULE_DISPATCHER_NAME, ruleDispatcherSize));
    actorContext.setActorSystem(system);
    appActor = system.createRootActor(APP_DISPATCHER_NAME, new AppActor.ActorCreator(actorContext));
    actorContext.setAppActor(appActor);
    TbActorRef statsActor = system.createRootActor(TENANT_DISPATCHER_NAME, new StatsActor.ActorCreator(actorContext, "StatsActor"));
    actorContext.setStatsActor(statsActor);
    log.info("Actor system initialized.");
}
Also used : StatsActor(org.thingsboard.server.actors.stats.StatsActor) TbActorSystemSettings(org.thingsboard.server.actors.TbActorSystemSettings) DefaultTbActorSystem(org.thingsboard.server.actors.DefaultTbActorSystem) TbActorRef(org.thingsboard.server.actors.TbActorRef) PostConstruct(javax.annotation.PostConstruct)

Example 2 with TbActorRef

use of org.thingsboard.server.actors.TbActorRef in project thingsboard by thingsboard.

the class RuleChainManagerActor method initRuleChains.

protected void initRuleChains() {
    for (RuleChain ruleChain : new PageDataIterable<>(link -> ruleChainService.findTenantRuleChainsByType(tenantId, RuleChainType.CORE, link), ContextAwareActor.ENTITY_PACK_LIMIT)) {
        RuleChainId ruleChainId = ruleChain.getId();
        log.debug("[{}|{}] Creating rule chain actor", ruleChainId.getEntityType(), ruleChain.getId());
        TbActorRef actorRef = getOrCreateActor(ruleChainId, id -> ruleChain);
        visit(ruleChain, actorRef);
        log.debug("[{}|{}] Rule Chain actor created.", ruleChainId.getEntityType(), ruleChainId.getId());
    }
}
Also used : PageDataIterable(org.thingsboard.server.common.data.page.PageDataIterable) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) TbActorRef(org.thingsboard.server.actors.TbActorRef) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId)

Example 3 with TbActorRef

use of org.thingsboard.server.actors.TbActorRef in project thingsboard by thingsboard.

the class AppActor method onComponentLifecycleMsg.

private void onComponentLifecycleMsg(ComponentLifecycleMsg msg) {
    TbActorRef target = null;
    if (TenantId.SYS_TENANT_ID.equals(msg.getTenantId())) {
        if (!EntityType.TENANT_PROFILE.equals(msg.getEntityId().getEntityType())) {
            log.warn("Message has system tenant id: {}", msg);
        }
    } else {
        if (EntityType.TENANT.equals(msg.getEntityId().getEntityType())) {
            TenantId tenantId = TenantId.fromUUID(msg.getEntityId().getId());
            if (msg.getEvent() == ComponentLifecycleEvent.DELETED) {
                log.info("[{}] Handling tenant deleted notification: {}", msg.getTenantId(), msg);
                deletedTenants.add(tenantId);
                ctx.stop(new TbEntityActorId(tenantId));
            } else {
                target = getOrCreateTenantActor(msg.getTenantId());
            }
        } else {
            target = getOrCreateTenantActor(msg.getTenantId());
        }
    }
    if (target != null) {
        target.tellWithHighPriority(msg);
    } else {
        log.debug("[{}] Invalid component lifecycle msg: {}", msg.getTenantId(), msg);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) TbEntityActorId(org.thingsboard.server.actors.TbEntityActorId) TbActorRef(org.thingsboard.server.actors.TbActorRef)

Example 4 with TbActorRef

use of org.thingsboard.server.actors.TbActorRef in project thingsboard by thingsboard.

the class RuleChainActorMessageProcessor method start.

@Override
public void start(TbActorCtx context) {
    if (!started) {
        RuleChain ruleChain = service.findRuleChainById(tenantId, entityId);
        if (ruleChain != null && RuleChainType.CORE.equals(ruleChain.getType())) {
            List<RuleNode> ruleNodeList = service.getRuleChainNodes(tenantId, entityId);
            log.trace("[{}][{}] Starting rule chain with {} nodes", tenantId, entityId, ruleNodeList.size());
            // Creating and starting the actors;
            for (RuleNode ruleNode : ruleNodeList) {
                log.trace("[{}][{}] Creating rule node [{}]: {}", entityId, ruleNode.getId(), ruleNode.getName(), ruleNode);
                TbActorRef ruleNodeActor = createRuleNodeActor(context, ruleNode);
                nodeActors.put(ruleNode.getId(), new RuleNodeCtx(tenantId, self, ruleNodeActor, ruleNode));
            }
            initRoutes(ruleChain, ruleNodeList);
            started = true;
        }
    } else {
        onUpdate(context);
    }
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbActorRef(org.thingsboard.server.actors.TbActorRef)

Example 5 with TbActorRef

use of org.thingsboard.server.actors.TbActorRef in project thingsboard by thingsboard.

the class RuleChainActorMessageProcessor method onUpdate.

@Override
public void onUpdate(TbActorCtx context) {
    RuleChain ruleChain = service.findRuleChainById(tenantId, entityId);
    if (ruleChain != null && RuleChainType.CORE.equals(ruleChain.getType())) {
        ruleChainName = ruleChain.getName();
        List<RuleNode> ruleNodeList = service.getRuleChainNodes(tenantId, entityId);
        log.trace("[{}][{}] Updating rule chain with {} nodes", tenantId, entityId, ruleNodeList.size());
        for (RuleNode ruleNode : ruleNodeList) {
            RuleNodeCtx existing = nodeActors.get(ruleNode.getId());
            if (existing == null) {
                log.trace("[{}][{}] Creating rule node [{}]: {}", entityId, ruleNode.getId(), ruleNode.getName(), ruleNode);
                TbActorRef ruleNodeActor = createRuleNodeActor(context, ruleNode);
                nodeActors.put(ruleNode.getId(), new RuleNodeCtx(tenantId, self, ruleNodeActor, ruleNode));
            } else {
                log.trace("[{}][{}] Updating rule node [{}]: {}", entityId, ruleNode.getId(), ruleNode.getName(), ruleNode);
                existing.setSelf(ruleNode);
                existing.getSelfActor().tellWithHighPriority(new RuleNodeUpdatedMsg(tenantId, existing.getSelf().getId()));
            }
        }
        Set<RuleNodeId> existingNodes = ruleNodeList.stream().map(RuleNode::getId).collect(Collectors.toSet());
        List<RuleNodeId> removedRules = nodeActors.keySet().stream().filter(node -> !existingNodes.contains(node)).collect(Collectors.toList());
        removedRules.forEach(ruleNodeId -> {
            log.trace("[{}][{}] Removing rule node [{}]", tenantId, entityId, ruleNodeId);
            RuleNodeCtx removed = nodeActors.remove(ruleNodeId);
            removed.getSelfActor().tellWithHighPriority(new ComponentLifecycleMsg(tenantId, removed.getSelf().getId(), ComponentLifecycleEvent.DELETED));
        });
        initRoutes(ruleChain, ruleNodeList);
    }
}
Also used : TbMsg(org.thingsboard.server.common.msg.TbMsg) HashMap(java.util.HashMap) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) ComponentMsgProcessor(org.thingsboard.server.actors.shared.ComponentMsgProcessor) TenantId(org.thingsboard.server.common.data.id.TenantId) RuleChainService(org.thingsboard.server.dao.rule.RuleChainService) DefaultActorService(org.thingsboard.server.actors.service.DefaultActorService) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) ComponentLifecycleState(org.thingsboard.server.common.data.plugin.ComponentLifecycleState) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) Map(java.util.Map) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) TbQueueTbMsgCallbackWrapper(org.thingsboard.server.queue.common.TbQueueTbMsgCallbackWrapper) RuleNodeUpdatedMsg(org.thingsboard.server.common.msg.plugin.RuleNodeUpdatedMsg) TbQueueCallback(org.thingsboard.server.queue.TbQueueCallback) TbApiUsageClient(org.thingsboard.server.queue.usagestats.TbApiUsageClient) TbEntityActorId(org.thingsboard.server.actors.TbEntityActorId) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) RuleChainType(org.thingsboard.server.common.data.rule.RuleChainType) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException) Set(java.util.Set) TbActorCtx(org.thingsboard.server.actors.TbActorCtx) TbRelationTypes(org.thingsboard.rule.engine.api.TbRelationTypes) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ComponentLifecycleEvent(org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent) PartitionChangeMsg(org.thingsboard.server.common.msg.queue.PartitionChangeMsg) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) ToRuleEngineMsg(org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) MultipleTbQueueTbMsgCallbackWrapper(org.thingsboard.server.queue.common.MultipleTbQueueTbMsgCallbackWrapper) TbClusterService(org.thingsboard.server.cluster.TbClusterService) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Collections(java.util.Collections) TbActorRef(org.thingsboard.server.actors.TbActorRef) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbActorRef(org.thingsboard.server.actors.TbActorRef) RuleNodeUpdatedMsg(org.thingsboard.server.common.msg.plugin.RuleNodeUpdatedMsg) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Aggregations

TbActorRef (org.thingsboard.server.actors.TbActorRef)6 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)4 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)3 TbEntityActorId (org.thingsboard.server.actors.TbEntityActorId)2 TenantId (org.thingsboard.server.common.data.id.TenantId)2 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 PostConstruct (javax.annotation.PostConstruct)1 Slf4j (lombok.extern.slf4j.Slf4j)1 TbRelationTypes (org.thingsboard.rule.engine.api.TbRelationTypes)1 ActorSystemContext (org.thingsboard.server.actors.ActorSystemContext)1 DefaultTbActorSystem (org.thingsboard.server.actors.DefaultTbActorSystem)1 TbActorCtx (org.thingsboard.server.actors.TbActorCtx)1