Search in sources :

Example 1 with CseServer

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

the class ZoneAwareServerListFilterExt method getFilteredListOfServers.

@Override
public List<Server> getFilteredListOfServers(List<Server> list) {
    List<Server> result = new ArrayList<>();
    MicroserviceInstance myself = RegistryUtils.getMicroserviceInstance();
    boolean find = false;
    for (Server server : list) {
        CseServer cseServer = (CseServer) server;
        if (regionAndAZMatch(myself, cseServer.getInstance())) {
            result.add(cseServer);
            find = true;
        }
    }
    if (!find) {
        for (Server server : list) {
            CseServer cseServer = (CseServer) server;
            if (regionMatch(myself, cseServer.getInstance())) {
                result.add(cseServer);
                find = true;
            }
        }
    }
    if (!find) {
        result = list;
    }
    return result;
}
Also used : CseServer(org.apache.servicecomb.loadbalance.CseServer) Server(com.netflix.loadbalancer.Server) CseServer(org.apache.servicecomb.loadbalance.CseServer) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)

Example 2 with CseServer

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

the class TestIsolationServerListFilter method testGetFilteredListOfServers.

@Test
public void testGetFilteredListOfServers() {
    Invocation invocation = Mockito.mock(Invocation.class);
    CseServer testServer = Mockito.mock(CseServer.class);
    Mockito.when(invocation.getMicroserviceName()).thenReturn("microserviceName");
    Mockito.when(testServer.getLastVisitTime()).thenReturn(System.currentTimeMillis());
    List<Server> serverList = new ArrayList<>();
    serverList.add(testServer);
    IsolationServerListFilter.setLoadBalancerStats(loadBalancerStats);
    IsolationServerListFilter.setInvocation(invocation);
    List<Server> returnedServerList = IsolationServerListFilter.getFilteredListOfServers(serverList);
    Assert.assertEquals(returnedServerList.size(), 1);
    loadBalancerStats.incrementNumRequests(testServer);
    loadBalancerStats.incrementNumRequests(testServer);
    loadBalancerStats.incrementNumRequests(testServer);
    loadBalancerStats.incrementSuccessiveConnectionFailureCount(testServer);
    returnedServerList = IsolationServerListFilter.getFilteredListOfServers(serverList);
    Assert.assertEquals(returnedServerList.size(), 0);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) CseServer(org.apache.servicecomb.loadbalance.CseServer) CseServer(org.apache.servicecomb.loadbalance.CseServer) Server(com.netflix.loadbalancer.Server) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with CseServer

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

the class TestIsolationServerListFilter method testGetFilteredListOfServersOnContinuousFailureIsBelowThreshold.

@Test
public void testGetFilteredListOfServersOnContinuousFailureIsBelowThreshold() {
    ((AbstractConfiguration) DynamicPropertyFactory.getBackingConfigurationSource()).addProperty("cse.loadbalance.isolation.continuousFailureThreshold", "3");
    Invocation invocation = Mockito.mock(Invocation.class);
    CseServer testServer = Mockito.mock(CseServer.class);
    Mockito.when(invocation.getMicroserviceName()).thenReturn("microserviceName");
    Mockito.when(testServer.getCountinuousFailureCount()).thenReturn(2);
    Mockito.when(testServer.getLastVisitTime()).thenReturn(System.currentTimeMillis());
    for (int i = 0; i < 3; ++i) {
        loadBalancerStats.incrementNumRequests(testServer);
    }
    List<Server> serverList = new ArrayList<>();
    serverList.add(testServer);
    IsolationServerListFilter.setLoadBalancerStats(loadBalancerStats);
    IsolationServerListFilter.setInvocation(invocation);
    List<Server> returnedServerList = IsolationServerListFilter.getFilteredListOfServers(serverList);
    Assert.assertEquals(1, returnedServerList.size());
}
Also used : AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) Invocation(org.apache.servicecomb.core.Invocation) CseServer(org.apache.servicecomb.loadbalance.CseServer) CseServer(org.apache.servicecomb.loadbalance.CseServer) Server(com.netflix.loadbalancer.Server) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with CseServer

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

the class TestIsolationServerListFilter method testGetFilteredListOfServersOnContinuousFailureReachesThreshold.

@Test
public void testGetFilteredListOfServersOnContinuousFailureReachesThreshold() {
    ((AbstractConfiguration) DynamicPropertyFactory.getBackingConfigurationSource()).addProperty("cse.loadbalance.isolation.continuousFailureThreshold", "3");
    Invocation invocation = Mockito.mock(Invocation.class);
    CseServer testServer = Mockito.mock(CseServer.class);
    Mockito.when(invocation.getMicroserviceName()).thenReturn("microserviceName");
    Mockito.when(testServer.getCountinuousFailureCount()).thenReturn(3);
    Mockito.when(testServer.getLastVisitTime()).thenReturn(System.currentTimeMillis());
    for (int i = 0; i < 3; ++i) {
        loadBalancerStats.incrementNumRequests(testServer);
    }
    List<Server> serverList = new ArrayList<>();
    serverList.add(testServer);
    IsolationServerListFilter.setLoadBalancerStats(loadBalancerStats);
    IsolationServerListFilter.setInvocation(invocation);
    List<Server> returnedServerList = IsolationServerListFilter.getFilteredListOfServers(serverList);
    Assert.assertEquals(0, returnedServerList.size());
}
Also used : AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) Invocation(org.apache.servicecomb.core.Invocation) CseServer(org.apache.servicecomb.loadbalance.CseServer) CseServer(org.apache.servicecomb.loadbalance.CseServer) Server(com.netflix.loadbalancer.Server) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with CseServer

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

the class TestZoneAwareServerListFilterExt method testZoneAwareServerListFilterExt.

@Test
public void testZoneAwareServerListFilterExt(@Mocked RegistryUtils registryUtils) {
    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");
    allmatchInstance.setDataCenterInfo(info);
    MicroserviceInstance regionMatchInstance = new MicroserviceInstance();
    info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region");
    info.setAvailableZone("test-zone2");
    regionMatchInstance.setDataCenterInfo(info);
    MicroserviceInstance noneMatchInstance = new MicroserviceInstance();
    info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region2");
    info.setAvailableZone("test-zone2");
    noneMatchInstance.setDataCenterInfo(info);
    new Expectations() {

        {
            RegistryUtils.getMicroserviceInstance();
            result = myself;
        }
    };
    ZoneAwareServerListFilterExt filter = new ZoneAwareServerListFilterExt();
    List<Server> servers = new ArrayList<>();
    CseServer noneMatchServer = new MockUp<CseServer>() {

        @Mock
        public String toString() {
            return "noneMatchServer";
        }

        @Mock
        public String getHost() {
            return "noneMatchServer";
        }

        @Mock
        public MicroserviceInstance getInstance() {
            return noneMatchInstance;
        }
    }.getMockInstance();
    CseServer regionMatchregionMatchServer = new MockUp<CseServer>() {

        @Mock
        public String toString() {
            return "regionMatchregionMatchServer";
        }

        @Mock
        public String getHost() {
            return "regionMatchregionMatchServer";
        }

        @Mock
        public MicroserviceInstance getInstance() {
            return regionMatchInstance;
        }
    }.getMockInstance();
    CseServer allmatchServer = new MockUp<CseServer>() {

        @Mock
        public String toString() {
            return "allmatchServer";
        }

        @Mock
        public String getHost() {
            return "allmatchServer";
        }

        @Mock
        public MicroserviceInstance getInstance() {
            return allmatchInstance;
        }
    }.getMockInstance();
    servers.add(noneMatchServer);
    List<Server> result = filter.getFilteredListOfServers(servers);
    Assert.assertEquals(result.size(), 1);
    Assert.assertEquals(result.get(0), noneMatchServer);
    servers.add(regionMatchregionMatchServer);
    result = filter.getFilteredListOfServers(servers);
    Assert.assertEquals(result.size(), 1);
    Assert.assertEquals(result.get(0), regionMatchregionMatchServer);
    servers.add(allmatchServer);
    result = filter.getFilteredListOfServers(servers);
    Assert.assertEquals(result.size(), 1);
    Assert.assertEquals(result.get(0), allmatchServer);
}
Also used : Expectations(mockit.Expectations) CseServer(org.apache.servicecomb.loadbalance.CseServer) Server(com.netflix.loadbalancer.Server) CseServer(org.apache.servicecomb.loadbalance.CseServer) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) DataCenterInfo(org.apache.servicecomb.serviceregistry.api.registry.DataCenterInfo) Mock(mockit.Mock) Test(org.junit.Test)

Aggregations

CseServer (org.apache.servicecomb.loadbalance.CseServer)8 Server (com.netflix.loadbalancer.Server)6 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 Expectations (mockit.Expectations)3 Invocation (org.apache.servicecomb.core.Invocation)3 MicroserviceInstance (org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)3 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)2 Mock (mockit.Mock)1 DataCenterInfo (org.apache.servicecomb.serviceregistry.api.registry.DataCenterInfo)1