Search in sources :

Example 21 with ServiceCombServer

use of org.apache.servicecomb.loadbalance.ServiceCombServer in project java-chassis by ServiceComb.

the class TestServerDiscoveryFilter method createEndpointNormal.

@Test
public void createEndpointNormal(@Injectable DiscoveryContext context, @Injectable Invocation invocation) {
    new Expectations(transportManager) {

        {
            transportManager.findTransport(anyString);
            result = trasport;
            context.getInputParameters();
            result = invocation;
            invocation.getMicroserviceName();
            result = "test";
        }
    };
    MicroserviceInstance instance = new MicroserviceInstance();
    instance.setInstanceId("0000001");
    ServiceCombServer server = (ServiceCombServer) filter.createEndpoint(context, Const.RESTFUL, "rest://localhost:8080", instance);
    Assert.assertSame(instance, server.getInstance());
    Assert.assertSame(trasport, server.getEndpoint().getTransport());
    Assert.assertEquals("rest://localhost:8080", server.getEndpoint().getEndpoint());
}
Also used : Expectations(mockit.Expectations) ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 22 with ServiceCombServer

use of org.apache.servicecomb.loadbalance.ServiceCombServer in project incubator-servicecomb-java-chassis by apache.

the class IsolationDiscoveryFilter method allowVisit.

private boolean allowVisit(Invocation invocation, MicroserviceInstance instance) {
    ServiceCombServer server = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServer(instance);
    if (server == null) {
        // first time accessed.
        return true;
    }
    ServiceCombServerStats serverStats = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server);
    Settings settings = createSettings(invocation);
    if (!checkThresholdAllowed(settings, serverStats)) {
        if (serverStats.isIsolated() && (System.currentTimeMillis() - serverStats.getLastVisitTime()) > settings.singleTestTime) {
            return ServiceCombServerStats.applyForTryingChance(invocation);
        }
        if (!serverStats.isIsolated()) {
            // checkThresholdAllowed is not concurrent control, may print several logs/events in current access.
            serverStats.markIsolated(true);
            eventBus.post(new IsolationServerEvent(invocation, instance, serverStats, settings, Type.OPEN, server.getEndpoint()));
            LOGGER.warn("Isolate service {}'s instance {}.", invocation.getMicroserviceName(), instance.getInstanceId());
        }
        return false;
    }
    if (serverStats.isIsolated()) {
        // high volume of concurrent requests with a percentage of error(e.g. 50%) scenario with no isolation
        if ((System.currentTimeMillis() - serverStats.getIsolatedTime()) <= settings.minIsolationTime) {
            return false;
        }
        serverStats.markIsolated(false);
        eventBus.post(new IsolationServerEvent(invocation, instance, serverStats, settings, Type.CLOSE, server.getEndpoint()));
        LOGGER.warn("Recover service {}'s instance {} from isolation.", invocation.getMicroserviceName(), instance.getInstanceId());
    }
    return true;
}
Also used : ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) IsolationServerEvent(org.apache.servicecomb.loadbalance.event.IsolationServerEvent) ServiceCombServerStats(org.apache.servicecomb.loadbalance.ServiceCombServerStats)

Example 23 with ServiceCombServer

use of org.apache.servicecomb.loadbalance.ServiceCombServer in project incubator-servicecomb-java-chassis by apache.

the class ServerDiscoveryFilter method createEndpoint.

@Override
protected Object createEndpoint(DiscoveryContext context, String transportName, String endpoint, MicroserviceInstance instance) {
    Transport transport = SCBEngine.getInstance().getTransportManager().findTransport(transportName);
    if (transport == null) {
        LOGGER.info("not deployed transport {}, ignore {}.", transportName, endpoint);
        return null;
    }
    Invocation invocation = context.getInputParameters();
    return new ServiceCombServer(invocation.getMicroserviceName(), transport, new CacheEndpoint(endpoint, instance));
}
Also used : ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) Invocation(org.apache.servicecomb.core.Invocation) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) Transport(org.apache.servicecomb.core.Transport)

Example 24 with ServiceCombServer

use of org.apache.servicecomb.loadbalance.ServiceCombServer in project incubator-servicecomb-java-chassis by apache.

the class IsolationDiscoveryFilterTest method before.

@Before
public void before() {
    discoveryContext = new DiscoveryContext();
    discoveryContext.setInputParameters(invocation);
    discoveryTreeNode = new DiscoveryTreeNode();
    Mockito.doAnswer(a -> a.getArguments()[0]).when(transport).parseAddress(Mockito.anyString());
    data = new HashMap<>();
    for (int i = 0; i < 3; ++i) {
        MicroserviceInstance instance = new MicroserviceInstance();
        instance.setInstanceId("i" + i);
        String endpoint = "rest://127.0.0.1:" + i;
        instance.setEndpoints(Collections.singletonList(endpoint));
        data.put(instance.getInstanceId(), instance);
        ServiceCombServer serviceCombServer = new ServiceCombServer(invocation.getMicroserviceName(), transport, new CacheEndpoint(endpoint, instance));
        ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer);
    }
    discoveryTreeNode.data(data);
    filter = new IsolationDiscoveryFilter();
    TestServiceCombServerStats.releaseTryingChance();
}
Also used : ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) Before(org.junit.Before)

Example 25 with ServiceCombServer

use of org.apache.servicecomb.loadbalance.ServiceCombServer in project incubator-servicecomb-java-chassis by apache.

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());
}
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) Test(org.junit.Test)

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