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

[ISSUE#1602] Fixed cluster page show bug #1828

Merged
merged 2 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.nacos.naming.controllers;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.common.Constants;
Expand All @@ -25,7 +26,6 @@
import com.alibaba.nacos.naming.cluster.ServerStatusManager;
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.RaftPeerSet;
import com.alibaba.nacos.naming.core.DistroMapper;
import com.alibaba.nacos.naming.core.Service;
import com.alibaba.nacos.naming.core.ServiceManager;
Expand All @@ -41,6 +41,7 @@
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -79,9 +80,6 @@ public class OperatorController {
@Autowired
private RaftCore raftCore;

@Autowired
private RaftPeerSet raftPeerSet;

@RequestMapping("/push/state")
public JSONObject pushState(HttpServletRequest request) {

Expand Down Expand Up @@ -238,11 +236,10 @@ public Object listStates(HttpServletRequest request) {
int page = Integer.parseInt(WebUtils.required(request, "pageNo"));
int pageSize = Integer.parseInt(WebUtils.required(request, "pageSize"));
String keyword = WebUtils.optional(request, "keyword", StringUtils.EMPTY);
String containedInstance = WebUtils.optional(request, "instance", StringUtils.EMPTY);

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)) {
result.put("clusterStateList", Collections.emptyList());
Expand All @@ -265,4 +262,13 @@ public Object listStates(HttpServletRequest request) {
result.put("count", total);
return result;
}

@RequestMapping(value = "/cluster/state", method = RequestMethod.GET)
public JSONObject getClusterStates() {

RaftPeer peer = serviceManager.getMySelfClusterState();

return JSON.parseObject(JSON.toJSONString(peer));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet;
import com.alibaba.nacos.naming.misc.*;
import com.alibaba.nacos.naming.push.PushService;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -83,6 +84,9 @@ public class ServiceManager implements RecordListener<Service> {
@Autowired
private PushService pushService;

@Autowired
private RaftPeerSet raftPeerSet;

private final Object putServiceLock = new Object();

@PostConstruct
Expand Down Expand Up @@ -230,10 +234,30 @@ public void run() {
}
}

public int getPagedClusterState(String namespaceId, int startPage, int pageSize, String keyword, String containedInstance, List<RaftPeer> raftPeerList, RaftPeerSet raftPeerSet) {

List<RaftPeer> matchList = new ArrayList<>(raftPeerSet.allPeers());
public int getPagedClusterState(String namespaceId, int startPage, int pageSize, String keyword, List<RaftPeer> raftPeerList) {

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<>();
if (StringUtils.isNotBlank(keyword)) {
for (RaftPeer raftPeer : matchList) {
Expand Down Expand Up @@ -265,6 +289,10 @@ public int getPagedClusterState(String namespaceId, int startPage, int pageSize,
return matchList.size();
}

public RaftPeer getMySelfClusterState() {
return raftPeerSet.local();
}

public void updatedHealthStatus(String namespaceId, String serviceName, String serverIP) {
Message msg = synchronizer.get(serverIP, UtilsAndCommons.assembleFullServiceName(namespaceId, serviceName));
JSONObject serviceJson = JSON.parseObject(msg.getData());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,47 @@ public static String reqAPI(String api, Map<String, String> params, String curSe
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 {

private Map<String, String> params = new HashMap<>(8);
Expand Down