Search in sources :

Example 26 with CacheEndpoint

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

the class TestServiceCombServer method setUp.

@Before
public void setUp() {
    MicroserviceInstance instance = new MicroserviceInstance();
    instance.setInstanceId("123456");
    cs = new ServiceCombServer(null, transport, new CacheEndpoint("abcd", instance));
}
Also used : CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Before(org.junit.Before)

Example 27 with CacheEndpoint

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

the class TestIpPortManager method testGetAvailableAddress.

@Test
public void testGetAvailableAddress(@Injectable ServiceRegistryConfig config, @Injectable InstanceCacheManager cacheManager, @Injectable InstanceCache cache, @Injectable ClassificationAddress classificationAddress) {
    ArrayList<IpPort> ipPortList = new ArrayList<>();
    ipPortList.add(new IpPort("127.0.0.1", 9980));
    ipPortList.add(new IpPort("127.0.0.1", 9981));
    new Expectations() {

        {
            config.getIpPort();
            result = ipPortList;
            config.getTransport();
            result = "rest";
            config.isRegistryAutoDiscovery();
            result = true;
        }
    };
    IpPortManager manager = new IpPortManager(config);
    manager.instanceCacheManager = cacheManager;
    IpPort address1 = manager.getAvailableAddress();
    // test initial
    Assert.assertEquals("127.0.0.1", address1.getHostOrIp());
    Assert.assertTrue(address1.getPort() == 9980 || address1.getPort() == 9981);
    // test getAvailableAddress()
    IpPort address2 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address2.getHostOrIp());
    if (address1.getPort() == 9980) {
        Assert.assertEquals(9981, address2.getPort());
    } else {
        Assert.assertEquals(9980, address2.getPort());
    }
    // test getAvailableAddress() when reaching the end
    IpPort address3 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address3.getHostOrIp());
    Assert.assertEquals(address1.getPort(), address3.getPort());
    // mock endpoint list
    Map<String, List<CacheEndpoint>> addresses = new HashMap<>();
    List<CacheEndpoint> instances = new ArrayList<>();
    instances.add(new CacheEndpoint("http://127.0.0.1:9982", null));
    addresses.put("rest", instances);
    ClassificationAddress classificationAddres = new ClassificationAddress(config, cacheManager);
    manager.classificationAddress = classificationAddres;
    new Expectations() {

        {
            cacheManager.getOrCreate("default", "SERVICECENTER", "latest");
            result = cache;
            cache.getOrCreateTransportMap();
            result = addresses;
        }
    };
    // test getAvailableAddress() when auto discovery is disabled
    IpPort address4 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address4.getHostOrIp());
    if (address1.getPort() == 9980) {
        address4 = manager.getAvailableAddress();
    }
    Assert.assertEquals(9980, address4.getPort());
    IpPort address5 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address5.getHostOrIp());
    Assert.assertEquals(9981, address5.getPort());
    // mock RegistrationManager.INSTANCE
    String instanceId = "e8a04b54cf2711e7b701286ed488fc20";
    MicroserviceInstance microserviceInstance = new MicroserviceInstance();
    microserviceInstance.setInstanceId(instanceId);
    Map<String, String> properties = new HashMap<>();
    microserviceInstance.setProperties(properties);
    new Expectations(RegistrationManager.INSTANCE) {

        {
            RegistrationManager.INSTANCE.getMicroserviceInstance();
            result = microserviceInstance;
        }
    };
    // test getAvailable address when auto discovery is enabled
    manager.initAutoDiscovery();
    manager.setAutoDiscoveryInited(true);
    IpPort address6 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address6.getHostOrIp());
    Assert.assertEquals(9982, address6.getPort());
}
Also used : Expectations(mockit.Expectations) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClassificationAddress(org.apache.servicecomb.serviceregistry.refresh.ClassificationAddress) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 28 with CacheEndpoint

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

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)

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