Search in sources :

Example 46 with TopicPartitionInfo

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

the class HashPartitionService method buildTopicPartitionInfo.

private TopicPartitionInfo buildTopicPartitionInfo(ServiceQueue serviceQueue, TenantId tenantId, int partition) {
    TopicPartitionInfo.TopicPartitionInfoBuilder tpi = TopicPartitionInfo.builder();
    tpi.topic(partitionTopics.get(serviceQueue));
    tpi.partition(partition);
    ServiceQueueKey myPartitionsSearchKey;
    if (isIsolated(serviceQueue, tenantId)) {
        tpi.tenantId(tenantId);
        myPartitionsSearchKey = new ServiceQueueKey(serviceQueue, tenantId);
    } else {
        myPartitionsSearchKey = new ServiceQueueKey(serviceQueue, TenantId.SYS_TENANT_ID);
    }
    List<Integer> partitions = myPartitions.get(myPartitionsSearchKey);
    if (partitions != null) {
        tpi.myPartition(partitions.contains(partition));
    } else {
        tpi.myPartition(false);
    }
    return tpi.build();
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) ServiceQueueKey(org.thingsboard.server.common.msg.queue.ServiceQueueKey)

Example 47 with TopicPartitionInfo

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

the class DefaultTelemetrySubscriptionService method onTimeSeriesUpdate.

private void onTimeSeriesUpdate(TenantId tenantId, EntityId entityId, List<TsKvEntry> ts) {
    TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, entityId);
    if (currentPartitions.contains(tpi)) {
        if (subscriptionManagerService.isPresent()) {
            subscriptionManagerService.get().onTimeSeriesUpdate(tenantId, entityId, ts, TbCallback.EMPTY);
        } else {
            log.warn("Possible misconfiguration because subscriptionManagerService is null!");
        }
    } else {
        TransportProtos.ToCoreMsg toCoreMsg = TbSubscriptionUtils.toTimeseriesUpdateProto(tenantId, entityId, ts);
        clusterService.pushMsgToCore(tpi, entityId.getId(), toCoreMsg, null);
    }
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos)

Example 48 with TopicPartitionInfo

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

the class DefaultTelemetrySubscriptionService method onAttributesUpdate.

private void onAttributesUpdate(TenantId tenantId, EntityId entityId, String scope, List<AttributeKvEntry> attributes, boolean notifyDevice) {
    TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, entityId);
    if (currentPartitions.contains(tpi)) {
        if (subscriptionManagerService.isPresent()) {
            subscriptionManagerService.get().onAttributesUpdate(tenantId, entityId, scope, attributes, notifyDevice, TbCallback.EMPTY);
        } else {
            log.warn("Possible misconfiguration because subscriptionManagerService is null!");
        }
    } else {
        TransportProtos.ToCoreMsg toCoreMsg = TbSubscriptionUtils.toAttributesUpdateProto(tenantId, entityId, scope, attributes);
        clusterService.pushMsgToCore(tpi, entityId.getId(), toCoreMsg, null);
    }
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos)

Example 49 with TopicPartitionInfo

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

the class DefaultTbApiUsageClient method reportStats.

private void reportStats() {
    ConcurrentMap<OwnerId, ToUsageStatsServiceMsg.Builder> report = new ConcurrentHashMap<>();
    for (ApiUsageRecordKey key : ApiUsageRecordKey.values()) {
        ConcurrentMap<OwnerId, AtomicLong> statsForKey = stats.get(key);
        statsForKey.forEach((ownerId, statsValue) -> {
            long value = statsValue.get();
            if (value == 0)
                return;
            ToUsageStatsServiceMsg.Builder statsMsgBuilder = report.computeIfAbsent(ownerId, id -> {
                ToUsageStatsServiceMsg.Builder newStatsMsgBuilder = ToUsageStatsServiceMsg.newBuilder();
                TenantId tenantId = ownerId.getTenantId();
                newStatsMsgBuilder.setTenantIdMSB(tenantId.getId().getMostSignificantBits());
                newStatsMsgBuilder.setTenantIdLSB(tenantId.getId().getLeastSignificantBits());
                EntityId entityId = ownerId.getEntityId();
                if (entityId != null && entityId.getEntityType() == EntityType.CUSTOMER) {
                    newStatsMsgBuilder.setCustomerIdMSB(entityId.getId().getMostSignificantBits());
                    newStatsMsgBuilder.setCustomerIdLSB(entityId.getId().getLeastSignificantBits());
                }
                return newStatsMsgBuilder;
            });
            statsMsgBuilder.addValues(UsageStatsKVProto.newBuilder().setKey(key.name()).setValue(value).build());
        });
        statsForKey.clear();
    }
    report.forEach(((ownerId, statsMsg) -> {
        // TODO: figure out how to minimize messages into the queue. Maybe group by 100s of messages?
        TenantId tenantId = ownerId.getTenantId();
        EntityId entityId = Optional.ofNullable(ownerId.getEntityId()).orElse(tenantId);
        TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, entityId).newByTopic(msgProducer.getDefaultTopic());
        msgProducer.send(tpi, new TbProtoQueueMsg<>(UUID.randomUUID(), statsMsg.build()), null);
    }));
    if (!report.isEmpty()) {
        log.debug("Reporting API usage statistics for {} tenants and customers", report.size());
    }
}
Also used : UsageStatsKVProto(org.thingsboard.server.gen.transport.TransportProtos.UsageStatsKVProto) PartitionService(org.thingsboard.server.queue.discovery.PartitionService) ApiUsageRecordKey(org.thingsboard.server.common.data.ApiUsageRecordKey) Random(java.util.Random) TenantId(org.thingsboard.server.common.data.id.TenantId) ConcurrentMap(java.util.concurrent.ConcurrentMap) Value(org.springframework.beans.factory.annotation.Value) ToUsageStatsServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToUsageStatsServiceMsg) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) TbQueueProducer(org.thingsboard.server.queue.TbQueueProducer) SchedulerComponent(org.thingsboard.server.queue.scheduler.SchedulerComponent) EnumMap(java.util.EnumMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UUID(java.util.UUID) TbQueueProducerProvider(org.thingsboard.server.queue.provider.TbQueueProducerProvider) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) Data(lombok.Data) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) ToUsageStatsServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToUsageStatsServiceMsg) ApiUsageRecordKey(org.thingsboard.server.common.data.ApiUsageRecordKey) EntityId(org.thingsboard.server.common.data.id.EntityId) AtomicLong(java.util.concurrent.atomic.AtomicLong) TenantId(org.thingsboard.server.common.data.id.TenantId) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 50 with TopicPartitionInfo

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

the class DefaultTransportService method sendToRuleEngine.

private void sendToRuleEngine(TenantId tenantId, TbMsg tbMsg, TbQueueCallback callback) {
    TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_RULE_ENGINE, tbMsg.getQueueName(), tenantId, tbMsg.getOriginator());
    if (log.isTraceEnabled()) {
        log.trace("[{}][{}] Pushing to topic {} message {}", tenantId, tbMsg.getOriginator(), tpi.getFullTopicName(), tbMsg);
    }
    ToRuleEngineMsg msg = ToRuleEngineMsg.newBuilder().setTbMsg(TbMsg.toByteString(tbMsg)).setTenantIdMSB(tenantId.getId().getMostSignificantBits()).setTenantIdLSB(tenantId.getId().getLeastSignificantBits()).build();
    ruleEngineProducerStats.incrementTotal();
    StatsCallback wrappedCallback = new StatsCallback(callback, ruleEngineProducerStats);
    ruleEngineMsgProducer.send(tpi, new TbProtoQueueMsg<>(tbMsg.getId(), msg), wrappedCallback);
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) ToRuleEngineMsg(org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg)

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