Search in sources :

Example 1 with ServerReloadRequest

use of com.alibaba.nacos.api.remote.request.ServerReloadRequest in project nacos by alibaba.

the class ServerLoaderController method smartReload.

/**
 * Get server state of current server.
 *
 * @return state json.
 */
@Secured(resource = Commons.NACOS_CORE_CONTEXT_V2 + "/loader", action = ActionTypes.WRITE)
@GetMapping("/smartReloadCluster")
public ResponseEntity<String> smartReload(HttpServletRequest request, @RequestParam(value = "loaderFactor", required = false) String loaderFactorStr, @RequestParam(value = "force", required = false) String force) {
    LOGGER.info("Smart reload request receive,requestIp={}", getRemoteIp(request));
    Map<String, Object> serverLoadMetrics = getServerLoadMetrics();
    Object avgString = serverLoadMetrics.get("avg");
    List<ServerLoaderMetrics> details = (List<ServerLoaderMetrics>) serverLoadMetrics.get("detail");
    int avg = Integer.parseInt(avgString.toString());
    float loaderFactor = StringUtils.isBlank(loaderFactorStr) ? RemoteUtils.LOADER_FACTOR : Float.parseFloat(loaderFactorStr);
    int overLimitCount = (int) (avg * (1 + loaderFactor));
    int lowLimitCount = (int) (avg * (1 - loaderFactor));
    List<ServerLoaderMetrics> overLimitServer = new ArrayList<>();
    List<ServerLoaderMetrics> lowLimitServer = new ArrayList<>();
    for (ServerLoaderMetrics metrics : details) {
        int sdkCount = Integer.parseInt(metrics.getMetric().get(SDK_CONNECTION_COUNT_METRIC));
        if (sdkCount > overLimitCount) {
            overLimitServer.add(metrics);
        }
        if (sdkCount < lowLimitCount) {
            lowLimitServer.add(metrics);
        }
    }
    // desc by sdkConCount
    overLimitServer.sort((o1, o2) -> {
        Integer sdkCount1 = Integer.valueOf(o1.getMetric().get(SDK_CONNECTION_COUNT_METRIC));
        Integer sdkCount2 = Integer.valueOf(o2.getMetric().get(SDK_CONNECTION_COUNT_METRIC));
        return sdkCount1.compareTo(sdkCount2) * -1;
    });
    LOGGER.info("Over load limit server list ={}", overLimitServer);
    // asc by sdkConCount
    lowLimitServer.sort((o1, o2) -> {
        Integer sdkCount1 = Integer.valueOf(o1.getMetric().get(SDK_CONNECTION_COUNT_METRIC));
        Integer sdkCount2 = Integer.valueOf(o2.getMetric().get(SDK_CONNECTION_COUNT_METRIC));
        return sdkCount1.compareTo(sdkCount2);
    });
    LOGGER.info("Low load limit server list ={}", lowLimitServer);
    AtomicBoolean result = new AtomicBoolean(true);
    for (int i = 0; i < overLimitServer.size() & i < lowLimitServer.size(); i++) {
        ServerReloadRequest serverLoaderInfoRequest = new ServerReloadRequest();
        serverLoaderInfoRequest.setReloadCount(overLimitCount);
        serverLoaderInfoRequest.setReloadServer(lowLimitServer.get(i).address);
        Member member = serverMemberManager.find(overLimitServer.get(i).address);
        LOGGER.info("Reload task submit ,fromServer ={},toServer={}, ", overLimitServer.get(i).address, lowLimitServer.get(i).address);
        if (serverMemberManager.getSelf().equals(member)) {
            try {
                serverReloaderRequestHandler.handle(serverLoaderInfoRequest, new RequestMeta());
            } catch (NacosException e) {
                LOGGER.error("Fail to loader self server", e);
                result.set(false);
            }
        } else {
            try {
                clusterRpcClientProxy.asyncRequest(member, serverLoaderInfoRequest, new RequestCallBack() {

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

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

                    @Override
                    public void onResponse(Response response) {
                        if (response == null || !response.isSuccess()) {
                            LOGGER.error("Fail to loader member={},response={}", member.getAddress(), response);
                            result.set(false);
                        }
                    }

                    @Override
                    public void onException(Throwable e) {
                        LOGGER.error("Fail to loader member={}", member.getAddress(), e);
                        result.set(false);
                    }
                });
            } catch (NacosException e) {
                LOGGER.error("Fail to loader member={}", member.getAddress(), e);
                result.set(false);
            }
        }
    }
    return ResponseEntity.ok().body(result.get() ? SUCCESS_RESULT : FAIL_RESULT);
}
Also used : RequestCallBack(com.alibaba.nacos.api.remote.RequestCallBack) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) ArrayList(java.util.ArrayList) NacosException(com.alibaba.nacos.api.exception.NacosException) ServerLoaderInfoResponse(com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse) Response(com.alibaba.nacos.api.remote.response.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Executor(java.util.concurrent.Executor) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ServerReloadRequest(com.alibaba.nacos.api.remote.request.ServerReloadRequest) Member(com.alibaba.nacos.core.cluster.Member) GetMapping(org.springframework.web.bind.annotation.GetMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 2 with ServerReloadRequest

use of com.alibaba.nacos.api.remote.request.ServerReloadRequest in project nacos by alibaba.

the class ServerReloaderRequestHandlerTest method testHandle.

@Test
public void testHandle() {
    Mockito.when(connectionManager.currentClientsCount(Mockito.any())).thenReturn(2);
    ServerReloadRequest reloadRequest = new ServerReloadRequest();
    reloadRequest.setReloadCount(2);
    reloadRequest.setReloadServer("test");
    RequestMeta meta = new RequestMeta();
    meta.setClientIp("1.1.1.1");
    try {
        ServerReloadResponse reloadResponse = handler.handle(reloadRequest, meta);
        Assert.assertEquals("ignore", reloadResponse.getMessage());
    } catch (NacosException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
    reloadRequest.setReloadCount(1);
    try {
        ServerReloadResponse reloadResponse = handler.handle(reloadRequest, meta);
        Assert.assertEquals("ok", reloadResponse.getMessage());
    } catch (NacosException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ServerReloadResponse(com.alibaba.nacos.api.remote.response.ServerReloadResponse) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) ServerReloadRequest(com.alibaba.nacos.api.remote.request.ServerReloadRequest) NacosException(com.alibaba.nacos.api.exception.NacosException) Test(org.junit.Test)

Aggregations

NacosException (com.alibaba.nacos.api.exception.NacosException)2 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)2 ServerReloadRequest (com.alibaba.nacos.api.remote.request.ServerReloadRequest)2 RequestCallBack (com.alibaba.nacos.api.remote.RequestCallBack)1 Response (com.alibaba.nacos.api.remote.response.Response)1 ServerLoaderInfoResponse (com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse)1 ServerReloadResponse (com.alibaba.nacos.api.remote.response.ServerReloadResponse)1 Secured (com.alibaba.nacos.auth.annotation.Secured)1 Member (com.alibaba.nacos.core.cluster.Member)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Executor (java.util.concurrent.Executor)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1