Optimize code that checks for empty service. (#5840)
This commit is contained in:
parent
b55b42cc0e
commit
a233563e82
@ -496,10 +496,7 @@ public class ServiceManager implements RecordListener<Service> {
|
|||||||
|
|
||||||
Service service = getService(namespaceId, serviceName);
|
Service service = getService(namespaceId, serviceName);
|
||||||
|
|
||||||
if (service == null) {
|
checkServiceIsNull(service, namespaceId, serviceName);
|
||||||
throw new NacosException(NacosException.INVALID_PARAM,
|
|
||||||
"service not found, namespace: " + namespaceId + ", service: " + serviceName);
|
|
||||||
}
|
|
||||||
|
|
||||||
addInstance(namespaceId, serviceName, instance.isEphemeral(), instance);
|
addInstance(namespaceId, serviceName, instance.isEphemeral(), instance);
|
||||||
}
|
}
|
||||||
@ -516,10 +513,7 @@ public class ServiceManager implements RecordListener<Service> {
|
|||||||
|
|
||||||
Service service = getService(namespaceId, serviceName);
|
Service service = getService(namespaceId, serviceName);
|
||||||
|
|
||||||
if (service == null) {
|
checkServiceIsNull(service, namespaceId, serviceName);
|
||||||
throw new NacosException(NacosException.INVALID_PARAM,
|
|
||||||
"service not found, namespace: " + namespaceId + ", service: " + serviceName);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!service.allIPs().contains(instance)) {
|
if (!service.allIPs().contains(instance)) {
|
||||||
throw new NacosException(NacosException.INVALID_PARAM, "instance not exist: " + 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);
|
Service service = getService(namespaceId, serviceName);
|
||||||
|
|
||||||
if (service == null) {
|
checkServiceIsNull(service, namespaceId, serviceName);
|
||||||
throw new NacosException(NacosException.INVALID_PARAM,
|
|
||||||
"service not found, namespace: " + namespaceId + ", service: " + serviceName);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Instance> locatedInstance = getLocatedInstance(namespaceId, serviceName, isEphemeral, all, ips);
|
List<Instance> locatedInstance = getLocatedInstance(namespaceId, serviceName, isEphemeral, all, ips);
|
||||||
|
|
||||||
@ -571,6 +562,21 @@ public class ServiceManager implements RecordListener<Service> {
|
|||||||
return locatedInstance;
|
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.
|
* Locate consistency's datum by all or instances provided.
|
||||||
*
|
*
|
||||||
@ -1002,8 +1008,9 @@ public class ServiceManager implements RecordListener<Service> {
|
|||||||
public void shutdown() throws NacosException {
|
public void shutdown() throws NacosException {
|
||||||
try {
|
try {
|
||||||
long start = System.nanoTime();
|
long start = System.nanoTime();
|
||||||
Loggers.SRV_LOG.info("Start to destroy ALL services. namespaces: {}, services: {}",
|
Loggers.SRV_LOG
|
||||||
serviceMap.keySet().size(), getServiceCount());
|
.info("Start to destroy ALL services. namespaces: {}, services: {}", serviceMap.keySet().size(),
|
||||||
|
getServiceCount());
|
||||||
for (Iterator<Map.Entry<String, Map<String, Service>>> iterator = serviceMap.entrySet().iterator();
|
for (Iterator<Map.Entry<String, Map<String, Service>>> iterator = serviceMap.entrySet().iterator();
|
||||||
iterator.hasNext(); ) {
|
iterator.hasNext(); ) {
|
||||||
Map.Entry<String, Map<String, Service>> entry = iterator.next();
|
Map.Entry<String, Map<String, Service>> entry = iterator.next();
|
||||||
@ -1026,7 +1033,7 @@ public class ServiceManager implements RecordListener<Service> {
|
|||||||
iterator.remove();
|
iterator.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cleanupService(String namespace, String name, Service service) throws Exception {
|
private void cleanupService(String namespace, String name, Service service) throws Exception {
|
||||||
service.destroy();
|
service.destroy();
|
||||||
String ephemeralInstanceListKey = KeyBuilder.buildInstanceListKey(namespace, name, true);
|
String ephemeralInstanceListKey = KeyBuilder.buildInstanceListKey(namespace, name, true);
|
||||||
@ -1035,14 +1042,14 @@ public class ServiceManager implements RecordListener<Service> {
|
|||||||
consistencyService.remove(ephemeralInstanceListKey);
|
consistencyService.remove(ephemeralInstanceListKey);
|
||||||
consistencyService.remove(persistInstanceListKey);
|
consistencyService.remove(persistInstanceListKey);
|
||||||
consistencyService.remove(serviceMetaKey);
|
consistencyService.remove(serviceMetaKey);
|
||||||
|
|
||||||
// remove listeners of key to avoid mem leak
|
// remove listeners of key to avoid mem leak
|
||||||
consistencyService.unListen(ephemeralInstanceListKey, service);
|
consistencyService.unListen(ephemeralInstanceListKey, service);
|
||||||
consistencyService.unListen(persistInstanceListKey, service);
|
consistencyService.unListen(persistInstanceListKey, service);
|
||||||
consistencyService.unListen(serviceMetaKey, service);
|
consistencyService.unListen(serviceMetaKey, service);
|
||||||
Loggers.SRV_LOG.info("[DEAD-SERVICE] {}", service.toJson());
|
Loggers.SRV_LOG.info("[DEAD-SERVICE] {}", service.toJson());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ServiceChecksum {
|
public static class ServiceChecksum {
|
||||||
|
|
||||||
public String namespaceId;
|
public String namespaceId;
|
||||||
|
@ -457,4 +457,12 @@ public class ServiceManagerTest extends BaseTest {
|
|||||||
assertTrue(actual.contains("\"namespaceId\":\"public\""));
|
assertTrue(actual.contains("\"namespaceId\":\"public\""));
|
||||||
assertTrue(actual.contains("\"serviceName2Checksum\":{\"test\":\"1234567890\"}"));
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user