Search in sources :

Example 21 with Response

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

the class DistroClientTransportAgent method getDatumSnapshot.

@Override
public DistroData getDatumSnapshot(String targetServer) {
    Member member = memberManager.find(targetServer);
    if (checkTargetServerStatusUnhealthy(member)) {
        throw new DistroException(String.format("[DISTRO] Cancel get snapshot caused by target server %s unhealthy", targetServer));
    }
    DistroDataRequest request = new DistroDataRequest();
    request.setDataOperation(DataOperation.SNAPSHOT);
    try {
        Response response = clusterRpcClientProxy.sendRequest(member, request);
        if (checkResponse(response)) {
            return ((DistroDataResponse) response).getDistroData();
        } else {
            throw new DistroException(String.format("[DISTRO-FAILED] Get snapshot request to %s failed, code: %d, message: %s", targetServer, response.getErrorCode(), response.getMessage()));
        }
    } catch (NacosException e) {
        throw new DistroException("[DISTRO-FAILED] Get distro snapshot failed! ", e);
    }
}
Also used : DistroException(com.alibaba.nacos.core.distributed.distro.exception.DistroException) Response(com.alibaba.nacos.api.remote.response.Response) DistroDataResponse(com.alibaba.nacos.naming.cluster.remote.response.DistroDataResponse) DistroDataRequest(com.alibaba.nacos.naming.cluster.remote.request.DistroDataRequest) Member(com.alibaba.nacos.core.cluster.Member) DistroDataResponse(com.alibaba.nacos.naming.cluster.remote.response.DistroDataResponse) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 22 with Response

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

the class DistroClientTransportAgent method syncVerifyData.

@Override
public boolean syncVerifyData(DistroData verifyData, String targetServer) {
    if (isNoExistTarget(targetServer)) {
        return true;
    }
    // replace target server as self server so that can callback.
    verifyData.getDistroKey().setTargetServer(memberManager.getSelf().getAddress());
    DistroDataRequest request = new DistroDataRequest(verifyData, DataOperation.VERIFY);
    Member member = memberManager.find(targetServer);
    if (checkTargetServerStatusUnhealthy(member)) {
        Loggers.DISTRO.warn("[DISTRO] Cancel distro verify caused by target server {} unhealthy", targetServer);
        return false;
    }
    try {
        Response response = clusterRpcClientProxy.sendRequest(member, request);
        return checkResponse(response);
    } catch (NacosException e) {
        Loggers.DISTRO.error("[DISTRO-FAILED] Verify distro data failed! ", e);
    }
    return false;
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) DistroDataResponse(com.alibaba.nacos.naming.cluster.remote.response.DistroDataResponse) DistroDataRequest(com.alibaba.nacos.naming.cluster.remote.request.DistroDataRequest) Member(com.alibaba.nacos.core.cluster.Member) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 23 with Response

use of com.alibaba.nacos.api.remote.response.Response 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 24 with Response

use of com.alibaba.nacos.api.remote.response.Response 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 25 with Response

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

the class ConnectionManager method start.

/**
 * Start Task:Expel the connection which active Time expire.
 */
@PostConstruct
public void start() {
    // Start UnHealthy Connection Expel Task.
    RpcScheduledExecutor.COMMON_SERVER_EXECUTOR.scheduleWithFixedDelay(new Runnable() {

        @Override
        public void run() {
            try {
                int totalCount = connections.size();
                Loggers.REMOTE_DIGEST.info("Connection check task start");
                MetricsMonitor.getLongConnectionMonitor().set(totalCount);
                Set<Map.Entry<String, Connection>> entries = connections.entrySet();
                int currentSdkClientCount = currentSdkClientCount();
                boolean isLoaderClient = loadClient >= 0;
                int currentMaxClient = isLoaderClient ? loadClient : connectionLimitRule.countLimit;
                int expelCount = currentMaxClient < 0 ? 0 : Math.max(currentSdkClientCount - currentMaxClient, 0);
                Loggers.REMOTE_DIGEST.info("Total count ={}, sdkCount={},clusterCount={}, currentLimit={}, toExpelCount={}", totalCount, currentSdkClientCount, (totalCount - currentSdkClientCount), currentMaxClient + (isLoaderClient ? "(loaderCount)" : ""), expelCount);
                List<String> expelClient = new LinkedList<>();
                Map<String, AtomicInteger> expelForIp = new HashMap<>(16);
                // 1. calculate expel count  of ip.
                for (Map.Entry<String, Connection> entry : entries) {
                    Connection client = entry.getValue();
                    String appName = client.getMetaInfo().getAppName();
                    String clientIp = client.getMetaInfo().getClientIp();
                    if (client.getMetaInfo().isSdkSource() && !expelForIp.containsKey(clientIp)) {
                        // get limit for current ip.
                        int countLimitOfIp = connectionLimitRule.getCountLimitOfIp(clientIp);
                        if (countLimitOfIp < 0) {
                            int countLimitOfApp = connectionLimitRule.getCountLimitOfApp(appName);
                            countLimitOfIp = countLimitOfApp < 0 ? countLimitOfIp : countLimitOfApp;
                        }
                        if (countLimitOfIp < 0) {
                            countLimitOfIp = connectionLimitRule.getCountLimitPerClientIpDefault();
                        }
                        if (countLimitOfIp >= 0 && connectionForClientIp.containsKey(clientIp)) {
                            AtomicInteger currentCountIp = connectionForClientIp.get(clientIp);
                            if (currentCountIp != null && currentCountIp.get() > countLimitOfIp) {
                                expelForIp.put(clientIp, new AtomicInteger(currentCountIp.get() - countLimitOfIp));
                            }
                        }
                    }
                }
                Loggers.REMOTE_DIGEST.info("Check over limit for ip limit rule, over limit ip count={}", expelForIp.size());
                if (expelForIp.size() > 0) {
                    Loggers.REMOTE_DIGEST.info("Over limit ip expel info, {}", expelForIp);
                }
                Set<String> outDatedConnections = new HashSet<>();
                long now = System.currentTimeMillis();
                // 2.get expel connection for ip limit.
                for (Map.Entry<String, Connection> entry : entries) {
                    Connection client = entry.getValue();
                    String clientIp = client.getMetaInfo().getClientIp();
                    AtomicInteger integer = expelForIp.get(clientIp);
                    if (integer != null && integer.intValue() > 0) {
                        integer.decrementAndGet();
                        expelClient.add(client.getMetaInfo().getConnectionId());
                        expelCount--;
                    } else if (now - client.getMetaInfo().getLastActiveTime() >= KEEP_ALIVE_TIME) {
                        outDatedConnections.add(client.getMetaInfo().getConnectionId());
                    }
                }
                // 3. if total count is still over limit.
                if (expelCount > 0) {
                    for (Map.Entry<String, Connection> entry : entries) {
                        Connection client = entry.getValue();
                        if (!expelForIp.containsKey(client.getMetaInfo().clientIp) && client.getMetaInfo().isSdkSource() && expelCount > 0) {
                            expelClient.add(client.getMetaInfo().getConnectionId());
                            expelCount--;
                            outDatedConnections.remove(client.getMetaInfo().getConnectionId());
                        }
                    }
                }
                String serverIp = null;
                String serverPort = null;
                if (StringUtils.isNotBlank(redirectAddress) && redirectAddress.contains(Constants.COLON)) {
                    String[] split = redirectAddress.split(Constants.COLON);
                    serverIp = split[0];
                    serverPort = split[1];
                }
                for (String expelledClientId : expelClient) {
                    try {
                        Connection connection = getConnection(expelledClientId);
                        if (connection != null) {
                            ConnectResetRequest connectResetRequest = new ConnectResetRequest();
                            connectResetRequest.setServerIp(serverIp);
                            connectResetRequest.setServerPort(serverPort);
                            connection.asyncRequest(connectResetRequest, null);
                            Loggers.REMOTE_DIGEST.info("Send connection reset request , connection id = {},recommendServerIp={}, recommendServerPort={}", expelledClientId, connectResetRequest.getServerIp(), connectResetRequest.getServerPort());
                        }
                    } catch (ConnectionAlreadyClosedException e) {
                        unregister(expelledClientId);
                    } catch (Exception e) {
                        Loggers.REMOTE_DIGEST.error("Error occurs when expel connection, expelledClientId:{}", expelledClientId, e);
                    }
                }
                // 4.client active detection.
                Loggers.REMOTE_DIGEST.info("Out dated connection ,size={}", outDatedConnections.size());
                if (CollectionUtils.isNotEmpty(outDatedConnections)) {
                    Set<String> successConnections = new HashSet<>();
                    final CountDownLatch latch = new CountDownLatch(outDatedConnections.size());
                    for (String outDateConnectionId : outDatedConnections) {
                        try {
                            Connection connection = getConnection(outDateConnectionId);
                            if (connection != null) {
                                ClientDetectionRequest clientDetectionRequest = new ClientDetectionRequest();
                                connection.asyncRequest(clientDetectionRequest, new RequestCallBack() {

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

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

                                    @Override
                                    public void onResponse(Response response) {
                                        latch.countDown();
                                        if (response != null && response.isSuccess()) {
                                            connection.freshActiveTime();
                                            successConnections.add(outDateConnectionId);
                                        }
                                    }

                                    @Override
                                    public void onException(Throwable e) {
                                        latch.countDown();
                                    }
                                });
                                Loggers.REMOTE_DIGEST.info("[{}]send connection active request ", outDateConnectionId);
                            } else {
                                latch.countDown();
                            }
                        } catch (ConnectionAlreadyClosedException e) {
                            latch.countDown();
                        } catch (Exception e) {
                            Loggers.REMOTE_DIGEST.error("[{}]Error occurs when check client active detection ,error={}", outDateConnectionId, e);
                            latch.countDown();
                        }
                    }
                    latch.await(3000L, TimeUnit.MILLISECONDS);
                    Loggers.REMOTE_DIGEST.info("Out dated connection check successCount={}", successConnections.size());
                    for (String outDateConnectionId : outDatedConnections) {
                        if (!successConnections.contains(outDateConnectionId)) {
                            Loggers.REMOTE_DIGEST.info("[{}]Unregister Out dated connection....", outDateConnectionId);
                            unregister(outDateConnectionId);
                        }
                    }
                }
                if (isLoaderClient) {
                    loadClient = -1;
                    redirectAddress = null;
                }
                Loggers.REMOTE_DIGEST.info("Connection check task end");
            } catch (Throwable e) {
                Loggers.REMOTE.error("Error occurs during connection check... ", e);
            }
        }
    }, 1000L, 3000L, TimeUnit.MILLISECONDS);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ConnectionAlreadyClosedException(com.alibaba.nacos.common.remote.exception.ConnectionAlreadyClosedException) ClientDetectionRequest(com.alibaba.nacos.api.remote.request.ClientDetectionRequest) RequestCallBack(com.alibaba.nacos.api.remote.RequestCallBack) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectResetRequest(com.alibaba.nacos.api.remote.request.ConnectResetRequest) ConnectionAlreadyClosedException(com.alibaba.nacos.common.remote.exception.ConnectionAlreadyClosedException) NacosException(com.alibaba.nacos.api.exception.NacosException) IOException(java.io.IOException) Response(com.alibaba.nacos.api.remote.response.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PostConstruct(javax.annotation.PostConstruct)

Aggregations

Response (com.alibaba.nacos.api.remote.response.Response)31 NacosException (com.alibaba.nacos.api.exception.NacosException)23 Payload (com.alibaba.nacos.api.grpc.auto.Payload)9 ErrorResponse (com.alibaba.nacos.api.remote.response.ErrorResponse)9 Member (com.alibaba.nacos.core.cluster.Member)8 Test (org.junit.Test)7 Request (com.alibaba.nacos.api.remote.request.Request)6 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)6 RequestCallBack (com.alibaba.nacos.api.remote.RequestCallBack)5 ConnectionSetupRequest (com.alibaba.nacos.api.remote.request.ConnectionSetupRequest)4 ServerCheckResponse (com.alibaba.nacos.api.remote.response.ServerCheckResponse)4 DistroDataRequest (com.alibaba.nacos.naming.cluster.remote.request.DistroDataRequest)4 DistroDataResponse (com.alibaba.nacos.naming.cluster.remote.response.DistroDataResponse)4 ConnectResetRequest (com.alibaba.nacos.api.remote.request.ConnectResetRequest)3 HealthCheckRequest (com.alibaba.nacos.api.remote.request.HealthCheckRequest)3 ServerCheckRequest (com.alibaba.nacos.api.remote.request.ServerCheckRequest)3 ConnectResetResponse (com.alibaba.nacos.api.remote.response.ConnectResetResponse)3 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 InstanceRequest (com.alibaba.nacos.api.naming.remote.request.InstanceRequest)2