[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
This commit is contained in:
MajorHe1 2021-08-09 09:52:32 +08:00 committed by GitHub
parent d54789dbc3
commit 0b15090bab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 4 deletions

View File

@ -35,6 +35,8 @@ public class ClientConstants {
public static final String PERSISTENT_IP_PORT = "persistentIpPort"; 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 String CLIENT_EXPIRED_TIME_CONFIG_KEY = "nacos.naming.client.expired.time";
public static final long DEFAULT_CLIENT_EXPIRED_TIME = TimeUnit.MINUTES.toMillis(3); public static final long DEFAULT_CLIENT_EXPIRED_TIME = TimeUnit.MINUTES.toMillis(3);

View File

@ -29,9 +29,12 @@ import com.alibaba.nacos.core.utils.WebUtils;
import com.alibaba.nacos.naming.cluster.ServerListManager; 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.constants.ClientConstants;
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;
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.Loggers;
import com.alibaba.nacos.naming.misc.SwitchDomain; import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.misc.SwitchEntry; import com.alibaba.nacos.naming.misc.SwitchEntry;
@ -50,6 +53,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
/** /**
@ -78,9 +82,11 @@ public class OperatorController {
private final RaftCore raftCore; private final RaftCore raftCore;
private final ClientManager clientManager;
public OperatorController(SwitchManager switchManager, ServerListManager serverListManager, public OperatorController(SwitchManager switchManager, ServerListManager serverListManager,
ServiceManager serviceManager, ServerMemberManager memberManager, ServerStatusManager serverStatusManager, ServiceManager serviceManager, ServerMemberManager memberManager, ServerStatusManager serverStatusManager,
SwitchDomain switchDomain, DistroMapper distroMapper, RaftCore raftCore) { SwitchDomain switchDomain, DistroMapper distroMapper, RaftCore raftCore, ClientManager clientManager) {
this.switchManager = switchManager; this.switchManager = switchManager;
this.serverListManager = serverListManager; this.serverListManager = serverListManager;
this.serviceManager = serviceManager; this.serviceManager = serviceManager;
@ -89,6 +95,7 @@ public class OperatorController {
this.switchDomain = switchDomain; this.switchDomain = switchDomain;
this.distroMapper = distroMapper; this.distroMapper = distroMapper;
this.raftCore = raftCore; this.raftCore = raftCore;
this.clientManager = clientManager;
} }
/** /**
@ -168,6 +175,26 @@ public class OperatorController {
if (onlyStatus) { if (onlyStatus) {
return result; return result;
} }
Collection<String> 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 responsibleDomCount = serviceManager.getResponsibleServiceCount();
int responsibleIpCount = serviceManager.getResponsibleInstanceCount(); int responsibleIpCount = serviceManager.getResponsibleInstanceCount();
result.put("serviceCount", MetricsMonitor.getDomCountMonitor().get()); result.put("serviceCount", MetricsMonitor.getDomCountMonitor().get());
@ -176,6 +203,11 @@ public class OperatorController {
result.put("raftNotifyTaskCount", raftCore.getNotifyTaskCount()); result.put("raftNotifyTaskCount", raftCore.getNotifyTaskCount());
result.put("responsibleServiceCount", responsibleDomCount); result.put("responsibleServiceCount", responsibleDomCount);
result.put("responsibleInstanceCount", responsibleIpCount); 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("cpu", EnvUtil.getCPU());
result.put("load", EnvUtil.getLoad()); result.put("load", EnvUtil.getLoad());
result.put("mem", EnvUtil.getMem()); result.put("mem", EnvUtil.getMem());

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.naming.core.v2.client.manager; 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.Client;
import com.alibaba.nacos.naming.core.v2.client.ClientAttributes; import com.alibaba.nacos.naming.core.v2.client.ClientAttributes;
import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient; import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient;
@ -41,8 +42,6 @@ public class ClientManagerDelegate implements ClientManager {
private final PersistentIpPortClientManager persistentIpPortClientManager; private final PersistentIpPortClientManager persistentIpPortClientManager;
private static final String SUFFIX = "false";
public ClientManagerDelegate(ConnectionBasedClientManager connectionBasedClientManager, public ClientManagerDelegate(ConnectionBasedClientManager connectionBasedClientManager,
EphemeralIpPortClientManager ephemeralIpPortClientManager, EphemeralIpPortClientManager ephemeralIpPortClientManager,
PersistentIpPortClientManager persistentIpPortClientManager) { PersistentIpPortClientManager persistentIpPortClientManager) {
@ -105,7 +104,7 @@ public class ClientManagerDelegate implements ClientManager {
if (isConnectionBasedClient(clientId)) { if (isConnectionBasedClient(clientId)) {
return connectionBasedClientManager; return connectionBasedClientManager;
} }
return clientId.endsWith(SUFFIX) ? persistentIpPortClientManager : ephemeralIpPortClientManager; return clientId.endsWith(ClientConstants.PERSISTENT_SUFFIX) ? persistentIpPortClientManager : ephemeralIpPortClientManager;
} }
private boolean isConnectionBasedClient(String clientId) { private boolean isConnectionBasedClient(String clientId) {

View File

@ -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.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;
import com.alibaba.nacos.naming.core.v2.client.manager.ClientManager;
import com.alibaba.nacos.naming.misc.SwitchDomain; import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.misc.SwitchManager; import com.alibaba.nacos.naming.misc.SwitchManager;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
@ -36,6 +37,9 @@ import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
import java.util.Collection;
import java.util.HashSet;
/** /**
* {@link OperatorController} unit test. * {@link OperatorController} unit test.
* *
@ -60,6 +64,9 @@ public class OperatorControllerTest {
@Mock @Mock
private ServiceManager serviceManager; private ServiceManager serviceManager;
@Mock
private ClientManager clientManager;
@Mock @Mock
private RaftCore raftCore; private RaftCore raftCore;
@ -95,6 +102,12 @@ public class OperatorControllerTest {
Mockito.when(serviceManager.getResponsibleServiceCount()).thenReturn(1); Mockito.when(serviceManager.getResponsibleServiceCount()).thenReturn(1);
Mockito.when(serviceManager.getResponsibleInstanceCount()).thenReturn(1); Mockito.when(serviceManager.getResponsibleInstanceCount()).thenReturn(1);
Mockito.when(raftCore.getNotifyTaskCount()).thenReturn(1); Mockito.when(raftCore.getNotifyTaskCount()).thenReturn(1);
Collection<String> 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(); MockHttpServletRequest servletRequest = new MockHttpServletRequest();
servletRequest.addParameter("onlyStatus", "false"); servletRequest.addParameter("onlyStatus", "false");
@ -103,6 +116,11 @@ public class OperatorControllerTest {
Assert.assertEquals(1, objectNode.get("responsibleServiceCount").asInt()); Assert.assertEquals(1, objectNode.get("responsibleServiceCount").asInt());
Assert.assertEquals(1, objectNode.get("responsibleInstanceCount").asInt()); Assert.assertEquals(1, objectNode.get("responsibleInstanceCount").asInt());
Assert.assertEquals(ServerStatus.UP.toString(), objectNode.get("status").asText()); 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 @Test