From 63560370aed6337de53bbc3cbe45964ffe2eb7e3 Mon Sep 17 00:00:00 2001 From: IanCao Date: Tue, 11 Jun 2019 14:40:28 +0800 Subject: [PATCH 01/15] Different instance has different TTL. --- .../alibaba/nacos/api/common/Constants.java | 14 ++++++++ .../healthcheck/ClientBeatCheckTask.java | 34 +++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/common/Constants.java b/api/src/main/java/com/alibaba/nacos/api/common/Constants.java index aac7c929f..321e146c6 100644 --- a/api/src/main/java/com/alibaba/nacos/api/common/Constants.java +++ b/api/src/main/java/com/alibaba/nacos/api/common/Constants.java @@ -15,6 +15,8 @@ */ package com.alibaba.nacos.api.common; +import java.util.concurrent.TimeUnit; + /** * Constant * @@ -42,6 +44,12 @@ public class Constants { public static final String GROUP = "group"; + public static final String HEART_BEAT_TIMEOUT = "heartBeatTimeout"; + + public static final String IP_DELETE_TIMEOUT = "ipDeleteTimeout"; + + public static final String HEART_BEAT_INTERVAL = "heartBeatInterval"; + public static final String LAST_MODIFIED = "Last-Modified"; public static final String ACCEPT_ENCODING = "Accept-Encoding"; @@ -123,6 +131,12 @@ public class Constants { public static final String DEFAULT_CLUSTER_NAME = "DEFAULT"; + public static final long DEFAULT_HEART_BEAT_TIMEOUT = TimeUnit.SECONDS.toMillis(15); + + public static final long DEFAULT_IP_DELETE_TIMEOUT = TimeUnit.SECONDS.toMillis(30); + + public static final long DEFAULT_HEART_BEAT_INTERVAL = TimeUnit.SECONDS.toMillis(5); + public static final String DEFAULT_NAMESPACE_ID = "public"; public static final boolean DEFAULT_USE_CLOUD_NAMESPACE_PARSING = true; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java index 50fa3a628..d6b2a3099 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.naming.healthcheck; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.annotation.JSONField; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.naming.boot.RunningConfig; import com.alibaba.nacos.naming.boot.SpringContext; import com.alibaba.nacos.naming.core.DistroMapper; @@ -26,9 +27,12 @@ import com.alibaba.nacos.naming.misc.*; import com.alibaba.nacos.naming.push.PushService; import com.ning.http.client.AsyncCompletionHandler; import com.ning.http.client.Response; +import org.springframework.util.StringUtils; import java.net.HttpURLConnection; import java.util.List; +import java.util.Map; + /** * Check and update statues of ephemeral instances, remove them if they have been expired. @@ -73,13 +77,13 @@ public class ClientBeatCheckTask implements Runnable { // first set health status of instances: for (Instance instance : instances) { - if (System.currentTimeMillis() - instance.getLastBeat() > ClientBeatProcessor.CLIENT_BEAT_TIMEOUT) { + if (System.currentTimeMillis() - instance.getLastBeat() > getInstanceHeartBeatTimeOut(instance)) { if (!instance.isMarked()) { if (instance.isHealthy()) { instance.setHealthy(false); Loggers.EVT_LOG.info("{POS} {IP-DISABLED} valid: {}:{}@{}@{}, region: {}, msg: client timeout after {}, last beat: {}", instance.getIp(), instance.getPort(), instance.getClusterName(), service.getName(), - UtilsAndCommons.LOCALHOST_SITE, ClientBeatProcessor.CLIENT_BEAT_TIMEOUT, instance.getLastBeat()); + UtilsAndCommons.LOCALHOST_SITE, getInstanceHeartBeatTimeOut(instance), instance.getLastBeat()); getPushService().serviceChanged(service.getNamespaceId(), service.getName()); } } @@ -92,7 +96,7 @@ public class ClientBeatCheckTask implements Runnable { // then remove obsolete instances: for (Instance instance : instances) { - if (System.currentTimeMillis() - instance.getLastBeat() > service.getIpDeleteTimeout()) { + if (System.currentTimeMillis() - instance.getLastBeat() > getIpDeleteTimeout(instance)) { // delete instance Loggers.SRV_LOG.info("[AUTO-DELETE-IP] service: {}, ip: {}", service.getName(), JSON.toJSONString(instance)); deleteIP(instance); @@ -105,6 +109,30 @@ public class ClientBeatCheckTask implements Runnable { } + private long getInstanceHeartBeatTimeOut(Instance instance) { + Map metaData = instance.getMetadata(); + if(metaData == null || metaData.isEmpty()) { + return Constants.DEFAULT_HEART_BEAT_TIMEOUT; + } + String timeout = metaData.get(Constants.HEART_BEAT_TIMEOUT); + if(!StringUtils.isEmpty(timeout) && timeout.matches("^\\d+$")){ + return Long.valueOf(timeout); + } + return Constants.DEFAULT_HEART_BEAT_TIMEOUT; + } + + private long getIpDeleteTimeout(Instance instance) { + Map metaData = instance.getMetadata(); + if(metaData == null || metaData.isEmpty()) { + return Constants.DEFAULT_IP_DELETE_TIMEOUT; + } + String timeout = metaData.get(Constants.IP_DELETE_TIMEOUT); + if(!StringUtils.isEmpty(timeout) && timeout.matches("^\\d+$")){ + return Long.valueOf(timeout); + } + return Constants.DEFAULT_IP_DELETE_TIMEOUT; + } + private void deleteIP(Instance instance) { try { From ba7cbf1a7fa553d15fb55245f4639b48b66d2478 Mon Sep 17 00:00:00 2001 From: IanCao Date: Tue, 11 Jun 2019 16:34:34 +0800 Subject: [PATCH 02/15] FIX BUG --- .../nacos/naming/healthcheck/ClientBeatCheckTask.java | 6 ++++-- .../java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java index d6b2a3099..0ebe82cd2 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java @@ -33,6 +33,8 @@ import java.net.HttpURLConnection; import java.util.List; import java.util.Map; +import static com.alibaba.nacos.naming.misc.UtilsAndCommons.NUMBER_PATTERN; + /** * Check and update statues of ephemeral instances, remove them if they have been expired. @@ -115,7 +117,7 @@ public class ClientBeatCheckTask implements Runnable { return Constants.DEFAULT_HEART_BEAT_TIMEOUT; } String timeout = metaData.get(Constants.HEART_BEAT_TIMEOUT); - if(!StringUtils.isEmpty(timeout) && timeout.matches("^\\d+$")){ + if(!StringUtils.isEmpty(timeout) && timeout.matches(NUMBER_PATTERN)){ return Long.valueOf(timeout); } return Constants.DEFAULT_HEART_BEAT_TIMEOUT; @@ -127,7 +129,7 @@ public class ClientBeatCheckTask implements Runnable { return Constants.DEFAULT_IP_DELETE_TIMEOUT; } String timeout = metaData.get(Constants.IP_DELETE_TIMEOUT); - if(!StringUtils.isEmpty(timeout) && timeout.matches("^\\d+$")){ + if(!StringUtils.isEmpty(timeout) && timeout.matches(NUMBER_PATTERN)){ return Long.valueOf(timeout); } return Constants.DEFAULT_IP_DELETE_TIMEOUT; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java index c19bf4c17..dd9e0d562 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java @@ -118,6 +118,8 @@ public class UtilsAndCommons { public static final String DATA_BASE_DIR = NACOS_HOME + File.separator + "data" + File.separator + "naming"; + public static final String NUMBER_PATTERN = "^\\d+$"; + public static final ScheduledExecutorService SERVICE_SYNCHRONIZATION_EXECUTOR; public static final ScheduledExecutorService SERVICE_UPDATE_EXECUTOR; From e11c37bc6b4d179d480160c2536f3015ce039e7a Mon Sep 17 00:00:00 2001 From: IanCao Date: Tue, 11 Jun 2019 18:16:45 +0800 Subject: [PATCH 03/15] client beat interval should be customized at instance level --- .../client/naming/NacosNamingService.java | 20 +++++++++++- .../nacos/client/naming/beat/BeatInfo.java | 18 +++++++++++ .../nacos/client/naming/beat/BeatReactor.java | 31 ++++++++++--------- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index d92492d3a..e9e25ac45 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -39,6 +39,7 @@ import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.math.NumberUtils; import java.util.*; +import java.util.concurrent.TimeUnit; /** * @author nkorange @@ -46,6 +47,8 @@ import java.util.*; @SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule") public class NacosNamingService implements NamingService { private static final String DEFAULT_PORT = "8080"; + private static final String NUMBER_PATTERN = "^\\d+$"; + private static final long DEFAULT_HEART_BEAT_INTERVAL = TimeUnit.SECONDS.toMillis(5); /** * Each Naming instance should have different namespace. @@ -148,7 +151,7 @@ public class NacosNamingService implements NamingService { cacheDir = System.getProperty("user.home") + "/nacos/naming/" + namespace; } } - + @Override public void registerInstance(String serviceName, String ip, int port) throws NacosException { registerInstance(serviceName, ip, port, Constants.DEFAULT_CLUSTER_NAME); @@ -193,6 +196,9 @@ public class NacosNamingService implements NamingService { beatInfo.setWeight(instance.getWeight()); beatInfo.setMetadata(instance.getMetadata()); beatInfo.setScheduled(false); + beatInfo.setTime(System.currentTimeMillis()); + long instanceInterval = getInstanceHeartBeatInterval(instance); + beatInfo.setPeriod(instanceInterval == 0 ? DEFAULT_HEART_BEAT_INTERVAL : instanceInterval); beatReactor.addBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), beatInfo); } @@ -200,6 +206,18 @@ public class NacosNamingService implements NamingService { serverProxy.registerService(NamingUtils.getGroupedName(serviceName, groupName), groupName, instance); } + private long getInstanceHeartBeatInterval(Instance instance) { + Map metaData = instance.getMetadata(); + if (metaData == null || metaData.isEmpty()) { + return Constants.DEFAULT_HEART_BEAT_INTERVAL; + } + String interval = metaData.get(Constants.HEART_BEAT_INTERVAL); + if (!StringUtils.isEmpty(interval) && interval.matches(NUMBER_PATTERN)) { + return Long.valueOf(interval); + } + return Constants.DEFAULT_HEART_BEAT_TIMEOUT; + } + @Override public void deregisterInstance(String serviceName, String ip, int port) throws NacosException { deregisterInstance(serviceName, ip, port, Constants.DEFAULT_CLUSTER_NAME); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java index 98d6d40b2..8a8ab263c 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java @@ -31,6 +31,8 @@ public class BeatInfo { private String cluster; private Map metadata; private volatile boolean scheduled; + private volatile long period; + private volatile long time; @Override public String toString() { @@ -92,4 +94,20 @@ public class BeatInfo { public void setScheduled(boolean scheduled) { this.scheduled = scheduled; } + + public long getPeriod() { + return period; + } + + public void setPeriod(long period) { + this.period = period; + } + + public long getTime() { + return time; + } + + public void setTime(long time) { + this.time = time; + } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java index b0792ffdf..92f67a8df 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java @@ -76,22 +76,24 @@ public class BeatReactor { } class BeatProcessor implements Runnable { - @Override public void run() { - try { - for (Map.Entry entry : dom2Beat.entrySet()) { - BeatInfo beatInfo = entry.getValue(); - if (beatInfo.isScheduled()) { - continue; + while (true) { + try { + for (Map.Entry entry : dom2Beat.entrySet()) { + BeatInfo beatInfo = entry.getValue(); + if (beatInfo.getTime() > System.currentTimeMillis()) { + continue; + } + if (beatInfo.isScheduled()) { + continue; + } + beatInfo.setScheduled(true); + executorService.schedule(new BeatTask(beatInfo), 0, TimeUnit.MILLISECONDS); } - beatInfo.setScheduled(true); - executorService.schedule(new BeatTask(beatInfo), 0, TimeUnit.MILLISECONDS); + } catch (Exception e) { + NAMING_LOGGER.error("[CLIENT-BEAT] Exception while scheduling beat.", e); } - } catch (Exception e) { - NAMING_LOGGER.error("[CLIENT-BEAT] Exception while scheduling beat.", e); - } finally { - executorService.schedule(this, clientBeatInterval, TimeUnit.MILLISECONDS); } } } @@ -108,9 +110,8 @@ public class BeatReactor { public void run() { long result = serverProxy.sendBeat(beatInfo); beatInfo.setScheduled(false); - if (result > 0) { - clientBeatInterval = result; - } + long nextTime = result > 0 ? System.currentTimeMillis() + result : System.currentTimeMillis() + beatInfo.getPeriod(); + beatInfo.setTime(nextTime); } } } From 6b4a9b72a516c109ca8e045124995e778010e42f Mon Sep 17 00:00:00 2001 From: IanCao Date: Mon, 17 Jun 2019 18:07:54 +0800 Subject: [PATCH 04/15] fix bug --- .../alibaba/nacos/api/common/Constants.java | 8 ++--- .../api/naming/PreservedMetadataKeys.java | 6 ++++ .../nacos/api/naming/pojo/Instance.java | 28 +++++++++++++++++ .../client/naming/NacosNamingService.java | 15 ++-------- .../healthcheck/ClientBeatCheckTask.java | 30 +++---------------- 5 files changed, 42 insertions(+), 45 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/common/Constants.java b/api/src/main/java/com/alibaba/nacos/api/common/Constants.java index bd029f690..e5d61a3fd 100644 --- a/api/src/main/java/com/alibaba/nacos/api/common/Constants.java +++ b/api/src/main/java/com/alibaba/nacos/api/common/Constants.java @@ -44,12 +44,6 @@ public class Constants { public static final String GROUP = "group"; - public static final String HEART_BEAT_TIMEOUT = "heartBeatTimeout"; - - public static final String IP_DELETE_TIMEOUT = "ipDeleteTimeout"; - - public static final String HEART_BEAT_INTERVAL = "heartBeatInterval"; - public static final String LAST_MODIFIED = "Last-Modified"; public static final String ACCEPT_ENCODING = "Accept-Encoding"; @@ -166,4 +160,6 @@ public class Constants { public static final String SERVICE_INFO_SPLITER = "@@"; public static final String NULL_STRING = "null"; + + public static final String NUMBER_PATTERN = "^\\d+$"; } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/PreservedMetadataKeys.java b/api/src/main/java/com/alibaba/nacos/api/naming/PreservedMetadataKeys.java index 21dc8ccd2..d2a90adc5 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/PreservedMetadataKeys.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/PreservedMetadataKeys.java @@ -27,4 +27,10 @@ public class PreservedMetadataKeys { * The key to indicate the registry source of service instance, such as Dubbo, SpringCloud, etc. */ public static final String REGISTER_SOURCE = "preserved.register.source"; + + public static final String HEART_BEAT_TIMEOUT = "preserved.heart.beat.timeout"; + + public static final String IP_DELETE_TIMEOUT = "preserved.ip.delete.timeout"; + + public static final String HEART_BEAT_INTERVAL = "preserved.heart.beat.interval"; } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java index a4ea0484b..0398a5a5e 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java @@ -17,10 +17,15 @@ package com.alibaba.nacos.api.naming.pojo; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.annotation.JSONField; +import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.api.naming.PreservedMetadataKeys; +import org.apache.commons.lang3.StringUtils; import java.util.HashMap; import java.util.Map; +import static com.alibaba.nacos.api.common.Constants.NUMBER_PATTERN; + /** * Instance * @@ -193,4 +198,27 @@ public class Instance { return str1 == null ? str2 == null : str1.equals(str2); } + public long getInstanceHeartBeatInterval() { + return getMetaDataByKeyWithDefault(PreservedMetadataKeys.HEART_BEAT_INTERVAL, Constants.DEFAULT_HEART_BEAT_INTERVAL); + } + + public long getInstanceHeartBeatTimeOut() { + return getMetaDataByKeyWithDefault(PreservedMetadataKeys.HEART_BEAT_TIMEOUT, Constants.DEFAULT_HEART_BEAT_TIMEOUT); + } + + public long getIpDeleteTimeout() { + return getMetaDataByKeyWithDefault(PreservedMetadataKeys.IP_DELETE_TIMEOUT, Constants.DEFAULT_IP_DELETE_TIMEOUT); + } + + private long getMetaDataByKeyWithDefault( String key, long defaultValue) { + if (getMetadata() == null || getMetadata().isEmpty()) { + return defaultValue; + } + String value = getMetadata().get(key); + if (!StringUtils.isEmpty(value) && value.matches(NUMBER_PATTERN)) { + return Long.valueOf(value); + } + return defaultValue; + } + } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index e9e25ac45..5185b5f67 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.PreservedMetadataKeys; import com.alibaba.nacos.api.naming.listener.EventListener; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; @@ -47,7 +48,6 @@ import java.util.concurrent.TimeUnit; @SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule") public class NacosNamingService implements NamingService { private static final String DEFAULT_PORT = "8080"; - private static final String NUMBER_PATTERN = "^\\d+$"; private static final long DEFAULT_HEART_BEAT_INTERVAL = TimeUnit.SECONDS.toMillis(5); /** @@ -197,7 +197,7 @@ public class NacosNamingService implements NamingService { beatInfo.setMetadata(instance.getMetadata()); beatInfo.setScheduled(false); beatInfo.setTime(System.currentTimeMillis()); - long instanceInterval = getInstanceHeartBeatInterval(instance); + long instanceInterval = instance.getInstanceHeartBeatInterval(); beatInfo.setPeriod(instanceInterval == 0 ? DEFAULT_HEART_BEAT_INTERVAL : instanceInterval); beatReactor.addBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), beatInfo); @@ -206,17 +206,6 @@ public class NacosNamingService implements NamingService { serverProxy.registerService(NamingUtils.getGroupedName(serviceName, groupName), groupName, instance); } - private long getInstanceHeartBeatInterval(Instance instance) { - Map metaData = instance.getMetadata(); - if (metaData == null || metaData.isEmpty()) { - return Constants.DEFAULT_HEART_BEAT_INTERVAL; - } - String interval = metaData.get(Constants.HEART_BEAT_INTERVAL); - if (!StringUtils.isEmpty(interval) && interval.matches(NUMBER_PATTERN)) { - return Long.valueOf(interval); - } - return Constants.DEFAULT_HEART_BEAT_TIMEOUT; - } @Override public void deregisterInstance(String serviceName, String ip, int port) throws NacosException { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java index 0ebe82cd2..9b6e609ac 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java @@ -18,6 +18,7 @@ package com.alibaba.nacos.naming.healthcheck; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.api.naming.PreservedMetadataKeys; import com.alibaba.nacos.naming.boot.RunningConfig; import com.alibaba.nacos.naming.boot.SpringContext; import com.alibaba.nacos.naming.core.DistroMapper; @@ -79,13 +80,13 @@ public class ClientBeatCheckTask implements Runnable { // first set health status of instances: for (Instance instance : instances) { - if (System.currentTimeMillis() - instance.getLastBeat() > getInstanceHeartBeatTimeOut(instance)) { + if (System.currentTimeMillis() - instance.getLastBeat() > instance.getInstanceHeartBeatTimeOut()) { if (!instance.isMarked()) { if (instance.isHealthy()) { instance.setHealthy(false); Loggers.EVT_LOG.info("{POS} {IP-DISABLED} valid: {}:{}@{}@{}, region: {}, msg: client timeout after {}, last beat: {}", instance.getIp(), instance.getPort(), instance.getClusterName(), service.getName(), - UtilsAndCommons.LOCALHOST_SITE, getInstanceHeartBeatTimeOut(instance), instance.getLastBeat()); + UtilsAndCommons.LOCALHOST_SITE, instance.getInstanceHeartBeatTimeOut(), instance.getLastBeat()); getPushService().serviceChanged(service.getNamespaceId(), service.getName()); } } @@ -98,7 +99,7 @@ public class ClientBeatCheckTask implements Runnable { // then remove obsolete instances: for (Instance instance : instances) { - if (System.currentTimeMillis() - instance.getLastBeat() > getIpDeleteTimeout(instance)) { + if (System.currentTimeMillis() - instance.getLastBeat() > instance.getIpDeleteTimeout()) { // delete instance Loggers.SRV_LOG.info("[AUTO-DELETE-IP] service: {}, ip: {}", service.getName(), JSON.toJSONString(instance)); deleteIP(instance); @@ -111,29 +112,6 @@ public class ClientBeatCheckTask implements Runnable { } - private long getInstanceHeartBeatTimeOut(Instance instance) { - Map metaData = instance.getMetadata(); - if(metaData == null || metaData.isEmpty()) { - return Constants.DEFAULT_HEART_BEAT_TIMEOUT; - } - String timeout = metaData.get(Constants.HEART_BEAT_TIMEOUT); - if(!StringUtils.isEmpty(timeout) && timeout.matches(NUMBER_PATTERN)){ - return Long.valueOf(timeout); - } - return Constants.DEFAULT_HEART_BEAT_TIMEOUT; - } - - private long getIpDeleteTimeout(Instance instance) { - Map metaData = instance.getMetadata(); - if(metaData == null || metaData.isEmpty()) { - return Constants.DEFAULT_IP_DELETE_TIMEOUT; - } - String timeout = metaData.get(Constants.IP_DELETE_TIMEOUT); - if(!StringUtils.isEmpty(timeout) && timeout.matches(NUMBER_PATTERN)){ - return Long.valueOf(timeout); - } - return Constants.DEFAULT_IP_DELETE_TIMEOUT; - } private void deleteIP(Instance instance) { From e6631316b4f79a11a08e5905d1977bbc0728aa38 Mon Sep 17 00:00:00 2001 From: IanCao Date: Tue, 18 Jun 2019 17:32:14 +0800 Subject: [PATCH 05/15] add ClientBeatCheckTaskTest junit test --- .../healthcheck/ClientBeatCheckTaskTest.java | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 naming/src/test/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTaskTest.java diff --git a/naming/src/test/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTaskTest.java b/naming/src/test/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTaskTest.java new file mode 100644 index 000000000..5012360de --- /dev/null +++ b/naming/src/test/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTaskTest.java @@ -0,0 +1,129 @@ +package com.alibaba.nacos.naming.healthcheck; + +import com.alibaba.nacos.api.naming.PreservedMetadataKeys; +import com.alibaba.nacos.core.utils.SystemUtils; +import com.alibaba.nacos.naming.core.*; +import com.alibaba.nacos.naming.misc.GlobalConfig; +import com.alibaba.nacos.naming.push.PushService; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.Spy; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.util.ReflectionTestUtils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author caoyixiong + */ +@RunWith(MockitoJUnitRunner.Silent.class) +public class ClientBeatCheckTaskTest { + + @InjectMocks + @Spy + private ClientBeatCheckTask clientBeatCheckTask; + @Mock + private DistroMapper distroMapperSpy; + @Mock + private Service serviceSpy; + @Mock + private GlobalConfig globalConfig; + @Mock + private PushService pushService; + + + @Before + public void init() { + ReflectionTestUtils.setField(clientBeatCheckTask, "service", serviceSpy); + Mockito.doReturn(distroMapperSpy).when(clientBeatCheckTask).getDistroMapper(); + Mockito.doReturn(globalConfig).when(clientBeatCheckTask).getGlobalConfig(); + Mockito.doReturn(pushService).when(clientBeatCheckTask).getPushService(); + } + + @Test + public void testHeartBeatNotTimeout() { + List instances = new ArrayList<>(); + Instance instance = new Instance(); + instance.setLastBeat(System.currentTimeMillis()); + instance.setMarked(false); + instance.setHealthy(true); + Map metadata = new HashMap<>(); + metadata.put(PreservedMetadataKeys.HEART_BEAT_TIMEOUT, "1000000000"); + instance.setMetadata(metadata); + instances.add(instance); + Mockito.when(serviceSpy.allIPs(true)).thenReturn(instances); + + Mockito.doReturn("test").when(serviceSpy).getName(); + Mockito.doReturn(true).when(distroMapperSpy).responsible(Mockito.anyString()); + clientBeatCheckTask.run(); + Assert.assertTrue(instance.isHealthy()); + } + + @Test + public void testHeartBeatTimeout() { + List instances = new ArrayList<>(); + Instance instance = new Instance(); + instance.setLastBeat(System.currentTimeMillis() - 1000); + instance.setMarked(false); + instance.setHealthy(true); + Map metadata = new HashMap<>(); + metadata.put(PreservedMetadataKeys.HEART_BEAT_TIMEOUT, "10"); + instance.setMetadata(metadata); + instances.add(instance); + Mockito.doReturn("test").when(serviceSpy).getName(); + Mockito.doReturn(true).when(distroMapperSpy).responsible(Mockito.anyString()); + + Mockito.when(serviceSpy.allIPs(true)).thenReturn(instances); + + clientBeatCheckTask.run(); + Assert.assertFalse(instance.isHealthy()); + } + + @Test + public void testIpDeleteTimeOut() { + List instances = new ArrayList<>(); + Instance instance = new Instance(); + instance.setLastBeat(System.currentTimeMillis()); + instance.setMarked(true); + instance.setHealthy(true); + Map metadata = new HashMap<>(); + metadata.put(PreservedMetadataKeys.IP_DELETE_TIMEOUT, "10"); + instance.setMetadata(metadata); + instances.add(instance); + Mockito.doReturn(true).when(distroMapperSpy).responsible(null); + Mockito.doReturn(true).when(globalConfig).isExpireInstance(); + Mockito.when(serviceSpy.allIPs(true)).thenReturn(instances); + + clientBeatCheckTask.run(); + } + + @Test + public void testIpDeleteNotTimeOut() { + List instances = new ArrayList<>(); + Instance instance = new Instance(); + instance.setLastBeat(System.currentTimeMillis()); + instance.setMarked(true); + instance.setHealthy(true); + Map metadata = new HashMap<>(); + metadata.put(PreservedMetadataKeys.IP_DELETE_TIMEOUT, "10000"); + instance.setMetadata(metadata); + instances.add(instance); + + Mockito.doReturn(true).when(distroMapperSpy).responsible(null); + Mockito.doReturn(true).when(globalConfig).isExpireInstance(); + Mockito.when(serviceSpy.allIPs(true)).thenReturn(instances); + + clientBeatCheckTask.run(); + } +} From d7908c651d79c88f927da110ed006e1aeb0bab16 Mon Sep 17 00:00:00 2001 From: IanCao Date: Tue, 18 Jun 2019 17:49:44 +0800 Subject: [PATCH 06/15] change into 2 threadpool(mainloop & taskThreadpool) --- .../nacos/client/naming/beat/BeatReactor.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java index 92f67a8df..46453dc8f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java @@ -30,7 +30,8 @@ import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; */ public class BeatReactor { - private ScheduledExecutorService executorService; + private ScheduledExecutorService taskExecutorService; + private ExecutorService mainLoopExecutor; private volatile long clientBeatInterval = 5 * 1000; @@ -45,7 +46,7 @@ public class BeatReactor { public BeatReactor(NamingProxy serverProxy, int threadCount) { this.serverProxy = serverProxy; - executorService = new ScheduledThreadPoolExecutor(threadCount, new ThreadFactory() { + taskExecutorService = new ScheduledThreadPoolExecutor(threadCount, new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r); @@ -55,7 +56,17 @@ public class BeatReactor { } }); - executorService.schedule(new BeatProcessor(), 0, TimeUnit.MILLISECONDS); + mainLoopExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r); + thread.setDaemon(true); + thread.setName("com.alibaba.nacos.naming.beat.sender.mainloop"); + return thread; + } + }); + + mainLoopExecutor.execute(new BeatProcessor()); } public void addBeatInfo(String serviceName, BeatInfo beatInfo) { @@ -89,7 +100,7 @@ public class BeatReactor { continue; } beatInfo.setScheduled(true); - executorService.schedule(new BeatTask(beatInfo), 0, TimeUnit.MILLISECONDS); + taskExecutorService.schedule(new BeatTask(beatInfo), 0, TimeUnit.MILLISECONDS); } } catch (Exception e) { NAMING_LOGGER.error("[CLIENT-BEAT] Exception while scheduling beat.", e); From f2d22f181ac5a90cef7eed04add6903995aa951f Mon Sep 17 00:00:00 2001 From: IanCao Date: Wed, 19 Jun 2019 16:58:27 +0800 Subject: [PATCH 07/15] fix --- .../nacos/client/naming/beat/BeatInfo.java | 10 ++-- .../nacos/client/naming/beat/BeatReactor.java | 52 ++++--------------- 2 files changed, 15 insertions(+), 47 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java index 8a8ab263c..4a49d7802 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java @@ -32,7 +32,7 @@ public class BeatInfo { private Map metadata; private volatile boolean scheduled; private volatile long period; - private volatile long time; + private volatile boolean stop; @Override public String toString() { @@ -103,11 +103,11 @@ public class BeatInfo { this.period = period; } - public long getTime() { - return time; + public boolean isStop() { + return stop; } - public void setTime(long time) { - this.time = time; + public void setStop(boolean stop) { + this.stop = stop; } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java index 46453dc8f..e0844e625 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java @@ -30,8 +30,7 @@ import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; */ public class BeatReactor { - private ScheduledExecutorService taskExecutorService; - private ExecutorService mainLoopExecutor; + private ScheduledExecutorService executorService; private volatile long clientBeatInterval = 5 * 1000; @@ -46,7 +45,7 @@ public class BeatReactor { public BeatReactor(NamingProxy serverProxy, int threadCount) { this.serverProxy = serverProxy; - taskExecutorService = new ScheduledThreadPoolExecutor(threadCount, new ThreadFactory() { + executorService = new ScheduledThreadPoolExecutor(threadCount, new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r); @@ -55,60 +54,27 @@ public class BeatReactor { return thread; } }); - - mainLoopExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() { - @Override - public Thread newThread(Runnable r) { - Thread thread = new Thread(r); - thread.setDaemon(true); - thread.setName("com.alibaba.nacos.naming.beat.sender.mainloop"); - return thread; - } - }); - - mainLoopExecutor.execute(new BeatProcessor()); } public void addBeatInfo(String serviceName, BeatInfo beatInfo) { NAMING_LOGGER.info("[BEAT] adding beat: {} to beat map.", beatInfo); dom2Beat.put(buildKey(serviceName, beatInfo.getIp(), beatInfo.getPort()), beatInfo); + executorService.schedule(new BeatTask(beatInfo), 0, TimeUnit.MILLISECONDS); MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size()); } public void removeBeatInfo(String serviceName, String ip, int port) { NAMING_LOGGER.info("[BEAT] removing beat: {}:{}:{} from beat map.", serviceName, ip, port); - dom2Beat.remove(buildKey(serviceName, ip, port)); + BeatInfo beatInfo = dom2Beat.remove(buildKey(serviceName, ip, port)); + beatInfo.setStop(true); MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size()); } - public String buildKey(String serviceName, String ip, int port) { + private String buildKey(String serviceName, String ip, int port) { return serviceName + Constants.NAMING_INSTANCE_ID_SPLITTER + ip + Constants.NAMING_INSTANCE_ID_SPLITTER + port; } - class BeatProcessor implements Runnable { - @Override - public void run() { - while (true) { - try { - for (Map.Entry entry : dom2Beat.entrySet()) { - BeatInfo beatInfo = entry.getValue(); - if (beatInfo.getTime() > System.currentTimeMillis()) { - continue; - } - if (beatInfo.isScheduled()) { - continue; - } - beatInfo.setScheduled(true); - taskExecutorService.schedule(new BeatTask(beatInfo), 0, TimeUnit.MILLISECONDS); - } - } catch (Exception e) { - NAMING_LOGGER.error("[CLIENT-BEAT] Exception while scheduling beat.", e); - } - } - } - } - class BeatTask implements Runnable { BeatInfo beatInfo; @@ -120,9 +86,11 @@ public class BeatReactor { @Override public void run() { long result = serverProxy.sendBeat(beatInfo); - beatInfo.setScheduled(false); long nextTime = result > 0 ? System.currentTimeMillis() + result : System.currentTimeMillis() + beatInfo.getPeriod(); - beatInfo.setTime(nextTime); + if (beatInfo.isStop()) { + return; + } + executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS); } } } From 5757881c0e798b0effdd98e5149b000b930fd7a7 Mon Sep 17 00:00:00 2001 From: IanCao Date: Wed, 19 Jun 2019 16:59:28 +0800 Subject: [PATCH 08/15] update --- .../java/com/alibaba/nacos/client/naming/beat/BeatReactor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java index e0844e625..f1654c846 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java @@ -86,10 +86,10 @@ public class BeatReactor { @Override public void run() { long result = serverProxy.sendBeat(beatInfo); - long nextTime = result > 0 ? System.currentTimeMillis() + result : System.currentTimeMillis() + beatInfo.getPeriod(); if (beatInfo.isStop()) { return; } + long nextTime = result > 0 ? System.currentTimeMillis() + result : System.currentTimeMillis() + beatInfo.getPeriod(); executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS); } } From 16f8a16cb37bcf37c4d1dfc12f50dd07d56f9350 Mon Sep 17 00:00:00 2001 From: IanCao Date: Wed, 19 Jun 2019 17:13:35 +0800 Subject: [PATCH 09/15] fix bug --- .../java/com/alibaba/nacos/client/naming/NacosNamingService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index 5185b5f67..d37971441 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -196,7 +196,6 @@ public class NacosNamingService implements NamingService { beatInfo.setWeight(instance.getWeight()); beatInfo.setMetadata(instance.getMetadata()); beatInfo.setScheduled(false); - beatInfo.setTime(System.currentTimeMillis()); long instanceInterval = instance.getInstanceHeartBeatInterval(); beatInfo.setPeriod(instanceInterval == 0 ? DEFAULT_HEART_BEAT_INTERVAL : instanceInterval); From 7b839d0f8344f39fc4c4cbd00e5ac82217aaeeaa Mon Sep 17 00:00:00 2001 From: IanCao Date: Wed, 19 Jun 2019 19:46:40 +0800 Subject: [PATCH 10/15] fix bug --- .../java/com/alibaba/nacos/client/naming/beat/BeatReactor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java index f1654c846..c2dc81374 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java @@ -89,7 +89,7 @@ public class BeatReactor { if (beatInfo.isStop()) { return; } - long nextTime = result > 0 ? System.currentTimeMillis() + result : System.currentTimeMillis() + beatInfo.getPeriod(); + long nextTime = result > 0 ? result : beatInfo.getPeriod(); executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS); } } From 490f194b820496a2a386c37a4fbdb943e58e21f6 Mon Sep 17 00:00:00 2001 From: IanCao Date: Thu, 20 Jun 2019 17:53:28 +0800 Subject: [PATCH 11/15] server update client interval --- .../alibaba/nacos/naming/controllers/InstanceController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java index 24a85f7f0..d7a19d413 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java @@ -263,7 +263,7 @@ public class InstanceController { } service.processClientBeat(clientBeat); - + result.put("clientBeatInterval", instance.getInstanceHeartBeatInterval()); return result; } From 5ad978b663fc489f080304753cff44ef420aeb2f Mon Sep 17 00:00:00 2001 From: caoyixiong Date: Thu, 20 Jun 2019 19:53:17 +0800 Subject: [PATCH 12/15] add BeatReactorTest --- client/pom.xml | 5 ++ .../alibaba/nacos/client/BeatReactorTest.java | 59 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java diff --git a/client/pom.xml b/client/pom.xml index 2e3fcac5d..dec0c14df 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -111,6 +111,11 @@ simpleclient 0.5.0 + + org.mockito + mockito-core + test + diff --git a/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java b/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java new file mode 100644 index 000000000..b397c36f2 --- /dev/null +++ b/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java @@ -0,0 +1,59 @@ +package com.alibaba.nacos.client; + +import com.alibaba.nacos.client.naming.beat.BeatInfo; +import com.alibaba.nacos.client.naming.beat.BeatReactor; +import com.alibaba.nacos.client.naming.net.NamingProxy; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.concurrent.ScheduledThreadPoolExecutor; + +/** + * @author caoyixiong + * @Date: 2019/6/20 + * @Copyright (c) 2015, lianjia.com All Rights Reserved + */ +@RunWith(MockitoJUnitRunner.class) +public class BeatReactorTest { + + @Mock + private NamingProxy namingProxy; + + @Test + public void test() throws NoSuchFieldException, IllegalAccessException, InterruptedException { + BeatReactor beatReactor = new BeatReactor(namingProxy); + + BeatInfo beatInfo = new BeatInfo(); + beatInfo.setServiceName("test"); + beatInfo.setIp("11.11.11.11"); + beatInfo.setPort(1234); + beatInfo.setCluster("clusterName"); + beatInfo.setWeight(1); + beatInfo.setMetadata(new HashMap()); + beatInfo.setScheduled(false); + beatInfo.setPeriod(1000L); + + Mockito.doReturn(0L).when(namingProxy).sendBeat(beatInfo); + beatReactor.addBeatInfo("testService", beatInfo); + + Assert.assertEquals(1, getActiveThread(beatReactor)); + Thread.sleep(1100L); + beatReactor.removeBeatInfo("testService", beatInfo.getIp(), beatInfo.getPort()); + Thread.sleep(3100L); + Assert.assertEquals(0, getActiveThread(beatReactor)); + } + + private int getActiveThread(BeatReactor beatReactor) throws NoSuchFieldException, IllegalAccessException { + Field field = BeatReactor.class.getDeclaredField("executorService"); + field.setAccessible(true); + ScheduledThreadPoolExecutor scheduledExecutorService = (ScheduledThreadPoolExecutor) field.get(beatReactor); + return scheduledExecutorService.getQueue().size(); + } + +} From 887d3b6c634ff67cf7ac0096a906095ddba0fdec Mon Sep 17 00:00:00 2001 From: caoyixiong <32847163+IanCao@users.noreply.github.com> Date: Thu, 20 Jun 2019 20:17:06 +0800 Subject: [PATCH 13/15] Update BeatReactorTest.java --- .../src/test/java/com/alibaba/nacos/client/BeatReactorTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java b/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java index b397c36f2..b9043d529 100644 --- a/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java @@ -16,8 +16,6 @@ import java.util.concurrent.ScheduledThreadPoolExecutor; /** * @author caoyixiong - * @Date: 2019/6/20 - * @Copyright (c) 2015, lianjia.com All Rights Reserved */ @RunWith(MockitoJUnitRunner.class) public class BeatReactorTest { From b2242194f421440cecfeefc0c34126ee31e18123 Mon Sep 17 00:00:00 2001 From: caoyixiong Date: Thu, 20 Jun 2019 23:11:02 +0800 Subject: [PATCH 14/15] fix --- .../main/java/com/alibaba/nacos/api/naming/pojo/Instance.java | 1 - 1 file changed, 1 deletion(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java index 0398a5a5e..2a31ecc44 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java @@ -16,7 +16,6 @@ package com.alibaba.nacos.api.naming.pojo; import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.PreservedMetadataKeys; import org.apache.commons.lang3.StringUtils; From 5c108ea0c44e9542c79f4438cbf8d50779b01885 Mon Sep 17 00:00:00 2001 From: caoyixiong Date: Sat, 22 Jun 2019 10:40:53 +0800 Subject: [PATCH 15/15] fix --- .../com/alibaba/nacos/client/naming/beat/BeatInfo.java | 10 +++++----- .../alibaba/nacos/client/naming/beat/BeatReactor.java | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java index 4a49d7802..d405838f7 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java @@ -32,7 +32,7 @@ public class BeatInfo { private Map metadata; private volatile boolean scheduled; private volatile long period; - private volatile boolean stop; + private volatile boolean stopped; @Override public String toString() { @@ -103,11 +103,11 @@ public class BeatInfo { this.period = period; } - public boolean isStop() { - return stop; + public boolean isStopped() { + return stopped; } - public void setStop(boolean stop) { - this.stop = stop; + public void setStopped(boolean stopped) { + this.stopped = stopped; } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java index c2dc81374..700555538 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java @@ -66,7 +66,7 @@ public class BeatReactor { public void removeBeatInfo(String serviceName, String ip, int port) { NAMING_LOGGER.info("[BEAT] removing beat: {}:{}:{} from beat map.", serviceName, ip, port); BeatInfo beatInfo = dom2Beat.remove(buildKey(serviceName, ip, port)); - beatInfo.setStop(true); + beatInfo.setStopped(true); MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size()); } @@ -86,7 +86,7 @@ public class BeatReactor { @Override public void run() { long result = serverProxy.sendBeat(beatInfo); - if (beatInfo.isStop()) { + if (beatInfo.isStopped()) { return; } long nextTime = result > 0 ? result : beatInfo.getPeriod();