use of org.apache.servicecomb.loadbalance.ServiceCombServer in project incubator-servicecomb-java-chassis by apache.
the class IsolationDiscoveryFilterTest method discovery_try_isolated_instance_after_singleTestTime.
@Test
public void discovery_try_isolated_instance_after_singleTestTime() {
ServiceCombServer server0 = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServer(data.get("i0"));
ServiceCombServerStats serviceCombServerStats = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server0);
for (int i = 0; i < 5; ++i) {
serviceCombServerStats.markFailure();
}
letIsolatedInstancePassSingleTestTime(serviceCombServerStats);
ServiceCombLoadBalancerStats.INSTANCE.markIsolated(server0, true);
Assert.assertTrue(ServiceCombServerStats.isolatedServerCanTry());
Assert.assertNull(TestServiceCombServerStats.getTryingIsolatedServerInvocation());
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.assertTrue(serviceCombServerStats.isIsolated());
Assert.assertFalse(ServiceCombServerStats.isolatedServerCanTry());
Assert.assertSame(invocation, TestServiceCombServerStats.getTryingIsolatedServerInvocation());
}
use of org.apache.servicecomb.loadbalance.ServiceCombServer in project incubator-servicecomb-java-chassis by apache.
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 incubator-servicecomb-java-chassis by apache.
the class DarklaunchServerListFilter method getFilteredListOfServers.
@Override
public List<ServiceCombServer> getFilteredListOfServers(List<ServiceCombServer> serverList, Invocation invocation) {
DynamicStringProperty ruleStr = DynamicPropertyFactory.getInstance().getStringProperty(String.format(POLICY_SERVICE_COMB, invocation.getMicroserviceName()), null);
if (ruleStr == null) {
ruleStr = DynamicPropertyFactory.getInstance().getStringProperty(String.format(POLICY_CSE, invocation.getMicroserviceName()), null);
}
DarklaunchRule rule = DarklaunchRule.parse(ruleStr.get());
if (rule == null) {
return serverList;
}
List<ServiceCombServer> defaultGroup = new ArrayList<>();
divideServerGroup(serverList, rule, defaultGroup);
if (rule.getPolicyType() == PolicyType.RULE) {
for (DarklaunchRuleItem item : rule.getRuleItems()) {
List<ServiceCombServer> ruleServers = getRuleServers(invocation, item, defaultGroup);
if (ruleServers != null) {
return ruleServers;
}
}
} else {
int rate = random.nextInt(HUNDRED);
for (DarklaunchRuleItem item : rule.getRuleItems()) {
item.getPolicyCondition().setActual(DarklaunchRule.PROP_PERCENT, rate);
if (item.getPolicyCondition().match()) {
if (item.getServers().isEmpty()) {
return defaultGroup;
}
return item.getServers();
}
rate = rate - Integer.parseInt(item.getPolicyCondition().expected());
}
}
return defaultGroup;
}
Aggregations