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;
}
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());
}
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;
}
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);
}
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));
}
Aggregations