use of org.apache.servicecomb.loadbalance.ServiceCombServer in project incubator-servicecomb-java-chassis by apache.
the class DarklaunchServerListFilter method divideServerGroup.
private void divideServerGroup(List<ServiceCombServer> serverList, DarklaunchRule rule, List<ServiceCombServer> defaultGroup) {
for (ServiceCombServer server : serverList) {
boolean hasGroup = false;
for (DarklaunchRuleItem item : rule.getRuleItems()) {
Microservice microservice = MicroserviceCache.getInstance().getService(server.getInstance().getServiceId());
item.getGroupCondition().setActual(DarklaunchRule.PROP_VERSION, microservice.getVersion());
if (item.getGroupCondition().match()) {
item.addServer(server);
hasGroup = true;
}
}
if (!hasGroup) {
defaultGroup.add(server);
}
}
}
use of org.apache.servicecomb.loadbalance.ServiceCombServer in project incubator-servicecomb-java-chassis by apache.
the class TestClientTimeout method getServiceCombServerStats.
private static ServiceCombServerStats getServiceCombServerStats() {
InstanceCache instanceCache = DiscoveryManager.INSTANCE.getInstanceCacheManager().getOrCreate(RegistrationManager.INSTANCE.getAppId(), "jaxrs", "0+");
org.apache.servicecomb.registry.api.registry.MicroserviceInstance microserviceInstance = instanceCache.getInstanceMap().values().iterator().next();
ServiceCombServer serviceCombServer = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServer(microserviceInstance);
return ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer);
}
use of org.apache.servicecomb.loadbalance.ServiceCombServer in project java-chassis by ServiceComb.
the class CommonHttpEdgeDispatcher method onRequest.
protected void onRequest(RoutingContext context) {
URLMappedConfigurationItem configurationItem = findConfigurationItem(context.request().uri());
if (configurationItem == null) {
context.next();
return;
}
String uri = Utils.findActualPath(context.request().uri(), configurationItem.getPrefixSegmentCount());
Invocation invocation = new Invocation() {
@Override
public String getConfigTransportName() {
return "rest";
}
@Override
public String getMicroserviceName() {
return configurationItem.getMicroserviceName();
}
};
LoadBalancer loadBalancer = getOrCreateLoadBalancer(invocation, configurationItem.getMicroserviceName(), configurationItem.getVersionRule());
ServiceCombServer server = loadBalancer.chooseServer(invocation);
if (server == null) {
LOG.warn("no available server for service {}", configurationItem.getMicroserviceName());
serverNotReadyResponse(context);
return;
}
URIEndpointObject endpointObject = new URIEndpointObject(server.getEndpoint().getEndpoint());
RequestOptions requestOptions = new RequestOptions();
requestOptions.setHost(endpointObject.getHostOrIp()).setPort(endpointObject.getPort()).setSsl(endpointObject.isSslEnabled()).setMethod(context.request().method()).setURI(uri);
HttpClient httpClient;
if (endpointObject.isHttp2Enabled()) {
httpClient = HttpClients.getClient(Http2TransportHttpClientOptionsSPI.CLIENT_NAME, false).getHttpClient();
} else {
httpClient = HttpClients.getClient(HttpTransportHttpClientOptionsSPI.CLIENT_NAME, false).getHttpClient();
}
context.request().pause();
httpClient.request(requestOptions).compose(httpClientRequest -> {
context.request().headers().forEach((header) -> httpClientRequest.headers().set(header.getKey(), header.getValue()));
context.request().resume();
context.request().handler(httpClientRequest::write);
context.request().endHandler((v) -> httpClientRequest.end());
return httpClientRequest.response().compose(httpClientResponse -> {
context.response().setStatusCode(httpClientResponse.statusCode());
httpClientResponse.headers().forEach((header) -> context.response().headers().set(header.getKey(), header.getValue()));
httpClientResponse.handler(this.responseHandler(context));
httpClientResponse.endHandler((v) -> context.response().end());
return Future.succeededFuture();
});
}).onFailure(failure -> {
LOG.warn("send request to target {}:{} failed, cause {}", endpointObject.getHostOrIp(), endpointObject.getPort(), failure.getMessage());
serverNotReadyResponse(context);
});
}
use of org.apache.servicecomb.loadbalance.ServiceCombServer in project java-chassis by ServiceComb.
the class IsolationDiscoveryFilterTest method discovery_recover_instance.
@Test
public void discovery_recover_instance() {
ServiceCombServer server0 = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServer(data.get("i0"));
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server0);
ServiceCombServerStats serviceCombServerStats = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server0);
ServiceCombLoadBalancerStats.INSTANCE.markIsolated(server0, true);
Deencapsulation.setField(serviceCombServerStats, "isolatedTime", System.currentTimeMillis() - Configuration.INSTANCE.getMinIsolationTime(invocation.getMicroserviceName()) - 1);
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(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server0).isIsolated());
}
use of org.apache.servicecomb.loadbalance.ServiceCombServer 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());
}
Aggregations