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;
}
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);
}
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());
}
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());
}
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);
}
Aggregations