Search in sources :

Example 11 with TopicPartitionInfo

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

the class DefaultTelemetrySubscriptionService method onAttributesDelete.

private void onAttributesDelete(TenantId tenantId, EntityId entityId, String scope, List<String> keys) {
    TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, entityId);
    if (currentPartitions.contains(tpi)) {
        if (subscriptionManagerService.isPresent()) {
            subscriptionManagerService.get().onAttributesDelete(tenantId, entityId, scope, keys, TbCallback.EMPTY);
        } else {
            log.warn("Possible misconfiguration because subscriptionManagerService is null!");
        }
    } else {
        TransportProtos.ToCoreMsg toCoreMsg = TbSubscriptionUtils.toAttributesDeleteProto(tenantId, entityId, scope, keys);
        clusterService.pushMsgToCore(tpi, entityId.getId(), toCoreMsg, null);
    }
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos)

Example 12 with TopicPartitionInfo

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

the class DefaultTelemetrySubscriptionService method onTimeSeriesDelete.

private void onTimeSeriesDelete(TenantId tenantId, EntityId entityId, List<String> keys, List<TsKvLatestRemovingResult> ts) {
    TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, entityId);
    if (currentPartitions.contains(tpi)) {
        if (subscriptionManagerService.isPresent()) {
            List<TsKvEntry> updated = new ArrayList<>();
            List<String> deleted = new ArrayList<>();
            ts.stream().filter(Objects::nonNull).forEach(res -> {
                if (res.isRemoved()) {
                    if (res.getData() != null) {
                        updated.add(res.getData());
                    } else {
                        deleted.add(res.getKey());
                    }
                }
            });
            subscriptionManagerService.get().onTimeSeriesUpdate(tenantId, entityId, updated, TbCallback.EMPTY);
            subscriptionManagerService.get().onTimeSeriesDelete(tenantId, entityId, deleted, TbCallback.EMPTY);
        } else {
            log.warn("Possible misconfiguration because subscriptionManagerService is null!");
        }
    } else {
        TransportProtos.ToCoreMsg toCoreMsg = TbSubscriptionUtils.toTimeseriesDeleteProto(tenantId, entityId, keys);
        clusterService.pushMsgToCore(tpi, entityId.getId(), toCoreMsg, null);
    }
}
Also used : TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) ArrayList(java.util.ArrayList) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos)

Example 13 with TopicPartitionInfo

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

the class DefaultTbCoreToTransportService method process.

@Override
public void process(String nodeId, ToTransportMsg msg, Runnable onSuccess, Consumer<Throwable> onFailure) {
    if (nodeId == null || nodeId.isEmpty()) {
        log.trace("process: skipping message without nodeId [{}], (ToTransportMsg) msg [{}]", nodeId, msg);
        if (onSuccess != null) {
            onSuccess.run();
        }
        return;
    }
    TopicPartitionInfo tpi = partitionService.getNotificationsTopic(ServiceType.TB_TRANSPORT, nodeId);
    UUID sessionId = new UUID(msg.getSessionIdMSB(), msg.getSessionIdLSB());
    log.trace("[{}][{}] Pushing session data to topic: {}", tpi.getFullTopicName(), sessionId, msg);
    TbProtoQueueMsg<ToTransportMsg> queueMsg = new TbProtoQueueMsg<>(NULL_UUID, msg);
    tbTransportProducer.send(tpi, queueMsg, new QueueCallbackAdaptor(onSuccess, onFailure));
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) ToTransportMsg(org.thingsboard.server.gen.transport.TransportProtos.ToTransportMsg) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) UUID(java.util.UUID)

Example 14 with TopicPartitionInfo

use of org.thingsboard.server.common.msg.queue.TopicPartitionInfo 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 15 with TopicPartitionInfo

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

the class DefaultTbContext method enqueue.

@Override
public void enqueue(TbMsg tbMsg, Runnable onSuccess, Consumer<Throwable> onFailure) {
    TopicPartitionInfo tpi = mainCtx.resolve(ServiceType.TB_RULE_ENGINE, getTenantId(), tbMsg.getOriginator());
    enqueue(tpi, tbMsg, onFailure, onSuccess);
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo)

Aggregations

TopicPartitionInfo (org.thingsboard.server.common.msg.queue.TopicPartitionInfo)50 ArrayList (java.util.ArrayList)10 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)10 TenantId (org.thingsboard.server.common.data.id.TenantId)7 TbProtoQueueMsg (org.thingsboard.server.queue.common.TbProtoQueueMsg)7 EntityId (org.thingsboard.server.common.data.id.EntityId)6 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)6 UUID (java.util.UUID)5 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)5 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Set (java.util.Set)4 PostConstruct (javax.annotation.PostConstruct)4 Slf4j (lombok.extern.slf4j.Slf4j)4 EntityType (org.thingsboard.server.common.data.EntityType)4 DeviceId (org.thingsboard.server.common.data.id.DeviceId)4 ServiceType (org.thingsboard.server.common.msg.queue.ServiceType)4 ToCoreNotificationMsg (org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg)4 ToRuleEngineMsg (org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg)4 ByteString (com.google.protobuf.ByteString)3