#631 Use protect threshold

This commit is contained in:
nkorange 2019-02-09 16:42:47 +08:00
parent 3b051f0ffe
commit 4fe7c3d3b8
2 changed files with 35 additions and 12 deletions

View File

@ -169,6 +169,21 @@ public class Service extends com.alibaba.nacos.api.naming.pojo.Service implement
// ignore
}
public int healthyInstanceCount() {
int healthyCount = 0;
for (Instance instance : allIPs()) {
if (instance.isHealthy()) {
healthyCount++;
}
}
return healthyCount;
}
public boolean meetProtectThreshold() {
return (healthyInstanceCount() * 1.0 / allIPs().size()) < getProtectThreshold();
}
public void updateIPs(Collection<Instance> ips, boolean ephemeral) {
// TODO prevent most of the instances from removed

View File

@ -36,10 +36,10 @@ import java.util.List;
*/
public class ClientBeatCheckTask implements Runnable {
private Service domain;
private Service service;
public ClientBeatCheckTask(Service domain) {
this.domain = domain;
public ClientBeatCheckTask(Service service) {
this.service = service;
}
@ -54,17 +54,17 @@ public class ClientBeatCheckTask implements Runnable {
}
public String taskKey() {
return domain.getName();
return service.getName();
}
@Override
public void run() {
try {
if (!getDistroMapper().responsible(domain.getName())) {
if (!getDistroMapper().responsible(service.getName())) {
return;
}
List<Instance> instances = domain.allIPs(true);
List<Instance> instances = service.allIPs(true);
for (Instance instance : instances) {
if (System.currentTimeMillis() - instance.getLastBeat() > ClientBeatProcessor.CLIENT_BEAT_TIMEOUT) {
@ -74,14 +74,22 @@ public class ClientBeatCheckTask implements Runnable {
Loggers.EVT_LOG.info("{POS} {IP-DISABLED} valid: {}:{}@{}, region: {}, msg: client timeout after {}, last beat: {}",
instance.getIp(), instance.getPort(), instance.getClusterName(),
UtilsAndCommons.LOCALHOST_SITE, ClientBeatProcessor.CLIENT_BEAT_TIMEOUT, instance.getLastBeat());
getPushService().serviceChanged(domain.getNamespaceId(), domain.getName());
getPushService().serviceChanged(service.getNamespaceId(), service.getName());
}
}
}
if (System.currentTimeMillis() - instance.getLastBeat() > domain.getIpDeleteTimeout()) {
// delete ip
Loggers.SRV_LOG.info("[AUTO-DELETE-IP] dom: {}, ip: {}", domain.getName(), JSON.toJSONString(instance));
if (System.currentTimeMillis() - instance.getLastBeat() > service.getIpDeleteTimeout()) {
// protect threshold met:
if (service.meetProtectThreshold()) {
Loggers.SRV_LOG.info("protect threshold met, service: {}, ip: {}, healthy: {}, total: {}",
service.getName(), JSON.toJSONString(instance), service.healthyInstanceCount(), service.allIPs().size());
return;
}
// delete instance
Loggers.SRV_LOG.info("[AUTO-DELETE-IP] service: {}, ip: {}", service.getName(), JSON.toJSONString(instance));
deleteIP(instance);
}
}
@ -97,8 +105,8 @@ public class ClientBeatCheckTask implements Runnable {
request.appendParam("ip", instance.getIp())
.appendParam("port", String.valueOf(instance.getPort()))
.appendParam("clusterName", instance.getClusterName())
.appendParam("serviceName", domain.getName())
.appendParam("namespaceId", domain.getNamespaceId());
.appendParam("serviceName", service.getName())
.appendParam("namespaceId", service.getNamespaceId());
String url = "http://127.0.0.1:" + RunningConfig.getServerPort() + RunningConfig.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance?" + request.toUrl();