Skip to content

Commit

Permalink
[ISSUE #12282]Fix the issue where monitoring data cannot be found thr…
Browse files Browse the repository at this point in the history
…ough the IP dimension in the ListeningQuery
  • Loading branch information
HMYDK committed Jun 27, 2024
1 parent b8d13e0 commit 1b78e43
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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<Connection> connectionsByIp = connectionManager.getConnectionByIp(ip);
for (Connection connectionByIp : connectionsByIp) {
Map<String, String> listenKeys = configChangeListenContext
.getListenKeys(connectionByIp.getMetaInfo().getConnectionId());
Map<String, String> 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();
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,7 +86,7 @@ private List<SampleResult> runConfigListenerCollectionJob(Map<String, String> pa

static class ClusterListenerJob extends ClusterJob<SampleResult> {

static final String URL = Constants.COMMUNICATION_CONTROLLER_PATH + "/configWatchers";
static final String URL = Constants.COMMUNICATION_CONTROLLER_PATH + "/config";

ClusterListenerJob(Map<String, String> params, CompletionService<SampleResult> completionService,
ServerMemberManager serverMemberManager) {
Expand Down Expand Up @@ -295,7 +296,8 @@ public SampleResult mergeSampleResult(SampleResult sampleCollectResult, List<Sam

public SampleResult getCollectSampleResult(String dataId, String group, String tenant, int sampleTime)
throws Exception {
Map<String, String> params = new HashMap<>(5);
Map<String, String> params = new HashMap<>(4);
params.put("type", ConfigSearchRequestTypeEnum.CONFIG.getType());
params.put("dataId", dataId);
params.put("group", group);
if (!StringUtils.isBlank(tenant)) {
Expand All @@ -316,7 +318,8 @@ public SampleResult getCollectSampleResult(String dataId, String group, String t
}

public SampleResult getCollectSampleResultByIp(String ip, int sampleTime) {
Map<String, String> params = new HashMap<>(50);
Map<String, String> params = new HashMap<>(2);
params.put("type", ConfigSearchRequestTypeEnum.IP.getType());
params.put("ip", ip);
BlockingQueue<Future<SampleResult>> queue = new LinkedBlockingDeque<>(memberManager.getServerList().size());
CompletionService<SampleResult> completionService = new ExecutorCompletionService<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class LongPollingService {

private Map<String, Long> 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<String, String> lisentersGroupkeyStatus = new HashMap<>(50);
Expand Down Expand Up @@ -123,7 +123,7 @@ public SampleResult mergeSampleResult(List<SampleResult> sampleResults) {
public SampleResult getCollectSubscribleInfo(String dataId, String group, String tenant) {
List<SampleResult> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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<Connection> connectionList = new ArrayList<>();
connectionList.add(connection);
when(connectionManager.getConnectionByIp(ip)).thenReturn(connectionList);
Map<String, String> 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());

}
}

0 comments on commit 1b78e43

Please sign in to comment.