Optimize code that checks for empty service. (#5840)

This commit is contained in:
tianqingzhao 2021-05-26 10:07:19 +08:00 committed by GitHub
parent b55b42cc0e
commit a233563e82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 17 deletions

View File

@ -496,10 +496,7 @@ public class ServiceManager implements RecordListener<Service> {
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 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 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<Instance> locatedInstance = getLocatedInstance(namespaceId, serviceName, isEphemeral, all, ips);
@ -571,6 +562,21 @@ public class ServiceManager implements RecordListener<Service> {
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<Service> {
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<Map.Entry<String, Map<String, Service>>> iterator = serviceMap.entrySet().iterator();
iterator.hasNext(); ) {
Map.Entry<String, Map<String, Service>> entry = iterator.next();
@ -1026,7 +1033,7 @@ public class ServiceManager implements RecordListener<Service> {
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<Service> {
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;

View File

@ -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);
}
}