Search in sources :

Example 6 with CacheEndpoint

use of org.apache.servicecomb.registry.cache.CacheEndpoint in project java-chassis by ServiceComb.

the class ClassificationAddress method generateZoneAndRegionAddress.

private Map<String, List<String>> generateZoneAndRegionAddress(String key) {
    InstanceCache KieCaches = instanceCacheManager.getOrCreate(REGISTRY_APP_ID, key, DefinitionConst.VERSION_RULE_LATEST);
    List<CacheEndpoint> CacheEndpoints = new ArrayList<>();
    if (REGISTRY_SERVICE_NAME.equals(key)) {
        CacheEndpoints = KieCaches.getOrCreateTransportMap().get(defaultTransport);
        maxRetryTimes = CacheEndpoints.size();
    } else {
        if (KieCaches.getInstanceMap().size() <= 0) {
            return null;
        }
        CacheEndpoints = KieCaches.getOrCreateTransportMap().get(defaultTransport);
    }
    Map<String, List<String>> zoneAndRegion = new HashMap<>();
    dataCenterInfo = findRegion(CacheEndpoints);
    Set<String> sameZone = new HashSet<>();
    Set<String> sameRegion = new HashSet<>();
    for (CacheEndpoint cacheEndpoint : CacheEndpoints) {
        if (regionAndAZMatch(dataCenterInfo, cacheEndpoint.getInstance())) {
            sameZone.add(cacheEndpoint.getEndpoint());
        } else {
            sameRegion.add(cacheEndpoint.getEndpoint());
        }
    }
    zoneAndRegion.put("sameZone", new ArrayList<>(sameZone));
    zoneAndRegion.put("sameRegion", new ArrayList<>(sameRegion));
    return zoneAndRegion;
}
Also used : CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InstanceCache(org.apache.servicecomb.registry.cache.InstanceCache) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 7 with CacheEndpoint

use of org.apache.servicecomb.registry.cache.CacheEndpoint in project java-chassis by ServiceComb.

the class TestLoadbalanceHandler method send_success.

@Test
public void send_success(@Injectable LoadBalancer loadBalancer) {
    MicroserviceInstance instance1 = new MicroserviceInstance();
    instance1.setInstanceId("1234");
    CacheEndpoint cacheEndpoint = new CacheEndpoint("rest://localhost:8080", instance1);
    ServiceCombServer server = new ServiceCombServer(null, restTransport, cacheEndpoint);
    LoadBalancerStats stats = new LoadBalancerStats("test");
    new Expectations(loadBalancer) {

        {
            loadBalancer.chooseServer(invocation);
            result = server;
            loadBalancer.getLoadBalancerStats();
            result = stats;
        }
    };
    sendResponse = Response.ok("success");
    Holder<String> result = new Holder<>();
    Deencapsulation.invoke(handler, "send", invocation, (AsyncResponse) resp -> {
        result.value = resp.getResult();
    }, loadBalancer);
    Assert.assertEquals(1, loadBalancer.getLoadBalancerStats().getSingleServerStat(server).getActiveRequestsCount());
    Assert.assertEquals("success", result.value);
}
Also used : Expectations(mockit.Expectations) SCBEngine(org.apache.servicecomb.core.SCBEngine) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Transport(org.apache.servicecomb.core.Transport) Expectations(mockit.Expectations) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) DiscoveryFilter(org.apache.servicecomb.registry.discovery.DiscoveryFilter) SPIServiceUtils(org.apache.servicecomb.foundation.common.utils.SPIServiceUtils) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) ArrayList(java.util.ArrayList) SocketException(java.net.SocketException) Map(java.util.Map) After(org.junit.After) Mock(mockit.Mock) SCBBootstrap(org.apache.servicecomb.core.bootstrap.SCBBootstrap) Status(javax.ws.rs.core.Response.Status) Response(org.apache.servicecomb.swagger.invocation.Response) ExpectedException(org.junit.rules.ExpectedException) ExecutorService(java.util.concurrent.ExecutorService) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats) Before(org.junit.Before) MockUp(mockit.MockUp) InstanceCacheManager(org.apache.servicecomb.registry.cache.InstanceCacheManager) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Matchers(org.hamcrest.Matchers) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test) Deencapsulation(mockit.Deencapsulation) Invocation(org.apache.servicecomb.core.Invocation) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Rule(org.junit.Rule) TransportManager(org.apache.servicecomb.core.transport.TransportManager) Injectable(mockit.Injectable) Assert(org.junit.Assert) Collections(java.util.Collections) Mocked(mockit.Mocked) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) Holder(org.apache.servicecomb.foundation.common.Holder) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 8 with CacheEndpoint

use of org.apache.servicecomb.registry.cache.CacheEndpoint in project java-chassis by ServiceComb.

the class TestLoadbalanceHandler method send_failed2.

@Test
public void send_failed2(@Injectable LoadBalancer loadBalancer) {
    MicroserviceInstance instance1 = new MicroserviceInstance();
    instance1.setInstanceId("1234");
    CacheEndpoint cacheEndpoint = new CacheEndpoint("rest://localhost:8080", instance1);
    ServiceCombServer server = new ServiceCombServer(null, restTransport, cacheEndpoint);
    LoadBalancerStats stats = new LoadBalancerStats("test");
    new Expectations(loadBalancer) {

        {
            loadBalancer.chooseServer(invocation);
            result = server;
            loadBalancer.getLoadBalancerStats();
            result = stats;
        }
    };
    sendResponse = Response.create(Status.BAD_REQUEST, "send failed");
    Holder<Throwable> result = new Holder<>();
    Deencapsulation.invoke(handler, "send", invocation, (AsyncResponse) resp -> {
        result.value = (Throwable) resp.getResult();
    }, loadBalancer);
    // InvocationException is not taken as a failure
    Assert.assertEquals(0, loadBalancer.getLoadBalancerStats().getSingleServerStat(server).getSuccessiveConnectionFailureCount());
    Assert.assertEquals("InvocationException: code=400;msg=send failed", result.value.getMessage());
}
Also used : Expectations(mockit.Expectations) SCBEngine(org.apache.servicecomb.core.SCBEngine) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Transport(org.apache.servicecomb.core.Transport) Expectations(mockit.Expectations) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) DiscoveryFilter(org.apache.servicecomb.registry.discovery.DiscoveryFilter) SPIServiceUtils(org.apache.servicecomb.foundation.common.utils.SPIServiceUtils) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) ArrayList(java.util.ArrayList) SocketException(java.net.SocketException) Map(java.util.Map) After(org.junit.After) Mock(mockit.Mock) SCBBootstrap(org.apache.servicecomb.core.bootstrap.SCBBootstrap) Status(javax.ws.rs.core.Response.Status) Response(org.apache.servicecomb.swagger.invocation.Response) ExpectedException(org.junit.rules.ExpectedException) ExecutorService(java.util.concurrent.ExecutorService) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats) Before(org.junit.Before) MockUp(mockit.MockUp) InstanceCacheManager(org.apache.servicecomb.registry.cache.InstanceCacheManager) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Matchers(org.hamcrest.Matchers) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test) Deencapsulation(mockit.Deencapsulation) Invocation(org.apache.servicecomb.core.Invocation) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Rule(org.junit.Rule) TransportManager(org.apache.servicecomb.core.transport.TransportManager) Injectable(mockit.Injectable) Assert(org.junit.Assert) Collections(java.util.Collections) Mocked(mockit.Mocked) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) Holder(org.apache.servicecomb.foundation.common.Holder) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 9 with CacheEndpoint

use of org.apache.servicecomb.registry.cache.CacheEndpoint in project java-chassis by ServiceComb.

the class TestServiceCombLoadBalancerStats method testMultiThread.

@Test
public void testMultiThread(@Injectable Transport transport) throws Exception {
    long time = System.currentTimeMillis();
    MicroserviceInstance instance = new MicroserviceInstance();
    instance.setInstanceId("instance2");
    ServiceCombServer serviceCombServer = new ServiceCombServer(null, transport, new CacheEndpoint("rest://localhost:8080", instance));
    CountDownLatch latch = new CountDownLatch(10);
    for (int i = 0; i < 10; i++) {
        new Thread(() -> {
            ServiceCombLoadBalancerStats.INSTANCE.markFailure(serviceCombServer);
            ServiceCombLoadBalancerStats.INSTANCE.markFailure(serviceCombServer);
            ServiceCombLoadBalancerStats.INSTANCE.markSuccess(serviceCombServer);
            ServiceCombLoadBalancerStats.INSTANCE.markSuccess(serviceCombServer);
            latch.countDown();
        }).start();
    }
    latch.await(30, TimeUnit.SECONDS);
    Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getTotalRequests(), 4 * 10);
    Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getFailedRate(), 50);
    Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getSuccessRate(), 50);
    Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getSuccessRequests(), 20);
    Assert.assertTrue(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getLastVisitTime() <= System.currentTimeMillis() && ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getLastVisitTime() >= time);
    // time consuming test for timers, taking about 20 seconds. ping timer will update instance status to failure
    Assert.assertTrue(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getFailedRate() <= 50);
    long beginTime = System.currentTimeMillis();
    long rate = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getFailedRequests();
    while (rate <= 20 && System.currentTimeMillis() - beginTime <= 30000) {
        Thread.sleep(2000);
        rate = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getFailedRequests();
        System.out.println("failedRequests: " + rate);
    }
    Assert.assertTrue(System.currentTimeMillis() - beginTime < 30000);
    Assert.assertTrue(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getFailedRequests() > 20);
}
Also used : CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) Test(org.junit.Test)

Example 10 with CacheEndpoint

use of org.apache.servicecomb.registry.cache.CacheEndpoint in project java-chassis by ServiceComb.

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)

Aggregations

CacheEndpoint (org.apache.servicecomb.registry.cache.CacheEndpoint)28 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)24 Test (org.junit.Test)20 Invocation (org.apache.servicecomb.core.Invocation)12 Transport (org.apache.servicecomb.core.Transport)12 ArrayList (java.util.ArrayList)10 List (java.util.List)10 Expectations (mockit.Expectations)10 Before (org.junit.Before)10 LoadBalancerStats (com.netflix.loadbalancer.LoadBalancerStats)8 MockUp (mockit.MockUp)8 SocketException (java.net.SocketException)6 Collections (java.util.Collections)6 Map (java.util.Map)6 ExecutionException (java.util.concurrent.ExecutionException)6 ExecutorService (java.util.concurrent.ExecutorService)6 Status (javax.ws.rs.core.Response.Status)6 Deencapsulation (mockit.Deencapsulation)6 Injectable (mockit.Injectable)6 Mock (mockit.Mock)6