Search in sources :

Example 1 with MatchString

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;
}
Also used : MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) RateLimit(com.tencent.polaris.client.pb.RateLimitProto.RateLimit) Rule(com.tencent.polaris.client.pb.RateLimitProto.Rule) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with MatchString

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);
}
Also used : MatchStringType(com.tencent.polaris.client.pb.ModelProto.MatchString.MatchStringType) Pattern(java.util.regex.Pattern) FlowCache(com.tencent.polaris.api.plugin.cache.FlowCache) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString)

Example 3 with MatchString

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);
}
Also used : MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) HashMap(java.util.HashMap) InstanceParameter(com.tencent.polaris.test.mock.discovery.NamingService.InstanceParameter) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) Routing(com.tencent.polaris.client.pb.RoutingProto.Routing) IOException(java.io.IOException) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) Before(org.junit.Before)

Example 4 with MatchString

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);
}
Also used : MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) Pattern(java.util.regex.Pattern) DestinationSet(com.tencent.polaris.client.pb.CircuitBreakerProto.DestinationSet) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString)

Example 5 with MatchString

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);
}
Also used : MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) ArrayList(java.util.ArrayList) Rule(com.tencent.polaris.client.pb.RateLimitProto.Rule) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

MatchString (com.tencent.polaris.client.pb.ModelProto.MatchString)7 HashMap (java.util.HashMap)4 Map (java.util.Map)3 ServiceRule (com.tencent.polaris.api.pojo.ServiceRule)2 Rule (com.tencent.polaris.client.pb.RateLimitProto.Rule)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Pattern (java.util.regex.Pattern)2 FlowCache (com.tencent.polaris.api.plugin.cache.FlowCache)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 DestinationSet (com.tencent.polaris.client.pb.CircuitBreakerProto.DestinationSet)1 MatchStringType (com.tencent.polaris.client.pb.ModelProto.MatchString.MatchStringType)1 RateLimit (com.tencent.polaris.client.pb.RateLimitProto.RateLimit)1 Routing (com.tencent.polaris.client.pb.RoutingProto.Routing)1 InstanceParameter (com.tencent.polaris.test.mock.discovery.NamingService.InstanceParameter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1