Search in sources :

Example 16 with TopicPartitionInfo

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

the class DefaultOtaPackageStateService method send.

private void send(TenantId tenantId, DeviceId deviceId, OtaPackageId firmwareId, long ts, OtaPackageType firmwareType) {
    ToOtaPackageStateServiceMsg msg = ToOtaPackageStateServiceMsg.newBuilder().setTenantIdMSB(tenantId.getId().getMostSignificantBits()).setTenantIdLSB(tenantId.getId().getLeastSignificantBits()).setDeviceIdMSB(deviceId.getId().getMostSignificantBits()).setDeviceIdLSB(deviceId.getId().getLeastSignificantBits()).setOtaPackageIdMSB(firmwareId.getId().getMostSignificantBits()).setOtaPackageIdLSB(firmwareId.getId().getLeastSignificantBits()).setType(firmwareType.name()).setTs(ts).build();
    OtaPackageInfo firmware = otaPackageService.findOtaPackageInfoById(tenantId, firmwareId);
    if (firmware == null) {
        log.warn("[{}] Failed to send firmware update because firmware was already deleted", firmwareId);
        return;
    }
    TopicPartitionInfo tpi = new TopicPartitionInfo(otaPackageStateMsgProducer.getDefaultTopic(), null, null, false);
    otaPackageStateMsgProducer.send(tpi, new TbProtoQueueMsg<>(UUID.randomUUID(), msg), null);
    List<TsKvEntry> telemetry = new ArrayList<>();
    telemetry.add(new BasicTsKvEntry(ts, new StringDataEntry(getTargetTelemetryKey(firmware.getType(), TITLE), firmware.getTitle())));
    telemetry.add(new BasicTsKvEntry(ts, new StringDataEntry(getTargetTelemetryKey(firmware.getType(), VERSION), firmware.getVersion())));
    if (StringUtils.isNotEmpty(firmware.getTag())) {
        telemetry.add(new BasicTsKvEntry(ts, new StringDataEntry(getTargetTelemetryKey(firmware.getType(), TAG), firmware.getTag())));
    }
    telemetry.add(new BasicTsKvEntry(ts, new LongDataEntry(getTargetTelemetryKey(firmware.getType(), TS), ts)));
    telemetry.add(new BasicTsKvEntry(ts, new StringDataEntry(getTelemetryKey(firmware.getType(), STATE), OtaPackageUpdateStatus.QUEUED.name())));
    telemetryService.saveAndNotify(tenantId, deviceId, telemetry, new FutureCallback<>() {

        @Override
        public void onSuccess(@Nullable Void tmp) {
            log.trace("[{}] Success save firmware status!", deviceId);
        }

        @Override
        public void onFailure(Throwable t) {
            log.error("[{}] Failed to save firmware status!", deviceId, t);
        }
    });
}
Also used : ToOtaPackageStateServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToOtaPackageStateServiceMsg) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) ArrayList(java.util.ArrayList) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry)

Example 17 with TopicPartitionInfo

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

the class DefaultTbClusterService method onEdgeEventUpdate.

@Override
public void onEdgeEventUpdate(TenantId tenantId, EdgeId edgeId) {
    log.trace("[{}] Processing edge {} event update ", tenantId, edgeId);
    EdgeEventUpdateMsg msg = new EdgeEventUpdateMsg(tenantId, edgeId);
    byte[] msgBytes = encodingService.encode(msg);
    TbQueueProducer<TbProtoQueueMsg<ToCoreNotificationMsg>> toCoreNfProducer = producerProvider.getTbCoreNotificationsMsgProducer();
    Set<String> tbCoreServices = partitionService.getAllServiceIds(ServiceType.TB_CORE);
    for (String serviceId : tbCoreServices) {
        TopicPartitionInfo tpi = partitionService.getNotificationsTopic(ServiceType.TB_CORE, serviceId);
        ToCoreNotificationMsg toCoreMsg = ToCoreNotificationMsg.newBuilder().setEdgeEventUpdateMsg(ByteString.copyFrom(msgBytes)).build();
        toCoreNfProducer.send(tpi, new TbProtoQueueMsg<>(msg.getEdgeId().getId(), toCoreMsg), null);
        toCoreNfs.incrementAndGet();
    }
}
Also used : ToCoreNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg) EdgeEventUpdateMsg(org.thingsboard.server.common.msg.edge.EdgeEventUpdateMsg) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) ByteString(com.google.protobuf.ByteString)

Example 18 with TopicPartitionInfo

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

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

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

the class DefaultTbRuleEngineRpcService method sendRpcRequestToDevice.

private void sendRpcRequestToDevice(ToDeviceRpcRequest msg) {
    TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, msg.getTenantId(), msg.getDeviceId());
    ToDeviceRpcRequestActorMsg rpcMsg = new ToDeviceRpcRequestActorMsg(serviceId, msg);
    if (tpi.isMyPartition()) {
        log.trace("[{}] Forwarding msg {} to device actor!", msg.getDeviceId(), msg);
        if (tbCoreRpcService.isPresent()) {
            tbCoreRpcService.get().forwardRpcRequestToDeviceActor(rpcMsg);
        } else {
            log.warn("Failed to find tbCoreRpcService for local service. Possible duplication of serviceIds.");
        }
    } else {
        log.trace("[{}] Forwarding msg {} to queue actor!", msg.getDeviceId(), msg);
        clusterService.pushMsgToCore(rpcMsg, null);
    }
}
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