Search in sources :

Example 31 with DiscoveryTreeNode

use of org.apache.servicecomb.registry.discovery.DiscoveryTreeNode in project java-chassis by ServiceComb.

the class IsolationDiscoveryFilter method discovery.

@Override
public DiscoveryTreeNode discovery(DiscoveryContext context, DiscoveryTreeNode parent) {
    Map<String, MicroserviceInstance> instances = parent.data();
    Invocation invocation = context.getInputParameters();
    if (!Configuration.INSTANCE.isIsolationFilterOpen(invocation.getMicroserviceName())) {
        return parent;
    }
    Map<String, MicroserviceInstance> filteredServers = new HashMap<>();
    instances.entrySet().forEach(stringMicroserviceInstanceEntry -> {
        MicroserviceInstance instance = stringMicroserviceInstanceEntry.getValue();
        if (allowVisit(invocation, instance)) {
            filteredServers.put(stringMicroserviceInstanceEntry.getKey(), instance);
        }
    });
    DiscoveryTreeNode child = parent.children().computeIfAbsent("filterred", etn -> new DiscoveryTreeNode());
    if (ZoneAwareDiscoveryFilter.GROUP_Instances_All.equals(context.getContextParameter(ZoneAwareDiscoveryFilter.KEY_ZONE_AWARE_STEP)) && filteredServers.isEmpty() && emptyProtection.get()) {
        LOGGER.warn("All servers have been isolated, allow one of them based on load balance rule.");
        child.data(instances);
    } else {
        child.data(filteredServers);
    }
    return child;
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)

Example 32 with DiscoveryTreeNode

use of org.apache.servicecomb.registry.discovery.DiscoveryTreeNode in project java-chassis by ServiceComb.

the class PriorityInstancePropertyDiscoveryFilter method init.

@Override
protected void init(DiscoveryContext context, DiscoveryTreeNode parent) {
    propertyKey = DynamicPropertyFactory.getInstance().getStringProperty("servicecomb.loadbalance.filter.priorityInstanceProperty.key", "environment").get();
    // group all instance by property
    Map<String, MicroserviceInstance> instances = parent.data();
    Map<String, Map<String, MicroserviceInstance>> groupByProperty = new HashMap<>();
    for (MicroserviceInstance microserviceInstance : instances.values()) {
        String propertyValue = new PriorityInstanceProperty(propertyKey, microserviceInstance).getPropertyValue();
        groupByProperty.computeIfAbsent(propertyValue, key -> new HashMap<>()).put(microserviceInstance.getInstanceId(), microserviceInstance);
    }
    Map<String, DiscoveryTreeNode> children = new HashMap<>();
    for (Map.Entry<String, Map<String, MicroserviceInstance>> entry : groupByProperty.entrySet()) {
        children.put(entry.getKey(), new DiscoveryTreeNode().subName(parent, entry.getKey()).data(entry.getValue()));
    }
    children.put(ALL_INSTANCE, new DiscoveryTreeNode().subName(parent, ALL_INSTANCE).data(instances));
    parent.children(children);
}
Also used : MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) NotNull(javax.validation.constraints.NotNull) StringUtils(org.apache.commons.lang3.StringUtils) DynamicPropertyFactory(com.netflix.config.DynamicPropertyFactory) Invocation(org.apache.servicecomb.core.Invocation) Objects(java.util.Objects) DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) Map(java.util.Map) AbstractDiscoveryFilter(org.apache.servicecomb.registry.discovery.AbstractDiscoveryFilter) Optional(java.util.Optional) RegistrationManager(org.apache.servicecomb.registry.RegistrationManager) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) HashMap(java.util.HashMap) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) HashMap(java.util.HashMap) Map(java.util.Map)

Example 33 with DiscoveryTreeNode

use of org.apache.servicecomb.registry.discovery.DiscoveryTreeNode in project java-chassis by ServiceComb.

the class ZoneAwareDiscoveryFilter method init.

@Override
protected void init(DiscoveryContext context, DiscoveryTreeNode parent) {
    MicroserviceInstance myself = RegistrationManager.INSTANCE.getMicroserviceInstance();
    Map<String, MicroserviceInstance> instancesRegionAndAZMatch = new HashMap<>();
    Map<String, MicroserviceInstance> instancesAZMatch = new HashMap<>();
    Map<String, MicroserviceInstance> instancesNoMatch = new HashMap<>();
    Map<String, MicroserviceInstance> instances = parent.data();
    instances.entrySet().forEach(stringMicroserviceInstanceEntry -> {
        String id = stringMicroserviceInstanceEntry.getKey();
        MicroserviceInstance target = stringMicroserviceInstanceEntry.getValue();
        if (regionAndAZMatch(myself, target)) {
            instancesRegionAndAZMatch.put(id, target);
        } else if (regionMatch(myself, target)) {
            instancesAZMatch.put(id, target);
        } else {
            instancesNoMatch.put(id, target);
        }
    });
    Map<String, DiscoveryTreeNode> children = new HashMap<>();
    children.put(GROUP_RegionAndAZMatch, new DiscoveryTreeNode().subName(parent, GROUP_RegionAndAZMatch).data(instancesRegionAndAZMatch));
    children.put(GROUP_InstancesAZMatch, new DiscoveryTreeNode().subName(parent, GROUP_InstancesAZMatch).data(instancesAZMatch));
    children.put(GROUP_InstancesNoMatch, new DiscoveryTreeNode().subName(parent, GROUP_InstancesNoMatch).data(instancesNoMatch));
    children.put(GROUP_Instances_All, new DiscoveryTreeNode().subName(parent, GROUP_Instances_All).data(instances));
    parent.children(children);
}
Also used : HashMap(java.util.HashMap) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)

Aggregations

DiscoveryTreeNode (org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)33 Test (org.junit.Test)25 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)20 HashMap (java.util.HashMap)11 Invocation (org.apache.servicecomb.core.Invocation)11 ArrayList (java.util.ArrayList)6 NonSwaggerInvocation (org.apache.servicecomb.core.NonSwaggerInvocation)6 Transport (org.apache.servicecomb.core.Transport)6 TransportManager (org.apache.servicecomb.core.transport.TransportManager)6 ServiceCombServer (org.apache.servicecomb.loadbalance.ServiceCombServer)6 InstanceCacheManager (org.apache.servicecomb.registry.cache.InstanceCacheManager)6 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)5 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)5 DataCenterInfo (org.apache.servicecomb.registry.api.registry.DataCenterInfo)5 DiscoveryContext (org.apache.servicecomb.registry.discovery.DiscoveryContext)5 InvocationRuntimeType (org.apache.servicecomb.core.definition.InvocationRuntimeType)4 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)4 ReferenceConfig (org.apache.servicecomb.core.provider.consumer.ReferenceConfig)4 ServiceCombServerStats (org.apache.servicecomb.loadbalance.ServiceCombServerStats)4 TestServiceCombServerStats (org.apache.servicecomb.loadbalance.TestServiceCombServerStats)4