From 538c4db53c0f4b384969c2632501533e6e24c9a5 Mon Sep 17 00:00:00 2001 From: HMYDK Date: Thu, 27 Jun 2024 00:23:21 +0800 Subject: [PATCH] [ISSUE #12282]Fix the issue where monitoring data cannot be found through the IP dimension in the ListeningQuery --- .../controller/CommunicationController.java | 51 ++++++++++++++-- .../enums/ConfigSearchRequestTypeEnum.java | 61 +++++++++++++++++++ .../server/service/ConfigSubService.java | 9 ++- .../server/service/LongPollingService.java | 4 +- .../CommunicationControllerTest.java | 52 ++++++++++++++++ 5 files changed, 168 insertions(+), 9 deletions(-) create mode 100644 config/src/main/java/com/alibaba/nacos/config/server/enums/ConfigSearchRequestTypeEnum.java diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/CommunicationController.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/CommunicationController.java index 28f75c97523..555b921e3c4 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/CommunicationController.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/CommunicationController.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.common.utils.CollectionUtils; import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.config.server.constant.Constants; +import com.alibaba.nacos.config.server.enums.ConfigSearchRequestTypeEnum; import com.alibaba.nacos.config.server.model.SampleResult; import com.alibaba.nacos.config.server.paramcheck.ConfigDefaultHttpParamExtractor; import com.alibaba.nacos.config.server.remote.ConfigChangeListenContext; @@ -67,9 +68,17 @@ public CommunicationController(LongPollingService longPollingService, /** * Get client config information of subscriber in local machine. */ + @Deprecated @GetMapping("/configWatchers") public SampleResult getSubClientConfig(@RequestParam("dataId") String dataId, @RequestParam("group") String group, @RequestParam(value = "tenant", required = false) String tenant, ModelMap modelMap) { + return searchByConfig(dataId, group, tenant); + } + + private SampleResult searchByConfig(String dataId, String group, String tenant) { + if (StringUtils.isBlank(dataId)) { + return new SampleResult(); + } group = StringUtils.isBlank(group) ? Constants.DEFAULT_GROUP : group; // long polling listeners. SampleResult result = longPollingService.getCollectSubscribleInfo(dataId, group, tenant); @@ -96,21 +105,55 @@ public SampleResult getSubClientConfig(@RequestParam("dataId") String dataId, @R /** * Get client config listener lists of subscriber in local machine. */ + @Deprecated @GetMapping("/watcherConfigs") public SampleResult getSubClientConfigByIp(HttpServletRequest request, HttpServletResponse response, @RequestParam("ip") String ip, ModelMap modelMap) { - + return searchByIp(ip); + } + + private SampleResult searchByIp(String ip) { + if (StringUtils.isBlank(ip)) { + return new SampleResult(); + } SampleResult result = longPollingService.getCollectSubscribleInfoByIp(ip); List connectionsByIp = connectionManager.getConnectionByIp(ip); for (Connection connectionByIp : connectionsByIp) { - Map listenKeys = configChangeListenContext - .getListenKeys(connectionByIp.getMetaInfo().getConnectionId()); + Map listenKeys = configChangeListenContext.getListenKeys( + connectionByIp.getMetaInfo().getConnectionId()); if (listenKeys != null) { result.getLisentersGroupkeyStatus().putAll(listenKeys); } } return result; - + } + + /** + * Get client config information of subscriber in local machine. + * + * @param type {@link ConfigSearchRequestTypeEnum} + * @param ip machine ip + * @param dataId config dataId + * @param group config group + * @param tenant config tenant + * @return SampleResult + */ + @GetMapping("/config") + public SampleResult getSubClientConfigV2( + @RequestParam("type") String type, + @RequestParam(value = "ip", required = false) String ip, + @RequestParam(value = "dataId", required = false) String dataId, + @RequestParam(value = "group", required = false) String group, + @RequestParam(value = "tenant", required = false) String tenant) { + if (!ConfigSearchRequestTypeEnum.checkTypeLegal(type)) { + return new SampleResult(); + } + if (ConfigSearchRequestTypeEnum.IP.getType().equals(type)) { + return searchByIp(ip); + } else if (ConfigSearchRequestTypeEnum.CONFIG.getType().equals(type)) { + return searchByConfig(dataId, group, tenant); + } + return new SampleResult(); } } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/enums/ConfigSearchRequestTypeEnum.java b/config/src/main/java/com/alibaba/nacos/config/server/enums/ConfigSearchRequestTypeEnum.java new file mode 100644 index 00000000000..ff928674748 --- /dev/null +++ b/config/src/main/java/com/alibaba/nacos/config/server/enums/ConfigSearchRequestTypeEnum.java @@ -0,0 +1,61 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.config.server.enums; + +/** + * Config search request type enum. + * + * @author hmydk + */ +public enum ConfigSearchRequestTypeEnum { + + /** + * ip. + */ + IP("IP"), + + /** + * config. + */ + CONFIG("config"); + + + private final String type; + + ConfigSearchRequestTypeEnum(String type) { + this.type = type; + } + + /** + * check type is legal. + * + * @param type type + * @return true or false + */ + public static boolean checkTypeLegal(String type) { + for (ConfigSearchRequestTypeEnum configSearchRequestTypeEnum : ConfigSearchRequestTypeEnum.values()) { + if (configSearchRequestTypeEnum.getType().equals(type)) { + return true; + } + } + return false; + } + + public String getType() { + return type; + } +} diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigSubService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigSubService.java index 31b2a2c9234..db464d3ba78 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigSubService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigSubService.java @@ -23,6 +23,7 @@ import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.config.server.constant.Constants; +import com.alibaba.nacos.config.server.enums.ConfigSearchRequestTypeEnum; import com.alibaba.nacos.config.server.model.ListenerCheckResult; import com.alibaba.nacos.config.server.model.SampleResult; import com.alibaba.nacos.config.server.service.notify.HttpClientManager; @@ -85,7 +86,7 @@ private List runConfigListenerCollectionJob(Map pa static class ClusterListenerJob extends ClusterJob { - static final String URL = Constants.COMMUNICATION_CONTROLLER_PATH + "/configWatchers"; + static final String URL = Constants.COMMUNICATION_CONTROLLER_PATH + "/config"; ClusterListenerJob(Map params, CompletionService completionService, ServerMemberManager serverMemberManager) { @@ -295,7 +296,8 @@ public SampleResult mergeSampleResult(SampleResult sampleCollectResult, List params = new HashMap<>(5); + Map params = new HashMap<>(4); + params.put("type", ConfigSearchRequestTypeEnum.CONFIG.getType()); params.put("dataId", dataId); params.put("group", group); if (!StringUtils.isBlank(tenant)) { @@ -316,7 +318,8 @@ public SampleResult getCollectSampleResult(String dataId, String group, String t } public SampleResult getCollectSampleResultByIp(String ip, int sampleTime) { - Map params = new HashMap<>(50); + Map params = new HashMap<>(2); + params.put("type", ConfigSearchRequestTypeEnum.IP.getType()); params.put("ip", ip); BlockingQueue> queue = new LinkedBlockingDeque<>(memberManager.getServerList().size()); CompletionService completionService = new ExecutorCompletionService<>( diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/LongPollingService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/LongPollingService.java index d79b34cd421..69c9a31084f 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/LongPollingService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/LongPollingService.java @@ -70,7 +70,7 @@ public class LongPollingService { private Map retainIps = new ConcurrentHashMap<>(); - public SampleResult getSubscribleInfo(String dataId, String group, String tenant) { + public SampleResult getSubscribeInfo(String dataId, String group, String tenant) { String groupKey = GroupKey.getKeyTenant(dataId, group, tenant); SampleResult sampleResult = new SampleResult(); Map lisentersGroupkeyStatus = new HashMap<>(50); @@ -123,7 +123,7 @@ public SampleResult mergeSampleResult(List sampleResults) { public SampleResult getCollectSubscribleInfo(String dataId, String group, String tenant) { List sampleResultLst = new ArrayList<>(50); for (int i = 0; i < SAMPLE_TIMES; i++) { - SampleResult sampleTmp = getSubscribleInfo(dataId, group, tenant); + SampleResult sampleTmp = getSubscribeInfo(dataId, group, tenant); if (sampleTmp != null) { sampleResultLst.add(sampleTmp); } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/controller/CommunicationControllerTest.java b/config/src/test/java/com/alibaba/nacos/config/server/controller/CommunicationControllerTest.java index 3bbd9d7f745..32282ffdd7f 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/controller/CommunicationControllerTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/controller/CommunicationControllerTest.java @@ -18,6 +18,7 @@ import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.config.server.constant.Constants; +import com.alibaba.nacos.config.server.enums.ConfigSearchRequestTypeEnum; import com.alibaba.nacos.config.server.model.SampleResult; import com.alibaba.nacos.config.server.remote.ConfigChangeListenContext; import com.alibaba.nacos.config.server.service.LongPollingService; @@ -150,4 +151,55 @@ void testGetSubClientConfigByIp() throws Exception { assertEquals("{\"test\":\"test\"}", JacksonUtils.toObj(actualValue).get("lisentersGroupkeyStatus").toString()); } + + @Test + void testGetSubClientConfigV21x() throws Exception { + + SampleResult result = new SampleResult(); + result.setLisentersGroupkeyStatus(new HashMap<>()); + when(longPollingService.getCollectSubscribleInfo("test", "test", "test")).thenReturn(result); + String groupKey = GroupKey2.getKey("test", "test", "test"); + Set listenersClients = new HashSet<>(); + String connectionId = "127.0.0.1"; + listenersClients.add(connectionId); + when(configChangeListenContext.getListeners(groupKey)).thenReturn(listenersClients); + ConnectionMeta connectionMeta = new ConnectionMeta(connectionId, connectionId, connectionId, 8888, 9848, "GRPC", "", "", + new HashMap<>()); + Connection client = new GrpcConnection(connectionMeta, null, null); + when(connectionManager.getConnection(connectionId)).thenReturn(client); + when(configChangeListenContext.getListenKeyMd5(connectionId, groupKey)).thenReturn("md5"); + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.COMMUNICATION_CONTROLLER_PATH + "/config") + .param("type", ConfigSearchRequestTypeEnum.CONFIG.getType()) + .param("dataId", "test") + .param("group", "test") + .param("tenant", "test"); + String actualValue = mockMvc.perform(builder).andReturn().getResponse().getContentAsString(); + assertEquals("{\"127.0.0.1\":\"md5\"}", JacksonUtils.toObj(actualValue).get("lisentersGroupkeyStatus").toString()); + + } + + @Test + void testGetSubClientConfigV22x() throws Exception { + + String ip = "127.0.0.1"; + SampleResult result = new SampleResult(); + result.setLisentersGroupkeyStatus(new HashMap<>()); + when(longPollingService.getCollectSubscribleInfoByIp(ip)).thenReturn(result); + ConnectionMeta connectionMeta = new ConnectionMeta(ip, ip, ip, 8888, 9848, "GRPC", "", "", new HashMap<>()); + Connection connection = new GrpcConnection(connectionMeta, null, null); + List connectionList = new ArrayList<>(); + connectionList.add(connection); + when(connectionManager.getConnectionByIp(ip)).thenReturn(connectionList); + Map map = new HashMap<>(); + map.put("test", "test"); + when(configChangeListenContext.getListenKeys(ip)).thenReturn(map); + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.COMMUNICATION_CONTROLLER_PATH + "/watcherConfigs") + .param("type", ConfigSearchRequestTypeEnum.IP.getType()) + .param("ip", ip); + String actualValue = mockMvc.perform(builder).andReturn().getResponse().getContentAsString(); + assertEquals("{\"test\":\"test\"}", JacksonUtils.toObj(actualValue).get("lisentersGroupkeyStatus").toString()); + + } }