Search in sources :

Example 1 with ParamFlowClusterConfig

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

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig in project pig by pig-mesh.

the class InMemParamFlowRuleStore method preProcess.

@Override
protected ParamFlowRuleEntity preProcess(ParamFlowRuleEntity entity) {
    if (entity != null && entity.isClusterMode()) {
        ParamFlowClusterConfig config = entity.getClusterConfig();
        if (config == null) {
            config = new ParamFlowClusterConfig();
        }
        // Set cluster rule id.
        config.setFlowId(entity.getId());
    }
    return entity;
}
Also used : ParamFlowClusterConfig(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig)

Example 3 with ParamFlowClusterConfig

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

the class JsonSerializeTest method paramFlowRuleSerializeTest.

@Test
public void paramFlowRuleSerializeTest() {
    ParamFlowRuleEntity emptyRule = new ParamFlowRuleEntity();
    Assert.assertTrue("{}".equals(JSON.toJSONString(emptyRule)));
    ParamFlowRuleEntity paramFlowRule = new ParamFlowRuleEntity();
    ParamFlowRule rule = new ParamFlowRule();
    rule.setClusterConfig(new ParamFlowClusterConfig());
    rule.setResource("rs").setLimitApp("default");
    paramFlowRule.setRule(rule);
    Assert.assertTrue("{\"rule\":{\"burstCount\":0,\"clusterConfig\":{\"fallbackToLocalWhenFail\":false,\"sampleCount\":10,\"thresholdType\":0,\"windowIntervalMs\":1000},\"clusterMode\":false,\"controlBehavior\":0,\"count\":0.0,\"durationInSec\":1,\"grade\":1,\"limitApp\":\"default\",\"maxQueueingTimeMs\":0,\"paramFlowItemList\":[],\"resource\":\"rs\"}}".equals(JSON.toJSONString(paramFlowRule)));
}
Also used : ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) ParamFlowClusterConfig(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig) ParamFlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity) Test(org.junit.Test)

Example 4 with ParamFlowClusterConfig

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

the class InMemParamFlowRuleStore method preProcess.

@Override
protected ParamFlowRuleEntity preProcess(ParamFlowRuleEntity entity) {
    if (entity != null && entity.isClusterMode()) {
        ParamFlowClusterConfig config = entity.getClusterConfig();
        if (config == null) {
            config = new ParamFlowClusterConfig();
        }
        // Set cluster rule id.
        config.setFlowId(entity.getId());
    }
    return entity;
}
Also used : ParamFlowClusterConfig(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig)

Example 5 with ParamFlowClusterConfig

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig in project spring-boot-student by wyh-spring-ecosystem-student.

the class JsonSerializeTest method paramFlowRuleSerializeTest.

@Test
public void paramFlowRuleSerializeTest() {
    ParamFlowRuleEntity emptyRule = new ParamFlowRuleEntity();
    Assert.assertTrue("{}".equals(JSON.toJSONString(emptyRule)));
    ParamFlowRuleEntity paramFlowRule = new ParamFlowRuleEntity();
    ParamFlowRule rule = new ParamFlowRule();
    rule.setClusterConfig(new ParamFlowClusterConfig());
    rule.setResource("rs").setLimitApp("default");
    paramFlowRule.setRule(rule);
    Assert.assertTrue("{\"rule\":{\"burstCount\":0,\"clusterConfig\":{\"fallbackToLocalWhenFail\":false,\"sampleCount\":10,\"thresholdType\":0,\"windowIntervalMs\":1000},\"clusterMode\":false,\"controlBehavior\":0,\"count\":0.0,\"durationInSec\":1,\"grade\":1,\"limitApp\":\"default\",\"maxQueueingTimeMs\":0,\"paramFlowItemList\":[],\"resource\":\"rs\"}}".equals(JSON.toJSONString(paramFlowRule)));
}
Also used : ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) ParamFlowClusterConfig(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig) ParamFlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity) Test(org.junit.Test)

Aggregations

ParamFlowClusterConfig (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig)8 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)3 ParamFlowRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity)2 Test (org.junit.Test)2 ClusterParamMetric (com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterParamMetric)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1