Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt query service list request with new model #3795

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@
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.client.Client;
import com.alibaba.nacos.naming.core.v2.client.manager.impl.ConnectionBasedClientManager;
import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

Expand All @@ -47,17 +50,17 @@ public class ServiceStorage {

private final ConcurrentMap<Service, ServiceInfo> serviceDataIndexes;

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

public ServiceStorage(ClientServiceIndexesManager serviceIndexesManager, ConnectionBasedClientManager clientManager) {
this.serviceIndexesManager = serviceIndexesManager;
this.clientManager = clientManager;
serviceDataIndexes = new ConcurrentHashMap<>();
namespaceServiceIndex = new ConcurrentHashMap<>();
}

public ServiceInfo getData(Service service) {
if (serviceDataIndexes.containsKey(service)) {
return serviceDataIndexes.get(service);
}
return getPushData(service);
return serviceDataIndexes.containsKey(service) ? serviceDataIndexes.get(service) : getPushData(service);
}

public ServiceInfo getPushData(Service service) {
Expand All @@ -71,9 +74,14 @@ public ServiceInfo getPushData(Service service) {
}
result.setHosts(instances);
serviceDataIndexes.put(service, result);
updateNamespaceIndex(service);
return result;
}

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

private Instance parseInstance(Service service, InstancePublishInfo instancePublishInfo) {
Instance result = new Instance();
result.setIp(instancePublishInfo.getIp());
Expand All @@ -91,4 +99,11 @@ private Instance parseInstance(Service service, InstancePublishInfo instancePubl
result.setEphemeral(service.isEphemeral());
return result;
}

private void updateNamespaceIndex(Service service) {
if (!namespaceServiceIndex.containsKey(service.getNamespace())) {
namespaceServiceIndex.putIfAbsent(service.getNamespace(), new ConcurrentHashSet<>());
}
namespaceServiceIndex.get(service.getNamespace()).add(service);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.alibaba.nacos.naming.core.v2.pojo;

import com.alibaba.nacos.api.naming.utils.NamingUtils;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;

Expand Down Expand Up @@ -80,6 +82,10 @@ public void incrementRevision() {
lastUpdatedTime = System.currentTimeMillis();
}

public String getGroupedServiceName() {
return NamingUtils.getGroupedName(name, group);
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.notify.listener.SmartSubscriber;
import com.alibaba.nacos.common.utils.ConcurrentHashSet;
import com.alibaba.nacos.core.remote.RpcPushService;
import com.alibaba.nacos.naming.core.v2.client.manager.impl.ConnectionBasedClientManager;
import com.alibaba.nacos.naming.core.v2.event.service.ServiceEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
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.Service;
import com.alibaba.nacos.naming.core.ServiceManager;
import com.alibaba.nacos.naming.core.v2.index.ServiceStorage;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.utils.ServiceUtil;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Service list request handler.
Expand All @@ -38,25 +40,33 @@
@Component
public class ServiceListRequestHandler extends RequestHandler<ServiceListRequest, ServiceListResponse> {

private final ServiceManager serviceManager;
private final ServiceStorage serviceStorage;

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

@Override
public ServiceListResponse handle(ServiceListRequest serviceListRequest, RequestMeta meta) throws NacosException {
Map<String, Service> serviceMap = serviceManager.chooseServiceMap(serviceListRequest.getNamespace());
public ServiceListResponse handle(ServiceListRequest request, RequestMeta meta) throws NacosException {
Collection<Service> serviceSet = serviceStorage.getAllServicesOfNamespace(request.getNamespace());
ServiceListResponse result = ServiceListResponse.buildSuccessResponse(0, new LinkedList<>());
if (null != serviceMap && !serviceMap.isEmpty()) {
serviceMap = ServiceUtil.selectServiceWithGroupName(serviceMap, serviceListRequest.getGroupName());
serviceMap = ServiceUtil.selectServiceBySelector(serviceMap, serviceListRequest.getSelector());
List<String> serviceNameList = ServiceUtil
.pageServiceName(serviceListRequest.getPageNo(), serviceListRequest.getPageSize(), serviceMap);
if (!serviceSet.isEmpty()) {
Collection<String> serviceNameSet = selectServiceWithGroupName(serviceSet, request.getGroupName());
List<String> serviceNameList = ServiceUtil.pageServiceName(request.getPageNo(), request.getPageSize(), serviceNameSet);
result.setCount(serviceNameList.size());
result.setServiceNames(serviceNameList);
}
return result;
}

private Collection<String> selectServiceWithGroupName(Collection<Service> serviceSet, String groupName) {
Collection<String> result = new HashSet<>(serviceSet.size());
for (Service each : serviceSet) {
if (Objects.equals(groupName, each.getGroup())) {
result.add(each.getGroupedServiceName());
}
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -131,7 +132,19 @@ private static Map<String, Service> filterServiceMetadata(Map<String, Service> s
* @return service name list by paged
*/
public static List<String> pageServiceName(int pageNo, int pageSize, Map<String, Service> serviceMap) {
List<String> result = new ArrayList<>(serviceMap.keySet());
return pageServiceName(pageNo, pageSize, serviceMap.keySet());
}

/**
* Page service name.
*
* @param pageNo page number
* @param pageSize size per page
* @param serviceNameSet service name set
* @return service name list by paged
*/
public static List<String> pageServiceName(int pageNo, int pageSize, Collection<String> serviceNameSet) {
List<String> result = new ArrayList<>(serviceNameSet);
int start = (pageNo - 1) * pageSize;
if (start < 0) {
start = 0;
Expand Down