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 all openAPI in for InstanceController. #4441

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 @@ -26,16 +26,17 @@
import com.alibaba.nacos.core.utils.WebUtils;
import com.alibaba.nacos.naming.core.Instance;
import com.alibaba.nacos.naming.core.InstanceOperatorClientImpl;
import com.alibaba.nacos.naming.core.InstancePatchObject;
import com.alibaba.nacos.naming.core.Service;
import com.alibaba.nacos.naming.core.ServiceManager;
import com.alibaba.nacos.naming.healthcheck.RsInfo;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.misc.SwitchEntry;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.naming.pojo.InstanceOperationContext;
import com.alibaba.nacos.naming.pojo.InstanceOperationInfo;
import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.naming.push.ClientInfo;
import com.alibaba.nacos.naming.push.DataSource;
import com.alibaba.nacos.naming.push.PushService;
Expand Down Expand Up @@ -173,8 +174,7 @@ public String deregister(HttpServletRequest request) throws Exception {
@PutMapping
@Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE)
public String update(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils
.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
NamingUtils.checkServiceNameFormat(serviceName);
instanceService.updateInstance(namespaceId, serviceName, parseInstance(request));
Expand Down Expand Up @@ -311,7 +311,6 @@ private List<Instance> batchOperateMetadata(String namespace, InstanceOperationI
@PatchMapping
@Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE)
public String patch(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
NamingUtils.checkServiceNameFormat(serviceName);
String ip = WebUtils.required(request, "ip");
Expand All @@ -320,35 +319,29 @@ public String patch(HttpServletRequest request) throws Exception {
if (StringUtils.isBlank(cluster)) {
cluster = WebUtils.optional(request, "cluster", UtilsAndCommons.DEFAULT_CLUSTER_NAME);
}

Instance instance = serviceManager.getInstance(namespaceId, serviceName, cluster, ip, Integer.parseInt(port));
if (instance == null) {
throw new IllegalArgumentException("instance not found");
}

InstancePatchObject patchObject = new InstancePatchObject(cluster, ip, Integer.parseInt(port));
String metadata = WebUtils.optional(request, "metadata", StringUtils.EMPTY);
if (StringUtils.isNotBlank(metadata)) {
instance.setMetadata(UtilsAndCommons.parseMetadata(metadata));
patchObject.setMetadata(UtilsAndCommons.parseMetadata(metadata));
}
String app = WebUtils.optional(request, "app", StringUtils.EMPTY);
if (StringUtils.isNotBlank(app)) {
instance.setApp(app);
patchObject.setApp(app);
}
String weight = WebUtils.optional(request, "weight", StringUtils.EMPTY);
if (StringUtils.isNotBlank(weight)) {
instance.setWeight(Double.parseDouble(weight));
patchObject.setWeight(Double.parseDouble(weight));
}
String healthy = WebUtils.optional(request, "healthy", StringUtils.EMPTY);
if (StringUtils.isNotBlank(healthy)) {
instance.setHealthy(BooleanUtils.toBoolean(healthy));
patchObject.setHealthy(BooleanUtils.toBoolean(healthy));
}
String enabledString = WebUtils.optional(request, "enabled", StringUtils.EMPTY);
if (StringUtils.isNotBlank(enabledString)) {
instance.setEnabled(BooleanUtils.toBoolean(enabledString));
patchObject.setEnabled(BooleanUtils.toBoolean(enabledString));
}
instance.setLastBeat(System.currentTimeMillis());
instance.validate();
serviceManager.updateInstance(namespaceId, serviceName, instance);
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
instanceService.patchInstance(namespaceId, serviceName, patchObject);
return "ok";
}

Expand Down Expand Up @@ -403,36 +396,18 @@ public ObjectNode detail(HttpServletRequest request) throws Exception {
String ip = WebUtils.required(request, "ip");
int port = Integer.parseInt(WebUtils.required(request, "port"));

Service service = serviceManager.getService(namespaceId, serviceName);
if (service == null) {
throw new NacosException(NacosException.NOT_FOUND, "no service " + serviceName + " found!");
}

List<String> clusters = new ArrayList<>();
clusters.add(cluster);

List<Instance> ips = service.allIPs(clusters);
if (ips == null || ips.isEmpty()) {
throw new NacosException(NacosException.NOT_FOUND,
"no ips found for cluster " + cluster + " in service " + serviceName);
}

for (Instance instance : ips) {
if (instance.getIp().equals(ip) && instance.getPort() == port) {
ObjectNode result = JacksonUtils.createEmptyJsonNode();
result.put("service", serviceName);
result.put("ip", ip);
result.put("port", port);
result.put("clusterName", cluster);
result.put("weight", instance.getWeight());
result.put("healthy", instance.isHealthy());
result.put("instanceId", instance.getInstanceId());
result.set("metadata", JacksonUtils.transferToJsonNode(instance.getMetadata()));
return result;
}
}

throw new NacosException(NacosException.NOT_FOUND, "no matched ip found!");
com.alibaba.nacos.api.naming.pojo.Instance instance = instanceService
.getInstance(namespaceId, serviceName, cluster, ip, port);
ObjectNode result = JacksonUtils.createEmptyJsonNode();
result.put("service", serviceName);
result.put("ip", ip);
result.put("port", port);
result.put("clusterName", cluster);
result.put("weight", instance.getWeight());
result.put("healthy", instance.isHealthy());
result.put("instanceId", instance.getInstanceId());
result.set("metadata", JacksonUtils.transferToJsonNode(instance.getMetadata()));
return result;
}

/**
Expand Down Expand Up @@ -503,21 +478,15 @@ public ObjectNode listWithHealthStatus(@RequestParam String key) throws NacosExc
serviceName = key;
}
NamingUtils.checkServiceNameFormat(serviceName);
Service service = serviceManager.getService(namespaceId, serviceName);

if (service == null) {
throw new NacosException(NacosException.NOT_FOUND, "service: " + serviceName + " not found.");
}

List<Instance> ips = service.allIPs();
List<? extends com.alibaba.nacos.api.naming.pojo.Instance> ips = instanceService
.listAllInstances(namespaceId, serviceName);

ObjectNode result = JacksonUtils.createEmptyJsonNode();
ArrayNode ipArray = JacksonUtils.createEmptyArrayNode();

for (Instance ip : ips) {
ipArray.add(ip.toIpAddr() + "_" + ip.isHealthy());
for (com.alibaba.nacos.api.naming.pojo.Instance ip : ips) {
ipArray.add(ip.toInetAddr() + "_" + ip.isHealthy());
}

result.replace("ips", ipArray);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.alibaba.nacos.naming.healthcheck.RsInfo;
import com.alibaba.nacos.naming.pojo.Subscriber;

import java.util.List;

/**
* Instance operator.
*
Expand Down Expand Up @@ -53,13 +55,28 @@ public interface InstanceOperator {
* Update instance information. Due to the basic information can't be changed, so this update should only update
* metadata.
*
* <p>Update API will replace the whole metadata with new input instance.
*
* @param namespaceId namespace
* @param serviceName grouped service name group@@service
* @param instance instance
* @throws NacosException nacos exception when update failed
*/
void updateInstance(String namespaceId, String serviceName, Instance instance) throws NacosException;

/**
* Patch update instance information. Due to the basic information can't be changed, so this update should only
* update metadata.
*
* <p>Patch update will only update variables in requests, the others will keep original value.
*
* @param namespaceId namespace
* @param serviceName grouped service name group@@service
* @param patchObject objects need to be patch
* @throws NacosException nacos exception when update failed
*/
void patchInstance(String namespaceId, String serviceName, InstancePatchObject patchObject) throws NacosException;

/**
* Get all instance of input service.
*
Expand All @@ -74,6 +91,20 @@ public interface InstanceOperator {
ServiceInfo listInstance(String namespaceId, String serviceName, Subscriber subscriber, String cluster,
boolean healthOnly) throws Exception;

/**
* Get instance detail information.
*
* @param namespaceId namespace
* @param serviceName grouped service name group@@service
* @param cluster cluster of instance
* @param ip ip of instance
* @param port port of instance
* @return instance info
* @throws NacosException nacos exception during query
*/
Instance getInstance(String namespaceId, String serviceName, String cluster, String ip, int port)
throws NacosException;

/**
* Handle beat request.
*
Expand All @@ -100,4 +131,14 @@ int handleBeat(String namespaceId, String serviceName, String ip, int port, Stri
* @return heart beat interval
*/
long getHeartBeatInterval(String namespaceId, String serviceName, String ip, int port, String cluster);

/**
* List all instances whatever status they are.
*
* @param namespaceId namespace
* @param serviceName grouped service name group@@service
* @return all instances
* @throws NacosException nacos exception during query
*/
List<? extends Instance> listAllInstances(String namespaceId, String serviceName) throws NacosException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.naming.utils.ServiceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
Expand All @@ -66,7 +68,8 @@ public class InstanceOperatorClientImpl implements InstanceOperator {

public InstanceOperatorClientImpl(ClientManagerDelegate clientManager,
ClientOperationService clientOperationService, ServiceStorage serviceStorage,
NamingMetadataOperateService metadataOperateService, NamingMetadataManager metadataManager, SwitchDomain switchDomain) {
NamingMetadataOperateService metadataOperateService, NamingMetadataManager metadataManager,
SwitchDomain switchDomain) {
this.clientManager = clientManager;
this.clientOperationService = clientOperationService;
this.serviceStorage = serviceStorage;
Expand All @@ -82,9 +85,7 @@ public InstanceOperatorClientImpl(ClientManagerDelegate clientManager,
public void registerInstance(String namespaceId, String serviceName, Instance instance) {
String clientId = instance.toInetAddr();
createIpPortClientIfAbsent(clientId, instance.isEphemeral());
String groupName = NamingUtils.getGroupName(serviceName);
String serviceNameNoGrouped = NamingUtils.getServiceName(serviceName);
Service service = Service.newService(namespaceId, groupName, serviceNameNoGrouped, instance.isEphemeral());
Service service = getService(namespaceId, serviceName, instance.isEphemeral());
clientOperationService.registerInstance(service, instance, clientId);
}

Expand All @@ -95,17 +96,13 @@ public void removeInstance(String namespaceId, String serviceName, Instance inst
Loggers.SRV_LOG.warn("remove instance from non-exist client: {}", clientId);
return;
}
String groupName = NamingUtils.getGroupName(serviceName);
String serviceNameNoGrouped = NamingUtils.getServiceName(serviceName);
Service service = Service.newService(namespaceId, groupName, serviceNameNoGrouped, instance.isEphemeral());
Service service = getService(namespaceId, serviceName, instance.isEphemeral());
clientOperationService.deregisterInstance(service, instance, clientId);
}

@Override
public void updateInstance(String namespaceId, String serviceName, Instance instance) throws NacosException {
String groupName = NamingUtils.getGroupName(serviceName);
String serviceNameNoGrouped = NamingUtils.getServiceName(serviceName);
Service service = Service.newService(namespaceId, groupName, serviceNameNoGrouped, instance.isEphemeral());
Service service = getService(namespaceId, serviceName, instance.isEphemeral());
if (!ServiceManager.getInstance().containSingleton(service)) {
throw new NacosException(NacosException.INVALID_PARAM,
"service not found, namespace: " + namespaceId + ", service: " + service);
Expand All @@ -121,12 +118,43 @@ private InstanceMetadata buildMetadata(Instance instance) {
return result;
}

@Override
public void patchInstance(String namespaceId, String serviceName, InstancePatchObject patchObject)
throws NacosException {
Service service = getService(namespaceId, serviceName, true);
Instance instance = getInstance(namespaceId, serviceName, patchObject.getCluster(), patchObject.getIp(),
patchObject.getPort());
String instanceId = instance.toInetAddr();
Optional<InstanceMetadata> instanceMetadata = metadataManager.getInstanceMetadata(service, instanceId);
InstanceMetadata newMetadata = instanceMetadata.map(this::cloneMetadata).orElseGet(InstanceMetadata::new);
mergeMetadata(newMetadata, patchObject);
metadataOperateService.updateInstanceMetadata(service, instanceId, newMetadata);
}

private InstanceMetadata cloneMetadata(InstanceMetadata instanceMetadata) {
InstanceMetadata result = new InstanceMetadata();
result.setExtendData(new HashMap<>(instanceMetadata.getExtendData()));
result.setWeight(instanceMetadata.getWeight());
result.setEnabled(instanceMetadata.isEnabled());
return result;
}

private void mergeMetadata(InstanceMetadata newMetadata, InstancePatchObject patchObject) {
if (null != patchObject.getMetadata()) {
newMetadata.setExtendData(new HashMap<>(patchObject.getMetadata()));
}
if (null != patchObject.getEnabled()) {
newMetadata.setEnabled(patchObject.getEnabled());
}
if (null != patchObject.getWeight()) {
newMetadata.setWeight(patchObject.getWeight());
}
}

@Override
public ServiceInfo listInstance(String namespaceId, String serviceName, Subscriber subscriber, String cluster,
boolean healthOnly) {
String groupName = NamingUtils.getGroupName(serviceName);
String serviceNameNoGrouped = NamingUtils.getServiceName(serviceName);
Service service = Service.newService(namespaceId, groupName, serviceNameNoGrouped);
Service service = getService(namespaceId, serviceName, true);
if (null != subscriber) {
createIpPortClientIfAbsent(subscriber.getAddrStr(), true);
clientOperationService.subscribeService(service, subscriber, subscriber.getAddrStr());
Expand All @@ -138,13 +166,28 @@ public ServiceInfo listInstance(String namespaceId, String serviceName, Subscrib
return result;
}

@Override
public Instance getInstance(String namespaceId, String serviceName, String cluster, String ip, int port)
throws NacosException {
Service service = getService(namespaceId, serviceName, true);
ServiceInfo serviceInfo = serviceStorage.getData(service);
if (serviceInfo.getHosts().isEmpty()) {
throw new NacosException(NacosException.NOT_FOUND,
"no ips found for cluster " + cluster + " in service " + serviceName);
}
for (Instance each : serviceInfo.getHosts()) {
if (cluster.equals(each.getClusterName()) && ip.equals(each.getIp()) && port == each.getPort()) {
return each;
}
}
throw new NacosException(NacosException.NOT_FOUND, "no matched ip found!");
}

@Override
public int handleBeat(String namespaceId, String serviceName, String ip, int port, String cluster,
RsInfo clientBeat) throws NacosException {
String groupName = NamingUtils.getGroupName(serviceName);
String serviceNameNoGrouped = NamingUtils.getServiceName(serviceName);
Service service = getService(namespaceId, serviceName, true);
String clientId = ip + ":" + port;
Service service = Service.newService(namespaceId, groupName, serviceNameNoGrouped);
IpPortBasedClient client = (IpPortBasedClient) clientManager.getClient(clientId);
if (null == client || !client.getAllPublishedService().contains(service)) {
if (null == clientBeat) {
Expand Down Expand Up @@ -181,11 +224,10 @@ public int handleBeat(String namespaceId, String serviceName, String ip, int por

@Override
public long getHeartBeatInterval(String namespaceId, String serviceName, String ip, int port, String cluster) {
String groupName = NamingUtils.getGroupName(serviceName);
String serviceNameNoGrouped = NamingUtils.getServiceName(serviceName);
Service service = Service.newService(namespaceId, groupName, serviceNameNoGrouped);
Service service = getService(namespaceId, serviceName, true);
Optional<InstanceMetadata> metadata = metadataManager.getInstanceMetadata(service, ip);
if (metadata.isPresent() && metadata.get().getExtendData().containsKey(PreservedMetadataKeys.HEART_BEAT_INTERVAL)) {
if (metadata.isPresent() && metadata.get().getExtendData()
.containsKey(PreservedMetadataKeys.HEART_BEAT_INTERVAL)) {
return ConvertUtils.toLong(metadata.get().getExtendData().get(PreservedMetadataKeys.HEART_BEAT_INTERVAL));
}
String clientId = ip + ":" + port;
Expand All @@ -197,9 +239,21 @@ public long getHeartBeatInterval(String namespaceId, String serviceName, String
return switchDomain.getClientBeatInterval();
}

@Override
public List<? extends Instance> listAllInstances(String namespaceId, String serviceName) throws NacosException {
Service service = getService(namespaceId, serviceName, true);
return serviceStorage.getData(service).getHosts();
}

private void createIpPortClientIfAbsent(String clientId, boolean ephemeral) {
if (!clientManager.allClientId().contains(clientId)) {
clientManager.clientConnected(new IpPortBasedClient(clientId, ephemeral));
}
}

private Service getService(String namespaceId, String serviceName, boolean ephemeral) {
String groupName = NamingUtils.getGroupName(serviceName);
String serviceNameNoGrouped = NamingUtils.getServiceName(serviceName);
return Service.newService(namespaceId, groupName, serviceNameNoGrouped, ephemeral);
}
}
Loading