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;
}
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);
}
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);
}
Aggregations