Search in sources :

Example 26 with TopicPartitionInfo

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

the class DefaultTbApiUsageStateService method getOrFetchState.

BaseApiUsageState getOrFetchState(TenantId tenantId, EntityId entityId) {
    if (entityId == null || entityId.isNullUid()) {
        entityId = tenantId;
    }
    BaseApiUsageState state = myUsageStates.get(entityId);
    if (state != null) {
        return state;
    }
    ApiUsageState storedState = apiUsageStateService.findApiUsageStateByEntityId(entityId);
    if (storedState == null) {
        try {
            storedState = apiUsageStateService.createDefaultApiUsageState(tenantId, entityId);
        } catch (Exception e) {
            storedState = apiUsageStateService.findApiUsageStateByEntityId(entityId);
        }
    }
    if (entityId.getEntityType() == EntityType.TENANT) {
        if (!entityId.equals(TenantId.SYS_TENANT_ID)) {
            state = new TenantApiUsageState(tenantProfileCache.get((TenantId) entityId), storedState);
        } else {
            state = new TenantApiUsageState(storedState);
        }
    } else {
        state = new CustomerApiUsageState(storedState);
    }
    List<ApiUsageRecordKey> newCounts = new ArrayList<>();
    try {
        List<TsKvEntry> dbValues = tsService.findAllLatest(tenantId, storedState.getId()).get();
        for (ApiUsageRecordKey key : ApiUsageRecordKey.values()) {
            boolean cycleEntryFound = false;
            boolean hourlyEntryFound = false;
            for (TsKvEntry tsKvEntry : dbValues) {
                if (tsKvEntry.getKey().equals(key.getApiCountKey())) {
                    cycleEntryFound = true;
                    boolean oldCount = tsKvEntry.getTs() == state.getCurrentCycleTs();
                    state.put(key, oldCount ? tsKvEntry.getLongValue().get() : 0L);
                    if (!oldCount) {
                        newCounts.add(key);
                    }
                } else if (tsKvEntry.getKey().equals(key.getApiCountKey() + HOURLY)) {
                    hourlyEntryFound = true;
                    state.putHourly(key, tsKvEntry.getTs() == state.getCurrentHourTs() ? tsKvEntry.getLongValue().get() : 0L);
                }
                if (cycleEntryFound && hourlyEntryFound) {
                    break;
                }
            }
        }
        log.debug("[{}] Initialized state: {}", entityId, storedState);
        TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, entityId);
        if (tpi.isMyPartition()) {
            addEntityState(tpi, state);
        } else {
            otherUsageStates.put(entityId, state.getApiUsageState());
        }
        saveNewCounts(state, newCounts);
    } catch (InterruptedException | ExecutionException e) {
        log.warn("[{}] Failed to fetch api usage state from db.", tenantId, e);
    }
    return state;
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) ArrayList(java.util.ArrayList) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ExecutionException(java.util.concurrent.ExecutionException) ApiUsageRecordKey(org.thingsboard.server.common.data.ApiUsageRecordKey) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) ExecutionException(java.util.concurrent.ExecutionException)

Example 27 with TopicPartitionInfo

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

the class DefaultTbContext method enqueueForTellNext.

@Override
public void enqueueForTellNext(TbMsg tbMsg, String relationType) {
    TopicPartitionInfo tpi = resolvePartition(tbMsg);
    enqueueForTellNext(tpi, tbMsg, Collections.singleton(relationType), null, null, null);
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo)

Example 28 with TopicPartitionInfo

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

the class DefaultTbContext method enqueue.

@Override
public void enqueue(TbMsg tbMsg, String queueName, Runnable onSuccess, Consumer<Throwable> onFailure) {
    TopicPartitionInfo tpi = resolvePartition(tbMsg, queueName);
    enqueue(tpi, tbMsg, onFailure, onSuccess);
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo)

Example 29 with TopicPartitionInfo

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

the class DefaultTbContext method enqueueForTellNext.

@Override
public void enqueueForTellNext(TbMsg tbMsg, String queueName, String relationType, Runnable onSuccess, Consumer<Throwable> onFailure) {
    TopicPartitionInfo tpi = resolvePartition(tbMsg, queueName);
    enqueueForTellNext(tpi, queueName, tbMsg, Collections.singleton(relationType), null, onSuccess, onFailure);
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo)

Example 30 with TopicPartitionInfo

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

the class DefaultTbContext method enqueueForTellNext.

@Override
public void enqueueForTellNext(TbMsg tbMsg, Set<String> relationTypes, Runnable onSuccess, Consumer<Throwable> onFailure) {
    TopicPartitionInfo tpi = resolvePartition(tbMsg);
    enqueueForTellNext(tpi, tbMsg, relationTypes, null, onSuccess, onFailure);
}
Also used : TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo)

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