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

repair the NPE and Optimize related code #1699

Merged
merged 21 commits into from Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
25a87a8
Subject to the actual startup context path
pbting Aug 6, 2019
da8e09f
if not set the context path with the WebServerInitializedEvent then r…
pbting Aug 6, 2019
19a5ac6
RunningConfig support get from spring.properties configuration file
pbting Aug 6, 2019
f09909c
1. Optimize log printing
pbting Aug 9, 2019
6496ae5
support datum is null case
pbting Aug 9, 2019
68f170c
repair httpGetLarge#httpGetLarge will call entity.getContentType().ge…
pbting Aug 9, 2019
7c5b002
Normalize http response entity with ResponseEntity by spring
pbting Aug 9, 2019
be7237e
merge upstream develop the latest code
pbting Aug 15, 2019
5222b9c
cluster conf support multi instance inline seperator with ','
pbting Aug 16, 2019
3bd5dcb
add comma division with some case to use
pbting Aug 16, 2019
a6deeb1
add comma division with some case to use
pbting Aug 16, 2019
caf0130
add comma division with some case to use
pbting Aug 16, 2019
ce66b46
resolve conflict
pbting Aug 16, 2019
17ab94d
Merge branch 'develop' into feature_enhance_interface
pbting Aug 28, 2019
46bcece
repair speel error and add debug log
pbting Sep 6, 2019
5e7be63
Merge branch 'feature_enhance_interface' of https://github.com/alibab…
pbting Sep 6, 2019
2431eda
Merge branch 'develop' into feature_enhance_interface
pbting Sep 6, 2019
1f1392b
fix #1609
pbting Sep 6, 2019
d2383e6
Merge branch 'feature_enhance_interface' of https://github.com/alibab…
pbting Sep 6, 2019
56c439e
fix #1609
pbting Sep 6, 2019
1ecd5f3
Merge branch 'develop' into feature_enhance_interface
pbting Sep 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -162,6 +162,7 @@ public class Constants {
public static final String NULL_STRING = "null";

public static final String NUMBER_PATTERN = "^\\d+$";

public static final String ANY_PATTERN = ".*";

}
11 changes: 1 addition & 10 deletions console/src/main/resources/application.properties
@@ -1,39 +1,30 @@
# spring

server.servlet.contextPath=/nacos
server.servlet.context-path=/nacos
server.port=8848

nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false


# metrics for prometheus
#management.endpoints.web.exposure.include=*

# metrics for elastic search
management.metrics.export.elastic.enabled=false
#management.metrics.export.elastic.host=http://localhost:9200

# metrics for influx
management.metrics.export.influx.enabled=false
#management.metrics.export.influx.db=springboot
#management.metrics.export.influx.uri=http://localhost:8086
#management.metrics.export.influx.auto-create-db=true
#management.metrics.export.influx.consistency=one
#management.metrics.export.influx.compressed=true

server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D
# default current work dir
server.tomcat.basedir=

## spring security config
### turn off security
#spring.security.enabled=false
#management.security=false
#security.basic.enabled=false
#nacos.security.ignore.urls=/**

nacos.security.ignore.urls=/,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/login,/v1/console/health/**,/v1/cs/**,/v1/ns/**,/v1/cmdb/**,/actuator/**,/v1/console/server/**
Expand Up @@ -33,7 +33,7 @@ public interface Constants {
*/
String STANDALONE_MODE_PROPERTY_NAME = "nacos.standalone";

/**
/**
* The System property name of Function mode
*/
String FUNCTION_MODE_PROPERTY_NAME = "nacos.functionMode";
Expand All @@ -43,6 +43,11 @@ public interface Constants {
*/
String PREFER_HOSTNAME_OVER_IP_PROPERTY_NAME = "nacos.preferHostnameOverIp";

/**
* the root context path
*/
String ROOT_WEB_CONTEXT_PATH = "/";

String NACOS_SERVER_IP = "nacos.server.ip";

String USE_ONLY_SITE_INTERFACES = "nacos.inetutils.use-only-site-local-interfaces";
Expand All @@ -51,4 +56,6 @@ public interface Constants {
String IP_ADDRESS = "nacos.inetutils.ip-address";
String PREFER_HOSTNAME_OVER_IP = "nacos.inetutils.prefer-hostname-over-ip";
String SYSTEM_PREFER_HOSTNAME_OVER_IP = "nacos.preferHostnameOverIp";
String WEB_CONTEXT_PATH = "server.servlet.context-path";
String COMMA_DIVISION = ",";
}
11 changes: 10 additions & 1 deletion core/src/main/java/com/alibaba/nacos/core/utils/SystemUtils.java
Expand Up @@ -139,7 +139,16 @@ public static List<String> readClusterConf() throws IOException {
instance = instance.substring(0, instance.indexOf(comment));
instance = instance.trim();
}
instanceList.add(instance);
int multiIndex = instance.indexOf(Constants.COMMA_DIVISION);
if (multiIndex > 0) {
// support the format: ip1:port,ip2:port # multi inline
for (String ins : instance.split(Constants.COMMA_DIVISION)) {
instanceList.add(ins);
}
} else {
//support the format: 192.168.71.52:8848
instanceList.add(instance);
}
}
return instanceList;
} finally {
Expand Down
7 changes: 6 additions & 1 deletion distribution/bin/startup.cmd
Expand Up @@ -26,20 +26,25 @@ set CUSTOM_SEARCH_LOCATIONS=%DEFAULT_SEARCH_LOCATIONS%,file:%BASE_DIR%/conf/

set MODE="standalone"
set FUNCTION_MODE="all"
set SERVER="nacos-server"
set MODE_INDEX=-1
set FUNCTION_MODE_INDEX=-1
set SERVER_INDEX=-1


set i=0
for %%a in (%*) do (
if "%%a" == "-m" ( set /a MODE_INDEX=!i!+1 )
if "%%a" == "-f" ( set /a FUNCTION_MODE_INDEX=!i!+1 )
if "%%a" == "-s" ( set /a SERVER_INDEX=!i!+1 )
set /a i+=1
)

set i=0
for %%a in (%*) do (
if %MODE_INDEX% == !i! ( set MODE="%%a" )
if %FUNCTION_MODE_INDEX% == !i! ( set FUNCTION_MODE="%%a" )
if %SERVER_INDEX% == !i! (set SERVER="%%a")
set /a i+=1
)

Expand All @@ -62,7 +67,7 @@ if %FUNCTION_MODE% == "naming" (

set "JAVA_OPT=%JAVA_OPT% -Xbootclasspath/a:%BASE_DIR%\plugins\cmdb:%BASE_DIR%\plugins\mysql"
set "JAVA_OPT=%JAVA_OPT% -Dnacos.home=%BASE_DIR%"
set "JAVA_OPT=%JAVA_OPT% -Dloader.path=%BASE_DIR%/plugins/health -jar %BASE_DIR%\target\nacos-server.jar"
set "JAVA_OPT=%JAVA_OPT% -Dloader.path=%BASE_DIR%/plugins/health -jar %BASE_DIR%\target\%SERVER%.jar"
set "JAVA_OPT=%JAVA_OPT% --spring.config.location=%CUSTOM_SEARCH_LOCATIONS%"
set "JAVA_OPT=%JAVA_OPT% --logging.config=%BASE_DIR%/conf/nacos-logback.xml"

Expand Down
Expand Up @@ -15,8 +15,9 @@
*/
package com.alibaba.nacos.naming.boot;

import com.alibaba.nacos.core.utils.Constants;
import com.alibaba.nacos.core.utils.PropertyUtil;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.context.WebServerInitializedEvent;
Expand All @@ -38,6 +39,8 @@ public class RunningConfig implements ApplicationListener<WebServerInitializedEv
@Autowired
private ServletContext servletContext;

private static volatile boolean isServerInitialized = false;

@Override
public void onApplicationEvent(WebServerInitializedEvent event) {

Expand All @@ -46,6 +49,7 @@ public void onApplicationEvent(WebServerInitializedEvent event) {

serverPort = event.getWebServer().getPort();
contextPath = servletContext.getContextPath();
isServerInitialized = true;
}

public static int getServerPort() {
Expand All @@ -54,8 +58,13 @@ public static int getServerPort() {

public static String getContextPath() {

if (StringUtils.isBlank(contextPath)) {
return UtilsAndCommons.NACOS_SERVER_CONTEXT;
if (!isServerInitialized) {
String contextPath = PropertyUtil.getProperty(Constants.WEB_CONTEXT_PATH);
if (Constants.ROOT_WEB_CONTEXT_PATH.equals(contextPath)) {
return StringUtils.EMPTY;
} else {
return contextPath;
}
}
return contextPath;
}
Expand Down
Expand Up @@ -59,10 +59,11 @@ public boolean contains(String key) {
public Map<String, Datum> batchGet(List<String> keys) {
Map<String, Datum> map = new HashMap<>(128);
for (String key : keys) {
if (!dataMap.containsKey(key)) {
Datum datum = dataMap.get(key);
if (datum == null) {
continue;
}
map.put(key, dataMap.get(key));
map.put(key, datum);
}
return map;
}
Expand Down
Expand Up @@ -88,52 +88,43 @@ public void submit(SyncTask task, long delay) {
return;
}

GlobalExecutor.submitDataSync(new Runnable() {
@Override
public void run() {

try {
if (getServers() == null || getServers().isEmpty()) {
Loggers.SRV_LOG.warn("try to sync data but server list is empty.");
return;
}

List<String> keys = task.getKeys();

if (Loggers.DISTRO.isDebugEnabled()) {
Loggers.DISTRO.debug("sync keys: {}", keys);
}

Map<String, Datum> datumMap = dataStore.batchGet(keys);
GlobalExecutor.submitDataSync(() -> {
// 1. check the server
if (getServers() == null || getServers().isEmpty()) {
Loggers.SRV_LOG.warn("try to sync data but server list is empty.");
return;
}

if (datumMap == null || datumMap.isEmpty()) {
// clear all flags of this task:
for (String key : task.getKeys()) {
taskMap.remove(buildKey(key, task.getTargetServer()));
}
return;
}
List<String> keys = task.getKeys();

byte[] data = serializer.serialize(datumMap);

long timestamp = System.currentTimeMillis();
boolean success = NamingProxy.syncData(data, task.getTargetServer());
if (!success) {
SyncTask syncTask = new SyncTask();
syncTask.setKeys(task.getKeys());
syncTask.setRetryCount(task.getRetryCount() + 1);
syncTask.setLastExecuteTime(timestamp);
syncTask.setTargetServer(task.getTargetServer());
retrySync(syncTask);
} else {
// clear all flags of this task:
for (String key : task.getKeys()) {
taskMap.remove(buildKey(key, task.getTargetServer()));
}
}
if (Loggers.SRV_LOG.isDebugEnabled()) {
Loggers.SRV_LOG.debug("try to sync data for this keys {}.", keys);
}
// 2. get the datums by keys and check the datum is empty or not
Map<String, Datum> datumMap = dataStore.batchGet(keys);
if (datumMap == null || datumMap.isEmpty()) {
// clear all flags of this task:
for (String key : keys) {
taskMap.remove(buildKey(key, task.getTargetServer()));
}
return;
}

} catch (Exception e) {
Loggers.DISTRO.error("sync data failed.", e);
byte[] data = serializer.serialize(datumMap);

long timestamp = System.currentTimeMillis();
boolean success = NamingProxy.syncData(data, task.getTargetServer());
if (!success) {
SyncTask syncTask = new SyncTask();
syncTask.setKeys(task.getKeys());
syncTask.setRetryCount(task.getRetryCount() + 1);
syncTask.setLastExecuteTime(timestamp);
syncTask.setTargetServer(task.getTargetServer());
retrySync(syncTask);
} else {
// clear all flags of this task:
for (String key : task.getKeys()) {
taskMap.remove(buildKey(key, task.getTargetServer()));
}
}
}, delay);
Expand Down Expand Up @@ -175,7 +166,11 @@ public void run() {
continue;
}

keyChecksums.put(key, dataStore.get(key).value.getChecksum());
Datum datum = dataStore.get(key);
if (datum == null) {
continue;
}
keyChecksums.put(key, datum.value.getChecksum());
}

if (keyChecksums.isEmpty()) {
Expand All @@ -196,6 +191,7 @@ public void run() {
Loggers.DISTRO.error("timed sync task failed.", e);
}
}

}

public List<Server> getServers() {
Expand Down
Expand Up @@ -211,6 +211,7 @@ public void onReceiveChecksums(Map<String, String> checksumMap, String server) {
// abort the procedure:
return;
}

if (!dataStore.contains(entry.getKey()) ||
dataStore.get(entry.getKey()).value == null ||
!dataStore.get(entry.getKey()).value.getChecksum().equals(entry.getValue())) {
Expand Down
Expand Up @@ -32,13 +32,13 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -69,7 +69,7 @@ public class DistroController {
private SwitchDomain switchDomain;

@RequestMapping(value = "/datum", method = RequestMethod.PUT)
public String onSyncDatum(HttpServletRequest request, HttpServletResponse response) throws Exception {
public ResponseEntity onSyncDatum(HttpServletRequest request) throws Exception {

String entity = IOUtils.toString(request.getInputStream(), "UTF-8");

Expand All @@ -92,35 +92,42 @@ public String onSyncDatum(HttpServletRequest request, HttpServletResponse respon
consistencyService.onPut(entry.getKey(), entry.getValue().value);
}
}
return "ok";
return ResponseEntity.ok("ok");
}

@RequestMapping(value = "/checksum", method = RequestMethod.PUT)
public String syncChecksum(HttpServletRequest request, HttpServletResponse response) throws Exception {
public ResponseEntity syncChecksum(HttpServletRequest request, HttpServletResponse response) throws Exception {
String source = WebUtils.required(request, "source");
String entity = IOUtils.toString(request.getInputStream(), "UTF-8");
Map<String, String> dataMap =
serializer.deserialize(entity.getBytes(), new TypeReference<Map<String, String>>() {
});
});
consistencyService.onReceiveChecksums(dataMap, source);
return "ok";
return ResponseEntity.ok("ok");
}

@RequestMapping(value = "/datum", method = RequestMethod.GET)
public void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
public ResponseEntity get(HttpServletRequest request, HttpServletResponse response) throws Exception {

String entity = IOUtils.toString(request.getInputStream(), "UTF-8");
String keys = JSON.parseObject(entity).getString("keys");
String keySplitter = ",";
Map<String, Datum> datumMap = new HashMap<>(64);
for (String key : keys.split(keySplitter)) {
datumMap.put(key, consistencyService.get(key));
Datum datum = consistencyService.get(key);
if (datum == null) {
continue;
}
datumMap.put(key, datum);
}
response.getWriter().write(new String(serializer.serialize(datumMap), StandardCharsets.UTF_8));

String content = new String(serializer.serialize(datumMap), StandardCharsets.UTF_8);
return ResponseEntity.ok(content);
}

@RequestMapping(value = "/datums", method = RequestMethod.GET)
public void getAllDatums(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.getWriter().write(new String(serializer.serialize(dataStore.getDataMap()), StandardCharsets.UTF_8));
public ResponseEntity getAllDatums(HttpServletRequest request, HttpServletResponse response) throws Exception {
String content = new String(serializer.serialize(dataStore.getDataMap()), StandardCharsets.UTF_8);
return ResponseEntity.ok(content);
}
}