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 694fbce26..0bb64c4f9 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 @@ -496,10 +496,7 @@ public class ServiceManager implements RecordListener { Service service = getService(namespaceId, serviceName); - if (service == null) { - throw new NacosException(NacosException.INVALID_PARAM, - "service not found, namespace: " + namespaceId + ", service: " + serviceName); - } + checkServiceIsNull(service, namespaceId, serviceName); addInstance(namespaceId, serviceName, instance.isEphemeral(), instance); } @@ -516,10 +513,7 @@ public class ServiceManager implements RecordListener { Service service = getService(namespaceId, serviceName); - if (service == null) { - throw new NacosException(NacosException.INVALID_PARAM, - "service not found, namespace: " + namespaceId + ", service: " + serviceName); - } + checkServiceIsNull(service, namespaceId, serviceName); if (!service.allIPs().contains(instance)) { throw new NacosException(NacosException.INVALID_PARAM, "instance not exist: " + instance); @@ -544,10 +538,7 @@ public class ServiceManager implements RecordListener { Service service = getService(namespaceId, serviceName); - if (service == null) { - throw new NacosException(NacosException.INVALID_PARAM, - "service not found, namespace: " + namespaceId + ", service: " + serviceName); - } + checkServiceIsNull(service, namespaceId, serviceName); List locatedInstance = getLocatedInstance(namespaceId, serviceName, isEphemeral, all, ips); @@ -571,6 +562,21 @@ public class ServiceManager implements RecordListener { return locatedInstance; } + /** + * Check if the service is null. + * + * @param service service + * @param namespaceId namespace + * @param serviceName service name + * @throws NacosException nacos exception + */ + public void checkServiceIsNull(Service service, String namespaceId, String serviceName) throws NacosException { + if (service == null) { + throw new NacosException(NacosException.INVALID_PARAM, + "service not found, namespace: " + namespaceId + ", serviceName: " + serviceName); + } + } + /** * Locate consistency's datum by all or instances provided. * @@ -1002,8 +1008,9 @@ public class ServiceManager implements RecordListener { public void shutdown() throws NacosException { try { long start = System.nanoTime(); - Loggers.SRV_LOG.info("Start to destroy ALL services. namespaces: {}, services: {}", - serviceMap.keySet().size(), getServiceCount()); + Loggers.SRV_LOG + .info("Start to destroy ALL services. namespaces: {}, services: {}", serviceMap.keySet().size(), + getServiceCount()); for (Iterator>> iterator = serviceMap.entrySet().iterator(); iterator.hasNext(); ) { Map.Entry> entry = iterator.next(); @@ -1026,7 +1033,7 @@ public class ServiceManager implements RecordListener { iterator.remove(); } } - + private void cleanupService(String namespace, String name, Service service) throws Exception { service.destroy(); String ephemeralInstanceListKey = KeyBuilder.buildInstanceListKey(namespace, name, true); @@ -1035,14 +1042,14 @@ public class ServiceManager implements RecordListener { consistencyService.remove(ephemeralInstanceListKey); consistencyService.remove(persistInstanceListKey); consistencyService.remove(serviceMetaKey); - + // remove listeners of key to avoid mem leak consistencyService.unListen(ephemeralInstanceListKey, service); consistencyService.unListen(persistInstanceListKey, service); consistencyService.unListen(serviceMetaKey, service); Loggers.SRV_LOG.info("[DEAD-SERVICE] {}", service.toJson()); } - + public static class ServiceChecksum { public String namespaceId; diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceManagerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceManagerTest.java index f481cf04b..10e8330a1 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceManagerTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceManagerTest.java @@ -457,4 +457,12 @@ public class ServiceManagerTest extends BaseTest { assertTrue(actual.contains("\"namespaceId\":\"public\"")); assertTrue(actual.contains("\"serviceName2Checksum\":{\"test\":\"1234567890\"}")); } + + @Test + public void testCheckServiceIsNull() throws NacosException { + serviceManager.createEmptyService(TEST_NAMESPACE, TEST_SERVICE_NAME, true); + String serviceName = "order-service"; + Service service = serviceManager.getService(TEST_NAMESPACE, serviceName); + serviceManager.checkServiceIsNull(service, TEST_NAMESPACE, serviceName); + } }