1. Optimize log printing
2. Improve the robustness and readability of your code
This commit is contained in:
parent
19a5ac632c
commit
f09909cd1f
@ -59,10 +59,11 @@ public class DataStore {
|
|||||||
public Map<String, Datum> batchGet(List<String> keys) {
|
public Map<String, Datum> batchGet(List<String> keys) {
|
||||||
Map<String, Datum> map = new HashMap<>(128);
|
Map<String, Datum> map = new HashMap<>(128);
|
||||||
for (String key : keys) {
|
for (String key : keys) {
|
||||||
if (!dataMap.containsKey(key)) {
|
Datum datum = dataMap.get(key);
|
||||||
|
if (datum == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
map.put(key, dataMap.get(key));
|
map.put(key, datum);
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
@ -88,34 +88,29 @@ public class DataSyncer {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
GlobalExecutor.submitDataSync(new Runnable() {
|
GlobalExecutor.submitDataSync(() -> {
|
||||||
@Override
|
// 1. check the server
|
||||||
public void run() {
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (getServers() == null || getServers().isEmpty()) {
|
if (getServers() == null || getServers().isEmpty()) {
|
||||||
Loggers.SRV_LOG.warn("try to sync data but server list is empty.");
|
Loggers.SRV_LOG.warn("try to sync data but server list is empty.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> keys = task.getKeys();
|
List<String> keys = task.getKeys();
|
||||||
|
|
||||||
if (Loggers.EPHEMERAL.isDebugEnabled()) {
|
if (Loggers.EPHEMERAL.isDebugEnabled()) {
|
||||||
Loggers.EPHEMERAL.debug("sync keys: {}", keys);
|
Loggers.EPHEMERAL.debug("sync keys: {}", keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. get the datums by keys and check the datum is empty or not
|
||||||
Map<String, Datum> datumMap = dataStore.batchGet(keys);
|
Map<String, Datum> datumMap = dataStore.batchGet(keys);
|
||||||
|
|
||||||
if (datumMap == null || datumMap.isEmpty()) {
|
if (datumMap == null || datumMap.isEmpty()) {
|
||||||
// clear all flags of this task:
|
// clear all flags of this task:
|
||||||
for (String key : task.getKeys()) {
|
for (String key : keys) {
|
||||||
taskMap.remove(buildKey(key, task.getTargetServer()));
|
taskMap.remove(buildKey(key, task.getTargetServer()));
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] data = serializer.serialize(datumMap);
|
byte[] data = serializer.serialize(datumMap);
|
||||||
|
|
||||||
long timestamp = System.currentTimeMillis();
|
long timestamp = System.currentTimeMillis();
|
||||||
boolean success = NamingProxy.syncData(data, task.getTargetServer());
|
boolean success = NamingProxy.syncData(data, task.getTargetServer());
|
||||||
if (!success) {
|
if (!success) {
|
||||||
@ -131,11 +126,6 @@ public class DataSyncer {
|
|||||||
taskMap.remove(buildKey(key, task.getTargetServer()));
|
taskMap.remove(buildKey(key, task.getTargetServer()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
Loggers.EPHEMERAL.error("sync data failed.", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, delay);
|
}, delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,7 +139,6 @@ public class DataSyncer {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO may choose other retry policy.
|
|
||||||
submit(syncTask, partitionConfig.getSyncRetryDelay());
|
submit(syncTask, partitionConfig.getSyncRetryDelay());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,10 +153,6 @@ public class DataSyncer {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
if (Loggers.EPHEMERAL.isDebugEnabled()) {
|
|
||||||
Loggers.EPHEMERAL.debug("server list is: {}", getServers());
|
|
||||||
}
|
|
||||||
|
|
||||||
// send local timestamps to other servers:
|
// send local timestamps to other servers:
|
||||||
Map<String, String> keyChecksums = new HashMap<>(64);
|
Map<String, String> keyChecksums = new HashMap<>(64);
|
||||||
for (String key : dataStore.keys()) {
|
for (String key : dataStore.keys()) {
|
||||||
@ -175,7 +160,11 @@ public class DataSyncer {
|
|||||||
continue;
|
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()) {
|
if (keyChecksums.isEmpty()) {
|
||||||
|
@ -211,6 +211,7 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
|
|||||||
// abort the procedure:
|
// abort the procedure:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dataStore.contains(entry.getKey()) ||
|
if (!dataStore.contains(entry.getKey()) ||
|
||||||
dataStore.get(entry.getKey()).value == null ||
|
dataStore.get(entry.getKey()).value == null ||
|
||||||
!dataStore.get(entry.getKey()).value.getChecksum().equals(entry.getValue())) {
|
!dataStore.get(entry.getKey()).value.getChecksum().equals(entry.getValue())) {
|
||||||
|
@ -15,7 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.alibaba.nacos.naming.consistency.ephemeral.distro;
|
package com.alibaba.nacos.naming.consistency.ephemeral.distro;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
|
||||||
import com.alibaba.nacos.naming.cluster.servers.Server;
|
import com.alibaba.nacos.naming.cluster.servers.Server;
|
||||||
import com.alibaba.nacos.naming.misc.*;
|
import com.alibaba.nacos.naming.misc.*;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@ -125,7 +124,7 @@ public class TaskDispatcher {
|
|||||||
syncTask.setTargetServer(member.getKey());
|
syncTask.setTargetServer(member.getKey());
|
||||||
|
|
||||||
if (Loggers.EPHEMERAL.isDebugEnabled() && StringUtils.isNotBlank(key)) {
|
if (Loggers.EPHEMERAL.isDebugEnabled() && StringUtils.isNotBlank(key)) {
|
||||||
Loggers.EPHEMERAL.debug("add sync task: {}", JSON.toJSONString(syncTask));
|
Loggers.EPHEMERAL.debug("add sync task. task server is {}, and key size {}", syncTask.getTargetServer(), keys.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
dataSyncer.submit(syncTask, 0);
|
dataSyncer.submit(syncTask, 0);
|
||||||
|
@ -38,7 +38,6 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -69,7 +68,7 @@ public class DistroController {
|
|||||||
private SwitchDomain switchDomain;
|
private SwitchDomain switchDomain;
|
||||||
|
|
||||||
@RequestMapping(value = "/datum", method = RequestMethod.PUT)
|
@RequestMapping(value = "/datum", method = RequestMethod.PUT)
|
||||||
public String onSyncDatum(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
public String onSyncDatum(HttpServletRequest request) throws Exception {
|
||||||
|
|
||||||
String entity = IOUtils.toString(request.getInputStream(), "UTF-8");
|
String entity = IOUtils.toString(request.getInputStream(), "UTF-8");
|
||||||
|
|
||||||
@ -114,7 +113,11 @@ public class DistroController {
|
|||||||
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)) {
|
||||||
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));
|
response.getWriter().write(new String(serializer.serialize(datumMap), StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
|
@ -39,9 +39,16 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public class Instances implements Record {
|
public class Instances implements Record {
|
||||||
|
|
||||||
private String cachedChecksum;
|
private static MessageDigest MESSAGE_DIGEST;
|
||||||
|
|
||||||
private long lastCalculateTime = 0L;
|
static {
|
||||||
|
try {
|
||||||
|
MESSAGE_DIGEST = MessageDigest.getInstance("MD5");
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
Loggers.SRV_LOG.error("error while calculating checksum(md5) for instances", e);
|
||||||
|
MESSAGE_DIGEST = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private List<Instance> instanceList = new ArrayList<>();
|
private List<Instance> instanceList = new ArrayList<>();
|
||||||
|
|
||||||
@ -61,15 +68,12 @@ public class Instances implements Record {
|
|||||||
@Override
|
@Override
|
||||||
@JSONField(serialize = false)
|
@JSONField(serialize = false)
|
||||||
public String getChecksum() {
|
public String getChecksum() {
|
||||||
recalculateChecksum();
|
|
||||||
return cachedChecksum;
|
return recalculateChecksum();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCachedChecksum() {
|
private String recalculateChecksum() {
|
||||||
return cachedChecksum;
|
String checksum;
|
||||||
}
|
|
||||||
|
|
||||||
private void recalculateChecksum() {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
Collections.sort(instanceList);
|
Collections.sort(instanceList);
|
||||||
for (Instance ip : instanceList) {
|
for (Instance ip : instanceList) {
|
||||||
@ -78,16 +82,14 @@ public class Instances implements Record {
|
|||||||
sb.append(string);
|
sb.append(string);
|
||||||
sb.append(",");
|
sb.append(",");
|
||||||
}
|
}
|
||||||
MessageDigest md5;
|
|
||||||
try {
|
if (MESSAGE_DIGEST != null) {
|
||||||
md5 = MessageDigest.getInstance("MD5");
|
checksum =
|
||||||
cachedChecksum =
|
new BigInteger(1, MESSAGE_DIGEST.digest((sb.toString()).getBytes(Charset.forName("UTF-8")))).toString(16);
|
||||||
new BigInteger(1, md5.digest((sb.toString()).getBytes(Charset.forName("UTF-8")))).toString(16);
|
} else {
|
||||||
} catch (NoSuchAlgorithmException e) {
|
checksum = RandomStringUtils.randomAscii(32);
|
||||||
Loggers.SRV_LOG.error("error while calculating checksum(md5) for instances", e);
|
|
||||||
cachedChecksum = RandomStringUtils.randomAscii(32);
|
|
||||||
}
|
}
|
||||||
lastCalculateTime = System.currentTimeMillis();
|
return checksum;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String convertMap2String(Map<String, String> map) {
|
public String convertMap2String(Map<String, String> map) {
|
||||||
|
@ -537,20 +537,17 @@ public class ServiceManager implements RecordListener<Service> {
|
|||||||
|
|
||||||
Datum datum = consistencyService.get(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), ephemeral));
|
Datum datum = consistencyService.get(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), ephemeral));
|
||||||
|
|
||||||
Map<String, Instance> oldInstanceMap = new HashMap<>(16);
|
|
||||||
List<Instance> currentIPs = service.allIPs(ephemeral);
|
List<Instance> currentIPs = service.allIPs(ephemeral);
|
||||||
Map<String, Instance> map = new ConcurrentHashMap<>(currentIPs.size());
|
Map<String, Instance> currentInstances = new HashMap<>(currentIPs.size());
|
||||||
|
|
||||||
for (Instance instance : currentIPs) {
|
for (Instance instance : currentIPs) {
|
||||||
map.put(instance.toIPAddr(), instance);
|
currentInstances.put(instance.toIPAddr(), instance);
|
||||||
}
|
|
||||||
if (datum != null) {
|
|
||||||
oldInstanceMap = setValid(((Instances) datum.value).getInstanceList(), map);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// use HashMap for deep copy:
|
Map<String, Instance> instanceMap = null;
|
||||||
HashMap<String, Instance> instanceMap = new HashMap<>(oldInstanceMap.size());
|
if (datum != null) {
|
||||||
instanceMap.putAll(oldInstanceMap);
|
instanceMap = setValid(((Instances) datum.value).getInstanceList(), currentInstances);
|
||||||
|
}
|
||||||
|
|
||||||
for (Instance instance : ips) {
|
for (Instance instance : ips) {
|
||||||
if (!service.getClusterMap().containsKey(instance.getClusterName())) {
|
if (!service.getClusterMap().containsKey(instance.getClusterName())) {
|
||||||
|
@ -108,8 +108,7 @@ public class NamingProxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static boolean syncData(byte[] data, String curServer) throws Exception {
|
public static boolean syncData(byte[] data, String curServer) {
|
||||||
try {
|
|
||||||
Map<String, String> headers = new HashMap<>(128);
|
Map<String, String> headers = new HashMap<>(128);
|
||||||
|
|
||||||
headers.put("Client-Version", UtilsAndCommons.SERVER_VERSION);
|
headers.put("Client-Version", UtilsAndCommons.SERVER_VERSION);
|
||||||
@ -118,17 +117,15 @@ public class NamingProxy {
|
|||||||
headers.put("Connection", "Keep-Alive");
|
headers.put("Connection", "Keep-Alive");
|
||||||
headers.put("Content-Encoding", "gzip");
|
headers.put("Content-Encoding", "gzip");
|
||||||
|
|
||||||
|
try {
|
||||||
HttpClient.HttpResult result = HttpClient.httpPutLarge("http://" + curServer + RunningConfig.getContextPath()
|
HttpClient.HttpResult result = HttpClient.httpPutLarge("http://" + curServer + RunningConfig.getContextPath()
|
||||||
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_ON_SYNC_URL, headers, data);
|
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_ON_SYNC_URL, headers, data);
|
||||||
|
|
||||||
if (HttpURLConnection.HTTP_OK == result.code) {
|
if (HttpURLConnection.HTTP_OK == result.code) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) {
|
if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new IOException("failed to req API:" + "http://" + curServer
|
throw new IOException("failed to req API:" + "http://" + curServer
|
||||||
+ RunningConfig.getContextPath()
|
+ RunningConfig.getContextPath()
|
||||||
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_ON_SYNC_URL + ". code:"
|
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_ON_SYNC_URL + ". code:"
|
||||||
|
Loading…
Reference in New Issue
Block a user