use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class IsolationDiscoveryFilterTest method discovery_not_try_isolated_instance_concurrently.
@Test
public void discovery_not_try_isolated_instance_concurrently() {
ServiceCombServer server0 = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServer(data.get("i0"));
ServiceCombServerStats serviceCombServerStats = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server0);
for (int i = 0; i < 5; ++i) {
serviceCombServerStats.markFailure();
}
ServiceCombLoadBalancerStats.INSTANCE.markIsolated(server0, true);
letIsolatedInstancePassSingleTestTime(serviceCombServerStats);
Assert.assertTrue(ServiceCombServerStats.isolatedServerCanTry());
// The first invocation can occupy the trying chance
DiscoveryTreeNode childNode = filter.discovery(discoveryContext, discoveryTreeNode);
Map<String, MicroserviceInstance> childNodeData = childNode.data();
Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i0", "i1", "i2"));
Assert.assertEquals(data.get("i0"), childNodeData.get("i0"));
Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
Assert.assertFalse(ServiceCombServerStats.isolatedServerCanTry());
// Other invocation cannot get trying chance concurrently
childNode = filter.discovery(discoveryContext, discoveryTreeNode);
childNodeData = childNode.data();
Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i1", "i2"));
Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
ServiceCombServerStats.checkAndReleaseTryingChance(// after the first invocation releases the trying chance
invocation);
// Other invocation can get the trying chance
childNode = filter.discovery(discoveryContext, discoveryTreeNode);
childNodeData = childNode.data();
Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i0", "i1", "i2"));
Assert.assertEquals(data.get("i0"), childNodeData.get("i0"));
Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
Assert.assertFalse(ServiceCombServerStats.isolatedServerCanTry());
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestServiceCombServer method testEqualsMethod.
@Test
public void testEqualsMethod() {
Assert.assertFalse(cs.equals((Object) "abcd"));
MicroserviceInstance instance1 = new MicroserviceInstance();
instance1.setInstanceId("1234");
ServiceCombServer other = new ServiceCombServer(null, transport, new CacheEndpoint("1234", instance1));
Assert.assertFalse(cs.equals(other));
MicroserviceInstance instance2 = new MicroserviceInstance();
instance2.setInstanceId("123456");
other = new ServiceCombServer(null, transport, new CacheEndpoint("abcd", instance2));
Assert.assertTrue(cs.equals(other));
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
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.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestLoadBalanceHandler2 method testZoneAwareAndIsolationFilterWorks.
@Test
public void testZoneAwareAndIsolationFilterWorks() throws Exception {
ReferenceConfig referenceConfig = Mockito.mock(ReferenceConfig.class);
OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
InvocationRuntimeType invocationRuntimeType = Mockito.mock(InvocationRuntimeType.class);
SchemaMeta schemaMeta = Mockito.mock(SchemaMeta.class);
when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
MicroserviceMeta microserviceMeta = Mockito.mock(MicroserviceMeta.class);
when(schemaMeta.getMicroserviceMeta()).thenReturn(microserviceMeta);
when(schemaMeta.getMicroserviceName()).thenReturn("testMicroserviceName");
when(microserviceMeta.getAppId()).thenReturn("testApp");
when(referenceConfig.getVersionRule()).thenReturn("0.0.0+");
when(referenceConfig.getTransport()).thenReturn("rest");
Invocation invocation = new Invocation(referenceConfig, operationMeta, invocationRuntimeType, new HashMap<>());
InstanceCacheManager instanceCacheManager = Mockito.mock(InstanceCacheManager.class);
TransportManager transportManager = Mockito.mock(TransportManager.class);
Transport transport = Mockito.mock(Transport.class);
ArchaiusUtils.setProperty("servicecomb.loadbalance.filter.operation.enabled", "false");
// set up data
MicroserviceInstance myself = new MicroserviceInstance();
DataCenterInfo info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region");
info.setAvailableZone("test-zone");
myself.setDataCenterInfo(info);
MicroserviceInstance allmatchInstance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region");
info.setAvailableZone("test-zone");
List<String> allMatchEndpoint = new ArrayList<>();
allMatchEndpoint.add("rest://localhost:9090");
allmatchInstance.setEndpoints(allMatchEndpoint);
allmatchInstance.setDataCenterInfo(info);
allmatchInstance.setInstanceId("allmatchInstance");
MicroserviceInstance regionMatchInstance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region");
info.setAvailableZone("test-zone2");
List<String> regionMatchEndpoint = new ArrayList<>();
regionMatchEndpoint.add("rest://localhost:9091");
regionMatchInstance.setEndpoints(regionMatchEndpoint);
regionMatchInstance.setDataCenterInfo(info);
regionMatchInstance.setInstanceId("regionMatchInstance");
MicroserviceInstance noneMatchInstance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region2");
info.setAvailableZone("test-zone2");
List<String> noMatchEndpoint = new ArrayList<>();
noMatchEndpoint.add("rest://localhost:9092");
noneMatchInstance.setEndpoints(noMatchEndpoint);
noneMatchInstance.setDataCenterInfo(info);
noneMatchInstance.setInstanceId("noneMatchInstance");
Map<String, MicroserviceInstance> data = new HashMap<>();
DiscoveryTreeNode parent = new DiscoveryTreeNode().name("parent").data(data);
scbEngine.setTransportManager(transportManager);
LocalRegistryStore.INSTANCE.initSelfWithMocked(null, myself);
mockUpInstanceCacheManager(instanceCacheManager);
when(instanceCacheManager.getOrCreateVersionedCache("testApp", "testMicroserviceName", "0.0.0+")).thenReturn(parent);
when(transportManager.findTransport("rest")).thenReturn(transport);
LoadbalanceHandler handler = null;
LoadBalancer loadBalancer = null;
ServiceCombServer server = null;
handler = new LoadbalanceHandler();
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals(null, server);
data.put("noneMatchInstance", noneMatchInstance);
parent.cacheVersion(1);
handler = new LoadbalanceHandler();
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9092", server.getEndpoint().getEndpoint());
data.put("regionMatchInstance", regionMatchInstance);
parent.cacheVersion(parent.cacheVersion() + 1);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9091", server.getEndpoint().getEndpoint());
data.put("allmatchInstance", allmatchInstance);
parent.cacheVersion(parent.cacheVersion() + 1);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9090", server.getEndpoint().getEndpoint());
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server);
// if errorThresholdPercentage is 0,that means errorThresholdPercentage is not active.
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "0");
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9090", server.getEndpoint().getEndpoint());
// if errorThresholdPercentage greater than 0, it will activate.
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "20");
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.minIsolationTime", "10");
ServiceCombServer server2 = server;
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9091", server.getEndpoint().getEndpoint());
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server2);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9091", server.getEndpoint().getEndpoint());
mockDelayMillis(20);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9090", server.getEndpoint().getEndpoint());
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9091", server.getEndpoint().getEndpoint());
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class InstancePropertyDiscoveryFilter method init.
@Override
protected void init(DiscoveryContext context, DiscoveryTreeNode parent) {
Map<String, MicroserviceInstance> matchedInstance = new HashMap<>();
Invocation invocation = context.getInputParameters();
Map<String, MicroserviceInstance> instances = parent.data();
Map<String, String> filterOptions = Configuration.INSTANCE.getFlowsplitFilterOptions(invocation.getMicroserviceName());
instances.entrySet().forEach(stringMicroserviceInstanceEntry -> {
MicroserviceInstance target = stringMicroserviceInstanceEntry.getValue();
if (allowVisit(target, filterOptions)) {
matchedInstance.put(stringMicroserviceInstanceEntry.getKey(), target);
}
});
parent.child(MATCHED, new DiscoveryTreeNode().subName(parent, MATCHED).data(matchedInstance));
}
Aggregations