Search in sources :

Example 6 with TopicPartitionInfo

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

the class DefaultTbLocalSubscriptionService method pushSubscriptionToManagerService.

private void pushSubscriptionToManagerService(TbSubscription subscription, boolean pushToLocalService) {
    TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, subscription.getTenantId(), subscription.getEntityId());
    if (currentPartitions.contains(tpi)) {
        // Subscription is managed on the same server;
        if (pushToLocalService) {
            subscriptionManagerService.addSubscription(subscription, TbCallback.EMPTY);
        }
    } else {
        // Push to the queue;
        TransportProtos.ToCoreMsg toCoreMsg = TbSubscriptionUtils.toNewSubscriptionProto(subscription);
        clusterService.pushMsgToCore(tpi, subscription.getEntityId().getId(), toCoreMsg, null);
    }
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos)

Example 7 with TopicPartitionInfo

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

the class DefaultDeviceStateService method onQueueMsg.

@Override
public void onQueueMsg(TransportProtos.DeviceStateServiceMsgProto proto, TbCallback callback) {
    try {
        TenantId tenantId = TenantId.fromUUID(new UUID(proto.getTenantIdMSB(), proto.getTenantIdLSB()));
        DeviceId deviceId = new DeviceId(new UUID(proto.getDeviceIdMSB(), proto.getDeviceIdLSB()));
        if (proto.getDeleted()) {
            onDeviceDeleted(tenantId, deviceId);
            callback.onSuccess();
        } else {
            Device device = deviceService.findDeviceById(TenantId.SYS_TENANT_ID, deviceId);
            if (device != null) {
                if (proto.getAdded()) {
                    Futures.addCallback(fetchDeviceState(device), new FutureCallback<>() {

                        @Override
                        public void onSuccess(@Nullable DeviceStateData state) {
                            TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, device.getId());
                            if (partitionedEntities.containsKey(tpi)) {
                                addDeviceUsingState(tpi, state);
                                save(deviceId, ACTIVITY_STATE, false);
                                callback.onSuccess();
                            } else {
                                log.debug("[{}][{}] Device belongs to external partition. Probably rebalancing is in progress. Topic: {}", tenantId, deviceId, tpi.getFullTopicName());
                                callback.onFailure(new RuntimeException("Device belongs to external partition " + tpi.getFullTopicName() + "!"));
                            }
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            log.warn("Failed to register device to the state service", t);
                            callback.onFailure(t);
                        }
                    }, deviceStateExecutor);
                } else if (proto.getUpdated()) {
                    DeviceStateData stateData = getOrFetchDeviceStateData(device.getId());
                    TbMsgMetaData md = new TbMsgMetaData();
                    md.putValue("deviceName", device.getName());
                    md.putValue("deviceType", device.getType());
                    stateData.setMetaData(md);
                    callback.onSuccess();
                }
            } else {
                // Device was probably deleted while message was in queue;
                callback.onSuccess();
            }
        }
    } catch (Exception e) {
        log.trace("Failed to process queue msg: [{}]", proto, e);
        callback.onFailure(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) UUID(java.util.UUID) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with TopicPartitionInfo

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

the class DefaultDeviceStateService method cleanDeviceStateIfBelongsExternalPartition.

private boolean cleanDeviceStateIfBelongsExternalPartition(TenantId tenantId, final DeviceId deviceId) {
    TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, deviceId);
    boolean cleanup = !partitionedEntities.containsKey(tpi);
    if (cleanup) {
        cleanupEntity(deviceId);
        log.debug("[{}][{}] device belongs to external partition. Probably rebalancing is in progress. Topic: {}", tenantId, deviceId, tpi.getFullTopicName());
    }
    return cleanup;
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo)

Example 9 with TopicPartitionInfo

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

the class DefaultDeviceStateService method processPageAndSubmitNextPage.

private void processPageAndSubmitNextPage(final Set<TopicPartitionInfo> addedPartitions, final Tenant tenant, final PageLink pageLink) {
    log.trace("[{}] Process page {} from {}", tenant, pageLink.getPage(), pageLink.getPageSize());
    List<ListenableFuture<Void>> fetchFutures = new ArrayList<>();
    PageData<Device> page = deviceService.findDevicesByTenantId(tenant.getId(), pageLink);
    for (Device device : page.getData()) {
        TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenant.getId(), device.getId());
        if (addedPartitions.contains(tpi) && !deviceStates.containsKey(device.getId())) {
            log.debug("[{}][{}] Device belong to current partition. tpi [{}]. Fetching state from DB", device.getName(), device.getId(), tpi);
            ListenableFuture<Void> future = Futures.transform(fetchDeviceState(device), new Function<>() {

                @Nullable
                @Override
                public Void apply(@Nullable DeviceStateData state) {
                    if (state != null) {
                        addDeviceUsingState(tpi, state);
                        checkAndUpdateState(device.getId(), state);
                    } else {
                        log.warn("{}][{}] Fetched null state from DB", device.getName(), device.getId());
                    }
                    return null;
                }
            }, deviceStateExecutor);
            fetchFutures.add(future);
        } else {
            log.debug("[{}][{}] Device doesn't belong to current partition. tpi [{}]", device.getName(), device.getId(), tpi);
        }
    }
    Futures.addCallback(Futures.successfulAsList(fetchFutures), new FutureCallback<>() {

        @Override
        public void onSuccess(List<Void> result) {
            log.trace("[{}] Success init device state from DB for batch size {}", tenant.getId(), result.size());
        }

        @Override
        public void onFailure(Throwable t) {
            log.warn("[" + tenant.getId() + "] Failed to init device state service from DB", t);
            log.warn("[{}] Failed to init device state service from DB", tenant.getId(), t);
        }
    }, deviceStateExecutor);
    final PageLink nextPageLink = page.hasNext() ? pageLink.nextPageLink() : null;
    if (nextPageLink != null) {
        log.trace("[{}] Submit next page {} from {}", tenant, nextPageLink.getPage(), nextPageLink.getPageSize());
        processPageAndSubmitNextPage(addedPartitions, tenant, nextPageLink);
    }
}
Also used : Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) PageLink(org.thingsboard.server.common.data.page.PageLink) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Nullable(javax.annotation.Nullable)

Example 10 with TopicPartitionInfo

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

the class DefaultAlarmSubscriptionService method onAlarmDeleted.

private void onAlarmDeleted(AlarmOperationResult result) {
    wsCallBackExecutor.submit(() -> {
        Alarm alarm = result.getAlarm();
        TenantId tenantId = result.getAlarm().getTenantId();
        for (EntityId entityId : result.getPropagatedEntitiesList()) {
            TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, entityId);
            if (currentPartitions.contains(tpi)) {
                if (subscriptionManagerService.isPresent()) {
                    subscriptionManagerService.get().onAlarmDeleted(tenantId, entityId, alarm, TbCallback.EMPTY);
                } else {
                    log.warn("Possible misconfiguration because subscriptionManagerService is null!");
                }
            } else {
                TransportProtos.ToCoreMsg toCoreMsg = TbSubscriptionUtils.toAlarmDeletedProto(tenantId, entityId, alarm);
                clusterService.pushMsgToCore(tpi, entityId.getId(), toCoreMsg, null);
            }
        }
    });
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) TenantId(org.thingsboard.server.common.data.id.TenantId) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) Alarm(org.thingsboard.server.common.data.alarm.Alarm) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos)

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