diff --git a/console/src/main/resources/static/console-fe/src/locales/en-US.js b/console/src/main/resources/static/console-fe/src/locales/en-US.js index 6ef9a3666..d03e60039 100644 --- a/console/src/main/resources/static/console-fe/src/locales/en-US.js +++ b/console/src/main/resources/static/console-fe/src/locales/en-US.js @@ -99,8 +99,8 @@ const I18N_CONF = { pubNoData: 'No results found.', nodeState: 'NodeState', clusterTerm: 'ClusterTerm', - leaderDueMs: 'LeaderDueMs', - heartbeatDueMs: 'HeartbeatDueMs', + leaderDueMs: 'LeaderDue(ms)', + heartbeatDueMs: 'HeartbeatDue(ms)', voteFor: 'VoteFor', }, EditClusterDialog: { diff --git a/console/src/main/resources/static/console-fe/src/locales/zh-CN.js b/console/src/main/resources/static/console-fe/src/locales/zh-CN.js index 80c704384..eeb360831 100644 --- a/console/src/main/resources/static/console-fe/src/locales/zh-CN.js +++ b/console/src/main/resources/static/console-fe/src/locales/zh-CN.js @@ -99,8 +99,8 @@ const I18N_CONF = { pubNoData: '没有数据', nodeState: '节点状态', clusterTerm: '集群任期', - leaderDueMs: 'Leader止时', - heartbeatDueMs: '心跳止时', + leaderDueMs: 'Leader止时(ms)', + heartbeatDueMs: '心跳止时(ms)', voteFor: '投票对象', }, EditClusterDialog: { diff --git a/console/src/main/resources/static/console-fe/src/pages/ClusterManagement/ClusterNodeList/ClusterNodeList.js b/console/src/main/resources/static/console-fe/src/pages/ClusterManagement/ClusterNodeList/ClusterNodeList.js index 5bacc257c..830a51b1a 100644 --- a/console/src/main/resources/static/console-fe/src/pages/ClusterManagement/ClusterNodeList/ClusterNodeList.js +++ b/console/src/main/resources/static/console-fe/src/pages/ClusterManagement/ClusterNodeList/ClusterNodeList.js @@ -77,7 +77,7 @@ class ClusterNodeList extends React.Component { `keyword=${keyword}`, ]; request({ - url: `v1/ns/cluster/states?${parameter.join('&')}`, + url: `v1/ns/operator/cluster/states?${parameter.join('&')}`, beforeSend: () => this.openLoading(), success: ({ count = 0, clusterStateList = [] } = {}) => { this.setState({ diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ClusterController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ClusterController.java index a2c4163b2..b8218ce51 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ClusterController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ClusterController.java @@ -56,9 +56,6 @@ public class ClusterController { @Autowired protected ServiceManager serviceManager; - @Autowired - private RaftPeerSet raftPeerSet; - @RequestMapping(value = "", method = RequestMethod.PUT) public String update(HttpServletRequest request) throws Exception { @@ -107,41 +104,4 @@ public class ClusterController { return "ok"; } - @RequestMapping(value = "/states", method = RequestMethod.GET) - public Object listStates(HttpServletRequest request) { - - String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, - Constants.DEFAULT_NAMESPACE_ID); - JSONObject result = new JSONObject(); - 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 raftPeerLists = new ArrayList<>(); - - int total = serviceManager.getPagedClusterState(namespaceId, page - 1, pageSize, keyword, containedInstance, raftPeerLists, raftPeerSet); - - if (CollectionUtils.isEmpty(raftPeerLists)) { - result.put("clusterStateList", Collections.emptyList()); - result.put("count", 0); - return result; - } - - JSONArray clusterStateJsonArray = new JSONArray(); - for(RaftPeer raftPeer: raftPeerLists) { - ClusterStateView clusterStateView = new ClusterStateView(); - clusterStateView.setClusterTerm(raftPeer.term.intValue()); - clusterStateView.setNodeIp(raftPeer.ip); - clusterStateView.setNodeState(raftPeer.state.name()); - clusterStateView.setVoteFor(raftPeer.voteFor); - clusterStateView.setHeartbeatDueMs(raftPeer.heartbeatDueMs); - clusterStateView.setLeaderDueMs(raftPeer.leaderDueMs); - clusterStateJsonArray.add(clusterStateView); - } - result.put("clusterStateList", clusterStateJsonArray); - result.put("count", total); - return result; - } - } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java index a4b80a610..f1c968e50 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/OperatorController.java @@ -24,6 +24,8 @@ import com.alibaba.nacos.core.utils.WebUtils; import com.alibaba.nacos.naming.cluster.ServerListManager; 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; @@ -31,8 +33,10 @@ import com.alibaba.nacos.naming.misc.SwitchDomain; import com.alibaba.nacos.naming.misc.SwitchEntry; import com.alibaba.nacos.naming.misc.SwitchManager; import com.alibaba.nacos.naming.misc.UtilsAndCommons; +import com.alibaba.nacos.naming.pojo.ClusterStateView; import com.alibaba.nacos.naming.push.PushService; import com.alibaba.nacos.naming.web.NeedAuth; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties; @@ -42,6 +46,8 @@ import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; /** @@ -77,6 +83,9 @@ public class OperatorController { @Autowired private RaftCore raftCore; + @Autowired + private RaftPeerSet raftPeerSet; + @RequestMapping("/push/state") public JSONObject pushState(HttpServletRequest request) { @@ -215,4 +224,41 @@ public class OperatorController { serverListManager.onReceiveServerStatus(serverStatus); return "ok"; } + + @RequestMapping(value = "/cluster/states", method = RequestMethod.GET) + public Object listStates(HttpServletRequest request) { + + String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, + Constants.DEFAULT_NAMESPACE_ID); + JSONObject result = new JSONObject(); + 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 raftPeerLists = new ArrayList<>(); + + int total = serviceManager.getPagedClusterState(namespaceId, page - 1, pageSize, keyword, containedInstance, raftPeerLists, raftPeerSet); + + if (CollectionUtils.isEmpty(raftPeerLists)) { + result.put("clusterStateList", Collections.emptyList()); + result.put("count", 0); + return result; + } + + JSONArray clusterStateJsonArray = new JSONArray(); + for(RaftPeer raftPeer: raftPeerLists) { + ClusterStateView clusterStateView = new ClusterStateView(); + clusterStateView.setClusterTerm(raftPeer.term.intValue()); + clusterStateView.setNodeIp(raftPeer.ip); + clusterStateView.setNodeState(raftPeer.state.name()); + clusterStateView.setVoteFor(raftPeer.voteFor); + clusterStateView.setHeartbeatDueMs(raftPeer.heartbeatDueMs); + clusterStateView.setLeaderDueMs(raftPeer.leaderDueMs); + clusterStateJsonArray.add(clusterStateView); + } + result.put("clusterStateList", clusterStateJsonArray); + result.put("count", total); + return result; + } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java index fea9087a6..3534946ed 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java @@ -229,6 +229,41 @@ public class ServiceManager implements RecordListener { } } + public int getPagedClusterState(String namespaceId, int startPage, int pageSize, String keyword, String containedInstance, List raftPeerList, RaftPeerSet raftPeerSet) { + + List matchList = new ArrayList<>(raftPeerSet.allPeers()); + + List tempList = new ArrayList<>(); + if(StringUtils.isNotBlank(keyword)) { + for(RaftPeer raftPeer: matchList) { + String ip = raftPeer.ip.split(":")[0]; + if(keyword.equals(ip)) { + tempList.add(raftPeer); + } + } + matchList = tempList; + } + + if (pageSize >= matchList.size()) { + raftPeerList.addAll(matchList); + return matchList.size(); + } + + for (int i = 0; i < matchList.size(); i++) { + if (i < startPage * pageSize) { + continue; + } + + raftPeerList.add(matchList.get(i)); + + if (raftPeerList.size() >= pageSize) { + break; + } + } + + return matchList.size(); + } + public void updatedHealthStatus(String namespaceId, String serviceName, String serverIP) { Message msg = synchronizer.get(serverIP, UtilsAndCommons.assembleFullServiceName(namespaceId, serviceName)); JSONObject serviceJson = JSON.parseObject(msg.getData()); @@ -651,41 +686,6 @@ public class ServiceManager implements RecordListener { return matchList.size(); } - public int getPagedClusterState(String namespaceId, int startPage, int pageSize, String keyword, String containedInstance, List raftPeerList, RaftPeerSet raftPeerSet) { - - List matchList = new ArrayList<>(raftPeerSet.allPeers()); - - List tempList = new ArrayList<>(); - if(StringUtils.isNotBlank(keyword)) { - for(RaftPeer raftPeer: matchList) { - String ip = raftPeer.ip.split(":")[0]; - if(keyword.equals(ip)) { - tempList.add(raftPeer); - } - } - matchList = tempList; - } - - if (pageSize >= matchList.size()) { - raftPeerList.addAll(matchList); - return matchList.size(); - } - - for (int i = 0; i < matchList.size(); i++) { - if (i < startPage * pageSize) { - continue; - } - - raftPeerList.add(matchList.get(i)); - - if (raftPeerList.size() >= pageSize) { - break; - } - } - - return matchList.size(); - } - public static class ServiceChecksum { public String namespaceId;