Search in sources :

Example 36 with MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project java-chassis by ServiceComb.

the class TestLoadBalanceCreator method testLoadBalanceWithSessionSticknessRule.

@Test
public void testLoadBalanceWithSessionSticknessRule(@Injectable Invocation invocation, @Injectable Transport transport) {
    SessionStickinessRule rule = new SessionStickinessRule();
    LoadBalancer lb = new LoadBalancer(rule, "service");
    List<ServiceCombServer> servers = new ArrayList<>();
    Endpoint host1 = new Endpoint(transport, "host1");
    MicroserviceInstance instance1 = new MicroserviceInstance();
    ServiceCombServer server = new ServiceCombServer(null, host1, instance1);
    instance1.setInstanceId("instance1");
    Endpoint host2 = new Endpoint(transport, "host2");
    MicroserviceInstance instance2 = new MicroserviceInstance();
    ServiceCombServer server2 = new ServiceCombServer(null, host2, instance2);
    instance2.setInstanceId("instance2");
    servers.add(server);
    servers.add(server2);
    lb.setFilters(new ArrayList<>());
    new Expectations() {

        {
            invocation.getLocalContext(LoadbalanceHandler.CONTEXT_KEY_SERVER_LIST);
            result = servers;
        }
    };
    Server s = lb.chooseServer(invocation);
    Assert.assertEquals(server, s);
    s = lb.chooseServer(invocation);
    Assert.assertEquals(server, s);
    long time = Deencapsulation.getField(rule, "lastAccessedTime");
    Deencapsulation.setField(rule, "lastAccessedTime", time - 1000 * 300);
    ArchaiusUtils.setProperty("cse.loadbalance.service.SessionStickinessRule.sessionTimeoutInSeconds", 9);
    s = lb.chooseServer(invocation);
    Assert.assertEquals(server2, s);
    ArchaiusUtils.setProperty("cse.loadbalance.service.SessionStickinessRule.successiveFailedTimes", 5);
    lb.getLoadBalancerStats().incrementSuccessiveConnectionFailureCount(s);
    lb.getLoadBalancerStats().incrementSuccessiveConnectionFailureCount(s);
    lb.getLoadBalancerStats().incrementSuccessiveConnectionFailureCount(s);
    lb.getLoadBalancerStats().incrementSuccessiveConnectionFailureCount(s);
    lb.getLoadBalancerStats().incrementSuccessiveConnectionFailureCount(s);
    s = lb.chooseServer(invocation);
    Assert.assertEquals(server, s);
}
Also used : Expectations(mockit.Expectations) Endpoint(org.apache.servicecomb.core.Endpoint) Server(com.netflix.loadbalancer.Server) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 37 with MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project java-chassis by ServiceComb.

the class TestLoadBalanceCreator method testLoadBalanceWithRandomRuleAndFilter.

@Test
public void testLoadBalanceWithRandomRuleAndFilter(@Injectable Invocation invocation, @Injectable Transport transport) {
    // Robin components implementations require getReachableServers & getServerList have the same size, we add a test case for this.
    RandomRuleExt rule = new RandomRuleExt();
    LoadBalancer lb = new LoadBalancer(rule, "service");
    List<ServiceCombServer> servers = new ArrayList<>();
    Endpoint host1 = new Endpoint(transport, "host1");
    MicroserviceInstance instance1 = new MicroserviceInstance();
    instance1.setInstanceId("instance1");
    ServiceCombServer server = new ServiceCombServer(null, host1, instance1);
    Endpoint host2 = new Endpoint(transport, "host2");
    MicroserviceInstance instance2 = new MicroserviceInstance();
    instance2.setInstanceId("instance2");
    ServiceCombServer server2 = new ServiceCombServer(null, host2, instance2);
    servers.add(server);
    servers.add(server2);
    List<ServerListFilterExt> filters = new ArrayList<>();
    filters.add(new ServerListFilterExt() {

        @Override
        public List<ServiceCombServer> getFilteredListOfServers(List<ServiceCombServer> serverList, Invocation invocation) {
            List<ServiceCombServer> filteredServers = new ArrayList<>();
            for (ServiceCombServer server : servers) {
                if (server.getHost().equals("host1")) {
                    continue;
                }
                filteredServers.add(server);
            }
            return filteredServers;
        }
    });
    lb.setFilters(filters);
    new Expectations() {

        {
            invocation.getLocalContext(LoadbalanceHandler.CONTEXT_KEY_SERVER_LIST);
            result = servers;
        }
    };
    Server s = lb.chooseServer(invocation);
    Assert.assertEquals(server2, s);
    s = lb.chooseServer(invocation);
    Assert.assertEquals(server2, s);
    s = lb.chooseServer(invocation);
    Assert.assertEquals(server2, s);
}
Also used : Expectations(mockit.Expectations) Invocation(org.apache.servicecomb.core.Invocation) Server(com.netflix.loadbalancer.Server) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Endpoint(org.apache.servicecomb.core.Endpoint) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 38 with MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project java-chassis by ServiceComb.

the class InstancePropertyDiscoveryFilter method init.

@Override
protected void init(DiscoveryContext context, DiscoveryTreeNode parent) {
    Map<String, MicroserviceInstance> matchedInstance = new HashMap<>();
    Invocation invocation = context.getInputParameters();
    Map<String, MicroserviceInstance> instances = parent.data();
    Map<String, String> filterOptions = Configuration.INSTANCE.getFlowsplitFilterOptions(invocation.getMicroserviceName());
    instances.entrySet().forEach(stringMicroserviceInstanceEntry -> {
        MicroserviceInstance target = stringMicroserviceInstanceEntry.getValue();
        if (allowVisit(target, filterOptions)) {
            matchedInstance.put(stringMicroserviceInstanceEntry.getKey(), target);
        }
    });
    parent.child(MATCHED, new DiscoveryTreeNode().subName(parent, MATCHED).data(matchedInstance));
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)

Example 39 with MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project java-chassis by ServiceComb.

the class TestLoadBalanceHandler2 method testZoneAwareAndIsolationFilterUsingMockedInvocationWorks.

@Test
public void testZoneAwareAndIsolationFilterUsingMockedInvocationWorks() throws Exception {
    Invocation invocation = new NonSwaggerInvocation("testApp", "testMicroserviceName", "0.0.0+", (inv, aysnc) -> {
        aysnc.success("OK");
    });
    InstanceCacheManager instanceCacheManager = Mockito.mock(InstanceCacheManager.class);
    TransportManager transportManager = Mockito.mock(TransportManager.class);
    Transport transport = Mockito.mock(Transport.class);
    ArchaiusUtils.setProperty("servicecomb.loadbalance.filter.operation.enabled", "false");
    // set up data
    MicroserviceInstance myself = new MicroserviceInstance();
    DataCenterInfo info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region");
    info.setAvailableZone("test-zone");
    myself.setDataCenterInfo(info);
    MicroserviceInstance allmatchInstance = new MicroserviceInstance();
    info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region");
    info.setAvailableZone("test-zone");
    List<String> allMatchEndpoint = new ArrayList<>();
    allMatchEndpoint.add("rest://localhost:7090");
    allmatchInstance.setEndpoints(allMatchEndpoint);
    allmatchInstance.setDataCenterInfo(info);
    allmatchInstance.setInstanceId("allmatchInstance");
    MicroserviceInstance regionMatchInstance = new MicroserviceInstance();
    info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region");
    info.setAvailableZone("test-zone2");
    List<String> regionMatchEndpoint = new ArrayList<>();
    regionMatchEndpoint.add("rest://localhost:7091");
    regionMatchInstance.setEndpoints(regionMatchEndpoint);
    regionMatchInstance.setDataCenterInfo(info);
    regionMatchInstance.setInstanceId("regionMatchInstance");
    MicroserviceInstance noneMatchInstance = new MicroserviceInstance();
    info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region2");
    info.setAvailableZone("test-zone2");
    List<String> noMatchEndpoint = new ArrayList<>();
    noMatchEndpoint.add("rest://localhost:7092");
    noneMatchInstance.setEndpoints(noMatchEndpoint);
    noneMatchInstance.setDataCenterInfo(info);
    noneMatchInstance.setInstanceId("noneMatchInstance");
    Map<String, MicroserviceInstance> data = new HashMap<>();
    DiscoveryTreeNode parent = new DiscoveryTreeNode().name("parent").data(data);
    scbEngine.setTransportManager(transportManager);
    LocalRegistryStore.INSTANCE.initSelfWithMocked(null, myself);
    mockUpInstanceCacheManager(instanceCacheManager);
    when(instanceCacheManager.getOrCreateVersionedCache("testApp", "testMicroserviceName", "0.0.0+")).thenReturn(parent);
    when(transportManager.findTransport("rest")).thenReturn(transport);
    LoadbalanceHandler handler = null;
    LoadBalancer loadBalancer = null;
    ServiceCombServer server = null;
    DiscoveryTree discoveryTree = new DiscoveryTree();
    discoveryTree.addFilter(new IsolationDiscoveryFilter());
    discoveryTree.addFilter(new ZoneAwareDiscoveryFilter());
    discoveryTree.addFilter(new ServerDiscoveryFilter());
    discoveryTree.sort();
    handler = new LoadbalanceHandler(discoveryTree);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals(null, server);
    data.put("noneMatchInstance", noneMatchInstance);
    parent.cacheVersion(1);
    handler = new LoadbalanceHandler();
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7092", server.getEndpoint().getEndpoint());
    handler.handle(invocation, (response) -> {
        Assert.assertEquals("OK", response.getResult());
    });
    data.put("regionMatchInstance", regionMatchInstance);
    parent.cacheVersion(parent.cacheVersion() + 1);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
    handler.handle(invocation, (response) -> {
        Assert.assertEquals("OK", response.getResult());
    });
    data.put("allmatchInstance", allmatchInstance);
    parent.cacheVersion(parent.cacheVersion() + 1);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7090", server.getEndpoint().getEndpoint());
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
    ServiceCombLoadBalancerStats.INSTANCE.markFailure(server);
    // if errorThresholdPercentage is 0,that means errorThresholdPercentage is not active.
    ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "0");
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7090", server.getEndpoint().getEndpoint());
    // if errorThresholdPercentage greater than 0, it will activate.
    ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "20");
    ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.minIsolationTime", "30");
    ServiceCombServer server2 = server;
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server2);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
    mockDelayMillis(31);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7090", server.getEndpoint().getEndpoint());
    ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
    ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
    handler.handle(invocation, (response) -> {
        Assert.assertEquals("OK", response.getResult());
    });
}
Also used : ZoneAwareDiscoveryFilter(org.apache.servicecomb.loadbalance.filter.ZoneAwareDiscoveryFilter) NonSwaggerInvocation(org.apache.servicecomb.core.NonSwaggerInvocation) Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) ServerDiscoveryFilter(org.apache.servicecomb.loadbalance.filter.ServerDiscoveryFilter) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) DataCenterInfo(org.apache.servicecomb.registry.api.registry.DataCenterInfo) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) InstanceCacheManager(org.apache.servicecomb.registry.cache.InstanceCacheManager) NonSwaggerInvocation(org.apache.servicecomb.core.NonSwaggerInvocation) TransportManager(org.apache.servicecomb.core.transport.TransportManager) Transport(org.apache.servicecomb.core.Transport) DiscoveryTree(org.apache.servicecomb.registry.discovery.DiscoveryTree) IsolationDiscoveryFilter(org.apache.servicecomb.loadbalance.filter.IsolationDiscoveryFilter) Test(org.junit.Test)

Example 40 with MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project java-chassis by ServiceComb.

the class TestLoadBalancer method testLoadBalancerFullOperationWithoutException.

@Test
public void testLoadBalancerFullOperationWithoutException() {
    List<ServiceCombServer> newServers = new ArrayList<>();
    ServiceCombServer server = Mockito.mock(ServiceCombServer.class);
    Invocation invocation = Mockito.mock(Invocation.class);
    MicroserviceInstance microserviceInstance = Mockito.mock(MicroserviceInstance.class);
    newServers.add(server);
    when(invocation.getLocalContext(LoadbalanceHandler.CONTEXT_KEY_SERVER_LIST)).thenReturn(newServers);
    when(server.getInstance()).thenReturn(microserviceInstance);
    when(microserviceInstance.getInstanceId()).thenReturn("123456");
    LoadBalancer loadBalancer = new LoadBalancer(rule, "test");
    loadBalancer.chooseServer(invocation);
    when(rule.choose(newServers, invocation)).thenReturn(server);
    Assert.assertEquals(server, loadBalancer.chooseServer(invocation));
    Assert.assertNotNull(loadBalancer.getLoadBalancerStats());
    Assert.assertEquals("test", loadBalancer.getMicroServiceName());
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Aggregations

MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)232 Test (org.junit.Test)136 ArrayList (java.util.ArrayList)60 HashMap (java.util.HashMap)42 Invocation (org.apache.servicecomb.core.Invocation)40 DiscoveryTreeNode (org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)40 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)38 Expectations (mockit.Expectations)36 MicroserviceInstances (org.apache.servicecomb.registry.api.registry.MicroserviceInstances)32 CacheEndpoint (org.apache.servicecomb.registry.cache.CacheEndpoint)30 List (java.util.List)22 Transport (org.apache.servicecomb.core.Transport)22 Before (org.junit.Before)22 Map (java.util.Map)18 TransportManager (org.apache.servicecomb.core.transport.TransportManager)18 InstanceCacheManager (org.apache.servicecomb.registry.cache.InstanceCacheManager)18 MockUp (mockit.MockUp)16 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)14 ServiceCombServer (org.apache.servicecomb.loadbalance.ServiceCombServer)14 Server (com.netflix.loadbalancer.Server)12