Search in sources :

Example 1 with TbRuleEngineQueueConfiguration

use of org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration 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 2 with TbRuleEngineQueueConfiguration

use of org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration in project thingsboard by thingsboard.

the class DefaultTbRuleEngineConsumerService method init.

@PostConstruct
public void init() {
    super.init("tb-rule-engine-consumer", "tb-rule-engine-notifications-consumer");
    for (TbRuleEngineQueueConfiguration configuration : ruleEngineSettings.getQueues()) {
        consumerConfigurations.putIfAbsent(configuration.getName(), configuration);
        consumerStats.put(configuration.getName(), new TbRuleEngineConsumerStats(configuration.getName(), statsFactory));
        if (!configuration.isConsumerPerPartition()) {
            consumers.computeIfAbsent(configuration.getName(), queueName -> tbRuleEngineQueueFactory.createToRuleEngineMsgConsumer(configuration));
        } else {
            topicsConsumerPerPartition.computeIfAbsent(configuration.getName(), TbTopicWithConsumerPerPartition::new);
        }
    }
}
Also used : TbRuleEngineQueueConfiguration(org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration) PostConstruct(javax.annotation.PostConstruct)

Example 3 with TbRuleEngineQueueConfiguration

use of org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration in project thingsboard by thingsboard.

the class DefaultTbServiceInfoProvider method init.

@PostConstruct
public void init() {
    if (StringUtils.isEmpty(serviceId)) {
        try {
            serviceId = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            serviceId = org.apache.commons.lang3.RandomStringUtils.randomAlphabetic(10);
        }
    }
    log.info("Current Service ID: {}", serviceId);
    if (serviceType.equalsIgnoreCase("monolith")) {
        serviceTypes = Collections.unmodifiableList(Arrays.asList(ServiceType.values()));
    } else {
        serviceTypes = Collections.singletonList(ServiceType.of(serviceType));
    }
    ServiceInfo.Builder builder = ServiceInfo.newBuilder().setServiceId(serviceId).addAllServiceTypes(serviceTypes.stream().map(ServiceType::name).collect(Collectors.toList()));
    UUID tenantId;
    if (!StringUtils.isEmpty(tenantIdStr)) {
        tenantId = UUID.fromString(tenantIdStr);
        isolatedTenant = TenantId.fromUUID(tenantId);
    } else {
        tenantId = TenantId.NULL_UUID;
    }
    builder.setTenantIdMSB(tenantId.getMostSignificantBits());
    builder.setTenantIdLSB(tenantId.getLeastSignificantBits());
    if (serviceTypes.contains(ServiceType.TB_RULE_ENGINE) && ruleEngineSettings != null) {
        for (TbRuleEngineQueueConfiguration queue : ruleEngineSettings.getQueues()) {
            TransportProtos.QueueInfo queueInfo = TransportProtos.QueueInfo.newBuilder().setName(queue.getName()).setTopic(queue.getTopic()).setPartitions(queue.getPartitions()).build();
            builder.addRuleEngineQueues(queueInfo);
        }
    }
    serviceInfo = builder.build();
}
Also used : ServiceInfo(org.thingsboard.server.gen.transport.TransportProtos.ServiceInfo) UnknownHostException(java.net.UnknownHostException) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) TbRuleEngineQueueConfiguration(org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) UUID(java.util.UUID) PostConstruct(javax.annotation.PostConstruct)

Aggregations

TbRuleEngineQueueConfiguration (org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration)3 PostConstruct (javax.annotation.PostConstruct)2 UnknownHostException (java.net.UnknownHostException)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 UUID (java.util.UUID)1 QueueToRuleEngineMsg (org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg)1 ServiceType (org.thingsboard.server.common.msg.queue.ServiceType)1 TopicPartitionInfo (org.thingsboard.server.common.msg.queue.TopicPartitionInfo)1 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)1 ServiceInfo (org.thingsboard.server.gen.transport.TransportProtos.ServiceInfo)1 ToRuleEngineMsg (org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg)1 TbQueueConsumer (org.thingsboard.server.queue.TbQueueConsumer)1 TbProtoQueueMsg (org.thingsboard.server.queue.common.TbProtoQueueMsg)1