use of org.apache.servicecomb.registry.discovery.DiscoveryTreeNode in project java-chassis by ServiceComb.
the class IsolationDiscoveryFilterTest method discovery_isolate_error_instance.
@Test
public void discovery_isolate_error_instance() {
ServiceCombServer server0 = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServer(data.get("i0"));
for (int i = 0; i < 4; ++i) {
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server0);
}
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"));
// by default 5 times continuous failure will cause isolation
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server0);
Assert.assertFalse(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server0).isIsolated());
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"));
Assert.assertTrue(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server0).isIsolated());
}
use of org.apache.servicecomb.registry.discovery.DiscoveryTreeNode in project java-chassis by ServiceComb.
the class TestLoadBalanceHandler2 method testConfigEndpoint.
@Test
public void testConfigEndpoint() {
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<>());
AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
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();
MicroserviceInstance findInstance = new MicroserviceInstance();
List<String> findEndpoint = new ArrayList<>();
findEndpoint.add("rest://localhost:9092");
findInstance.setEndpoints(findEndpoint);
findInstance.setInstanceId("findInstance");
Map<String, MicroserviceInstance> data = new HashMap<>();
DiscoveryTreeNode parent = new DiscoveryTreeNode().name("parent").data(data);
scbEngine.setTransportManager(transportManager);
SCBEngine.getInstance().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);
data.put("findInstance", findInstance);
parent.cacheVersion(1);
LoadbalanceHandler handler = new LoadbalanceHandler();
try {
handler.handle(invocation, asyncResp);
} catch (Exception e) {
}
Assert.assertEquals("rest://localhost:9092", invocation.getEndpoint().getEndpoint());
// reset
invocation.setEndpoint(null);
// success
invocation.addLocalContext("scb-endpoint", "rest://127.0.0.1:8080?sslEnabled=true&protocol=http2");
try {
handler.handle(invocation, asyncResp);
} catch (Exception e) {
}
Assert.assertEquals("rest://127.0.0.1:8080?sslEnabled=true&protocol=http2", invocation.getEndpoint().getEndpoint());
// reset
invocation.setEndpoint(null);
// endpoint format is not correct
invocation.addLocalContext("scb-endpoint", "127.0.0.1:8080");
try {
handler.handle(invocation, asyncResp);
Assert.assertEquals("endpoint's format is not correct, throw exception", " but not throw exception");
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("Illegal character in scheme name"));
}
// reset
invocation.setEndpoint(null);
// transport is not find
invocation.addLocalContext("scb-endpoint", "my://127.0.0.1:8080?sslEnabled=true&protocol=http2");
try {
handler.handle(invocation, asyncResp);
Assert.assertEquals("endpoint's transport not found, throw exception", "but not throw exception");
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("the endpoint's transport is not found."));
}
}
use of org.apache.servicecomb.registry.discovery.DiscoveryTreeNode in project java-chassis by ServiceComb.
the class TestLoadBalanceHandler2 method testIsolationEventWithEndpoint.
@Test
public void testIsolationEventWithEndpoint() 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");
info.setAvailableZone("test");
myself.setDataCenterInfo(info);
MicroserviceInstance instance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test");
info.setAvailableZone("test");
List<String> allMatchEndpoint = new ArrayList<>();
allMatchEndpoint.add("rest://localhost:9090");
instance.setEndpoints(allMatchEndpoint);
instance.setDataCenterInfo(info);
instance.setInstanceId("instance");
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.assertNull(server);
data.put("instance", instance);
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 greater than 0, it will activate.
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "10");
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.minIsolationTime", "10");
Holder<Integer> count = new Holder<>(0);
IsolationEndpointListener isolationEndpointListener = new IsolationEndpointListener(count);
EventManager.getEventBus().register(isolationEndpointListener);
Assert.assertEquals(0, count.value.intValue());
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
// no server is available
Assert.assertNull(server);
Assert.assertEquals(1, count.value.intValue());
EventManager.unregister(isolationEndpointListener);
}
use of org.apache.servicecomb.registry.discovery.DiscoveryTreeNode in project java-chassis by ServiceComb.
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.DiscoveryTreeNode in project java-chassis by ServiceComb.
the class OperationInstancesDiscoveryFilter method init.
@Override
public void init(DiscoveryContext context, DiscoveryTreeNode parent) {
Invocation invocation = context.getInputParameters();
// sort versions
List<MicroserviceVersion> microserviceVersions = sortedMicroserviceVersion(invocation, parent.data());
Map<String, DiscoveryTreeNode> operationNodes = new ConcurrentHashMapEx<>();
for (MicroserviceVersion microserviceVersion : microserviceVersions) {
MicroserviceMeta microserviceMeta = CoreMetaUtils.getMicroserviceMeta(microserviceVersion);
for (OperationMeta operationMeta : microserviceMeta.getOperations()) {
DiscoveryTreeNode node = operationNodes.get(operationMeta.getMicroserviceQualifiedName());
if (node == null) {
Map<String, MicroserviceInstance> instanceMap = microserviceVersion.getInstances().stream().collect(Collectors.toMap(MicroserviceInstance::getInstanceId, Function.identity()));
DiscoveryTreeNode tempNode = createOperationNode(parent, microserviceVersion);
tempNode.data(instanceMap);
operationNodes.put(operationMeta.getMicroserviceQualifiedName(), tempNode);
continue;
}
// exist, append instances
microserviceVersion.getInstances().forEach(microserviceInstance -> node.mapData().put(microserviceInstance.getInstanceId(), microserviceInstance));
}
}
parent.children(operationNodes);
}
Aggregations