use of org.apache.servicecomb.registry.discovery.DiscoveryContext 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.DiscoveryContext in project incubator-servicecomb-java-chassis by apache.
the class PriorityInstancePropertyDiscoveryFilterTest method executeTest.
private void executeTest(String selfProperty, Set<String> expectedMatchedKeys) {
Invocation invocation = new Invocation();
DiscoveryContext discoveryContext = new DiscoveryContext();
discoveryContext.setInputParameters(invocation);
self.getProperties().put(PROPERTY_KEY, selfProperty);
DiscoveryTreeNode parent = new DiscoveryTreeNode();
parent.name("parent");
parent.data(instances);
DiscoveryTreeNode node = filter.discovery(discoveryContext, parent);
Map<String, MicroserviceInstance> filterInstance = node.data();
assertThat(filterInstance.keySet()).containsAnyElementsOf(expectedMatchedKeys);
}
use of org.apache.servicecomb.registry.discovery.DiscoveryContext in project incubator-servicecomb-java-chassis by apache.
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.DiscoveryContext in project incubator-servicecomb-java-chassis by apache.
the class IsolationDiscoveryFilterTest method before.
@Before
public void before() {
discoveryContext = new DiscoveryContext();
discoveryContext.setInputParameters(invocation);
discoveryTreeNode = new DiscoveryTreeNode();
Mockito.doAnswer(a -> a.getArguments()[0]).when(transport).parseAddress(Mockito.anyString());
data = new HashMap<>();
for (int i = 0; i < 3; ++i) {
MicroserviceInstance instance = new MicroserviceInstance();
instance.setInstanceId("i" + i);
String endpoint = "rest://127.0.0.1:" + i;
instance.setEndpoints(Collections.singletonList(endpoint));
data.put(instance.getInstanceId(), instance);
ServiceCombServer serviceCombServer = new ServiceCombServer(invocation.getMicroserviceName(), transport, new CacheEndpoint(endpoint, instance));
ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer);
}
discoveryTreeNode.data(data);
filter = new IsolationDiscoveryFilter();
TestServiceCombServerStats.releaseTryingChance();
}
use of org.apache.servicecomb.registry.discovery.DiscoveryContext in project incubator-servicecomb-java-chassis by apache.
the class CommonHttpEdgeDispatcher method getOrCreateLoadBalancer.
protected LoadBalancer getOrCreateLoadBalancer(Invocation invocation, String microserviceName, String versionRule) {
DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(invocation);
VersionedCache serversVersionedCache = discoveryTree.discovery(context, RegistrationManager.INSTANCE.getMicroservice().getAppId(), microserviceName, versionRule);
invocation.addLocalContext(LoadbalanceHandler.CONTEXT_KEY_SERVER_LIST, serversVersionedCache.data());
return loadBalancerMap.computeIfAbsent(microserviceName, name -> createLoadBalancer(microserviceName));
}
Aggregations