use of org.apache.servicecomb.registry.cache.CacheEndpoint in project java-chassis by ServiceComb.
the class ClassificationAddress method generateZoneAndRegionAddress.
private Map<String, List<String>> generateZoneAndRegionAddress(String key) {
InstanceCache KieCaches = instanceCacheManager.getOrCreate(REGISTRY_APP_ID, key, DefinitionConst.VERSION_RULE_LATEST);
List<CacheEndpoint> CacheEndpoints = new ArrayList<>();
if (REGISTRY_SERVICE_NAME.equals(key)) {
CacheEndpoints = KieCaches.getOrCreateTransportMap().get(defaultTransport);
maxRetryTimes = CacheEndpoints.size();
} else {
if (KieCaches.getInstanceMap().size() <= 0) {
return null;
}
CacheEndpoints = KieCaches.getOrCreateTransportMap().get(defaultTransport);
}
Map<String, List<String>> zoneAndRegion = new HashMap<>();
dataCenterInfo = findRegion(CacheEndpoints);
Set<String> sameZone = new HashSet<>();
Set<String> sameRegion = new HashSet<>();
for (CacheEndpoint cacheEndpoint : CacheEndpoints) {
if (regionAndAZMatch(dataCenterInfo, cacheEndpoint.getInstance())) {
sameZone.add(cacheEndpoint.getEndpoint());
} else {
sameRegion.add(cacheEndpoint.getEndpoint());
}
}
zoneAndRegion.put("sameZone", new ArrayList<>(sameZone));
zoneAndRegion.put("sameRegion", new ArrayList<>(sameRegion));
return zoneAndRegion;
}
use of org.apache.servicecomb.registry.cache.CacheEndpoint in project java-chassis by ServiceComb.
the class TestLoadbalanceHandler method send_success.
@Test
public void send_success(@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.ok("success");
Holder<String> result = new Holder<>();
Deencapsulation.invoke(handler, "send", invocation, (AsyncResponse) resp -> {
result.value = resp.getResult();
}, loadBalancer);
Assert.assertEquals(1, loadBalancer.getLoadBalancerStats().getSingleServerStat(server).getActiveRequestsCount());
Assert.assertEquals("success", result.value);
}
use of org.apache.servicecomb.registry.cache.CacheEndpoint in project java-chassis by ServiceComb.
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 java-chassis by ServiceComb.
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);
}
use of org.apache.servicecomb.registry.cache.CacheEndpoint in project java-chassis by ServiceComb.
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));
}
Aggregations