From 44b739c8b5565651ff4f5bdb0378dc49378810cd Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Thu, 28 Sep 2023 11:14:37 +0800 Subject: [PATCH 01/32] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=A3=81=E7=9B=98?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0Nacos=E5=AE=A2=E6=88=B7=E7=AB=AF=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E9=99=8D=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/naming/NacosNamingService.java | 189 ++++++----- .../client/naming/backups/FailoverData.java | 52 +++ .../naming/backups/FailoverDataSource.java | 49 +++ .../naming/backups/FailoverReactor.java | 303 +++++++----------- .../client/naming/backups/FailoverSwitch.java | 56 ++++ .../naming/backups/NamingFailoverData.java | 29 ++ .../datasource/DiskFailoverDataSource.java | 202 ++++++++++++ .../naming/cache/ServiceInfoHolder.java | 88 ++--- .../client/naming/utils/UtilAndComs.java | 2 + ...s.client.naming.backups.FailoverDataSource | 18 ++ .../naming/backups/FailoverReactorTest.java | 20 +- .../DiskFailoverDataSourceTest.java | 34 ++ .../naming/cache/ServiceInfoHolderTest.java | 6 +- 13 files changed, 722 insertions(+), 326 deletions(-) create mode 100644 client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java create mode 100644 client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java create mode 100644 client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java create mode 100644 client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java create mode 100644 client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java create mode 100644 client/src/main/resources/META-INF/services/com.alibaba.nacos.client.naming.backups.FailoverDataSource create mode 100644 client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java 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 fa7f063becf..48d70bdb936 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 @@ -36,16 +36,11 @@ import com.alibaba.nacos.client.naming.utils.CollectionUtils; import com.alibaba.nacos.client.naming.utils.InitUtils; import com.alibaba.nacos.client.naming.utils.UtilAndComs; -import com.alibaba.nacos.client.utils.PreInitUtils; import com.alibaba.nacos.client.utils.ValidatorUtils; import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.utils.StringUtils; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Properties; -import java.util.UUID; +import java.util.*; /** * Nacos Naming Service. @@ -54,47 +49,47 @@ */ @SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule") public class NacosNamingService implements NamingService { - + private static final String DEFAULT_NAMING_LOG_FILE_PATH = "naming.log"; - + private static final String UP = "UP"; - + private static final String DOWN = "DOWN"; - + /** * Each Naming service should have different namespace. */ private String namespace; - + private String logName; - + private ServiceInfoHolder serviceInfoHolder; - + private InstancesChangeNotifier changeNotifier; - + private NamingClientProxy clientProxy; - + private String notifierEventScope; - + public NacosNamingService(String serverList) throws NacosException { Properties properties = new Properties(); properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList); init(properties); } - + public NacosNamingService(Properties properties) throws NacosException { init(properties); } - + private void init(Properties properties) throws NacosException { - PreInitUtils.asyncPreLoadCostComponent(); final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties); + ValidatorUtils.checkInitParam(nacosClientProperties); this.namespace = InitUtils.initNamespaceForNaming(nacosClientProperties); InitUtils.initSerialization(); InitUtils.initWebRootContext(nacosClientProperties); initLogName(nacosClientProperties); - + this.notifierEventScope = UUID.randomUUID().toString(); this.changeNotifier = new InstancesChangeNotifier(this.notifierEventScope); NotifyCenter.registerToPublisher(InstancesChangeEvent.class, 16384); @@ -103,26 +98,26 @@ private void init(Properties properties) throws NacosException { this.clientProxy = new NamingClientProxyDelegate(this.namespace, serviceInfoHolder, nacosClientProperties, changeNotifier); } - + private void initLogName(NacosClientProperties properties) { logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME, DEFAULT_NAMING_LOG_FILE_PATH); } - + @Override public void registerInstance(String serviceName, String ip, int port) throws NacosException { registerInstance(serviceName, ip, port, Constants.DEFAULT_CLUSTER_NAME); } - + @Override public void registerInstance(String serviceName, String groupName, String ip, int port) throws NacosException { registerInstance(serviceName, groupName, ip, port, Constants.DEFAULT_CLUSTER_NAME); } - + @Override public void registerInstance(String serviceName, String ip, int port, String clusterName) throws NacosException { registerInstance(serviceName, Constants.DEFAULT_GROUP, ip, port, clusterName); } - + @Override public void registerInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException { @@ -133,47 +128,47 @@ public void registerInstance(String serviceName, String groupName, String ip, in instance.setClusterName(clusterName); registerInstance(serviceName, groupName, instance); } - + @Override public void registerInstance(String serviceName, Instance instance) throws NacosException { registerInstance(serviceName, Constants.DEFAULT_GROUP, instance); } - + @Override public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException { NamingUtils.checkInstanceIsLegal(instance); clientProxy.registerService(serviceName, groupName, instance); } - + @Override public void batchRegisterInstance(String serviceName, String groupName, List instances) throws NacosException { NamingUtils.batchCheckInstanceIsLegal(instances); clientProxy.batchRegisterService(serviceName, groupName, instances); } - + @Override public void batchDeregisterInstance(String serviceName, String groupName, List instances) throws NacosException { NamingUtils.batchCheckInstanceIsLegal(instances); clientProxy.batchDeregisterService(serviceName, groupName, instances); } - + @Override public void deregisterInstance(String serviceName, String ip, int port) throws NacosException { deregisterInstance(serviceName, ip, port, Constants.DEFAULT_CLUSTER_NAME); } - + @Override public void deregisterInstance(String serviceName, String groupName, String ip, int port) throws NacosException { deregisterInstance(serviceName, groupName, ip, port, Constants.DEFAULT_CLUSTER_NAME); } - + @Override public void deregisterInstance(String serviceName, String ip, int port, String clusterName) throws NacosException { deregisterInstance(serviceName, Constants.DEFAULT_GROUP, ip, port, clusterName); } - + @Override public void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException { @@ -183,119 +178,137 @@ public void deregisterInstance(String serviceName, String groupName, String ip, instance.setClusterName(clusterName); deregisterInstance(serviceName, groupName, instance); } - + @Override public void deregisterInstance(String serviceName, Instance instance) throws NacosException { deregisterInstance(serviceName, Constants.DEFAULT_GROUP, instance); } - + @Override public void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException { clientProxy.deregisterService(serviceName, groupName, instance); } - + @Override public List getAllInstances(String serviceName) throws NacosException { return getAllInstances(serviceName, new ArrayList<>()); } - + @Override public List getAllInstances(String serviceName, String groupName) throws NacosException { return getAllInstances(serviceName, groupName, new ArrayList<>()); } - + @Override public List getAllInstances(String serviceName, boolean subscribe) throws NacosException { return getAllInstances(serviceName, new ArrayList<>(), subscribe); } - + @Override public List getAllInstances(String serviceName, String groupName, boolean subscribe) throws NacosException { return getAllInstances(serviceName, groupName, new ArrayList<>(), subscribe); } - + @Override public List getAllInstances(String serviceName, List clusters) throws NacosException { return getAllInstances(serviceName, clusters, true); } - + @Override public List getAllInstances(String serviceName, String groupName, List clusters) throws NacosException { return getAllInstances(serviceName, groupName, clusters, true); } - + @Override public List getAllInstances(String serviceName, List clusters, boolean subscribe) throws NacosException { return getAllInstances(serviceName, Constants.DEFAULT_GROUP, clusters, subscribe); } - + @Override public List getAllInstances(String serviceName, String groupName, List clusters, - boolean subscribe) throws NacosException { + boolean subscribe) throws NacosException { ServiceInfo serviceInfo; + List list; String clusterString = StringUtils.join(clusters, ","); - if (subscribe) { - serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); - if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { - serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); + if (serviceInfoHolder.isFailoverSwitch()) { + list = new ArrayList<>(); + serviceInfo = serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString); + if (serviceInfo != null && serviceInfo.getHosts().size() > 0) { + serviceInfo.getHosts(); + if (!clientProxy.isSubscribed(serviceName, groupName, clusterString)){ + serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); + } + } else{ + if (subscribe) { + serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); + if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { + serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); + } + } + } + }else { + if (subscribe) { + serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); + if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { + serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); + } + } else { + serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, false); } - } else { - serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, false); } - List list; + if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { return new ArrayList<>(); } return list; } - + @Override public List selectInstances(String serviceName, boolean healthy) throws NacosException { return selectInstances(serviceName, new ArrayList<>(), healthy); } - + @Override public List selectInstances(String serviceName, String groupName, boolean healthy) throws NacosException { return selectInstances(serviceName, groupName, healthy, true); } - + @Override public List selectInstances(String serviceName, boolean healthy, boolean subscribe) throws NacosException { return selectInstances(serviceName, new ArrayList<>(), healthy, subscribe); } - + @Override public List selectInstances(String serviceName, String groupName, boolean healthy, boolean subscribe) throws NacosException { return selectInstances(serviceName, groupName, new ArrayList<>(), healthy, subscribe); } - + @Override public List selectInstances(String serviceName, List clusters, boolean healthy) throws NacosException { return selectInstances(serviceName, clusters, healthy, true); } - + @Override public List selectInstances(String serviceName, String groupName, List clusters, boolean healthy) throws NacosException { return selectInstances(serviceName, groupName, clusters, healthy, true); } - + @Override public List selectInstances(String serviceName, List clusters, boolean healthy, boolean subscribe) throws NacosException { return selectInstances(serviceName, Constants.DEFAULT_GROUP, clusters, healthy, subscribe); } - + @Override public List selectInstances(String serviceName, String groupName, List clusters, boolean healthy, - boolean subscribe) throws NacosException { - + boolean subscribe) throws NacosException { ServiceInfo serviceInfo; String clusterString = StringUtils.join(clusters, ","); if (subscribe) { @@ -308,13 +321,13 @@ public List selectInstances(String serviceName, String groupName, List } return selectInstances(serviceInfo, healthy); } - + private List selectInstances(ServiceInfo serviceInfo, boolean healthy) { List list; if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { return new ArrayList<>(); } - + Iterator iterator = list.iterator(); while (iterator.hasNext()) { Instance instance = iterator.next(); @@ -322,51 +335,51 @@ private List selectInstances(ServiceInfo serviceInfo, boolean healthy) iterator.remove(); } } - + return list; } - + @Override public Instance selectOneHealthyInstance(String serviceName) throws NacosException { return selectOneHealthyInstance(serviceName, new ArrayList<>()); } - + @Override public Instance selectOneHealthyInstance(String serviceName, String groupName) throws NacosException { return selectOneHealthyInstance(serviceName, groupName, true); } - + @Override public Instance selectOneHealthyInstance(String serviceName, boolean subscribe) throws NacosException { return selectOneHealthyInstance(serviceName, new ArrayList<>(), subscribe); } - + @Override public Instance selectOneHealthyInstance(String serviceName, String groupName, boolean subscribe) throws NacosException { return selectOneHealthyInstance(serviceName, groupName, new ArrayList<>(), subscribe); } - + @Override public Instance selectOneHealthyInstance(String serviceName, List clusters) throws NacosException { return selectOneHealthyInstance(serviceName, clusters, true); } - + @Override public Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters) throws NacosException { return selectOneHealthyInstance(serviceName, groupName, clusters, true); } - + @Override public Instance selectOneHealthyInstance(String serviceName, List clusters, boolean subscribe) throws NacosException { return selectOneHealthyInstance(serviceName, Constants.DEFAULT_GROUP, clusters, subscribe); } - + @Override public Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters, - boolean subscribe) throws NacosException { + boolean subscribe) throws NacosException { String clusterString = StringUtils.join(clusters, ","); if (subscribe) { ServiceInfo serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); @@ -379,22 +392,22 @@ public Instance selectOneHealthyInstance(String serviceName, String groupName, L return Balancer.RandomByWeight.selectHost(serviceInfo); } } - + @Override public void subscribe(String serviceName, EventListener listener) throws NacosException { subscribe(serviceName, new ArrayList<>(), listener); } - + @Override public void subscribe(String serviceName, String groupName, EventListener listener) throws NacosException { subscribe(serviceName, groupName, new ArrayList<>(), listener); } - + @Override public void subscribe(String serviceName, List clusters, EventListener listener) throws NacosException { subscribe(serviceName, Constants.DEFAULT_GROUP, clusters, listener); } - + @Override public void subscribe(String serviceName, String groupName, List clusters, EventListener listener) throws NacosException { @@ -405,22 +418,22 @@ public void subscribe(String serviceName, String groupName, List cluster changeNotifier.registerListener(groupName, serviceName, clusterString, listener); clientProxy.subscribe(serviceName, groupName, clusterString); } - + @Override public void unsubscribe(String serviceName, EventListener listener) throws NacosException { unsubscribe(serviceName, new ArrayList<>(), listener); } - + @Override public void unsubscribe(String serviceName, String groupName, EventListener listener) throws NacosException { unsubscribe(serviceName, groupName, new ArrayList<>(), listener); } - + @Override public void unsubscribe(String serviceName, List clusters, EventListener listener) throws NacosException { unsubscribe(serviceName, Constants.DEFAULT_GROUP, clusters, listener); } - + @Override public void unsubscribe(String serviceName, String groupName, List clusters, EventListener listener) throws NacosException { @@ -430,39 +443,39 @@ public void unsubscribe(String serviceName, String groupName, List clust clientProxy.unsubscribe(serviceName, groupName, clustersString); } } - + @Override public ListView getServicesOfServer(int pageNo, int pageSize) throws NacosException { return getServicesOfServer(pageNo, pageSize, Constants.DEFAULT_GROUP); } - + @Override public ListView getServicesOfServer(int pageNo, int pageSize, String groupName) throws NacosException { return getServicesOfServer(pageNo, pageSize, groupName, null); } - + @Override public ListView getServicesOfServer(int pageNo, int pageSize, AbstractSelector selector) throws NacosException { return getServicesOfServer(pageNo, pageSize, Constants.DEFAULT_GROUP, selector); } - + @Override public ListView getServicesOfServer(int pageNo, int pageSize, String groupName, AbstractSelector selector) throws NacosException { return clientProxy.getServiceList(pageNo, pageSize, groupName, selector); } - + @Override public List getSubscribeServices() { return changeNotifier.getSubscribeServices(); } - + @Override public String getServerStatus() { return clientProxy.serverHealthy() ? UP : DOWN; } - + @Override public void shutDown() throws NacosException { serviceInfoHolder.shutdown(); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java new file mode 100644 index 00000000000..7b2b97f28a0 --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java @@ -0,0 +1,52 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.client.naming.backups; + +public class FailoverData { + + private DataType dataType; + + /** + * 容灾数据,子类设置具体类型 + */ + private Object data; + + public FailoverData(DataType dataType, Object data) { + this.data = data; + this.dataType = dataType; + } + + public enum DataType { + naming, + config + } + + public DataType getDataType() { + return dataType; + } + + public void setDataType(DataType dataType) { + this.dataType = dataType; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } +} diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java new file mode 100644 index 00000000000..457ace1cd38 --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java @@ -0,0 +1,49 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.client.naming.backups; + +import java.util.Map; + +/** + * Failover Service Interface. + * + * @author Nacos + */ +public interface FailoverDataSource { + + /** + * Get current disaster recovery switch. + * + * @return + */ + FailoverSwitch getSwitch(); + + + /** + * Get current disaster recovery data. + * + * @return map + */ + Map getFailoverData(); + + /** + * Save disaster recovery data. + * + * @param failoverDataMap + */ + void saveFailoverData(Map failoverDataMap); + +} diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index d215bc276fb..e9552c2d204 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -18,32 +18,22 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; -import com.alibaba.nacos.client.naming.cache.ConcurrentDiskUtil; -import com.alibaba.nacos.client.naming.cache.DiskCache; +import com.alibaba.nacos.client.naming.backups.datasource.DiskFailoverDataSource; import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; -import com.alibaba.nacos.client.naming.utils.CollectionUtils; -import com.alibaba.nacos.client.naming.utils.UtilAndComs; +import com.alibaba.nacos.client.naming.event.InstancesChangeEvent; import com.alibaba.nacos.common.lifecycle.Closeable; -import com.alibaba.nacos.common.utils.JacksonUtils; -import com.alibaba.nacos.common.utils.StringUtils; +import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.utils.ThreadUtils; +import io.micrometer.core.instrument.MultiGauge; +import io.micrometer.core.instrument.Tags; -import java.io.BufferedReader; import java.io.File; -import java.io.StringReader; -import java.net.URLDecoder; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.nio.file.Paths; -import java.util.Calendar; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.TimerTask; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; @@ -53,30 +43,34 @@ * @author nkorange */ public class FailoverReactor implements Closeable { - + private static final String FAILOVER_DIR = "/failover"; - - private static final String IS_FAILOVER_MODE = "1"; - - private static final String NO_FAILOVER_MODE = "0"; - - private static final String FAILOVER_MODE_PARAM = "failover-mode"; - + + private Map serviceMap = new ConcurrentHashMap<>(); - - private final Map switchParams = new ConcurrentHashMap<>(); - + + private boolean failoverSwitchEnable; + private static final long DAY_PERIOD_MINUTES = 24 * 60; - + private final String failoverDir; - + private final ServiceInfoHolder serviceInfoHolder; - + private final ScheduledExecutorService executorService; - - public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String cacheDir) { + + private final FailoverDataSource failoverDataSource; + + private final MultiGauge failoverInstanceCounts; + + private String notifierEventScope; + + public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String cacheDir, String notifierEventScope) { this.serviceInfoHolder = serviceInfoHolder; this.failoverDir = cacheDir + FAILOVER_DIR; + this.notifierEventScope = notifierEventScope; + failoverDataSource = new DiskFailoverDataSource(serviceInfoHolder, cacheDir); + failoverInstanceCounts = MultiGauge.builder("nacos_naming_client_failover_instances").register(io.micrometer.core.instrument.Metrics.globalRegistry); // init executorService this.executorService = new ScheduledThreadPoolExecutor(1, r -> { Thread thread = new Thread(r); @@ -86,16 +80,16 @@ public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String cacheDir) { }); this.init(); } - + /** * Init. */ public void init() { - - executorService.scheduleWithFixedDelay(new SwitchRefresher(), 0L, 5000L, TimeUnit.MILLISECONDS); - - executorService.scheduleWithFixedDelay(new DiskFileWriter(), 30, DAY_PERIOD_MINUTES, TimeUnit.MINUTES); - + + executorService.scheduleWithFixedDelay(new FailoverSwitchRefresher(), 0L, 5000L, TimeUnit.MILLISECONDS); + + executorService.scheduleWithFixedDelay(new FailoverDataWriter(), 30, DAY_PERIOD_MINUTES, TimeUnit.MINUTES); + // backup file on startup if failover directory is empty. executorService.schedule(() -> { try { @@ -107,7 +101,7 @@ public void init() { File[] files = cacheDir.listFiles(); if (files == null || files.length <= 0) { - new DiskFileWriter().run(); + new FailoverDataWriter().run(); } } catch (Throwable e) { NAMING_LOGGER.error("[NA] failed to backup file on startup.", e); @@ -115,176 +109,113 @@ public void init() { }, 10000L, TimeUnit.MILLISECONDS); } - - /** - * Add day. - * - * @param date start time - * @param num add day number - * @return new date - */ - public Date addDay(Date date, int num) { - Calendar startDT = Calendar.getInstance(); - startDT.setTime(date); - startDT.add(Calendar.DAY_OF_MONTH, num); - return startDT.getTime(); - } - - @Override - public void shutdown() throws NacosException { - String className = this.getClass().getName(); - NAMING_LOGGER.info("{} do shutdown begin", className); - ThreadUtils.shutdownThreadPool(executorService, NAMING_LOGGER); - NAMING_LOGGER.info("{} do shutdown stop", className); - } - - class SwitchRefresher implements Runnable { - - long lastModifiedMillis = 0L; - + + + class FailoverSwitchRefresher implements Runnable { + @Override public void run() { - try { - File switchFile = Paths.get(failoverDir, UtilAndComs.FAILOVER_SWITCH).toFile(); - if (!switchFile.exists()) { - switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString()); - NAMING_LOGGER.debug("failover switch is not found, {}", switchFile.getName()); - return; + + FailoverSwitch fSwitch = failoverDataSource.getSwitch(); + if (fSwitch != null && fSwitch.getEnabled() && !failoverSwitchEnable) { + failoverSwitchEnable = Boolean.TRUE; + Map map = new ConcurrentHashMap<>(); + Map failoverData = failoverDataSource.getFailoverData(); + for (Map.Entry entry : failoverData.entrySet()) { + map.put(entry.getKey(), (ServiceInfo) entry.getValue().getData()); } - - long modified = switchFile.lastModified(); - - if (lastModifiedMillis < modified) { - lastModifiedMillis = modified; - String failover = ConcurrentDiskUtil.getFileContent(switchFile.getPath(), - Charset.defaultCharset().toString()); - if (!StringUtils.isEmpty(failover)) { - String[] lines = failover.split(DiskCache.getLineSeparator()); - - for (String line : lines) { - String line1 = line.trim(); - if (IS_FAILOVER_MODE.equals(line1)) { - switchParams.put(FAILOVER_MODE_PARAM, Boolean.TRUE.toString()); - NAMING_LOGGER.info("failover-mode is on"); - new FailoverFileReader().run(); - } else if (NO_FAILOVER_MODE.equals(line1)) { - switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString()); - NAMING_LOGGER.info("failover-mode is off"); - } - } - } else { - switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString()); - } + + if (map.size() > 0) { + serviceMap = map; } - - } catch (Throwable e) { - NAMING_LOGGER.error("[NA] failed to read failover switch.", e); + + return; } - } - } - - class FailoverFileReader implements Runnable { - - @Override - public void run() { - Map domMap = new HashMap<>(16); - - BufferedReader reader = null; - try { - - File cacheDir = new File(failoverDir); - if (!cacheDir.exists() && !cacheDir.mkdirs()) { - throw new IllegalStateException("failed to create cache dir: " + failoverDir); - } - - File[] files = cacheDir.listFiles(); - if (files == null) { - return; + + if (fSwitch != null && failoverSwitchEnable && !fSwitch.getEnabled()) { + failoverSwitchEnable = Boolean.FALSE; + Map map = new ConcurrentHashMap<>(); + Map failoverData = failoverDataSource.getFailoverData(); + for (Map.Entry entry : failoverData.entrySet()) { + map.put(entry.getKey(), (ServiceInfo) entry.getValue().getData()); } - - for (File file : files) { - if (!file.isFile()) { - continue; - } - - if (file.getName().equals(UtilAndComs.FAILOVER_SWITCH)) { - continue; - } - - ServiceInfo dom = null; - - try { - dom = new ServiceInfo(URLDecoder.decode(file.getName(), StandardCharsets.UTF_8.name())); - String dataString = ConcurrentDiskUtil.getFileContent(file, - Charset.defaultCharset().toString()); - reader = new BufferedReader(new StringReader(dataString)); - - String json; - if ((json = reader.readLine()) != null) { - try { - dom = JacksonUtils.toObj(json, ServiceInfo.class); - } catch (Exception e) { - NAMING_LOGGER.error("[NA] error while parsing cached dom : {}", json, e); - } - } - - } catch (Exception e) { - NAMING_LOGGER.error("[NA] failed to read cache for dom: {}", file.getName(), e); - } finally { - try { - if (reader != null) { - reader.close(); - } - } catch (Exception e) { - //ignore - } - } - if (dom != null && !CollectionUtils.isEmpty(dom.getHosts())) { - domMap.put(dom.getKey(), dom); + + Map serviceInfoMap = serviceInfoHolder.getServiceInfoMap(); + for (Map.Entry entry : map.entrySet()) { + ServiceInfo oldService = entry.getValue(); + ServiceInfo newService = serviceInfoMap.get(entry.getKey()); + boolean changed = serviceInfoHolder.isChangedServiceInfo(oldService, newService); + if (changed) { + NotifyCenter.publishEvent(new InstancesChangeEvent(notifierEventScope, newService.getName(), newService.getGroupName(), + newService.getClusters(), newService.getHosts())); } } - } catch (Exception e) { - NAMING_LOGGER.error("[NA] failed to read cache file", e); - } - - if (domMap.size() > 0) { - serviceMap = domMap; } + + serviceMap.clear(); } } - - class DiskFileWriter extends TimerTask { - + + class FailoverDataWriter extends TimerTask { + @Override public void run() { - Map map = serviceInfoHolder.getServiceInfoMap(); - for (Map.Entry entry : map.entrySet()) { - ServiceInfo serviceInfo = entry.getValue(); - if (StringUtils.equals(serviceInfo.getKey(), UtilAndComs.ALL_IPS) || StringUtils - .equals(serviceInfo.getName(), UtilAndComs.ENV_LIST_KEY) || StringUtils - .equals(serviceInfo.getName(), UtilAndComs.ENV_CONFIGS) || StringUtils - .equals(serviceInfo.getName(), UtilAndComs.VIP_CLIENT_FILE) || StringUtils - .equals(serviceInfo.getName(), UtilAndComs.ALL_HOSTS)) { - continue; - } - - DiskCache.write(serviceInfo, failoverDir); + if (isFailoverSwitch()) { + return; } + + Map serviceInfoMap = serviceInfoHolder.getServiceInfoMap(); + Map failoverDataMap = new HashMap<>(serviceInfoMap.size()); + for (Map.Entry entry : serviceInfoMap.entrySet()) { + failoverDataMap.put(entry.getKey(), new FailoverData(FailoverData.DataType.naming, entry.getValue())); + } + + failoverDataSource.saveFailoverData(failoverDataMap); + + Set serviceNames = failoverDataSource.getSwitch().getServiceNames(); + + failoverInstanceCounts.register(serviceNames.stream().map(serviceName -> MultiGauge.Row.of(Tags.of("service_name", serviceName), (serviceInfoMap.get(serviceName)).ipCount())).collect(Collectors.toList()), true); } } - + public boolean isFailoverSwitch() { - return Boolean.parseBoolean(switchParams.get(FAILOVER_MODE_PARAM)); + return failoverSwitchEnable; } - + public ServiceInfo getService(String key) { ServiceInfo serviceInfo = serviceMap.get(key); - + if (serviceInfo == null) { serviceInfo = new ServiceInfo(); serviceInfo.setName(key); } - + return serviceInfo; } + + /** + * Add day. + * + * @param date start time + * @param num add day number + * @return new date + */ + public Date addDay(Date date, int num) { + Calendar startDT = Calendar.getInstance(); + startDT.setTime(date); + startDT.add(Calendar.DAY_OF_MONTH, num); + return startDT.getTime(); + } + + /** + * shutdown ThreadPool. + * @throws NacosException + */ + @Override + public void shutdown() throws NacosException { + String className = this.getClass().getName(); + NAMING_LOGGER.info("{} do shutdown begin", className); + ThreadUtils.shutdownThreadPool(executorService, NAMING_LOGGER); + NAMING_LOGGER.info("{} do shutdown stop", className); + } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java new file mode 100644 index 00000000000..55f4b539f46 --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java @@ -0,0 +1,56 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.client.naming.backups; + +import java.util.Set; + +public class FailoverSwitch { + + /** + * 容灾是否打开; + */ + private boolean enabled; + + /** + * 参与容灾的服务名列表 + */ + private Set serviceNames; + + public boolean getEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public Set getServiceNames() { + return serviceNames; + } + + public void setServiceNames(Set serviceNames) { + this.serviceNames = serviceNames; + } + + public FailoverSwitch(boolean enabled) { + this.enabled = enabled; + } + + public FailoverSwitch(boolean enabled, Set serviceNames) { + this.enabled = enabled; + this.serviceNames = serviceNames; + } +} diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java new file mode 100644 index 00000000000..2d1b9856c51 --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java @@ -0,0 +1,29 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.client.naming.backups; + +import com.alibaba.nacos.api.naming.pojo.ServiceInfo; + +public class NamingFailoverData extends FailoverData { + + private NamingFailoverData(ServiceInfo serviceInfo) { + super(DataType.naming, serviceInfo); + } + + public static NamingFailoverData newNamingFailoverData(ServiceInfo serviceInfo) { + return new NamingFailoverData(serviceInfo); + } +} diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java new file mode 100644 index 00000000000..dfd8c14a4f8 --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java @@ -0,0 +1,202 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.client.naming.backups.datasource; + +import com.alibaba.nacos.api.naming.pojo.ServiceInfo; +import com.alibaba.nacos.client.naming.backups.FailoverData; +import com.alibaba.nacos.client.naming.backups.FailoverDataSource; +import com.alibaba.nacos.client.naming.backups.FailoverSwitch; +import com.alibaba.nacos.client.naming.backups.NamingFailoverData; +import com.alibaba.nacos.client.naming.cache.ConcurrentDiskUtil; +import com.alibaba.nacos.client.naming.cache.DiskCache; +import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; +import com.alibaba.nacos.client.naming.utils.CollectionUtils; +import com.alibaba.nacos.client.naming.utils.UtilAndComs; +import com.alibaba.nacos.common.utils.JacksonUtils; +import com.alibaba.nacos.common.utils.StringUtils; + +import java.io.BufferedReader; +import java.io.File; +import java.io.StringReader; +import java.net.URLDecoder; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; + +/** + * + */ +public class DiskFailoverDataSource implements FailoverDataSource { + + private static final String FAILOVER_DIR = "/failover"; + + private static final String IS_FAILOVER_MODE = "1"; + + private static final String NO_FAILOVER_MODE = "0"; + + private Map serviceMap = new ConcurrentHashMap<>(); + + + + private final String failoverDir; + + private long lastModifiedMillis = 0L; + + + public DiskFailoverDataSource(ServiceInfoHolder serviceInfoHolder, String cacheDir) { + this.failoverDir = cacheDir + FAILOVER_DIR; + } + + + class FailoverFileReader implements Runnable { + + @Override + public void run() { + Map domMap = new HashMap<>(200); + + BufferedReader reader = null; + try { + + File cacheDir = new File(failoverDir); + if (!cacheDir.exists() && !cacheDir.mkdirs()) { + throw new IllegalStateException("failed to create cache dir: " + failoverDir); + } + + File[] files = cacheDir.listFiles(); + if (files == null) { + return; + } + + for (File file : files) { + if (!file.isFile()) { + continue; + } + + if (file.getName().equals(UtilAndComs.FAILOVER_SWITCH)) { + continue; + } + + ServiceInfo dom = null; + + try { + dom = new ServiceInfo(URLDecoder.decode(file.getName(), StandardCharsets.UTF_8.name())); + String dataString = ConcurrentDiskUtil.getFileContent(file, + Charset.defaultCharset().toString()); + reader = new BufferedReader(new StringReader(dataString)); + + String json; + if ((json = reader.readLine()) != null) { + try { + dom = JacksonUtils.toObj(json, ServiceInfo.class); + } catch (Exception e) { + NAMING_LOGGER.error("[NA] error while parsing cached dom : {}", json, e); + } + } + + } catch (Exception e) { + NAMING_LOGGER.error("[NA] failed to read cache for dom: {}", file.getName(), e); + } finally { + try { + if (reader != null) { + reader.close(); + } + } catch (Exception e) { + //ignore + } + } + if (dom != null && !CollectionUtils.isEmpty(dom.getHosts())) { + domMap.put(dom.getKey(), NamingFailoverData.newNamingFailoverData(dom)); + } + } + } catch (Exception e) { + NAMING_LOGGER.error("[NA] failed to read cache file", e); + } + + if (domMap.size() > 0) { + serviceMap = domMap; + } + } + } + + @Override + public FailoverSwitch getSwitch() { + try { + File switchFile = Paths.get(failoverDir, UtilAndComs.FAILOVER_SWITCH).toFile(); + if (!switchFile.exists()) { + NAMING_LOGGER.debug("failover switch is not found, {}", switchFile.getName()); + return new FailoverSwitch(Boolean.FALSE); + } + + long modified = switchFile.lastModified(); + + if (lastModifiedMillis < modified) { + lastModifiedMillis = modified; + String failover = ConcurrentDiskUtil.getFileContent(switchFile.getPath(), + Charset.defaultCharset().toString()); + if (!StringUtils.isEmpty(failover)) { + String[] lines = failover.split(DiskCache.getLineSeparator()); + + for (String line : lines) { + String line1 = line.trim(); + if (IS_FAILOVER_MODE.equals(line1)) { + NAMING_LOGGER.info("failover-mode is on"); + new FailoverFileReader().run(); + return new FailoverSwitch(Boolean.TRUE); + } else if (NO_FAILOVER_MODE.equals(line1)) { + NAMING_LOGGER.info("failover-mode is off"); + } + } + } + } + + } catch (Throwable e) { + NAMING_LOGGER.error("[NA] failed to read failover switch.", e); + } + + return new FailoverSwitch(Boolean.FALSE); + } + + + @Override + public Map getFailoverData() { + return serviceMap; + } + + @Override + public void saveFailoverData(Map failoverDataMap) { + // 2.从ServiceInfoHoler复制服务数据写入到容灾数据源FailoverDataSource; + for (Map.Entry entry : failoverDataMap.entrySet()) { + ServiceInfo serviceInfo = (ServiceInfo) entry.getValue().getData(); + if (StringUtils.equals(serviceInfo.getKey(), UtilAndComs.ALL_IPS) || StringUtils + .equals(serviceInfo.getName(), UtilAndComs.ENV_LIST_KEY) || StringUtils + .equals(serviceInfo.getName(), UtilAndComs.ENV_CONFIGS) || StringUtils + .equals(serviceInfo.getName(), UtilAndComs.VIP_CLIENT_FILE) || StringUtils + .equals(serviceInfo.getName(), UtilAndComs.ALL_HOSTS)) { + continue; + } + + DiskCache.write(serviceInfo, failoverDir); + } + + return; + } + +} diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java index 3a4d6311832..c766ace3e00 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java @@ -32,12 +32,7 @@ import com.alibaba.nacos.common.utils.StringUtils; import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -49,25 +44,25 @@ * @author xiweng.yy */ public class ServiceInfoHolder implements Closeable { - + private static final String JM_SNAPSHOT_PATH_PROPERTY = "JM.SNAPSHOT.PATH"; - + private static final String FILE_PATH_NACOS = "nacos"; - + private static final String FILE_PATH_NAMING = "naming"; - + private static final String USER_HOME_PROPERTY = "user.home"; - + private final ConcurrentMap serviceInfoMap; - + private final FailoverReactor failoverReactor; - + private final boolean pushEmptyProtection; - + private String cacheDir; - + private String notifierEventScope; - + public ServiceInfoHolder(String namespace, String notifierEventScope, NacosClientProperties properties) { initCacheDir(namespace, properties); if (isLoadCacheAtStart(properties)) { @@ -75,19 +70,21 @@ public ServiceInfoHolder(String namespace, String notifierEventScope, NacosClien } else { this.serviceInfoMap = new ConcurrentHashMap<>(16); } - this.failoverReactor = new FailoverReactor(this, cacheDir); + + this.failoverReactor = new FailoverReactor(this, cacheDir,notifierEventScope); + this.pushEmptyProtection = isPushEmptyProtect(properties); this.notifierEventScope = notifierEventScope; } - + private void initCacheDir(String namespace, NacosClientProperties properties) { String jmSnapshotPath = properties.getProperty(JM_SNAPSHOT_PATH_PROPERTY); - + String namingCacheRegistryDir = ""; if (properties.getProperty(PropertyKeyConst.NAMING_CACHE_REGISTRY_DIR) != null) { namingCacheRegistryDir = File.separator + properties.getProperty(PropertyKeyConst.NAMING_CACHE_REGISTRY_DIR); } - + if (!StringUtils.isBlank(jmSnapshotPath)) { cacheDir = jmSnapshotPath + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir + File.separator + FILE_PATH_NAMING + File.separator + namespace; @@ -96,7 +93,7 @@ private void initCacheDir(String namespace, NacosClientProperties properties) { + File.separator + FILE_PATH_NAMING + File.separator + namespace; } } - + private boolean isLoadCacheAtStart(NacosClientProperties properties) { boolean loadCacheAtStart = false; if (properties != null && StringUtils @@ -106,7 +103,7 @@ private boolean isLoadCacheAtStart(NacosClientProperties properties) { } return loadCacheAtStart; } - + private boolean isPushEmptyProtect(NacosClientProperties properties) { boolean pushEmptyProtection = false; if (properties != null && StringUtils @@ -116,11 +113,11 @@ private boolean isPushEmptyProtect(NacosClientProperties properties) { } return pushEmptyProtection; } - + public Map getServiceInfoMap() { return serviceInfoMap; } - + public ServiceInfo getServiceInfo(final String serviceName, final String groupName, final String clusters) { NAMING_LOGGER.debug("failover-mode: {}", failoverReactor.isFailoverSwitch()); String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName); @@ -130,7 +127,7 @@ public ServiceInfo getServiceInfo(final String serviceName, final String groupNa } return serviceInfoMap.get(key); } - + /** * Process service json. * @@ -142,7 +139,7 @@ public ServiceInfo processServiceInfo(String json) { serviceInfo.setJsonFromServer(json); return processServiceInfo(serviceInfo); } - + /** * Process service info. * @@ -168,18 +165,21 @@ public ServiceInfo processServiceInfo(ServiceInfo serviceInfo) { if (changed) { NAMING_LOGGER.info("current ips:({}) service: {} -> {}", serviceInfo.ipCount(), serviceInfo.getKey(), JacksonUtils.toJson(serviceInfo.getHosts())); - NotifyCenter.publishEvent(new InstancesChangeEvent(notifierEventScope, serviceInfo.getName(), serviceInfo.getGroupName(), - serviceInfo.getClusters(), serviceInfo.getHosts())); + if (!failoverReactor.isFailoverSwitch()) { + NotifyCenter.publishEvent(new InstancesChangeEvent(notifierEventScope, serviceInfo.getName(), serviceInfo.getGroupName(), + serviceInfo.getClusters(), serviceInfo.getHosts())); + } + DiskCache.write(serviceInfo, cacheDir); } return serviceInfo; } - + private boolean isEmptyOrErrorPush(ServiceInfo serviceInfo) { return null == serviceInfo.getHosts() || (pushEmptyProtection && !serviceInfo.validate()); } - - private boolean isChangedServiceInfo(ServiceInfo oldService, ServiceInfo newService) { + + public boolean isChangedServiceInfo(ServiceInfo oldService, ServiceInfo newService) { if (null == oldService) { NAMING_LOGGER.info("init new ips({}) service: {} -> {}", newService.ipCount(), newService.getKey(), JacksonUtils.toJson(newService.getHosts())); @@ -199,11 +199,11 @@ private boolean isChangedServiceInfo(ServiceInfo oldService, ServiceInfo newServ for (Instance host : newService.getHosts()) { newHostMap.put(host.toInetAddr(), host); } - + Set modHosts = new HashSet<>(); Set newHosts = new HashSet<>(); Set remvHosts = new HashSet<>(); - + List> newServiceHosts = new ArrayList<>( newHostMap.entrySet()); for (Map.Entry entry : newServiceHosts) { @@ -213,12 +213,12 @@ private boolean isChangedServiceInfo(ServiceInfo oldService, ServiceInfo newServ modHosts.add(host); continue; } - + if (!oldHostMap.containsKey(key)) { newHosts.add(host); } } - + for (Map.Entry entry : oldHostMap.entrySet()) { Instance host = entry.getValue(); String key = entry.getKey(); @@ -229,19 +229,19 @@ private boolean isChangedServiceInfo(ServiceInfo oldService, ServiceInfo newServ //add to remove hosts remvHosts.add(host); } - + if (newHosts.size() > 0) { changed = true; NAMING_LOGGER.info("new ips({}) service: {} -> {}", newHosts.size(), newService.getKey(), JacksonUtils.toJson(newHosts)); } - + if (remvHosts.size() > 0) { changed = true; NAMING_LOGGER.info("removed ips({}) service: {} -> {}", remvHosts.size(), newService.getKey(), JacksonUtils.toJson(remvHosts)); } - + if (modHosts.size() > 0) { changed = true; NAMING_LOGGER.info("modified ips({}) service: {} -> {}", modHosts.size(), newService.getKey(), @@ -249,7 +249,17 @@ private boolean isChangedServiceInfo(ServiceInfo oldService, ServiceInfo newServ } return changed; } - + + public boolean isFailoverSwitch() { + return failoverReactor.isFailoverSwitch(); + } + + public ServiceInfo getFailoverServiceInfo(final String serviceName, final String groupName, final String clusters) { + String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName); + String key = ServiceInfo.getKey(groupedServiceName, clusters); + return failoverReactor.getService(key); + } + @Override public void shutdown() throws NacosException { String className = this.getClass().getName(); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java index 5a8f08e2689..1309037b6fc 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java @@ -46,6 +46,8 @@ public class UtilAndComs { public static final String NACOS_NAMING_LOG_NAME = "com.alibaba.nacos.naming.log.filename"; public static final String NACOS_NAMING_LOG_LEVEL = "com.alibaba.nacos.naming.log.level"; + + public static final String NACOS_FAIL_OVER_STORAGE_TYPE = "com.alibaba.nacos.failover.storage.type"; public static final int DEFAULT_POLLING_THREAD_COUNT = ThreadUtils.getSuitableThreadCount(1) > 1 ? ThreadUtils.getSuitableThreadCount(1) / 2 : 1; diff --git a/client/src/main/resources/META-INF/services/com.alibaba.nacos.client.naming.backups.FailoverDataSource b/client/src/main/resources/META-INF/services/com.alibaba.nacos.client.naming.backups.FailoverDataSource new file mode 100644 index 00000000000..68f35c72e75 --- /dev/null +++ b/client/src/main/resources/META-INF/services/com.alibaba.nacos.client.naming.backups.FailoverDataSource @@ -0,0 +1,18 @@ +# +# Copyright 1999-2021 Alibaba Group Holding Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# + +com.alibaba.nacos.client.naming.backups.datasource.DiskFailoverDataSource diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java index 993b85b1f74..b52f455e1e8 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java @@ -26,15 +26,16 @@ import java.lang.reflect.Field; import java.util.Date; import java.util.HashMap; +import java.util.UUID; import java.util.concurrent.ScheduledExecutorService; public class FailoverReactorTest { - + @Test public void testInit() throws NacosException, NoSuchFieldException, IllegalAccessException { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp"); + FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp", UUID.randomUUID().toString()); Field executorService = FailoverReactor.class.getDeclaredField("executorService"); executorService.setAccessible(true); ScheduledExecutorService o = (ScheduledExecutorService) executorService.get(failoverReactor); @@ -42,36 +43,35 @@ public void testInit() throws NacosException, NoSuchFieldException, IllegalAcces failoverReactor.shutdown(); Assert.assertTrue(o.isShutdown()); } - + @Test public void testAddDay() throws NacosException { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp"); + FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp", UUID.randomUUID().toString()); Date date = new Date(); Date actual = failoverReactor.addDay(date, 1); Assert.assertEquals(date.getTime() + 24 * 60 * 60 * 1000, actual.getTime()); failoverReactor.shutdown(); } - + @Test public void testIsFailoverSwitch() throws NacosException { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp"); + FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp", UUID.randomUUID().toString()); Assert.assertFalse(failoverReactor.isFailoverSwitch()); failoverReactor.shutdown(); - + } - + @Test public void testGetService() throws NacosException { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp"); + FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp",UUID.randomUUID().toString()); ServiceInfo info = failoverReactor.getService("aa@@bb"); Assert.assertEquals(new ServiceInfo("aa@@bb").toString(), info.toString()); failoverReactor.shutdown(); - } } \ No newline at end of file diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java new file mode 100644 index 00000000000..9a73fb9aa58 --- /dev/null +++ b/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java @@ -0,0 +1,34 @@ +package com.alibaba.nacos.client.naming.backups.datasource; + +import com.alibaba.nacos.client.naming.backups.FailoverData; +import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; +import junit.framework.TestCase; +import org.mockito.Mockito; + +import java.util.HashMap; +import java.util.Map; + +public class DiskFailoverDataSourceTest extends TestCase { + + public void testGetSwitch() { + ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); + Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); + DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder, "/tmp"); + diskFailoverDataSource.getSwitch(); + } + + public void testGetFailoverData() { + ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); + Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); + DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder, "/tmp"); + diskFailoverDataSource.getFailoverData(); + } + + public void testSaveFailoverData() { + ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); + Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); + DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder, "/tmp"); + Map failoverDataMap = new HashMap(); + diskFailoverDataSource.saveFailoverData(failoverDataMap); + } +} \ No newline at end of file diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java index fce1db6dfc5..9afe6f647cc 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java @@ -23,7 +23,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.client.env.NacosClientProperties; -import com.alibaba.nacos.client.naming.backups.FailoverReactor; +import com.alibaba.nacos.client.naming.backups.FailoverReactor3; import org.junit.Assert; import org.junit.Test; @@ -150,8 +150,8 @@ public void testShutdown() throws NacosException, NoSuchFieldException, IllegalA ServiceInfoHolder holder = new ServiceInfoHolder("aa", "scope-001", nacosClientProperties); Field field = ServiceInfoHolder.class.getDeclaredField("failoverReactor"); field.setAccessible(true); - FailoverReactor reactor = (FailoverReactor) field.get(holder); - Field executorService = FailoverReactor.class.getDeclaredField("executorService"); + FailoverReactor3 reactor = (FailoverReactor3) field.get(holder); + Field executorService = FailoverReactor3.class.getDeclaredField("executorService"); executorService.setAccessible(true); ScheduledExecutorService pool = (ScheduledExecutorService) executorService.get(reactor); Assert.assertFalse(pool.isShutdown()); From 6af7bbcd279fc9ff7d18d2855a74348054b8e55c Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Thu, 28 Sep 2023 14:10:23 +0800 Subject: [PATCH 02/32] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=A3=81=E7=9B=98?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0Nacos=E5=AE=A2=E6=88=B7=E7=AB=AF=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E9=99=8D=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nacos/client/naming/cache/ServiceInfoHolderTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java index 9afe6f647cc..fce1db6dfc5 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java @@ -23,7 +23,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.client.env.NacosClientProperties; -import com.alibaba.nacos.client.naming.backups.FailoverReactor3; +import com.alibaba.nacos.client.naming.backups.FailoverReactor; import org.junit.Assert; import org.junit.Test; @@ -150,8 +150,8 @@ public void testShutdown() throws NacosException, NoSuchFieldException, IllegalA ServiceInfoHolder holder = new ServiceInfoHolder("aa", "scope-001", nacosClientProperties); Field field = ServiceInfoHolder.class.getDeclaredField("failoverReactor"); field.setAccessible(true); - FailoverReactor3 reactor = (FailoverReactor3) field.get(holder); - Field executorService = FailoverReactor3.class.getDeclaredField("executorService"); + FailoverReactor reactor = (FailoverReactor) field.get(holder); + Field executorService = FailoverReactor.class.getDeclaredField("executorService"); executorService.setAccessible(true); ScheduledExecutorService pool = (ScheduledExecutorService) executorService.get(reactor); Assert.assertFalse(pool.isShutdown()); From 938dfb81d10093e893b3189922b23c1033c44d37 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Thu, 28 Sep 2023 17:45:13 +0800 Subject: [PATCH 03/32] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=A3=81=E7=9B=98?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0Nacos=E5=AE=A2=E6=88=B7=E7=AB=AF=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E9=99=8D=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/pom.xml | 4 +++ .../datasource/DiskFailoverDataSource.java | 36 ++++++++++++++++--- .../client/naming/utils/UtilAndComs.java | 4 ++- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/client/pom.xml b/client/pom.xml index 72877a62018..4dd003e3e9c 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -119,6 +119,10 @@ org.yaml snakeyaml + + io.micrometer + micrometer-core + diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java index dfd8c14a4f8..41de0adf355 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java @@ -35,8 +35,7 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Paths; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; @@ -54,6 +53,7 @@ public class DiskFailoverDataSource implements FailoverDataSource { private Map serviceMap = new ConcurrentHashMap<>(); + private Set serviceNames = new HashSet<>(); private final String failoverDir; @@ -94,6 +94,33 @@ public void run() { continue; } + if (file.getName().equals(UtilAndComs.FAILOVER_SERVICENAME)) { + try { + String dataString = ConcurrentDiskUtil.getFileContent(file, + Charset.defaultCharset().toString()); + reader = new BufferedReader(new StringReader(dataString)); + String serviceNameStr; + if ((serviceNameStr = reader.readLine()) != null) { + String[] serviceNameList = serviceNameStr.split(","); + if (serviceNameList.length > 0) { + serviceNames = new HashSet<>(Arrays.asList(serviceNameList)); + } + } + continue; + } catch (Exception e) { + NAMING_LOGGER.error("[NA] failed to read cache for dom: {}", file.getName(), e); + continue; + } finally { + try { + if (reader != null) { + reader.close(); + } + } catch (Exception e) { + //ignore + } + } + } + ServiceInfo dom = null; try { @@ -159,7 +186,7 @@ public FailoverSwitch getSwitch() { if (IS_FAILOVER_MODE.equals(line1)) { NAMING_LOGGER.info("failover-mode is on"); new FailoverFileReader().run(); - return new FailoverSwitch(Boolean.TRUE); + return new FailoverSwitch(Boolean.TRUE, serviceNames); } else if (NO_FAILOVER_MODE.equals(line1)) { NAMING_LOGGER.info("failover-mode is off"); } @@ -182,10 +209,9 @@ public Map getFailoverData() { @Override public void saveFailoverData(Map failoverDataMap) { - // 2.从ServiceInfoHoler复制服务数据写入到容灾数据源FailoverDataSource; for (Map.Entry entry : failoverDataMap.entrySet()) { ServiceInfo serviceInfo = (ServiceInfo) entry.getValue().getData(); - if (StringUtils.equals(serviceInfo.getKey(), UtilAndComs.ALL_IPS) || StringUtils + if (!serviceNames.contains(serviceInfo.getKey()) || StringUtils.equals(serviceInfo.getKey(), UtilAndComs.ALL_IPS) || StringUtils .equals(serviceInfo.getName(), UtilAndComs.ENV_LIST_KEY) || StringUtils .equals(serviceInfo.getName(), UtilAndComs.ENV_CONFIGS) || StringUtils .equals(serviceInfo.getName(), UtilAndComs.VIP_CLIENT_FILE) || StringUtils diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java index 1309037b6fc..31c28e32464 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java @@ -38,7 +38,9 @@ public class UtilAndComs { public static final String ALL_IPS = "000--00-ALL_IPS--00--000"; public static final String FAILOVER_SWITCH = "00-00---000-VIPSRV_FAILOVER_SWITCH-000---00-00"; - + + public static final String FAILOVER_SERVICENAME = "00-00---000-VIPSRV_FAILOVER_SERVICENAME-000---00-00"; + public static final String DEFAULT_NAMESPACE_ID = "public"; public static final int REQUEST_DOMAIN_RETRY_COUNT = 3; From 36a4546a7651f36a19cfc1751f467a0b843fc735 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Fri, 29 Sep 2023 13:56:28 +0800 Subject: [PATCH 04/32] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=A3=81=E7=9B=98?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0Nacos=E5=AE=A2=E6=88=B7=E7=AB=AF=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E9=99=8D=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/naming/backups/FailoverData.java | 16 +++++++++++++++- .../client/naming/backups/FailoverReactor.java | 4 ++-- .../client/naming/backups/FailoverSwitch.java | 10 ++++++++-- .../naming/backups/NamingFailoverData.java | 5 +++++ .../datasource/DiskFailoverDataSource.java | 4 +++- 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java index 7b2b97f28a0..fd3f3714b69 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java @@ -15,12 +15,20 @@ */ package com.alibaba.nacos.client.naming.backups; +/** + * Failover Data. + * + * @author zongkang.guo + */ public class FailoverData { + /** + * failover type,naming or config. + */ private DataType dataType; /** - * 容灾数据,子类设置具体类型 + * failover data. */ private Object data; @@ -30,7 +38,13 @@ public FailoverData(DataType dataType, Object data) { } public enum DataType { + /** + * naming. + */ naming, + /** + * config. + */ config } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index e9552c2d204..86cfcd034ec 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -119,7 +119,7 @@ public void run() { FailoverSwitch fSwitch = failoverDataSource.getSwitch(); if (fSwitch != null && fSwitch.getEnabled() && !failoverSwitchEnable) { failoverSwitchEnable = Boolean.TRUE; - Map map = new ConcurrentHashMap<>(); + Map map = new ConcurrentHashMap<>(200); Map failoverData = failoverDataSource.getFailoverData(); for (Map.Entry entry : failoverData.entrySet()) { map.put(entry.getKey(), (ServiceInfo) entry.getValue().getData()); @@ -134,7 +134,7 @@ public void run() { if (fSwitch != null && failoverSwitchEnable && !fSwitch.getEnabled()) { failoverSwitchEnable = Boolean.FALSE; - Map map = new ConcurrentHashMap<>(); + Map map = new ConcurrentHashMap<>(200); Map failoverData = failoverDataSource.getFailoverData(); for (Map.Entry entry : failoverData.entrySet()) { map.put(entry.getKey(), (ServiceInfo) entry.getValue().getData()); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java index 55f4b539f46..aaa1aba1273 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java @@ -17,15 +17,21 @@ import java.util.Set; + +/** + * Failover switch. + * + * @author zongkang.guo + */ public class FailoverSwitch { /** - * 容灾是否打开; + * Failover switch enable. */ private boolean enabled; /** - * 参与容灾的服务名列表 + * Failover service name. */ private Set serviceNames; diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java index 2d1b9856c51..e4596f21078 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java @@ -17,6 +17,11 @@ import com.alibaba.nacos.api.naming.pojo.ServiceInfo; +/** + * Naming Failover Data. + * + * @author zongkang.guo + */ public class NamingFailoverData extends FailoverData { private NamingFailoverData(ServiceInfo serviceInfo) { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java index 41de0adf355..f6f913aa8ef 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java @@ -41,7 +41,9 @@ import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; /** + * Failover Data Disk Impl. * + * @author zongkang.guo */ public class DiskFailoverDataSource implements FailoverDataSource { @@ -225,4 +227,4 @@ public void saveFailoverData(Map failoverDataMap) { return; } -} +} \ No newline at end of file From 18cfd5f7762651527d622d5301be21ba7d7809be Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Sat, 7 Oct 2023 10:40:06 +0800 Subject: [PATCH 05/32] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E6=97=B6=E5=8E=BB=E9=99=A4cachedir=EF=BC=8C=E5=8E=BB=E9=99=A4?= =?UTF-8?q?=E7=A3=81=E7=9B=98=E5=AE=9A=E6=97=B6=E4=BF=9D=E5=AD=98=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/naming/NacosNamingService.java | 33 ++++++++--- .../naming/backups/FailoverReactor.java | 58 ++++--------------- .../datasource/DiskFailoverDataSource.java | 7 +-- .../naming/cache/ServiceInfoHolder.java | 6 +- 4 files changed, 43 insertions(+), 61 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 48d70bdb936..dce913e775a 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 @@ -234,7 +234,6 @@ public List getAllInstances(String serviceName, String groupName, List List list; String clusterString = StringUtils.join(clusters, ","); if (serviceInfoHolder.isFailoverSwitch()) { - list = new ArrayList<>(); serviceInfo = serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString); if (serviceInfo != null && serviceInfo.getHosts().size() > 0) { serviceInfo.getHosts(); @@ -311,14 +310,32 @@ public List selectInstances(String serviceName, String groupName, List boolean subscribe) throws NacosException { ServiceInfo serviceInfo; String clusterString = StringUtils.join(clusters, ","); - if (subscribe) { - serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); - if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { - serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); + if (serviceInfoHolder.isFailoverSwitch()) { + serviceInfo = serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString); + if (serviceInfo != null && serviceInfo.getHosts().size() > 0) { + serviceInfo.getHosts(); + if (!clientProxy.isSubscribed(serviceName, groupName, clusterString)){ + serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); + } + } else{ + if (subscribe) { + serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); + if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { + serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); + } + } + } + }else { + if (subscribe) { + serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); + if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { + serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); + } + } else { + serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, false); } - } else { - serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, false); } + return selectInstances(serviceInfo, healthy); } @@ -481,6 +498,6 @@ public void shutDown() throws NacosException { serviceInfoHolder.shutdown(); clientProxy.shutdown(); NotifyCenter.deregisterSubscriber(changeNotifier); - + } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 86cfcd034ec..572f9003755 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.client.naming.backups; +import com.alibaba.nacos.api.config.filter.IConfigFilter; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.client.naming.backups.datasource.DiskFailoverDataSource; @@ -25,15 +26,12 @@ import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.utils.ThreadUtils; import io.micrometer.core.instrument.MultiGauge; -import io.micrometer.core.instrument.Tags; -import java.io.File; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; @@ -69,7 +67,13 @@ public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String cacheDir, Str this.serviceInfoHolder = serviceInfoHolder; this.failoverDir = cacheDir + FAILOVER_DIR; this.notifierEventScope = notifierEventScope; - failoverDataSource = new DiskFailoverDataSource(serviceInfoHolder, cacheDir); + // spi + ServiceLoader configFilters = ServiceLoader.load(FailoverDataSource.class); + for (FailoverDataSource dataSource : configFilters) { + dataSource.getSwitch(); + } + + failoverDataSource = new DiskFailoverDataSource(serviceInfoHolder); failoverInstanceCounts = MultiGauge.builder("nacos_naming_client_failover_instances").register(io.micrometer.core.instrument.Metrics.globalRegistry); // init executorService this.executorService = new ScheduledThreadPoolExecutor(1, r -> { @@ -88,26 +92,6 @@ public void init() { executorService.scheduleWithFixedDelay(new FailoverSwitchRefresher(), 0L, 5000L, TimeUnit.MILLISECONDS); - executorService.scheduleWithFixedDelay(new FailoverDataWriter(), 30, DAY_PERIOD_MINUTES, TimeUnit.MINUTES); - - // backup file on startup if failover directory is empty. - executorService.schedule(() -> { - try { - File cacheDir = new File(failoverDir); - - if (!cacheDir.exists() && !cacheDir.mkdirs()) { - throw new IllegalStateException("failed to create cache dir: " + failoverDir); - } - - File[] files = cacheDir.listFiles(); - if (files == null || files.length <= 0) { - new FailoverDataWriter().run(); - } - } catch (Throwable e) { - NAMING_LOGGER.error("[NA] failed to backup file on startup.", e); - } - - }, 10000L, TimeUnit.MILLISECONDS); } @@ -118,7 +102,7 @@ public void run() { FailoverSwitch fSwitch = failoverDataSource.getSwitch(); if (fSwitch != null && fSwitch.getEnabled() && !failoverSwitchEnable) { - failoverSwitchEnable = Boolean.TRUE; + failoverSwitchEnable = true; Map map = new ConcurrentHashMap<>(200); Map failoverData = failoverDataSource.getFailoverData(); for (Map.Entry entry : failoverData.entrySet()) { @@ -133,7 +117,7 @@ public void run() { } if (fSwitch != null && failoverSwitchEnable && !fSwitch.getEnabled()) { - failoverSwitchEnable = Boolean.FALSE; + failoverSwitchEnable = false; Map map = new ConcurrentHashMap<>(200); Map failoverData = failoverDataSource.getFailoverData(); for (Map.Entry entry : failoverData.entrySet()) { @@ -156,28 +140,6 @@ public void run() { } } - class FailoverDataWriter extends TimerTask { - - @Override - public void run() { - if (isFailoverSwitch()) { - return; - } - - Map serviceInfoMap = serviceInfoHolder.getServiceInfoMap(); - Map failoverDataMap = new HashMap<>(serviceInfoMap.size()); - for (Map.Entry entry : serviceInfoMap.entrySet()) { - failoverDataMap.put(entry.getKey(), new FailoverData(FailoverData.DataType.naming, entry.getValue())); - } - - failoverDataSource.saveFailoverData(failoverDataMap); - - Set serviceNames = failoverDataSource.getSwitch().getServiceNames(); - - failoverInstanceCounts.register(serviceNames.stream().map(serviceName -> MultiGauge.Row.of(Tags.of("service_name", serviceName), (serviceInfoMap.get(serviceName)).ipCount())).collect(Collectors.toList()), true); - } - } - public boolean isFailoverSwitch() { return failoverSwitchEnable; } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java index f6f913aa8ef..80ceda2fe9b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java @@ -58,13 +58,12 @@ public class DiskFailoverDataSource implements FailoverDataSource { private Set serviceNames = new HashSet<>(); - private final String failoverDir; + private String failoverDir; private long lastModifiedMillis = 0L; - - public DiskFailoverDataSource(ServiceInfoHolder serviceInfoHolder, String cacheDir) { - this.failoverDir = cacheDir + FAILOVER_DIR; + public DiskFailoverDataSource(ServiceInfoHolder serviceInfoHolder) { + this.failoverDir = serviceInfoHolder.getCacheDir() + FAILOVER_DIR; } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java index c766ace3e00..b73e74520c0 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java @@ -71,7 +71,7 @@ public ServiceInfoHolder(String namespace, String notifierEventScope, NacosClien this.serviceInfoMap = new ConcurrentHashMap<>(16); } - this.failoverReactor = new FailoverReactor(this, cacheDir,notifierEventScope); + this.failoverReactor = new FailoverReactor(this, cacheDir, notifierEventScope); this.pushEmptyProtection = isPushEmptyProtect(properties); this.notifierEventScope = notifierEventScope; @@ -250,6 +250,10 @@ public boolean isChangedServiceInfo(ServiceInfo oldService, ServiceInfo newServi return changed; } + public String getCacheDir() { + return cacheDir; + } + public boolean isFailoverSwitch() { return failoverReactor.isFailoverSwitch(); } From 93af51a76d9ae1bc8eacf1c1a0f23473b584c9d4 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Sat, 7 Oct 2023 11:17:05 +0800 Subject: [PATCH 06/32] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E6=97=B6=E5=8E=BB=E9=99=A4cachedir=EF=BC=8C=E5=8E=BB=E9=99=A4?= =?UTF-8?q?=E7=A3=81=E7=9B=98=E5=AE=9A=E6=97=B6=E4=BF=9D=E5=AD=98=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/naming/NacosNamingService.java | 18 +++++++++++------- .../client/naming/backups/FailoverData.java | 1 + .../naming/backups/FailoverDataSource.java | 3 ++- .../client/naming/backups/FailoverReactor.java | 13 +++++++------ .../client/naming/backups/FailoverSwitch.java | 2 +- .../naming/backups/NamingFailoverData.java | 1 + .../datasource/DiskFailoverDataSource.java | 11 +++++++---- .../client/naming/cache/ServiceInfoHolder.java | 13 ++++++++++++- .../naming/backups/FailoverReactorTest.java | 2 +- .../datasource/DiskFailoverDataSourceTest.java | 10 +++++++--- 10 files changed, 50 insertions(+), 24 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 dce913e775a..fe1cfd3435a 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 @@ -40,7 +40,11 @@ import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.utils.StringUtils; -import java.util.*; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Properties; +import java.util.UUID; /** * Nacos Naming Service. @@ -237,10 +241,10 @@ public List getAllInstances(String serviceName, String groupName, List serviceInfo = serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString); if (serviceInfo != null && serviceInfo.getHosts().size() > 0) { serviceInfo.getHosts(); - if (!clientProxy.isSubscribed(serviceName, groupName, clusterString)){ + if (!clientProxy.isSubscribed(serviceName, groupName, clusterString)) { serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); } - } else{ + } else { if (subscribe) { serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { @@ -248,7 +252,7 @@ public List getAllInstances(String serviceName, String groupName, List } } } - }else { + } else { if (subscribe) { serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { @@ -314,10 +318,10 @@ public List selectInstances(String serviceName, String groupName, List serviceInfo = serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString); if (serviceInfo != null && serviceInfo.getHosts().size() > 0) { serviceInfo.getHosts(); - if (!clientProxy.isSubscribed(serviceName, groupName, clusterString)){ + if (!clientProxy.isSubscribed(serviceName, groupName, clusterString)) { serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); } - } else{ + } else { if (subscribe) { serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { @@ -325,7 +329,7 @@ public List selectInstances(String serviceName, String groupName, List } } } - }else { + } else { if (subscribe) { serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java index fd3f3714b69..dd437916526 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.alibaba.nacos.client.naming.backups; /** diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java index 457ace1cd38..6042fb45038 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.alibaba.nacos.client.naming.backups; import java.util.Map; @@ -42,7 +43,7 @@ public interface FailoverDataSource { /** * Save disaster recovery data. * - * @param failoverDataMap + * @param failoverDataMap Failover data map */ void saveFailoverData(Map failoverDataMap); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 572f9003755..064727147ce 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -16,7 +16,6 @@ package com.alibaba.nacos.client.naming.backups; -import com.alibaba.nacos.api.config.filter.IConfigFilter; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.client.naming.backups.datasource.DiskFailoverDataSource; @@ -27,7 +26,10 @@ import com.alibaba.nacos.common.utils.ThreadUtils; import io.micrometer.core.instrument.MultiGauge; -import java.util.*; +import java.util.Calendar; +import java.util.Date; +import java.util.Map; +import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; @@ -44,7 +46,6 @@ public class FailoverReactor implements Closeable { private static final String FAILOVER_DIR = "/failover"; - private Map serviceMap = new ConcurrentHashMap<>(); private boolean failoverSwitchEnable; @@ -74,7 +75,8 @@ public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String cacheDir, Str } failoverDataSource = new DiskFailoverDataSource(serviceInfoHolder); - failoverInstanceCounts = MultiGauge.builder("nacos_naming_client_failover_instances").register(io.micrometer.core.instrument.Metrics.globalRegistry); + failoverInstanceCounts = MultiGauge.builder("nacos_naming_client_failover_instances") + .register(io.micrometer.core.instrument.Metrics.globalRegistry); // init executorService this.executorService = new ScheduledThreadPoolExecutor(1, r -> { Thread thread = new Thread(r); @@ -94,7 +96,6 @@ public void init() { } - class FailoverSwitchRefresher implements Runnable { @Override @@ -171,7 +172,7 @@ public Date addDay(Date date, int num) { /** * shutdown ThreadPool. - * @throws NacosException + * @throws NacosException Nacos exception */ @Override public void shutdown() throws NacosException { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java index aaa1aba1273..47318f5a748 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.alibaba.nacos.client.naming.backups; import java.util.Set; - /** * Failover switch. * diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java index e4596f21078..84488ae7766 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.alibaba.nacos.client.naming.backups; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java index 80ceda2fe9b..31cb1339431 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.alibaba.nacos.client.naming.backups.datasource; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; @@ -35,7 +36,12 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Paths; -import java.util.*; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; @@ -57,7 +63,6 @@ public class DiskFailoverDataSource implements FailoverDataSource { private Set serviceNames = new HashSet<>(); - private String failoverDir; private long lastModifiedMillis = 0L; @@ -66,7 +71,6 @@ public DiskFailoverDataSource(ServiceInfoHolder serviceInfoHolder) { this.failoverDir = serviceInfoHolder.getCacheDir() + FAILOVER_DIR; } - class FailoverFileReader implements Runnable { @Override @@ -202,7 +206,6 @@ public FailoverSwitch getSwitch() { return new FailoverSwitch(Boolean.FALSE); } - @Override public Map getFailoverData() { return serviceMap; diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java index b73e74520c0..5bce99a0ed9 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java @@ -32,7 +32,12 @@ import com.alibaba.nacos.common.utils.StringUtils; import java.io.File; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -179,6 +184,12 @@ private boolean isEmptyOrErrorPush(ServiceInfo serviceInfo) { return null == serviceInfo.getHosts() || (pushEmptyProtection && !serviceInfo.validate()); } + /** + * isChangedServiceInfo. + * @param oldService old service data + * @param newService new service data + * @return + */ public boolean isChangedServiceInfo(ServiceInfo oldService, ServiceInfo newService) { if (null == oldService) { NAMING_LOGGER.info("init new ips({}) service: {} -> {}", newService.ipCount(), newService.getKey(), diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java index b52f455e1e8..843d4cefbe3 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java @@ -69,7 +69,7 @@ public void testIsFailoverSwitch() throws NacosException { public void testGetService() throws NacosException { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp",UUID.randomUUID().toString()); + FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp", UUID.randomUUID().toString()); ServiceInfo info = failoverReactor.getService("aa@@bb"); Assert.assertEquals(new ServiceInfo("aa@@bb").toString(), info.toString()); failoverReactor.shutdown(); diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java index 9a73fb9aa58..346f01c7d24 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java @@ -3,6 +3,7 @@ import com.alibaba.nacos.client.naming.backups.FailoverData; import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; import junit.framework.TestCase; +import org.junit.Test; import org.mockito.Mockito; import java.util.HashMap; @@ -10,24 +11,27 @@ public class DiskFailoverDataSourceTest extends TestCase { + @Test public void testGetSwitch() { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder, "/tmp"); + DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder); diskFailoverDataSource.getSwitch(); } + @Test public void testGetFailoverData() { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder, "/tmp"); + DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder); diskFailoverDataSource.getFailoverData(); } + @Test public void testSaveFailoverData() { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder, "/tmp"); + DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder); Map failoverDataMap = new HashMap(); diskFailoverDataSource.saveFailoverData(failoverDataMap); } From 066b256bd21e841af880ba4d73bdae28eaf3d1c7 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Sat, 7 Oct 2023 15:36:36 +0800 Subject: [PATCH 07/32] =?UTF-8?q?=E5=88=A0=E9=99=A4saveFailoverData?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../naming/backups/FailoverDataSource.java | 7 ------- .../datasource/DiskFailoverDataSource.java | 18 ------------------ .../datasource/DiskFailoverDataSourceTest.java | 17 ++++++++++++++++- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java index 6042fb45038..305507dcb9e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java @@ -40,11 +40,4 @@ public interface FailoverDataSource { */ Map getFailoverData(); - /** - * Save disaster recovery data. - * - * @param failoverDataMap Failover data map - */ - void saveFailoverData(Map failoverDataMap); - } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java index 31cb1339431..e741545b35f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java @@ -211,22 +211,4 @@ public Map getFailoverData() { return serviceMap; } - @Override - public void saveFailoverData(Map failoverDataMap) { - for (Map.Entry entry : failoverDataMap.entrySet()) { - ServiceInfo serviceInfo = (ServiceInfo) entry.getValue().getData(); - if (!serviceNames.contains(serviceInfo.getKey()) || StringUtils.equals(serviceInfo.getKey(), UtilAndComs.ALL_IPS) || StringUtils - .equals(serviceInfo.getName(), UtilAndComs.ENV_LIST_KEY) || StringUtils - .equals(serviceInfo.getName(), UtilAndComs.ENV_CONFIGS) || StringUtils - .equals(serviceInfo.getName(), UtilAndComs.VIP_CLIENT_FILE) || StringUtils - .equals(serviceInfo.getName(), UtilAndComs.ALL_HOSTS)) { - continue; - } - - DiskCache.write(serviceInfo, failoverDir); - } - - return; - } - } \ No newline at end of file diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java index 346f01c7d24..a7cebf9b480 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.client.naming.backups.datasource; import com.alibaba.nacos.client.naming.backups.FailoverData; @@ -33,6 +49,5 @@ public void testSaveFailoverData() { Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder); Map failoverDataMap = new HashMap(); - diskFailoverDataSource.saveFailoverData(failoverDataMap); } } \ No newline at end of file From 714d2f40010aba95a8380ece0ad0fdea0d52b75f Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Sat, 7 Oct 2023 18:59:31 +0800 Subject: [PATCH 08/32] =?UTF-8?q?=E7=A7=BB=E9=99=A4getServiceInfo=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E4=B8=AD=E7=9A=84=E5=AE=B9=E7=81=BE=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nacos/client/naming/cache/ServiceInfoHolder.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java index 5bce99a0ed9..d6323b60dd4 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java @@ -127,9 +127,9 @@ public ServiceInfo getServiceInfo(final String serviceName, final String groupNa NAMING_LOGGER.debug("failover-mode: {}", failoverReactor.isFailoverSwitch()); String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName); String key = ServiceInfo.getKey(groupedServiceName, clusters); - if (failoverReactor.isFailoverSwitch()) { - return failoverReactor.getService(key); - } +// if (failoverReactor.isFailoverSwitch()) { +// return failoverReactor.getService(key); +// } return serviceInfoMap.get(key); } From 6b3abe92124adffbd59e00aaa03df2c7ec172647 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Sat, 7 Oct 2023 19:02:32 +0800 Subject: [PATCH 09/32] =?UTF-8?q?=E7=A7=BB=E9=99=A4getServiceInfo=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E4=B8=AD=E7=9A=84=E5=AE=B9=E7=81=BE=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/alibaba/nacos/client/naming/NacosNamingService.java | 2 ++ 1 file changed, 2 insertions(+) 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 fe1cfd3435a..63903b27fdd 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 @@ -250,6 +250,8 @@ public List getAllInstances(String serviceName, String groupName, List if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); } + } else { + serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, false); } } } else { From 77e817c2bf0550132810dc4f53c639364bf5c767 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Sat, 7 Oct 2023 23:42:26 +0800 Subject: [PATCH 10/32] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=8E=89=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/nacos/client/naming/cache/ServiceInfoHolder.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java index d6323b60dd4..ea76d24f011 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java @@ -127,9 +127,6 @@ public ServiceInfo getServiceInfo(final String serviceName, final String groupNa NAMING_LOGGER.debug("failover-mode: {}", failoverReactor.isFailoverSwitch()); String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName); String key = ServiceInfo.getKey(groupedServiceName, clusters); -// if (failoverReactor.isFailoverSwitch()) { -// return failoverReactor.getService(key); -// } return serviceInfoMap.get(key); } From 76c5eb301416b1acf11adeaa11f31ad9d8d2d31f Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Sun, 8 Oct 2023 10:13:58 +0800 Subject: [PATCH 11/32] =?UTF-8?q?=E4=BD=BF=E7=94=A8serviceMap=E5=AF=B9?= =?UTF-8?q?=E6=AF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/naming/backups/FailoverReactor.java | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 064727147ce..6403dfd8489 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -44,16 +44,12 @@ */ public class FailoverReactor implements Closeable { - private static final String FAILOVER_DIR = "/failover"; - private Map serviceMap = new ConcurrentHashMap<>(); private boolean failoverSwitchEnable; private static final long DAY_PERIOD_MINUTES = 24 * 60; - private final String failoverDir; - private final ServiceInfoHolder serviceInfoHolder; private final ScheduledExecutorService executorService; @@ -66,7 +62,6 @@ public class FailoverReactor implements Closeable { public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String cacheDir, String notifierEventScope) { this.serviceInfoHolder = serviceInfoHolder; - this.failoverDir = cacheDir + FAILOVER_DIR; this.notifierEventScope = notifierEventScope; // spi ServiceLoader configFilters = ServiceLoader.load(FailoverDataSource.class); @@ -119,14 +114,8 @@ public void run() { if (fSwitch != null && failoverSwitchEnable && !fSwitch.getEnabled()) { failoverSwitchEnable = false; - Map map = new ConcurrentHashMap<>(200); - Map failoverData = failoverDataSource.getFailoverData(); - for (Map.Entry entry : failoverData.entrySet()) { - map.put(entry.getKey(), (ServiceInfo) entry.getValue().getData()); - } - Map serviceInfoMap = serviceInfoHolder.getServiceInfoMap(); - for (Map.Entry entry : map.entrySet()) { + for (Map.Entry entry : serviceMap.entrySet()) { ServiceInfo oldService = entry.getValue(); ServiceInfo newService = serviceInfoMap.get(entry.getKey()); boolean changed = serviceInfoHolder.isChangedServiceInfo(oldService, newService); From d38d4940839755b7c5ac64c2a92507a58ec7bc20 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Sun, 8 Oct 2023 10:17:34 +0800 Subject: [PATCH 12/32] =?UTF-8?q?=E4=BD=BF=E7=94=A8serviceMap=E5=AF=B9?= =?UTF-8?q?=E6=AF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/nacos/client/naming/backups/FailoverReactor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 6403dfd8489..3373007dbe8 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -97,7 +97,7 @@ class FailoverSwitchRefresher implements Runnable { public void run() { FailoverSwitch fSwitch = failoverDataSource.getSwitch(); - if (fSwitch != null && fSwitch.getEnabled() && !failoverSwitchEnable) { + if (fSwitch != null && fSwitch.getEnabled()) { failoverSwitchEnable = true; Map map = new ConcurrentHashMap<>(200); Map failoverData = failoverDataSource.getFailoverData(); From f692b4d554c38b1a83040bdbcb38d9d1bc27d0e1 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Sun, 8 Oct 2023 17:58:44 +0800 Subject: [PATCH 13/32] =?UTF-8?q?failoverSwith=E5=8E=BB=E9=99=A4serviceNam?= =?UTF-8?q?eSet=E9=9B=86=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/naming/NacosNamingService.java | 97 ++++++------------- .../naming/backups/FailoverReactor.java | 41 ++++---- .../client/naming/backups/FailoverSwitch.java | 17 ---- .../datasource/DiskFailoverDataSource.java | 34 +------ .../client/naming/utils/UtilAndComs.java | 2 - 5 files changed, 49 insertions(+), 142 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 63903b27fdd..df5a42d6767 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 @@ -234,37 +234,8 @@ public List getAllInstances(String serviceName, List clusters, @Override public List getAllInstances(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException { - ServiceInfo serviceInfo; List list; - String clusterString = StringUtils.join(clusters, ","); - if (serviceInfoHolder.isFailoverSwitch()) { - serviceInfo = serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString); - if (serviceInfo != null && serviceInfo.getHosts().size() > 0) { - serviceInfo.getHosts(); - if (!clientProxy.isSubscribed(serviceName, groupName, clusterString)) { - serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); - } - } else { - if (subscribe) { - serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); - if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { - serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); - } - } else { - serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, false); - } - } - } else { - if (subscribe) { - serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); - if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { - serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); - } - } else { - serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, false); - } - } - + ServiceInfo serviceInfo = getServiceInfo(serviceName,groupName, clusters, subscribe); if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { return new ArrayList<>(); } @@ -314,34 +285,7 @@ public List selectInstances(String serviceName, List clusters, @Override public List selectInstances(String serviceName, String groupName, List clusters, boolean healthy, boolean subscribe) throws NacosException { - ServiceInfo serviceInfo; - String clusterString = StringUtils.join(clusters, ","); - if (serviceInfoHolder.isFailoverSwitch()) { - serviceInfo = serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString); - if (serviceInfo != null && serviceInfo.getHosts().size() > 0) { - serviceInfo.getHosts(); - if (!clientProxy.isSubscribed(serviceName, groupName, clusterString)) { - serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); - } - } else { - if (subscribe) { - serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); - if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { - serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); - } - } - } - } else { - if (subscribe) { - serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); - if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { - serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); - } - } else { - serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, false); - } - } - + ServiceInfo serviceInfo = getServiceInfo(serviceName,groupName, clusters, subscribe); return selectInstances(serviceInfo, healthy); } @@ -362,6 +306,30 @@ private List selectInstances(ServiceInfo serviceInfo, boolean healthy) return list; } + private ServiceInfo getServiceInfo(String serviceName, String groupName, List clusters, + boolean subscribe) throws NacosException { + ServiceInfo serviceInfo; + String clusterString = StringUtils.join(clusters, ","); + if (serviceInfoHolder.isFailoverSwitch()) { + serviceInfo = serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString); + if (serviceInfo != null && serviceInfo.getHosts().size() > 0) { + if (!clientProxy.isSubscribed(serviceName, groupName, clusterString)) { + serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); + } + return serviceInfo; + } + } + if (subscribe) { + serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); + if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { + serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); + } + } else { + serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, false); + } + return serviceInfo; + } + @Override public Instance selectOneHealthyInstance(String serviceName) throws NacosException { return selectOneHealthyInstance(serviceName, new ArrayList<>()); @@ -403,17 +371,8 @@ public Instance selectOneHealthyInstance(String serviceName, List cluste @Override public Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException { - String clusterString = StringUtils.join(clusters, ","); - if (subscribe) { - ServiceInfo serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); - if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { - serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); - } - return Balancer.RandomByWeight.selectHost(serviceInfo); - } else { - ServiceInfo serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, false); - return Balancer.RandomByWeight.selectHost(serviceInfo); - } + ServiceInfo serviceInfo = getServiceInfo(serviceName,groupName, clusters, subscribe); + return Balancer.RandomByWeight.selectHost(serviceInfo); } @Override diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 3373007dbe8..e401a1aa00e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -18,22 +18,25 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; -import com.alibaba.nacos.client.naming.backups.datasource.DiskFailoverDataSource; import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; import com.alibaba.nacos.client.naming.event.InstancesChangeEvent; import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.notify.NotifyCenter; +import com.alibaba.nacos.common.spi.NacosServiceLoader; import com.alibaba.nacos.common.utils.ThreadUtils; +import io.micrometer.core.instrument.Metrics; import io.micrometer.core.instrument.MultiGauge; +import io.micrometer.core.instrument.Tags; import java.util.Calendar; +import java.util.Collection; import java.util.Date; import java.util.Map; -import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; @@ -48,30 +51,24 @@ public class FailoverReactor implements Closeable { private boolean failoverSwitchEnable; - private static final long DAY_PERIOD_MINUTES = 24 * 60; - private final ServiceInfoHolder serviceInfoHolder; private final ScheduledExecutorService executorService; - private final FailoverDataSource failoverDataSource; - - private final MultiGauge failoverInstanceCounts; + private FailoverDataSource failoverDataSource; private String notifierEventScope; + private MultiGauge failoverInstanceCounts = MultiGauge.builder("nacos_naming_client_failover_instances").description("Nacos failover data service count").register(Metrics.globalRegistry); + public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String cacheDir, String notifierEventScope) { this.serviceInfoHolder = serviceInfoHolder; this.notifierEventScope = notifierEventScope; - // spi - ServiceLoader configFilters = ServiceLoader.load(FailoverDataSource.class); - for (FailoverDataSource dataSource : configFilters) { - dataSource.getSwitch(); + Collection dataSources = NacosServiceLoader.load(FailoverDataSource.class); + for (FailoverDataSource dataSource : dataSources) { + failoverDataSource = dataSource; + break; } - - failoverDataSource = new DiskFailoverDataSource(serviceInfoHolder); - failoverInstanceCounts = MultiGauge.builder("nacos_naming_client_failover_instances") - .register(io.micrometer.core.instrument.Metrics.globalRegistry); // init executorService this.executorService = new ScheduledThreadPoolExecutor(1, r -> { Thread thread = new Thread(r); @@ -99,14 +96,15 @@ public void run() { FailoverSwitch fSwitch = failoverDataSource.getSwitch(); if (fSwitch != null && fSwitch.getEnabled()) { failoverSwitchEnable = true; - Map map = new ConcurrentHashMap<>(200); + Map failoverMap = new ConcurrentHashMap<>(200); Map failoverData = failoverDataSource.getFailoverData(); for (Map.Entry entry : failoverData.entrySet()) { - map.put(entry.getKey(), (ServiceInfo) entry.getValue().getData()); + failoverMap.put(entry.getKey(), (ServiceInfo) entry.getValue().getData()); } - if (map.size() > 0) { - serviceMap = map; + if (failoverMap.size() > 0) { + failoverInstanceCounts.register(failoverMap.keySet().stream().map(serviceName -> MultiGauge.Row.of(Tags.of("service_name", serviceName), ((ServiceInfo)failoverMap.get(serviceName)).ipCount())).collect(Collectors.toList()), true); + serviceMap = failoverMap; } return; @@ -124,9 +122,9 @@ public void run() { newService.getClusters(), newService.getHosts())); } } + serviceMap.clear(); + return; } - - serviceMap.clear(); } } @@ -161,6 +159,7 @@ public Date addDay(Date date, int num) { /** * shutdown ThreadPool. + * * @throws NacosException Nacos exception */ @Override diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java index 47318f5a748..8285fbeca4d 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java @@ -30,10 +30,6 @@ public class FailoverSwitch { */ private boolean enabled; - /** - * Failover service name. - */ - private Set serviceNames; public boolean getEnabled() { return enabled; @@ -43,20 +39,7 @@ public void setEnabled(boolean enabled) { this.enabled = enabled; } - public Set getServiceNames() { - return serviceNames; - } - - public void setServiceNames(Set serviceNames) { - this.serviceNames = serviceNames; - } - public FailoverSwitch(boolean enabled) { this.enabled = enabled; } - - public FailoverSwitch(boolean enabled, Set serviceNames) { - this.enabled = enabled; - this.serviceNames = serviceNames; - } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java index e741545b35f..4fb4ffeae23 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java @@ -37,11 +37,8 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Paths; -import java.util.Arrays; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; @@ -61,8 +58,6 @@ public class DiskFailoverDataSource implements FailoverDataSource { private Map serviceMap = new ConcurrentHashMap<>(); - private Set serviceNames = new HashSet<>(); - private String failoverDir; private long lastModifiedMillis = 0L; @@ -99,33 +94,6 @@ public void run() { continue; } - if (file.getName().equals(UtilAndComs.FAILOVER_SERVICENAME)) { - try { - String dataString = ConcurrentDiskUtil.getFileContent(file, - Charset.defaultCharset().toString()); - reader = new BufferedReader(new StringReader(dataString)); - String serviceNameStr; - if ((serviceNameStr = reader.readLine()) != null) { - String[] serviceNameList = serviceNameStr.split(","); - if (serviceNameList.length > 0) { - serviceNames = new HashSet<>(Arrays.asList(serviceNameList)); - } - } - continue; - } catch (Exception e) { - NAMING_LOGGER.error("[NA] failed to read cache for dom: {}", file.getName(), e); - continue; - } finally { - try { - if (reader != null) { - reader.close(); - } - } catch (Exception e) { - //ignore - } - } - } - ServiceInfo dom = null; try { @@ -191,7 +159,7 @@ public FailoverSwitch getSwitch() { if (IS_FAILOVER_MODE.equals(line1)) { NAMING_LOGGER.info("failover-mode is on"); new FailoverFileReader().run(); - return new FailoverSwitch(Boolean.TRUE, serviceNames); + return new FailoverSwitch(Boolean.TRUE); } else if (NO_FAILOVER_MODE.equals(line1)) { NAMING_LOGGER.info("failover-mode is off"); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java index 31c28e32464..ff39653417c 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java @@ -39,8 +39,6 @@ public class UtilAndComs { public static final String FAILOVER_SWITCH = "00-00---000-VIPSRV_FAILOVER_SWITCH-000---00-00"; - public static final String FAILOVER_SERVICENAME = "00-00---000-VIPSRV_FAILOVER_SERVICENAME-000---00-00"; - public static final String DEFAULT_NAMESPACE_ID = "public"; public static final int REQUEST_DOMAIN_RETRY_COUNT = 3; From ce8b20565d42347ae6b6b1dbc68a9862dd62d6bb Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Sun, 8 Oct 2023 18:15:35 +0800 Subject: [PATCH 14/32] =?UTF-8?q?failoverSwith=E5=8E=BB=E9=99=A4serviceNam?= =?UTF-8?q?eSet=E9=9B=86=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/nacos/client/naming/NacosNamingService.java | 6 +++--- .../nacos/client/naming/backups/FailoverReactor.java | 9 +++++++-- .../nacos/client/naming/backups/FailoverSwitch.java | 3 --- 3 files changed, 10 insertions(+), 8 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 df5a42d6767..03fba8397c5 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 @@ -235,7 +235,7 @@ public List getAllInstances(String serviceName, List clusters, public List getAllInstances(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException { List list; - ServiceInfo serviceInfo = getServiceInfo(serviceName,groupName, clusters, subscribe); + ServiceInfo serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { return new ArrayList<>(); } @@ -285,7 +285,7 @@ public List selectInstances(String serviceName, List clusters, @Override public List selectInstances(String serviceName, String groupName, List clusters, boolean healthy, boolean subscribe) throws NacosException { - ServiceInfo serviceInfo = getServiceInfo(serviceName,groupName, clusters, subscribe); + ServiceInfo serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); return selectInstances(serviceInfo, healthy); } @@ -371,7 +371,7 @@ public Instance selectOneHealthyInstance(String serviceName, List cluste @Override public Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException { - ServiceInfo serviceInfo = getServiceInfo(serviceName,groupName, clusters, subscribe); + ServiceInfo serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); return Balancer.RandomByWeight.selectHost(serviceInfo); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index e401a1aa00e..48dac385e12 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -59,7 +59,9 @@ public class FailoverReactor implements Closeable { private String notifierEventScope; - private MultiGauge failoverInstanceCounts = MultiGauge.builder("nacos_naming_client_failover_instances").description("Nacos failover data service count").register(Metrics.globalRegistry); + private MultiGauge failoverInstanceCounts = MultiGauge.builder("nacos_naming_client_failover_instances") + .description("Nacos failover data service count") + .register(Metrics.globalRegistry); public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String cacheDir, String notifierEventScope) { this.serviceInfoHolder = serviceInfoHolder; @@ -103,7 +105,10 @@ public void run() { } if (failoverMap.size() > 0) { - failoverInstanceCounts.register(failoverMap.keySet().stream().map(serviceName -> MultiGauge.Row.of(Tags.of("service_name", serviceName), ((ServiceInfo)failoverMap.get(serviceName)).ipCount())).collect(Collectors.toList()), true); + failoverInstanceCounts.register(failoverMap.keySet().stream().map( + serviceName -> MultiGauge.Row.of(Tags.of("service_name", serviceName), + ((ServiceInfo)failoverMap.get(serviceName)).ipCount())) + .collect(Collectors.toList()), true); serviceMap = failoverMap; } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java index 8285fbeca4d..a0df8cb2809 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java @@ -16,8 +16,6 @@ package com.alibaba.nacos.client.naming.backups; -import java.util.Set; - /** * Failover switch. * @@ -30,7 +28,6 @@ public class FailoverSwitch { */ private boolean enabled; - public boolean getEnabled() { return enabled; } From 7f555ec3ed1321e54af3291d0ec587412abad5d1 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Sun, 8 Oct 2023 18:21:52 +0800 Subject: [PATCH 15/32] =?UTF-8?q?failoverSwith=E5=8E=BB=E9=99=A4serviceNam?= =?UTF-8?q?eSet=E9=9B=86=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/nacos/client/naming/backups/FailoverReactor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 48dac385e12..d4b8fcfae66 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -107,7 +107,7 @@ public void run() { if (failoverMap.size() > 0) { failoverInstanceCounts.register(failoverMap.keySet().stream().map( serviceName -> MultiGauge.Row.of(Tags.of("service_name", serviceName), - ((ServiceInfo)failoverMap.get(serviceName)).ipCount())) + ((ServiceInfo) failoverMap.get(serviceName)).ipCount())) .collect(Collectors.toList()), true); serviceMap = failoverMap; } From aec2cfcf4a4c9c35e0c80f5132ca0ca0258e4f5b Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Mon, 16 Oct 2023 00:05:27 +0800 Subject: [PATCH 16/32] =?UTF-8?q?=E7=89=B9=E6=AE=8A=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8B=E5=A2=9E=E8=A1=A5=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../naming/backups/FailoverReactor.java | 15 ++-- .../datasource/DiskFailoverDataSource.java | 18 ++++- .../naming/cache/ServiceInfoHolder.java | 34 ++------- .../client/naming/utils/CacheDirUtil.java | 72 +++++++++++++++++++ 4 files changed, 104 insertions(+), 35 deletions(-) create mode 100644 client/src/main/java/com/alibaba/nacos/client/naming/utils/CacheDirUtil.java diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index d4b8fcfae66..3859a23efa4 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -23,6 +23,7 @@ import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.spi.NacosServiceLoader; +import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.ThreadUtils; import io.micrometer.core.instrument.Metrics; import io.micrometer.core.instrument.MultiGauge; @@ -63,12 +64,13 @@ public class FailoverReactor implements Closeable { .description("Nacos failover data service count") .register(Metrics.globalRegistry); - public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String cacheDir, String notifierEventScope) { + public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String notifierEventScope) { this.serviceInfoHolder = serviceInfoHolder; this.notifierEventScope = notifierEventScope; Collection dataSources = NacosServiceLoader.load(FailoverDataSource.class); for (FailoverDataSource dataSource : dataSources) { failoverDataSource = dataSource; + NAMING_LOGGER.info("FailoverDataSource type is {}", dataSource.getClass()); break; } // init executorService @@ -101,13 +103,18 @@ public void run() { Map failoverMap = new ConcurrentHashMap<>(200); Map failoverData = failoverDataSource.getFailoverData(); for (Map.Entry entry : failoverData.entrySet()) { + ServiceInfo newService = (ServiceInfo) entry.getValue().getData(); + ServiceInfo oldService = serviceMap.get(entry.getKey()); + if (serviceInfoHolder.isChangedServiceInfo(oldService, newService)) { + NAMING_LOGGER.info("[NA] failoverdata isChangedServiceInfo. newService:{}", JacksonUtils.toJson(newService)); + } failoverMap.put(entry.getKey(), (ServiceInfo) entry.getValue().getData()); } if (failoverMap.size() > 0) { failoverInstanceCounts.register(failoverMap.keySet().stream().map( - serviceName -> MultiGauge.Row.of(Tags.of("service_name", serviceName), - ((ServiceInfo) failoverMap.get(serviceName)).ipCount())) + serviceName -> MultiGauge.Row.of(Tags.of("service_name", serviceName, ""), + ((ServiceInfo) failoverMap.get(serviceName)).ipCount())) .collect(Collectors.toList()), true); serviceMap = failoverMap; } @@ -174,4 +181,4 @@ public void shutdown() throws NacosException { ThreadUtils.shutdownThreadPool(executorService, NAMING_LOGGER); NAMING_LOGGER.info("{} do shutdown stop", className); } -} +} \ No newline at end of file diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java index 4fb4ffeae23..86f50964cd1 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java @@ -24,6 +24,7 @@ import com.alibaba.nacos.client.naming.cache.ConcurrentDiskUtil; import com.alibaba.nacos.client.naming.cache.DiskCache; import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; +import com.alibaba.nacos.client.naming.utils.CacheDirUtil; import com.alibaba.nacos.client.naming.utils.CollectionUtils; import com.alibaba.nacos.client.naming.utils.UtilAndComs; import com.alibaba.nacos.common.utils.JacksonUtils; @@ -56,16 +57,24 @@ public class DiskFailoverDataSource implements FailoverDataSource { private static final String NO_FAILOVER_MODE = "0"; + private static final String FAILOVER_MODE_PARAM = "failover-mode"; + private Map serviceMap = new ConcurrentHashMap<>(); + private final Map switchParams = new ConcurrentHashMap<>(); + private String failoverDir; private long lastModifiedMillis = 0L; public DiskFailoverDataSource(ServiceInfoHolder serviceInfoHolder) { + failoverDir = CacheDirUtil.gettCacheDir() + FAILOVER_DIR; this.failoverDir = serviceInfoHolder.getCacheDir() + FAILOVER_DIR; } + public void init() { + } + class FailoverFileReader implements Runnable { @Override @@ -141,6 +150,7 @@ public FailoverSwitch getSwitch() { try { File switchFile = Paths.get(failoverDir, UtilAndComs.FAILOVER_SWITCH).toFile(); if (!switchFile.exists()) { + switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString()); NAMING_LOGGER.debug("failover switch is not found, {}", switchFile.getName()); return new FailoverSwitch(Boolean.FALSE); } @@ -157,10 +167,12 @@ public FailoverSwitch getSwitch() { for (String line : lines) { String line1 = line.trim(); if (IS_FAILOVER_MODE.equals(line1)) { + switchParams.put(FAILOVER_MODE_PARAM, Boolean.TRUE.toString()); NAMING_LOGGER.info("failover-mode is on"); new FailoverFileReader().run(); return new FailoverSwitch(Boolean.TRUE); } else if (NO_FAILOVER_MODE.equals(line1)) { + switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString()); NAMING_LOGGER.info("failover-mode is off"); } } @@ -171,12 +183,16 @@ public FailoverSwitch getSwitch() { NAMING_LOGGER.error("[NA] failed to read failover switch.", e); } + switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString()); return new FailoverSwitch(Boolean.FALSE); } @Override public Map getFailoverData() { - return serviceMap; + if (Boolean.parseBoolean(switchParams.get(FAILOVER_MODE_PARAM))) { + return serviceMap; + } + return new ConcurrentHashMap<>(0); } } \ No newline at end of file diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java index ea76d24f011..9d8f0528ea1 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java @@ -25,13 +25,13 @@ import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.naming.backups.FailoverReactor; import com.alibaba.nacos.client.naming.event.InstancesChangeEvent; +import com.alibaba.nacos.client.naming.utils.CacheDirUtil; import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.utils.ConvertUtils; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.StringUtils; -import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -50,14 +50,6 @@ */ public class ServiceInfoHolder implements Closeable { - private static final String JM_SNAPSHOT_PATH_PROPERTY = "JM.SNAPSHOT.PATH"; - - private static final String FILE_PATH_NACOS = "nacos"; - - private static final String FILE_PATH_NAMING = "naming"; - - private static final String USER_HOME_PROPERTY = "user.home"; - private final ConcurrentMap serviceInfoMap; private final FailoverReactor failoverReactor; @@ -69,36 +61,18 @@ public class ServiceInfoHolder implements Closeable { private String notifierEventScope; public ServiceInfoHolder(String namespace, String notifierEventScope, NacosClientProperties properties) { - initCacheDir(namespace, properties); + cacheDir = CacheDirUtil.initCacheDir(namespace, properties); if (isLoadCacheAtStart(properties)) { this.serviceInfoMap = new ConcurrentHashMap<>(DiskCache.read(this.cacheDir)); } else { this.serviceInfoMap = new ConcurrentHashMap<>(16); } - this.failoverReactor = new FailoverReactor(this, cacheDir, notifierEventScope); - + this.failoverReactor = new FailoverReactor(this, notifierEventScope); this.pushEmptyProtection = isPushEmptyProtect(properties); this.notifierEventScope = notifierEventScope; } - private void initCacheDir(String namespace, NacosClientProperties properties) { - String jmSnapshotPath = properties.getProperty(JM_SNAPSHOT_PATH_PROPERTY); - - String namingCacheRegistryDir = ""; - if (properties.getProperty(PropertyKeyConst.NAMING_CACHE_REGISTRY_DIR) != null) { - namingCacheRegistryDir = File.separator + properties.getProperty(PropertyKeyConst.NAMING_CACHE_REGISTRY_DIR); - } - - if (!StringUtils.isBlank(jmSnapshotPath)) { - cacheDir = jmSnapshotPath + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir - + File.separator + FILE_PATH_NAMING + File.separator + namespace; - } else { - cacheDir = properties.getProperty(USER_HOME_PROPERTY) + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir - + File.separator + FILE_PATH_NAMING + File.separator + namespace; - } - } - private boolean isLoadCacheAtStart(NacosClientProperties properties) { boolean loadCacheAtStart = false; if (properties != null && StringUtils @@ -124,7 +98,6 @@ public Map getServiceInfoMap() { } public ServiceInfo getServiceInfo(final String serviceName, final String groupName, final String clusters) { - NAMING_LOGGER.debug("failover-mode: {}", failoverReactor.isFailoverSwitch()); String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName); String key = ServiceInfo.getKey(groupedServiceName, clusters); return serviceInfoMap.get(key); @@ -183,6 +156,7 @@ private boolean isEmptyOrErrorPush(ServiceInfo serviceInfo) { /** * isChangedServiceInfo. + * * @param oldService old service data * @param newService new service data * @return diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/CacheDirUtil.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/CacheDirUtil.java new file mode 100644 index 00000000000..e6732c2cf2d --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/CacheDirUtil.java @@ -0,0 +1,72 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.client.naming.utils; + +import com.alibaba.nacos.api.PropertyKeyConst; +import com.alibaba.nacos.common.utils.StringUtils; + +import java.io.File; + +import com.alibaba.nacos.client.env.NacosClientProperties; + +/** + * Cache Dir Utils. + * + * @author zongkang.guo + */ +public class CacheDirUtil { + + private static String cacheDir; + + private static final String JM_SNAPSHOT_PATH_PROPERTY = "JM.SNAPSHOT.PATH"; + + private static final String FILE_PATH_NACOS = "nacos"; + + private static final String FILE_PATH_NAMING = "naming"; + + private static final String USER_HOME_PROPERTY = "user.home"; + + /** + * Init cache dir. + * @param namespace namespace. + * @param properties nacosClientProperties. + * @return + */ + public static String initCacheDir(String namespace, NacosClientProperties properties) { + + String jmSnapshotPath = System.getProperty(JM_SNAPSHOT_PATH_PROPERTY); + + String namingCacheRegistryDir = ""; + if (properties.getProperty(PropertyKeyConst.NAMING_CACHE_REGISTRY_DIR) != null) { + namingCacheRegistryDir = File.separator + properties.getProperty(PropertyKeyConst.NAMING_CACHE_REGISTRY_DIR); + } + + if (!StringUtils.isBlank(jmSnapshotPath)) { + cacheDir = jmSnapshotPath + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir + + File.separator + FILE_PATH_NAMING + File.separator + namespace; + } else { + cacheDir = System.getProperty(USER_HOME_PROPERTY) + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir + + File.separator + FILE_PATH_NAMING + File.separator + namespace; + } + + return cacheDir; + } + + public static String gettCacheDir() { + return cacheDir; + } +} From 425a318f83d5bb7ffcb1e36e8a0f5fa0f180abfe Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Thu, 19 Oct 2023 17:39:17 +0800 Subject: [PATCH 17/32] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96checkstyle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/naming/NacosNamingService.java | 156 +++++++++--------- .../client/naming/backups/FailoverData.java | 16 +- .../naming/backups/FailoverDataSource.java | 8 +- .../naming/backups/FailoverReactor.java | 62 +++---- .../client/naming/backups/FailoverSwitch.java | 8 +- .../naming/backups/NamingFailoverData.java | 4 +- .../datasource/DiskFailoverDataSource.java | 60 +++---- .../naming/cache/ServiceInfoHolder.java | 80 ++++----- .../client/naming/utils/CacheDirUtil.java | 37 +++-- 9 files changed, 217 insertions(+), 214 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 03fba8397c5..a874f610127 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 @@ -53,47 +53,47 @@ */ @SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule") public class NacosNamingService implements NamingService { - + private static final String DEFAULT_NAMING_LOG_FILE_PATH = "naming.log"; - + private static final String UP = "UP"; - + private static final String DOWN = "DOWN"; - + /** * Each Naming service should have different namespace. */ private String namespace; - + private String logName; - + private ServiceInfoHolder serviceInfoHolder; - + private InstancesChangeNotifier changeNotifier; - + private NamingClientProxy clientProxy; - + private String notifierEventScope; - + public NacosNamingService(String serverList) throws NacosException { Properties properties = new Properties(); properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList); init(properties); } - + public NacosNamingService(Properties properties) throws NacosException { init(properties); } - + private void init(Properties properties) throws NacosException { final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties); - + ValidatorUtils.checkInitParam(nacosClientProperties); this.namespace = InitUtils.initNamespaceForNaming(nacosClientProperties); InitUtils.initSerialization(); InitUtils.initWebRootContext(nacosClientProperties); initLogName(nacosClientProperties); - + this.notifierEventScope = UUID.randomUUID().toString(); this.changeNotifier = new InstancesChangeNotifier(this.notifierEventScope); NotifyCenter.registerToPublisher(InstancesChangeEvent.class, 16384); @@ -102,26 +102,26 @@ private void init(Properties properties) throws NacosException { this.clientProxy = new NamingClientProxyDelegate(this.namespace, serviceInfoHolder, nacosClientProperties, changeNotifier); } - + private void initLogName(NacosClientProperties properties) { logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME, DEFAULT_NAMING_LOG_FILE_PATH); } - + @Override public void registerInstance(String serviceName, String ip, int port) throws NacosException { registerInstance(serviceName, ip, port, Constants.DEFAULT_CLUSTER_NAME); } - + @Override public void registerInstance(String serviceName, String groupName, String ip, int port) throws NacosException { registerInstance(serviceName, groupName, ip, port, Constants.DEFAULT_CLUSTER_NAME); } - + @Override public void registerInstance(String serviceName, String ip, int port, String clusterName) throws NacosException { registerInstance(serviceName, Constants.DEFAULT_GROUP, ip, port, clusterName); } - + @Override public void registerInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException { @@ -132,47 +132,47 @@ public void registerInstance(String serviceName, String groupName, String ip, in instance.setClusterName(clusterName); registerInstance(serviceName, groupName, instance); } - + @Override public void registerInstance(String serviceName, Instance instance) throws NacosException { registerInstance(serviceName, Constants.DEFAULT_GROUP, instance); } - + @Override public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException { NamingUtils.checkInstanceIsLegal(instance); clientProxy.registerService(serviceName, groupName, instance); } - + @Override public void batchRegisterInstance(String serviceName, String groupName, List instances) throws NacosException { NamingUtils.batchCheckInstanceIsLegal(instances); clientProxy.batchRegisterService(serviceName, groupName, instances); } - + @Override public void batchDeregisterInstance(String serviceName, String groupName, List instances) throws NacosException { NamingUtils.batchCheckInstanceIsLegal(instances); clientProxy.batchDeregisterService(serviceName, groupName, instances); } - + @Override public void deregisterInstance(String serviceName, String ip, int port) throws NacosException { deregisterInstance(serviceName, ip, port, Constants.DEFAULT_CLUSTER_NAME); } - + @Override public void deregisterInstance(String serviceName, String groupName, String ip, int port) throws NacosException { deregisterInstance(serviceName, groupName, ip, port, Constants.DEFAULT_CLUSTER_NAME); } - + @Override public void deregisterInstance(String serviceName, String ip, int port, String clusterName) throws NacosException { deregisterInstance(serviceName, Constants.DEFAULT_GROUP, ip, port, clusterName); } - + @Override public void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException { @@ -182,58 +182,58 @@ public void deregisterInstance(String serviceName, String groupName, String ip, instance.setClusterName(clusterName); deregisterInstance(serviceName, groupName, instance); } - + @Override public void deregisterInstance(String serviceName, Instance instance) throws NacosException { deregisterInstance(serviceName, Constants.DEFAULT_GROUP, instance); } - + @Override public void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException { clientProxy.deregisterService(serviceName, groupName, instance); } - + @Override public List getAllInstances(String serviceName) throws NacosException { return getAllInstances(serviceName, new ArrayList<>()); } - + @Override public List getAllInstances(String serviceName, String groupName) throws NacosException { return getAllInstances(serviceName, groupName, new ArrayList<>()); } - + @Override public List getAllInstances(String serviceName, boolean subscribe) throws NacosException { return getAllInstances(serviceName, new ArrayList<>(), subscribe); } - + @Override public List getAllInstances(String serviceName, String groupName, boolean subscribe) throws NacosException { return getAllInstances(serviceName, groupName, new ArrayList<>(), subscribe); } - + @Override public List getAllInstances(String serviceName, List clusters) throws NacosException { return getAllInstances(serviceName, clusters, true); } - + @Override public List getAllInstances(String serviceName, String groupName, List clusters) throws NacosException { return getAllInstances(serviceName, groupName, clusters, true); } - + @Override public List getAllInstances(String serviceName, List clusters, boolean subscribe) throws NacosException { return getAllInstances(serviceName, Constants.DEFAULT_GROUP, clusters, subscribe); } - + @Override public List getAllInstances(String serviceName, String groupName, List clusters, - boolean subscribe) throws NacosException { + boolean subscribe) throws NacosException { List list; ServiceInfo serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { @@ -241,60 +241,60 @@ public List getAllInstances(String serviceName, String groupName, List } return list; } - + @Override public List selectInstances(String serviceName, boolean healthy) throws NacosException { return selectInstances(serviceName, new ArrayList<>(), healthy); } - + @Override public List selectInstances(String serviceName, String groupName, boolean healthy) throws NacosException { return selectInstances(serviceName, groupName, healthy, true); } - + @Override public List selectInstances(String serviceName, boolean healthy, boolean subscribe) throws NacosException { return selectInstances(serviceName, new ArrayList<>(), healthy, subscribe); } - + @Override public List selectInstances(String serviceName, String groupName, boolean healthy, boolean subscribe) throws NacosException { return selectInstances(serviceName, groupName, new ArrayList<>(), healthy, subscribe); } - + @Override public List selectInstances(String serviceName, List clusters, boolean healthy) throws NacosException { return selectInstances(serviceName, clusters, healthy, true); } - + @Override public List selectInstances(String serviceName, String groupName, List clusters, boolean healthy) throws NacosException { return selectInstances(serviceName, groupName, clusters, healthy, true); } - + @Override public List selectInstances(String serviceName, List clusters, boolean healthy, boolean subscribe) throws NacosException { return selectInstances(serviceName, Constants.DEFAULT_GROUP, clusters, healthy, subscribe); } - + @Override public List selectInstances(String serviceName, String groupName, List clusters, boolean healthy, - boolean subscribe) throws NacosException { + boolean subscribe) throws NacosException { ServiceInfo serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); return selectInstances(serviceInfo, healthy); } - + private List selectInstances(ServiceInfo serviceInfo, boolean healthy) { List list; if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { return new ArrayList<>(); } - + Iterator iterator = list.iterator(); while (iterator.hasNext()) { Instance instance = iterator.next(); @@ -302,12 +302,12 @@ private List selectInstances(ServiceInfo serviceInfo, boolean healthy) iterator.remove(); } } - + return list; } - - private ServiceInfo getServiceInfo(String serviceName, String groupName, List clusters, - boolean subscribe) throws NacosException { + + private ServiceInfo getServiceInfo(String serviceName, String groupName, List clusters, boolean subscribe) + throws NacosException { ServiceInfo serviceInfo; String clusterString = StringUtils.join(clusters, ","); if (serviceInfoHolder.isFailoverSwitch()) { @@ -329,67 +329,67 @@ private ServiceInfo getServiceInfo(String serviceName, String groupName, List()); } - + @Override public Instance selectOneHealthyInstance(String serviceName, String groupName) throws NacosException { return selectOneHealthyInstance(serviceName, groupName, true); } - + @Override public Instance selectOneHealthyInstance(String serviceName, boolean subscribe) throws NacosException { return selectOneHealthyInstance(serviceName, new ArrayList<>(), subscribe); } - + @Override public Instance selectOneHealthyInstance(String serviceName, String groupName, boolean subscribe) throws NacosException { return selectOneHealthyInstance(serviceName, groupName, new ArrayList<>(), subscribe); } - + @Override public Instance selectOneHealthyInstance(String serviceName, List clusters) throws NacosException { return selectOneHealthyInstance(serviceName, clusters, true); } - + @Override public Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters) throws NacosException { return selectOneHealthyInstance(serviceName, groupName, clusters, true); } - + @Override public Instance selectOneHealthyInstance(String serviceName, List clusters, boolean subscribe) throws NacosException { return selectOneHealthyInstance(serviceName, Constants.DEFAULT_GROUP, clusters, subscribe); } - + @Override public Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters, - boolean subscribe) throws NacosException { + boolean subscribe) throws NacosException { ServiceInfo serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); return Balancer.RandomByWeight.selectHost(serviceInfo); } - + @Override public void subscribe(String serviceName, EventListener listener) throws NacosException { subscribe(serviceName, new ArrayList<>(), listener); } - + @Override public void subscribe(String serviceName, String groupName, EventListener listener) throws NacosException { subscribe(serviceName, groupName, new ArrayList<>(), listener); } - + @Override public void subscribe(String serviceName, List clusters, EventListener listener) throws NacosException { subscribe(serviceName, Constants.DEFAULT_GROUP, clusters, listener); } - + @Override public void subscribe(String serviceName, String groupName, List clusters, EventListener listener) throws NacosException { @@ -400,22 +400,22 @@ public void subscribe(String serviceName, String groupName, List cluster changeNotifier.registerListener(groupName, serviceName, clusterString, listener); clientProxy.subscribe(serviceName, groupName, clusterString); } - + @Override public void unsubscribe(String serviceName, EventListener listener) throws NacosException { unsubscribe(serviceName, new ArrayList<>(), listener); } - + @Override public void unsubscribe(String serviceName, String groupName, EventListener listener) throws NacosException { unsubscribe(serviceName, groupName, new ArrayList<>(), listener); } - + @Override public void unsubscribe(String serviceName, List clusters, EventListener listener) throws NacosException { unsubscribe(serviceName, Constants.DEFAULT_GROUP, clusters, listener); } - + @Override public void unsubscribe(String serviceName, String groupName, List clusters, EventListener listener) throws NacosException { @@ -425,44 +425,44 @@ public void unsubscribe(String serviceName, String groupName, List clust clientProxy.unsubscribe(serviceName, groupName, clustersString); } } - + @Override public ListView getServicesOfServer(int pageNo, int pageSize) throws NacosException { return getServicesOfServer(pageNo, pageSize, Constants.DEFAULT_GROUP); } - + @Override public ListView getServicesOfServer(int pageNo, int pageSize, String groupName) throws NacosException { return getServicesOfServer(pageNo, pageSize, groupName, null); } - + @Override public ListView getServicesOfServer(int pageNo, int pageSize, AbstractSelector selector) throws NacosException { return getServicesOfServer(pageNo, pageSize, Constants.DEFAULT_GROUP, selector); } - + @Override public ListView getServicesOfServer(int pageNo, int pageSize, String groupName, AbstractSelector selector) throws NacosException { return clientProxy.getServiceList(pageNo, pageSize, groupName, selector); } - + @Override public List getSubscribeServices() { return changeNotifier.getSubscribeServices(); } - + @Override public String getServerStatus() { return clientProxy.serverHealthy() ? UP : DOWN; } - + @Override public void shutDown() throws NacosException { serviceInfoHolder.shutdown(); clientProxy.shutdown(); NotifyCenter.deregisterSubscriber(changeNotifier); - + } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java index dd437916526..0f0422c9762 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverData.java @@ -22,22 +22,22 @@ * @author zongkang.guo */ public class FailoverData { - + /** * failover type,naming or config. */ private DataType dataType; - + /** * failover data. */ private Object data; - + public FailoverData(DataType dataType, Object data) { this.data = data; this.dataType = dataType; } - + public enum DataType { /** * naming. @@ -48,19 +48,19 @@ public enum DataType { */ config } - + public DataType getDataType() { return dataType; } - + public void setDataType(DataType dataType) { this.dataType = dataType; } - + public Object getData() { return data; } - + public void setData(Object data) { this.data = data; } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java index 305507dcb9e..1f34661fd08 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverDataSource.java @@ -24,20 +24,20 @@ * @author Nacos */ public interface FailoverDataSource { - + /** * Get current disaster recovery switch. * * @return */ FailoverSwitch getSwitch(); - - + + /** * Get current disaster recovery data. * * @return map */ Map getFailoverData(); - + } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 3859a23efa4..9ea98117d61 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -47,23 +47,22 @@ * @author nkorange */ public class FailoverReactor implements Closeable { - + private Map serviceMap = new ConcurrentHashMap<>(); - + private boolean failoverSwitchEnable; - + private final ServiceInfoHolder serviceInfoHolder; - + private final ScheduledExecutorService executorService; - + private FailoverDataSource failoverDataSource; - + private String notifierEventScope; - + private MultiGauge failoverInstanceCounts = MultiGauge.builder("nacos_naming_client_failover_instances") - .description("Nacos failover data service count") - .register(Metrics.globalRegistry); - + .description("Nacos failover data service count").register(Metrics.globalRegistry); + public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String notifierEventScope) { this.serviceInfoHolder = serviceInfoHolder; this.notifierEventScope = notifierEventScope; @@ -82,21 +81,21 @@ public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String notifierEvent }); this.init(); } - + /** * Init. */ public void init() { - + executorService.scheduleWithFixedDelay(new FailoverSwitchRefresher(), 0L, 5000L, TimeUnit.MILLISECONDS); - + } - + class FailoverSwitchRefresher implements Runnable { - + @Override public void run() { - + FailoverSwitch fSwitch = failoverDataSource.getSwitch(); if (fSwitch != null && fSwitch.getEnabled()) { failoverSwitchEnable = true; @@ -106,22 +105,23 @@ public void run() { ServiceInfo newService = (ServiceInfo) entry.getValue().getData(); ServiceInfo oldService = serviceMap.get(entry.getKey()); if (serviceInfoHolder.isChangedServiceInfo(oldService, newService)) { - NAMING_LOGGER.info("[NA] failoverdata isChangedServiceInfo. newService:{}", JacksonUtils.toJson(newService)); + NAMING_LOGGER.info("[NA] failoverdata isChangedServiceInfo. newService:{}", + JacksonUtils.toJson(newService)); } failoverMap.put(entry.getKey(), (ServiceInfo) entry.getValue().getData()); } - + if (failoverMap.size() > 0) { - failoverInstanceCounts.register(failoverMap.keySet().stream().map( - serviceName -> MultiGauge.Row.of(Tags.of("service_name", serviceName, ""), - ((ServiceInfo) failoverMap.get(serviceName)).ipCount())) + failoverInstanceCounts.register(failoverMap.keySet().stream() + .map(serviceName -> MultiGauge.Row.of(Tags.of("service_name", serviceName, ""), + ((ServiceInfo) failoverMap.get(serviceName)).ipCount())) .collect(Collectors.toList()), true); serviceMap = failoverMap; } - + return; } - + if (fSwitch != null && failoverSwitchEnable && !fSwitch.getEnabled()) { failoverSwitchEnable = false; Map serviceInfoMap = serviceInfoHolder.getServiceInfoMap(); @@ -130,8 +130,8 @@ public void run() { ServiceInfo newService = serviceInfoMap.get(entry.getKey()); boolean changed = serviceInfoHolder.isChangedServiceInfo(oldService, newService); if (changed) { - NotifyCenter.publishEvent(new InstancesChangeEvent(notifierEventScope, newService.getName(), newService.getGroupName(), - newService.getClusters(), newService.getHosts())); + NotifyCenter.publishEvent(new InstancesChangeEvent(notifierEventScope, newService.getName(), + newService.getGroupName(), newService.getClusters(), newService.getHosts())); } } serviceMap.clear(); @@ -139,22 +139,22 @@ public void run() { } } } - + public boolean isFailoverSwitch() { return failoverSwitchEnable; } - + public ServiceInfo getService(String key) { ServiceInfo serviceInfo = serviceMap.get(key); - + if (serviceInfo == null) { serviceInfo = new ServiceInfo(); serviceInfo.setName(key); } - + return serviceInfo; } - + /** * Add day. * @@ -168,7 +168,7 @@ public Date addDay(Date date, int num) { startDT.add(Calendar.DAY_OF_MONTH, num); return startDT.getTime(); } - + /** * shutdown ThreadPool. * diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java index a0df8cb2809..518609467bd 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverSwitch.java @@ -22,20 +22,20 @@ * @author zongkang.guo */ public class FailoverSwitch { - + /** * Failover switch enable. */ private boolean enabled; - + public boolean getEnabled() { return enabled; } - + public void setEnabled(boolean enabled) { this.enabled = enabled; } - + public FailoverSwitch(boolean enabled) { this.enabled = enabled; } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java index 84488ae7766..61011324d20 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/NamingFailoverData.java @@ -24,11 +24,11 @@ * @author zongkang.guo */ public class NamingFailoverData extends FailoverData { - + private NamingFailoverData(ServiceInfo serviceInfo) { super(DataType.naming, serviceInfo); } - + public static NamingFailoverData newNamingFailoverData(ServiceInfo serviceInfo) { return new NamingFailoverData(serviceInfo); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java index 86f50964cd1..7ae70c71f1e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java @@ -50,67 +50,67 @@ * @author zongkang.guo */ public class DiskFailoverDataSource implements FailoverDataSource { - + private static final String FAILOVER_DIR = "/failover"; - + private static final String IS_FAILOVER_MODE = "1"; - + private static final String NO_FAILOVER_MODE = "0"; - + private static final String FAILOVER_MODE_PARAM = "failover-mode"; - + private Map serviceMap = new ConcurrentHashMap<>(); - + private final Map switchParams = new ConcurrentHashMap<>(); - + private String failoverDir; - + private long lastModifiedMillis = 0L; - + public DiskFailoverDataSource(ServiceInfoHolder serviceInfoHolder) { failoverDir = CacheDirUtil.gettCacheDir() + FAILOVER_DIR; this.failoverDir = serviceInfoHolder.getCacheDir() + FAILOVER_DIR; } - + public void init() { } - + class FailoverFileReader implements Runnable { - + @Override public void run() { Map domMap = new HashMap<>(200); - + BufferedReader reader = null; try { - + File cacheDir = new File(failoverDir); if (!cacheDir.exists() && !cacheDir.mkdirs()) { throw new IllegalStateException("failed to create cache dir: " + failoverDir); } - + File[] files = cacheDir.listFiles(); if (files == null) { return; } - + for (File file : files) { if (!file.isFile()) { continue; } - + if (file.getName().equals(UtilAndComs.FAILOVER_SWITCH)) { continue; } - + ServiceInfo dom = null; - + try { dom = new ServiceInfo(URLDecoder.decode(file.getName(), StandardCharsets.UTF_8.name())); String dataString = ConcurrentDiskUtil.getFileContent(file, Charset.defaultCharset().toString()); reader = new BufferedReader(new StringReader(dataString)); - + String json; if ((json = reader.readLine()) != null) { try { @@ -119,7 +119,7 @@ public void run() { NAMING_LOGGER.error("[NA] error while parsing cached dom : {}", json, e); } } - + } catch (Exception e) { NAMING_LOGGER.error("[NA] failed to read cache for dom: {}", file.getName(), e); } finally { @@ -138,13 +138,13 @@ public void run() { } catch (Exception e) { NAMING_LOGGER.error("[NA] failed to read cache file", e); } - + if (domMap.size() > 0) { serviceMap = domMap; } } } - + @Override public FailoverSwitch getSwitch() { try { @@ -154,16 +154,16 @@ public FailoverSwitch getSwitch() { NAMING_LOGGER.debug("failover switch is not found, {}", switchFile.getName()); return new FailoverSwitch(Boolean.FALSE); } - + long modified = switchFile.lastModified(); - + if (lastModifiedMillis < modified) { lastModifiedMillis = modified; String failover = ConcurrentDiskUtil.getFileContent(switchFile.getPath(), Charset.defaultCharset().toString()); if (!StringUtils.isEmpty(failover)) { String[] lines = failover.split(DiskCache.getLineSeparator()); - + for (String line : lines) { String line1 = line.trim(); if (IS_FAILOVER_MODE.equals(line1)) { @@ -178,15 +178,15 @@ public FailoverSwitch getSwitch() { } } } - + } catch (Throwable e) { NAMING_LOGGER.error("[NA] failed to read failover switch.", e); } - + switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString()); return new FailoverSwitch(Boolean.FALSE); } - + @Override public Map getFailoverData() { if (Boolean.parseBoolean(switchParams.get(FAILOVER_MODE_PARAM))) { @@ -194,5 +194,5 @@ public Map getFailoverData() { } return new ConcurrentHashMap<>(0); } - + } \ No newline at end of file diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java index 9d8f0528ea1..5816f95a5fc 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java @@ -49,17 +49,17 @@ * @author xiweng.yy */ public class ServiceInfoHolder implements Closeable { - + private final ConcurrentMap serviceInfoMap; - + private final FailoverReactor failoverReactor; - + private final boolean pushEmptyProtection; - + private String cacheDir; - + private String notifierEventScope; - + public ServiceInfoHolder(String namespace, String notifierEventScope, NacosClientProperties properties) { cacheDir = CacheDirUtil.initCacheDir(namespace, properties); if (isLoadCacheAtStart(properties)) { @@ -67,42 +67,42 @@ public ServiceInfoHolder(String namespace, String notifierEventScope, NacosClien } else { this.serviceInfoMap = new ConcurrentHashMap<>(16); } - + this.failoverReactor = new FailoverReactor(this, notifierEventScope); this.pushEmptyProtection = isPushEmptyProtect(properties); this.notifierEventScope = notifierEventScope; } - + private boolean isLoadCacheAtStart(NacosClientProperties properties) { boolean loadCacheAtStart = false; - if (properties != null && StringUtils - .isNotEmpty(properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START))) { - loadCacheAtStart = ConvertUtils - .toBoolean(properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START)); + if (properties != null && StringUtils.isNotEmpty( + properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START))) { + loadCacheAtStart = ConvertUtils.toBoolean( + properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START)); } return loadCacheAtStart; } - + private boolean isPushEmptyProtect(NacosClientProperties properties) { boolean pushEmptyProtection = false; - if (properties != null && StringUtils - .isNotEmpty(properties.getProperty(PropertyKeyConst.NAMING_PUSH_EMPTY_PROTECTION))) { - pushEmptyProtection = ConvertUtils - .toBoolean(properties.getProperty(PropertyKeyConst.NAMING_PUSH_EMPTY_PROTECTION)); + if (properties != null && StringUtils.isNotEmpty( + properties.getProperty(PropertyKeyConst.NAMING_PUSH_EMPTY_PROTECTION))) { + pushEmptyProtection = ConvertUtils.toBoolean( + properties.getProperty(PropertyKeyConst.NAMING_PUSH_EMPTY_PROTECTION)); } return pushEmptyProtection; } - + public Map getServiceInfoMap() { return serviceInfoMap; } - + public ServiceInfo getServiceInfo(final String serviceName, final String groupName, final String clusters) { String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName); String key = ServiceInfo.getKey(groupedServiceName, clusters); return serviceInfoMap.get(key); } - + /** * Process service json. * @@ -114,7 +114,7 @@ public ServiceInfo processServiceInfo(String json) { serviceInfo.setJsonFromServer(json); return processServiceInfo(serviceInfo); } - + /** * Process service info. * @@ -141,19 +141,20 @@ public ServiceInfo processServiceInfo(ServiceInfo serviceInfo) { NAMING_LOGGER.info("current ips:({}) service: {} -> {}", serviceInfo.ipCount(), serviceInfo.getKey(), JacksonUtils.toJson(serviceInfo.getHosts())); if (!failoverReactor.isFailoverSwitch()) { - NotifyCenter.publishEvent(new InstancesChangeEvent(notifierEventScope, serviceInfo.getName(), serviceInfo.getGroupName(), - serviceInfo.getClusters(), serviceInfo.getHosts())); + NotifyCenter.publishEvent( + new InstancesChangeEvent(notifierEventScope, serviceInfo.getName(), serviceInfo.getGroupName(), + serviceInfo.getClusters(), serviceInfo.getHosts())); } - + DiskCache.write(serviceInfo, cacheDir); } return serviceInfo; } - + private boolean isEmptyOrErrorPush(ServiceInfo serviceInfo) { return null == serviceInfo.getHosts() || (pushEmptyProtection && !serviceInfo.validate()); } - + /** * isChangedServiceInfo. * @@ -181,13 +182,12 @@ public boolean isChangedServiceInfo(ServiceInfo oldService, ServiceInfo newServi for (Instance host : newService.getHosts()) { newHostMap.put(host.toInetAddr(), host); } - + Set modHosts = new HashSet<>(); Set newHosts = new HashSet<>(); Set remvHosts = new HashSet<>(); - - List> newServiceHosts = new ArrayList<>( - newHostMap.entrySet()); + + List> newServiceHosts = new ArrayList<>(newHostMap.entrySet()); for (Map.Entry entry : newServiceHosts) { Instance host = entry.getValue(); String key = entry.getKey(); @@ -195,35 +195,35 @@ public boolean isChangedServiceInfo(ServiceInfo oldService, ServiceInfo newServi modHosts.add(host); continue; } - + if (!oldHostMap.containsKey(key)) { newHosts.add(host); } } - + for (Map.Entry entry : oldHostMap.entrySet()) { Instance host = entry.getValue(); String key = entry.getKey(); if (newHostMap.containsKey(key)) { continue; } - + //add to remove hosts remvHosts.add(host); } - + if (newHosts.size() > 0) { changed = true; NAMING_LOGGER.info("new ips({}) service: {} -> {}", newHosts.size(), newService.getKey(), JacksonUtils.toJson(newHosts)); } - + if (remvHosts.size() > 0) { changed = true; NAMING_LOGGER.info("removed ips({}) service: {} -> {}", remvHosts.size(), newService.getKey(), JacksonUtils.toJson(remvHosts)); } - + if (modHosts.size() > 0) { changed = true; NAMING_LOGGER.info("modified ips({}) service: {} -> {}", modHosts.size(), newService.getKey(), @@ -231,21 +231,21 @@ public boolean isChangedServiceInfo(ServiceInfo oldService, ServiceInfo newServi } return changed; } - + public String getCacheDir() { return cacheDir; } - + public boolean isFailoverSwitch() { return failoverReactor.isFailoverSwitch(); } - + public ServiceInfo getFailoverServiceInfo(final String serviceName, final String groupName, final String clusters) { String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName); String key = ServiceInfo.getKey(groupedServiceName, clusters); return failoverReactor.getService(key); } - + @Override public void shutdown() throws NacosException { String className = this.getClass().getName(); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/CacheDirUtil.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/CacheDirUtil.java index e6732c2cf2d..74b212aaf4a 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/CacheDirUtil.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/CacheDirUtil.java @@ -29,43 +29,46 @@ * @author zongkang.guo */ public class CacheDirUtil { - + private static String cacheDir; - + private static final String JM_SNAPSHOT_PATH_PROPERTY = "JM.SNAPSHOT.PATH"; - + private static final String FILE_PATH_NACOS = "nacos"; - + private static final String FILE_PATH_NAMING = "naming"; - + private static final String USER_HOME_PROPERTY = "user.home"; - + /** * Init cache dir. - * @param namespace namespace. + * + * @param namespace namespace. * @param properties nacosClientProperties. * @return */ public static String initCacheDir(String namespace, NacosClientProperties properties) { - + String jmSnapshotPath = System.getProperty(JM_SNAPSHOT_PATH_PROPERTY); - + String namingCacheRegistryDir = ""; if (properties.getProperty(PropertyKeyConst.NAMING_CACHE_REGISTRY_DIR) != null) { - namingCacheRegistryDir = File.separator + properties.getProperty(PropertyKeyConst.NAMING_CACHE_REGISTRY_DIR); + namingCacheRegistryDir = + File.separator + properties.getProperty(PropertyKeyConst.NAMING_CACHE_REGISTRY_DIR); } - + if (!StringUtils.isBlank(jmSnapshotPath)) { - cacheDir = jmSnapshotPath + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir - + File.separator + FILE_PATH_NAMING + File.separator + namespace; + cacheDir = jmSnapshotPath + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir + File.separator + + FILE_PATH_NAMING + File.separator + namespace; } else { - cacheDir = System.getProperty(USER_HOME_PROPERTY) + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir - + File.separator + FILE_PATH_NAMING + File.separator + namespace; + cacheDir = + System.getProperty(USER_HOME_PROPERTY) + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir + + File.separator + FILE_PATH_NAMING + File.separator + namespace; } - + return cacheDir; } - + public static String gettCacheDir() { return cacheDir; } From 781a8dea1ea868f9548b8233a2289b700120c430 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Thu, 19 Oct 2023 17:43:28 +0800 Subject: [PATCH 18/32] =?UTF-8?q?=E5=AF=B9UtilAndComs=20=20checkstyle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/alibaba/nacos/client/naming/utils/UtilAndComs.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java index ff39653417c..1c55b1da088 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java @@ -38,7 +38,7 @@ public class UtilAndComs { public static final String ALL_IPS = "000--00-ALL_IPS--00--000"; public static final String FAILOVER_SWITCH = "00-00---000-VIPSRV_FAILOVER_SWITCH-000---00-00"; - + public static final String DEFAULT_NAMESPACE_ID = "public"; public static final int REQUEST_DOMAIN_RETRY_COUNT = 3; @@ -46,7 +46,7 @@ public class UtilAndComs { public static final String NACOS_NAMING_LOG_NAME = "com.alibaba.nacos.naming.log.filename"; public static final String NACOS_NAMING_LOG_LEVEL = "com.alibaba.nacos.naming.log.level"; - + public static final String NACOS_FAIL_OVER_STORAGE_TYPE = "com.alibaba.nacos.failover.storage.type"; public static final int DEFAULT_POLLING_THREAD_COUNT = From 282e2abbfd41002f63ed0fcede11b411c37f2702 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Thu, 19 Oct 2023 17:49:02 +0800 Subject: [PATCH 19/32] =?UTF-8?q?=E5=AF=B9test=E7=B1=BB=20=20checkstyle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../naming/backups/FailoverReactorTest.java | 18 +++++++++--------- .../datasource/DiskFailoverDataSourceTest.java | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java index 843d4cefbe3..149e78cf793 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/backups/FailoverReactorTest.java @@ -30,12 +30,12 @@ import java.util.concurrent.ScheduledExecutorService; public class FailoverReactorTest { - + @Test public void testInit() throws NacosException, NoSuchFieldException, IllegalAccessException { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp", UUID.randomUUID().toString()); + FailoverReactor failoverReactor = new FailoverReactor(holder, UUID.randomUUID().toString()); Field executorService = FailoverReactor.class.getDeclaredField("executorService"); executorService.setAccessible(true); ScheduledExecutorService o = (ScheduledExecutorService) executorService.get(failoverReactor); @@ -43,33 +43,33 @@ public void testInit() throws NacosException, NoSuchFieldException, IllegalAcces failoverReactor.shutdown(); Assert.assertTrue(o.isShutdown()); } - + @Test public void testAddDay() throws NacosException { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp", UUID.randomUUID().toString()); + FailoverReactor failoverReactor = new FailoverReactor(holder, UUID.randomUUID().toString()); Date date = new Date(); Date actual = failoverReactor.addDay(date, 1); Assert.assertEquals(date.getTime() + 24 * 60 * 60 * 1000, actual.getTime()); failoverReactor.shutdown(); } - + @Test public void testIsFailoverSwitch() throws NacosException { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp", UUID.randomUUID().toString()); + FailoverReactor failoverReactor = new FailoverReactor(holder, UUID.randomUUID().toString()); Assert.assertFalse(failoverReactor.isFailoverSwitch()); failoverReactor.shutdown(); - + } - + @Test public void testGetService() throws NacosException { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - FailoverReactor failoverReactor = new FailoverReactor(holder, "/tmp", UUID.randomUUID().toString()); + FailoverReactor failoverReactor = new FailoverReactor(holder, UUID.randomUUID().toString()); ServiceInfo info = failoverReactor.getService("aa@@bb"); Assert.assertEquals(new ServiceInfo("aa@@bb").toString(), info.toString()); failoverReactor.shutdown(); diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java index a7cebf9b480..ca08140be5a 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java @@ -26,7 +26,7 @@ import java.util.Map; public class DiskFailoverDataSourceTest extends TestCase { - + @Test public void testGetSwitch() { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); @@ -34,7 +34,7 @@ public void testGetSwitch() { DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder); diskFailoverDataSource.getSwitch(); } - + @Test public void testGetFailoverData() { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); @@ -42,7 +42,7 @@ public void testGetFailoverData() { DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder); diskFailoverDataSource.getFailoverData(); } - + @Test public void testSaveFailoverData() { ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); From bfe1740ebc1bc99f2b0e96853223ee25016c2cf5 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Thu, 19 Oct 2023 17:49:57 +0800 Subject: [PATCH 20/32] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=97=A0=E6=95=88?= =?UTF-8?q?=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java index 1c55b1da088..5a8f08e2689 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java @@ -47,8 +47,6 @@ public class UtilAndComs { public static final String NACOS_NAMING_LOG_LEVEL = "com.alibaba.nacos.naming.log.level"; - public static final String NACOS_FAIL_OVER_STORAGE_TYPE = "com.alibaba.nacos.failover.storage.type"; - public static final int DEFAULT_POLLING_THREAD_COUNT = ThreadUtils.getSuitableThreadCount(1) > 1 ? ThreadUtils.getSuitableThreadCount(1) / 2 : 1; From 1eb8cee8340fe9f71eaf140b1264318d08a1146a Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Thu, 19 Oct 2023 17:54:59 +0800 Subject: [PATCH 21/32] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=97=A0=E6=95=88?= =?UTF-8?q?=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/alibaba/nacos/client/naming/NacosNamingService.java | 1 - .../alibaba/nacos/client/naming/cache/ServiceInfoHolder.java | 2 -- 2 files changed, 3 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 a874f610127..757a791c10e 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 @@ -87,7 +87,6 @@ public NacosNamingService(Properties properties) throws NacosException { private void init(Properties properties) throws NacosException { final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties); - ValidatorUtils.checkInitParam(nacosClientProperties); this.namespace = InitUtils.initNamespaceForNaming(nacosClientProperties); InitUtils.initSerialization(); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java index 5816f95a5fc..74b1bb5d2d9 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java @@ -67,7 +67,6 @@ public ServiceInfoHolder(String namespace, String notifierEventScope, NacosClien } else { this.serviceInfoMap = new ConcurrentHashMap<>(16); } - this.failoverReactor = new FailoverReactor(this, notifierEventScope); this.pushEmptyProtection = isPushEmptyProtect(properties); this.notifierEventScope = notifierEventScope; @@ -145,7 +144,6 @@ public ServiceInfo processServiceInfo(ServiceInfo serviceInfo) { new InstancesChangeEvent(notifierEventScope, serviceInfo.getName(), serviceInfo.getGroupName(), serviceInfo.getClusters(), serviceInfo.getHosts())); } - DiskCache.write(serviceInfo, cacheDir); } return serviceInfo; From 8f91c08e667a7c56f1fd8a9601fa5a6e358749ea Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Thu, 19 Oct 2023 18:01:36 +0800 Subject: [PATCH 22/32] =?UTF-8?q?=E4=BF=AE=E6=94=B9serviceInfo=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E8=A2=AB=E9=87=8D=E6=96=B0=E8=A6=86=E7=9B=96=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/alibaba/nacos/client/naming/NacosNamingService.java | 2 +- 1 file changed, 1 insertion(+), 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 757a791c10e..c243be3bbd2 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 @@ -313,7 +313,7 @@ private ServiceInfo getServiceInfo(String serviceName, String groupName, List 0) { if (!clientProxy.isSubscribed(serviceName, groupName, clusterString)) { - serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString); + clientProxy.subscribe(serviceName, groupName, clusterString); } return serviceInfo; } From f8a6b5e6bdb14203f4bf542eec1e5e82f79ffa41 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Tue, 24 Oct 2023 17:47:46 +0800 Subject: [PATCH 23/32] =?UTF-8?q?=E4=BC=98=E5=8C=96getServiceInfo=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/naming/NacosNamingService.java | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 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 c243be3bbd2..ab5910c6667 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 @@ -36,6 +36,7 @@ import com.alibaba.nacos.client.naming.utils.CollectionUtils; import com.alibaba.nacos.client.naming.utils.InitUtils; import com.alibaba.nacos.client.naming.utils.UtilAndComs; +import com.alibaba.nacos.client.utils.PreInitUtils; import com.alibaba.nacos.client.utils.ValidatorUtils; import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.utils.StringUtils; @@ -86,6 +87,7 @@ public NacosNamingService(Properties properties) throws NacosException { } private void init(Properties properties) throws NacosException { + PreInitUtils.asyncPreLoadCostComponent(); final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties); ValidatorUtils.checkInitParam(nacosClientProperties); this.namespace = InitUtils.initNamespaceForNaming(nacosClientProperties); @@ -234,7 +236,10 @@ public List getAllInstances(String serviceName, List clusters, public List getAllInstances(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException { List list; - ServiceInfo serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); + ServiceInfo serviceInfo = getFailoverService(serviceName, groupName, clusters); + if (serviceInfo == null) { + serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); + } if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { return new ArrayList<>(); } @@ -284,7 +289,10 @@ public List selectInstances(String serviceName, List clusters, @Override public List selectInstances(String serviceName, String groupName, List clusters, boolean healthy, boolean subscribe) throws NacosException { - ServiceInfo serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); + ServiceInfo serviceInfo = getFailoverService(serviceName, groupName, clusters); + if (serviceInfo == null) { + serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); + } return selectInstances(serviceInfo, healthy); } @@ -305,9 +313,9 @@ private List selectInstances(ServiceInfo serviceInfo, boolean healthy) return list; } - private ServiceInfo getServiceInfo(String serviceName, String groupName, List clusters, boolean subscribe) + private ServiceInfo getFailoverService(String serviceName, String groupName, List clusters) throws NacosException { - ServiceInfo serviceInfo; + ServiceInfo serviceInfo = null; String clusterString = StringUtils.join(clusters, ","); if (serviceInfoHolder.isFailoverSwitch()) { serviceInfo = serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString); @@ -318,6 +326,13 @@ private ServiceInfo getServiceInfo(String serviceName, String groupName, List clusters, boolean subscribe) + throws NacosException { + ServiceInfo serviceInfo; + String clusterString = StringUtils.join(clusters, ","); if (subscribe) { serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { @@ -370,7 +385,10 @@ public Instance selectOneHealthyInstance(String serviceName, List cluste @Override public Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException { - ServiceInfo serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); + ServiceInfo serviceInfo = getFailoverService(serviceName, groupName, clusters); + if (serviceInfo == null) { + serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); + } return Balancer.RandomByWeight.selectHost(serviceInfo); } From d05d452d53dd9b53dc67e194cb436ad9017ba12c Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Wed, 25 Oct 2023 16:22:46 +0800 Subject: [PATCH 24/32] =?UTF-8?q?=E9=99=8D=E7=BA=A7=E5=BC=80=E5=85=B3?= =?UTF-8?q?=E5=88=9D=E6=AC=A1=E5=BC=80=E5=90=AF=E6=97=B6=E5=8F=91=E9=80=81?= =?UTF-8?q?=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/naming/NacosNamingService.java | 50 ++++++++++--------- .../naming/backups/FailoverReactor.java | 15 ++++-- 2 files changed, 37 insertions(+), 28 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 ab5910c6667..34d239ff81e 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 @@ -236,10 +236,7 @@ public List getAllInstances(String serviceName, List clusters, public List getAllInstances(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException { List list; - ServiceInfo serviceInfo = getFailoverService(serviceName, groupName, clusters); - if (serviceInfo == null) { - serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); - } + ServiceInfo serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { return new ArrayList<>(); } @@ -289,10 +286,7 @@ public List selectInstances(String serviceName, List clusters, @Override public List selectInstances(String serviceName, String groupName, List clusters, boolean healthy, boolean subscribe) throws NacosException { - ServiceInfo serviceInfo = getFailoverService(serviceName, groupName, clusters); - if (serviceInfo == null) { - serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); - } + ServiceInfo serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); return selectInstances(serviceInfo, healthy); } @@ -313,26 +307,22 @@ private List selectInstances(ServiceInfo serviceInfo, boolean healthy) return list; } - private ServiceInfo getFailoverService(String serviceName, String groupName, List clusters) + private ServiceInfo getServiceInfoByFailover(String serviceName, String groupName, String clusterString) throws NacosException { ServiceInfo serviceInfo = null; - String clusterString = StringUtils.join(clusters, ","); - if (serviceInfoHolder.isFailoverSwitch()) { - serviceInfo = serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString); - if (serviceInfo != null && serviceInfo.getHosts().size() > 0) { - if (!clientProxy.isSubscribed(serviceName, groupName, clusterString)) { - clientProxy.subscribe(serviceName, groupName, clusterString); - } - return serviceInfo; + serviceInfo = serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString); + if (serviceInfo != null && serviceInfo.getHosts().size() > 0) { + if (!clientProxy.isSubscribed(serviceName, groupName, clusterString)) { + clientProxy.subscribe(serviceName, groupName, clusterString); } + return serviceInfo; } return serviceInfo; } - private ServiceInfo getServiceInfo(String serviceName, String groupName, List clusters, boolean subscribe) - throws NacosException { + private ServiceInfo getServiceInfoBySubscribe(String serviceName, String groupName, String clusterString, + boolean subscribe) throws NacosException { ServiceInfo serviceInfo; - String clusterString = StringUtils.join(clusters, ","); if (subscribe) { serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString); if (null == serviceInfo || !clientProxy.isSubscribed(serviceName, groupName, clusterString)) { @@ -344,6 +334,21 @@ private ServiceInfo getServiceInfo(String serviceName, String groupName, List clusters, boolean subscribe) + throws NacosException { + ServiceInfo serviceInfo; + String clusterString = StringUtils.join(clusters, ","); + if (serviceInfoHolder.isFailoverSwitch()) { + serviceInfo = getServiceInfoByFailover(serviceName, groupName, clusterString); + if (serviceInfo != null) { + return serviceInfo; + } + } + + serviceInfo = getServiceInfoBySubscribe(serviceName, groupName, clusterString, subscribe); + return serviceInfo; + } + @Override public Instance selectOneHealthyInstance(String serviceName) throws NacosException { return selectOneHealthyInstance(serviceName, new ArrayList<>()); @@ -385,10 +390,7 @@ public Instance selectOneHealthyInstance(String serviceName, List cluste @Override public Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException { - ServiceInfo serviceInfo = getFailoverService(serviceName, groupName, clusters); - if (serviceInfo == null) { - serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); - } + ServiceInfo serviceInfo = getServiceInfo(serviceName, groupName, clusters, subscribe); return Balancer.RandomByWeight.selectHost(serviceInfo); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 9ea98117d61..69b568cd1cf 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -95,10 +95,12 @@ class FailoverSwitchRefresher implements Runnable { @Override public void run() { - FailoverSwitch fSwitch = failoverDataSource.getSwitch(); + boolean switchEnable = false; if (fSwitch != null && fSwitch.getEnabled()) { - failoverSwitchEnable = true; + switchEnable = true; + } + if (switchEnable) { Map failoverMap = new ConcurrentHashMap<>(200); Map failoverData = failoverDataSource.getFailoverData(); for (Map.Entry entry : failoverData.entrySet()) { @@ -107,6 +109,10 @@ public void run() { if (serviceInfoHolder.isChangedServiceInfo(oldService, newService)) { NAMING_LOGGER.info("[NA] failoverdata isChangedServiceInfo. newService:{}", JacksonUtils.toJson(newService)); + if (!failoverSwitchEnable) { + NotifyCenter.publishEvent(new InstancesChangeEvent(notifierEventScope, newService.getName(), + newService.getGroupName(), newService.getClusters(), newService.getHosts())); + } } failoverMap.put(entry.getKey(), (ServiceInfo) entry.getValue().getData()); } @@ -119,11 +125,11 @@ public void run() { serviceMap = failoverMap; } + failoverSwitchEnable = true; return; } - if (fSwitch != null && failoverSwitchEnable && !fSwitch.getEnabled()) { - failoverSwitchEnable = false; + if (failoverSwitchEnable && !switchEnable) { Map serviceInfoMap = serviceInfoHolder.getServiceInfoMap(); for (Map.Entry entry : serviceMap.entrySet()) { ServiceInfo oldService = entry.getValue(); @@ -135,6 +141,7 @@ public void run() { } } serviceMap.clear(); + failoverSwitchEnable = false; return; } } From 289a81e850c593220a23d85450772ef720b1f16d Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Thu, 26 Oct 2023 18:47:22 +0800 Subject: [PATCH 25/32] =?UTF-8?q?=E9=99=8D=E7=BA=A7=E5=BC=80=E5=85=B3?= =?UTF-8?q?=E5=BC=80=E5=90=AF=E6=97=B6=EF=BC=8C=E4=B9=9F=E5=8F=91=E9=80=81?= =?UTF-8?q?=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nacos/client/naming/backups/FailoverReactor.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 69b568cd1cf..ea857b066aa 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -109,10 +109,8 @@ public void run() { if (serviceInfoHolder.isChangedServiceInfo(oldService, newService)) { NAMING_LOGGER.info("[NA] failoverdata isChangedServiceInfo. newService:{}", JacksonUtils.toJson(newService)); - if (!failoverSwitchEnable) { - NotifyCenter.publishEvent(new InstancesChangeEvent(notifierEventScope, newService.getName(), - newService.getGroupName(), newService.getClusters(), newService.getHosts())); - } + NotifyCenter.publishEvent(new InstancesChangeEvent(notifierEventScope, newService.getName(), + newService.getGroupName(), newService.getClusters(), newService.getHosts())); } failoverMap.put(entry.getKey(), (ServiceInfo) entry.getValue().getData()); } From c00d0879e37d4966b89c52f3beb20544995766fc Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Wed, 1 Nov 2023 16:53:09 +0800 Subject: [PATCH 26/32] =?UTF-8?q?=E5=BD=93=E9=99=8D=E7=BA=A7=E5=BC=80?= =?UTF-8?q?=E5=90=AF=E6=97=B6=EF=BC=8C=E4=B8=8D=E5=86=8D=E8=AE=A2=E9=98=85?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E4=BF=A1=E6=81=AF=E3=80=82=E5=8E=BB=E9=99=A4?= =?UTF-8?q?=E5=A4=9A=E4=BD=99=E7=9A=84init=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nacos/client/naming/NacosNamingService.java | 15 +++------------ .../datasource/DiskFailoverDataSource.java | 3 --- 2 files changed, 3 insertions(+), 15 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 34d239ff81e..d3b7e953471 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 @@ -307,17 +307,8 @@ private List selectInstances(ServiceInfo serviceInfo, boolean healthy) return list; } - private ServiceInfo getServiceInfoByFailover(String serviceName, String groupName, String clusterString) - throws NacosException { - ServiceInfo serviceInfo = null; - serviceInfo = serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString); - if (serviceInfo != null && serviceInfo.getHosts().size() > 0) { - if (!clientProxy.isSubscribed(serviceName, groupName, clusterString)) { - clientProxy.subscribe(serviceName, groupName, clusterString); - } - return serviceInfo; - } - return serviceInfo; + private ServiceInfo getServiceInfoByFailover(String serviceName, String groupName, String clusterString) { + return serviceInfoHolder.getFailoverServiceInfo(serviceName, groupName, clusterString); } private ServiceInfo getServiceInfoBySubscribe(String serviceName, String groupName, String clusterString, @@ -340,7 +331,7 @@ private ServiceInfo getServiceInfo(String serviceName, String groupName, List 0) { return serviceInfo; } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java index 7ae70c71f1e..d46238240b7 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java @@ -72,9 +72,6 @@ public DiskFailoverDataSource(ServiceInfoHolder serviceInfoHolder) { this.failoverDir = serviceInfoHolder.getCacheDir() + FAILOVER_DIR; } - public void init() { - } - class FailoverFileReader implements Runnable { @Override From b86e132157c72c1cc5f8fb1632086bd0d6e49d61 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Fri, 10 Nov 2023 11:09:05 +0800 Subject: [PATCH 27/32] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E4=BF=AE=E5=A4=8DCI?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../naming/backups/datasource/DiskFailoverDataSource.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java index d46238240b7..ee3abb5b3a5 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSource.java @@ -23,7 +23,6 @@ import com.alibaba.nacos.client.naming.backups.NamingFailoverData; import com.alibaba.nacos.client.naming.cache.ConcurrentDiskUtil; import com.alibaba.nacos.client.naming.cache.DiskCache; -import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; import com.alibaba.nacos.client.naming.utils.CacheDirUtil; import com.alibaba.nacos.client.naming.utils.CollectionUtils; import com.alibaba.nacos.client.naming.utils.UtilAndComs; @@ -37,7 +36,6 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Paths; - import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -67,9 +65,8 @@ public class DiskFailoverDataSource implements FailoverDataSource { private long lastModifiedMillis = 0L; - public DiskFailoverDataSource(ServiceInfoHolder serviceInfoHolder) { + public DiskFailoverDataSource() { failoverDir = CacheDirUtil.gettCacheDir() + FAILOVER_DIR; - this.failoverDir = serviceInfoHolder.getCacheDir() + FAILOVER_DIR; } class FailoverFileReader implements Runnable { @@ -147,7 +144,6 @@ public FailoverSwitch getSwitch() { try { File switchFile = Paths.get(failoverDir, UtilAndComs.FAILOVER_SWITCH).toFile(); if (!switchFile.exists()) { - switchParams.put(FAILOVER_MODE_PARAM, Boolean.FALSE.toString()); NAMING_LOGGER.debug("failover switch is not found, {}", switchFile.getName()); return new FailoverSwitch(Boolean.FALSE); } From 778222fb28793b443b91781936cf55d87a22599e Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Fri, 10 Nov 2023 12:10:30 +0800 Subject: [PATCH 28/32] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E4=BF=AE=E5=A4=8DCI?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DiskFailoverDataSourceTest.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java index ca08140be5a..97f1a7849f1 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/backups/datasource/DiskFailoverDataSourceTest.java @@ -16,38 +16,38 @@ package com.alibaba.nacos.client.naming.backups.datasource; -import com.alibaba.nacos.client.naming.backups.FailoverData; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; +import com.alibaba.nacos.client.naming.utils.CacheDirUtil; import junit.framework.TestCase; import org.junit.Test; import org.mockito.Mockito; import java.util.HashMap; -import java.util.Map; +import java.util.Properties; public class DiskFailoverDataSourceTest extends TestCase { @Test public void testGetSwitch() { + Properties prop = new Properties(); ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder); + final NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive(prop); + String cacheDir = CacheDirUtil.initCacheDir("public", properties); + DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(); diskFailoverDataSource.getSwitch(); } @Test public void testGetFailoverData() { + Properties prop = new Properties(); ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder); + final NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive(prop); + String cacheDir = CacheDirUtil.initCacheDir("public", properties); + DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(); diskFailoverDataSource.getFailoverData(); } - @Test - public void testSaveFailoverData() { - ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); - Mockito.when(holder.getServiceInfoMap()).thenReturn(new HashMap<>()); - DiskFailoverDataSource diskFailoverDataSource = new DiskFailoverDataSource(holder); - Map failoverDataMap = new HashMap(); - } } \ No newline at end of file From e048010001a7c86fd3051df5e6098797793e5b36 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Fri, 10 Nov 2023 14:50:23 +0800 Subject: [PATCH 29/32] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E4=BF=AE=E5=A4=8DCI?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nacos/client/naming/NacosNamingService.java | 5 +++++ .../client/naming/backups/FailoverReactor.java | 13 ++++++++----- 2 files changed, 13 insertions(+), 5 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 d3b7e953471..0c20fedc41e 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 com.alibaba.nacos.client.utils.PreInitUtils; import com.alibaba.nacos.client.utils.ValidatorUtils; import com.alibaba.nacos.common.notify.NotifyCenter; +import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.StringUtils; import java.util.ArrayList; @@ -47,6 +48,8 @@ import java.util.Properties; import java.util.UUID; +import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; + /** * Nacos Naming Service. * @@ -332,6 +335,8 @@ private ServiceInfo getServiceInfo(String serviceName, String groupName, List 0) { + NAMING_LOGGER.debug("getServiceInfo from failover,serviceName: {} data:{}", serviceName, + JacksonUtils.toJson(serviceInfo.getHosts())); return serviceInfo; } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index ea857b066aa..4a1495e8012 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -96,11 +96,14 @@ class FailoverSwitchRefresher implements Runnable { @Override public void run() { FailoverSwitch fSwitch = failoverDataSource.getSwitch(); - boolean switchEnable = false; - if (fSwitch != null && fSwitch.getEnabled()) { - switchEnable = true; + if (fSwitch == null) { + failoverSwitchEnable = false; + return; + } + if (fSwitch.getEnabled() != failoverSwitchEnable) { + NAMING_LOGGER.info("failover switch changed, new: {}", fSwitch.getEnabled()); } - if (switchEnable) { + if (fSwitch.getEnabled() ) { Map failoverMap = new ConcurrentHashMap<>(200); Map failoverData = failoverDataSource.getFailoverData(); for (Map.Entry entry : failoverData.entrySet()) { @@ -127,7 +130,7 @@ public void run() { return; } - if (failoverSwitchEnable && !switchEnable) { + if (failoverSwitchEnable && !fSwitch.getEnabled() ) { Map serviceInfoMap = serviceInfoHolder.getServiceInfoMap(); for (Map.Entry entry : serviceMap.entrySet()) { ServiceInfo oldService = entry.getValue(); From 469a126e2a04daedc9ecf1f276600f262a76373d Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Fri, 10 Nov 2023 16:54:19 +0800 Subject: [PATCH 30/32] =?UTF-8?q?=E7=9B=91=E6=8E=A7=E6=8C=87=E6=A0=87?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=86=85=E9=83=A8=E7=89=88=E6=9C=AC=E5=90=8C?= =?UTF-8?q?=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../naming/backups/FailoverReactor.java | 41 +++++++++++++++---- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 4a1495e8012..84444eb9ee7 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -25,19 +25,19 @@ import com.alibaba.nacos.common.spi.NacosServiceLoader; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.ThreadUtils; +import io.micrometer.core.instrument.Gauge; +import io.micrometer.core.instrument.Meter; import io.micrometer.core.instrument.Metrics; -import io.micrometer.core.instrument.MultiGauge; -import io.micrometer.core.instrument.Tags; import java.util.Calendar; import java.util.Collection; import java.util.Date; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; @@ -60,8 +60,7 @@ public class FailoverReactor implements Closeable { private String notifierEventScope; - private MultiGauge failoverInstanceCounts = MultiGauge.builder("nacos_naming_client_failover_instances") - .description("Nacos failover data service count").register(Metrics.globalRegistry); + private HashMap meterMap = new HashMap<>(10); public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String notifierEventScope) { this.serviceInfoHolder = serviceInfoHolder; @@ -119,10 +118,7 @@ public void run() { } if (failoverMap.size() > 0) { - failoverInstanceCounts.register(failoverMap.keySet().stream() - .map(serviceName -> MultiGauge.Row.of(Tags.of("service_name", serviceName, ""), - ((ServiceInfo) failoverMap.get(serviceName)).ipCount())) - .collect(Collectors.toList()), true); + failoverServiceCntMetrics(failoverMap); serviceMap = failoverMap; } @@ -143,6 +139,7 @@ public void run() { } serviceMap.clear(); failoverSwitchEnable = false; + failoverServiceCntMetricsClear(); return; } } @@ -189,4 +186,30 @@ public void shutdown() throws NacosException { ThreadUtils.shutdownThreadPool(executorService, NAMING_LOGGER); NAMING_LOGGER.info("{} do shutdown stop", className); } + + private void failoverServiceCntMetrics(Map failoverMap) { + try { + for (Map.Entry entry : failoverMap.entrySet()) { + String serviceName = entry.getKey(); + Gauge register = Gauge.builder("nacos_naming_client_failover_instances", + ((ServiceInfo) failoverMap.get(serviceName)).ipCount(), Integer::intValue) + .tag("service_name", "serviceName").description("Nacos failover data service count") + .register(Metrics.globalRegistry); + meterMap.put(serviceName, register); + } + } catch (Exception e) { + NAMING_LOGGER.info("[NA] registerFailoverServiceCnt fail.", e); + } + } + + private void failoverServiceCntMetricsClear() { + try { + for (Map.Entry entry : meterMap.entrySet()) { + Metrics.globalRegistry.remove(entry.getValue()); + } + meterMap.clear(); + } catch (Exception e) { + NAMING_LOGGER.info("[NA] registerFailoverServiceCnt fail.", e); + } + } } \ No newline at end of file From 0ac2cc96f210d9545521ab37d86e4b29f2e0b68f Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Fri, 10 Nov 2023 16:56:45 +0800 Subject: [PATCH 31/32] =?UTF-8?q?=E7=9B=91=E6=8E=A7=E6=8C=87=E6=A0=87?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=86=85=E9=83=A8=E7=89=88=E6=9C=AC=E5=90=8C?= =?UTF-8?q?=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../naming/backups/FailoverReactor.java | 93 ++++++++++--------- 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 84444eb9ee7..2aa4b94431d 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -94,53 +94,62 @@ class FailoverSwitchRefresher implements Runnable { @Override public void run() { - FailoverSwitch fSwitch = failoverDataSource.getSwitch(); - if (fSwitch == null) { - failoverSwitchEnable = false; - return; - } - if (fSwitch.getEnabled() != failoverSwitchEnable) { - NAMING_LOGGER.info("failover switch changed, new: {}", fSwitch.getEnabled()); - } - if (fSwitch.getEnabled() ) { - Map failoverMap = new ConcurrentHashMap<>(200); - Map failoverData = failoverDataSource.getFailoverData(); - for (Map.Entry entry : failoverData.entrySet()) { - ServiceInfo newService = (ServiceInfo) entry.getValue().getData(); - ServiceInfo oldService = serviceMap.get(entry.getKey()); - if (serviceInfoHolder.isChangedServiceInfo(oldService, newService)) { - NAMING_LOGGER.info("[NA] failoverdata isChangedServiceInfo. newService:{}", - JacksonUtils.toJson(newService)); - NotifyCenter.publishEvent(new InstancesChangeEvent(notifierEventScope, newService.getName(), - newService.getGroupName(), newService.getClusters(), newService.getHosts())); - } - failoverMap.put(entry.getKey(), (ServiceInfo) entry.getValue().getData()); + try { + FailoverSwitch fSwitch = failoverDataSource.getSwitch(); + if (fSwitch == null) { + failoverSwitchEnable = false; + return; } - - if (failoverMap.size() > 0) { - failoverServiceCntMetrics(failoverMap); - serviceMap = failoverMap; + if (fSwitch.getEnabled() != failoverSwitchEnable) { + NAMING_LOGGER.info("failover switch changed, new: {}", fSwitch.getEnabled()); + } + if (fSwitch.getEnabled()) { + Map failoverMap = new ConcurrentHashMap<>(200); + Map failoverData = failoverDataSource.getFailoverData(); + for (Map.Entry entry : failoverData.entrySet()) { + ServiceInfo newService = (ServiceInfo) entry.getValue().getData(); + ServiceInfo oldService = serviceMap.get(entry.getKey()); + if (serviceInfoHolder.isChangedServiceInfo(oldService, newService)) { + NAMING_LOGGER.info("[NA] failoverdata isChangedServiceInfo. newService:{}", + JacksonUtils.toJson(newService)); + NotifyCenter.publishEvent(new InstancesChangeEvent(notifierEventScope, newService.getName(), + newService.getGroupName(), newService.getClusters(), newService.getHosts())); + } + failoverMap.put(entry.getKey(), (ServiceInfo) entry.getValue().getData()); + } + + if (failoverMap.size() > 0) { + failoverServiceCntMetrics(failoverMap); + serviceMap = failoverMap; + } + + failoverSwitchEnable = true; + return; } - failoverSwitchEnable = true; - return; - } - - if (failoverSwitchEnable && !fSwitch.getEnabled() ) { - Map serviceInfoMap = serviceInfoHolder.getServiceInfoMap(); - for (Map.Entry entry : serviceMap.entrySet()) { - ServiceInfo oldService = entry.getValue(); - ServiceInfo newService = serviceInfoMap.get(entry.getKey()); - boolean changed = serviceInfoHolder.isChangedServiceInfo(oldService, newService); - if (changed) { - NotifyCenter.publishEvent(new InstancesChangeEvent(notifierEventScope, newService.getName(), - newService.getGroupName(), newService.getClusters(), newService.getHosts())); + if (failoverSwitchEnable && !fSwitch.getEnabled()) { + Map serviceInfoMap = serviceInfoHolder.getServiceInfoMap(); + for (Map.Entry entry : serviceMap.entrySet()) { + ServiceInfo oldService = entry.getValue(); + ServiceInfo newService = serviceInfoMap.get(entry.getKey()); + if (newService != null) { + boolean changed = serviceInfoHolder.isChangedServiceInfo(oldService, newService); + if (changed) { + NotifyCenter.publishEvent( + new InstancesChangeEvent(notifierEventScope, newService.getName(), + newService.getGroupName(), newService.getClusters(), + newService.getHosts())); + } + } } + + serviceMap.clear(); + failoverSwitchEnable = false; + failoverServiceCntMetricsClear(); + return; } - serviceMap.clear(); - failoverSwitchEnable = false; - failoverServiceCntMetricsClear(); - return; + } catch (Exception e) { + NAMING_LOGGER.error("FailoverSwitchRefresher run err", e); } } } From de60b8aa6ce4db88d00ef21357cfb9832ac4d2f1 Mon Sep 17 00:00:00 2001 From: "zongkang.guo1" Date: Sun, 12 Nov 2023 15:39:09 +0800 Subject: [PATCH 32/32] =?UTF-8?q?serviceName=E5=AD=97=E6=AE=B5=E5=8E=BB?= =?UTF-8?q?=E9=99=A4""?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/nacos/client/naming/backups/FailoverReactor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 2aa4b94431d..1819d91a149 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -202,7 +202,7 @@ private void failoverServiceCntMetrics(Map failoverMap) { String serviceName = entry.getKey(); Gauge register = Gauge.builder("nacos_naming_client_failover_instances", ((ServiceInfo) failoverMap.get(serviceName)).ipCount(), Integer::intValue) - .tag("service_name", "serviceName").description("Nacos failover data service count") + .tag("service_name", serviceName).description("Nacos failover data service count") .register(Metrics.globalRegistry); meterMap.put(serviceName, register); }