Merge pull request #1828 from universefeeler/fixed_cluster_page_bug

[ISSUE#1602] Fixed cluster page show bug
This commit is contained in:
Fury Zhu 2019-09-17 14:28:02 +08:00 committed by GitHub
commit 1e9fefca34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 9 deletions

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.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.common.Constants;
@ -25,7 +26,6 @@ import com.alibaba.nacos.naming.cluster.ServerListManager;
import com.alibaba.nacos.naming.cluster.ServerStatusManager; import com.alibaba.nacos.naming.cluster.ServerStatusManager;
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftCore; import com.alibaba.nacos.naming.consistency.persistent.raft.RaftCore;
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer; import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer;
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet;
import com.alibaba.nacos.naming.core.DistroMapper; import com.alibaba.nacos.naming.core.DistroMapper;
import com.alibaba.nacos.naming.core.Service; import com.alibaba.nacos.naming.core.Service;
import com.alibaba.nacos.naming.core.ServiceManager; import com.alibaba.nacos.naming.core.ServiceManager;
@ -41,6 +41,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -79,9 +80,6 @@ public class OperatorController {
@Autowired @Autowired
private RaftCore raftCore; private RaftCore raftCore;
@Autowired
private RaftPeerSet raftPeerSet;
@RequestMapping("/push/state") @RequestMapping("/push/state")
public JSONObject pushState(HttpServletRequest request) { public JSONObject pushState(HttpServletRequest request) {
@ -238,11 +236,10 @@ public class OperatorController {
int page = Integer.parseInt(WebUtils.required(request, "pageNo")); int page = Integer.parseInt(WebUtils.required(request, "pageNo"));
int pageSize = Integer.parseInt(WebUtils.required(request, "pageSize")); int pageSize = Integer.parseInt(WebUtils.required(request, "pageSize"));
String keyword = WebUtils.optional(request, "keyword", StringUtils.EMPTY); String keyword = WebUtils.optional(request, "keyword", StringUtils.EMPTY);
String containedInstance = WebUtils.optional(request, "instance", StringUtils.EMPTY);
List<RaftPeer> raftPeerLists = new ArrayList<>(); List<RaftPeer> raftPeerLists = new ArrayList<>();
int total = serviceManager.getPagedClusterState(namespaceId, page - 1, pageSize, keyword, containedInstance, raftPeerLists, raftPeerSet); int total = serviceManager.getPagedClusterState(namespaceId, page - 1, pageSize, keyword, raftPeerLists);
if (CollectionUtils.isEmpty(raftPeerLists)) { if (CollectionUtils.isEmpty(raftPeerLists)) {
result.put("clusterStateList", Collections.emptyList()); result.put("clusterStateList", Collections.emptyList());
@ -265,4 +262,13 @@ public class OperatorController {
result.put("count", total); result.put("count", total);
return result; return result;
} }
@RequestMapping(value = "/cluster/state", method = RequestMethod.GET)
public JSONObject getClusterStates() {
RaftPeer peer = serviceManager.getMySelfClusterState();
return JSON.parseObject(JSON.toJSONString(peer));
}
} }

View File

@ -31,6 +31,7 @@ import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer;
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet; import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet;
import com.alibaba.nacos.naming.misc.*; import com.alibaba.nacos.naming.misc.*;
import com.alibaba.nacos.naming.push.PushService; import com.alibaba.nacos.naming.push.PushService;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -83,6 +84,9 @@ public class ServiceManager implements RecordListener<Service> {
@Autowired @Autowired
private PushService pushService; private PushService pushService;
@Autowired
private RaftPeerSet raftPeerSet;
private final Object putServiceLock = new Object(); private final Object putServiceLock = new Object();
@PostConstruct @PostConstruct
@ -230,10 +234,30 @@ public class ServiceManager implements RecordListener<Service> {
} }
} }
public int getPagedClusterState(String namespaceId, int startPage, int pageSize, String keyword, String containedInstance, List<RaftPeer> raftPeerList, RaftPeerSet raftPeerSet) { public int getPagedClusterState(String namespaceId, int startPage, int pageSize, String keyword, List<RaftPeer> raftPeerList) {
List<RaftPeer> matchList = new ArrayList<>(raftPeerSet.allPeers());
List<RaftPeer> matchList = new ArrayList<>();
RaftPeer localRaftPeer = raftPeerSet.local();
matchList.add(localRaftPeer);
Set<String> otherServerSet = raftPeerSet.allServersWithoutMySelf();
if (null != otherServerSet && otherServerSet.size() > 0) {
for (String server: otherServerSet) {
String path = UtilsAndCommons.NACOS_NAMING_OPERATOR_CONTEXT + UtilsAndCommons.NACOS_NAMING_CLUSTER_CONTEXT + "/state";
Map<String, String> params = Maps.newHashMapWithExpectedSize(2);
try {
String content = NamingProxy.reqCommon(path, params, server, false);
if (!StringUtils.EMPTY.equals(content)) {
RaftPeer raftPeer = JSONObject.parseObject(content, RaftPeer.class);
if (null != raftPeer) {
matchList.add(raftPeer);
}
}
} catch (Exception e) {
Loggers.SRV_LOG.warn("[QUERY-CLUSTER-STATE] Exception while query cluster state from {}, error: {}",
server, e);
}
}
}
List<RaftPeer> tempList = new ArrayList<>(); List<RaftPeer> tempList = new ArrayList<>();
if (StringUtils.isNotBlank(keyword)) { if (StringUtils.isNotBlank(keyword)) {
for (RaftPeer raftPeer : matchList) { for (RaftPeer raftPeer : matchList) {
@ -265,6 +289,10 @@ public class ServiceManager implements RecordListener<Service> {
return matchList.size(); return matchList.size();
} }
public RaftPeer getMySelfClusterState() {
return raftPeerSet.local();
}
public void updatedHealthStatus(String namespaceId, String serviceName, String serverIP) { public void updatedHealthStatus(String namespaceId, String serviceName, String serverIP) {
Message msg = synchronizer.get(serverIP, UtilsAndCommons.assembleFullServiceName(namespaceId, serviceName)); Message msg = synchronizer.get(serverIP, UtilsAndCommons.assembleFullServiceName(namespaceId, serviceName));
JSONObject serviceJson = JSON.parseObject(msg.getData()); JSONObject serviceJson = JSON.parseObject(msg.getData());

View File

@ -215,6 +215,47 @@ public class NamingProxy {
return StringUtils.EMPTY; return StringUtils.EMPTY;
} }
public static String reqCommon(String path, Map<String, String> params, String curServer, boolean isPost) throws Exception {
try {
List<String> headers = Arrays.asList("Client-Version", UtilsAndCommons.SERVER_VERSION,
"User-Agent", UtilsAndCommons.SERVER_VERSION,
"Accept-Encoding", "gzip,deflate,sdch",
"Connection", "Keep-Alive",
"Content-Encoding", "gzip");
HttpClient.HttpResult result;
if (!curServer.contains(UtilsAndCommons.IP_PORT_SPLITER)) {
curServer = curServer + UtilsAndCommons.IP_PORT_SPLITER + RunningConfig.getServerPort();
}
if (isPost) {
result = HttpClient.httpPost("http://" + curServer + RunningConfig.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + path, headers, params);
} else {
result = HttpClient.httpGet("http://" + curServer + RunningConfig.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + path, headers, params);
}
if (HttpURLConnection.HTTP_OK == result.code) {
return result.content;
}
if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) {
return StringUtils.EMPTY;
}
throw new IOException("failed to req API:" + "http://" + curServer
+ RunningConfig.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + path + ". code:"
+ result.code + " msg: " + result.content);
} catch (Exception e) {
Loggers.SRV_LOG.warn("NamingProxy", e);
}
return StringUtils.EMPTY;
}
public static class Request { public static class Request {
private Map<String, String> params = new HashMap<>(8); private Map<String, String> params = new HashMap<>(8);