|
|
@ -17,6 +17,7 @@ package org.thingsboard.server.service.stats; |
|
|
|
|
|
|
|
|
import com.google.common.util.concurrent.FutureCallback; |
|
|
import com.google.common.util.concurrent.FutureCallback; |
|
|
import lombok.Data; |
|
|
import lombok.Data; |
|
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.thingsboard.server.common.data.asset.Asset; |
|
|
import org.thingsboard.server.common.data.asset.Asset; |
|
|
@ -26,7 +27,9 @@ import org.thingsboard.server.common.data.kv.BasicTsKvEntry; |
|
|
import org.thingsboard.server.common.data.kv.JsonDataEntry; |
|
|
import org.thingsboard.server.common.data.kv.JsonDataEntry; |
|
|
import org.thingsboard.server.common.data.kv.LongDataEntry; |
|
|
import org.thingsboard.server.common.data.kv.LongDataEntry; |
|
|
import org.thingsboard.server.common.data.kv.TsKvEntry; |
|
|
import org.thingsboard.server.common.data.kv.TsKvEntry; |
|
|
|
|
|
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; |
|
|
import org.thingsboard.server.dao.asset.AssetService; |
|
|
import org.thingsboard.server.dao.asset.AssetService; |
|
|
|
|
|
import org.thingsboard.server.dao.usagerecord.ApiLimitService; |
|
|
import org.thingsboard.server.queue.discovery.TbServiceInfoProvider; |
|
|
import org.thingsboard.server.queue.discovery.TbServiceInfoProvider; |
|
|
import org.thingsboard.server.queue.util.TbRuleEngineComponent; |
|
|
import org.thingsboard.server.queue.util.TbRuleEngineComponent; |
|
|
import org.thingsboard.server.service.queue.TbRuleEngineConsumerStats; |
|
|
import org.thingsboard.server.service.queue.TbRuleEngineConsumerStats; |
|
|
@ -37,6 +40,7 @@ import java.util.Collections; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
import java.util.concurrent.ConcurrentMap; |
|
|
import java.util.concurrent.ConcurrentMap; |
|
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.concurrent.locks.Lock; |
|
|
import java.util.concurrent.locks.Lock; |
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
import java.util.stream.Collectors; |
|
|
import java.util.stream.Collectors; |
|
|
@ -44,9 +48,11 @@ import java.util.stream.Collectors; |
|
|
@TbRuleEngineComponent |
|
|
@TbRuleEngineComponent |
|
|
@Service |
|
|
@Service |
|
|
@Slf4j |
|
|
@Slf4j |
|
|
|
|
|
@RequiredArgsConstructor |
|
|
public class DefaultRuleEngineStatisticsService implements RuleEngineStatisticsService { |
|
|
public class DefaultRuleEngineStatisticsService implements RuleEngineStatisticsService { |
|
|
|
|
|
|
|
|
public static final String TB_SERVICE_QUEUE = "TbServiceQueue"; |
|
|
public static final String TB_SERVICE_QUEUE = "TbServiceQueue"; |
|
|
|
|
|
public static final String RULE_ENGINE_EXCEPTION = "ruleEngineException"; |
|
|
public static final FutureCallback<Integer> CALLBACK = new FutureCallback<Integer>() { |
|
|
public static final FutureCallback<Integer> CALLBACK = new FutureCallback<Integer>() { |
|
|
@Override |
|
|
@Override |
|
|
public void onSuccess(@Nullable Integer result) { |
|
|
public void onSuccess(@Nullable Integer result) { |
|
|
@ -61,16 +67,10 @@ public class DefaultRuleEngineStatisticsService implements RuleEngineStatisticsS |
|
|
|
|
|
|
|
|
private final TbServiceInfoProvider serviceInfoProvider; |
|
|
private final TbServiceInfoProvider serviceInfoProvider; |
|
|
private final TelemetrySubscriptionService tsService; |
|
|
private final TelemetrySubscriptionService tsService; |
|
|
private final Lock lock = new ReentrantLock(); |
|
|
|
|
|
private final AssetService assetService; |
|
|
private final AssetService assetService; |
|
|
private final ConcurrentMap<TenantQueueKey, AssetId> tenantQueueAssets; |
|
|
private final ApiLimitService apiLimitService; |
|
|
|
|
|
private final Lock lock = new ReentrantLock(); |
|
|
public DefaultRuleEngineStatisticsService(TelemetrySubscriptionService tsService, TbServiceInfoProvider serviceInfoProvider, AssetService assetService) { |
|
|
private final ConcurrentMap<TenantQueueKey, AssetId> tenantQueueAssets = new ConcurrentHashMap<>(); |
|
|
this.tsService = tsService; |
|
|
|
|
|
this.serviceInfoProvider = serviceInfoProvider; |
|
|
|
|
|
this.assetService = assetService; |
|
|
|
|
|
this.tenantQueueAssets = new ConcurrentHashMap<>(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void reportQueueStats(long ts, TbRuleEngineConsumerStats ruleEngineStats) { |
|
|
public void reportQueueStats(long ts, TbRuleEngineConsumerStats ruleEngineStats) { |
|
|
@ -84,7 +84,9 @@ public class DefaultRuleEngineStatisticsService implements RuleEngineStatisticsS |
|
|
.map(kv -> new BasicTsKvEntry(ts, new LongDataEntry(kv.getKey(), (long) kv.getValue().get()))) |
|
|
.map(kv -> new BasicTsKvEntry(ts, new LongDataEntry(kv.getKey(), (long) kv.getValue().get()))) |
|
|
.collect(Collectors.toList()); |
|
|
.collect(Collectors.toList()); |
|
|
if (!tsList.isEmpty()) { |
|
|
if (!tsList.isEmpty()) { |
|
|
tsService.saveAndNotifyInternal(tenantId, serviceAssetId, tsList, CALLBACK); |
|
|
long ttl = apiLimitService.getLimit(tenantId, DefaultTenantProfileConfiguration::getQueueStatsTtlDays); |
|
|
|
|
|
ttl = TimeUnit.DAYS.toSeconds(ttl); |
|
|
|
|
|
tsService.saveAndNotifyInternal(tenantId, serviceAssetId, tsList, ttl, CALLBACK); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
@ -95,8 +97,10 @@ public class DefaultRuleEngineStatisticsService implements RuleEngineStatisticsS |
|
|
}); |
|
|
}); |
|
|
ruleEngineStats.getTenantExceptions().forEach((tenantId, e) -> { |
|
|
ruleEngineStats.getTenantExceptions().forEach((tenantId, e) -> { |
|
|
try { |
|
|
try { |
|
|
TsKvEntry tsKv = new BasicTsKvEntry(e.getTs(), new JsonDataEntry("ruleEngineException", e.toJsonString())); |
|
|
TsKvEntry tsKv = new BasicTsKvEntry(e.getTs(), new JsonDataEntry(RULE_ENGINE_EXCEPTION, e.toJsonString())); |
|
|
tsService.saveAndNotifyInternal(tenantId, getServiceAssetId(tenantId, queueName), Collections.singletonList(tsKv), CALLBACK); |
|
|
long ttl = apiLimitService.getLimit(tenantId, DefaultTenantProfileConfiguration::getRuleEngineExceptionsTtlDays); |
|
|
|
|
|
ttl = TimeUnit.DAYS.toSeconds(ttl); |
|
|
|
|
|
tsService.saveAndNotifyInternal(tenantId, getServiceAssetId(tenantId, queueName), Collections.singletonList(tsKv), ttl, CALLBACK); |
|
|
} catch (Exception e2) { |
|
|
} catch (Exception e2) { |
|
|
if (!"Asset is referencing to non-existent tenant!".equalsIgnoreCase(e2.getMessage())) { |
|
|
if (!"Asset is referencing to non-existent tenant!".equalsIgnoreCase(e2.getMessage())) { |
|
|
log.debug("[{}] Failed to store the statistics", tenantId, e2); |
|
|
log.debug("[{}] Failed to store the statistics", tenantId, e2); |
|
|
|