use of com.tencent.polaris.client.pb.ModelProto.MatchString in project polaris-java by polarismesh.
the class QuotaFlow method lookupRule.
private Rule lookupRule(ServiceRule serviceRule, Map<String, String> labels) {
if (null == serviceRule.getRule()) {
return null;
}
RateLimit rateLimitProto = (RateLimit) serviceRule.getRule();
List<Rule> rulesList = rateLimitProto.getRulesList();
if (CollectionUtils.isEmpty(rulesList)) {
return null;
}
for (Rule rule : rulesList) {
if (null != rule.getDisable() && rule.getDisable().getValue()) {
continue;
}
if (rule.getAmountsCount() == 0) {
// 没有amount的规则就忽略
continue;
}
if (rule.getLabelsCount() == 0) {
return rule;
}
boolean allMatchLabels = true;
Map<String, MatchString> labelsMap = rule.getLabelsMap();
for (Map.Entry<String, MatchString> entry : labelsMap.entrySet()) {
if (!matchLabels(entry.getKey(), entry.getValue(), labels)) {
allMatchLabels = false;
break;
}
}
if (allMatchLabels) {
return rule;
}
}
return null;
}
use of com.tencent.polaris.client.pb.ModelProto.MatchString in project polaris-java by polarismesh.
the class QuotaFlow method matchLabels.
private boolean matchLabels(String ruleLabelKey, MatchString ruleLabelMatch, Map<String, String> labels) {
// 设置了MatchAllValue,相当于这个规则就无效了
if (RuleUtils.isMatchAllValue(ruleLabelMatch)) {
return true;
}
if (MapUtils.isEmpty(labels)) {
return false;
}
// 集成的路由规则不包含这个key,就不匹配
if (!labels.containsKey(ruleLabelKey)) {
return false;
}
String labelValue = labels.get(ruleLabelKey);
FlowCache flowCache = rateLimitExtension.getExtensions().getFlowCache();
MatchStringType matchType = ruleLabelMatch.getType();
String matchValue = ruleLabelMatch.getValue().getValue();
if (matchType == MatchStringType.REGEX) {
// 正则表达式匹配
Pattern pattern = flowCache.loadOrStoreCompiledRegex(matchValue);
return pattern.matcher(labelValue).find();
}
return StringUtils.equals(labelValue, matchValue);
}
use of com.tencent.polaris.client.pb.ModelProto.MatchString in project polaris-java by polarismesh.
the class ServiceDynamicRuleTest method before.
@Before
public void before() {
try {
namingServer = NamingServer.startNamingServer(10081);
} catch (IOException e) {
Assert.fail(e.getMessage());
}
ServiceKey serviceKey = new ServiceKey(NAMESPACE_PRODUCTION, RULE_ROUTER_SERVICE);
InstanceParameter parameter = new InstanceParameter();
parameter.setWeight(100);
parameter.setHealthy(true);
parameter.setIsolated(false);
Map<String, String> metadata = new HashMap<>();
metadata.put("env", "base");
parameter.setMetadata(metadata);
namingServer.getNamingService().batchAddInstances(serviceKey, 10001, MATCH_META_COUNT, parameter);
parameter.setMetadata(null);
namingServer.getNamingService().batchAddInstances(serviceKey, 10010, 8, parameter);
Map<String, MatchString> data = new HashMap<>();
data.put("env", MatchString.newBuilder().setType(MatchStringType.EXACT).setValue(StringValue.newBuilder().setValue("base").build()).build());
Map<String, MatchString> srcData = new HashMap<>();
srcData.put("uid", MatchString.newBuilder().setType(MatchStringType.EXACT).setValue(StringValue.newBuilder().setValue("144115217417489762").build()).build());
Routing routing = Routing.newBuilder().addInbounds(Route.newBuilder().addDestinations(Destination.newBuilder().putAllMetadata(data).setWeight(UInt32Value.newBuilder().setValue(100).build()).build()).addSources(Source.newBuilder().setNamespace(StringValue.newBuilder().setValue("*").build()).setService(StringValue.newBuilder().setValue("*").build()).putAllMetadata(srcData).build()).build()).build();
namingServer.getNamingService().setRouting(serviceKey, routing);
}
use of com.tencent.polaris.client.pb.ModelProto.MatchString in project polaris-java by polarismesh.
the class CircuitBreakUtils method matchDestination.
private static MatchDestResult matchDestination(CbRule rule, RuleIdentifier ruleIdentifier, FlowCache flowCache) {
if (rule.getDestinationsCount() == 0) {
return new MatchDestResult(null, false);
}
for (DestinationSet destinationSet : rule.getDestinationsList()) {
boolean namespaceMatch = destinationSet.getNamespace().getValue().equals(matchAll);
boolean serviceMatch = destinationSet.getService().getValue().equals(matchAll);
if (!namespaceMatch) {
namespaceMatch = destinationSet.getNamespace().getValue().equals(ruleIdentifier.getNamespace());
}
if (!serviceMatch) {
serviceMatch = destinationSet.getService().getValue().equals(ruleIdentifier.getService());
}
if (!namespaceMatch || !serviceMatch) {
continue;
}
MatchString methodMatcher = destinationSet.getMethod();
if (null == methodMatcher) {
return new MatchDestResult(destinationSet, true);
}
if (RuleUtils.isMatchAllValue(methodMatcher)) {
return new MatchDestResult(destinationSet, true);
}
String method = ruleIdentifier.getMethod();
if (methodMatcher.getType() == MatchStringType.EXACT) {
if (StringUtils.equals(methodMatcher.getValue().getValue(), method)) {
return new MatchDestResult(destinationSet, false);
}
}
Pattern pattern = flowCache.loadOrStoreCompiledRegex(methodMatcher.getValue().getValue());
if (pattern.matcher(method).find()) {
return new MatchDestResult(destinationSet, false);
}
}
return new MatchDestResult(null, false);
}
use of com.tencent.polaris.client.pb.ModelProto.MatchString in project polaris-java by polarismesh.
the class QuotaFlow method formatLabelsToStr.
private static String formatLabelsToStr(CommonQuotaRequest request) {
Rule rule = request.getInitCriteria().getRule();
Map<String, String> labels = request.getLabels();
if (rule.getLabelsCount() == 0 || MapUtils.isEmpty(labels)) {
return "";
}
List<String> tmpList = new ArrayList<>();
boolean regexCombine = rule.getRegexCombine().getValue();
Map<String, MatchString> labelsMap = rule.getLabelsMap();
for (Map.Entry<String, MatchString> entry : labelsMap.entrySet()) {
MatchString matcher = entry.getValue();
String labelEntry;
if (matcher.getType() == MatchStringType.REGEX && regexCombine) {
labelEntry = entry.getKey() + RateLimitConstants.DEFAULT_KV_SEPARATOR + matcher.getValue().getValue();
} else {
labelEntry = entry.getKey() + RateLimitConstants.DEFAULT_KV_SEPARATOR + labels.get(entry.getKey());
if (matcher.getType() == MatchStringType.REGEX) {
// 正则表达式扩散
request.setRegexSpread(true);
}
}
tmpList.add(labelEntry);
}
Collections.sort(tmpList);
return String.join(RateLimitConstants.DEFAULT_ENTRY_SEPARATOR, tmpList);
}
Aggregations