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