Search in sources :

Example 1 with ClusterParamMetric

use of com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterParamMetric in project Sentinel by alibaba.

the class ClusterParamFlowRuleManager method applyClusterParamRules.

private static void applyClusterParamRules(List<ParamFlowRule> list, /*@Valid*/
String namespace) {
    if (list == null || list.isEmpty()) {
        clearAndResetRulesFor(namespace);
        return;
    }
    final ConcurrentHashMap<Long, ParamFlowRule> ruleMap = new ConcurrentHashMap<>();
    Set<Long> flowIdSet = new HashSet<>();
    for (ParamFlowRule rule : list) {
        if (!rule.isClusterMode()) {
            continue;
        }
        if (!ParamFlowRuleUtil.isValidRule(rule)) {
            RecordLog.warn("[ClusterParamFlowRuleManager] Ignoring invalid param flow rule when loading new flow rules: " + rule);
            continue;
        }
        if (StringUtil.isBlank(rule.getLimitApp())) {
            rule.setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);
        }
        ParamFlowRuleUtil.fillExceptionFlowItems(rule);
        ParamFlowClusterConfig clusterConfig = rule.getClusterConfig();
        // Flow id should not be null after filtered.
        Long flowId = clusterConfig.getFlowId();
        if (flowId == null) {
            continue;
        }
        ruleMap.put(flowId, rule);
        FLOW_NAMESPACE_MAP.put(flowId, namespace);
        flowIdSet.add(flowId);
        // Prepare cluster parameter metric from valid rule ID.
        ClusterParamMetricStatistics.putMetricIfAbsent(flowId, new ClusterParamMetric(clusterConfig.getSampleCount(), clusterConfig.getWindowIntervalMs()));
    }
    // Cleanup unused cluster parameter metrics.
    clearAndResetRulesConditional(namespace, new Predicate<Long>() {

        @Override
        public boolean test(Long flowId) {
            return !ruleMap.containsKey(flowId);
        }
    });
    PARAM_RULES.putAll(ruleMap);
    NAMESPACE_FLOW_ID_MAP.put(namespace, flowIdSet);
}
Also used : ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) ParamFlowClusterConfig(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig) ClusterParamMetric(com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterParamMetric) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashSet(java.util.HashSet)

Example 2 with ClusterParamMetric

use of com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterParamMetric in project Sentinel by alibaba.

the class ClusterMetricNodeGenerator method paramToMetricNode.

public static ClusterMetricNode paramToMetricNode(long flowId) {
    ParamFlowRule rule = ClusterParamFlowRuleManager.getParamRuleById(flowId);
    if (rule == null) {
        return null;
    }
    ClusterParamMetric metric = ClusterParamMetricStatistics.getMetric(flowId);
    if (metric == null) {
        return new ClusterMetricNode().setFlowId(flowId).setResourceName(rule.getResource()).setTimestamp(TimeUtil.currentTimeMillis()).setTopParams(new HashMap<Object, Double>(0));
    }
    return new ClusterMetricNode().setFlowId(flowId).setResourceName(rule.getResource()).setTimestamp(TimeUtil.currentTimeMillis()).setTopParams(metric.getTopValues(5));
}
Also used : ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) ClusterParamMetric(com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterParamMetric)

Example 3 with ClusterParamMetric

use of com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterParamMetric in project Sentinel by alibaba.

the class ClusterParamFlowChecker method acquireClusterToken.

static TokenResult acquireClusterToken(ParamFlowRule rule, int count, Collection<Object> values) {
    Long id = rule.getClusterConfig().getFlowId();
    if (!allowProceed(id)) {
        return new TokenResult(TokenResultStatus.TOO_MANY_REQUEST);
    }
    ClusterParamMetric metric = ClusterParamMetricStatistics.getMetric(id);
    if (metric == null) {
        // Unexpected state, return FAIL.
        return new TokenResult(TokenResultStatus.FAIL);
    }
    if (values == null || values.isEmpty()) {
        // Empty parameter list will always pass.
        return new TokenResult(TokenResultStatus.OK);
    }
    double remaining = -1;
    boolean hasPassed = true;
    Object blockObject = null;
    for (Object value : values) {
        double latestQps = metric.getAvg(value);
        double threshold = calcGlobalThreshold(rule, value);
        double nextRemaining = threshold - latestQps - count;
        remaining = nextRemaining;
        if (nextRemaining < 0) {
            hasPassed = false;
            blockObject = value;
            break;
        }
    }
    if (hasPassed) {
        for (Object value : values) {
            metric.addValue(value, count);
        }
        ClusterServerStatLogUtil.log(String.format("param|pass|%d", id));
    } else {
        ClusterServerStatLogUtil.log(String.format("param|block|%d|%s", id, blockObject));
    }
    if (values.size() > 1) {
        // Remaining field is unsupported for multi-values.
        remaining = -1;
    }
    return hasPassed ? newPassResponse((int) remaining) : newBlockResponse();
}
Also used : TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) ClusterParamMetric(com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterParamMetric)

Aggregations

ClusterParamMetric (com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterParamMetric)3 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)2 TokenResult (com.alibaba.csp.sentinel.cluster.TokenResult)1 ParamFlowClusterConfig (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1