Skip to content

Commit

Permalink
[ISSUE #3628] set naming client updateTask interval more flexible (#3637
Browse files Browse the repository at this point in the history
)

* 1.use server cacheMillis event service deleted
2.naming client UpdateTask's interval will inc by failCount that connect with server

* 1.move failCount to updateTask
2.redefine the updateService method name. updateServiceNow -> updateService, wrap updateService in updateServiceNow when first getServiceInfo

* 1.create push client even service is not exist
2.serviceInfo's hosts is empty or can't connect to server both add the updateTsk interval

* format the indent
  • Loading branch information
horizonzy authored Aug 20, 2020
1 parent 136b456 commit 6e4b0c9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.alibaba.nacos.client.naming.beat.BeatReactor;
import com.alibaba.nacos.client.naming.cache.DiskCache;
import com.alibaba.nacos.client.naming.net.NamingProxy;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.utils.JacksonUtils;
Expand Down Expand Up @@ -296,6 +297,14 @@ public ServiceInfo getServiceInfo(final String serviceName, final String cluster
return serviceInfoMap.get(serviceObj.getKey());
}

private void updateServiceNow(String serviceName, String clusters) {
try {
updateService(serviceName, clusters);
} catch (NacosException e) {
NAMING_LOGGER.error("[NA] failed to update serviceName: " + serviceName, e);
}
}

/**
* Schedule update if absent.
*
Expand Down Expand Up @@ -323,7 +332,7 @@ public void scheduleUpdateIfAbsent(String serviceName, String clusters) {
* @param serviceName service name
* @param clusters clusters
*/
public void updateServiceNow(String serviceName, String clusters) {
public void updateService(String serviceName, String clusters) throws NacosException {
ServiceInfo oldService = getServiceInfo0(serviceName, clusters);
try {

Expand All @@ -332,8 +341,6 @@ public void updateServiceNow(String serviceName, String clusters) {
if (StringUtils.isNotEmpty(result)) {
processServiceJson(result);
}
} catch (Exception e) {
NAMING_LOGGER.error("[NA] failed to update serviceName: " + serviceName, e);
} finally {
if (oldService != null) {
synchronized (oldService) {
Expand Down Expand Up @@ -375,26 +382,42 @@ public class UpdateTask implements Runnable {

private final String serviceName;

/**
* the fail situation. 1:can't connect to server 2:serviceInfo's hosts is empty
*/
private int failCount = 0;

public UpdateTask(String serviceName, String clusters) {
this.serviceName = serviceName;
this.clusters = clusters;
}

private void incFailCount() {
int limit = 6;
if (failCount == limit) {
return;
}
failCount++;
}

private void resetFailCount() {
failCount = 0;
}

@Override
public void run() {
long delayTime = -1;
long delayTime = DEFAULT_DELAY;

try {
ServiceInfo serviceObj = serviceInfoMap.get(ServiceInfo.getKey(serviceName, clusters));

if (serviceObj == null) {
updateServiceNow(serviceName, clusters);
delayTime = DEFAULT_DELAY;
updateService(serviceName, clusters);
return;
}

if (serviceObj.getLastRefTime() <= lastRefTime) {
updateServiceNow(serviceName, clusters);
updateService(serviceName, clusters);
serviceObj = serviceInfoMap.get(ServiceInfo.getKey(serviceName, clusters));
} else {
// if serviceName already updated by push, we should not override it
Expand All @@ -410,17 +433,18 @@ public void run() {
NAMING_LOGGER.info("update task is stopped, service:" + serviceName + ", clusters:" + clusters);
return;
}

if (CollectionUtils.isEmpty(serviceObj.getHosts())) {
incFailCount();
return;
}
delayTime = serviceObj.getCacheMillis();

resetFailCount();
} catch (Throwable e) {
incFailCount();
NAMING_LOGGER.warn("[NA] failed to update serviceName: " + serviceName, e);
} finally {
if (delayTime > 0) {
executor.schedule(this, delayTime, TimeUnit.MILLISECONDS);
}
executor.schedule(this, Math.min(delayTime << failCount, DEFAULT_DELAY * 60), TimeUnit.MILLISECONDS);
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -516,19 +516,6 @@ public ObjectNode doSrvIpxt(String namespaceId, String serviceName, String agent
ClientInfo clientInfo = new ClientInfo(agent);
ObjectNode result = JacksonUtils.createEmptyJsonNode();
Service service = serviceManager.getService(namespaceId, serviceName);

if (service == null) {
if (Loggers.SRV_LOG.isDebugEnabled()) {
Loggers.SRV_LOG.debug("no instance to serve for service: {}", serviceName);
}
result.put("name", serviceName);
result.put("clusters", clusters);
result.replace("hosts", JacksonUtils.createEmptyArrayNode());
return result;
}

checkIfDisabled(service);

long cacheMillis = switchDomain.getDefaultCacheMillis();

// now try to enable the push
Expand All @@ -546,6 +533,19 @@ public ObjectNode doSrvIpxt(String namespaceId, String serviceName, String agent
cacheMillis = switchDomain.getDefaultCacheMillis();
}

if (service == null) {
if (Loggers.SRV_LOG.isDebugEnabled()) {
Loggers.SRV_LOG.debug("no instance to serve for service: {}", serviceName);
}
result.put("name", serviceName);
result.put("clusters", clusters);
result.put("cacheMillis", cacheMillis);
result.replace("hosts", JacksonUtils.createEmptyArrayNode());
return result;
}

checkIfDisabled(service);

List<Instance> srvedIPs;

srvedIPs = service.srvIPs(Arrays.asList(StringUtils.split(clusters, ",")));
Expand Down

0 comments on commit 6e4b0c9

Please sign in to comment.