Search in sources :

Example 1 with RuleNodeException

use of org.thingsboard.server.common.msg.queue.RuleNodeException in project thingsboard by thingsboard.

the class RuleChainActorMessageProcessor method onTellNext.

private void onTellNext(TbMsg msg, RuleNodeId originatorNodeId, Set<String> relationTypes, String failureMessage) {
    try {
        checkComponentStateActive(msg);
        EntityId entityId = msg.getOriginator();
        TopicPartitionInfo tpi = systemContext.resolve(ServiceType.TB_RULE_ENGINE, msg.getQueueName(), tenantId, entityId);
        List<RuleNodeRelation> ruleNodeRelations = nodeRoutes.get(originatorNodeId);
        if (ruleNodeRelations == null) {
            // When unchecked, this will cause NullPointerException when rule node doesn't exist anymore
            log.warn("[{}][{}][{}] No outbound relations (null). Probably rule node does not exist. Probably old message.", tenantId, entityId, msg.getId());
            ruleNodeRelations = Collections.emptyList();
        }
        List<RuleNodeRelation> relationsByTypes = ruleNodeRelations.stream().filter(r -> contains(relationTypes, r.getType())).collect(Collectors.toList());
        int relationsCount = relationsByTypes.size();
        if (relationsCount == 0) {
            log.trace("[{}][{}][{}] No outbound relations to process", tenantId, entityId, msg.getId());
            if (relationTypes.contains(TbRelationTypes.FAILURE)) {
                RuleNodeCtx ruleNodeCtx = nodeActors.get(originatorNodeId);
                if (ruleNodeCtx != null) {
                    msg.getCallback().onFailure(new RuleNodeException(failureMessage, ruleChainName, ruleNodeCtx.getSelf()));
                } else {
                    log.debug("[{}] Failure during message processing by Rule Node [{}]. Enable and see debug events for more info", entityId, originatorNodeId.getId());
                    msg.getCallback().onFailure(new RuleEngineException("Failure during message processing by Rule Node [" + originatorNodeId.getId().toString() + "]"));
                }
            } else {
                msg.getCallback().onSuccess();
            }
        } else if (relationsCount == 1) {
            for (RuleNodeRelation relation : relationsByTypes) {
                log.trace("[{}][{}][{}] Pushing message to single target: [{}]", tenantId, entityId, msg.getId(), relation.getOut());
                pushToTarget(tpi, msg, relation.getOut(), relation.getType());
            }
        } else {
            MultipleTbQueueTbMsgCallbackWrapper callbackWrapper = new MultipleTbQueueTbMsgCallbackWrapper(relationsCount, msg.getCallback());
            log.trace("[{}][{}][{}] Pushing message to multiple targets: [{}]", tenantId, entityId, msg.getId(), relationsByTypes);
            for (RuleNodeRelation relation : relationsByTypes) {
                EntityId target = relation.getOut();
                putToQueue(tpi, msg, callbackWrapper, target);
            }
        }
    } catch (RuleNodeException rne) {
        msg.getCallback().onFailure(rne);
    } catch (Exception e) {
        log.warn("[" + tenantId + "]" + "[" + entityId + "]" + "[" + msg.getId() + "]" + " onTellNext failure", e);
        msg.getCallback().onFailure(new RuleEngineException("onTellNext - " + e.getMessage()));
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) 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) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException) MultipleTbQueueTbMsgCallbackWrapper(org.thingsboard.server.queue.common.MultipleTbQueueTbMsgCallbackWrapper) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException) RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException)

Example 2 with RuleNodeException

use of org.thingsboard.server.common.msg.queue.RuleNodeException in project thingsboard by thingsboard.

the class RuleNodeActorMessageProcessor method onRuleChainToRuleNodeMsg.

void onRuleChainToRuleNodeMsg(RuleChainToRuleNodeMsg msg) throws Exception {
    msg.getMsg().getCallback().onProcessingStart(info);
    checkComponentStateActive(msg.getMsg());
    TbMsg tbMsg = msg.getMsg();
    int ruleNodeCount = tbMsg.getAndIncrementRuleNodeCounter();
    int maxRuleNodeExecutionsPerMessage = getTenantProfileConfiguration().getMaxRuleNodeExecsPerMessage();
    if (maxRuleNodeExecutionsPerMessage == 0 || ruleNodeCount < maxRuleNodeExecutionsPerMessage) {
        apiUsageClient.report(tenantId, tbMsg.getCustomerId(), ApiUsageRecordKey.RE_EXEC_COUNT);
        if (ruleNode.isDebugMode()) {
            systemContext.persistDebugInput(tenantId, entityId, msg.getMsg(), msg.getFromRelationType());
        }
        try {
            tbNode.onMsg(msg.getCtx(), msg.getMsg());
        } catch (Exception e) {
            msg.getCtx().tellFailure(msg.getMsg(), e);
        }
    } else {
        tbMsg.getCallback().onFailure(new RuleNodeException("Message is processed by more then " + maxRuleNodeExecutionsPerMessage + " rule nodes!", ruleChainName, ruleNode));
    }
}
Also used : RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException) TbMsg(org.thingsboard.server.common.msg.TbMsg) TbRuleNodeUpdateException(org.thingsboard.server.actors.TbRuleNodeUpdateException) RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException)

Example 3 with RuleNodeException

use of org.thingsboard.server.common.msg.queue.RuleNodeException in project thingsboard by thingsboard.

the class RuleChainActorMessageProcessor method onTellNext.

private void onTellNext(TbMsg msg, boolean useRuleNodeIdFromMsg) {
    try {
        checkComponentStateActive(msg);
        RuleNodeId targetId = useRuleNodeIdFromMsg ? msg.getRuleNodeId() : null;
        RuleNodeCtx targetCtx;
        if (targetId == null) {
            targetCtx = firstNode;
            msg = msg.copyWithRuleChainId(entityId);
        } else {
            targetCtx = nodeActors.get(targetId);
        }
        if (targetCtx != null) {
            log.trace("[{}][{}] Pushing message to target rule node", entityId, targetId);
            pushMsgToNode(targetCtx, msg, NA_RELATION_TYPE);
        } else {
            log.trace("[{}][{}] Rule node does not exist. Probably old message", entityId, targetId);
            msg.getCallback().onSuccess();
        }
    } catch (RuleNodeException rne) {
        msg.getCallback().onFailure(rne);
    } catch (Exception e) {
        msg.getCallback().onFailure(new RuleEngineException(e.getMessage()));
    }
}
Also used : RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException) RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 4 with RuleNodeException

use of org.thingsboard.server.common.msg.queue.RuleNodeException in project thingsboard by thingsboard.

the class ComponentMsgProcessor method checkComponentStateActive.

protected void checkComponentStateActive(TbMsg tbMsg) throws RuleNodeException {
    if (state != ComponentLifecycleState.ACTIVE) {
        log.debug("Component is not active. Current state [{}] for processor [{}][{}] tenant [{}]", state, entityId.getEntityType(), entityId, tenantId);
        RuleNodeException ruleNodeException = getInactiveException();
        if (tbMsg != null) {
            tbMsg.getCallback().onFailure(ruleNodeException);
        }
        throw ruleNodeException;
    }
}
Also used : RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException)

Example 5 with RuleNodeException

use of org.thingsboard.server.common.msg.queue.RuleNodeException in project thingsboard by thingsboard.

the class RuleNodeActorMessageProcessor method onRuleToSelfMsg.

public void onRuleToSelfMsg(RuleNodeToSelfMsg msg) throws Exception {
    checkComponentStateActive(msg.getMsg());
    TbMsg tbMsg = msg.getMsg();
    int ruleNodeCount = tbMsg.getAndIncrementRuleNodeCounter();
    int maxRuleNodeExecutionsPerMessage = getTenantProfileConfiguration().getMaxRuleNodeExecsPerMessage();
    if (maxRuleNodeExecutionsPerMessage == 0 || ruleNodeCount < maxRuleNodeExecutionsPerMessage) {
        apiUsageClient.report(tenantId, tbMsg.getCustomerId(), ApiUsageRecordKey.RE_EXEC_COUNT);
        if (ruleNode.isDebugMode()) {
            systemContext.persistDebugInput(tenantId, entityId, msg.getMsg(), "Self");
        }
        try {
            tbNode.onMsg(defaultCtx, msg.getMsg());
        } catch (Exception e) {
            defaultCtx.tellFailure(msg.getMsg(), e);
        }
    } else {
        tbMsg.getCallback().onFailure(new RuleNodeException("Message is processed by more then " + maxRuleNodeExecutionsPerMessage + " rule nodes!", ruleChainName, ruleNode));
    }
}
Also used : RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException) TbMsg(org.thingsboard.server.common.msg.TbMsg) TbRuleNodeUpdateException(org.thingsboard.server.actors.TbRuleNodeUpdateException) RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException)

Aggregations

RuleNodeException (org.thingsboard.server.common.msg.queue.RuleNodeException)6 TbMsg (org.thingsboard.server.common.msg.TbMsg)3 TbRuleNodeUpdateException (org.thingsboard.server.actors.TbRuleNodeUpdateException)2 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)2 RuleEngineException (org.thingsboard.server.common.msg.queue.RuleEngineException)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 Slf4j (lombok.extern.slf4j.Slf4j)1 TbRelationTypes (org.thingsboard.rule.engine.api.TbRelationTypes)1 ActorSystemContext (org.thingsboard.server.actors.ActorSystemContext)1 TbActorCtx (org.thingsboard.server.actors.TbActorCtx)1 TbActorRef (org.thingsboard.server.actors.TbActorRef)1 TbEntityActorId (org.thingsboard.server.actors.TbEntityActorId)1 DefaultActorService (org.thingsboard.server.actors.service.DefaultActorService)1