Search in sources :

Example 1 with ParamFlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule in project Sentinel by alibaba.

the class GatewayRuleConverter method applyToParamRule.

/**
 * Convert a gateway rule to parameter flow rule, then apply the generated
 * parameter index to {@link GatewayParamFlowItem} of the rule.
 *
 * @param gatewayRule a valid gateway rule that should contain valid parameter items
 * @param idx generated parameter index (callers should guarantee it's unique and incremental)
 * @return converted parameter flow rule
 */
static ParamFlowRule applyToParamRule(/*@Valid*/
GatewayFlowRule gatewayRule, int idx) {
    ParamFlowRule paramRule = new ParamFlowRule(gatewayRule.getResource()).setCount(gatewayRule.getCount()).setGrade(gatewayRule.getGrade()).setDurationInSec(gatewayRule.getIntervalSec()).setBurstCount(gatewayRule.getBurst()).setControlBehavior(gatewayRule.getControlBehavior()).setMaxQueueingTimeMs(gatewayRule.getMaxQueueingTimeoutMs()).setParamIdx(idx);
    GatewayParamFlowItem gatewayItem = gatewayRule.getParamItem();
    // Apply the current idx to gateway rule item.
    gatewayItem.setIndex(idx);
    // Apply for pattern-based parameters.
    String valuePattern = gatewayItem.getPattern();
    if (valuePattern != null) {
        paramRule.getParamFlowItemList().add(generateNonMatchPassParamItem());
    }
    return paramRule;
}
Also used : ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)

Example 2 with ParamFlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule in project Sentinel by alibaba.

the class GatewayRuleConverterTest method testConvertAndApplyToParamRule.

@Test
public void testConvertAndApplyToParamRule() {
    GatewayFlowRule routeRule1 = new GatewayFlowRule("routeId1").setCount(2).setIntervalSec(2).setBurst(2).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP));
    int idx = 1;
    ParamFlowRule paramRule = GatewayRuleConverter.applyToParamRule(routeRule1, idx);
    assertEquals(routeRule1.getResource(), paramRule.getResource());
    assertEquals(routeRule1.getCount(), paramRule.getCount(), 0.01);
    assertEquals(routeRule1.getControlBehavior(), paramRule.getControlBehavior());
    assertEquals(routeRule1.getIntervalSec(), paramRule.getDurationInSec());
    assertEquals(routeRule1.getBurst(), paramRule.getBurstCount());
    assertEquals(idx, (int) paramRule.getParamIdx());
    assertEquals(idx, (int) routeRule1.getParamItem().getIndex());
}
Also used : ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) Test(org.junit.Test)

Example 3 with ParamFlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule in project Sentinel by alibaba.

the class ClusterParamFlowRuleManager method getParamRules.

/**
 * Get all cluster parameter flow rules within a specific namespace.
 *
 * @param namespace a valid namespace
 * @return cluster parameter flow rules within the provided namespace
 */
public static List<ParamFlowRule> getParamRules(String namespace) {
    if (StringUtil.isEmpty(namespace)) {
        return new ArrayList<>();
    }
    List<ParamFlowRule> rules = new ArrayList<>();
    Set<Long> flowIdSet = NAMESPACE_FLOW_ID_MAP.get(namespace);
    if (flowIdSet == null || flowIdSet.isEmpty()) {
        return rules;
    }
    for (Long flowId : flowIdSet) {
        ParamFlowRule rule = PARAM_RULES.get(flowId);
        if (rule != null) {
            rules.add(rule);
        }
    }
    return rules;
}
Also used : ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) ArrayList(java.util.ArrayList)

Example 4 with ParamFlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule 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 5 with ParamFlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule 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)

Aggregations

ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)11 ParamFlowClusterConfig (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig)3 ParamFlowItem (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 ClusterParamMetric (com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterParamMetric)2 ParamFlowRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity)2 AuthorityRule (com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule)2 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)2 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)2 SystemRule (com.alibaba.csp.sentinel.slots.system.SystemRule)2 ParamFlowException (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 PostConstruct (javax.annotation.PostConstruct)1