Skip to content

Commit

Permalink
Move namespace cache to ServiceManager (#4345)
Browse files Browse the repository at this point in the history
  • Loading branch information
KomachiSion committed Nov 26, 2020
1 parent 18439da commit 8264505
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
Expand Up @@ -171,14 +171,14 @@ public Object pageListServiceDetail(String namespaceId, String groupName, String
private Collection<Service> 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<Service> 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);
}
Expand Down
Expand Up @@ -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;

/**
Expand All @@ -31,23 +35,57 @@ public class ServiceManager {

private final ConcurrentHashMap<Service, Service> singletonRepository;

private final ConcurrentHashMap<String, Set<Service>> namespaceSingletonMaps;

private ServiceManager() {
singletonRepository = new ConcurrentHashMap<>(1 << 10);
namespaceSingletonMaps = new ConcurrentHashMap<>(1 << 2);
}

public static ServiceManager getInstance() {
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
* @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<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));
}

/**
Expand Down
Expand Up @@ -20,7 +20,6 @@
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;
Expand All @@ -32,7 +31,6 @@
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;
Expand Down Expand Up @@ -63,8 +61,6 @@ public class ServiceStorage {

private final ConcurrentMap<Service, Set<String>> serviceClusterIndex;

private final ConcurrentMap<String, Set<Service>> namespaceServiceIndex;

public ServiceStorage(ClientServiceIndexesManager serviceIndexesManager, ClientManagerDelegate clientManager,
SwitchDomain switchDomain, NamingMetadataManager metadataManager) {
this.serviceIndexesManager = serviceIndexesManager;
Expand All @@ -73,17 +69,12 @@ public ServiceStorage(ClientServiceIndexesManager serviceIndexesManager, ClientM
this.metadataManager = metadataManager;
this.serviceDataIndexes = new ConcurrentHashMap<>();
this.serviceClusterIndex = new ConcurrentHashMap<>();
this.namespaceServiceIndex = new ConcurrentHashMap<>();
}

public Set<String> getClusters(Service service) {
return serviceClusterIndex.getOrDefault(service, new HashSet<>());
}

public Collection<Service> getAllServicesOfNamespace(String namespace) {
return namespaceServiceIndex.getOrDefault(namespace, new ConcurrentHashSet<>());
}

public ServiceInfo getData(Service service) {
return serviceDataIndexes.containsKey(service) ? serviceDataIndexes.get(service) : getPushData(service);
}
Expand All @@ -95,7 +86,6 @@ public ServiceInfo getPushData(Service service) {
}
result.setHosts(getAllInstancesFromIndex(service));
serviceDataIndexes.put(service, result);
updateNamespaceIndex(service);
return result;
}

Expand Down Expand Up @@ -158,11 +148,4 @@ private Instance parseInstance(Service service, InstancePublishInfo instancePubl
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);
}
}
Expand Up @@ -21,7 +21,7 @@
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;
Expand All @@ -40,19 +40,14 @@
@Component
public class ServiceListRequestHandler extends RequestHandler<ServiceListRequest, ServiceListResponse> {

private final ServiceStorage serviceStorage;

public ServiceListRequestHandler(ServiceStorage serviceStorage) {
this.serviceStorage = serviceStorage;
}

@Override
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<>());
if (!serviceSet.isEmpty()) {
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.setServiceNames(serviceNameList);
}
Expand Down

0 comments on commit 8264505

Please sign in to comment.