Search in sources :

Example 1 with ServiceRule

use of com.tencent.polaris.api.pojo.ServiceRule in project polaris-java by polarismesh.

the class BaseFlow method loadLocalResources.

private static boolean loadLocalResources(ServiceEventKey svcEventKey, ResourcesResponse resourcesResponse, LocalRegistry localRegistry) {
    ResourceFilter filter = new ResourceFilter(svcEventKey, false, true);
    if (svcEventKey.getEventType() == EventType.INSTANCE) {
        ServiceInstances instances = localRegistry.getInstances(filter);
        if (instances.isInitialized()) {
            resourcesResponse.addServiceInstances(svcEventKey, instances);
            return false;
        } else {
            return true;
        }
    }
    if (svcEventKey.getEventType() == EventType.SERVICE) {
        Services services = localRegistry.getServices(filter);
        if (services.isInitialized()) {
            resourcesResponse.addServices(svcEventKey, services);
            return false;
        } else {
            return true;
        }
    }
    ServiceRule serviceRule = localRegistry.getServiceRule(filter);
    if (serviceRule.isInitialized()) {
        resourcesResponse.addServiceRule(svcEventKey, serviceRule);
        return false;
    } else {
        return true;
    }
}
Also used : Services(com.tencent.polaris.api.pojo.Services) ResourceFilter(com.tencent.polaris.api.plugin.registry.ResourceFilter) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule)

Example 2 with ServiceRule

use of com.tencent.polaris.api.pojo.ServiceRule in project polaris-java by polarismesh.

the class GetResourcesInvoker method processSvcEventKey.

private int processSvcEventKey(LocalRegistry localRegistry, int callbacks, ServiceEventKey svcEventKey) {
    ResourceFilter filter = new ResourceFilter(svcEventKey, internalRequest, useCache);
    switch(svcEventKey.getEventType()) {
        case INSTANCE:
            ServiceInstances instances = localRegistry.getInstances(filter);
            if (instances.isInitialized()) {
                resourcesResponse.addServiceInstances(svcEventKey, instances);
            } else {
                localRegistry.loadInstances(svcEventKey, this);
                callbacks++;
            }
            break;
        case SERVICE:
            Services services = localRegistry.getServices(filter);
            if (services.isInitialized()) {
                resourcesResponse.addServices(svcEventKey, services);
            } else {
                localRegistry.loadServices(svcEventKey, this);
                callbacks++;
            }
            break;
        default:
            ServiceRule serviceRule = localRegistry.getServiceRule(filter);
            if (serviceRule.isInitialized()) {
                resourcesResponse.addServiceRule(svcEventKey, serviceRule);
            } else {
                localRegistry.loadServiceRule(svcEventKey, this);
                callbacks++;
            }
            break;
    }
    return callbacks;
}
Also used : Services(com.tencent.polaris.api.pojo.Services) ResourceFilter(com.tencent.polaris.api.plugin.registry.ResourceFilter) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule)

Example 3 with ServiceRule

use of com.tencent.polaris.api.pojo.ServiceRule in project polaris-java by polarismesh.

the class GetResourcesInvoker method complete.

@Override
public void complete(ServiceEventKey svcEventKey) {
    LocalRegistry localRegistry = extensions.getLocalRegistry();
    ResourceFilter filter = new ResourceFilter(svcEventKey, internalRequest, useCache);
    if (svcEventKey.getEventType() == ServiceEventKey.EventType.INSTANCE) {
        ServiceInstances instances = localRegistry.getInstances(filter);
        resourcesResponse.addServiceInstances(svcEventKey, instances);
    } else if (svcEventKey.getEventType() == EventType.SERVICE) {
        Services services = localRegistry.getServices(filter);
        resourcesResponse.addServices(svcEventKey, services);
    } else {
        ServiceRule serviceRule = localRegistry.getServiceRule(filter);
        resourcesResponse.addServiceRule(svcEventKey, serviceRule);
    }
    synchronized (notifier) {
        int curTotal = responseIncrement.addAndGet(1);
        if (totalCallback == curTotal) {
            notifier.notifyAll();
        }
    }
}
Also used : Services(com.tencent.polaris.api.pojo.Services) ResourceFilter(com.tencent.polaris.api.plugin.registry.ResourceFilter) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule) LocalRegistry(com.tencent.polaris.api.plugin.registry.LocalRegistry)

Example 4 with ServiceRule

use of com.tencent.polaris.api.pojo.ServiceRule 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 5 with ServiceRule

use of com.tencent.polaris.api.pojo.ServiceRule 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)

Aggregations

ServiceRule (com.tencent.polaris.api.pojo.ServiceRule)8 ResourceFilter (com.tencent.polaris.api.plugin.registry.ResourceFilter)3 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)3 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)3 Services (com.tencent.polaris.api.pojo.Services)3 ResourcesResponse (com.tencent.polaris.client.flow.ResourcesResponse)3 MatchString (com.tencent.polaris.client.pb.ModelProto.MatchString)3 Rule (com.tencent.polaris.client.pb.RateLimitProto.Rule)3 DefaultServiceEventKeysProvider (com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider)2 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)2 RateLimit (com.tencent.polaris.client.pb.RateLimitProto.RateLimit)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 LocalRegistry (com.tencent.polaris.api.plugin.registry.LocalRegistry)1 RoutingProto (com.tencent.polaris.client.pb.RoutingProto)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1