From 7303ea7b665d86d80dadfd9eca35f9e8abe75089 Mon Sep 17 00:00:00 2001 From: nkorange Date: Wed, 20 Mar 2019 18:08:36 +0800 Subject: [PATCH] #745 Remove server mode --- .../distro/DistroConsistencyServiceImpl.java | 2 +- .../naming/controllers/DistroController.java | 4 ++-- .../controllers/InstanceController.java | 22 +++++-------------- .../nacos/naming/core/ServiceManager.java | 22 ++++++++++--------- .../nacos/naming/misc/SwitchDomain.java | 10 ++++----- .../nacos/naming/misc/SwitchEntry.java | 2 +- .../nacos/naming/misc/SwitchManager.java | 9 ++++---- .../nacos/naming/web/TrafficReviseFilter.java | 18 --------------- .../test/naming/CPInstancesAPI_ITCase.java | 2 -- .../alibaba/nacos/test/naming/NamingBase.java | 14 ++---------- 10 files changed, 33 insertions(+), 72 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java index d6cfbe0bf..9228786eb 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java @@ -271,7 +271,7 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService if (!listeners.containsKey(entry.getKey())) { // pretty sure the service not exist: - if (ServerMode.AP.name().equals(switchDomain.getServerMode())) { + if (switchDomain.isDefaultInstanceEphemeral()) { // create empty service Loggers.EPHEMERAL.info("creating service {}", entry.getKey()); Service service = new Service(); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java index 6fb7ea93f..058dd273c 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java @@ -85,8 +85,8 @@ public class DistroController { String namespaceId = KeyBuilder.getNamespace(entry.getKey()); String serviceName = KeyBuilder.getServiceName(entry.getKey()); if (!serviceManager.containService(namespaceId, serviceName) - && ServerMode.AP.name().equals(switchDomain.getServerMode())) { - serviceManager.createEmptyService(namespaceId, serviceName); + && switchDomain.isDefaultInstanceEphemeral()) { + serviceManager.createEmptyService(namespaceId, serviceName, true); } consistencyService.onPut(entry.getKey(), entry.getValue().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 aa6cd746b..e665d7abc 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 @@ -216,15 +216,15 @@ public class InstanceController { result.put("clientBeatInterval", switchDomain.getClientBeatInterval()); - // ignore client beat in CP mode: - if (ServerMode.CP.name().equals(switchDomain.getServerMode())) { - return result; - } - String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); String beat = WebUtils.required(request, "beat"); RsInfo clientBeat = JSON.parseObject(beat, RsInfo.class); + + if (!switchDomain.isDefaultInstanceEphemeral() && !clientBeat.isEphemeral()) { + return result; + } + if (StringUtils.isBlank(clientBeat.getCluster())) { clientBeat.setCluster(UtilsAndCommons.DEFAULT_CLUSTER_NAME); } @@ -323,16 +323,6 @@ public class InstanceController { throw new NacosException(NacosException.INVALID_PARAM, "instance format invalid:" + instance); } - if ((ServerMode.AP.name().equals(switchDomain.getServerMode()) && !instance.isEphemeral())) { - throw new NacosException(NacosException.INVALID_PARAM, "wrong instance type: " + instance.isEphemeral() - + " in " + switchDomain.getServerMode() + " mode."); - } - - if ((ServerMode.CP.name().equals(switchDomain.getServerMode()) && instance.isEphemeral())) { - throw new NacosException(NacosException.INVALID_PARAM, "wrong instance type: " + instance.isEphemeral() - + " in " + switchDomain.getServerMode() + " mode."); - } - return instance; } @@ -346,7 +336,7 @@ public class InstanceController { boolean enabled = BooleanUtils.toBoolean(WebUtils.optional(request, "enable", "true")); // If server running in CP mode, we set this flag to false: boolean ephemeral = BooleanUtils.toBoolean(WebUtils.optional(request, "ephemeral", - String.valueOf(!ServerMode.CP.name().equals(switchDomain.getServerMode())))); + String.valueOf(switchDomain.isDefaultInstanceEphemeral()))); Instance instance = new Instance(); instance.setPort(Integer.parseInt(port)); 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 e74980cab..d06bd90b6 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 @@ -21,7 +21,6 @@ import com.alibaba.fastjson.JSONObject; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.naming.cluster.ServerListManager; -import com.alibaba.nacos.naming.cluster.ServerMode; import com.alibaba.nacos.naming.cluster.servers.Server; import com.alibaba.nacos.naming.consistency.ConsistencyService; import com.alibaba.nacos.naming.consistency.Datum; @@ -335,13 +334,14 @@ public class ServiceManager implements RecordListener { consistencyService.remove(KeyBuilder.buildServiceMetaKey(namespaceId, serviceName)); } - public void addOrReplaceService(Service service) throws Exception { + public void addOrReplaceService(Service service) throws NacosException { consistencyService.put(KeyBuilder.buildServiceMetaKey(service.getNamespaceId(), service.getName()), service); } - public void createEmptyService(String namespaceId, String serviceName) throws NacosException { + public void createEmptyService(String namespaceId, String serviceName, boolean local) throws NacosException { Service service = getService(namespaceId, serviceName); if (service == null) { + Loggers.SRV_LOG.info("creating empty service {}:{}", namespaceId, serviceName); service = new Service(); service.setName(serviceName); @@ -351,10 +351,14 @@ public class ServiceManager implements RecordListener { service.setLastModifiedMillis(System.currentTimeMillis()); service.recalculateChecksum(); service.validate(); - putService(service); - service.init(); - consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), true), service); - consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), false), service); + if (local) { + putService(service); + service.init(); + consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), true), service); + consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), false), service); + } else { + addOrReplaceService(service); + } } } @@ -370,9 +374,7 @@ public class ServiceManager implements RecordListener { */ public void registerInstance(String namespaceId, String serviceName, Instance instance) throws NacosException { - if (ServerMode.AP.name().equals(switchDomain.getServerMode())) { - createEmptyService(namespaceId, serviceName); - } + createEmptyService(namespaceId, serviceName, instance.isEphemeral()); Service service = getService(namespaceId, serviceName); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchDomain.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchDomain.java index 3abf979cb..51c0c5dd6 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchDomain.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchDomain.java @@ -89,7 +89,7 @@ public class SwitchDomain implements Record, Cloneable { private String overriddenServerStatus = null; - private String serverMode = "MIXED"; + private boolean defaultInstanceEphemeral = true; public boolean isEnableAuthentication() { return enableAuthentication; @@ -342,12 +342,12 @@ public class SwitchDomain implements Record, Cloneable { this.overriddenServerStatus = overriddenServerStatus; } - public String getServerMode() { - return serverMode; + public boolean isDefaultInstanceEphemeral() { + return defaultInstanceEphemeral; } - public void setServerMode(String serverMode) { - this.serverMode = serverMode; + public void setDefaultInstanceEphemeral(boolean defaultInstanceEphemeral) { + this.defaultInstanceEphemeral = defaultInstanceEphemeral; } @Override diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchEntry.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchEntry.java index d9d16e9ff..b3233c430 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchEntry.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchEntry.java @@ -59,5 +59,5 @@ public class SwitchEntry { public static final String PARAM_JSON = "json"; public static final String OVERRIDDEN_SERVER_STATUS = "overriddenServerStatus"; - public static final String SERVER_MODE = "serverMode"; + public static final String INSTANCE_DEFAULT_EPHEMERAL = "instanceDefaultEphemeral"; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchManager.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchManager.java index a4df47f1f..b6ffc83fb 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/SwitchManager.java @@ -18,7 +18,6 @@ package com.alibaba.nacos.naming.misc; import com.alibaba.fastjson.JSON; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.naming.cluster.ServerMode; import com.alibaba.nacos.naming.consistency.ConsistencyService; import com.alibaba.nacos.naming.consistency.Datum; import com.alibaba.nacos.naming.consistency.KeyBuilder; @@ -263,9 +262,9 @@ public class SwitchManager implements RecordListener { switchDomain.setOverriddenServerStatus(status); } - if (entry.equals(SwitchEntry.SERVER_MODE)) { - String mode = value; - switchDomain.setServerMode(ServerMode.valueOf(mode).name()); + if (entry.equals(SwitchEntry.INSTANCE_DEFAULT_EPHEMERAL)) { + String defaultEphemeral = value; + switchDomain.setDefaultInstanceEphemeral(Boolean.parseBoolean(defaultEphemeral)); } if (debug) { @@ -308,7 +307,7 @@ public class SwitchManager implements RecordListener { switchDomain.setPushCVersion(newSwitchDomain.getPushCVersion()); switchDomain.setEnableAuthentication(newSwitchDomain.isEnableAuthentication()); switchDomain.setOverriddenServerStatus(newSwitchDomain.getOverriddenServerStatus()); - switchDomain.setServerMode(newSwitchDomain.getServerMode()); + switchDomain.setDefaultInstanceEphemeral(newSwitchDomain.isDefaultInstanceEphemeral()); } public SwitchDomain getSwitchDomain() { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java b/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java index 136123e2f..6ecf30879 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/TrafficReviseFilter.java @@ -66,24 +66,6 @@ public class TrafficReviseFilter implements Filter { } } - // in AP mode, service and cluster cannot be edited: - try { - String path = new URI(req.getRequestURI()).getPath(); - if (ServerMode.AP.name().equals(switchDomain.getServerMode()) && !HttpMethod.GET.equals(req.getMethod())) { - if (path.contains(UtilsAndCommons.NACOS_NAMING_CONTEXT + UtilsAndCommons.NACOS_NAMING_SERVICE_CONTEXT) - || path.contains(UtilsAndCommons.NACOS_NAMING_CONTEXT + UtilsAndCommons.NACOS_NAMING_CLUSTER_CONTEXT)) { - resp.getWriter().write("server in AP mode, request: " + req.getMethod() + " " + path + " not permitted"); - resp.setStatus(HttpServletResponse.SC_FORBIDDEN); - return; - } - } - - } catch (URISyntaxException e) { - resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, - "Server parse url failed," + UtilsAndCommons.getAllExceptionMsg(e)); - return; - } - // if server is UP: if (serverStatusManager.getServerStatus() == ServerStatus.UP) { filterChain.doFilter(req, resp); diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/CPInstancesAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/CPInstancesAPI_ITCase.java index b4099a461..0016fd346 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/CPInstancesAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/CPInstancesAPI_ITCase.java @@ -79,8 +79,6 @@ public class CPInstancesAPI_ITCase { public void setUp() throws Exception { String url = String.format("http://localhost:%d/", port); this.base = new URL(url); - NamingBase.prepareServer(port, "UP", "CP"); - TimeUnit.SECONDS.sleep(5L); naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port); diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java b/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java index b2b91dd57..b87282fe5 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/NamingBase.java @@ -165,10 +165,10 @@ public class NamingBase { } public static void prepareServer(int localPort) { - prepareServer(localPort, "UP", "AP"); + prepareServer(localPort, "UP"); } - public static void prepareServer(int localPort, String status, String mode) { + public static void prepareServer(int localPort, String status) { String url = "http://127.0.0.1:" + localPort + "/nacos/v1/ns/operator/switches?entry=overriddenServerStatus&value=" + status; List headers = new ArrayList(); headers.add("User-Agent"); @@ -177,15 +177,5 @@ public class NamingBase { HttpClient.request(url, headers, new HashMap(), "UTF-8", "PUT"); Assert.assertEquals(HttpStatus.SC_OK, result.code); - - - url = "http://127.0.0.1:" + localPort + "/nacos/v1/ns/operator/switches?entry=serverMode&value=" + mode; - headers = new ArrayList(); - headers.add("User-Agent"); - headers.add("Nacos-Server"); - result = - HttpClient.request(url, headers, new HashMap(), "UTF-8", "PUT"); - - Assert.assertEquals(HttpStatus.SC_OK, result.code); } }