Search in sources :

Example 1 with RateLimit

use of com.tencent.polaris.client.pb.RateLimitProto.RateLimit in project polaris-java by polarismesh.

the class QuotaFlow method parseRules.

private static Map<String, Rule> parseRules(RegistryCacheValue oldValue) {
    if (null == oldValue || !oldValue.isInitialized()) {
        return null;
    }
    ServiceRule serviceRule = (ServiceRule) oldValue;
    if (null == serviceRule.getRule()) {
        return null;
    }
    Map<String, Rule> ruleMap = new HashMap<>();
    RateLimit rateLimit = (RateLimit) serviceRule.getRule();
    for (Rule rule : rateLimit.getRulesList()) {
        ruleMap.put(rule.getRevision().getValue(), rule);
    }
    return ruleMap;
}
Also used : RateLimit(com.tencent.polaris.client.pb.RateLimitProto.RateLimit) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) Rule(com.tencent.polaris.client.pb.RateLimitProto.Rule) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule)

Example 2 with RateLimit

use of com.tencent.polaris.client.pb.RateLimitProto.RateLimit 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 3 with RateLimit

use of com.tencent.polaris.client.pb.RateLimitProto.RateLimit in project polaris-java by polarismesh.

the class NamingService method buildServiceResponse.

private ResponseProto.DiscoverResponse buildServiceResponse(int code, String info, DiscoverRequest req) {
    ResponseProto.DiscoverResponse.Builder builder = ResponseProto.DiscoverResponse.newBuilder();
    builder.setCode(UInt32Value.newBuilder().setValue(code).build());
    CircuitBreakerProto.CircuitBreaker circuitBreaker;
    RateLimitProto.RateLimit rateLimit;
    RoutingProto.Routing routing;
    List<ServiceProto.Instance> instances;
    ServiceProto.Service service = req.getService();
    ServiceKey serviceKey = new ServiceKey(service.getNamespace().getValue(), service.getName().getValue());
    switch(req.getType()) {
        case UNKNOWN:
            break;
        case INSTANCE:
            instances = services.get(serviceKey);
            if (CollectionUtils.isNotEmpty(instances)) {
                builder.addAllInstances(instances);
            }
            builder.setType(DiscoverResponseType.INSTANCE);
            break;
        case CLUSTER:
            break;
        case ROUTING:
            routing = serviceRoutings.get(serviceKey);
            if (null != routing) {
                builder.setRouting(routing);
            }
            builder.setType(DiscoverResponseType.ROUTING);
            break;
        case CIRCUIT_BREAKER:
            circuitBreaker = serviceCircuitBreakers.get(serviceKey);
            if (null != circuitBreaker) {
                builder.setCircuitBreaker(circuitBreaker);
            }
            builder.setType(DiscoverResponseType.CIRCUIT_BREAKER);
            break;
        case RATE_LIMIT:
            rateLimit = serviceRateLimits.get(serviceKey);
            if (null != rateLimit) {
                builder.setRateLimit(rateLimit);
            }
            builder.setType(DiscoverResponseType.RATE_LIMIT);
            break;
        case SERVICES:
            Set<ServiceKey> keys = services.keySet();
            Map<String, ServiceProto.Service> tmp = new HashMap<>();
            String namespace = req.getService().getNamespace().getValue();
            System.out.println("get service param : " + namespace);
            keys.removeIf(serviceKey1 -> {
                if (StringUtils.isBlank(namespace)) {
                    return false;
                }
                return !Objects.equals(namespace, serviceKey1.getNamespace());
            });
            keys.forEach(key -> {
                tmp.put(key.getNamespace() + "##" + key.getService(), ServiceProto.Service.newBuilder().setNamespace(StringValue.newBuilder().setValue(key.getNamespace()).build()).setName(StringValue.newBuilder().setValue(key.getService()).build()).build());
            });
            final int[] index = { 0 };
            tmp.forEach((s, svc) -> {
                builder.addServices(index[0], svc);
                index[0]++;
            });
            builder.setType(DiscoverResponseType.SERVICES);
            break;
        case UNRECOGNIZED:
            break;
        default:
            break;
    }
    if (StringUtils.isNotBlank(info)) {
        builder.setInfo(StringValue.newBuilder().setValue(info).build());
    }
    builder.setService(service);
    return builder.build();
}
Also used : Instance(com.tencent.polaris.client.pb.ServiceProto.Instance) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) RateLimitProto(com.tencent.polaris.client.pb.RateLimitProto) RoutingProto(com.tencent.polaris.client.pb.RoutingProto) RateLimit(com.tencent.polaris.client.pb.RateLimitProto.RateLimit) CircuitBreaker(com.tencent.polaris.client.pb.CircuitBreakerProto.CircuitBreaker) CircuitBreakerProto(com.tencent.polaris.client.pb.CircuitBreakerProto) ServiceProto(com.tencent.polaris.client.pb.ServiceProto)

Example 4 with RateLimit

use of com.tencent.polaris.client.pb.RateLimitProto.RateLimit in project polaris-java by polarismesh.

the class RateLimitingCacheHandler method messageToCacheValue.

@Override
public RegistryCacheValue messageToCacheValue(RegistryCacheValue oldValue, Object newValue, boolean isCacheLoaded) {
    DiscoverResponse discoverResponse = (DiscoverResponse) newValue;
    RateLimit rateLimit = discoverResponse.getRateLimit();
    String revision = getRevision(discoverResponse);
    List<Rule> rulesList = rateLimit.getRulesList();
    // 需要做一次排序,PB中的数据不可变,需要单独构建一份
    List<Rule> sortedRules = new ArrayList<>(rulesList);
    sortedRules.sort((o1, o2) -> {
        if (o1.getPriority().getValue() != o2.getPriority().getValue()) {
            return o1.getPriority().getValue() - o2.getPriority().getValue();
        }
        return o1.getId().getValue().compareTo(o2.getId().getValue());
    });
    RateLimit newRateLimit = RateLimit.newBuilder().addAllRules(sortedRules).setRevision(StringValue.newBuilder().setValue(revision).build()).build();
    return new ServiceRuleByProto(newRateLimit, revision, isCacheLoaded, getTargetEventType());
}
Also used : RateLimit(com.tencent.polaris.client.pb.RateLimitProto.RateLimit) ArrayList(java.util.ArrayList) DiscoverResponse(com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse) Rule(com.tencent.polaris.client.pb.RateLimitProto.Rule) ServiceRuleByProto(com.tencent.polaris.client.pojo.ServiceRuleByProto)

Aggregations

RateLimit (com.tencent.polaris.client.pb.RateLimitProto.RateLimit)4 Rule (com.tencent.polaris.client.pb.RateLimitProto.Rule)3 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ServiceRule (com.tencent.polaris.api.pojo.ServiceRule)2 MatchString (com.tencent.polaris.client.pb.ModelProto.MatchString)2 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 CircuitBreakerProto (com.tencent.polaris.client.pb.CircuitBreakerProto)1 CircuitBreaker (com.tencent.polaris.client.pb.CircuitBreakerProto.CircuitBreaker)1 RateLimitProto (com.tencent.polaris.client.pb.RateLimitProto)1 DiscoverResponse (com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse)1 RoutingProto (com.tencent.polaris.client.pb.RoutingProto)1 ServiceProto (com.tencent.polaris.client.pb.ServiceProto)1 Instance (com.tencent.polaris.client.pb.ServiceProto.Instance)1 ServiceRuleByProto (com.tencent.polaris.client.pojo.ServiceRuleByProto)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1