From 3d684b6f568c6f89b03e459f368e52dedc096610 Mon Sep 17 00:00:00 2001 From: nkorange Date: Tue, 30 Apr 2019 16:18:35 +0800 Subject: [PATCH] Fix #1091 --- .../ephemeral/distro/DataSyncer.java | 29 +++++++------------ .../healthcheck/ClientBeatCheckTask.java | 4 +-- .../naming/misc/ServerStatusSynchronizer.java | 2 +- .../nacos/naming/web/DistroFilter.java | 11 +++++++ 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DataSyncer.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DataSyncer.java index 75b0be45e..af4107f88 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DataSyncer.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DataSyncer.java @@ -17,7 +17,6 @@ package com.alibaba.nacos.naming.consistency.ephemeral.distro; import com.alibaba.nacos.naming.cluster.ServerListManager; import com.alibaba.nacos.naming.cluster.servers.Server; -import com.alibaba.nacos.naming.cluster.servers.ServerChangeListener; import com.alibaba.nacos.naming.cluster.transport.Serializer; import com.alibaba.nacos.naming.consistency.Datum; import com.alibaba.nacos.naming.consistency.KeyBuilder; @@ -43,7 +42,7 @@ import java.util.concurrent.ConcurrentHashMap; */ @Component @DependsOn("serverListManager") -public class DataSyncer implements ServerChangeListener { +public class DataSyncer { @Autowired private DataStore dataStore; @@ -62,11 +61,8 @@ public class DataSyncer implements ServerChangeListener { private Map taskMap = new ConcurrentHashMap<>(); - private List servers; - @PostConstruct public void init() { - serverListManager.listen(this); startTimedSync(); } @@ -97,7 +93,7 @@ public class DataSyncer implements ServerChangeListener { public void run() { try { - if (servers == null || servers.isEmpty()) { + if (getServers() == null || getServers().isEmpty()) { Loggers.SRV_LOG.warn("try to sync data but server list is empty."); return; } @@ -148,7 +144,7 @@ public class DataSyncer implements ServerChangeListener { Server server = new Server(); server.setIp(syncTask.getTargetServer().split(":")[0]); server.setServePort(Integer.parseInt(syncTask.getTargetServer().split(":")[1])); - if (!servers.contains(server)) { + if (!getServers().contains(server)) { // if server is no longer in healthy server list, ignore this task: return; } @@ -167,6 +163,11 @@ public class DataSyncer implements ServerChangeListener { public void run() { try { + + if (Loggers.EPHEMERAL.isDebugEnabled()) { + Loggers.EPHEMERAL.debug("server list is: {}", getServers()); + } + // send local timestamps to other servers: Map keyChecksums = new HashMap<>(64); for (String key : dataStore.keys()) { @@ -185,7 +186,7 @@ public class DataSyncer implements ServerChangeListener { Loggers.EPHEMERAL.debug("sync checksums: {}", keyChecksums); } - for (Server member : servers) { + for (Server member : getServers()) { if (NetUtils.localServer().equals(member.getKey())) { continue; } @@ -198,20 +199,10 @@ public class DataSyncer implements ServerChangeListener { } public List getServers() { - return servers; + return serverListManager.getHealthyServers(); } public String buildKey(String key, String targetServer) { return key + UtilsAndCommons.CACHE_KEY_SPLITER + targetServer; } - - @Override - public void onChangeServerList(List latestMembers) { - - } - - @Override - public void onChangeHealthyServerList(List healthServers) { - servers = healthServers; - } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java index 70f19b554..50fa3a628 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java @@ -77,8 +77,8 @@ public class ClientBeatCheckTask implements Runnable { if (!instance.isMarked()) { if (instance.isHealthy()) { instance.setHealthy(false); - Loggers.EVT_LOG.info("{POS} {IP-DISABLED} valid: {}:{}@{}, region: {}, msg: client timeout after {}, last beat: {}", - instance.getIp(), instance.getPort(), instance.getClusterName(), + Loggers.EVT_LOG.info("{POS} {IP-DISABLED} valid: {}:{}@{}@{}, region: {}, msg: client timeout after {}, last beat: {}", + instance.getIp(), instance.getPort(), instance.getClusterName(), service.getName(), UtilsAndCommons.LOCALHOST_SITE, ClientBeatProcessor.CLIENT_BEAT_TIMEOUT, instance.getLastBeat()); getPushService().serviceChanged(service.getNamespaceId(), service.getName()); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/ServerStatusSynchronizer.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/ServerStatusSynchronizer.java index 0f4813687..d7b2c0a46 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/ServerStatusSynchronizer.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/ServerStatusSynchronizer.java @@ -61,7 +61,7 @@ public class ServerStatusSynchronizer implements Synchronizer { } }); } catch (Exception e) { - Loggers.SRV_LOG.warn("[STATUS-SYNCHRONIZE] failed to request serverStatus, remote server: " + serverIP, e); + Loggers.SRV_LOG.warn("[STATUS-SYNCHRONIZE] failed to request serverStatus, remote server: {}", serverIP, e); } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java b/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java index 67b32405c..0c83a3d2a 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.naming.web; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; +import com.alibaba.nacos.naming.boot.SpringContext; import com.alibaba.nacos.naming.core.DistroMapper; import com.alibaba.nacos.naming.misc.HttpClient; import com.alibaba.nacos.naming.misc.Loggers; @@ -97,6 +98,16 @@ public class DistroFilter implements Filter { // proxy request to other server if necessary: if (method.isAnnotationPresent(CanDistro.class) && !distroMapper.responsible(groupedServiceName)) { + String userAgent = req.getHeader("User-Agent"); + + if (StringUtils.isNotBlank(userAgent) && userAgent.contains(UtilsAndCommons.NACOS_SERVER_HEADER)) { + // This request is sent from peer server, should not be redirected again: + Loggers.SRV_LOG.error("receive invalid redirect request from peer {}", req.getRemoteAddr()); + resp.sendError(HttpServletResponse.SC_BAD_REQUEST, + "receive invalid redirect request from peer " + req.getRemoteAddr()); + return; + } + List headerList = new ArrayList<>(16); Enumeration headers = req.getHeaderNames(); while (headers.hasMoreElements()) {