Search in sources :

Example 6 with SampleResult

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"));
}
Also used : GroupkeyListenserStatus(com.alibaba.nacos.config.server.model.GroupkeyListenserStatus) HashMap(java.util.HashMap) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) SampleResult(com.alibaba.nacos.config.server.model.SampleResult) Test(org.junit.Test)

Example 7 with SampleResult

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());
}
Also used : HashMap(java.util.HashMap) GrpcConnection(com.alibaba.nacos.core.remote.grpc.GrpcConnection) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) GrpcConnection(com.alibaba.nacos.core.remote.grpc.GrpcConnection) Connection(com.alibaba.nacos.core.remote.Connection) ArrayList(java.util.ArrayList) SampleResult(com.alibaba.nacos.config.server.model.SampleResult) ConnectionMeta(com.alibaba.nacos.core.remote.ConnectionMeta) Test(org.junit.Test)

Example 8 with SampleResult

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;
}
Also used : SampleResult(com.alibaba.nacos.config.server.model.SampleResult)

Example 9 with SampleResult

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;
}
Also used : ArrayList(java.util.ArrayList) SampleResult(com.alibaba.nacos.config.server.model.SampleResult) ExecutionException(java.util.concurrent.ExecutionException) Member(com.alibaba.nacos.core.cluster.Member) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 10 with SampleResult

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;
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) SampleResult(com.alibaba.nacos.config.server.model.SampleResult) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService)

Aggregations

SampleResult (com.alibaba.nacos.config.server.model.SampleResult)17 HashMap (java.util.HashMap)11 Test (org.junit.Test)5 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)5 GroupkeyListenserStatus (com.alibaba.nacos.config.server.model.GroupkeyListenserStatus)4 Connection (com.alibaba.nacos.core.remote.Connection)4 ArrayList (java.util.ArrayList)4 GetMapping (org.springframework.web.bind.annotation.GetMapping)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConnectionMeta (com.alibaba.nacos.core.remote.ConnectionMeta)2 GrpcConnection (com.alibaba.nacos.core.remote.grpc.GrpcConnection)2 Map (java.util.Map)2 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)2 Future (java.util.concurrent.Future)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2 Secured (com.alibaba.nacos.auth.annotation.Secured)1 Member (com.alibaba.nacos.core.cluster.Member)1 HashSet (java.util.HashSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1