Search in sources :

Example 1 with CacheEndpoint

use of org.apache.servicecomb.serviceregistry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.

the class IpPortManager method getAvailableAddress.

private IpPort getAvailableAddress(int index) {
    if (index < defaultIpPort.size()) {
        return defaultIpPort.get(index);
    }
    List<CacheEndpoint> endpoints = getDiscoveredIpPort();
    if (endpoints == null || (index >= defaultIpPort.size() + endpoints.size())) {
        currentAvailableIndex.set(0);
        return defaultIpPort.get(0);
    }
    CacheEndpoint nextEndpoint = endpoints.get(index - defaultIpPort.size());
    return new URIEndpointObject(nextEndpoint.getEndpoint());
}
Also used : CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject)

Example 2 with CacheEndpoint

use of org.apache.servicecomb.serviceregistry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.

the class AbstractEndpointsCache method createEndpoints.

protected List<ENDPOINT> createEndpoints(Map<String, List<CacheEndpoint>> transportMap) {
    List<ENDPOINT> tmpEndpoints = new ArrayList<>();
    for (Entry<String, List<CacheEndpoint>> entry : transportMap.entrySet()) {
        Transport transport = transportManager.findTransport(entry.getKey());
        if (transport == null) {
            continue;
        }
        List<CacheEndpoint> endpointList = entry.getValue();
        if (endpointList == null) {
            continue;
        }
        for (CacheEndpoint cacheEndpoint : endpointList) {
            ENDPOINT endpoint = createEndpoint(transport, cacheEndpoint);
            tmpEndpoints.add(endpoint);
        }
    }
    return tmpEndpoints;
}
Also used : CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Transport(org.apache.servicecomb.core.Transport)

Example 3 with CacheEndpoint

use of org.apache.servicecomb.serviceregistry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.

the class TestTransport method testAbstractTransport.

@Test
public void testAbstractTransport(@Mocked Microservice microservice, @Injectable InstanceCacheManager instanceCacheManager, @Injectable TransportManager transportManager, @Mocked InstanceCache instanceCache, @Injectable MicroserviceInstance instance) throws Exception {
    EndpointsCache.init(instanceCacheManager, transportManager);
    EndpointsCache oEndpointsCache = new EndpointsCache("app", "testname", "test", "rest");
    List<Endpoint> endpoionts = oEndpointsCache.getLatestEndpoints();
    Assert.assertEquals(endpoionts.size(), 0);
    Map<String, List<CacheEndpoint>> allTransportMap = new HashMap<>();
    CacheEndpoint cacheEndpoint = new CacheEndpoint("rest://127.0.0.1:9999", instance);
    List<CacheEndpoint> restEndpoints = new ArrayList<>();
    restEndpoints.add(cacheEndpoint);
    allTransportMap.put("rest", restEndpoints);
    new Expectations() {

        {
            instanceCacheManager.getOrCreate(anyString, anyString, anyString);
            result = instanceCache;
            instanceCache.cacheChanged((InstanceCache) any);
            result = true;
            instanceCache.getOrCreateTransportMap();
            result = allTransportMap;
        }
    };
    endpoionts = oEndpointsCache.getLatestEndpoints();
    Assert.assertEquals(endpoionts.size(), 1);
}
Also used : Expectations(mockit.Expectations) CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) HashMap(java.util.HashMap) CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) EndpointsCache(org.apache.servicecomb.core.endpoint.EndpointsCache) Test(org.junit.Test)

Example 4 with CacheEndpoint

use of org.apache.servicecomb.serviceregistry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.

the class TestLoadbalanceHandler method send_success.

@Test
public void send_success() {
    CacheEndpoint cacheEndpoint = new CacheEndpoint("rest://localhost:8080", null);
    CseServer server = new CseServer(restTransport, cacheEndpoint);
    new MockUp<System>() {

        @Mock
        long currentTimeMillis() {
            return 123;
        }
    };
    new Expectations(loadBalancer) {

        {
            loadBalancer.chooseServer(invocation);
            result = server;
        }
    };
    server.incrementContinuousFailureCount();
    sendResponse = Response.ok("success");
    Holder<String> result = new Holder<>();
    Deencapsulation.invoke(handler, "send", invocation, (AsyncResponse) resp -> {
        result.value = resp.getResult();
    }, loadBalancer);
    Assert.assertEquals(123, server.getLastVisitTime());
    Assert.assertEquals(1, loadBalancer.getLoadBalancerStats().getSingleServerStat(server).getActiveRequestsCount());
    Assert.assertEquals("success", result.value);
    Assert.assertEquals(0, server.getCountinuousFailureCount());
}
Also used : Expectations(mockit.Expectations) BeforeClass(org.junit.BeforeClass) Transport(org.apache.servicecomb.core.Transport) RegistryUtils(org.apache.servicecomb.serviceregistry.RegistryUtils) Expectations(mockit.Expectations) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) HashMap(java.util.HashMap) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) SimpleTransactionControlFilter(org.apache.servicecomb.loadbalance.filter.SimpleTransactionControlFilter) ArrayList(java.util.ArrayList) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) Map(java.util.Map) After(org.junit.After) Mock(mockit.Mock) Status(javax.ws.rs.core.Response.Status) InstanceCacheManager(org.apache.servicecomb.serviceregistry.cache.InstanceCacheManager) Response(org.apache.servicecomb.swagger.invocation.Response) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) AfterClass(org.junit.AfterClass) MockUp(mockit.MockUp) Server(com.netflix.loadbalancer.Server) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Deencapsulation(mockit.Deencapsulation) DynamicPropertyFactory(com.netflix.config.DynamicPropertyFactory) Invocation(org.apache.servicecomb.core.Invocation) CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) Mockito(org.mockito.Mockito) List(java.util.List) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) Rule(org.junit.Rule) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) IRule(com.netflix.loadbalancer.IRule) Holder(javax.xml.ws.Holder) TransportManager(org.apache.servicecomb.core.transport.TransportManager) Injectable(mockit.Injectable) CseContext(org.apache.servicecomb.core.CseContext) Assert(org.junit.Assert) Mocked(mockit.Mocked) CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) Holder(javax.xml.ws.Holder) MockUp(mockit.MockUp) Test(org.junit.Test)

Example 5 with CacheEndpoint

use of org.apache.servicecomb.serviceregistry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.

the class TestLoadbalanceHandler method send_failed.

@Test
public void send_failed() {
    CacheEndpoint cacheEndpoint = new CacheEndpoint("rest://localhost:8080", null);
    CseServer server = new CseServer(restTransport, cacheEndpoint);
    new MockUp<System>() {

        @Mock
        long currentTimeMillis() {
            return 123;
        }
    };
    new Expectations(loadBalancer) {

        {
            loadBalancer.chooseServer(invocation);
            result = server;
        }
    };
    int continuousFailureCount = server.getCountinuousFailureCount();
    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);
    Assert.assertEquals(123, server.getLastVisitTime());
    Assert.assertEquals(1, loadBalancer.getLoadBalancerStats().getSingleServerStat(server).getSuccessiveConnectionFailureCount());
    Assert.assertEquals("InvocationException: code=400;msg=send failed", result.value.getMessage());
    Assert.assertEquals(continuousFailureCount + 1, server.getCountinuousFailureCount());
}
Also used : Expectations(mockit.Expectations) BeforeClass(org.junit.BeforeClass) Transport(org.apache.servicecomb.core.Transport) RegistryUtils(org.apache.servicecomb.serviceregistry.RegistryUtils) Expectations(mockit.Expectations) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) HashMap(java.util.HashMap) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) SimpleTransactionControlFilter(org.apache.servicecomb.loadbalance.filter.SimpleTransactionControlFilter) ArrayList(java.util.ArrayList) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) Map(java.util.Map) After(org.junit.After) Mock(mockit.Mock) Status(javax.ws.rs.core.Response.Status) InstanceCacheManager(org.apache.servicecomb.serviceregistry.cache.InstanceCacheManager) Response(org.apache.servicecomb.swagger.invocation.Response) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) AfterClass(org.junit.AfterClass) MockUp(mockit.MockUp) Server(com.netflix.loadbalancer.Server) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Deencapsulation(mockit.Deencapsulation) DynamicPropertyFactory(com.netflix.config.DynamicPropertyFactory) Invocation(org.apache.servicecomb.core.Invocation) CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) Mockito(org.mockito.Mockito) List(java.util.List) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) Rule(org.junit.Rule) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) IRule(com.netflix.loadbalancer.IRule) Holder(javax.xml.ws.Holder) TransportManager(org.apache.servicecomb.core.transport.TransportManager) Injectable(mockit.Injectable) CseContext(org.apache.servicecomb.core.CseContext) Assert(org.junit.Assert) Mocked(mockit.Mocked) CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) Holder(javax.xml.ws.Holder) MockUp(mockit.MockUp) CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) Test(org.junit.Test)

Aggregations

CacheEndpoint (org.apache.servicecomb.serviceregistry.cache.CacheEndpoint)8 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 HashMap (java.util.HashMap)4 Expectations (mockit.Expectations)4 Transport (org.apache.servicecomb.core.Transport)4 Server (com.netflix.loadbalancer.Server)3 DynamicPropertyFactory (com.netflix.config.DynamicPropertyFactory)2 IRule (com.netflix.loadbalancer.IRule)2 Map (java.util.Map)2 Status (javax.ws.rs.core.Response.Status)2 Holder (javax.xml.ws.Holder)2 Deencapsulation (mockit.Deencapsulation)2 Injectable (mockit.Injectable)2 Mock (mockit.Mock)2 MockUp (mockit.MockUp)2 Mocked (mockit.Mocked)2 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)2 ConfigUtil (org.apache.servicecomb.config.ConfigUtil)2