Search in sources :

Example 1 with ToRuleEngineMsg

use of org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg in project thingsboard by thingsboard.

the class DefaultTbClusterService method pushMsgToRuleEngine.

@Override
public void pushMsgToRuleEngine(TenantId tenantId, EntityId entityId, TbMsg tbMsg, TbQueueCallback callback) {
    if (tenantId.isNullUid()) {
        if (entityId.getEntityType().equals(EntityType.TENANT)) {
            tenantId = TenantId.fromUUID(entityId.getId());
        } else {
            log.warn("[{}][{}] Received invalid message: {}", tenantId, entityId, tbMsg);
            return;
        }
    } else {
        if (entityId.getEntityType().equals(EntityType.DEVICE)) {
            tbMsg = transformMsg(tbMsg, deviceProfileCache.get(tenantId, new DeviceId(entityId.getId())));
        } else if (entityId.getEntityType().equals(EntityType.DEVICE_PROFILE)) {
            tbMsg = transformMsg(tbMsg, deviceProfileCache.get(tenantId, new DeviceProfileId(entityId.getId())));
        }
    }
    TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_RULE_ENGINE, tbMsg.getQueueName(), tenantId, entityId);
    log.trace("PUSHING msg: {} to:{}", tbMsg, tpi);
    ToRuleEngineMsg msg = ToRuleEngineMsg.newBuilder().setTenantIdMSB(tenantId.getId().getMostSignificantBits()).setTenantIdLSB(tenantId.getId().getLeastSignificantBits()).setTbMsg(TbMsg.toByteString(tbMsg)).build();
    producerProvider.getRuleEngineMsgProducer().send(tpi, new TbProtoQueueMsg<>(tbMsg.getId(), msg), callback);
    toRuleEngineMsgs.incrementAndGet();
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ToRuleEngineMsg(org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg)

Example 2 with ToRuleEngineMsg

use of org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg in project thingsboard by thingsboard.

the class DefaultTbRuleEngineConsumerService method printFirstOrAll.

private void printFirstOrAll(TbRuleEngineQueueConfiguration configuration, TbMsgPackProcessingContext ctx, Map<UUID, TbProtoQueueMsg<ToRuleEngineMsg>> map, String prefix) {
    boolean printAll = log.isTraceEnabled();
    log.info("{} to process [{}] messages", prefix, map.size());
    for (Map.Entry<UUID, TbProtoQueueMsg<ToRuleEngineMsg>> pending : map.entrySet()) {
        ToRuleEngineMsg tmp = pending.getValue().getValue();
        TbMsg tmpMsg = TbMsg.fromBytes(configuration.getName(), tmp.getTbMsg().toByteArray(), TbMsgCallback.EMPTY);
        RuleNodeInfo ruleNodeInfo = ctx.getLastVisitedRuleNode(pending.getKey());
        if (printAll) {
            log.trace("[{}] {} to process message: {}, Last Rule Node: {}", TenantId.fromUUID(new UUID(tmp.getTenantIdMSB(), tmp.getTenantIdLSB())), prefix, tmpMsg, ruleNodeInfo);
        } else {
            log.info("[{}] {} to process message: {}, Last Rule Node: {}", TenantId.fromUUID(new UUID(tmp.getTenantIdMSB(), tmp.getTenantIdLSB())), prefix, tmpMsg, ruleNodeInfo);
            break;
        }
    }
}
Also used : RuleNodeInfo(org.thingsboard.server.common.msg.queue.RuleNodeInfo) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) UUID(java.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) ToRuleEngineMsg(org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg) TbMsg(org.thingsboard.server.common.msg.TbMsg)

Example 3 with ToRuleEngineMsg

use of org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg in project thingsboard by thingsboard.

the class DefaultTbRuleEngineConsumerService method repartitionTopicWithConsumerPerPartition.

void repartitionTopicWithConsumerPerPartition(final String queueName) {
    if (stopped) {
        return;
    }
    TbTopicWithConsumerPerPartition tbTopicWithConsumerPerPartition = topicsConsumerPerPartition.get(queueName);
    Queue<Set<TopicPartitionInfo>> subscribeQueue = tbTopicWithConsumerPerPartition.getSubscribeQueue();
    if (subscribeQueue.isEmpty()) {
        return;
    }
    if (tbTopicWithConsumerPerPartition.getLock().tryLock()) {
        try {
            Set<TopicPartitionInfo> partitions = null;
            while (!subscribeQueue.isEmpty()) {
                partitions = subscribeQueue.poll();
            }
            if (partitions == null) {
                return;
            }
            Set<TopicPartitionInfo> addedPartitions = new HashSet<>(partitions);
            ConcurrentMap<TopicPartitionInfo, TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>>> consumers = tbTopicWithConsumerPerPartition.getConsumers();
            addedPartitions.removeAll(consumers.keySet());
            log.info("calculated addedPartitions {}", addedPartitions);
            Set<TopicPartitionInfo> removedPartitions = new HashSet<>(consumers.keySet());
            removedPartitions.removeAll(partitions);
            log.info("calculated removedPartitions {}", removedPartitions);
            removedPartitions.forEach((tpi) -> {
                removeConsumerForTopicByTpi(queueName, consumers, tpi);
            });
            addedPartitions.forEach((tpi) -> {
                log.info("[{}] Adding consumer for topic: {}", queueName, tpi);
                TbRuleEngineQueueConfiguration configuration = consumerConfigurations.get(queueName);
                TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer = tbRuleEngineQueueFactory.createToRuleEngineMsgConsumer(configuration);
                consumers.put(tpi, consumer);
                launchConsumer(consumer, consumerConfigurations.get(queueName), consumerStats.get(queueName), "" + queueName + "-" + tpi.getPartition().orElse(-999999));
                consumer.subscribe(Collections.singleton(tpi));
            });
        } finally {
            tbTopicWithConsumerPerPartition.getLock().unlock();
        }
    } else {
        // reschedule later
        scheduleTopicRepartition(queueName);
    }
}
Also used : TbQueueConsumer(org.thingsboard.server.queue.TbQueueConsumer) Set(java.util.Set) HashSet(java.util.HashSet) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) TbRuleEngineQueueConfiguration(org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) ToRuleEngineMsg(org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg) HashSet(java.util.HashSet)

Example 4 with ToRuleEngineMsg

use of org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg in project thingsboard by thingsboard.

the class RuleChainActorMessageProcessor method putToQueue.

private void putToQueue(TopicPartitionInfo tpi, TbMsg newMsg, TbQueueCallback callbackWrapper) {
    ToRuleEngineMsg toQueueMsg = ToRuleEngineMsg.newBuilder().setTenantIdMSB(tenantId.getId().getMostSignificantBits()).setTenantIdLSB(tenantId.getId().getLeastSignificantBits()).setTbMsg(TbMsg.toByteString(newMsg)).build();
    clusterService.pushMsgToRuleEngine(tpi, newMsg.getId(), toQueueMsg, callbackWrapper);
}
Also used : QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) ToRuleEngineMsg(org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg)

Example 5 with ToRuleEngineMsg

use of org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg in project thingsboard by thingsboard.

the class DefaultTbRuleEngineConsumerService method submitMessage.

void submitMessage(TbRuleEngineQueueConfiguration configuration, TbRuleEngineConsumerStats stats, TbMsgPackProcessingContext ctx, UUID id, TbProtoQueueMsg<ToRuleEngineMsg> msg) {
    log.trace("[{}] Creating callback for topic {} message: {}", id, configuration.getName(), msg.getValue());
    ToRuleEngineMsg toRuleEngineMsg = msg.getValue();
    TenantId tenantId = TenantId.fromUUID(new UUID(toRuleEngineMsg.getTenantIdMSB(), toRuleEngineMsg.getTenantIdLSB()));
    TbMsgCallback callback = prometheusStatsEnabled ? new TbMsgPackCallback(id, tenantId, ctx, stats.getTimer(tenantId, SUCCESSFUL_STATUS), stats.getTimer(tenantId, FAILED_STATUS)) : new TbMsgPackCallback(id, tenantId, ctx);
    try {
        if (toRuleEngineMsg.getTbMsg() != null && !toRuleEngineMsg.getTbMsg().isEmpty()) {
            forwardToRuleEngineActor(configuration.getName(), tenantId, toRuleEngineMsg, callback);
        } else {
            callback.onSuccess();
        }
    } catch (Exception e) {
        callback.onFailure(new RuleEngineException(e.getMessage()));
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException) UUID(java.util.UUID) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) ToRuleEngineMsg(org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg) TbMsgCallback(org.thingsboard.server.common.msg.queue.TbMsgCallback) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException)

Aggregations

ToRuleEngineMsg (org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg)6 QueueToRuleEngineMsg (org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg)4 TopicPartitionInfo (org.thingsboard.server.common.msg.queue.TopicPartitionInfo)3 UUID (java.util.UUID)2 TbProtoQueueMsg (org.thingsboard.server.queue.common.TbProtoQueueMsg)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 DeviceId (org.thingsboard.server.common.data.id.DeviceId)1 DeviceProfileId (org.thingsboard.server.common.data.id.DeviceProfileId)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1 TbMsg (org.thingsboard.server.common.msg.TbMsg)1 RuleEngineException (org.thingsboard.server.common.msg.queue.RuleEngineException)1 RuleNodeInfo (org.thingsboard.server.common.msg.queue.RuleNodeInfo)1 TbMsgCallback (org.thingsboard.server.common.msg.queue.TbMsgCallback)1 TbQueueConsumer (org.thingsboard.server.queue.TbQueueConsumer)1 TbRuleEngineQueueConfiguration (org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration)1