Search in sources :

Example 26 with ServiceCombServer

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());
}
Also used : ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) ServiceCombServerStats(org.apache.servicecomb.loadbalance.ServiceCombServerStats) TestServiceCombServerStats(org.apache.servicecomb.loadbalance.TestServiceCombServerStats) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) Test(org.junit.Test)

Example 27 with ServiceCombServer

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);
    });
}
Also used : ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) ServerDiscoveryFilter(org.apache.servicecomb.loadbalance.filter.ServerDiscoveryFilter) ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) HttpTransportHttpClientOptionsSPI(org.apache.servicecomb.transport.rest.client.HttpTransportHttpClientOptionsSPI) LoggerFactory(org.slf4j.LoggerFactory) Router(io.vertx.ext.web.Router) HashMap(java.util.HashMap) RequestOptions(io.vertx.core.http.RequestOptions) RoutingContext(io.vertx.ext.web.RoutingContext) HttpClients(org.apache.servicecomb.foundation.vertx.client.http.HttpClients) DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) Map(java.util.Map) LoadbalanceHandler(org.apache.servicecomb.loadbalance.LoadbalanceHandler) ConcurrentHashMapEx(org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx) RuleExt(org.apache.servicecomb.loadbalance.RuleExt) RegistrationManager(org.apache.servicecomb.registry.RegistrationManager) LoadBalancer(org.apache.servicecomb.loadbalance.LoadBalancer) Logger(org.slf4j.Logger) Http2TransportHttpClientOptionsSPI(org.apache.servicecomb.transport.rest.client.Http2TransportHttpClientOptionsSPI) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) Future(io.vertx.core.Future) DynamicPropertyFactory(com.netflix.config.DynamicPropertyFactory) Invocation(org.apache.servicecomb.core.Invocation) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration) Buffer(io.vertx.core.buffer.Buffer) DiscoveryTree(org.apache.servicecomb.registry.discovery.DiscoveryTree) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject) Handler(io.vertx.core.Handler) ExtensionsManager(org.apache.servicecomb.loadbalance.ExtensionsManager) HttpClient(io.vertx.core.http.HttpClient) Invocation(org.apache.servicecomb.core.Invocation) RequestOptions(io.vertx.core.http.RequestOptions) HttpClient(io.vertx.core.http.HttpClient) LoadBalancer(org.apache.servicecomb.loadbalance.LoadBalancer) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject)

Example 28 with ServiceCombServer

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;
}
Also used : ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) DynamicStringProperty(com.netflix.config.DynamicStringProperty) ArrayList(java.util.ArrayList)

Aggregations

ServiceCombServer (org.apache.servicecomb.loadbalance.ServiceCombServer)28 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)14 Test (org.junit.Test)14 DiscoveryTreeNode (org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)12 ServiceCombServerStats (org.apache.servicecomb.loadbalance.ServiceCombServerStats)10 CacheEndpoint (org.apache.servicecomb.registry.cache.CacheEndpoint)10 TestServiceCombServerStats (org.apache.servicecomb.loadbalance.TestServiceCombServerStats)8 Expectations (mockit.Expectations)4 Invocation (org.apache.servicecomb.core.Invocation)4 DiscoveryContext (org.apache.servicecomb.registry.discovery.DiscoveryContext)4 ConcurrentCompositeConfiguration (com.netflix.config.ConcurrentCompositeConfiguration)2 DynamicPropertyFactory (com.netflix.config.DynamicPropertyFactory)2 DynamicStringProperty (com.netflix.config.DynamicStringProperty)2 Future (io.vertx.core.Future)2 Handler (io.vertx.core.Handler)2 Buffer (io.vertx.core.buffer.Buffer)2 HttpClient (io.vertx.core.http.HttpClient)2 RequestOptions (io.vertx.core.http.RequestOptions)2 Router (io.vertx.ext.web.Router)2 RoutingContext (io.vertx.ext.web.RoutingContext)2