use of com.alibaba.nacos.config.server.model.SampleResult in project nacos by alibaba.
the class ListenerControllerTest method testGetAllSubClientConfigByIp.
@Test
public void testGetAllSubClientConfigByIp() throws Exception {
SampleResult sampleResult = new SampleResult();
Map<String, String> map = new HashMap<>();
map.put("test", "test");
sampleResult.setLisentersGroupkeyStatus(map);
when(configSubService.getCollectSampleResultByIp("localhost", 1)).thenReturn(sampleResult);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.LISTENER_CONTROLLER_PATH).param("ip", "localhost").param("all", "true").param("tenant", "test").param("sampleTime", "1");
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
GroupkeyListenserStatus groupkeyListenserStatus = JacksonUtils.toObj(actualValue, GroupkeyListenserStatus.class);
Map<String, String> resultMap = groupkeyListenserStatus.getLisentersGroupkeyStatus();
Assert.assertEquals(map.get("test"), resultMap.get("test"));
}
use of com.alibaba.nacos.config.server.model.SampleResult in project nacos by alibaba.
the class CommunicationControllerTest method testGetSubClientConfigByIp.
@Test
public void testGetSubClientConfigByIp() 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("ip", ip);
String actualValue = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("{\"test\":\"test\"}", JacksonUtils.toObj(actualValue).get("lisentersGroupkeyStatus").toString());
}
use of com.alibaba.nacos.config.server.model.SampleResult in project nacos by alibaba.
the class ConfigSubService method mergeSampleResult.
/**
* Merge SampleResult.
*
* @param sampleCollectResult sampleCollectResult.
* @param sampleResults sampleResults.
* @return SampleResult.
*/
public SampleResult mergeSampleResult(SampleResult sampleCollectResult, List<SampleResult> sampleResults) {
SampleResult mergeResult = new SampleResult();
Map<String, String> listenersGroupkeyStatus;
if (sampleCollectResult.getLisentersGroupkeyStatus() == null || sampleCollectResult.getLisentersGroupkeyStatus().isEmpty()) {
listenersGroupkeyStatus = new HashMap<>(10);
} else {
listenersGroupkeyStatus = sampleCollectResult.getLisentersGroupkeyStatus();
}
for (SampleResult sampleResult : sampleResults) {
Map<String, String> listenersGroupkeyStatusTmp = sampleResult.getLisentersGroupkeyStatus();
listenersGroupkeyStatus.putAll(listenersGroupkeyStatusTmp);
}
mergeResult.setLisentersGroupkeyStatus(listenersGroupkeyStatus);
return mergeResult;
}
use of com.alibaba.nacos.config.server.model.SampleResult in project nacos by alibaba.
the class ConfigSubService method runCollectionJob.
private List<SampleResult> runCollectionJob(String url, Map<String, String> params, CompletionService<SampleResult> completionService, List<SampleResult> resultList) {
Collection<Member> ipList = memberManager.allMembers();
List<SampleResult> collectionResult = new ArrayList<>(ipList.size());
// Submit query task.
for (Member ip : ipList) {
try {
completionService.submit(new Job(ip.getAddress(), url, params));
} catch (Exception e) {
// Send request failed.
LogUtil.DEFAULT_LOG.warn("Get client info from {} with exception: {} during submit job", ip, e.getMessage());
}
}
// Get and merge result.
SampleResult sampleResults;
for (Member member : ipList) {
try {
Future<SampleResult> f = completionService.poll(1000, TimeUnit.MILLISECONDS);
try {
if (f != null) {
sampleResults = f.get(500, TimeUnit.MILLISECONDS);
if (sampleResults != null) {
collectionResult.add(sampleResults);
}
} else {
LogUtil.DEFAULT_LOG.warn("The task in ip: {} did not completed in 1000ms ", member);
}
} catch (TimeoutException e) {
f.cancel(true);
LogUtil.DEFAULT_LOG.warn("get task result with TimeoutException: {} ", e.getMessage());
}
} catch (InterruptedException e) {
LogUtil.DEFAULT_LOG.warn("get task result with InterruptedException: {} ", e.getMessage());
} catch (ExecutionException e) {
LogUtil.DEFAULT_LOG.warn("get task result with ExecutionException: {} ", e.getMessage());
}
}
return collectionResult;
}
use of com.alibaba.nacos.config.server.model.SampleResult in project nacos by alibaba.
the class ConfigSubService method getCollectSampleResult.
public SampleResult getCollectSampleResult(String dataId, String group, String tenant, int sampleTime) throws Exception {
List<SampleResult> resultList = new ArrayList<>();
String url = Constants.COMMUNICATION_CONTROLLER_PATH + "/configWatchers";
Map<String, String> params = new HashMap<>(5);
params.put("dataId", dataId);
params.put("group", group);
if (!StringUtils.isBlank(tenant)) {
params.put("tenant", tenant);
}
BlockingQueue<Future<SampleResult>> queue = new LinkedBlockingDeque<>(memberManager.getServerList().size());
CompletionService<SampleResult> completionService = new ExecutorCompletionService<>(ConfigExecutor.getConfigSubServiceExecutor(), queue);
SampleResult sampleCollectResult = new SampleResult();
for (int i = 0; i < sampleTime; i++) {
List<SampleResult> sampleResults = runCollectionJob(url, params, completionService, resultList);
sampleCollectResult = mergeSampleResult(sampleCollectResult, sampleResults);
}
return sampleCollectResult;
}
Aggregations