Search in sources :

Example 1 with ServerLoaderInfoResponse

use of com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse in project nacos by alibaba.

the class ServerLoaderControllerTest method testSmartReload.

@Test
public void testSmartReload() throws NacosException {
    EnvUtil.setEnvironment(new MockEnvironment());
    Member member = new Member();
    member.setIp("1.1.1.1");
    member.setPort(8848);
    ServerAbilities serverAbilities = new ServerAbilities();
    ServerRemoteAbility serverRemoteAbility = new ServerRemoteAbility();
    serverRemoteAbility.setSupportRemoteConnection(true);
    serverAbilities.setRemoteAbility(serverRemoteAbility);
    member.setAbilities(serverAbilities);
    Mockito.when(serverMemberManager.allMembersWithoutSelf()).thenReturn(Collections.singletonList(member));
    Map<String, String> metrics = new HashMap<>();
    metrics.put("conCount", "1");
    metrics.put("sdkConCount", "1");
    ServerLoaderInfoResponse serverLoaderInfoResponse = new ServerLoaderInfoResponse();
    serverLoaderInfoResponse.setLoaderMetrics(metrics);
    Mockito.when(serverLoaderInfoRequestHandler.handle(Mockito.any(), Mockito.any())).thenReturn(serverLoaderInfoResponse);
    Mockito.when(serverMemberManager.getSelf()).thenReturn(member);
    MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    ResponseEntity<String> result = serverLoaderController.smartReload(httpServletRequest, "1", null);
    Assert.assertEquals("Ok", result.getBody());
}
Also used : HashMap(java.util.HashMap) MockEnvironment(org.springframework.mock.env.MockEnvironment) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServerLoaderInfoResponse(com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse) ServerRemoteAbility(com.alibaba.nacos.api.remote.ability.ServerRemoteAbility) ServerAbilities(com.alibaba.nacos.api.ability.ServerAbilities) Member(com.alibaba.nacos.core.cluster.Member) Test(org.junit.Test)

Example 2 with ServerLoaderInfoResponse

use of com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse in project nacos by alibaba.

the class ServerLoaderController method getServerLoadMetrics.

private Map<String, Object> getServerLoadMetrics() {
    List<ServerLoaderMetrics> responseList = new LinkedList<>();
    // default include self.
    int memberSize = serverMemberManager.allMembersWithoutSelf().size();
    CountDownLatch countDownLatch = new CountDownLatch(memberSize);
    for (Member member : serverMemberManager.allMembersWithoutSelf()) {
        if (MemberUtil.isSupportedLongCon(member)) {
            ServerLoaderInfoRequest serverLoaderInfoRequest = new ServerLoaderInfoRequest();
            try {
                clusterRpcClientProxy.asyncRequest(member, serverLoaderInfoRequest, new RequestCallBack() {

                    @Override
                    public Executor getExecutor() {
                        return null;
                    }

                    @Override
                    public long getTimeout() {
                        return 200L;
                    }

                    @Override
                    public void onResponse(Response response) {
                        if (response instanceof ServerLoaderInfoResponse) {
                            ServerLoaderMetrics metrics = new ServerLoaderMetrics();
                            metrics.setAddress(member.getAddress());
                            metrics.setMetric(((ServerLoaderInfoResponse) response).getLoaderMetrics());
                            responseList.add(metrics);
                        }
                        countDownLatch.countDown();
                    }

                    @Override
                    public void onException(Throwable e) {
                        LOGGER.error("Get metrics fail,member={}", member.getAddress(), e);
                        countDownLatch.countDown();
                    }
                });
            } catch (NacosException e) {
                LOGGER.error("Get metrics fail,member={}", member.getAddress(), e);
                countDownLatch.countDown();
            }
        } else {
            countDownLatch.countDown();
        }
    }
    try {
        ServerLoaderInfoResponse handle = serverLoaderInfoRequestHandler.handle(new ServerLoaderInfoRequest(), new RequestMeta());
        ServerLoaderMetrics metrics = new ServerLoaderMetrics();
        metrics.setAddress(serverMemberManager.getSelf().getAddress());
        metrics.setMetric(handle.getLoaderMetrics());
        responseList.add(metrics);
    } catch (NacosException e) {
        LOGGER.error("Get self metrics fail", e);
    }
    try {
        countDownLatch.await(1000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        LOGGER.warn("Get  metrics timeout,metrics info may not complete.");
    }
    int max = 0;
    int min = -1;
    int total = 0;
    for (ServerLoaderMetrics serverLoaderMetrics : responseList) {
        String sdkConCountStr = serverLoaderMetrics.getMetric().get("sdkConCount");
        if (StringUtils.isNotBlank(sdkConCountStr)) {
            int sdkConCount = Integer.parseInt(sdkConCountStr);
            if (max == 0 || max < sdkConCount) {
                max = sdkConCount;
            }
            if (min == -1 || sdkConCount < min) {
                min = sdkConCount;
            }
            total += sdkConCount;
        }
    }
    Map<String, Object> responseMap = new HashMap<>(9);
    responseList.sort(Comparator.comparing(ServerLoaderMetrics::getAddress));
    responseMap.put("detail", responseList);
    responseMap.put("memberCount", serverMemberManager.allMembers().size());
    responseMap.put("metricsCount", responseList.size());
    responseMap.put("completed", responseList.size() == serverMemberManager.allMembers().size());
    responseMap.put("max", max);
    responseMap.put("min", min);
    responseMap.put("avg", total / responseList.size());
    responseMap.put("threshold", total / responseList.size() * 1.1);
    responseMap.put("total", total);
    return responseMap;
}
Also used : RequestCallBack(com.alibaba.nacos.api.remote.RequestCallBack) HashMap(java.util.HashMap) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) NacosException(com.alibaba.nacos.api.exception.NacosException) ServerLoaderInfoResponse(com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse) Response(com.alibaba.nacos.api.remote.response.Response) Executor(java.util.concurrent.Executor) ServerLoaderInfoResponse(com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse) ServerLoaderInfoRequest(com.alibaba.nacos.api.remote.request.ServerLoaderInfoRequest) Member(com.alibaba.nacos.core.cluster.Member)

Example 3 with ServerLoaderInfoResponse

use of com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse in project nacos by alibaba.

the class ServerLoaderControllerTest method testLoaderMetrics.

@Test
public void testLoaderMetrics() throws NacosException {
    EnvUtil.setEnvironment(new MockEnvironment());
    Member member = new Member();
    member.setIp("1.1.1.1");
    member.setPort(8848);
    ServerAbilities serverAbilities = new ServerAbilities();
    ServerRemoteAbility serverRemoteAbility = new ServerRemoteAbility();
    serverRemoteAbility.setSupportRemoteConnection(true);
    serverAbilities.setRemoteAbility(serverRemoteAbility);
    member.setAbilities(serverAbilities);
    Mockito.when(serverMemberManager.allMembersWithoutSelf()).thenReturn(Collections.singletonList(member));
    Map<String, String> metrics = new HashMap<>();
    metrics.put("conCount", "1");
    ServerLoaderInfoResponse serverLoaderInfoResponse = new ServerLoaderInfoResponse();
    serverLoaderInfoResponse.setLoaderMetrics(metrics);
    Mockito.when(serverLoaderInfoRequestHandler.handle(Mockito.any(), Mockito.any())).thenReturn(serverLoaderInfoResponse);
    Mockito.when(serverMemberManager.getSelf()).thenReturn(member);
    ResponseEntity<Map<String, Object>> result = serverLoaderController.loaderMetrics();
    Assert.assertEquals(9, result.getBody().size());
}
Also used : HashMap(java.util.HashMap) MockEnvironment(org.springframework.mock.env.MockEnvironment) ServerLoaderInfoResponse(com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse) ServerRemoteAbility(com.alibaba.nacos.api.remote.ability.ServerRemoteAbility) ServerAbilities(com.alibaba.nacos.api.ability.ServerAbilities) Member(com.alibaba.nacos.core.cluster.Member) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with ServerLoaderInfoResponse

use of com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse in project nacos by alibaba.

the class ServerLoaderInfoRequestHandlerTest method testHandle.

@Test
public void testHandle() {
    Mockito.when(connectionManager.currentClientsCount()).thenReturn(1);
    Mockito.when(connectionManager.currentClientsCount(Mockito.any())).thenReturn(1);
    Mockito.when(connectionManager.getConnectionLimitRule()).thenReturn(null);
    ServerLoaderInfoRequest request = new ServerLoaderInfoRequest();
    RequestMeta meta = new RequestMeta();
    try {
        ServerLoaderInfoResponse response = handler.handle(request, meta);
        String sdkConCount = response.getMetricsValue("sdkConCount");
        Assert.assertEquals(sdkConCount, "1");
    } catch (NacosException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) ServerLoaderInfoResponse(com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse) ServerLoaderInfoRequest(com.alibaba.nacos.api.remote.request.ServerLoaderInfoRequest) NacosException(com.alibaba.nacos.api.exception.NacosException) Test(org.junit.Test)

Example 5 with ServerLoaderInfoResponse

use of com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse in project nacos by alibaba.

the class ServerLoaderInfoRequestHandler method handle.

@Override
public ServerLoaderInfoResponse handle(ServerLoaderInfoRequest request, RequestMeta meta) throws NacosException {
    ServerLoaderInfoResponse serverLoaderInfoResponse = new ServerLoaderInfoResponse();
    serverLoaderInfoResponse.putMetricsValue("conCount", String.valueOf(connectionManager.currentClientsCount()));
    Map<String, String> filter = new HashMap<String, String>(2);
    filter.put(RemoteConstants.LABEL_SOURCE, RemoteConstants.LABEL_SOURCE_SDK);
    serverLoaderInfoResponse.putMetricsValue("sdkConCount", String.valueOf(connectionManager.currentClientsCount(filter)));
    serverLoaderInfoResponse.putMetricsValue("limitRule", JacksonUtils.toJson(connectionManager.getConnectionLimitRule()));
    serverLoaderInfoResponse.putMetricsValue("load", String.valueOf(EnvUtil.getLoad()));
    serverLoaderInfoResponse.putMetricsValue("cpu", String.valueOf(EnvUtil.getCPU()));
    return serverLoaderInfoResponse;
}
Also used : HashMap(java.util.HashMap) ServerLoaderInfoResponse(com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse)

Aggregations

ServerLoaderInfoResponse (com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse)5 HashMap (java.util.HashMap)4 Member (com.alibaba.nacos.core.cluster.Member)3 Test (org.junit.Test)3 ServerAbilities (com.alibaba.nacos.api.ability.ServerAbilities)2 NacosException (com.alibaba.nacos.api.exception.NacosException)2 ServerRemoteAbility (com.alibaba.nacos.api.remote.ability.ServerRemoteAbility)2 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)2 ServerLoaderInfoRequest (com.alibaba.nacos.api.remote.request.ServerLoaderInfoRequest)2 MockEnvironment (org.springframework.mock.env.MockEnvironment)2 RequestCallBack (com.alibaba.nacos.api.remote.RequestCallBack)1 Response (com.alibaba.nacos.api.remote.response.Response)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executor (java.util.concurrent.Executor)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1