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

[ISSUE #3904] feature - operate instance's metadata alonely #3912

Merged
merged 22 commits into from
Oct 19, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
move metadata operate to InstanceController.
horizonzy committed Sep 26, 2020
commit 52c8c91da5864387ac7df2b3e591ecd0195574cc
Original file line number Diff line number Diff line change
@@ -64,6 +64,9 @@
import java.util.List;
import java.util.Map;

import static com.alibaba.nacos.naming.misc.UtilsAndCommons.UPDATE_INSTANCE_METADATA_ACTION_REMOVE;
import static com.alibaba.nacos.naming.misc.UtilsAndCommons.UPDATE_INSTANCE_METADATA_ACTION_UPDATE;

/**
* Instance operation controller.
*
@@ -182,6 +185,78 @@ public String update(HttpServletRequest request) throws Exception {
return "ok";
}

/**
* Update instance's metadata. old key exist = update, old key not exist = add.
*
* @param request http request
* @return 'ok' if success
* @throws Exception any error during update
horizonzy marked this conversation as resolved.
Show resolved Hide resolved
*/
@CanDistro
@PutMapping(value = "/metadata")
@Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE)
public String updateInstanceMatadata(HttpServletRequest request) throws Exception {
final String namespaceId = WebUtils
.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
horizonzy marked this conversation as resolved.
Show resolved Hide resolved
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
NamingUtils.checkServiceNameFormat(serviceName);

Instance instance = parseMetaInstance(request);

serviceManager.updateMetadata(namespaceId, serviceName, instance, UPDATE_INSTANCE_METADATA_ACTION_UPDATE);
return "ok";
}

/**
* Batch update instance's metadata. old key exist = update, old key not exist = add.
*
* @param request http request
* @return 'ok' if success
* @throws Exception any error during update
horizonzy marked this conversation as resolved.
Show resolved Hide resolved
*/
@CanDistro
@PutMapping(value = "/metadata/batch")
@Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE)
public String batchUpdateInstanceMatadata(HttpServletRequest request) throws Exception {
return "ok";
}

/**
* Delete instance's metadata. old key exist = delete, old key not exist = not operate
*
* @param request http request
* @return 'ok' if success
* @throws Exception any error during update
*/
@CanDistro
@DeleteMapping("/metadata")
@Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE)
public String deleteInstanceMatadata(HttpServletRequest request) throws Exception {
final String namespaceId = WebUtils
.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
NamingUtils.checkServiceNameFormat(serviceName);

Instance instance = parseMetaInstance(request);

serviceManager.updateMetadata(namespaceId, serviceName, instance, UPDATE_INSTANCE_METADATA_ACTION_REMOVE);
return "ok";
}

/**
* Batch delete instance's metadata. old key exist = delete, old key not exist = not operate
*
* @param request http request
* @return 'ok' if success
* @throws Exception any error during update
*/
@CanDistro
@DeleteMapping("/metadata/batch")
@Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE)
public String batchDeleteInstanceMatadata(HttpServletRequest request) throws Exception {
return "ok";
}

/**
* Patch instance.
*
@@ -440,6 +515,20 @@ public ObjectNode listWithHealthStatus(@RequestParam String key) throws NacosExc
return result;
}

private Instance parseMetaInstance(HttpServletRequest request) throws Exception {

Instance basicInstance = getBasicIpAddress(request);

String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
basicInstance.setServiceName(serviceName);

String metadata = WebUtils.optional(request, "metadata", StringUtils.EMPTY);
if (StringUtils.isNotEmpty(metadata)) {
basicInstance.setMetadata(UtilsAndCommons.parseMetadata(metadata));
}
return basicInstance;
}

private Instance parseInstance(HttpServletRequest request) throws Exception {

String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
@@ -461,13 +550,28 @@ private Instance parseInstance(HttpServletRequest request) throws Exception {
return instance;
}

private Instance getIpAddress(HttpServletRequest request) {
private Instance getBasicIpAddress(HttpServletRequest request) {

final String ip = WebUtils.required(request, "ip");
final String port = WebUtils.required(request, "port");
String cluster = WebUtils.optional(request, CommonParams.CLUSTER_NAME, StringUtils.EMPTY);
if (StringUtils.isBlank(cluster)) {
cluster = WebUtils.optional(request, "cluster", UtilsAndCommons.DEFAULT_CLUSTER_NAME);
}
boolean ephemeral = BooleanUtils.toBoolean(
WebUtils.optional(request, "ephemeral", String.valueOf(switchDomain.isDefaultInstanceEphemeral())));

Instance instance = new Instance();
instance.setPort(Integer.parseInt(port));
instance.setIp(ip);
instance.setEphemeral(ephemeral);
instance.setClusterName(cluster);

return instance;
}

private Instance getIpAddress(HttpServletRequest request) {

String enabledString = WebUtils.optional(request, "enabled", StringUtils.EMPTY);
boolean enabled;
if (StringUtils.isBlank(enabledString)) {
@@ -476,20 +580,13 @@ private Instance getIpAddress(HttpServletRequest request) {
enabled = BooleanUtils.toBoolean(enabledString);
}

boolean ephemeral = BooleanUtils.toBoolean(
WebUtils.optional(request, "ephemeral", String.valueOf(switchDomain.isDefaultInstanceEphemeral())));

String weight = WebUtils.optional(request, "weight", "1");
boolean healthy = BooleanUtils.toBoolean(WebUtils.optional(request, "healthy", "true"));

Instance instance = new Instance();
instance.setPort(Integer.parseInt(port));
instance.setIp(ip);
Instance instance = getBasicIpAddress(request);
instance.setWeight(Double.parseDouble(weight));
instance.setClusterName(cluster);
instance.setHealthy(healthy);
instance.setEnabled(enabled);
instance.setEphemeral(ephemeral);

return instance;
}
Original file line number Diff line number Diff line change
@@ -17,20 +17,16 @@
package com.alibaba.nacos.naming.controllers;

import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.auth.common.ActionTypes;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.core.cluster.Member;
import com.alibaba.nacos.core.cluster.NodeState;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.core.utils.WebUtils;
import com.alibaba.nacos.naming.cluster.ServerListManager;
import com.alibaba.nacos.naming.cluster.ServerStatusManager;
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftCore;
import com.alibaba.nacos.naming.core.DistroMapper;
import com.alibaba.nacos.naming.core.Instance;
import com.alibaba.nacos.naming.core.Service;
import com.alibaba.nacos.naming.core.ServiceManager;
import com.alibaba.nacos.naming.misc.Loggers;
@@ -39,15 +35,11 @@
import com.alibaba.nacos.naming.misc.SwitchManager;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.push.PushService;
import com.alibaba.nacos.naming.web.CanDistro;
import com.alibaba.nacos.naming.web.NamingResourceParser;
import com.alibaba.nacos.sys.utils.ApplicationUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -60,9 +52,6 @@
import java.util.ArrayList;
import java.util.List;

import static com.alibaba.nacos.naming.misc.UtilsAndCommons.UPDATE_INSTANCE_METADATA_ACTION_REMOVE;
import static com.alibaba.nacos.naming.misc.UtilsAndCommons.UPDATE_INSTANCE_METADATA_ACTION_UPDATE;

/**
* Operation for operators.
*
@@ -294,75 +283,4 @@ public String setLogLevel(@RequestParam String logName, @RequestParam String log
public JsonNode getClusterStates() {
return JacksonUtils.transferToJsonNode(serviceManager.getMySelfClusterState());
}

/**
* Update instance's metadata. old key exist = update, old key not exist = add.
*
* @param request http request
* @return 'ok' if success
* @throws Exception any error during update
*/
@CanDistro
@PutMapping(value = "/instance/metadata")
@Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE)
public String updateInstanceMatadata(HttpServletRequest request) throws Exception {
final String namespaceId = WebUtils
.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
NamingUtils.checkServiceNameFormat(serviceName);

Instance instance = parseMetaInstance(request);

serviceManager.updateMetadata(namespaceId, serviceName, instance, UPDATE_INSTANCE_METADATA_ACTION_UPDATE);
return "ok";
}


/**
* delete instance's metadata. old key exist = delete, old key not exist = not operate
*
* @param request http request
* @return 'ok' if success
* @throws Exception any error during update
*/
@CanDistro
@DeleteMapping("/instance/metadata")
@Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE)
public String deleteInstanceMatadata(HttpServletRequest request) throws Exception {
final String namespaceId = WebUtils
.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
NamingUtils.checkServiceNameFormat(serviceName);

Instance instance = parseMetaInstance(request);

serviceManager.updateMetadata(namespaceId, serviceName, instance, UPDATE_INSTANCE_METADATA_ACTION_REMOVE);
return "ok";
}

private Instance parseMetaInstance(HttpServletRequest request) throws Exception {

final String ip = WebUtils.required(request, "ip");
final String port = WebUtils.required(request, "port");
String cluster = WebUtils.optional(request, CommonParams.CLUSTER_NAME, StringUtils.EMPTY);
if (StringUtils.isBlank(cluster)) {
cluster = WebUtils.optional(request, "cluster", UtilsAndCommons.DEFAULT_CLUSTER_NAME);
}
boolean ephemeral = BooleanUtils.toBoolean(
WebUtils.optional(request, "ephemeral", String.valueOf(switchDomain.isDefaultInstanceEphemeral())));
Instance instance = new Instance();
instance.setPort(Integer.parseInt(port));
instance.setIp(ip);
instance.setEphemeral(ephemeral);
instance.setClusterName(cluster);

String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
instance.setServiceName(serviceName);
String metadata = WebUtils.optional(request, "metadata", StringUtils.EMPTY);
if (StringUtils.isNotEmpty(metadata)) {
instance.setMetadata(UtilsAndCommons.parseMetadata(metadata));
}
return instance;
}

}