Search in sources :

Example 1 with SampleResult

use of com.alibaba.nacos.config.server.model.SampleResult in project nacos by alibaba.

the class ConfigSubService method getCollectSampleResultByIp.

public SampleResult getCollectSampleResultByIp(String ip, int sampleTime) {
    List<SampleResult> resultList = new ArrayList<>(10);
    String url = Constants.COMMUNICATION_CONTROLLER_PATH + "/watcherConfigs";
    Map<String, String> params = new HashMap<>(50);
    params.put("ip", ip);
    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)

Example 2 with SampleResult

use of com.alibaba.nacos.config.server.model.SampleResult in project nacos by alibaba.

the class LongPollingService method mergeSampleResult.

/**
 * Aggregate the sampling IP and monitoring configuration information in the sampling results. There is no problem
 * for the merging strategy to cover the previous one with the latter.
 *
 * @param sampleResults sample Results.
 * @return Results.
 */
public SampleResult mergeSampleResult(List<SampleResult> sampleResults) {
    SampleResult mergeResult = new SampleResult();
    Map<String, String> lisentersGroupkeyStatus = new HashMap<String, String>(50);
    for (SampleResult sampleResult : sampleResults) {
        Map<String, String> lisentersGroupkeyStatusTmp = sampleResult.getLisentersGroupkeyStatus();
        for (Map.Entry<String, String> entry : lisentersGroupkeyStatusTmp.entrySet()) {
            lisentersGroupkeyStatus.put(entry.getKey(), entry.getValue());
        }
    }
    mergeResult.setLisentersGroupkeyStatus(lisentersGroupkeyStatus);
    return mergeResult;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SampleResult(com.alibaba.nacos.config.server.model.SampleResult) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with SampleResult

use of com.alibaba.nacos.config.server.model.SampleResult in project nacos by alibaba.

the class CommunicationControllerTest method testGetSubClientConfig2x.

@Test
public void testGetSubClientConfig2x() 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 + "/configWatchers").param("dataId", "test").param("group", "test").param("tenant", "test");
    String actualValue = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    Assert.assertEquals("{\"127.0.0.1\":\"md5\"}", JacksonUtils.toObj(actualValue).get("lisentersGroupkeyStatus").toString());
}
Also used : 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) SampleResult(com.alibaba.nacos.config.server.model.SampleResult) ConnectionMeta(com.alibaba.nacos.core.remote.ConnectionMeta) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with SampleResult

use of com.alibaba.nacos.config.server.model.SampleResult in project nacos by alibaba.

the class CommunicationControllerTest method testGetSubClientConfig1x.

@Test
public void testGetSubClientConfig1x() throws Exception {
    SampleResult result = new SampleResult();
    Map<String, String> lisentersGroupkeyStatus = new HashMap<>();
    lisentersGroupkeyStatus.put("test", "test");
    result.setLisentersGroupkeyStatus(lisentersGroupkeyStatus);
    when(longPollingService.getCollectSubscribleInfo("test", "test", "test")).thenReturn(result);
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.COMMUNICATION_CONTROLLER_PATH + "/configWatchers").param("dataId", "test").param("group", "test").param("tenant", "test");
    String actualValue = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    Assert.assertEquals("{\"test\":\"test\"}", JacksonUtils.toObj(actualValue).get("lisentersGroupkeyStatus").toString());
}
Also used : 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 5 with SampleResult

use of com.alibaba.nacos.config.server.model.SampleResult in project nacos by alibaba.

the class ConfigControllerTest method testGetListeners.

@Test
public void testGetListeners() throws Exception {
    Map<String, String> listenersGroupkeyStatus = new HashMap<>();
    listenersGroupkeyStatus.put("test", "test");
    SampleResult sampleResult = new SampleResult();
    sampleResult.setLisentersGroupkeyStatus(listenersGroupkeyStatus);
    when(configSubService.getCollectSampleResult("test", "test", "", 1)).thenReturn(sampleResult);
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CONFIG_CONTROLLER_PATH + "/listener").param("dataId", "test").param("group", "test").param("tenant", "").param("sampleTime", "1");
    String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
    GroupkeyListenserStatus groupkeyListenserStatus = JacksonUtils.toObj(actualValue, GroupkeyListenserStatus.class);
    Assert.assertEquals(200, groupkeyListenserStatus.getCollectStatus());
    Assert.assertEquals(1, groupkeyListenserStatus.getLisentersGroupkeyStatus().size());
    Assert.assertEquals("test", groupkeyListenserStatus.getLisentersGroupkeyStatus().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) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

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