com.alibaba.csp.sentinel.cluster.flow.rule.ClusterFlowRuleManager Java Examples

The following examples show how to use com.alibaba.csp.sentinel.cluster.flow.rule.ClusterFlowRuleManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClusterMetricNodeGenerator.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
public static ClusterMetricNode flowToMetricNode(long flowId) {
    FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(flowId);
    if (rule == null) {
        return null;
    }
    ClusterMetric metric = ClusterMetricStatistics.getMetric(flowId);
    if (metric == null) {
        return new ClusterMetricNode().setFlowId(flowId)
            .setResourceName(rule.getResource());
    }
    return new ClusterMetricNode()
        .setFlowId(flowId)
        .setResourceName(rule.getResource())
        .setBlockQps(metric.getAvg(ClusterFlowEvent.BLOCK))
        .setPassQps(metric.getAvg(ClusterFlowEvent.PASS))
        .setTimestamp(TimeUtil.currentTimeMillis());
}
 
Example #2
Source File: ModifyClusterFlowRulesCommandHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String namespace = request.getParam("namespace");
    if (StringUtil.isEmpty(namespace)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("empty namespace"));
    }
    String data = request.getParam("data");
    if (StringUtil.isBlank(data)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("empty data"));
    }
    try {
        data = URLDecoder.decode(data, "UTF-8");
        RecordLog.info("[ModifyClusterFlowRulesCommandHandler] Receiving cluster flow rules for namespace <{0}>: {1}", namespace, data);

        List<FlowRule> flowRules = JSONArray.parseArray(data, FlowRule.class);
        ClusterFlowRuleManager.loadRules(namespace, flowRules);

        return CommandResponse.ofSuccess(SUCCESS);
    } catch (Exception e) {
        RecordLog.warn("[ModifyClusterFlowRulesCommandHandler] Decode cluster flow rules error", e);
        return CommandResponse.ofFailure(e, "decode cluster flow rules error");
    }
}
 
Example #3
Source File: ClusterMetricNodeGenerator.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
public static ClusterMetricNode flowToMetricNode(long flowId) {
    FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(flowId);
    if (rule == null) {
        return null;
    }
    ClusterMetric metric = ClusterMetricStatistics.getMetric(flowId);
    if (metric == null) {
        return new ClusterMetricNode().setFlowId(flowId)
            .setResourceName(rule.getResource());
    }
    return new ClusterMetricNode()
        .setFlowId(flowId)
        .setResourceName(rule.getResource())
        .setBlockQps(metric.getAvg(ClusterFlowEvent.BLOCK))
        .setPassQps(metric.getAvg(ClusterFlowEvent.PASS))
        .setTimestamp(TimeUtil.currentTimeMillis());
}
 
Example #4
Source File: ModifyClusterFlowRulesCommandHandler.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String namespace = request.getParam("namespace");
    if (StringUtil.isEmpty(namespace)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("empty namespace"));
    }
    String data = request.getParam("data");
    if (StringUtil.isBlank(data)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("empty data"));
    }
    try {
        data = URLDecoder.decode(data, "UTF-8");
        RecordLog.info("[ModifyClusterFlowRulesCommandHandler] Receiving cluster flow rules for namespace <{}>: {}", namespace, data);

        List<FlowRule> flowRules = JSONArray.parseArray(data, FlowRule.class);
        ClusterFlowRuleManager.loadRules(namespace, flowRules);

        return CommandResponse.ofSuccess(SUCCESS);
    } catch (Exception e) {
        RecordLog.warn("[ModifyClusterFlowRulesCommandHandler] Decode cluster flow rules error", e);
        return CommandResponse.ofFailure(e, "decode cluster flow rules error");
    }
}
 
Example #5
Source File: ClusterFlowChecker.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private static double calcGlobalThreshold(FlowRule rule) {
    double count = rule.getCount();
    switch (rule.getClusterConfig().getThresholdType()) {
        case ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL:
            return count;
        case ClusterRuleConstant.FLOW_THRESHOLD_AVG_LOCAL:
        default:
            int connectedCount = ClusterFlowRuleManager.getConnectedCount(rule.getClusterConfig().getFlowId());
            return count * connectedCount;
    }
}
 
Example #6
Source File: DefaultTokenService.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public TokenResult requestToken(Long ruleId, int acquireCount, boolean prioritized) {
    if (notValidRequest(ruleId, acquireCount)) {
        return badRequest();
    }
    // The rule should be valid.
    FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(ruleId);
    if (rule == null) {
        return new TokenResult(TokenResultStatus.NO_RULE_EXISTS);
    }

    return ClusterFlowChecker.acquireClusterToken(rule, acquireCount, prioritized);
}
 
Example #7
Source File: FetchClusterFlowRulesCommandHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String namespace = request.getParam("namespace");
    if (StringUtil.isEmpty(namespace)) {
        return CommandResponse.ofSuccess(JSON.toJSONString(ClusterFlowRuleManager.getAllFlowRules()));
    } else {
        return CommandResponse.ofSuccess(JSON.toJSONString(ClusterFlowRuleManager.getFlowRules(namespace)));
    }
}
 
Example #8
Source File: ClusterServer.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化集群限流的Supplier
 * 这样如果后期集群限流的规则发生变更的话,系统可以自动感知到
 */
private void initClusterFlowSupplier() {
    // 为集群流控注册一个Supplier,该Supplier会根据namespace动态创建数据源
    ClusterFlowRuleManager.setPropertySupplier(namespace -> {
        // 使用 Nacos 数据源作为配置中心,需要在 REMOTE_ADDRESS 上启动一个 Nacos 的服务
        ReadableDataSource<String, List<FlowRule>> ds = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID,
                namespace + FLOW_POSTFIX,
                source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
        return ds.getProperty();
    });
}
 
Example #9
Source File: EnvoyRlsRuleManager.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void configUpdate(List<EnvoyRlsRule> conf) {
    Map<String, EnvoyRlsRule> ruleMap = generateRuleMap(conf);

    List<FlowRule> flowRules = ruleMap.values().stream()
        .flatMap(e -> EnvoySentinelRuleConverter.toSentinelFlowRules(e).stream())
        .collect(Collectors.toList());

    RULE_MAP.clear();
    RULE_MAP.putAll(ruleMap);
    RecordLog.info("[EnvoyRlsRuleManager] Envoy RLS rules loaded: " + flowRules);

    // Use the "default" namespace.
    ClusterFlowRuleManager.loadRules(ServerConstants.DEFAULT_NAMESPACE, flowRules);
}
 
Example #10
Source File: SentinelEnvoyRlsServiceImpl.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
protected Tuple2<FlowRule, TokenResult> checkToken(String domain, RateLimitDescriptor descriptor, int acquireCount) {
    long ruleId = EnvoySentinelRuleConverter.generateFlowId(generateKey(domain, descriptor));

    FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(ruleId);
    if (rule == null) {
        // Pass if the target rule is absent.
        return Tuple2.of(null, new TokenResult(TokenResultStatus.NO_RULE_EXISTS));
    }
    // If the rule is present, it should be valid.
    return Tuple2.of(rule, SimpleClusterFlowChecker.acquireClusterToken(rule, acquireCount));
}
 
Example #11
Source File: ClusterFlowChecker.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private static double calcGlobalThreshold(FlowRule rule) {
    double count = rule.getCount();
    switch (rule.getClusterConfig().getThresholdType()) {
        case ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL:
            return count;
        case ClusterRuleConstant.FLOW_THRESHOLD_AVG_LOCAL:
        default:
            int connectedCount = ClusterFlowRuleManager.getConnectedCount(rule.getClusterConfig().getFlowId());
            return count * connectedCount;
    }
}
 
Example #12
Source File: DefaultTokenService.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public TokenResult requestToken(Long ruleId, int acquireCount, boolean prioritized) {
    if (notValidRequest(ruleId, acquireCount)) {
        return badRequest();
    }
    // The rule should be valid.
    FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(ruleId);
    if (rule == null) {
        return new TokenResult(TokenResultStatus.NO_RULE_EXISTS);
    }

    return ClusterFlowChecker.acquireClusterToken(rule, acquireCount, prioritized);
}
 
Example #13
Source File: FetchClusterFlowRulesCommandHandler.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String namespace = request.getParam("namespace");
    if (StringUtil.isEmpty(namespace)) {
        return CommandResponse.ofSuccess(JSON.toJSONString(ClusterFlowRuleManager.getAllFlowRules()));
    } else {
        return CommandResponse.ofSuccess(JSON.toJSONString(ClusterFlowRuleManager.getFlowRules(namespace)));
    }
}
 
Example #14
Source File: ClusterFlowChecker.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
static boolean allowProceed(long flowId) {
    String namespace = ClusterFlowRuleManager.getNamespace(flowId);
    return GlobalRequestLimiter.tryPass(namespace);
}
 
Example #15
Source File: ClusterFlowChecker.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
static boolean allowProceed(long flowId) {
    String namespace = ClusterFlowRuleManager.getNamespace(flowId);
    return GlobalRequestLimiter.tryPass(namespace);
}