Search in sources :

Example 1 with TbQueueConsumer

use of org.thingsboard.server.queue.TbQueueConsumer 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)

Aggregations

HashSet (java.util.HashSet)1 Set (java.util.Set)1 QueueToRuleEngineMsg (org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg)1 TopicPartitionInfo (org.thingsboard.server.common.msg.queue.TopicPartitionInfo)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 TbRuleEngineQueueConfiguration (org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration)1