#502 Fix several bugs

This commit is contained in:
nkorange 2019-03-18 20:57:28 +08:00
parent 3f2d704691
commit 52d4e01bc7
18 changed files with 104 additions and 29 deletions

View File

@ -16,7 +16,7 @@
<parent> <parent>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId> <artifactId>nacos-all</artifactId>
<version>1.0.0-RC1</version> <version>1.0.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -16,7 +16,7 @@
<parent> <parent>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId> <artifactId>nacos-all</artifactId>
<version>1.0.0-RC1</version> <version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>

View File

@ -18,7 +18,7 @@
<parent> <parent>
<artifactId>nacos-all</artifactId> <artifactId>nacos-all</artifactId>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<version>1.0.0-RC1</version> <version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -18,7 +18,7 @@
<parent> <parent>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId> <artifactId>nacos-all</artifactId>
<version>1.0.0-RC1</version> <version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -17,7 +17,7 @@
<parent> <parent>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId> <artifactId>nacos-all</artifactId>
<version>1.0.0-RC1</version> <version>1.0.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -18,7 +18,7 @@
<parent> <parent>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId> <artifactId>nacos-all</artifactId>
<version>1.0.0-RC1</version> <version>1.0.0-SNAPSHOT</version>
</parent> </parent>
<artifactId>nacos-console</artifactId> <artifactId>nacos-console</artifactId>
<!--<packaging>war</packaging>--> <!--<packaging>war</packaging>-->

View File

@ -18,7 +18,7 @@
<parent> <parent>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId> <artifactId>nacos-all</artifactId>
<version>1.0.0-RC1</version> <version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>

View File

@ -18,7 +18,7 @@
<parent> <parent>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId> <artifactId>nacos-all</artifactId>
<version>1.0.0-RC1</version> <version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>

View File

@ -18,7 +18,7 @@
<parent> <parent>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId> <artifactId>nacos-all</artifactId>
<version>1.0.0-RC1</version> <version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>

View File

@ -18,7 +18,7 @@
<parent> <parent>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId> <artifactId>nacos-all</artifactId>
<version>1.0.0-RC1</version> <version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>

View File

@ -102,6 +102,8 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
private volatile Map<String, CopyOnWriteArrayList<RecordListener>> listeners = new ConcurrentHashMap<>(); private volatile Map<String, CopyOnWriteArrayList<RecordListener>> listeners = new ConcurrentHashMap<>();
private volatile Map<String, String> syncChecksumTasks = new ConcurrentHashMap<>(16);
@PostConstruct @PostConstruct
public void init() throws Exception { public void init() throws Exception {
GlobalExecutor.submit(new Runnable() { GlobalExecutor.submit(new Runnable() {
@ -190,6 +192,14 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
public void onReceiveChecksums(Map<String, String> checksumMap, String server) { public void onReceiveChecksums(Map<String, String> checksumMap, String server) {
if (syncChecksumTasks.containsKey(server)) {
// Already in process of this server:
Loggers.EPHEMERAL.warn("sync checksum task already in process with {}", server);
return;
}
syncChecksumTasks.put(server, "1");
List<String> toUpdateKeys = new ArrayList<>(); List<String> toUpdateKeys = new ArrayList<>();
List<String> toRemoveKeys = new ArrayList<>(); List<String> toRemoveKeys = new ArrayList<>();
for (Map.Entry<String, String> entry : checksumMap.entrySet()) { for (Map.Entry<String, String> entry : checksumMap.entrySet()) {
@ -234,6 +244,8 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
Loggers.EPHEMERAL.error("get data from " + server + " failed!", e); Loggers.EPHEMERAL.error("get data from " + server + " failed!", e);
} }
// Remove this 'in process' flag:
syncChecksumTasks.remove(server);
} }
public boolean syncAllDataFromRemote(Server server) { public boolean syncAllDataFromRemote(Server server) {
@ -261,6 +273,7 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
// pretty sure the service not exist: // pretty sure the service not exist:
if (ServerMode.AP.name().equals(switchDomain.getServerMode())) { if (ServerMode.AP.name().equals(switchDomain.getServerMode())) {
// create empty service // create empty service
Loggers.EPHEMERAL.info("creating service {}", entry.getKey());
Service service = new Service(); Service service = new Service();
String serviceName = KeyBuilder.getServiceName(entry.getKey()); String serviceName = KeyBuilder.getServiceName(entry.getKey());
String namespaceId = KeyBuilder.getNamespace(entry.getKey()); String namespaceId = KeyBuilder.getNamespace(entry.getKey());
@ -277,19 +290,24 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
} }
for (Map.Entry<String, Datum<Instances>> entry : datumMap.entrySet()) { for (Map.Entry<String, Datum<Instances>> entry : datumMap.entrySet()) {
dataStore.put(entry.getKey(), entry.getValue());
if (!listeners.containsKey(entry.getKey())) { if (!listeners.containsKey(entry.getKey())) {
Loggers.EPHEMERAL.warn("listener not found: {}", entry.getKey()); // Should not happen:
Loggers.EPHEMERAL.warn("listener of {} not found.", entry.getKey());
continue; continue;
} }
for (RecordListener listener : listeners.get(entry.getKey())) {
try { try {
for (RecordListener listener : listeners.get(entry.getKey())) {
listener.onChange(entry.getKey(), entry.getValue().value); listener.onChange(entry.getKey(), entry.getValue().value);
}
} catch (Exception e) { } catch (Exception e) {
Loggers.EPHEMERAL.error("notify " + listener + ", key: " + entry.getKey() + " failed.", e); Loggers.EPHEMERAL.error("[NACOS-DISTRO] error while execute listener of key: {}", entry.getKey(), e);
} continue;
} }
// Update data store if listener executed successfully:
dataStore.put(entry.getKey(), entry.getValue());
} }
} }
} }
@ -384,7 +402,7 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
continue; continue;
} }
} catch (Throwable e) { } catch (Throwable e) {
Loggers.EPHEMERAL.error("[NACOS-DISTRO] error while notifying listener of key: {} {}", datumKey, e); Loggers.EPHEMERAL.error("[NACOS-DISTRO] error while notifying listener of key: {}", datumKey, e);
} }
} }

View File

@ -15,6 +15,7 @@
*/ */
package com.alibaba.nacos.naming.controllers; package com.alibaba.nacos.naming.controllers;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.core.utils.WebUtils; import com.alibaba.nacos.core.utils.WebUtils;
import com.alibaba.nacos.naming.cluster.ServerMode; import com.alibaba.nacos.naming.cluster.ServerMode;
@ -106,7 +107,9 @@ public class DistroController {
@RequestMapping(value = "/datum", method = RequestMethod.GET) @RequestMapping(value = "/datum", method = RequestMethod.GET)
public void get(HttpServletRequest request, HttpServletResponse response) throws Exception { public void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
String keys = WebUtils.required(request, "keys");
String entity = IOUtils.toString(request.getInputStream(), "UTF-8");
String keys = JSON.parseObject(entity).getString("keys");
String keySplitter = ","; String keySplitter = ",";
Map<String, Datum> datumMap = new HashMap<>(64); Map<String, Datum> datumMap = new HashMap<>(64);
for (String key : keys.split(keySplitter)) { for (String key : keys.split(keySplitter)) {

View File

@ -18,8 +18,8 @@ package com.alibaba.nacos.naming.core;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.nacos.naming.boot.SpringContext; import com.alibaba.nacos.naming.boot.SpringContext;
import com.alibaba.nacos.naming.consistency.RecordListener;
import com.alibaba.nacos.naming.consistency.KeyBuilder; import com.alibaba.nacos.naming.consistency.KeyBuilder;
import com.alibaba.nacos.naming.consistency.RecordListener;
import com.alibaba.nacos.naming.healthcheck.ClientBeatCheckTask; import com.alibaba.nacos.naming.healthcheck.ClientBeatCheckTask;
import com.alibaba.nacos.naming.healthcheck.ClientBeatProcessor; import com.alibaba.nacos.naming.healthcheck.ClientBeatProcessor;
import com.alibaba.nacos.naming.healthcheck.HealthCheckReactor; import com.alibaba.nacos.naming.healthcheck.HealthCheckReactor;
@ -156,14 +156,19 @@ public class Service extends com.alibaba.nacos.api.naming.pojo.Service implement
Loggers.SRV_LOG.info("[NACOS-RAFT] datum is changed, key: {}, value: {}", key, value); Loggers.SRV_LOG.info("[NACOS-RAFT] datum is changed, key: {}, value: {}", key, value);
for (Instance ip : value.getInstanceList()) { for (Instance instance : value.getInstanceList()) {
if (ip.getWeight() > 10000.0D) { if (instance == null) {
ip.setWeight(10000.0D); // Reject this abnormal instance list:
throw new RuntimeException("got null instance " + key);
} }
if (ip.getWeight() < 0.01D && ip.getWeight() > 0.0D) { if (instance.getWeight() > 10000.0D) {
ip.setWeight(0.01D); instance.setWeight(10000.0D);
}
if (instance.getWeight() < 0.01D && instance.getWeight() > 0.0D) {
instance.setWeight(0.01D);
} }
} }

View File

@ -79,6 +79,8 @@ public class ServiceManager implements RecordListener<Service> {
@Autowired @Autowired
private PushService pushService; private PushService pushService;
private final Object putServiceLock = new Object();
@PostConstruct @PostConstruct
public void init() { public void init() {
@ -330,6 +332,7 @@ public class ServiceManager implements RecordListener<Service> {
public void createEmptyService(String namespaceId, String serviceName) throws NacosException { public void createEmptyService(String namespaceId, String serviceName) throws NacosException {
Service service = getService(namespaceId, serviceName); Service service = getService(namespaceId, serviceName);
if (service == null) { if (service == null) {
Loggers.SRV_LOG.info("creating empty service {}:{}", namespaceId, serviceName);
service = new Service(); service = new Service();
service.setName(serviceName); service.setName(serviceName);
service.setNamespaceId(namespaceId); service.setNamespaceId(namespaceId);
@ -520,9 +523,13 @@ public class ServiceManager implements RecordListener<Service> {
} }
public void putService(Service service) { public void putService(Service service) {
if (!serviceMap.containsKey(service.getNamespaceId())) {
synchronized (putServiceLock) {
if (!serviceMap.containsKey(service.getNamespaceId())) { if (!serviceMap.containsKey(service.getNamespaceId())) {
serviceMap.put(service.getNamespaceId(), new ConcurrentHashMap<>(16)); serviceMap.put(service.getNamespaceId(), new ConcurrentHashMap<>(16));
} }
}
}
serviceMap.get(service.getNamespaceId()).put(service.getName(), service); serviceMap.get(service.getNamespaceId()).put(service.getName(), service);
} }

View File

@ -27,6 +27,8 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.http.*; import org.apache.http.*;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
@ -41,6 +43,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.*; import java.util.*;
@ -313,6 +316,35 @@ public class HttpClient {
} }
} }
public static HttpResult httpGetLarge(String url, Map<String, String> headers, String content) {
try {
HttpClientBuilder builder = HttpClients.custom();
builder.setUserAgent(UtilsAndCommons.SERVER_VERSION);
builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS);
CloseableHttpClient httpClient = builder.build();
HttpGetWithEntity httpGetWithEntity = new HttpGetWithEntity();
httpGetWithEntity.setURI(new URI(url));
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpGetWithEntity.setHeader(entry.getKey(), entry.getValue());
}
httpGetWithEntity.setEntity(new StringEntity(content, ContentType.create("application/json", "UTF-8")));
HttpResponse response = httpClient.execute(httpGetWithEntity);
HttpEntity entity = response.getEntity();
HeaderElement[] headerElements = entity.getContentType().getElements();
String charset = headerElements[0].getParameterByName("charset").getValue();
return new HttpResult(response.getStatusLine().getStatusCode(),
IOUtils.toString(entity.getContent(), charset), Collections.<String, String>emptyMap());
} catch (Exception e) {
return new HttpResult(500, e.toString(), Collections.<String, String>emptyMap());
}
}
public static HttpResult httpPostLarge(String url, Map<String, String> headers, String content) { public static HttpResult httpPostLarge(String url, Map<String, String> headers, String content) {
try { try {
HttpClientBuilder builder = HttpClients.custom(); HttpClientBuilder builder = HttpClients.custom();
@ -442,4 +474,14 @@ public class HttpClient {
return respHeaders.get(name); return respHeaders.get(name);
} }
} }
public static class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
public final static String METHOD_NAME = "GET";
@Override
public String getMethod() {
return METHOD_NAME;
}
}
} }

View File

@ -79,8 +79,8 @@ public class NamingProxy {
Map<String, String> params = new HashMap<>(8); Map<String, String> params = new HashMap<>(8);
params.put("keys", StringUtils.join(keys, ",")); params.put("keys", StringUtils.join(keys, ","));
HttpClient.HttpResult result = HttpClient.httpGet("http://" + server + RunningConfig.getContextPath() HttpClient.HttpResult result = HttpClient.httpGetLarge("http://" + server + RunningConfig.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_GET_URL, new ArrayList<>(), params); + UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_GET_URL, new HashMap<>(8), JSON.toJSONString(params));
if (HttpURLConnection.HTTP_OK == result.code) { if (HttpURLConnection.HTTP_OK == result.code) {
return result.content.getBytes(); return result.content.getBytes();

View File

@ -21,7 +21,7 @@
<inceptionYear>2018</inceptionYear> <inceptionYear>2018</inceptionYear>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId> <artifactId>nacos-all</artifactId>
<version>1.0.0-RC1</version> <version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<name>Alibaba NACOS ${project.version}</name> <name>Alibaba NACOS ${project.version}</name>

View File

@ -17,7 +17,7 @@
<parent> <parent>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId> <artifactId>nacos-all</artifactId>
<version>1.0.0-RC1</version> <version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>