deleteMetadata = new HashMap<>(16);
+ deleteMetadata.put("key2", null);
+ deleteMetadata.put("key3", null);
+ updateMetadataInstance.setMetadata(deleteMetadata);
+
+ serviceManager.updateMetadata(TEST_NAMESPACE, TEST_SERVICE_NAME, updateMetadataInstance,
+ UPDATE_INSTANCE_METADATA_ACTION_REMOVE);
+
+ assertEquals(instance.getMetadata().get("key1"), "new-value1");
+ assertNull(instance.getMetadata().get("key2"));
+ assertNull(instance.getMetadata().get("key3"));
+
+
+ }
+
@Test
public void testRemoveInstance() throws NacosException {
serviceManager.createEmptyService(TEST_NAMESPACE, TEST_SERVICE_NAME, true);
From 21ae919c8ca9cebc5dddcb29b044a976aaeb3e68 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=B5=B5=E5=BB=B6?= <1060026287@qq.com>
Date: Fri, 25 Sep 2020 12:36:36 +0800
Subject: [PATCH 03/22] format code style
---
.../java/com/alibaba/nacos/naming/core/ServiceManagerTest.java | 2 --
1 file changed, 2 deletions(-)
diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceManagerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceManagerTest.java
index 961e7b3ad9c..3eebe8151f5 100644
--- a/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceManagerTest.java
+++ b/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceManagerTest.java
@@ -268,8 +268,6 @@ public void testUpdateMetadata() throws NacosException {
assertEquals(instance.getMetadata().get("key1"), "new-value1");
assertNull(instance.getMetadata().get("key2"));
assertNull(instance.getMetadata().get("key3"));
-
-
}
@Test
From 5224bdecaf795113434c82352e9639a79f7125cd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=B5=B5=E5=BB=B6?= <1060026287@qq.com>
Date: Fri, 25 Sep 2020 13:53:35 +0800
Subject: [PATCH 04/22] add @link on class
---
.../java/com/alibaba/nacos/api/naming/utils/NamingUtils.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java b/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java
index 5e3941fed88..2f0d6e26246 100644
--- a/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java
+++ b/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java
@@ -56,8 +56,8 @@ public static String getGroupName(final String serviceNameWithGroup) {
}
/**
- * check combineServiceName format. the serviceName can't be blank. some relational logic in
- * com.alibaba.nacos.naming.web.DistroFilter#doFilter, it will handle combineServiceName in some case, you should
+ * check combineServiceName format. the serviceName can't be blank. some relational logic in {@link
+ * com.alibaba.nacos.naming.web.DistroFilter#doFilter}, it will handle combineServiceName in some case, you should
* know it.
*
* serviceName = "@@"; the length = 0; illegal
From 52c8c91da5864387ac7df2b3e591ecd0195574cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=B5=B5=E5=BB=B6?= <1060026287@qq.com>
Date: Sat, 26 Sep 2020 16:43:32 +0800
Subject: [PATCH 05/22] move metadata operate to InstanceController.
---
.../controllers/InstanceController.java | 115 ++++++++++++++++--
.../controllers/OperatorController.java | 82 -------------
2 files changed, 106 insertions(+), 91 deletions(-)
diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java
index cf79e985272..5994cc71148 100644
--- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java
+++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java
@@ -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
+ */
+ @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);
+ 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
+ */
+ @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;
}
diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java
index be2c40f3691..d67a46c7f4c 100644
--- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java
+++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java
@@ -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;
- }
-
}
From ba7df4ae388b34d4f972a1052447f4186b629010 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=B5=B5=E5=BB=B6?= <1060026287@qq.com>
Date: Sun, 27 Sep 2020 23:39:24 +0800
Subject: [PATCH 06/22] format code 50%
---
.../nacos/api/naming/utils/NamingUtils.java | 36 ++-
.../controllers/InstanceController.java | 255 ++++++++++++++----
.../nacos/naming/core/ServiceManager.java | 61 +++--
.../nacos/naming/misc/UtilsAndCommons.java | 2 +-
.../nacos/naming/core/ServiceManagerTest.java | 11 +-
5 files changed, 293 insertions(+), 72 deletions(-)
diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java b/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java
index 2f0d6e26246..66d0a794970 100644
--- a/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java
+++ b/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java
@@ -19,6 +19,8 @@
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.utils.StringUtils;
+import java.util.Map;
+
/**
* NamingUtils.
*
@@ -72,7 +74,39 @@ public static void checkServiceNameFormat(String combineServiceName) {
String[] split = combineServiceName.split(Constants.SERVICE_INFO_SPLITER);
if (split.length <= 1) {
throw new IllegalArgumentException(
- "Param 'serviceName' is illegal, it should be format as 'groupName@@serviceName");
+ "Param 'serviceName' is illegal, it should be format as 'groupName@@serviceName'");
+ }
+ }
+
+ /**
+ * get target value from param, if not found will throw {@link IllegalArgumentException}.
+ *
+ * @param param param
+ * @param key key
+ * @return value
+ */
+ public static String required(final Map param, final String key) {
+ String value = param.get(key);
+ if (StringUtils.isEmpty(value)) {
+ throw new IllegalArgumentException("Param '" + key + "' is required.");
}
+ return value;
}
+
+ /**
+ * get target value from param, if not found will return default value.
+ *
+ * @param param param
+ * @param key key
+ * @param defaultValue default value
+ * @return value
+ */
+ public static String optional(final Map param, final String key, final String defaultValue) {
+ String value = param.get(key);
+ if (StringUtils.isBlank(value)) {
+ return defaultValue;
+ }
+ return value;
+ }
+
}
diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java
index 5994cc71148..814b002bf3e 100644
--- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java
+++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java
@@ -39,9 +39,9 @@
import com.alibaba.nacos.naming.push.PushService;
import com.alibaba.nacos.naming.web.CanDistro;
import com.alibaba.nacos.naming.web.NamingResourceParser;
+import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
-
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
@@ -63,6 +63,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
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;
@@ -186,75 +188,189 @@ public String update(HttpServletRequest request) throws Exception {
}
/**
- * Update instance's metadata. old key exist = update, old key not exist = add.
+ * 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
*/
@CanDistro
- @PutMapping(value = "/metadata")
+ @PutMapping(value = "/metadata/batch")
@Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE)
- public String updateInstanceMatadata(HttpServletRequest request) throws Exception {
+ public String batchUpdateInstanceMatadata(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);
+ String targetInstances = WebUtils.required(request, "target");
+
+ List