diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/CatalogServiceV2Impl.java b/naming/src/main/java/com/alibaba/nacos/naming/core/CatalogServiceV2Impl.java index f7455f88f..ab23f391b 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/CatalogServiceV2Impl.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/CatalogServiceV2Impl.java @@ -171,14 +171,14 @@ public class CatalogServiceV2Impl implements CatalogService { private Collection patternServices(String namespaceId, String group, String serviceName) { boolean noFilter = StringUtils.isBlank(serviceName) && StringUtils.isBlank(group); if (noFilter) { - return serviceStorage.getAllServicesOfNamespace(namespaceId); + return ServiceManager.getInstance().getSingletons(namespaceId); } Collection result = new LinkedList<>(); StringJoiner regex = new StringJoiner(Constants.SERVICE_INFO_SPLITER); regex.add(getRegexString(group)); regex.add(getRegexString(serviceName)); String regexString = regex.toString(); - for (Service each : serviceStorage.getAllServicesOfNamespace(namespaceId)) { + for (Service each : ServiceManager.getInstance().getSingletons(namespaceId)) { if (each.getGroupedServiceName().matches(regexString)) { result.add(each); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/ServiceManager.java index 5c62caf26..84ac7355e 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/ServiceManager.java @@ -16,8 +16,12 @@ package com.alibaba.nacos.naming.core.v2; +import com.alibaba.nacos.common.utils.ConcurrentHashSet; import com.alibaba.nacos.naming.core.v2.pojo.Service; +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** @@ -31,23 +35,57 @@ public class ServiceManager { private final ConcurrentHashMap singletonRepository; + private final ConcurrentHashMap> namespaceSingletonMaps; + private ServiceManager() { singletonRepository = new ConcurrentHashMap<>(1 << 10); + namespaceSingletonMaps = new ConcurrentHashMap<>(1 << 2); } public static ServiceManager getInstance() { return INSTANCE; } + public Set getSingletons(String namespace) { + return namespaceSingletonMaps.getOrDefault(namespace, new HashSet<>(1)); + } + /** - * Get singleton service. + * Get singleton service. Put to manager if no singleton. * * @param service new service * @return if service is exist, return exist service, otherwise return new service */ public Service getSingleton(Service service) { - Service previous = singletonRepository.putIfAbsent(service, service); - return (null == previous) ? service : previous; + singletonRepository.putIfAbsent(service, service); + Service result = singletonRepository.get(service); + if (!namespaceSingletonMaps.containsKey(result.getNamespace())) { + namespaceSingletonMaps.putIfAbsent(result.getNamespace(), new ConcurrentHashSet<>()); + namespaceSingletonMaps.get(result.getNamespace()).add(result); + } + return result; + } + + /** + * Get singleton service if Exist. + * + * @param namespace namespace of service + * @param group group of service + * @param name name of service + * @return singleton service if exist, otherwise null optional + */ + public Optional getSingletonIfExist(String namespace, String group, String name) { + return getSingletonIfExist(Service.newService(namespace, group, name)); + } + + /** + * Get singleton service if Exist. + * + * @param service service template + * @return singleton service if exist, otherwise null optional + */ + public Optional getSingletonIfExist(Service service) { + return Optional.ofNullable(singletonRepository.get(service)); } /** diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ServiceStorage.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ServiceStorage.java index 627ca1ad6..41e2a8dad 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ServiceStorage.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ServiceStorage.java @@ -20,7 +20,6 @@ import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.naming.utils.NamingUtils; -import com.alibaba.nacos.common.utils.ConcurrentHashSet; import com.alibaba.nacos.naming.core.v2.ServiceManager; import com.alibaba.nacos.naming.core.v2.client.Client; import com.alibaba.nacos.naming.core.v2.client.manager.ClientManager; @@ -32,7 +31,6 @@ import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.misc.SwitchDomain; import org.springframework.stereotype.Component; -import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; @@ -63,8 +61,6 @@ public class ServiceStorage { private final ConcurrentMap> serviceClusterIndex; - private final ConcurrentMap> namespaceServiceIndex; - public ServiceStorage(ClientServiceIndexesManager serviceIndexesManager, ClientManagerDelegate clientManager, SwitchDomain switchDomain, NamingMetadataManager metadataManager) { this.serviceIndexesManager = serviceIndexesManager; @@ -73,17 +69,12 @@ public class ServiceStorage { this.metadataManager = metadataManager; this.serviceDataIndexes = new ConcurrentHashMap<>(); this.serviceClusterIndex = new ConcurrentHashMap<>(); - this.namespaceServiceIndex = new ConcurrentHashMap<>(); } public Set getClusters(Service service) { return serviceClusterIndex.getOrDefault(service, new HashSet<>()); } - public Collection getAllServicesOfNamespace(String namespace) { - return namespaceServiceIndex.getOrDefault(namespace, new ConcurrentHashSet<>()); - } - public ServiceInfo getData(Service service) { return serviceDataIndexes.containsKey(service) ? serviceDataIndexes.get(service) : getPushData(service); } @@ -95,7 +86,6 @@ public class ServiceStorage { } result.setHosts(getAllInstancesFromIndex(service)); serviceDataIndexes.put(service, result); - updateNamespaceIndex(service); return result; } @@ -158,11 +148,4 @@ public class ServiceStorage { result.setHealthy(instancePublishInfo.isHealthy()); return result; } - - private void updateNamespaceIndex(Service service) { - if (!namespaceServiceIndex.containsKey(service.getNamespace())) { - namespaceServiceIndex.putIfAbsent(service.getNamespace(), new ConcurrentHashSet<>()); - } - namespaceServiceIndex.get(service.getNamespace()).add(service); - } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/remote/handler/ServiceListRequestHandler.java b/naming/src/main/java/com/alibaba/nacos/naming/remote/handler/ServiceListRequestHandler.java index b16d036db..299780b15 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/remote/handler/ServiceListRequestHandler.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/remote/handler/ServiceListRequestHandler.java @@ -21,7 +21,7 @@ import com.alibaba.nacos.api.naming.remote.request.ServiceListRequest; import com.alibaba.nacos.api.naming.remote.response.ServiceListResponse; import com.alibaba.nacos.api.remote.request.RequestMeta; import com.alibaba.nacos.core.remote.RequestHandler; -import com.alibaba.nacos.naming.core.v2.index.ServiceStorage; +import com.alibaba.nacos.naming.core.v2.ServiceManager; import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.utils.ServiceUtil; import org.springframework.stereotype.Component; @@ -40,19 +40,14 @@ import java.util.Objects; @Component public class ServiceListRequestHandler extends RequestHandler { - private final ServiceStorage serviceStorage; - - public ServiceListRequestHandler(ServiceStorage serviceStorage) { - this.serviceStorage = serviceStorage; - } - @Override public ServiceListResponse handle(ServiceListRequest request, RequestMeta meta) throws NacosException { - Collection serviceSet = serviceStorage.getAllServicesOfNamespace(request.getNamespace()); + Collection serviceSet = ServiceManager.getInstance().getSingletons(request.getNamespace()); ServiceListResponse result = ServiceListResponse.buildSuccessResponse(0, new LinkedList<>()); if (!serviceSet.isEmpty()) { Collection serviceNameSet = selectServiceWithGroupName(serviceSet, request.getGroupName()); - List serviceNameList = ServiceUtil.pageServiceName(request.getPageNo(), request.getPageSize(), serviceNameSet); + List serviceNameList = ServiceUtil + .pageServiceName(request.getPageNo(), request.getPageSize(), serviceNameSet); result.setCount(serviceNameList.size()); result.setServiceNames(serviceNameList); }