use of org.apache.servicecomb.registry.cache.CacheEndpoint in project java-chassis by ServiceComb.
the class TestSessionSticknessRule method testLastServerNotExist.
@Test
public void testLastServerNotExist() {
SessionStickinessRule rule = new SessionStickinessRule();
Transport transport = mock(Transport.class);
Invocation invocation = mock(Invocation.class);
MicroserviceInstance instance1 = new MicroserviceInstance();
instance1.setInstanceId("1234");
ServiceCombServer mockedServer = new ServiceCombServer(null, transport, new CacheEndpoint("rest:127.0.0.1:8890", instance1));
mockedServer.setAlive(true);
mockedServer.setReadyToServe(true);
mockedServer.setId("mockedServer");
List<ServiceCombServer> allServers = Arrays.asList(mockedServer);
LoadBalancer lb = new LoadBalancer(rule, "mockedServer");
when(invocation.getLocalContext(LoadbalanceHandler.CONTEXT_KEY_SERVER_LIST)).thenReturn(allServers);
rule.setLoadBalancer(lb);
ServiceCombServer server = new ServiceCombServer(null, transport, new CacheEndpoint("rest:127.0.0.1:8890", instance1));
Deencapsulation.setField(rule, "lastServer", server);
new MockUp<SessionStickinessRule>(rule) {
@Mock
private boolean isTimeOut() {
return false;
}
@Mock
private boolean isErrorThresholdMet() {
return false;
}
};
Server s = rule.choose(allServers, invocation);
Assert.assertEquals(mockedServer, s);
}
use of org.apache.servicecomb.registry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.
the class ServerDiscoveryFilter method createEndpoint.
@Override
protected Object createEndpoint(DiscoveryContext context, String transportName, String endpoint, MicroserviceInstance instance) {
Transport transport = SCBEngine.getInstance().getTransportManager().findTransport(transportName);
if (transport == null) {
LOGGER.info("not deployed transport {}, ignore {}.", transportName, endpoint);
return null;
}
Invocation invocation = context.getInputParameters();
return new ServiceCombServer(invocation.getMicroserviceName(), transport, new CacheEndpoint(endpoint, instance));
}
use of org.apache.servicecomb.registry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.
the class TestLoadbalanceHandler method send_failed.
@Test
public void send_failed(@Injectable LoadBalancer loadBalancer) {
MicroserviceInstance instance1 = new MicroserviceInstance();
instance1.setInstanceId("1234");
CacheEndpoint cacheEndpoint = new CacheEndpoint("rest://localhost:8080", instance1);
ServiceCombServer server = new ServiceCombServer(null, restTransport, cacheEndpoint);
LoadBalancerStats stats = new LoadBalancerStats("test");
new Expectations(loadBalancer) {
{
loadBalancer.chooseServer(invocation);
result = server;
loadBalancer.getLoadBalancerStats();
result = stats;
}
};
sendResponse = Response.consumerFailResp(new SocketException());
Holder<Throwable> result = new Holder<>();
Deencapsulation.invoke(handler, "send", invocation, (AsyncResponse) resp -> {
result.value = (Throwable) resp.getResult();
}, loadBalancer);
Assert.assertEquals(1, loadBalancer.getLoadBalancerStats().getSingleServerStat(server).getSuccessiveConnectionFailureCount());
Assert.assertEquals("InvocationException: code=490;msg=CommonExceptionData [message=Unexpected consumer error, please check logs for details]", result.value.getMessage());
}
use of org.apache.servicecomb.registry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.
the class TestLoadbalanceHandler method send_failed2.
@Test
public void send_failed2(@Injectable LoadBalancer loadBalancer) {
MicroserviceInstance instance1 = new MicroserviceInstance();
instance1.setInstanceId("1234");
CacheEndpoint cacheEndpoint = new CacheEndpoint("rest://localhost:8080", instance1);
ServiceCombServer server = new ServiceCombServer(null, restTransport, cacheEndpoint);
LoadBalancerStats stats = new LoadBalancerStats("test");
new Expectations(loadBalancer) {
{
loadBalancer.chooseServer(invocation);
result = server;
loadBalancer.getLoadBalancerStats();
result = stats;
}
};
sendResponse = Response.create(Status.BAD_REQUEST, "send failed");
Holder<Throwable> result = new Holder<>();
Deencapsulation.invoke(handler, "send", invocation, (AsyncResponse) resp -> {
result.value = (Throwable) resp.getResult();
}, loadBalancer);
// InvocationException is not taken as a failure
Assert.assertEquals(0, loadBalancer.getLoadBalancerStats().getSingleServerStat(server).getSuccessiveConnectionFailureCount());
Assert.assertEquals("InvocationException: code=400;msg=send failed", result.value.getMessage());
}
use of org.apache.servicecomb.registry.cache.CacheEndpoint in project incubator-servicecomb-java-chassis by apache.
the class TestServiceCombLoadBalancerStats method testMultiThread.
@Test
public void testMultiThread(@Injectable Transport transport) throws Exception {
long time = System.currentTimeMillis();
MicroserviceInstance instance = new MicroserviceInstance();
instance.setInstanceId("instance2");
ServiceCombServer serviceCombServer = new ServiceCombServer(null, transport, new CacheEndpoint("rest://localhost:8080", instance));
CountDownLatch latch = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
new Thread(() -> {
ServiceCombLoadBalancerStats.INSTANCE.markFailure(serviceCombServer);
ServiceCombLoadBalancerStats.INSTANCE.markFailure(serviceCombServer);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(serviceCombServer);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(serviceCombServer);
latch.countDown();
}).start();
}
latch.await(30, TimeUnit.SECONDS);
Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getTotalRequests(), 4 * 10);
Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getFailedRate(), 50);
Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getSuccessRate(), 50);
Assert.assertEquals(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getSuccessRequests(), 20);
Assert.assertTrue(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getLastVisitTime() <= System.currentTimeMillis() && ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getLastVisitTime() >= time);
// time consuming test for timers, taking about 20 seconds. ping timer will update instance status to failure
Assert.assertTrue(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getFailedRate() <= 50);
long beginTime = System.currentTimeMillis();
long rate = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getFailedRequests();
while (rate <= 20 && System.currentTimeMillis() - beginTime <= 30000) {
Thread.sleep(2000);
rate = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getFailedRequests();
System.out.println("failedRequests: " + rate);
}
Assert.assertTrue(System.currentTimeMillis() - beginTime < 30000);
Assert.assertTrue(ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer).getFailedRequests() > 20);
}
Aggregations