Search in sources :

Example 86 with FlowRule

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

the class SentinelMotanConsumerService method initFlowRule.

private static void initFlowRule(int interfaceFlowLimit, boolean method) {
    FlowRule flowRule = new FlowRule(INTERFACE_RES_KEY).setCount(interfaceFlowLimit).setGrade(RuleConstant.FLOW_GRADE_QPS);
    List<FlowRule> list = new ArrayList<>();
    if (method) {
        FlowRule flowRule1 = new FlowRule(RES_KEY).setCount(5).setGrade(RuleConstant.FLOW_GRADE_QPS);
        list.add(flowRule1);
    }
    list.add(flowRule);
    FlowRuleManager.loadRules(list);
}
Also used : ArrayList(java.util.ArrayList) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule)

Example 87 with FlowRule

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

the class ConcurrentClusterFlowChecker method releaseConcurrentToken.

public static TokenResult releaseConcurrentToken(/*@Valid*/
long tokenId) {
    TokenCacheNode node = TokenCacheNodeManager.getTokenCacheNode(tokenId);
    if (node == null) {
        RecordLog.info("[ConcurrentClusterFlowChecker] Token<{}> is already released", tokenId);
        return new TokenResult(TokenResultStatus.ALREADY_RELEASE);
    }
    FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(node.getFlowId());
    if (rule == null) {
        RecordLog.info("[ConcurrentClusterFlowChecker] Fail to get rule by flowId<{}>", node.getFlowId());
        return new TokenResult(TokenResultStatus.NO_RULE_EXISTS);
    }
    if (TokenCacheNodeManager.removeTokenCacheNode(tokenId) == null) {
        RecordLog.info("[ConcurrentClusterFlowChecker] Token<{}> is already released for flowId<{}>", tokenId, node.getFlowId());
        return new TokenResult(TokenResultStatus.ALREADY_RELEASE);
    }
    int acquireCount = node.getAcquireCount();
    AtomicInteger nowCalls = CurrentConcurrencyManager.get(node.getFlowId());
    nowCalls.getAndAdd(-1 * acquireCount);
    ClusterServerStatLogUtil.log("concurrent|release|" + rule.getClusterConfig().getFlowId(), acquireCount);
    return new TokenResult(TokenResultStatus.RELEASE_OK);
}
Also used : TokenCacheNode(com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.TokenCacheNode) TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule)

Example 88 with FlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule 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 89 with FlowRule

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

the class ClusterConcurrentCheckerLogListener method collectInformation.

private void collectInformation() {
    Set<Long> keySet = CurrentConcurrencyManager.getConcurrencyMapKeySet();
    for (long flowId : keySet) {
        FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(flowId);
        if (rule == null || CurrentConcurrencyManager.get(flowId).get() == 0) {
            continue;
        }
        double concurrencyLevel = ConcurrentClusterFlowChecker.calcGlobalThreshold(rule);
        String resource = rule.getResource();
        ClusterServerStatLogUtil.log(String.format("concurrent|resource:%s|flowId:%dl|concurrencyLevel:%fl|currentConcurrency", resource, flowId, concurrencyLevel), CurrentConcurrencyManager.get(flowId).get());
    }
    if (TokenCacheNodeManager.getSize() != 0) {
        ClusterServerStatLogUtil.log("flow|totalTokenSize", TokenCacheNodeManager.getSize());
    }
}
Also used : FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule)

Example 90 with FlowRule

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

the class StandaloneRedisDataSourceTest method buildResource.

@Before
public void buildResource() {
    try {
        // Bind to a random port.
        server = RedisServer.newRedisServer();
        server.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Converter<String, List<FlowRule>> flowConfigParser = buildFlowConfigParser();
    client = RedisClient.create(RedisURI.create(server.getHost(), server.getBindPort()));
    RedisConnectionConfig config = RedisConnectionConfig.builder().withHost(server.getHost()).withPort(server.getBindPort()).build();
    initRedisRuleData();
    ReadableDataSource<String, List<FlowRule>> redisDataSource = new RedisDataSource<List<FlowRule>>(config, ruleKey, channel, flowConfigParser);
    FlowRuleManager.register2Property(redisDataSource.getProperty());
}
Also used : RedisConnectionConfig(com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig) List(java.util.List) IOException(java.io.IOException) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) Before(org.junit.Before)

Aggregations

FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)113 ArrayList (java.util.ArrayList)31 Test (org.junit.Test)31 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)22 List (java.util.List)17 TokenResult (com.alibaba.csp.sentinel.cluster.TokenResult)11 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)9 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)6 Test (org.junit.jupiter.api.Test)6 ClusterFlowConfig (com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig)5 TypeReference (com.alibaba.fastjson.TypeReference)5 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)5 Before (org.junit.Before)5 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)4 ClusterMetric (com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterMetric)3 FlowRuleManager (com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager)3 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)3 RateLimitDescriptor (io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor)3 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse)3 IOException (java.io.IOException)3