Search in sources :

Example 1 with IdMsgPair

use of org.thingsboard.server.service.queue.processing.IdMsgPair in project thingsboard by thingsboard.

the class DefaultTbCoreConsumerService method launchMainConsumers.

@Override
protected void launchMainConsumers() {
    consumersExecutor.submit(() -> {
        while (!stopped) {
            try {
                List<TbProtoQueueMsg<ToCoreMsg>> msgs = mainConsumer.poll(pollDuration);
                if (msgs.isEmpty()) {
                    continue;
                }
                List<IdMsgPair<ToCoreMsg>> orderedMsgList = msgs.stream().map(msg -> new IdMsgPair<>(UUID.randomUUID(), msg)).collect(Collectors.toList());
                ConcurrentMap<UUID, TbProtoQueueMsg<ToCoreMsg>> pendingMap = orderedMsgList.stream().collect(Collectors.toConcurrentMap(IdMsgPair::getUuid, IdMsgPair::getMsg));
                CountDownLatch processingTimeoutLatch = new CountDownLatch(1);
                TbPackProcessingContext<TbProtoQueueMsg<ToCoreMsg>> ctx = new TbPackProcessingContext<>(processingTimeoutLatch, pendingMap, new ConcurrentHashMap<>());
                PendingMsgHolder pendingMsgHolder = new PendingMsgHolder();
                Future<?> packSubmitFuture = consumersExecutor.submit(() -> {
                    orderedMsgList.forEach((element) -> {
                        UUID id = element.getUuid();
                        TbProtoQueueMsg<ToCoreMsg> msg = element.getMsg();
                        log.trace("[{}] Creating main callback for message: {}", id, msg.getValue());
                        TbCallback callback = new TbPackCallback<>(id, ctx);
                        try {
                            ToCoreMsg toCoreMsg = msg.getValue();
                            pendingMsgHolder.setToCoreMsg(toCoreMsg);
                            if (toCoreMsg.hasToSubscriptionMgrMsg()) {
                                log.trace("[{}] Forwarding message to subscription manager service {}", id, toCoreMsg.getToSubscriptionMgrMsg());
                                forwardToSubMgrService(toCoreMsg.getToSubscriptionMgrMsg(), callback);
                            } else if (toCoreMsg.hasToDeviceActorMsg()) {
                                log.trace("[{}] Forwarding message to device actor {}", id, toCoreMsg.getToDeviceActorMsg());
                                forwardToDeviceActor(toCoreMsg.getToDeviceActorMsg(), callback);
                            } else if (toCoreMsg.hasDeviceStateServiceMsg()) {
                                log.trace("[{}] Forwarding message to state service {}", id, toCoreMsg.getDeviceStateServiceMsg());
                                forwardToStateService(toCoreMsg.getDeviceStateServiceMsg(), callback);
                            } else if (toCoreMsg.hasEdgeNotificationMsg()) {
                                log.trace("[{}] Forwarding message to edge service {}", id, toCoreMsg.getEdgeNotificationMsg());
                                forwardToEdgeNotificationService(toCoreMsg.getEdgeNotificationMsg(), callback);
                            } else if (!toCoreMsg.getToDeviceActorNotificationMsg().isEmpty()) {
                                Optional<TbActorMsg> actorMsg = encodingService.decode(toCoreMsg.getToDeviceActorNotificationMsg().toByteArray());
                                if (actorMsg.isPresent()) {
                                    TbActorMsg tbActorMsg = actorMsg.get();
                                    if (tbActorMsg.getMsgType().equals(MsgType.DEVICE_RPC_REQUEST_TO_DEVICE_ACTOR_MSG)) {
                                        tbCoreDeviceRpcService.forwardRpcRequestToDeviceActor((ToDeviceRpcRequestActorMsg) tbActorMsg);
                                    } else {
                                        log.trace("[{}] Forwarding message to App Actor {}", id, actorMsg.get());
                                        actorContext.tell(actorMsg.get());
                                    }
                                }
                                callback.onSuccess();
                            }
                        } catch (Throwable e) {
                            log.warn("[{}] Failed to process message: {}", id, msg, e);
                            callback.onFailure(e);
                        }
                    });
                });
                if (!processingTimeoutLatch.await(packProcessingTimeout, TimeUnit.MILLISECONDS)) {
                    if (!packSubmitFuture.isDone()) {
                        packSubmitFuture.cancel(true);
                        ToCoreMsg lastSubmitMsg = pendingMsgHolder.getToCoreMsg();
                        log.info("Timeout to process message: {}", lastSubmitMsg);
                    }
                    ctx.getAckMap().forEach((id, msg) -> log.debug("[{}] Timeout to process message: {}", id, msg.getValue()));
                    ctx.getFailedMap().forEach((id, msg) -> log.warn("[{}] Failed to process message: {}", id, msg.getValue()));
                }
                mainConsumer.commit();
            } catch (Exception e) {
                if (!stopped) {
                    log.warn("Failed to obtain messages from queue.", e);
                    try {
                        Thread.sleep(pollDuration);
                    } catch (InterruptedException e2) {
                        log.trace("Failed to wait until the server has capacity to handle new requests", e2);
                    }
                }
            }
        }
        log.info("TB Core 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) IdMsgPair(org.thingsboard.server.service.queue.processing.IdMsgPair) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) CountDownLatch(java.util.concurrent.CountDownLatch) ToCoreMsg(org.thingsboard.server.gen.transport.TransportProtos.ToCoreMsg) TbCallback(org.thingsboard.server.common.msg.queue.TbCallback) UUID(java.util.UUID) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg)

Aggregations

List (java.util.List)1 Optional (java.util.Optional)1 UUID (java.util.UUID)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 EventListener (org.springframework.context.event.EventListener)1