|
|
@ -21,9 +21,10 @@ import org.springframework.beans.factory.annotation.Value; |
|
|
import org.springframework.scheduling.annotation.Scheduled; |
|
|
import org.springframework.scheduling.annotation.Scheduled; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.thingsboard.common.util.ThingsBoardThreadFactory; |
|
|
import org.thingsboard.common.util.ThingsBoardThreadFactory; |
|
|
import org.thingsboard.server.common.data.rpc.RpcError; |
|
|
|
|
|
import org.thingsboard.server.actors.ActorSystemContext; |
|
|
import org.thingsboard.server.actors.ActorSystemContext; |
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
|
|
import org.thingsboard.server.common.data.queue.Queue; |
|
|
|
|
|
import org.thingsboard.server.common.data.rpc.RpcError; |
|
|
import org.thingsboard.server.common.msg.TbMsg; |
|
|
import org.thingsboard.server.common.msg.TbMsg; |
|
|
import org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg; |
|
|
import org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg; |
|
|
import org.thingsboard.server.common.msg.queue.RuleEngineException; |
|
|
import org.thingsboard.server.common.msg.queue.RuleEngineException; |
|
|
@ -33,21 +34,23 @@ import org.thingsboard.server.common.msg.queue.ServiceType; |
|
|
import org.thingsboard.server.common.msg.queue.TbCallback; |
|
|
import org.thingsboard.server.common.msg.queue.TbCallback; |
|
|
import org.thingsboard.server.common.msg.queue.TbMsgCallback; |
|
|
import org.thingsboard.server.common.msg.queue.TbMsgCallback; |
|
|
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; |
|
|
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; |
|
|
|
|
|
import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse; |
|
|
import org.thingsboard.server.common.stats.StatsFactory; |
|
|
import org.thingsboard.server.common.stats.StatsFactory; |
|
|
import org.thingsboard.server.common.transport.util.DataDecodingEncodingService; |
|
|
import org.thingsboard.server.common.transport.util.DataDecodingEncodingService; |
|
|
|
|
|
import org.thingsboard.server.dao.queue.QueueService; |
|
|
|
|
|
import org.thingsboard.server.dao.tenant.TbTenantProfileCache; |
|
|
import org.thingsboard.server.gen.transport.TransportProtos; |
|
|
import org.thingsboard.server.gen.transport.TransportProtos; |
|
|
import org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg; |
|
|
import org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg; |
|
|
import org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineNotificationMsg; |
|
|
import org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineNotificationMsg; |
|
|
import org.thingsboard.server.queue.TbQueueConsumer; |
|
|
import org.thingsboard.server.queue.TbQueueConsumer; |
|
|
import org.thingsboard.server.queue.common.TbProtoQueueMsg; |
|
|
import org.thingsboard.server.queue.common.TbProtoQueueMsg; |
|
|
|
|
|
import org.thingsboard.server.queue.discovery.PartitionService; |
|
|
|
|
|
import org.thingsboard.server.queue.discovery.TbServiceInfoProvider; |
|
|
import org.thingsboard.server.queue.discovery.event.PartitionChangeEvent; |
|
|
import org.thingsboard.server.queue.discovery.event.PartitionChangeEvent; |
|
|
import org.thingsboard.server.queue.provider.TbRuleEngineQueueFactory; |
|
|
import org.thingsboard.server.queue.provider.TbRuleEngineQueueFactory; |
|
|
import org.thingsboard.server.queue.settings.TbQueueRuleEngineSettings; |
|
|
|
|
|
import org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration; |
|
|
|
|
|
import org.thingsboard.server.queue.util.TbRuleEngineComponent; |
|
|
import org.thingsboard.server.queue.util.TbRuleEngineComponent; |
|
|
import org.thingsboard.server.service.apiusage.TbApiUsageStateService; |
|
|
import org.thingsboard.server.service.apiusage.TbApiUsageStateService; |
|
|
import org.thingsboard.server.service.profile.TbDeviceProfileCache; |
|
|
import org.thingsboard.server.service.profile.TbDeviceProfileCache; |
|
|
import org.thingsboard.server.dao.tenant.TbTenantProfileCache; |
|
|
|
|
|
import org.thingsboard.server.service.queue.processing.AbstractConsumerService; |
|
|
import org.thingsboard.server.service.queue.processing.AbstractConsumerService; |
|
|
import org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingDecision; |
|
|
import org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingDecision; |
|
|
import org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingResult; |
|
|
import org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingResult; |
|
|
@ -55,17 +58,16 @@ import org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingStr |
|
|
import org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingStrategyFactory; |
|
|
import org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingStrategyFactory; |
|
|
import org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrategy; |
|
|
import org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrategy; |
|
|
import org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrategyFactory; |
|
|
import org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrategyFactory; |
|
|
import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse; |
|
|
|
|
|
import org.thingsboard.server.service.rpc.TbRuleEngineDeviceRpcService; |
|
|
import org.thingsboard.server.service.rpc.TbRuleEngineDeviceRpcService; |
|
|
import org.thingsboard.server.service.stats.RuleEngineStatisticsService; |
|
|
import org.thingsboard.server.service.stats.RuleEngineStatisticsService; |
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
import javax.annotation.PostConstruct; |
|
|
import javax.annotation.PreDestroy; |
|
|
import javax.annotation.PreDestroy; |
|
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.Collections; |
|
|
import java.util.Collections; |
|
|
import java.util.HashSet; |
|
|
import java.util.HashSet; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
import java.util.Queue; |
|
|
|
|
|
import java.util.Set; |
|
|
import java.util.Set; |
|
|
import java.util.UUID; |
|
|
import java.util.UUID; |
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
@ -74,6 +76,7 @@ import java.util.concurrent.ExecutorService; |
|
|
import java.util.concurrent.Executors; |
|
|
import java.util.concurrent.Executors; |
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
|
|
|
|
|
|
@Service |
|
|
@Service |
|
|
@TbRuleEngineComponent |
|
|
@TbRuleEngineComponent |
|
|
@ -96,11 +99,14 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< |
|
|
private final TbRuleEngineSubmitStrategyFactory submitStrategyFactory; |
|
|
private final TbRuleEngineSubmitStrategyFactory submitStrategyFactory; |
|
|
private final TbRuleEngineProcessingStrategyFactory processingStrategyFactory; |
|
|
private final TbRuleEngineProcessingStrategyFactory processingStrategyFactory; |
|
|
private final TbRuleEngineQueueFactory tbRuleEngineQueueFactory; |
|
|
private final TbRuleEngineQueueFactory tbRuleEngineQueueFactory; |
|
|
private final TbQueueRuleEngineSettings ruleEngineSettings; |
|
|
|
|
|
private final RuleEngineStatisticsService statisticsService; |
|
|
private final RuleEngineStatisticsService statisticsService; |
|
|
private final TbRuleEngineDeviceRpcService tbDeviceRpcService; |
|
|
private final TbRuleEngineDeviceRpcService tbDeviceRpcService; |
|
|
|
|
|
private final PartitionService partitionService; |
|
|
|
|
|
private final TbServiceInfoProvider serviceInfoProvider; |
|
|
|
|
|
private final QueueService queueService; |
|
|
|
|
|
private final TenantId tenantId; |
|
|
private final ConcurrentMap<String, TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>>> consumers = new ConcurrentHashMap<>(); |
|
|
private final ConcurrentMap<String, TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>>> consumers = new ConcurrentHashMap<>(); |
|
|
private final ConcurrentMap<String, TbRuleEngineQueueConfiguration> consumerConfigurations = new ConcurrentHashMap<>(); |
|
|
private final ConcurrentMap<String, Queue> consumerConfigurations = new ConcurrentHashMap<>(); |
|
|
private final ConcurrentMap<String, TbRuleEngineConsumerStats> consumerStats = new ConcurrentHashMap<>(); |
|
|
private final ConcurrentMap<String, TbRuleEngineConsumerStats> consumerStats = new ConcurrentHashMap<>(); |
|
|
private final ConcurrentMap<String, TbTopicWithConsumerPerPartition> topicsConsumerPerPartition = new ConcurrentHashMap<>(); |
|
|
private final ConcurrentMap<String, TbTopicWithConsumerPerPartition> topicsConsumerPerPartition = new ConcurrentHashMap<>(); |
|
|
final ExecutorService submitExecutor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("tb-rule-engine-consumer-submit")); |
|
|
final ExecutorService submitExecutor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("tb-rule-engine-consumer-submit")); |
|
|
@ -108,7 +114,6 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< |
|
|
|
|
|
|
|
|
public DefaultTbRuleEngineConsumerService(TbRuleEngineProcessingStrategyFactory processingStrategyFactory, |
|
|
public DefaultTbRuleEngineConsumerService(TbRuleEngineProcessingStrategyFactory processingStrategyFactory, |
|
|
TbRuleEngineSubmitStrategyFactory submitStrategyFactory, |
|
|
TbRuleEngineSubmitStrategyFactory submitStrategyFactory, |
|
|
TbQueueRuleEngineSettings ruleEngineSettings, |
|
|
|
|
|
TbRuleEngineQueueFactory tbRuleEngineQueueFactory, |
|
|
TbRuleEngineQueueFactory tbRuleEngineQueueFactory, |
|
|
RuleEngineStatisticsService statisticsService, |
|
|
RuleEngineStatisticsService statisticsService, |
|
|
ActorSystemContext actorContext, |
|
|
ActorSystemContext actorContext, |
|
|
@ -117,28 +122,37 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< |
|
|
StatsFactory statsFactory, |
|
|
StatsFactory statsFactory, |
|
|
TbDeviceProfileCache deviceProfileCache, |
|
|
TbDeviceProfileCache deviceProfileCache, |
|
|
TbTenantProfileCache tenantProfileCache, |
|
|
TbTenantProfileCache tenantProfileCache, |
|
|
TbApiUsageStateService apiUsageStateService) { |
|
|
TbApiUsageStateService apiUsageStateService, |
|
|
|
|
|
PartitionService partitionService, TbServiceInfoProvider serviceInfoProvider, QueueService queueService) { |
|
|
super(actorContext, encodingService, tenantProfileCache, deviceProfileCache, apiUsageStateService, tbRuleEngineQueueFactory.createToRuleEngineNotificationsMsgConsumer()); |
|
|
super(actorContext, encodingService, tenantProfileCache, deviceProfileCache, apiUsageStateService, tbRuleEngineQueueFactory.createToRuleEngineNotificationsMsgConsumer()); |
|
|
this.statisticsService = statisticsService; |
|
|
this.statisticsService = statisticsService; |
|
|
this.ruleEngineSettings = ruleEngineSettings; |
|
|
|
|
|
this.tbRuleEngineQueueFactory = tbRuleEngineQueueFactory; |
|
|
this.tbRuleEngineQueueFactory = tbRuleEngineQueueFactory; |
|
|
this.submitStrategyFactory = submitStrategyFactory; |
|
|
this.submitStrategyFactory = submitStrategyFactory; |
|
|
this.processingStrategyFactory = processingStrategyFactory; |
|
|
this.processingStrategyFactory = processingStrategyFactory; |
|
|
this.tbDeviceRpcService = tbDeviceRpcService; |
|
|
this.tbDeviceRpcService = tbDeviceRpcService; |
|
|
this.statsFactory = statsFactory; |
|
|
this.statsFactory = statsFactory; |
|
|
|
|
|
this.partitionService = partitionService; |
|
|
|
|
|
this.serviceInfoProvider = serviceInfoProvider; |
|
|
|
|
|
this.queueService = queueService; |
|
|
|
|
|
this.tenantId = actorContext.getServiceInfoProvider().getIsolatedTenant().orElse(TenantId.SYS_TENANT_ID); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
@PostConstruct |
|
|
public void init() { |
|
|
public void init() { |
|
|
super.init("tb-rule-engine-consumer", "tb-rule-engine-notifications-consumer"); |
|
|
super.init("tb-rule-engine-consumer", "tb-rule-engine-notifications-consumer"); |
|
|
for (TbRuleEngineQueueConfiguration configuration : ruleEngineSettings.getQueues()) { |
|
|
List<Queue> queues = queueService.findQueuesByTenantId(tenantId); |
|
|
consumerConfigurations.putIfAbsent(configuration.getName(), configuration); |
|
|
for (Queue configuration : queues) { |
|
|
consumerStats.put(configuration.getName(), new TbRuleEngineConsumerStats(configuration.getName(), statsFactory)); |
|
|
initConsumer(configuration); |
|
|
if (!configuration.isConsumerPerPartition()) { |
|
|
} |
|
|
consumers.computeIfAbsent(configuration.getName(), queueName -> tbRuleEngineQueueFactory.createToRuleEngineMsgConsumer(configuration)); |
|
|
} |
|
|
} else { |
|
|
|
|
|
topicsConsumerPerPartition.computeIfAbsent(configuration.getName(), TbTopicWithConsumerPerPartition::new); |
|
|
private void initConsumer(Queue configuration) { |
|
|
} |
|
|
consumerConfigurations.putIfAbsent(configuration.getName(), configuration); |
|
|
|
|
|
consumerStats.putIfAbsent(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); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -147,7 +161,6 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< |
|
|
super.destroy(); |
|
|
super.destroy(); |
|
|
submitExecutor.shutdownNow(); |
|
|
submitExecutor.shutdownNow(); |
|
|
repartitionExecutor.shutdownNow(); |
|
|
repartitionExecutor.shutdownNow(); |
|
|
ruleEngineSettings.getQueues().forEach(config -> consumerConfigurations.put(config.getName(), config)); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
@ -178,7 +191,7 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
TbTopicWithConsumerPerPartition tbTopicWithConsumerPerPartition = topicsConsumerPerPartition.get(queueName); |
|
|
TbTopicWithConsumerPerPartition tbTopicWithConsumerPerPartition = topicsConsumerPerPartition.get(queueName); |
|
|
Queue<Set<TopicPartitionInfo>> subscribeQueue = tbTopicWithConsumerPerPartition.getSubscribeQueue(); |
|
|
java.util.Queue<Set<TopicPartitionInfo>> subscribeQueue = tbTopicWithConsumerPerPartition.getSubscribeQueue(); |
|
|
if (subscribeQueue.isEmpty()) { |
|
|
if (subscribeQueue.isEmpty()) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
@ -207,7 +220,7 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< |
|
|
|
|
|
|
|
|
addedPartitions.forEach((tpi) -> { |
|
|
addedPartitions.forEach((tpi) -> { |
|
|
log.info("[{}] Adding consumer for topic: {}", queueName, tpi); |
|
|
log.info("[{}] Adding consumer for topic: {}", queueName, tpi); |
|
|
TbRuleEngineQueueConfiguration configuration = consumerConfigurations.get(queueName); |
|
|
Queue configuration = consumerConfigurations.get(queueName); |
|
|
TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer = tbRuleEngineQueueFactory.createToRuleEngineMsgConsumer(configuration); |
|
|
TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer = tbRuleEngineQueueFactory.createToRuleEngineMsgConsumer(configuration); |
|
|
consumers.put(tpi, consumer); |
|
|
consumers.put(tpi, consumer); |
|
|
launchConsumer(consumer, consumerConfigurations.get(queueName), consumerStats.get(queueName), "" + queueName + "-" + tpi.getPartition().orElse(-999999)); |
|
|
launchConsumer(consumer, consumerConfigurations.get(queueName), consumerStats.get(queueName), "" + queueName + "-" + tpi.getPartition().orElse(-999999)); |
|
|
@ -241,11 +254,11 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< |
|
|
.forEach((tpi) -> removeConsumerForTopicByTpi(tbTopicWithConsumerPerPartition.getTopic(), tbTopicWithConsumerPerPartition.getConsumers(), tpi))); |
|
|
.forEach((tpi) -> removeConsumerForTopicByTpi(tbTopicWithConsumerPerPartition.getTopic(), tbTopicWithConsumerPerPartition.getConsumers(), tpi))); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void launchConsumer(TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer, TbRuleEngineQueueConfiguration configuration, TbRuleEngineConsumerStats stats, String threadSuffix) { |
|
|
void launchConsumer(TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer, Queue configuration, TbRuleEngineConsumerStats stats, String threadSuffix) { |
|
|
consumersExecutor.execute(() -> consumerLoop(consumer, configuration, stats, threadSuffix)); |
|
|
consumersExecutor.execute(() -> consumerLoop(consumer, configuration, stats, threadSuffix)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void consumerLoop(TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer, TbRuleEngineQueueConfiguration configuration, TbRuleEngineConsumerStats stats, String threadSuffix) { |
|
|
void consumerLoop(TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer, org.thingsboard.server.common.data.queue.Queue configuration, TbRuleEngineConsumerStats stats, String threadSuffix) { |
|
|
updateCurrentThreadName(threadSuffix); |
|
|
updateCurrentThreadName(threadSuffix); |
|
|
while (!stopped && !consumer.isStopped()) { |
|
|
while (!stopped && !consumer.isStopped()) { |
|
|
try { |
|
|
try { |
|
|
@ -310,15 +323,15 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< |
|
|
Thread.currentThread().setName(name); |
|
|
Thread.currentThread().setName(name); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
TbRuleEngineProcessingStrategy getAckStrategy(TbRuleEngineQueueConfiguration configuration) { |
|
|
TbRuleEngineProcessingStrategy getAckStrategy(Queue configuration) { |
|
|
return processingStrategyFactory.newInstance(configuration.getName(), configuration.getProcessingStrategy()); |
|
|
return processingStrategyFactory.newInstance(configuration.getName(), configuration.getProcessingStrategy()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
TbRuleEngineSubmitStrategy getSubmitStrategy(TbRuleEngineQueueConfiguration configuration) { |
|
|
TbRuleEngineSubmitStrategy getSubmitStrategy(Queue configuration) { |
|
|
return submitStrategyFactory.newInstance(configuration.getName(), configuration.getSubmitStrategy()); |
|
|
return submitStrategyFactory.newInstance(configuration.getName(), configuration.getSubmitStrategy()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void submitMessage(TbRuleEngineQueueConfiguration configuration, TbRuleEngineConsumerStats stats, TbMsgPackProcessingContext ctx, UUID id, TbProtoQueueMsg<ToRuleEngineMsg> msg) { |
|
|
void submitMessage(Queue configuration, TbRuleEngineConsumerStats stats, TbMsgPackProcessingContext ctx, UUID id, TbProtoQueueMsg<ToRuleEngineMsg> msg) { |
|
|
log.trace("[{}] Creating callback for topic {} message: {}", id, configuration.getName(), msg.getValue()); |
|
|
log.trace("[{}] Creating callback for topic {} message: {}", id, configuration.getName(), msg.getValue()); |
|
|
ToRuleEngineMsg toRuleEngineMsg = msg.getValue(); |
|
|
ToRuleEngineMsg toRuleEngineMsg = msg.getValue(); |
|
|
TenantId tenantId = TenantId.fromUUID(new UUID(toRuleEngineMsg.getTenantIdMSB(), toRuleEngineMsg.getTenantIdLSB())); |
|
|
TenantId tenantId = TenantId.fromUUID(new UUID(toRuleEngineMsg.getTenantIdMSB(), toRuleEngineMsg.getTenantIdLSB())); |
|
|
@ -336,7 +349,7 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void printFirstOrAll(TbRuleEngineQueueConfiguration configuration, TbMsgPackProcessingContext ctx, Map<UUID, TbProtoQueueMsg<ToRuleEngineMsg>> map, String prefix) { |
|
|
private void printFirstOrAll(Queue configuration, TbMsgPackProcessingContext ctx, Map<UUID, TbProtoQueueMsg<ToRuleEngineMsg>> map, String prefix) { |
|
|
boolean printAll = log.isTraceEnabled(); |
|
|
boolean printAll = log.isTraceEnabled(); |
|
|
log.info("{} to process [{}] messages", prefix, map.size()); |
|
|
log.info("{} to process [{}] messages", prefix, map.size()); |
|
|
for (Map.Entry<UUID, TbProtoQueueMsg<ToRuleEngineMsg>> pending : map.entrySet()) { |
|
|
for (Map.Entry<UUID, TbProtoQueueMsg<ToRuleEngineMsg>> pending : map.entrySet()) { |
|
|
@ -380,12 +393,67 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< |
|
|
, proto.getResponse(), error); |
|
|
, proto.getResponse(), error); |
|
|
tbDeviceRpcService.processRpcResponseFromDevice(response); |
|
|
tbDeviceRpcService.processRpcResponseFromDevice(response); |
|
|
callback.onSuccess(); |
|
|
callback.onSuccess(); |
|
|
|
|
|
} else if (nfMsg.hasQueueUpdateMsg()) { |
|
|
|
|
|
updateQueue(nfMsg.getQueueUpdateMsg()); |
|
|
|
|
|
callback.onSuccess(); |
|
|
|
|
|
} else if (nfMsg.hasQueueDeleteMsg()) { |
|
|
|
|
|
deleteQueue(nfMsg.getQueueDeleteMsg()); |
|
|
|
|
|
callback.onSuccess(); |
|
|
} else { |
|
|
} else { |
|
|
log.trace("Received notification with missing handler"); |
|
|
log.trace("Received notification with missing handler"); |
|
|
callback.onSuccess(); |
|
|
callback.onSuccess(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void updateQueue(TransportProtos.QueueUpdateMsg queueUpdateMsg) { |
|
|
|
|
|
String queueName = queueUpdateMsg.getQueueName(); |
|
|
|
|
|
Queue queue = queueService.findQueueByTenantIdAndName(tenantId, queueName); |
|
|
|
|
|
Queue oldQueue = consumerConfigurations.remove(queueName); |
|
|
|
|
|
if (oldQueue != null) { |
|
|
|
|
|
if (oldQueue.isConsumerPerPartition()) { |
|
|
|
|
|
TbTopicWithConsumerPerPartition consumerPerPartition = topicsConsumerPerPartition.remove(queueName); |
|
|
|
|
|
ReentrantLock lock = consumerPerPartition.getLock(); |
|
|
|
|
|
try { |
|
|
|
|
|
lock.lock(); |
|
|
|
|
|
consumerPerPartition.getConsumers().values().forEach(TbQueueConsumer::unsubscribe); |
|
|
|
|
|
} finally { |
|
|
|
|
|
lock.unlock(); |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer = consumers.remove(queueName); |
|
|
|
|
|
consumer.unsubscribe(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
initConsumer(queue); |
|
|
|
|
|
|
|
|
|
|
|
if (!queue.isConsumerPerPartition()) { |
|
|
|
|
|
launchConsumer(consumers.get(queueName), consumerConfigurations.get(queueName), consumerStats.get(queueName), queueName); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
partitionService.updateQueue(queueUpdateMsg); |
|
|
|
|
|
partitionService.recalculatePartitions(serviceInfoProvider.getServiceInfo(), new ArrayList<>(partitionService.getOtherServices(ServiceType.TB_RULE_ENGINE))); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void deleteQueue(TransportProtos.QueueDeleteMsg queueDeleteMsg) { |
|
|
|
|
|
Queue queue = consumerConfigurations.remove(queueDeleteMsg.getQueueName()); |
|
|
|
|
|
if (queue != null) { |
|
|
|
|
|
if (queue.isConsumerPerPartition()) { |
|
|
|
|
|
TbTopicWithConsumerPerPartition tbTopicWithConsumerPerPartition = topicsConsumerPerPartition.remove(queueDeleteMsg.getQueueName()); |
|
|
|
|
|
if (tbTopicWithConsumerPerPartition != null) { |
|
|
|
|
|
tbTopicWithConsumerPerPartition.getConsumers().values().forEach(TbQueueConsumer::unsubscribe); |
|
|
|
|
|
tbTopicWithConsumerPerPartition.getConsumers().clear(); |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer = consumers.remove(queueDeleteMsg.getQueueName()); |
|
|
|
|
|
if (consumer != null) { |
|
|
|
|
|
consumer.unsubscribe(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
partitionService.removeQueue(queueDeleteMsg); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private void forwardToRuleEngineActor(String queueName, TenantId tenantId, ToRuleEngineMsg toRuleEngineMsg, TbMsgCallback callback) { |
|
|
private void forwardToRuleEngineActor(String queueName, TenantId tenantId, ToRuleEngineMsg toRuleEngineMsg, TbMsgCallback callback) { |
|
|
TbMsg tbMsg = TbMsg.fromBytes(queueName, toRuleEngineMsg.getTbMsg().toByteArray(), callback); |
|
|
TbMsg tbMsg = TbMsg.fromBytes(queueName, toRuleEngineMsg.getTbMsg().toByteArray(), callback); |
|
|
QueueToRuleEngineMsg msg; |
|
|
QueueToRuleEngineMsg msg; |
|
|
|