From 419a6118c4a7d7ac6d52e917e8a401912d35ee06 Mon Sep 17 00:00:00 2001 From: zhouchunhai Date: Thu, 2 Jan 2025 15:06:43 +0800 Subject: [PATCH 1/3] [ISSUE #12940] Add a new param to support return all subscriber when ip or port is null. --- .../naming/controllers/v2/ClientInfoControllerV2.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/v2/ClientInfoControllerV2.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/v2/ClientInfoControllerV2.java index 7625a170ed7..dfcf6327c20 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/v2/ClientInfoControllerV2.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/v2/ClientInfoControllerV2.java @@ -261,7 +261,8 @@ public Result> getSubscribeClientList( @RequestParam(value = "groupName", required = false, defaultValue = Constants.DEFAULT_GROUP) String groupName, @RequestParam(value = "ephemeral", required = false, defaultValue = "true") Boolean ephemeral, @RequestParam("serviceName") String serviceName, @RequestParam(value = "ip", required = false) String ip, - @RequestParam(value = "port", required = false) Integer port) { + @RequestParam(value = "port", required = false) Integer port, + @RequestParam(value = "ignoreNull", required = false, defaultValue = "false") boolean ignoreNull) { Service service = Service.newService(namespaceId, groupName, serviceName, ephemeral); Collection allClientsSubscribeService = clientServiceIndexesManager .getAllClientsSubscribeService(service); @@ -269,7 +270,9 @@ public Result> getSubscribeClientList( for (String clientId : allClientsSubscribeService) { Client client = clientManager.getClient(clientId); Subscriber subscriber = client.getSubscriber(service); - if (!Objects.equals(subscriber.getIp(), ip) || !Objects.equals(port, subscriber.getPort())) { + boolean ipMatch = (ignoreNull && Objects.isNull(ip)) || Objects.equals(ip, subscriber.getIp()); + boolean portMatch = (ignoreNull && Objects.isNull(port)) || Objects.equals(port, subscriber.getPort()); + if (!ipMatch || !portMatch) { continue; } ObjectNode item = JacksonUtils.createEmptyJsonNode(); From abdc5ce0a271211eb28600b92181acfebeb9c18f Mon Sep 17 00:00:00 2001 From: zhouchunhai Date: Thu, 2 Jan 2025 15:06:43 +0800 Subject: [PATCH 2/3] [ISSUE #12940] Add a new param to support return all subscriber when ip or port is null. --- .../naming/controllers/v2/ClientInfoControllerV2.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/v2/ClientInfoControllerV2.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/v2/ClientInfoControllerV2.java index dfcf6327c20..c8dc5634451 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/v2/ClientInfoControllerV2.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/v2/ClientInfoControllerV2.java @@ -261,8 +261,7 @@ public Result> getSubscribeClientList( @RequestParam(value = "groupName", required = false, defaultValue = Constants.DEFAULT_GROUP) String groupName, @RequestParam(value = "ephemeral", required = false, defaultValue = "true") Boolean ephemeral, @RequestParam("serviceName") String serviceName, @RequestParam(value = "ip", required = false) String ip, - @RequestParam(value = "port", required = false) Integer port, - @RequestParam(value = "ignoreNull", required = false, defaultValue = "false") boolean ignoreNull) { + @RequestParam(value = "port", required = false) Integer port) { Service service = Service.newService(namespaceId, groupName, serviceName, ephemeral); Collection allClientsSubscribeService = clientServiceIndexesManager .getAllClientsSubscribeService(service); @@ -270,8 +269,8 @@ public Result> getSubscribeClientList( for (String clientId : allClientsSubscribeService) { Client client = clientManager.getClient(clientId); Subscriber subscriber = client.getSubscriber(service); - boolean ipMatch = (ignoreNull && Objects.isNull(ip)) || Objects.equals(ip, subscriber.getIp()); - boolean portMatch = (ignoreNull && Objects.isNull(port)) || Objects.equals(port, subscriber.getPort()); + boolean ipMatch = Objects.isNull(ip) || Objects.equals(ip, subscriber.getIp()); + boolean portMatch = Objects.isNull(port) || Objects.equals(port, subscriber.getPort()); if (!ipMatch || !portMatch) { continue; } From efc5ee4dfa970d4d6900b779590d077b823a015a Mon Sep 17 00:00:00 2001 From: zhouchunhai Date: Tue, 7 Jan 2025 10:51:41 +0800 Subject: [PATCH 3/3] [ISSUE #12940] add some unit tests. --- .../v2/ClientInfoControllerV2Test.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/naming/src/test/java/com/alibaba/nacos/naming/controllers/v2/ClientInfoControllerV2Test.java b/naming/src/test/java/com/alibaba/nacos/naming/controllers/v2/ClientInfoControllerV2Test.java index 5eae88e39a9..f8ebdafd951 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/controllers/v2/ClientInfoControllerV2Test.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/controllers/v2/ClientInfoControllerV2Test.java @@ -27,6 +27,7 @@ import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo; import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.misc.UtilsAndCommons; +import com.alibaba.nacos.naming.pojo.Subscriber; import com.fasterxml.jackson.databind.JsonNode; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -47,6 +48,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) @@ -142,4 +144,30 @@ void testGetPublishedClientList() throws Exception { .param("groupName", baseTestKey).param("serviceName", baseTestKey).param("ip", "127.0.0.1").param("port", "8848"); mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1)); } + + @Test + void testGetSubscribeClientList() throws Exception { + String baseTestKey = "nacos-getSubScribedClientList-test"; + // ip port match + Service service = Service.newService(baseTestKey, baseTestKey, baseTestKey); + when(clientServiceIndexesManager.getAllClientsSubscribeService(service)).thenReturn(Arrays.asList("test")); + when(clientManager.getClient("test")).thenReturn(ipPortBasedClient); + Subscriber subscriber = mock(Subscriber.class); + when(subscriber.getIp()).thenReturn("127.0.0.1"); + when(subscriber.getPort()).thenReturn(8848); + ipPortBasedClient.addServiceSubscriber(service, subscriber); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/service/subscriber/list") + .param("namespaceId", baseTestKey).param("groupName", baseTestKey).param("serviceName", baseTestKey) + .param("ip", "127.0.0.1").param("port", "8848"); + mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1)); + // ip port not match + mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/service/subscriber/list") + .param("namespaceId", baseTestKey).param("groupName", baseTestKey).param("serviceName", baseTestKey) + .param("ip", "127.0.0.1").param("port", "8849"); + mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(0)); + // ip port is null + mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/service/subscriber/list") + .param("namespaceId", baseTestKey).param("groupName", baseTestKey).param("serviceName", baseTestKey); + mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1)); + } }