From 0b15090bab4e17c528a4a404eb7c0444eaa9458f Mon Sep 17 00:00:00 2001 From: MajorHe1 <53824889+MajorHe1@users.noreply.github.com> Date: Mon, 9 Aug 2021 09:52:32 +0800 Subject: [PATCH] [ISSUE #6572] add client info for OperatorController metrics interface (#6576) * [ISSUE #6572] add client info for OperatorController metrics interface * [ISSUE #6572] move PERSISTENT_SUFFIX to ClientConstants --- .../naming/constants/ClientConstants.java | 2 ++ .../controllers/OperatorController.java | 34 ++++++++++++++++++- .../client/manager/ClientManagerDelegate.java | 5 ++- .../controllers/OperatorControllerTest.java | 18 ++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/constants/ClientConstants.java b/naming/src/main/java/com/alibaba/nacos/naming/constants/ClientConstants.java index e7758f853..1a0724ff5 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/constants/ClientConstants.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/constants/ClientConstants.java @@ -35,6 +35,8 @@ public class ClientConstants { public static final String PERSISTENT_IP_PORT = "persistentIpPort"; + public static final String PERSISTENT_SUFFIX = "false"; + public static final String CLIENT_EXPIRED_TIME_CONFIG_KEY = "nacos.naming.client.expired.time"; public static final long DEFAULT_CLIENT_EXPIRED_TIME = TimeUnit.MINUTES.toMillis(3); 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 31eafbd2b..83c9e5ac7 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 @@ -29,9 +29,12 @@ 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.constants.ClientConstants; import com.alibaba.nacos.naming.core.DistroMapper; import com.alibaba.nacos.naming.core.Service; import com.alibaba.nacos.naming.core.ServiceManager; +import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient; +import com.alibaba.nacos.naming.core.v2.client.manager.ClientManager; import com.alibaba.nacos.naming.misc.Loggers; import com.alibaba.nacos.naming.misc.SwitchDomain; import com.alibaba.nacos.naming.misc.SwitchEntry; @@ -50,6 +53,7 @@ import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; +import java.util.Collection; import java.util.List; /** @@ -78,9 +82,11 @@ public class OperatorController { private final RaftCore raftCore; + private final ClientManager clientManager; + public OperatorController(SwitchManager switchManager, ServerListManager serverListManager, ServiceManager serviceManager, ServerMemberManager memberManager, ServerStatusManager serverStatusManager, - SwitchDomain switchDomain, DistroMapper distroMapper, RaftCore raftCore) { + SwitchDomain switchDomain, DistroMapper distroMapper, RaftCore raftCore, ClientManager clientManager) { this.switchManager = switchManager; this.serverListManager = serverListManager; this.serviceManager = serviceManager; @@ -89,6 +95,7 @@ public class OperatorController { this.switchDomain = switchDomain; this.distroMapper = distroMapper; this.raftCore = raftCore; + this.clientManager = clientManager; } /** @@ -168,6 +175,26 @@ public class OperatorController { if (onlyStatus) { return result; } + Collection allClientId = clientManager.allClientId(); + int connectionBasedClient = 0; + int ephemeralIpPortClient = 0; + int persistentIpPortClient = 0; + int responsibleClientCount = 0; + for (String clientId : allClientId) { + if (clientId.contains(IpPortBasedClient.ID_DELIMITER)) { + if (clientId.endsWith(ClientConstants.PERSISTENT_SUFFIX)) { + persistentIpPortClient += 1; + } else { + ephemeralIpPortClient += 1; + } + } else { + connectionBasedClient += 1; + } + if (clientManager.isResponsibleClient(clientManager.getClient(clientId))) { + responsibleClientCount += 1; + } + } + int responsibleDomCount = serviceManager.getResponsibleServiceCount(); int responsibleIpCount = serviceManager.getResponsibleInstanceCount(); result.put("serviceCount", MetricsMonitor.getDomCountMonitor().get()); @@ -176,6 +203,11 @@ public class OperatorController { result.put("raftNotifyTaskCount", raftCore.getNotifyTaskCount()); result.put("responsibleServiceCount", responsibleDomCount); result.put("responsibleInstanceCount", responsibleIpCount); + result.put("clientCount", allClientId.size()); + result.put("connectionBasedClientCount", connectionBasedClient); + result.put("ephemeralIpPortClientCount", ephemeralIpPortClient); + result.put("persistentIpPortClientCount", persistentIpPortClient); + result.put("responsibleClientCount", responsibleClientCount); result.put("cpu", EnvUtil.getCPU()); result.put("load", EnvUtil.getLoad()); result.put("mem", EnvUtil.getMem()); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/manager/ClientManagerDelegate.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/manager/ClientManagerDelegate.java index 1dca6dbab..53a35fb81 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/manager/ClientManagerDelegate.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/manager/ClientManagerDelegate.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.naming.core.v2.client.manager; +import com.alibaba.nacos.naming.constants.ClientConstants; import com.alibaba.nacos.naming.core.v2.client.Client; import com.alibaba.nacos.naming.core.v2.client.ClientAttributes; import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient; @@ -41,8 +42,6 @@ public class ClientManagerDelegate implements ClientManager { private final PersistentIpPortClientManager persistentIpPortClientManager; - private static final String SUFFIX = "false"; - public ClientManagerDelegate(ConnectionBasedClientManager connectionBasedClientManager, EphemeralIpPortClientManager ephemeralIpPortClientManager, PersistentIpPortClientManager persistentIpPortClientManager) { @@ -105,7 +104,7 @@ public class ClientManagerDelegate implements ClientManager { if (isConnectionBasedClient(clientId)) { return connectionBasedClientManager; } - return clientId.endsWith(SUFFIX) ? persistentIpPortClientManager : ephemeralIpPortClientManager; + return clientId.endsWith(ClientConstants.PERSISTENT_SUFFIX) ? persistentIpPortClientManager : ephemeralIpPortClientManager; } private boolean isConnectionBasedClient(String clientId) { diff --git a/naming/src/test/java/com/alibaba/nacos/naming/controllers/OperatorControllerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/controllers/OperatorControllerTest.java index 4efdcbadc..e6ac0ca6e 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/controllers/OperatorControllerTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/controllers/OperatorControllerTest.java @@ -24,6 +24,7 @@ import com.alibaba.nacos.naming.consistency.persistent.raft.RaftCore; import com.alibaba.nacos.naming.core.DistroMapper; import com.alibaba.nacos.naming.core.Service; import com.alibaba.nacos.naming.core.ServiceManager; +import com.alibaba.nacos.naming.core.v2.client.manager.ClientManager; import com.alibaba.nacos.naming.misc.SwitchDomain; import com.alibaba.nacos.naming.misc.SwitchManager; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -36,6 +37,9 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.springframework.mock.web.MockHttpServletRequest; +import java.util.Collection; +import java.util.HashSet; + /** * {@link OperatorController} unit test. * @@ -60,6 +64,9 @@ public class OperatorControllerTest { @Mock private ServiceManager serviceManager; + @Mock + private ClientManager clientManager; + @Mock private RaftCore raftCore; @@ -95,6 +102,12 @@ public class OperatorControllerTest { Mockito.when(serviceManager.getResponsibleServiceCount()).thenReturn(1); Mockito.when(serviceManager.getResponsibleInstanceCount()).thenReturn(1); Mockito.when(raftCore.getNotifyTaskCount()).thenReturn(1); + Collection clients = new HashSet<>(); + clients.add("1628132208793_127.0.0.1_8080"); + clients.add("127.0.0.1:8081#true"); + clients.add("127.0.0.1:8082#false"); + Mockito.when(clientManager.allClientId()).thenReturn(clients); + Mockito.when(clientManager.isResponsibleClient(null)).thenReturn(Boolean.TRUE); MockHttpServletRequest servletRequest = new MockHttpServletRequest(); servletRequest.addParameter("onlyStatus", "false"); @@ -103,6 +116,11 @@ public class OperatorControllerTest { Assert.assertEquals(1, objectNode.get("responsibleServiceCount").asInt()); Assert.assertEquals(1, objectNode.get("responsibleInstanceCount").asInt()); Assert.assertEquals(ServerStatus.UP.toString(), objectNode.get("status").asText()); + Assert.assertEquals(3, objectNode.get("clientCount").asInt()); + Assert.assertEquals(1, objectNode.get("connectionBasedClientCount").asInt()); + Assert.assertEquals(1, objectNode.get("ephemeralIpPortClientCount").asInt()); + Assert.assertEquals(1, objectNode.get("persistentIpPortClientCount").asInt()); + Assert.assertEquals(3, objectNode.get("responsibleClientCount").asInt()); } @Test