Move namespace cache to ServiceManager (#4345)
This commit is contained in:
parent
18439da98e
commit
826450532e
@ -171,14 +171,14 @@ public class CatalogServiceV2Impl implements CatalogService {
|
|||||||
private Collection<Service> patternServices(String namespaceId, String group, String serviceName) {
|
private Collection<Service> patternServices(String namespaceId, String group, String serviceName) {
|
||||||
boolean noFilter = StringUtils.isBlank(serviceName) && StringUtils.isBlank(group);
|
boolean noFilter = StringUtils.isBlank(serviceName) && StringUtils.isBlank(group);
|
||||||
if (noFilter) {
|
if (noFilter) {
|
||||||
return serviceStorage.getAllServicesOfNamespace(namespaceId);
|
return ServiceManager.getInstance().getSingletons(namespaceId);
|
||||||
}
|
}
|
||||||
Collection<Service> result = new LinkedList<>();
|
Collection<Service> result = new LinkedList<>();
|
||||||
StringJoiner regex = new StringJoiner(Constants.SERVICE_INFO_SPLITER);
|
StringJoiner regex = new StringJoiner(Constants.SERVICE_INFO_SPLITER);
|
||||||
regex.add(getRegexString(group));
|
regex.add(getRegexString(group));
|
||||||
regex.add(getRegexString(serviceName));
|
regex.add(getRegexString(serviceName));
|
||||||
String regexString = regex.toString();
|
String regexString = regex.toString();
|
||||||
for (Service each : serviceStorage.getAllServicesOfNamespace(namespaceId)) {
|
for (Service each : ServiceManager.getInstance().getSingletons(namespaceId)) {
|
||||||
if (each.getGroupedServiceName().matches(regexString)) {
|
if (each.getGroupedServiceName().matches(regexString)) {
|
||||||
result.add(each);
|
result.add(each);
|
||||||
}
|
}
|
||||||
|
@ -16,8 +16,12 @@
|
|||||||
|
|
||||||
package com.alibaba.nacos.naming.core.v2;
|
package com.alibaba.nacos.naming.core.v2;
|
||||||
|
|
||||||
|
import com.alibaba.nacos.common.utils.ConcurrentHashSet;
|
||||||
import com.alibaba.nacos.naming.core.v2.pojo.Service;
|
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;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,23 +35,57 @@ public class ServiceManager {
|
|||||||
|
|
||||||
private final ConcurrentHashMap<Service, Service> singletonRepository;
|
private final ConcurrentHashMap<Service, Service> singletonRepository;
|
||||||
|
|
||||||
|
private final ConcurrentHashMap<String, Set<Service>> namespaceSingletonMaps;
|
||||||
|
|
||||||
private ServiceManager() {
|
private ServiceManager() {
|
||||||
singletonRepository = new ConcurrentHashMap<>(1 << 10);
|
singletonRepository = new ConcurrentHashMap<>(1 << 10);
|
||||||
|
namespaceSingletonMaps = new ConcurrentHashMap<>(1 << 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ServiceManager getInstance() {
|
public static ServiceManager getInstance() {
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<Service> 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
|
* @param service new service
|
||||||
* @return if service is exist, return exist service, otherwise return new service
|
* @return if service is exist, return exist service, otherwise return new service
|
||||||
*/
|
*/
|
||||||
public Service getSingleton(Service service) {
|
public Service getSingleton(Service service) {
|
||||||
Service previous = singletonRepository.putIfAbsent(service, service);
|
singletonRepository.putIfAbsent(service, service);
|
||||||
return (null == previous) ? service : previous;
|
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<Service> 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<Service> getSingletonIfExist(Service service) {
|
||||||
|
return Optional.ofNullable(singletonRepository.get(service));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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.Instance;
|
||||||
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
|
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
|
||||||
import com.alibaba.nacos.api.naming.utils.NamingUtils;
|
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.ServiceManager;
|
||||||
import com.alibaba.nacos.naming.core.v2.client.Client;
|
import com.alibaba.nacos.naming.core.v2.client.Client;
|
||||||
import com.alibaba.nacos.naming.core.v2.client.manager.ClientManager;
|
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 com.alibaba.nacos.naming.misc.SwitchDomain;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
@ -63,8 +61,6 @@ public class ServiceStorage {
|
|||||||
|
|
||||||
private final ConcurrentMap<Service, Set<String>> serviceClusterIndex;
|
private final ConcurrentMap<Service, Set<String>> serviceClusterIndex;
|
||||||
|
|
||||||
private final ConcurrentMap<String, Set<Service>> namespaceServiceIndex;
|
|
||||||
|
|
||||||
public ServiceStorage(ClientServiceIndexesManager serviceIndexesManager, ClientManagerDelegate clientManager,
|
public ServiceStorage(ClientServiceIndexesManager serviceIndexesManager, ClientManagerDelegate clientManager,
|
||||||
SwitchDomain switchDomain, NamingMetadataManager metadataManager) {
|
SwitchDomain switchDomain, NamingMetadataManager metadataManager) {
|
||||||
this.serviceIndexesManager = serviceIndexesManager;
|
this.serviceIndexesManager = serviceIndexesManager;
|
||||||
@ -73,17 +69,12 @@ public class ServiceStorage {
|
|||||||
this.metadataManager = metadataManager;
|
this.metadataManager = metadataManager;
|
||||||
this.serviceDataIndexes = new ConcurrentHashMap<>();
|
this.serviceDataIndexes = new ConcurrentHashMap<>();
|
||||||
this.serviceClusterIndex = new ConcurrentHashMap<>();
|
this.serviceClusterIndex = new ConcurrentHashMap<>();
|
||||||
this.namespaceServiceIndex = new ConcurrentHashMap<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> getClusters(Service service) {
|
public Set<String> getClusters(Service service) {
|
||||||
return serviceClusterIndex.getOrDefault(service, new HashSet<>());
|
return serviceClusterIndex.getOrDefault(service, new HashSet<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<Service> getAllServicesOfNamespace(String namespace) {
|
|
||||||
return namespaceServiceIndex.getOrDefault(namespace, new ConcurrentHashSet<>());
|
|
||||||
}
|
|
||||||
|
|
||||||
public ServiceInfo getData(Service service) {
|
public ServiceInfo getData(Service service) {
|
||||||
return serviceDataIndexes.containsKey(service) ? serviceDataIndexes.get(service) : getPushData(service);
|
return serviceDataIndexes.containsKey(service) ? serviceDataIndexes.get(service) : getPushData(service);
|
||||||
}
|
}
|
||||||
@ -95,7 +86,6 @@ public class ServiceStorage {
|
|||||||
}
|
}
|
||||||
result.setHosts(getAllInstancesFromIndex(service));
|
result.setHosts(getAllInstancesFromIndex(service));
|
||||||
serviceDataIndexes.put(service, result);
|
serviceDataIndexes.put(service, result);
|
||||||
updateNamespaceIndex(service);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,11 +148,4 @@ public class ServiceStorage {
|
|||||||
result.setHealthy(instancePublishInfo.isHealthy());
|
result.setHealthy(instancePublishInfo.isHealthy());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateNamespaceIndex(Service service) {
|
|
||||||
if (!namespaceServiceIndex.containsKey(service.getNamespace())) {
|
|
||||||
namespaceServiceIndex.putIfAbsent(service.getNamespace(), new ConcurrentHashSet<>());
|
|
||||||
}
|
|
||||||
namespaceServiceIndex.get(service.getNamespace()).add(service);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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.naming.remote.response.ServiceListResponse;
|
||||||
import com.alibaba.nacos.api.remote.request.RequestMeta;
|
import com.alibaba.nacos.api.remote.request.RequestMeta;
|
||||||
import com.alibaba.nacos.core.remote.RequestHandler;
|
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.core.v2.pojo.Service;
|
||||||
import com.alibaba.nacos.naming.utils.ServiceUtil;
|
import com.alibaba.nacos.naming.utils.ServiceUtil;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@ -40,19 +40,14 @@ import java.util.Objects;
|
|||||||
@Component
|
@Component
|
||||||
public class ServiceListRequestHandler extends RequestHandler<ServiceListRequest, ServiceListResponse> {
|
public class ServiceListRequestHandler extends RequestHandler<ServiceListRequest, ServiceListResponse> {
|
||||||
|
|
||||||
private final ServiceStorage serviceStorage;
|
|
||||||
|
|
||||||
public ServiceListRequestHandler(ServiceStorage serviceStorage) {
|
|
||||||
this.serviceStorage = serviceStorage;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServiceListResponse handle(ServiceListRequest request, RequestMeta meta) throws NacosException {
|
public ServiceListResponse handle(ServiceListRequest request, RequestMeta meta) throws NacosException {
|
||||||
Collection<Service> serviceSet = serviceStorage.getAllServicesOfNamespace(request.getNamespace());
|
Collection<Service> serviceSet = ServiceManager.getInstance().getSingletons(request.getNamespace());
|
||||||
ServiceListResponse result = ServiceListResponse.buildSuccessResponse(0, new LinkedList<>());
|
ServiceListResponse result = ServiceListResponse.buildSuccessResponse(0, new LinkedList<>());
|
||||||
if (!serviceSet.isEmpty()) {
|
if (!serviceSet.isEmpty()) {
|
||||||
Collection<String> serviceNameSet = selectServiceWithGroupName(serviceSet, request.getGroupName());
|
Collection<String> serviceNameSet = selectServiceWithGroupName(serviceSet, request.getGroupName());
|
||||||
List<String> serviceNameList = ServiceUtil.pageServiceName(request.getPageNo(), request.getPageSize(), serviceNameSet);
|
List<String> serviceNameList = ServiceUtil
|
||||||
|
.pageServiceName(request.getPageNo(), request.getPageSize(), serviceNameSet);
|
||||||
result.setCount(serviceNameList.size());
|
result.setCount(serviceNameList.size());
|
||||||
result.setServiceNames(serviceNameList);
|
result.setServiceNames(serviceNameList);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user