From 4704200f87fc027111042deb4d2a91e8f27cd6ec Mon Sep 17 00:00:00 2001 From: nkorange Date: Thu, 7 Mar 2019 17:59:08 +0800 Subject: [PATCH] #839 Rest of methods refactored. --- .../{partition => distro}/DataStore.java | 2 +- .../{partition => distro}/DataSyncer.java | 2 +- .../DistroConsistencyServiceImpl.java} | 8 +- .../PartitionConfig.java | 12 +- .../{partition => distro}/SyncTask.java | 2 +- .../{partition => distro}/TaskDispatcher.java | 2 +- .../consistency/persistent/raft/RaftCore.java | 18 +- .../persistent/raft/RaftPeerSet.java | 2 +- .../persistent/raft/RaftProxy.java | 28 +++ .../naming/controllers/CatalogController.java | 178 ++++++++---------- ...nController.java => DistroController.java} | 10 +- .../naming/controllers/HealthController.java | 2 +- .../controllers/InstanceController.java | 16 +- .../controllers/OperatorController.java | 60 +++--- .../naming/controllers/RaftController.java | 20 +- .../nacos/naming/core/ServiceManager.java | 31 ++- .../nacos/naming/misc/GlobalExecutor.java | 4 +- .../alibaba/nacos/naming/misc/HttpClient.java | 23 +++ .../nacos/naming/misc/NamingProxy.java | 8 +- .../nacos/naming/misc/UtilsAndCommons.java | 2 +- .../alibaba/nacos/naming/web/FilterBase.java | 3 +- 21 files changed, 237 insertions(+), 196 deletions(-) rename naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/{partition => distro}/DataStore.java (97%) rename naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/{partition => distro}/DataSyncer.java (99%) rename naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/{partition/PartitionConsistencyServiceImpl.java => distro/DistroConsistencyServiceImpl.java} (97%) rename naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/{partition => distro}/PartitionConfig.java (81%) rename naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/{partition => distro}/SyncTask.java (95%) rename naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/{partition => distro}/TaskDispatcher.java (98%) rename naming/src/main/java/com/alibaba/nacos/naming/controllers/{PartitionController.java => DistroController.java} (93%) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/DataStore.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DataStore.java similarity index 97% rename from naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/DataStore.java rename to naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DataStore.java index 45ec94d11..fe95237f7 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/DataStore.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DataStore.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.nacos.naming.consistency.ephemeral.partition; +package com.alibaba.nacos.naming.consistency.ephemeral.distro; import com.alibaba.nacos.naming.consistency.Datum; import com.alibaba.nacos.naming.core.Instances; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/DataSyncer.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DataSyncer.java similarity index 99% rename from naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/DataSyncer.java rename to naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DataSyncer.java index 2fa27c389..9e3768c96 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/DataSyncer.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DataSyncer.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.nacos.naming.consistency.ephemeral.partition; +package com.alibaba.nacos.naming.consistency.ephemeral.distro; import com.alibaba.nacos.naming.cluster.ServerListManager; import com.alibaba.nacos.naming.cluster.servers.Server; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/PartitionConsistencyServiceImpl.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java similarity index 97% rename from naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/PartitionConsistencyServiceImpl.java rename to naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java index dd798ca69..062dd9eff 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/PartitionConsistencyServiceImpl.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.nacos.naming.consistency.ephemeral.partition; +package com.alibaba.nacos.naming.consistency.ephemeral.distro; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; @@ -43,7 +43,7 @@ import java.util.concurrent.ConcurrentHashMap; /** * A consistency protocol algorithm called Partition *

- * Use a partition algorithm to divide data into many blocks. Each Nacos server node takes + * Use a distro algorithm to divide data into many blocks. Each Nacos server node takes * responsibility for exactly one block of data. Each block of data is generated, removed * and synchronized by its responsible server. So every Nacos server only handles writings * for a subset of the total service data. @@ -54,8 +54,8 @@ import java.util.concurrent.ConcurrentHashMap; * @author nkorange * @since 1.0.0 */ -@org.springframework.stereotype.Service("partitionConsistencyService") -public class PartitionConsistencyServiceImpl implements EphemeralConsistencyService { +@org.springframework.stereotype.Service("distroConsistencyService") +public class DistroConsistencyServiceImpl implements EphemeralConsistencyService { @Autowired private DistroMapper distroMapper; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/PartitionConfig.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/PartitionConfig.java similarity index 81% rename from naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/PartitionConfig.java rename to naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/PartitionConfig.java index f8c3c02dd..34c07e655 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/PartitionConfig.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/PartitionConfig.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.nacos.naming.consistency.ephemeral.partition; +package com.alibaba.nacos.naming.consistency.ephemeral.distro; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -27,19 +27,19 @@ import org.springframework.stereotype.Component; @Component public class PartitionConfig { - @Value("${nacos.naming.partition.taskDispatchPeriod}") + @Value("${nacos.naming.distro.taskDispatchPeriod}") private int taskDispatchPeriod = 2000; - @Value("${nacos.naming.partition.batchSyncKeyCount}") + @Value("${nacos.naming.distro.batchSyncKeyCount}") private int batchSyncKeyCount = 1000; - @Value("${nacos.naming.partition.initDataRatio}") + @Value("${nacos.naming.distro.initDataRatio}") private float initDataRatio = 0.9F; - @Value("${nacos.naming.partition.syncRetryDelay}") + @Value("${nacos.naming.distro.syncRetryDelay}") private long syncRetryDelay = 5000L; - @Value("${nacos.naming.partition.taskDispatchThreadCount}") + @Value("${nacos.naming.distro.taskDispatchThreadCount}") private int taskDispatchThreadCount = Runtime.getRuntime().availableProcessors(); public int getTaskDispatchPeriod() { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/SyncTask.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/SyncTask.java similarity index 95% rename from naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/SyncTask.java rename to naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/SyncTask.java index 4c416fb4b..412221444 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/SyncTask.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/SyncTask.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.nacos.naming.consistency.ephemeral.partition; +package com.alibaba.nacos.naming.consistency.ephemeral.distro; import java.util.List; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/TaskDispatcher.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/TaskDispatcher.java similarity index 98% rename from naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/TaskDispatcher.java rename to naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/TaskDispatcher.java index 66980b389..f91ec038a 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/partition/TaskDispatcher.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/TaskDispatcher.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.nacos.naming.consistency.ephemeral.partition; +package com.alibaba.nacos.naming.consistency.ephemeral.distro; import com.alibaba.nacos.naming.cluster.servers.Server; import com.alibaba.nacos.naming.misc.GlobalExecutor; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java index 26b9c517e..2b5bab443 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java @@ -35,6 +35,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.javatuples.Pair; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @@ -62,17 +63,17 @@ public class RaftCore { public static final String API_BEAT = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/beat"; - public static final String API_PUB = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/publish"; + public static final String API_PUB = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/datum"; - public static final String API_DEL = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/delete"; + public static final String API_DEL = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/datum"; - public static final String API_GET = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/get"; + public static final String API_GET = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/datum"; - public static final String API_ON_PUB = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/onPublish"; + public static final String API_ON_PUB = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/datum/commit"; - public static final String API_ON_DEL = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/onDelete"; + public static final String API_ON_DEL = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/datum/commit"; - public static final String API_GET_PEER = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/getPeer"; + public static final String API_GET_PEER = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/raft/peer"; private ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { @Override @@ -228,8 +229,7 @@ public class RaftCore { if (!isLeader()) { Map params = new HashMap<>(1); params.put("key", URLEncoder.encode(key, "UTF-8")); - - raftProxy.proxyGET(getLeader().ip, API_DEL, params); + raftProxy.proxy(getLeader().ip, API_DEL, params, HttpMethod.DELETE); return; } @@ -244,7 +244,7 @@ public class RaftCore { for (final String server : peers.allServersWithoutMySelf()) { String url = buildURL(server, API_ON_DEL); - HttpClient.asyncHttpPostLarge(url, null, JSON.toJSONString(json) + HttpClient.asyncHttpDeleteLarge(url, null, JSON.toJSONString(json) , new AsyncCompletionHandler() { @Override public Integer onCompleted(Response response) throws Exception { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftPeerSet.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftPeerSet.java index 202324c81..03847a3c6 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftPeerSet.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftPeerSet.java @@ -172,7 +172,7 @@ public class RaftPeerSet implements ServerChangeListener { if (!Objects.equals(peer, candidate) && peer.state == RaftPeer.State.LEADER) { try { String url = RaftCore.buildURL(peer.ip, RaftCore.API_GET_PEER); - HttpClient.asyncHttpPost(url, null, params, new AsyncCompletionHandler() { + HttpClient.asyncHttpGet(url, null, params, new AsyncCompletionHandler() { @Override public Integer onCompleted(Response response) throws Exception { if (response.getStatusCode() != HttpURLConnection.HTTP_OK) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftProxy.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftProxy.java index 0528343f4..9c8ae71d3 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftProxy.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftProxy.java @@ -15,9 +15,11 @@ */ package com.alibaba.nacos.naming.consistency.persistent.raft; +import com.alibaba.nacos.api.naming.pojo.AbstractHealthChecker; import com.alibaba.nacos.naming.boot.RunningConfig; import com.alibaba.nacos.naming.misc.HttpClient; import com.alibaba.nacos.naming.misc.UtilsAndCommons; +import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; import java.net.HttpURLConnection; @@ -42,6 +44,32 @@ public class RaftProxy { } } + public void proxy(String server, String api, Map params, HttpMethod method) throws Exception { + // do proxy + if (!server.contains(UtilsAndCommons.IP_PORT_SPLITER)) { + server = server + UtilsAndCommons.IP_PORT_SPLITER + RunningConfig.getServerPort(); + } + String url = "http://" + server + RunningConfig.getContextPath() + api; + HttpClient.HttpResult result; + switch (method) { + case GET: + result = HttpClient.httpGet(url, null, params); + break; + case POST: + result = HttpClient.httpPost(url, null, params); + break; + case DELETE: + result = HttpClient.httpDelete(url, null, params); + break; + default: + throw new RuntimeException("unsupported method:" + method); + } + + if (result.code != HttpURLConnection.HTTP_OK) { + throw new IllegalStateException("leader failed, caused by: " + result.content); + } + } + public void proxyPostLarge(String server, String api, String content, Map headers) throws Exception { // do proxy if (!server.contains(UtilsAndCommons.IP_PORT_SPLITER)) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/CatalogController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/CatalogController.java index 6da4a2470..279541908 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/CatalogController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/CatalogController.java @@ -32,6 +32,7 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.map.HashedMap; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @@ -49,54 +50,7 @@ public class CatalogController { @Autowired protected ServiceManager serviceManager; - @RequestMapping(value = "/serviceList") - public JSONObject serviceList(HttpServletRequest request) throws Exception { - - String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - Constants.DEFAULT_NAMESPACE_ID); - JSONObject result = new JSONObject(); - - int page = Integer.parseInt(WebUtils.required(request, "startPg")); - int pageSize = Integer.parseInt(WebUtils.required(request, "pgSize")); - String keyword = WebUtils.optional(request, "keyword", StringUtils.EMPTY); - - List services = new ArrayList<>(); - int total = serviceManager.getPagedService(namespaceId, page - 1, pageSize, keyword, services); - - if (CollectionUtils.isEmpty(services)) { - result.put("serviceList", Collections.emptyList()); - result.put("count", 0); - return result; - } - - JSONArray serviceJsonArray = new JSONArray(); - for (Service service : services) { - ServiceView serviceView = new ServiceView(); - serviceView.setName(UtilsAndCommons.getServiceName(service.getName())); - serviceView.setGroupName(UtilsAndCommons.getGroupName(service.getName())); - serviceView.setClusterCount(service.getClusterMap().size()); - serviceView.setIpCount(service.allIPs().size()); - - // FIXME should be optimized: - int validCount = 0; - for (Instance instance : service.allIPs()) { - if (instance.isHealthy()) { - validCount++; - } - } - - serviceView.setHealthyInstanceCount(validCount); - - serviceJsonArray.add(serviceView); - } - - result.put("serviceList", serviceJsonArray); - result.put("count", total); - - return result; - } - - @RequestMapping(value = "/serviceDetail") + @RequestMapping(value = "/service") public ServiceDetailView serviceDetail(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, @@ -130,15 +84,15 @@ public class CatalogController { return detailView; } - @RequestMapping(value = "/instanceList") + @RequestMapping(value = "/instances") public JSONObject instanceList(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String clusterName = WebUtils.required(request, CommonParams.CLUSTER_NAME); - int page = Integer.parseInt(WebUtils.required(request, "startPg")); - int pageSize = Integer.parseInt(WebUtils.required(request, "pgSize")); + int page = Integer.parseInt(WebUtils.required(request, "pageNo")); + int pageSize = Integer.parseInt(WebUtils.required(request, "pageSize")); Service service = serviceManager.getService(namespaceId, serviceName); if (service == null) { @@ -174,37 +128,43 @@ public class CatalogController { } @RequestMapping(value = "/services", method = RequestMethod.GET) - public List listDetail(HttpServletRequest request) { + public Object listDetail(HttpServletRequest request) throws Exception { - String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - Constants.DEFAULT_NAMESPACE_ID); - List serviceDetailInfoList = new ArrayList<>(); + boolean withInstances = Boolean.parseBoolean(WebUtils.optional(request, "withInstances", "true")); - serviceManager - .getServiceMap(namespaceId) - .forEach( - (serviceName, service) -> { + if (withInstances) { + String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, + Constants.DEFAULT_NAMESPACE_ID); + List serviceDetailInfoList = new ArrayList<>(); - ServiceDetailInfo serviceDetailInfo = new ServiceDetailInfo(); - serviceDetailInfo.setServiceName(UtilsAndCommons.getServiceName(serviceName)); - serviceDetailInfo.setGroupName(UtilsAndCommons.getGroupName(serviceName)); - serviceDetailInfo.setMetadata(service.getMetadata()); + serviceManager + .getServiceMap(namespaceId) + .forEach( + (serviceName, service) -> { - Map clusterInfoMap = getStringClusterInfoMap(service); - serviceDetailInfo.setClusterMap(clusterInfoMap); + ServiceDetailInfo serviceDetailInfo = new ServiceDetailInfo(); + serviceDetailInfo.setServiceName(UtilsAndCommons.getServiceName(serviceName)); + serviceDetailInfo.setGroupName(UtilsAndCommons.getGroupName(serviceName)); + serviceDetailInfo.setMetadata(service.getMetadata()); - serviceDetailInfoList.add(serviceDetailInfo); - }); + Map clusterInfoMap = getStringClusterInfoMap(service); + serviceDetailInfo.setClusterMap(clusterInfoMap); - return serviceDetailInfoList; + serviceDetailInfoList.add(serviceDetailInfo); + }); + return serviceDetailInfoList; + } else { + return serviceList(request); + } } - @RequestMapping("/rt4Service") + @RequestMapping("/rt/service") public JSONObject rt4Service(HttpServletRequest request) { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); + String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); Service service = serviceManager.getService(namespaceId, serviceName); @@ -231,38 +191,6 @@ public class CatalogController { return result; } - @RequestMapping("/getServicesByIP") - public JSONObject getServicesByIP(HttpServletRequest request) { - String ip = WebUtils.required(request, "ip"); - - Set serviceNames = new HashSet<>(); - Map> serviceNameMap = serviceManager.getAllServiceNames(); - - for (String namespaceId : serviceNameMap.keySet()) { - for (String serviceName : serviceNameMap.get(namespaceId)) { - Service service = serviceManager.getService(namespaceId, serviceName); - List instances = service.allIPs(); - for (Instance instance : instances) { - if (ip.contains(":")) { - if (StringUtils.equals(instance.getIp() + ":" + instance.getPort(), ip)) { - serviceNames.add(namespaceId + UtilsAndCommons.NAMESPACE_SERVICE_CONNECTOR + service.getName()); - } - } else { - if (StringUtils.equals(instance.getIp(), ip)) { - serviceNames.add(namespaceId + UtilsAndCommons.NAMESPACE_SERVICE_CONNECTOR + service.getName()); - } - } - } - } - } - - JSONObject result = new JSONObject(); - - result.put("doms", serviceNames); - - return result; - } - /** * getStringClusterInfoMap * @@ -307,4 +235,52 @@ public class CatalogController { return ipAddressInfos; } + private JSONObject serviceList(HttpServletRequest request) throws Exception { + + String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, + Constants.DEFAULT_NAMESPACE_ID); + JSONObject result = new JSONObject(); + + int page = Integer.parseInt(WebUtils.required(request, "pageNo")); + int pageSize = Integer.parseInt(WebUtils.required(request, "pageSize")); + String keyword = WebUtils.optional(request, "keyword", StringUtils.EMPTY); + String containedInstance = WebUtils.required(request, "instance"); + + List services = new ArrayList<>(); + int total = serviceManager.getPagedService(namespaceId, page - 1, pageSize, keyword, containedInstance, services); + + if (CollectionUtils.isEmpty(services)) { + result.put("serviceList", Collections.emptyList()); + result.put("count", 0); + return result; + } + + JSONArray serviceJsonArray = new JSONArray(); + for (Service service : services) { + ServiceView serviceView = new ServiceView(); + serviceView.setName(UtilsAndCommons.getServiceName(service.getName())); + serviceView.setGroupName(UtilsAndCommons.getGroupName(service.getName())); + serviceView.setClusterCount(service.getClusterMap().size()); + serviceView.setIpCount(service.allIPs().size()); + + // FIXME should be optimized: + int validCount = 0; + for (Instance instance : service.allIPs()) { + if (instance.isHealthy()) { + validCount++; + } + + } + + serviceView.setHealthyInstanceCount(validCount); + + serviceJsonArray.add(serviceView); + } + + result.put("serviceList", serviceJsonArray); + result.put("count", total); + + return result; + } + } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/PartitionController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java similarity index 93% rename from naming/src/main/java/com/alibaba/nacos/naming/controllers/PartitionController.java rename to naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java index 12315bbb1..2504a2767 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/PartitionController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java @@ -21,8 +21,8 @@ import com.alibaba.nacos.naming.cluster.ServerMode; import com.alibaba.nacos.naming.cluster.transport.Serializer; import com.alibaba.nacos.naming.consistency.Datum; import com.alibaba.nacos.naming.consistency.KeyBuilder; -import com.alibaba.nacos.naming.consistency.ephemeral.partition.DataStore; -import com.alibaba.nacos.naming.consistency.ephemeral.partition.PartitionConsistencyServiceImpl; +import com.alibaba.nacos.naming.consistency.ephemeral.distro.DataStore; +import com.alibaba.nacos.naming.consistency.ephemeral.distro.DistroConsistencyServiceImpl; import com.alibaba.nacos.naming.core.Instances; import com.alibaba.nacos.naming.core.ServiceManager; import com.alibaba.nacos.naming.exception.NacosException; @@ -48,14 +48,14 @@ import java.util.Map; * @since 1.0.0 */ @RestController -@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/partition") -public class PartitionController { +@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/distro") +public class DistroController { @Autowired private Serializer serializer; @Autowired - private PartitionConsistencyServiceImpl consistencyService; + private DistroConsistencyServiceImpl consistencyService; @Autowired private DataStore dataStore; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/HealthController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/HealthController.java index 461ec1fcd..931f711a7 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/HealthController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/HealthController.java @@ -68,7 +68,7 @@ public class HealthController { return result; } - @RequestMapping(value = "", method = RequestMethod.PUT) + @RequestMapping(value = {"", "/instance"}, method = RequestMethod.PUT) public String update(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, 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 2f2173add..301d719bf 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 @@ -52,7 +52,7 @@ import java.util.*; * @author nkorange */ @RestController -@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT) +@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance") public class InstanceController { @Autowired @@ -89,7 +89,7 @@ public class InstanceController { }; @CanDistro - @RequestMapping(value = "/instance", method = RequestMethod.POST) + @RequestMapping(value = "", method = RequestMethod.POST) public String register(HttpServletRequest request) throws Exception { String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); @@ -100,7 +100,7 @@ public class InstanceController { } @CanDistro - @RequestMapping(value = "/instance", method = RequestMethod.DELETE) + @RequestMapping(value = "", method = RequestMethod.DELETE) public String deregister(HttpServletRequest request) throws Exception { Instance instance = getIPAddress(request); String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, @@ -117,7 +117,7 @@ public class InstanceController { return "ok"; } - @RequestMapping(value = "/instance", method = RequestMethod.PUT) + @RequestMapping(value = "", method = RequestMethod.PUT) public String update(HttpServletRequest request) throws Exception { String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); @@ -126,7 +126,7 @@ public class InstanceController { return "ok"; } - @RequestMapping(value = {"/instances", "/instance/list"}, method = RequestMethod.GET) + @RequestMapping(value = "/list", method = RequestMethod.GET) public JSONObject list(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, @@ -152,7 +152,7 @@ public class InstanceController { return doSrvIPXT(namespaceId, serviceName, agent, clusters, clientIP, udpPort, env, isCheck, app, tenant, healthyOnly); } - @RequestMapping(value = "/instance", method = RequestMethod.GET) + @RequestMapping(value = "", method = RequestMethod.GET) public JSONObject detail(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, @@ -194,7 +194,7 @@ public class InstanceController { } @CanDistro - @RequestMapping(value = "/instance/beat", method = RequestMethod.PUT) + @RequestMapping(value = "/beat", method = RequestMethod.PUT) public JSONObject beat(HttpServletRequest request) throws Exception { JSONObject result = new JSONObject(); @@ -254,7 +254,7 @@ public class InstanceController { } - @RequestMapping("/instance/listWithHealthStatus") + @RequestMapping("/statuses") public JSONObject listWithHealthStatus(HttpServletRequest request) throws NacosException { String key = WebUtils.required(request, "key"); 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 b78d410af..6af3f80b7 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 @@ -34,6 +34,7 @@ import com.alibaba.nacos.naming.push.PushService; import com.alibaba.nacos.naming.web.NeedAuth; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @@ -72,7 +73,7 @@ public class OperatorController { @Autowired private DistroMapper distroMapper; - @RequestMapping("/pushState") + @RequestMapping("/push/state") public JSONObject pushState(HttpServletRequest request) { JSONObject result = new JSONObject(); @@ -112,7 +113,7 @@ public class OperatorController { return result; } - @RequestMapping("/switches") + @RequestMapping(value = "/switches", method = RequestMethod.GET) public SwitchDomain switches(HttpServletRequest request) { return switchDomain; } @@ -152,12 +153,12 @@ public class OperatorController { return result; } - @RequestMapping("/getResponsibleServer4Dom") - public JSONObject getResponsibleServer4Dom(HttpServletRequest request) { + @RequestMapping(value = "/distro/server", method = RequestMethod.GET) + public JSONObject getResponsibleServer4Service(HttpServletRequest request) { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); - String dom = WebUtils.required(request, "dom"); - Service service = serviceManager.getService(namespaceId, dom); + String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); + Service service = serviceManager.getService(namespaceId, serviceName); if (service == null) { throw new IllegalArgumentException("service not found"); @@ -165,39 +166,12 @@ public class OperatorController { JSONObject result = new JSONObject(); - result.put("responsibleServer", distroMapper.mapSrv(dom)); + result.put("responsibleServer", distroMapper.mapSrv(serviceName)); return result; } - @RequestMapping("/getHealthyServerList") - public JSONObject getHealthyServerList(HttpServletRequest request) { - - JSONObject result = new JSONObject(); - result.put("healthyList", distroMapper.getHealthyList()); - - return result; - } - - @RequestMapping("/responsible") - public JSONObject responsible(HttpServletRequest request) { - String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - Constants.DEFAULT_NAMESPACE_ID); - String dom = WebUtils.required(request, "dom"); - Service service = serviceManager.getService(namespaceId, dom); - - if (service == null) { - throw new IllegalArgumentException("service not found"); - } - - JSONObject result = new JSONObject(); - - result.put("responsible", distroMapper.responsible(dom)); - - return result; - } - - @RequestMapping("/distroStatus") + @RequestMapping(value = "/distro/status", method = RequestMethod.GET) public JSONObject distroStatus(HttpServletRequest request) { JSONObject result = new JSONObject(); @@ -216,7 +190,21 @@ public class OperatorController { return result; } - @RequestMapping("/serverStatus") + @RequestMapping(value = "/servers", method = RequestMethod.GET) + public JSONObject getHealthyServerList(HttpServletRequest request) { + + boolean healthy = Boolean.parseBoolean(WebUtils.optional(request, "healthy", "false")); + JSONObject result = new JSONObject(); + if (healthy) { + result.put("servers", serverListManager.getHealthyServers()); + } else { + result.put("servers", serverListManager.getServers()); + } + + return result; + } + + @RequestMapping("/server/status") public String serverStatus(HttpServletRequest request) { String serverStatus = WebUtils.required(request, "serverStatus"); serverListManager.onReceiveServerStatus(serverStatus); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/RaftController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/RaftController.java index acf075ed0..5b84551e6 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/RaftController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/RaftController.java @@ -96,7 +96,7 @@ public class RaftController { } @NeedAuth - @RequestMapping("/getPeer") + @RequestMapping(value = "/peer", method = RequestMethod.GET) public JSONObject getPeer(HttpServletRequest request, HttpServletResponse response) { List peers = raftCore.getPeers(); RaftPeer peer = null; @@ -116,7 +116,7 @@ public class RaftController { } @NeedAuth - @RequestMapping("/reloadDatum") + @RequestMapping(value = "/datum/reload", method = RequestMethod.PUT) public String reloadDatum(HttpServletRequest request, HttpServletResponse response) throws Exception { String key = WebUtils.required(request, "key"); raftCore.loadDatum(key); @@ -124,7 +124,7 @@ public class RaftController { } @NeedAuth - @RequestMapping(value = "/publish", method = RequestMethod.POST) + @RequestMapping(value = "/datum", method = RequestMethod.POST) public String publish(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request)); @@ -155,7 +155,7 @@ public class RaftController { } @NeedAuth - @RequestMapping(value = "/delete", method = RequestMethod.POST) + @RequestMapping(value = "/datum", method = RequestMethod.DELETE) public String delete(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request)); @@ -166,7 +166,7 @@ public class RaftController { } @NeedAuth - @RequestMapping("/get") + @RequestMapping(value = "/datum", method = RequestMethod.GET) public String get(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request)); @@ -185,7 +185,7 @@ public class RaftController { return JSON.toJSONString(datums); } - @RequestMapping("/state") + @RequestMapping(value = "/state", method = RequestMethod.GET) public JSONObject state(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request)); @@ -200,7 +200,7 @@ public class RaftController { } @NeedAuth - @RequestMapping(value = "/onPublish", method = RequestMethod.POST) + @RequestMapping(value = "/datum/commit", method = RequestMethod.POST) public String onPublish(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request)); @@ -232,7 +232,7 @@ public class RaftController { } @NeedAuth - @RequestMapping(value = "/onDelete", method = RequestMethod.POST) + @RequestMapping(value = "/datum/commit", method = RequestMethod.DELETE) public String onDelete(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request)); @@ -252,7 +252,7 @@ public class RaftController { return "ok"; } - @RequestMapping("/getLeader") + @RequestMapping(value = "/leader", method = RequestMethod.GET) public JSONObject getLeader(HttpServletRequest request, HttpServletResponse response) { JSONObject result = new JSONObject(); @@ -260,7 +260,7 @@ public class RaftController { return result; } - @RequestMapping("/getAllListeners") + @RequestMapping(value = "/listeners", method = RequestMethod.GET) public JSONObject getAllListeners(HttpServletRequest request, HttpServletResponse response) { JSONObject result = new JSONObject(); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java index f9f3d4a8f..c304b320a 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java @@ -561,7 +561,7 @@ public class ServiceManager implements RecordListener { return serviceMap.get(namespaceId); } - public int getPagedService(String namespaceId, int startPage, int pageSize, String keyword, List serviceList) { + public int getPagedService(String namespaceId, int startPage, int pageSize, String keyword, String containedInstance, List serviceList) { List matchList; @@ -572,7 +572,34 @@ public class ServiceManager implements RecordListener { if (StringUtils.isNotBlank(keyword)) { matchList = searchServices(namespaceId, ".*" + keyword + ".*"); } else { - matchList = new ArrayList(chooseServiceMap(namespaceId).values()); + matchList = new ArrayList<>(chooseServiceMap(namespaceId).values()); + } + + if (StringUtils.isNotBlank(containedInstance)) { + + boolean contained; + for (int i = 0; i < matchList.size(); i++) { + Service service = matchList.get(i); + contained = false; + List instances = service.allIPs(); + for (Instance instance : instances) { + if (containedInstance.contains(":")) { + if (StringUtils.equals(instance.getIp() + ":" + instance.getPort(), containedInstance)) { + contained = true; + break; + } + } else { + if (StringUtils.equals(instance.getIp(), containedInstance)) { + contained = true; + break; + } + } + } + if (!contained) { + matchList.remove(i); + i--; + } + } } if (pageSize >= matchList.size()) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/GlobalExecutor.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/GlobalExecutor.java index 0509128da..f1bf12ea9 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/GlobalExecutor.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/GlobalExecutor.java @@ -56,7 +56,7 @@ public class GlobalExecutor { Thread t = new Thread(r); t.setDaemon(true); - t.setName("com.alibaba.nacos.naming.partition.task.dispatcher"); + t.setName("com.alibaba.nacos.naming.distro.task.dispatcher"); return t; } @@ -70,7 +70,7 @@ public class GlobalExecutor { Thread t = new Thread(r); t.setDaemon(true); - t.setName("com.alibaba.nacos.naming.partition.data.syncer"); + t.setName("com.alibaba.nacos.naming.distro.data.syncer"); return t; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java index b12df7a8d..22088155d 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java @@ -200,6 +200,29 @@ public class HttpClient { } } + public static void asyncHttpDeleteLarge(String url, List headers, String content, AsyncCompletionHandler handler) throws Exception { + AsyncHttpClient.BoundRequestBuilder builder = asyncHttpClient.prepareDelete(url); + + if (!CollectionUtils.isEmpty(headers)) { + for (String header : headers) { + builder.setHeader(header.split("=")[0], header.split("=")[1]); + } + } + + builder.setBody(content.getBytes()); + + builder.setHeader("Content-Type", "application/json; charset=UTF-8"); + builder.setHeader("Accept-Charset", "UTF-8"); + builder.setHeader("Accept-Encoding", "gzip"); + builder.setHeader("Content-Encoding", "gzip"); + + if (handler != null) { + builder.execute(handler); + } else { + builder.execute(); + } + } + public static HttpResult httpPost(String url, List headers, Map paramValues) { return httpPost(url, headers, paramValues, "UTF-8"); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java index 370d52113..9903967a2 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java @@ -31,13 +31,13 @@ import java.util.*; */ public class NamingProxy { - private static final String DATA_ON_SYNC_URL = "/partition/datum"; + private static final String DATA_ON_SYNC_URL = "/distro/datum"; - private static final String DATA_GET_URL = "/partition/datum"; + private static final String DATA_GET_URL = "/distro/datum"; - private static final String ALL_DATA_GET_URL = "/partition/datums"; + private static final String ALL_DATA_GET_URL = "/distro/datums"; - private static final String TIMESTAMP_SYNC_URL = "/partition/checksum"; + private static final String TIMESTAMP_SYNC_URL = "/distro/checksum"; public static void syncChecksums(Map checksumMap, String server) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java index 050a9b9ab..950650fe7 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/UtilsAndCommons.java @@ -62,7 +62,7 @@ public class UtilsAndCommons { public static final String NACOS_NAMING_RAFT_CONTEXT = "/raft"; - public static final String NACOS_NAMING_PARTITION_CONTEXT = "/partition"; + public static final String NACOS_NAMING_PARTITION_CONTEXT = "/distro"; public static final String NACOS_NAMING_OPERATOR_CONTEXT = "/operator"; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java b/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java index 1875c810c..6eac81575 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java @@ -15,7 +15,6 @@ */ package com.alibaba.nacos.naming.web; -import com.alibaba.nacos.naming.boot.RunningConfig; import com.alibaba.nacos.naming.controllers.*; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; @@ -46,7 +45,7 @@ public class FilterBase { initClassMethod(CatalogController.class); initClassMethod(HealthController.class); initClassMethod(RaftController.class); - initClassMethod(PartitionController.class); + initClassMethod(DistroController.class); initClassMethod(OperatorController.class); }