Search in sources :

Example 1 with DebugTbRateLimits

use of org.thingsboard.server.actors.tenant.DebugTbRateLimits in project thingsboard by thingsboard.

the class RuleChainController method saveRuleChainMetaData.

@ApiOperation(value = "Update Rule Chain Metadata", notes = "Updates the rule chain metadata. " + RULE_CHAIN_METADATA_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleChain/metadata", method = RequestMethod.POST)
@ResponseBody
public RuleChainMetaData saveRuleChainMetaData(@ApiParam(value = "A JSON value representing the rule chain metadata.") @RequestBody RuleChainMetaData ruleChainMetaData, @ApiParam(value = "Update related rule nodes.") @RequestParam(value = "updateRelated", required = false, defaultValue = "true") boolean updateRelated) throws ThingsboardException {
    try {
        TenantId tenantId = getTenantId();
        if (debugPerTenantEnabled) {
            ConcurrentMap<TenantId, DebugTbRateLimits> debugPerTenantLimits = actorContext.getDebugPerTenantLimits();
            DebugTbRateLimits debugTbRateLimits = debugPerTenantLimits.getOrDefault(tenantId, null);
            if (debugTbRateLimits != null) {
                debugPerTenantLimits.remove(tenantId, debugTbRateLimits);
            }
        }
        RuleChain ruleChain = checkRuleChain(ruleChainMetaData.getRuleChainId(), Operation.WRITE);
        RuleChainUpdateResult result = ruleChainService.saveRuleChainMetaData(tenantId, ruleChainMetaData);
        checkNotNull(result.isSuccess() ? true : null);
        List<RuleChain> updatedRuleChains;
        if (updateRelated && result.isSuccess()) {
            updatedRuleChains = tbRuleChainService.updateRelatedRuleChains(tenantId, ruleChainMetaData.getRuleChainId(), result);
        } else {
            updatedRuleChains = Collections.emptyList();
        }
        RuleChainMetaData savedRuleChainMetaData = checkNotNull(ruleChainService.loadRuleChainMetaData(tenantId, ruleChainMetaData.getRuleChainId()));
        if (RuleChainType.CORE.equals(ruleChain.getType())) {
            tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), ruleChain.getId(), ComponentLifecycleEvent.UPDATED);
            updatedRuleChains.forEach(updatedRuleChain -> {
                tbClusterService.broadcastEntityStateChangeEvent(updatedRuleChain.getTenantId(), updatedRuleChain.getId(), ComponentLifecycleEvent.UPDATED);
            });
        }
        logEntityAction(ruleChain.getId(), ruleChain, null, ActionType.UPDATED, null, ruleChainMetaData);
        for (RuleChain updatedRuleChain : updatedRuleChains) {
            RuleChainMetaData updatedRuleChainMetaData = checkNotNull(ruleChainService.loadRuleChainMetaData(tenantId, updatedRuleChain.getId()));
            logEntityAction(updatedRuleChain.getId(), updatedRuleChain, null, ActionType.UPDATED, null, updatedRuleChainMetaData);
        }
        if (RuleChainType.EDGE.equals(ruleChain.getType())) {
            sendEntityNotificationMsg(ruleChain.getTenantId(), ruleChain.getId(), EdgeEventActionType.UPDATED);
            updatedRuleChains.forEach(updatedRuleChain -> {
                sendEntityNotificationMsg(updatedRuleChain.getTenantId(), updatedRuleChain.getId(), EdgeEventActionType.UPDATED);
            });
        }
        return savedRuleChainMetaData;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.RULE_CHAIN), null, null, ActionType.UPDATED, e, ruleChainMetaData);
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleChainUpdateResult(org.thingsboard.server.common.data.rule.RuleChainUpdateResult) DebugTbRateLimits(org.thingsboard.server.actors.tenant.DebugTbRateLimits) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with DebugTbRateLimits

use of org.thingsboard.server.actors.tenant.DebugTbRateLimits in project thingsboard by thingsboard.

the class ActorSystemContext method checkLimits.

private boolean checkLimits(TenantId tenantId, TbMsg tbMsg, Throwable error) {
    if (debugPerTenantEnabled) {
        DebugTbRateLimits debugTbRateLimits = debugPerTenantLimits.computeIfAbsent(tenantId, id -> new DebugTbRateLimits(new TbRateLimits(debugPerTenantLimitsConfiguration), false));
        if (!debugTbRateLimits.getTbRateLimits().tryConsume()) {
            if (!debugTbRateLimits.isRuleChainEventSaved()) {
                persistRuleChainDebugModeEvent(tenantId, tbMsg.getRuleChainId(), error);
                debugTbRateLimits.setRuleChainEventSaved(true);
            }
            if (log.isTraceEnabled()) {
                log.trace("[{}] Tenant level debug mode rate limit detected: {}", tenantId, tbMsg);
            }
            return false;
        }
    }
    return true;
}
Also used : DebugTbRateLimits(org.thingsboard.server.actors.tenant.DebugTbRateLimits) TbRateLimits(org.thingsboard.server.common.msg.tools.TbRateLimits) DebugTbRateLimits(org.thingsboard.server.actors.tenant.DebugTbRateLimits)

Aggregations

DebugTbRateLimits (org.thingsboard.server.actors.tenant.DebugTbRateLimits)2 ApiOperation (io.swagger.annotations.ApiOperation)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)1 RuleChainMetaData (org.thingsboard.server.common.data.rule.RuleChainMetaData)1 RuleChainUpdateResult (org.thingsboard.server.common.data.rule.RuleChainUpdateResult)1 TbRateLimits (org.thingsboard.server.common.msg.tools.TbRateLimits)1