Search in sources :

Example 6 with ClusterFlowConfig

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

the class ConcurrentClusterFlowCheckerTest method setUp.

@Before
public void setUp() {
    FlowRule rule = new FlowRule();
    ClusterFlowConfig config = new ClusterFlowConfig();
    config.setResourceTimeout(500);
    config.setClientOfflineTime(1000);
    config.setFlowId(111L);
    config.setThresholdType(ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL);
    rule.setClusterConfig(config);
    rule.setClusterMode(true);
    rule.setCount(10);
    rule.setResource("test");
    rule.setGrade(RuleConstant.FLOW_GRADE_THREAD);
    ArrayList<FlowRule> rules = new ArrayList<>();
    rules.add(rule);
    ClusterFlowRuleManager.registerPropertyIfAbsent("1-name");
    ClusterFlowRuleManager.loadRules("1-name", rules);
}
Also used : ArrayList(java.util.ArrayList) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) ClusterFlowConfig(com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig) Before(org.junit.Before)

Example 7 with ClusterFlowConfig

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

the class TokenCacheNodeManagerTest method setUp.

@Before
public void setUp() {
    FlowRule rule = new FlowRule();
    ClusterFlowConfig config = new ClusterFlowConfig();
    config.setResourceTimeout(500);
    config.setClientOfflineTime(1000);
    config.setFlowId(111L);
    config.setThresholdType(ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL);
    rule.setClusterConfig(config);
    rule.setClusterMode(true);
    rule.setCount(10);
    rule.setResource("test");
    rule.setGrade(RuleConstant.FLOW_GRADE_THREAD);
    ArrayList<FlowRule> rules = new ArrayList<>();
    rules.add(rule);
    ClusterFlowRuleManager.registerPropertyIfAbsent("1-name");
    ClusterFlowRuleManager.loadRules("1-name", rules);
}
Also used : ArrayList(java.util.ArrayList) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) ClusterFlowConfig(com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig) Before(org.junit.Before)

Example 8 with ClusterFlowConfig

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

the class ClusterFlowRuleManager method applyClusterFlowRule.

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

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

Example 9 with ClusterFlowConfig

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

the class InMemFlowRuleStore method preProcess.

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

Example 10 with ClusterFlowConfig

use of com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig in project RuoYi-Cloud-Plus by JavaLionLi.

the class InMemFlowRuleStore method preProcess.

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

Aggregations

ClusterFlowConfig (com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig)10 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)5 ClusterMetric (com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterMetric)2 ArrayList (java.util.ArrayList)2 Before (org.junit.Before)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1