Search in sources :

Example 31 with Member

use of com.alibaba.nacos.core.cluster.Member in project nacos by alibaba.

the class DistroClientTransportAgent method syncData.

@Override
public void syncData(DistroData data, String targetServer, DistroCallback callback) {
    if (isNoExistTarget(targetServer)) {
        callback.onSuccess();
        return;
    }
    DistroDataRequest request = new DistroDataRequest(data, data.getType());
    Member member = memberManager.find(targetServer);
    try {
        clusterRpcClientProxy.asyncRequest(member, request, new DistroRpcCallbackWrapper(callback, member));
    } catch (NacosException nacosException) {
        callback.onFailed(nacosException);
    }
}
Also used : DistroDataRequest(com.alibaba.nacos.naming.cluster.remote.request.DistroDataRequest) Member(com.alibaba.nacos.core.cluster.Member) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 32 with Member

use of com.alibaba.nacos.core.cluster.Member 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 33 with Member

use of com.alibaba.nacos.core.cluster.Member 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 34 with Member

use of com.alibaba.nacos.core.cluster.Member in project nacos by alibaba.

the class ClusterVersionJudgement method judge.

protected boolean judge() {
    boolean finish = false;
    Collection<Member> members = memberManager.allMembers();
    final String oldVersion = "1.4.0";
    boolean allMemberIsNewVersion = true;
    for (Member member : members) {
        final String curV = (String) member.getExtendVal(MemberMetaDataConstants.VERSION);
        if (StringUtils.isBlank(curV) || VersionUtils.compareVersion(oldVersion, curV) > 0) {
            allMemberIsNewVersion = false;
        }
    }
    // can only trigger once
    if (allMemberIsNewVersion && !this.allMemberIsNewVersion) {
        notifyAllListener();
        finish = true;
    }
    return finish;
}
Also used : Member(com.alibaba.nacos.core.cluster.Member)

Example 35 with Member

use of com.alibaba.nacos.core.cluster.Member 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)

Aggregations

Member (com.alibaba.nacos.core.cluster.Member)53 Test (org.junit.Test)17 NacosException (com.alibaba.nacos.api.exception.NacosException)11 HashMap (java.util.HashMap)11 Response (com.alibaba.nacos.api.remote.response.Response)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 DistroDataRequest (com.alibaba.nacos.naming.cluster.remote.request.DistroDataRequest)6 Map (java.util.Map)5 ServerLoaderInfoResponse (com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse)4 NacosAsyncRestTemplate (com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate)4 DistroDataResponse (com.alibaba.nacos.naming.cluster.remote.response.DistroDataResponse)4 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 Before (org.junit.Before)4 GetMapping (org.springframework.web.bind.annotation.GetMapping)4 ServerAbilities (com.alibaba.nacos.api.ability.ServerAbilities)3 RequestCallBack (com.alibaba.nacos.api.remote.RequestCallBack)3 ServerRemoteAbility (com.alibaba.nacos.api.remote.ability.ServerRemoteAbility)3 RestResult (com.alibaba.nacos.common.model.RestResult)3 GenericType (com.alibaba.nacos.core.utils.GenericType)3