Search in sources :

Example 1 with ToUsageStatsServiceMsg

use of org.thingsboard.server.gen.transport.TransportProtos.ToUsageStatsServiceMsg in project thingsboard by thingsboard.

the class DefaultTbCoreConsumerService method launchUsageStatsConsumer.

private void launchUsageStatsConsumer() {
    usageStatsExecutor.submit(() -> {
        while (!stopped) {
            try {
                List<TbProtoQueueMsg<ToUsageStatsServiceMsg>> msgs = usageStatsConsumer.poll(getNotificationPollDuration());
                if (msgs.isEmpty()) {
                    continue;
                }
                ConcurrentMap<UUID, TbProtoQueueMsg<ToUsageStatsServiceMsg>> pendingMap = msgs.stream().collect(Collectors.toConcurrentMap(s -> UUID.randomUUID(), Function.identity()));
                CountDownLatch processingTimeoutLatch = new CountDownLatch(1);
                TbPackProcessingContext<TbProtoQueueMsg<ToUsageStatsServiceMsg>> ctx = new TbPackProcessingContext<>(processingTimeoutLatch, pendingMap, new ConcurrentHashMap<>());
                pendingMap.forEach((id, msg) -> {
                    log.trace("[{}] Creating usage stats callback for message: {}", id, msg.getValue());
                    TbCallback callback = new TbPackCallback<>(id, ctx);
                    try {
                        handleUsageStats(msg, callback);
                    } catch (Throwable e) {
                        log.warn("[{}] Failed to process usage stats: {}", id, msg, e);
                        callback.onFailure(e);
                    }
                });
                if (!processingTimeoutLatch.await(getNotificationPackProcessingTimeout(), TimeUnit.MILLISECONDS)) {
                    ctx.getAckMap().forEach((id, msg) -> log.warn("[{}] Timeout to process usage stats: {}", id, msg.getValue()));
                    ctx.getFailedMap().forEach((id, msg) -> log.warn("[{}] Failed to process usage stats: {}", id, msg.getValue()));
                }
                usageStatsConsumer.commit();
            } catch (Exception e) {
                if (!stopped) {
                    log.warn("Failed to obtain usage stats from queue.", e);
                    try {
                        Thread.sleep(getNotificationPollDuration());
                    } catch (InterruptedException e2) {
                        log.trace("Failed to wait until the server has capacity to handle new usage stats", e2);
                    }
                }
            }
        }
        log.info("TB Usage Stats Consumer stopped.");
    });
}
Also used : TbCoreDeviceRpcService(org.thingsboard.server.service.rpc.TbCoreDeviceRpcService) DeviceStateServiceMsgProto(org.thingsboard.server.gen.transport.TransportProtos.DeviceStateServiceMsgProto) ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) ToDeviceRpcRequestActorMsg(org.thingsboard.server.service.rpc.ToDeviceRpcRequestActorMsg) ToCoreNotificationMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg) StatsFactory(org.thingsboard.server.common.stats.StatsFactory) TenantId(org.thingsboard.server.common.data.id.TenantId) PreDestroy(javax.annotation.PreDestroy) Future(java.util.concurrent.Future) TbAlarmDeleteProto(org.thingsboard.server.gen.transport.TransportProtos.TbAlarmDeleteProto) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) EdgeNotificationService(org.thingsboard.server.service.edge.EdgeNotificationService) SubscriptionMgrMsgProto(org.thingsboard.server.gen.transport.TransportProtos.SubscriptionMgrMsgProto) AbstractConsumerService(org.thingsboard.server.service.queue.processing.AbstractConsumerService) LocalSubscriptionServiceMsgProto(org.thingsboard.server.gen.transport.TransportProtos.LocalSubscriptionServiceMsgProto) FromDeviceRPCResponseProto(org.thingsboard.server.gen.transport.TransportProtos.FromDeviceRPCResponseProto) TbSubscriptionCloseProto(org.thingsboard.server.gen.transport.TransportProtos.TbSubscriptionCloseProto) TransportToDeviceActorMsg(org.thingsboard.server.gen.transport.TransportProtos.TransportToDeviceActorMsg) TbAlarmUpdateProto(org.thingsboard.server.gen.transport.TransportProtos.TbAlarmUpdateProto) ToOtaPackageStateServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToOtaPackageStateServiceMsg) DeviceStateService(org.thingsboard.server.service.state.DeviceStateService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EventListener(org.springframework.context.event.EventListener) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TbTimeSeriesUpdateProto(org.thingsboard.server.gen.transport.TransportProtos.TbTimeSeriesUpdateProto) Executors(java.util.concurrent.Executors) Alarm(org.thingsboard.server.common.data.alarm.Alarm) CountDownLatch(java.util.concurrent.CountDownLatch) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg) TbCoreQueueFactory(org.thingsboard.server.queue.provider.TbCoreQueueFactory) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) TbTenantProfileCache(org.thingsboard.server.dao.tenant.TbTenantProfileCache) TbApiUsageStateService(org.thingsboard.server.service.apiusage.TbApiUsageStateService) TbQueueConsumer(org.thingsboard.server.queue.TbQueueConsumer) TransportToDeviceActorMsgWrapper(org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper) Setter(lombok.Setter) Getter(lombok.Getter) JacksonUtil(org.thingsboard.common.util.JacksonUtil) Scheduled(org.springframework.scheduling.annotation.Scheduled) Function(java.util.function.Function) TbAttributeDeleteProto(org.thingsboard.server.gen.transport.TransportProtos.TbAttributeDeleteProto) DataDecodingEncodingService(org.thingsboard.server.common.transport.util.DataDecodingEncodingService) ConcurrentMap(java.util.concurrent.ConcurrentMap) Value(org.springframework.beans.factory.annotation.Value) MsgType(org.thingsboard.server.common.msg.MsgType) ToUsageStatsServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToUsageStatsServiceMsg) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) Service(org.springframework.stereotype.Service) ToCoreMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreMsg) TbAttributeUpdateProto(org.thingsboard.server.gen.transport.TransportProtos.TbAttributeUpdateProto) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) ExecutorService(java.util.concurrent.ExecutorService) IdMsgPair(org.thingsboard.server.service.queue.processing.IdMsgPair) TbSubscriptionUtils(org.thingsboard.server.service.subscription.TbSubscriptionUtils) Order(org.springframework.core.annotation.Order) FromDeviceRpcResponse(org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse) PartitionChangeEvent(org.thingsboard.server.queue.discovery.event.PartitionChangeEvent) EdgeNotificationMsgProto(org.thingsboard.server.gen.transport.TransportProtos.EdgeNotificationMsgProto) RpcError(org.thingsboard.server.common.data.rpc.RpcError) TbDeviceProfileCache(org.thingsboard.server.service.profile.TbDeviceProfileCache) TimeUnit(java.util.concurrent.TimeUnit) SubscriptionManagerService(org.thingsboard.server.service.subscription.SubscriptionManagerService) OtaPackageStateService(org.thingsboard.server.service.ota.OtaPackageStateService) TbLocalSubscriptionService(org.thingsboard.server.service.subscription.TbLocalSubscriptionService) TbTimeSeriesDeleteProto(org.thingsboard.server.gen.transport.TransportProtos.TbTimeSeriesDeleteProto) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) CountDownLatch(java.util.concurrent.CountDownLatch) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) UUID(java.util.UUID)

Example 2 with ToUsageStatsServiceMsg

use of org.thingsboard.server.gen.transport.TransportProtos.ToUsageStatsServiceMsg in project thingsboard by thingsboard.

the class DefaultTbApiUsageStateService method process.

@Override
public void process(TbProtoQueueMsg<ToUsageStatsServiceMsg> msg, TbCallback callback) {
    ToUsageStatsServiceMsg statsMsg = msg.getValue();
    TenantId tenantId = TenantId.fromUUID(new UUID(statsMsg.getTenantIdMSB(), statsMsg.getTenantIdLSB()));
    EntityId entityId;
    if (statsMsg.getCustomerIdMSB() != 0 && statsMsg.getCustomerIdLSB() != 0) {
        entityId = new CustomerId(new UUID(statsMsg.getCustomerIdMSB(), statsMsg.getCustomerIdLSB()));
    } else {
        entityId = tenantId;
    }
    processEntityUsageStats(tenantId, entityId, statsMsg.getValuesList());
    callback.onSuccess();
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) TenantId(org.thingsboard.server.common.data.id.TenantId) ToUsageStatsServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToUsageStatsServiceMsg) CustomerId(org.thingsboard.server.common.data.id.CustomerId) UUID(java.util.UUID)

Aggregations

UUID (java.util.UUID)2 TenantId (org.thingsboard.server.common.data.id.TenantId)2 List (java.util.List)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 PostConstruct (javax.annotation.PostConstruct)1 PreDestroy (javax.annotation.PreDestroy)1 Getter (lombok.Getter)1 Setter (lombok.Setter)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Value (org.springframework.beans.factory.annotation.Value)1 ApplicationReadyEvent (org.springframework.boot.context.event.ApplicationReadyEvent)1