Search in sources :

Example 41 with TopicPartitionInfo

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

the class DefaultAlarmSubscriptionService method onAlarmUpdated.

private void onAlarmUpdated(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().onAlarmUpdate(tenantId, entityId, alarm, TbCallback.EMPTY);
                } else {
                    log.warn("Possible misconfiguration because subscriptionManagerService is null!");
                }
            } else {
                TransportProtos.ToCoreMsg toCoreMsg = TbSubscriptionUtils.toAlarmUpdateProto(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)

Example 42 with TopicPartitionInfo

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

the class DefaultDeviceStateService method onDeviceDeleted.

private void onDeviceDeleted(TenantId tenantId, DeviceId deviceId) {
    cleanupEntity(deviceId);
    TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, deviceId);
    Set<DeviceId> deviceIdSet = partitionedEntities.get(tpi);
    if (deviceIdSet != null) {
        deviceIdSet.remove(deviceId);
    }
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) DeviceId(org.thingsboard.server.common.data.id.DeviceId)

Example 43 with TopicPartitionInfo

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

the class DefaultSubscriptionManagerService method handleNewAttributeSubscription.

private void handleNewAttributeSubscription(TbAttributeSubscription subscription) {
    log.trace("[{}][{}][{}] Processing remote attribute subscription for entity [{}]", serviceId, subscription.getSessionId(), subscription.getSubscriptionId(), subscription.getEntityId());
    final Map<String, Long> keyStates = subscription.getKeyStates();
    DonAsynchron.withCallback(attrService.find(subscription.getTenantId(), subscription.getEntityId(), DataConstants.CLIENT_SCOPE, keyStates.keySet()), values -> {
        List<TsKvEntry> missedUpdates = new ArrayList<>();
        values.forEach(latestEntry -> {
            if (latestEntry.getLastUpdateTs() > keyStates.get(latestEntry.getKey())) {
                missedUpdates.add(new BasicTsKvEntry(latestEntry.getLastUpdateTs(), latestEntry));
            }
        });
        if (!missedUpdates.isEmpty()) {
            TopicPartitionInfo tpi = partitionService.getNotificationsTopic(ServiceType.TB_CORE, subscription.getServiceId());
            toCoreNotificationsProducer.send(tpi, toProto(subscription, missedUpdates), null);
        }
    }, e -> log.error("Failed to fetch missed updates.", e), tsCallBackExecutor);
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) ArrayList(java.util.ArrayList)

Example 44 with TopicPartitionInfo

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

the class DefaultTbLocalSubscriptionService method cancelSubscription.

@Override
public void cancelSubscription(String sessionId, int subscriptionId) {
    log.debug("[{}][{}] Going to remove subscription.", sessionId, subscriptionId);
    Map<Integer, TbSubscription> sessionSubscriptions = subscriptionsBySessionId.get(sessionId);
    if (sessionSubscriptions != null) {
        TbSubscription subscription = sessionSubscriptions.remove(subscriptionId);
        if (subscription != null) {
            if (sessionSubscriptions.isEmpty()) {
                subscriptionsBySessionId.remove(sessionId);
            }
            TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, subscription.getTenantId(), subscription.getEntityId());
            if (currentPartitions.contains(tpi)) {
                // Subscription is managed on the same server;
                subscriptionManagerService.cancelSubscription(sessionId, subscriptionId, TbCallback.EMPTY);
            } else {
                // Push to the queue;
                TransportProtos.ToCoreMsg toCoreMsg = TbSubscriptionUtils.toCloseSubscriptionProto(subscription);
                clusterService.pushMsgToCore(tpi, subscription.getEntityId().getId(), toCoreMsg, null);
            }
        } else {
            log.debug("[{}][{}] Subscription not found!", sessionId, subscriptionId);
        }
    } else {
        log.debug("[{}] No session subscriptions found!", sessionId);
    }
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos)

Example 45 with TopicPartitionInfo

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

the class HashPartitionService method recalculatePartitions.

@Override
public synchronized void recalculatePartitions(ServiceInfo currentService, List<ServiceInfo> otherServices) {
    tbTransportServicesByType.clear();
    logServiceInfo(currentService);
    otherServices.forEach(this::logServiceInfo);
    Map<ServiceQueueKey, List<ServiceInfo>> queueServicesMap = new HashMap<>();
    addNode(queueServicesMap, currentService);
    for (ServiceInfo other : otherServices) {
        addNode(queueServicesMap, other);
    }
    queueServicesMap.values().forEach(list -> list.sort(Comparator.comparing(ServiceInfo::getServiceId)));
    ConcurrentMap<ServiceQueueKey, List<Integer>> oldPartitions = myPartitions;
    TenantId myIsolatedOrSystemTenantId = getSystemOrIsolatedTenantId(currentService);
    myPartitions = new ConcurrentHashMap<>();
    partitionSizes.forEach((serviceQueue, size) -> {
        ServiceQueueKey myServiceQueueKey = new ServiceQueueKey(serviceQueue, myIsolatedOrSystemTenantId);
        for (int i = 0; i < size; i++) {
            ServiceInfo serviceInfo = resolveByPartitionIdx(queueServicesMap.get(myServiceQueueKey), i);
            if (currentService.equals(serviceInfo)) {
                ServiceQueueKey serviceQueueKey = new ServiceQueueKey(serviceQueue, getSystemOrIsolatedTenantId(serviceInfo));
                myPartitions.computeIfAbsent(serviceQueueKey, key -> new ArrayList<>()).add(i);
            }
        }
    });
    tpiCache.clear();
    oldPartitions.forEach((serviceQueueKey, partitions) -> {
        if (!myPartitions.containsKey(serviceQueueKey)) {
            log.info("[{}] NO MORE PARTITIONS FOR CURRENT KEY", serviceQueueKey);
            applicationEventPublisher.publishEvent(new PartitionChangeEvent(this, serviceQueueKey, Collections.emptySet()));
        }
    });
    myPartitions.forEach((serviceQueueKey, partitions) -> {
        if (!partitions.equals(oldPartitions.get(serviceQueueKey))) {
            log.info("[{}] NEW PARTITIONS: {}", serviceQueueKey, partitions);
            Set<TopicPartitionInfo> tpiList = partitions.stream().map(partition -> buildTopicPartitionInfo(serviceQueueKey, partition)).collect(Collectors.toSet());
            applicationEventPublisher.publishEvent(new PartitionChangeEvent(this, serviceQueueKey, tpiList));
        }
    });
    if (currentOtherServices == null) {
        currentOtherServices = new ArrayList<>(otherServices);
    } else {
        Set<ServiceQueueKey> changes = new HashSet<>();
        Map<ServiceQueueKey, List<ServiceInfo>> currentMap = getServiceKeyListMap(currentOtherServices);
        Map<ServiceQueueKey, List<ServiceInfo>> newMap = getServiceKeyListMap(otherServices);
        currentOtherServices = otherServices;
        currentMap.forEach((key, list) -> {
            if (!list.equals(newMap.get(key))) {
                changes.add(key);
            }
        });
        currentMap.keySet().forEach(newMap::remove);
        changes.addAll(newMap.keySet());
        if (!changes.isEmpty()) {
            applicationEventPublisher.publishEvent(new ClusterTopologyChangeEvent(this, changes));
        }
    }
    applicationEventPublisher.publishEvent(new ServiceListChangedEvent(otherServices, currentService));
}
Also used : ClusterTopologyChangeEvent(org.thingsboard.server.queue.discovery.event.ClusterTopologyChangeEvent) Hashing(com.google.common.hash.Hashing) HashMap(java.util.HashMap) QueueService(org.thingsboard.server.queue.QueueService) TenantId(org.thingsboard.server.common.data.id.TenantId) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Value(org.springframework.beans.factory.annotation.Value) HashSet(java.util.HashSet) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) Service(org.springframework.stereotype.Service) ServiceQueueKey(org.thingsboard.server.common.msg.queue.ServiceQueueKey) Map(java.util.Map) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) EntityId(org.thingsboard.server.common.data.id.EntityId) Hasher(com.google.common.hash.Hasher) TbQueueRuleEngineSettings(org.thingsboard.server.queue.settings.TbQueueRuleEngineSettings) ServiceInfo(org.thingsboard.server.gen.transport.TransportProtos.ServiceInfo) ServiceListChangedEvent(org.thingsboard.server.queue.discovery.event.ServiceListChangedEvent) PartitionChangeEvent(org.thingsboard.server.queue.discovery.event.PartitionChangeEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) ServiceQueue(org.thingsboard.server.common.msg.queue.ServiceQueue) List(java.util.List) PostConstruct(javax.annotation.PostConstruct) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) HashFunction(com.google.common.hash.HashFunction) Comparator(java.util.Comparator) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClusterTopologyChangeEvent(org.thingsboard.server.queue.discovery.event.ClusterTopologyChangeEvent) ServiceQueueKey(org.thingsboard.server.common.msg.queue.ServiceQueueKey) ArrayList(java.util.ArrayList) PartitionChangeEvent(org.thingsboard.server.queue.discovery.event.PartitionChangeEvent) ServiceInfo(org.thingsboard.server.gen.transport.TransportProtos.ServiceInfo) TenantId(org.thingsboard.server.common.data.id.TenantId) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) ArrayList(java.util.ArrayList) List(java.util.List) ServiceListChangedEvent(org.thingsboard.server.queue.discovery.event.ServiceListChangedEvent) HashSet(java.util.HashSet)

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