use of org.apache.servicecomb.registry.consumer.MicroserviceInstancePing in project java-chassis by ServiceComb.
the class ServiceCombLoadBalancerStats method init.
void init() {
// for testing
if (timer != null) {
timer.cancel();
}
if (serverStatsCache != null) {
serverStatsCache.cleanUp();
}
pingView.clear();
serverStatsCache = CacheBuilder.newBuilder().expireAfterAccess(serverExpireInSeconds, TimeUnit.SECONDS).removalListener((RemovalListener<ServiceCombServer, ServiceCombServerStats>) notification -> {
ServiceCombServer server = notification.getKey();
LOGGER.info("stats of instance {} removed, host is {}", server.getInstance().getInstanceId(), server.getHost());
pingView.remove(notification.getKey());
serviceCombServers.remove(notification.getKey());
}).build(new CacheLoader<ServiceCombServer, ServiceCombServerStats>() {
public ServiceCombServerStats load(ServiceCombServer server) {
ServiceCombServerStats stats = new ServiceCombServerStats(server.getMicroserviceName());
pingView.put(server, stats);
serviceCombServers.put(server.getInstance().getInstanceId(), server);
return stats;
}
});
timer = new Timer("LoadBalancerStatsTimer", true);
timer.schedule(new TimerTask() {
private MicroserviceInstancePing ping = SPIServiceUtils.getPriorityHighestService(MicroserviceInstancePing.class);
@Override
public void run() {
try {
Map<ServiceCombServer, ServiceCombServerStats> allServers = pingView;
allServers.entrySet().forEach(serviceCombServerServiceCombServerStatsEntry -> {
ServiceCombServer server = serviceCombServerServiceCombServerStatsEntry.getKey();
ServiceCombServerStats stats = serviceCombServerServiceCombServerStatsEntry.getValue();
if ((System.currentTimeMillis() - stats.getLastVisitTime() > timerIntervalInMillis) && !ping.ping(server.getInstance())) {
LOGGER.info("ping mark server {} failure.", server.getInstance().getInstanceId());
stats.markFailure();
}
});
serverStatsCache.cleanUp();
} catch (Throwable e) {
LOGGER.warn("LoadBalancerStatsTimer error.", e);
}
}
}, timerIntervalInMillis, timerIntervalInMillis);
}
use of org.apache.servicecomb.registry.consumer.MicroserviceInstancePing in project java-chassis by ServiceComb.
the class RefreshableMicroserviceCacheTest method refresh_empty_instance_protection_enabled.
@Test
public void refresh_empty_instance_protection_enabled() {
microserviceCache.setEmptyInstanceProtectionEnabled(true);
microserviceCache.instancePing = new MicroserviceInstancePing() {
@Override
public int getOrder() {
return 0;
}
@Override
public boolean ping(MicroserviceInstance instance) {
return true;
}
};
microserviceCache.instances = new ArrayList<>();
MicroserviceInstance instance0 = new MicroserviceInstance();
instance0.setInstanceId("instanceId0");
microserviceCache.instances.add(instance0);
pulledInstances = new ArrayList<>();
microserviceCache.refresh();
Assert.assertEquals(MicroserviceCacheStatus.REFRESHED, microserviceCache.getStatus());
Assert.assertEquals(1, microserviceCache.getInstances().size());
Assert.assertEquals("instanceId0", microserviceCache.getInstances().get(0).getInstanceId());
}
use of org.apache.servicecomb.registry.consumer.MicroserviceInstancePing in project incubator-servicecomb-java-chassis by apache.
the class ServiceCombLoadBalancerStats method init.
void init() {
// for testing
if (timer != null) {
timer.cancel();
}
if (serverStatsCache != null) {
serverStatsCache.cleanUp();
}
pingView.clear();
serverStatsCache = CacheBuilder.newBuilder().expireAfterAccess(serverExpireInSeconds, TimeUnit.SECONDS).removalListener((RemovalListener<ServiceCombServer, ServiceCombServerStats>) notification -> {
ServiceCombServer server = notification.getKey();
LOGGER.info("stats of instance {} removed, host is {}", server.getInstance().getInstanceId(), server.getHost());
pingView.remove(notification.getKey());
serviceCombServers.remove(notification.getKey());
}).build(new CacheLoader<ServiceCombServer, ServiceCombServerStats>() {
public ServiceCombServerStats load(ServiceCombServer server) {
ServiceCombServerStats stats = new ServiceCombServerStats(server.getMicroserviceName());
pingView.put(server, stats);
serviceCombServers.put(server.getInstance().getInstanceId(), server);
return stats;
}
});
timer = new Timer("LoadBalancerStatsTimer", true);
timer.schedule(new TimerTask() {
private MicroserviceInstancePing ping = SPIServiceUtils.getPriorityHighestService(MicroserviceInstancePing.class);
@Override
public void run() {
try {
Map<ServiceCombServer, ServiceCombServerStats> allServers = pingView;
allServers.entrySet().forEach(serviceCombServerServiceCombServerStatsEntry -> {
ServiceCombServer server = serviceCombServerServiceCombServerStatsEntry.getKey();
ServiceCombServerStats stats = serviceCombServerServiceCombServerStatsEntry.getValue();
if ((System.currentTimeMillis() - stats.getLastVisitTime() > timerIntervalInMillis) && !ping.ping(server.getInstance())) {
LOGGER.info("ping mark server {} failure.", server.getInstance().getInstanceId());
stats.markFailure();
}
});
serverStatsCache.cleanUp();
} catch (Throwable e) {
LOGGER.warn("LoadBalancerStatsTimer error.", e);
}
}
}, timerIntervalInMillis, timerIntervalInMillis);
}
use of org.apache.servicecomb.registry.consumer.MicroserviceInstancePing in project incubator-servicecomb-java-chassis by apache.
the class RefreshableMicroserviceCacheTest method refresh_empty_instance_protection_enabled.
@Test
public void refresh_empty_instance_protection_enabled() {
microserviceCache.setEmptyInstanceProtectionEnabled(true);
microserviceCache.instancePing = new MicroserviceInstancePing() {
@Override
public int getOrder() {
return 0;
}
@Override
public boolean ping(MicroserviceInstance instance) {
return true;
}
};
microserviceCache.instances = new ArrayList<>();
MicroserviceInstance instance0 = new MicroserviceInstance();
instance0.setInstanceId("instanceId0");
microserviceCache.instances.add(instance0);
pulledInstances = new ArrayList<>();
microserviceCache.refresh();
Assert.assertEquals(MicroserviceCacheStatus.REFRESHED, microserviceCache.getStatus());
Assert.assertEquals(1, microserviceCache.getInstances().size());
Assert.assertEquals("instanceId0", microserviceCache.getInstances().get(0).getInstanceId());
}
Aggregations