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