#745 Remove server mode

This commit is contained in:
nkorange 2019-03-20 18:08:36 +08:00
parent f6ea5042c8
commit 7303ea7b66
10 changed files with 33 additions and 72 deletions

View File

@ -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();

View File

@ -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);
}

View File

@ -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));

View File

@ -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<Service> {
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> {
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<Service> {
*/
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);

View File

@ -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

View File

@ -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";
}

View File

@ -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> {
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> {
switchDomain.setPushCVersion(newSwitchDomain.getPushCVersion());
switchDomain.setEnableAuthentication(newSwitchDomain.isEnableAuthentication());
switchDomain.setOverriddenServerStatus(newSwitchDomain.getOverriddenServerStatus());
switchDomain.setServerMode(newSwitchDomain.getServerMode());
switchDomain.setDefaultInstanceEphemeral(newSwitchDomain.isDefaultInstanceEphemeral());
}
public SwitchDomain getSwitchDomain() {

View File

@ -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);

View File

@ -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);

View File

@ -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<String> headers = new ArrayList<String>();
headers.add("User-Agent");
@ -177,15 +177,5 @@ public class NamingBase {
HttpClient.request(url, headers, new HashMap<String, String>(), "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<String>();
headers.add("User-Agent");
headers.add("Nacos-Server");
result =
HttpClient.request(url, headers, new HashMap<String, String>(), "UTF-8", "PUT");
Assert.assertEquals(HttpStatus.SC_OK, result.code);
}
}