From 8cc92c598dba5eb20f884c1ffc67c80c0d1911bc Mon Sep 17 00:00:00 2001 From: ZhangShenao <15201440436@163.com> Date: Thu, 14 Jul 2022 09:29:40 +0800 Subject: [PATCH] =?UTF-8?q?Use=20Fail-Fast=20in=20Method=20`com.alibaba.na?= =?UTF-8?q?cos.naming.core.ServiceManager=E2=80=A6=20(#8731)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Use Fail-Fast in Method `com.alibaba.nacos.naming.core.ServiceManager#createServiceIfAbsent` * Modify Comments --- .../nacos/naming/core/ServiceManager.java | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java index 6e0fefcfafa..adcb7790793 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java @@ -446,26 +446,28 @@ public void createEmptyService(String namespaceId, String serviceName, boolean l public void createServiceIfAbsent(String namespaceId, String serviceName, boolean local, Cluster cluster) throws NacosException { Service service = getService(namespaceId, serviceName); - if (service == null) { - - Loggers.SRV_LOG.info("creating empty service {}:{}", namespaceId, serviceName); - service = new Service(); - service.setName(serviceName); - service.setNamespaceId(namespaceId); - service.setGroupName(NamingUtils.getGroupName(serviceName)); - // now validate the service. if failed, exception will be thrown - service.setLastModifiedMillis(System.currentTimeMillis()); - service.recalculateChecksum(); - if (cluster != null) { - cluster.setService(service); - service.getClusterMap().put(cluster.getName(), cluster); - } - service.validate(); - - putServiceAndInit(service); - if (!local) { - addOrReplaceService(service); - } + //return if service already exists + if (service != null) { + return; + } + + Loggers.SRV_LOG.info("creating empty service {}:{}", namespaceId, serviceName); + service = new Service(); + service.setName(serviceName); + service.setNamespaceId(namespaceId); + service.setGroupName(NamingUtils.getGroupName(serviceName)); + // now validate the service. if failed, exception will be thrown + service.setLastModifiedMillis(System.currentTimeMillis()); + service.recalculateChecksum(); + if (cluster != null) { + cluster.setService(service); + service.getClusterMap().put(cluster.getName(), cluster); + } + service.validate(); + + putServiceAndInit(service); + if (!local) { + addOrReplaceService(service); } }