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

The following examples show how to use com.alibaba.csp.sentinel.cluster.flow.rule.ClusterParamFlowRuleManager. 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 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));
}
 
Example #2
Source File: ModifyClusterParamFlowRulesCommandHandler.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("[ModifyClusterParamFlowRulesCommandHandler] Receiving cluster param rules for namespace <{0}>: {1}", namespace, data);

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

        return CommandResponse.ofSuccess(SUCCESS);
    } catch (Exception e) {
        RecordLog.warn("[ModifyClusterParamFlowRulesCommandHandler] Decode cluster param rules error", e);
        return CommandResponse.ofFailure(e, "decode cluster param rules error");
    }
}
 
Example #3
Source File: ClusterMetricNodeGenerator.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
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));
}
 
Example #4
Source File: ModifyClusterParamFlowRulesCommandHandler.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("Receiving cluster param rules for namespace <{}> from command handler: {}", namespace, data);

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

        return CommandResponse.ofSuccess(SUCCESS);
    } catch (Exception e) {
        RecordLog.warn("[ModifyClusterParamFlowRulesCommandHandler] Decode cluster param rules error", e);
        return CommandResponse.ofFailure(e, "decode cluster param rules error");
    }
}
 
Example #5
Source File: ClusterParamFlowChecker.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private static double calcGlobalThreshold(ParamFlowRule rule, Object value) {
    double count = getRawThreshold(rule, value);
    switch (rule.getClusterConfig().getThresholdType()) {
        case ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL:
            return count;
        case ClusterRuleConstant.FLOW_THRESHOLD_AVG_LOCAL:
        default:
            int connectedCount = ClusterParamFlowRuleManager.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 requestParamToken(Long ruleId, int acquireCount, Collection<Object> params) {
    if (notValidRequest(ruleId, acquireCount) || params == null || params.isEmpty()) {
        return badRequest();
    }
    // The rule should be valid.
    ParamFlowRule rule = ClusterParamFlowRuleManager.getParamRuleById(ruleId);
    if (rule == null) {
        return new TokenResult(TokenResultStatus.NO_RULE_EXISTS);
    }

    return ClusterParamFlowChecker.acquireClusterToken(rule, acquireCount, params);
}
 
Example #7
Source File: FetchClusterParamFlowRulesCommandHandler.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(ClusterParamFlowRuleManager.getAllParamRules()));
    } else {
        return CommandResponse.ofSuccess(JSON.toJSONString(ClusterParamFlowRuleManager.getParamRules(namespace)));
    }
}
 
Example #8
Source File: ClusterServer.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化集群热点参数限流的Supplier
 * 这样如果后期集群热点参数限流的规则发生变更的话,系统可以自动感知到
 */
public void initClusterParamFlowSupplier() {
    // 为集群热点参数流控注册一个Supplier,该Supplier会根据namespace动态创建数据源
    ClusterParamFlowRuleManager.setPropertySupplier(namespace -> {
        // 使用 Nacos 数据源作为配置中心,需要在 REMOTE_ADDRESS 上启动一个 Nacos 的服务
        ReadableDataSource<String, List<ParamFlowRule>> ds = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID,
                namespace + PARAM_FLOW_POSTFIX,
                source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {}));
        return ds.getProperty();
    });
}
 
Example #9
Source File: ClusterParamFlowChecker.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private static double calcGlobalThreshold(ParamFlowRule rule, Object value) {
    double count = getRawThreshold(rule, value);
    switch (rule.getClusterConfig().getThresholdType()) {
        case ClusterRuleConstant.FLOW_THRESHOLD_GLOBAL:
            return count;
        case ClusterRuleConstant.FLOW_THRESHOLD_AVG_LOCAL:
        default:
            int connectedCount = ClusterParamFlowRuleManager.getConnectedCount(rule.getClusterConfig().getFlowId());
            return count * connectedCount;
    }
}
 
Example #10
Source File: DefaultTokenService.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public TokenResult requestParamToken(Long ruleId, int acquireCount, Collection<Object> params) {
    if (notValidRequest(ruleId, acquireCount) || params == null || params.isEmpty()) {
        return badRequest();
    }
    // The rule should be valid.
    ParamFlowRule rule = ClusterParamFlowRuleManager.getParamRuleById(ruleId);
    if (rule == null) {
        return new TokenResult(TokenResultStatus.NO_RULE_EXISTS);
    }

    return ClusterParamFlowChecker.acquireClusterToken(rule, acquireCount, params);
}
 
Example #11
Source File: FetchClusterParamFlowRulesCommandHandler.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(ClusterParamFlowRuleManager.getAllParamRules()));
    } else {
        return CommandResponse.ofSuccess(JSON.toJSONString(ClusterParamFlowRuleManager.getParamRules(namespace)));
    }
}
 
Example #12
Source File: ClusterParamFlowChecker.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
static boolean allowProceed(long flowId) {
    String namespace = ClusterParamFlowRuleManager.getNamespace(flowId);
    return GlobalRequestLimiter.tryPass(namespace);
}
 
Example #13
Source File: ClusterParamFlowChecker.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
static boolean allowProceed(long flowId) {
    String namespace = ClusterParamFlowRuleManager.getNamespace(flowId);
    return GlobalRequestLimiter.tryPass(namespace);
}