Search in sources :

Example 1 with Member

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

the class NacosClusterController method leave.

/**
 * member leave.
 *
 * @param params member ip list, example [ip1:port1,ip2:port2,...]
 * @return {@link RestResult}
 * @throws Exception {@link Exception}
 */
@PostMapping("/server/leave")
public RestResult<String> leave(@RequestBody Collection<String> params) throws Exception {
    Collection<Member> memberList = MemberUtil.multiParse(params);
    memberManager.memberLeave(memberList);
    final NacosAsyncRestTemplate nacosAsyncRestTemplate = HttpClientBeanHolder.getNacosAsyncRestTemplate(Loggers.CLUSTER);
    final GenericType<RestResult<String>> genericType = new GenericType<RestResult<String>>() {
    };
    final Collection<Member> notifyList = memberManager.allMembersWithoutSelf();
    notifyList.removeAll(memberList);
    CountDownLatch latch = new CountDownLatch(notifyList.size());
    for (Member member : notifyList) {
        final String url = HttpUtils.buildUrl(false, member.getAddress(), EnvUtil.getContextPath(), Commons.NACOS_CORE_CONTEXT, "/cluster/server/leave");
        nacosAsyncRestTemplate.post(url, Header.EMPTY, Query.EMPTY, params, genericType.getType(), new Callback<String>() {

            @Override
            public void onReceive(RestResult<String> result) {
                try {
                    if (result.ok()) {
                        LoggerUtils.printIfDebugEnabled(Loggers.CLUSTER, "The node : [{}] success to process the request", member);
                        MemberUtil.onSuccess(memberManager, member);
                    } else {
                        Loggers.CLUSTER.warn("The node : [{}] failed to process the request, response is : {}", member, result);
                        MemberUtil.onFail(memberManager, member);
                    }
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onError(Throwable throwable) {
                try {
                    Loggers.CLUSTER.error("Failed to communicate with the node : {}", member);
                    MemberUtil.onFail(memberManager, member);
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onCancel() {
            }
        });
    }
    try {
        latch.await(10_000, TimeUnit.MILLISECONDS);
        return RestResultUtils.success("ok");
    } catch (Throwable ex) {
        return RestResultUtils.failed(ex.getMessage());
    }
}
Also used : GenericType(com.alibaba.nacos.core.utils.GenericType) RestResult(com.alibaba.nacos.common.model.RestResult) NacosAsyncRestTemplate(com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate) CountDownLatch(java.util.concurrent.CountDownLatch) Member(com.alibaba.nacos.core.cluster.Member) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with Member

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

the class UpgradeJudgement method checkForUpgrade.

private boolean checkForUpgrade() {
    if (!useGrpcFeatures.get()) {
        boolean selfCheckResult = selfUpgradeChecker.isReadyToUpgrade(serviceManager, doubleWriteDelayTaskEngine);
        Member self = memberManager.getSelf();
        self.setExtendVal(MemberMetaDataConstants.READY_TO_UPGRADE, selfCheckResult);
        memberManager.updateMember(self);
        if (!selfCheckResult) {
            NamingExecuteTaskDispatcher.getInstance().dispatchAndExecuteTask(AsyncServicesCheckTask.class, new AsyncServicesCheckTask(doubleWriteDelayTaskEngine, this));
        }
    }
    boolean result = true;
    for (Member each : memberManager.allMembers()) {
        Object isReadyToUpgrade = each.getExtendVal(MemberMetaDataConstants.READY_TO_UPGRADE);
        result &= null != isReadyToUpgrade && (boolean) isReadyToUpgrade;
    }
    return result;
}
Also used : AsyncServicesCheckTask(com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.execute.AsyncServicesCheckTask) Member(com.alibaba.nacos.core.cluster.Member)

Example 3 with Member

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

the class NacosClusterController method leave.

/**
 * member leave.
 *
 * @param params member ip list, example [ip1:port1,ip2:port2,...]
 * @return {@link RestResult}
 * @throws Exception {@link Exception}
 */
@PostMapping("/server/leave")
public RestResult<String> leave(@RequestBody Collection<String> params, @RequestParam(defaultValue = "true") Boolean notifyOtherMembers) throws Exception {
    Collection<Member> memberList = MemberUtil.multiParse(params);
    memberManager.memberLeave(memberList);
    // End conditions for cluster notifications, FIX https://github.com/alibaba/nacos/issues/6273
    if (Boolean.FALSE.equals(notifyOtherMembers)) {
        return RestResultUtils.success("ok");
    }
    final NacosAsyncRestTemplate nacosAsyncRestTemplate = HttpClientBeanHolder.getNacosAsyncRestTemplate(Loggers.CLUSTER);
    final GenericType<RestResult<String>> genericType = new GenericType<RestResult<String>>() {
    };
    final Collection<Member> notifyList = memberManager.allMembersWithoutSelf();
    notifyList.removeAll(memberList);
    CountDownLatch latch = new CountDownLatch(notifyList.size());
    for (Member member : notifyList) {
        final String url = HttpUtils.buildUrl(false, member.getAddress(), EnvUtil.getContextPath(), Commons.NACOS_CORE_CONTEXT, "/cluster/server/leave?notifyOtherMembers=false");
        nacosAsyncRestTemplate.post(url, Header.EMPTY, Query.EMPTY, params, genericType.getType(), new Callback<String>() {

            @Override
            public void onReceive(RestResult<String> result) {
                try {
                    if (result.ok()) {
                        LoggerUtils.printIfDebugEnabled(Loggers.CLUSTER, "The node : [{}] success to process the request", member);
                        MemberUtil.onSuccess(memberManager, member);
                    } else {
                        Loggers.CLUSTER.warn("The node : [{}] failed to process the request, response is : {}", member, result);
                        MemberUtil.onFail(memberManager, member);
                    }
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onError(Throwable throwable) {
                try {
                    Loggers.CLUSTER.error("Failed to communicate with the node : {}", member);
                    MemberUtil.onFail(memberManager, member);
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onCancel() {
            }
        });
    }
    try {
        latch.await(10_000, TimeUnit.MILLISECONDS);
        return RestResultUtils.success("ok");
    } catch (Throwable ex) {
        return RestResultUtils.failed(ex.getMessage());
    }
}
Also used : GenericType(com.alibaba.nacos.core.utils.GenericType) RestResult(com.alibaba.nacos.common.model.RestResult) NacosAsyncRestTemplate(com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate) CountDownLatch(java.util.concurrent.CountDownLatch) Member(com.alibaba.nacos.core.cluster.Member) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 4 with Member

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

the class NacosClusterV2Controller method deleteNodes.

/**
 * member leave.
 *
 * @param addresses member ip list, example [ip1:port1,ip2:port2,...]
 * @return {@link RestResult}
 * @throws Exception throw {@link Exception}
 */
@DeleteMapping("/nodes")
public RestResult<Void> deleteNodes(@RequestParam("addresses") List<String> addresses) throws Exception {
    Collection<Member> memberList = MemberUtil.multiParse(addresses);
    memberManager.memberLeave(memberList);
    final NacosAsyncRestTemplate nacosAsyncRestTemplate = HttpClientBeanHolder.getNacosAsyncRestTemplate(Loggers.CLUSTER);
    final GenericType<RestResult<String>> genericType = new GenericType<RestResult<String>>() {
    };
    final Collection<Member> notifyList = memberManager.allMembersWithoutSelf();
    notifyList.removeAll(memberList);
    CountDownLatch latch = new CountDownLatch(notifyList.size());
    for (Member member : notifyList) {
        final String url = HttpUtils.buildUrl(false, member.getAddress(), EnvUtil.getContextPath(), Commons.NACOS_CORE_CONTEXT_V2, "/cluster/nodes");
        nacosAsyncRestTemplate.delete(url, Header.EMPTY, StringUtils.join(addresses, ","), genericType.getType(), new Callback<Void>() {

            @Override
            public void onReceive(RestResult<Void> result) {
                try {
                    if (result.ok()) {
                        LoggerUtils.printIfDebugEnabled(Loggers.CLUSTER, "The node : [{}] success to process the request", member);
                        MemberUtil.onSuccess(memberManager, member);
                    } else {
                        Loggers.CLUSTER.warn("The node : [{}] failed to process the request, response is : {}", member, result);
                        MemberUtil.onFail(memberManager, member);
                    }
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onError(Throwable throwable) {
                try {
                    Loggers.CLUSTER.error("Failed to communicate with the node : {}", member);
                    MemberUtil.onFail(memberManager, member);
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onCancel() {
            }
        });
    }
    try {
        latch.await(10_000, TimeUnit.MILLISECONDS);
        return RestResultUtils.success();
    } catch (Throwable ex) {
        return RestResultUtils.failed(ex.getMessage());
    }
}
Also used : GenericType(com.alibaba.nacos.core.utils.GenericType) RestResult(com.alibaba.nacos.common.model.RestResult) NacosAsyncRestTemplate(com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate) CountDownLatch(java.util.concurrent.CountDownLatch) Member(com.alibaba.nacos.core.cluster.Member) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping)

Example 5 with Member

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

the class NacosClusterV2Controller method updateNodes.

// The client can get all the nacos node information in the current
// cluster according to this interface
/**
 * Other nodes return their own metadata information.
 *
 * @param nodes List of {@link Member}
 * @return {@link RestResult}
 */
@PutMapping(value = "/nodes")
public RestResult<Void> updateNodes(@RequestBody List<Member> nodes) {
    for (Member node : nodes) {
        if (!node.check()) {
            LoggerUtils.printIfWarnEnabled(Loggers.CLUSTER, "node information is illegal, ignore node: {}", node);
            continue;
        }
        LoggerUtils.printIfDebugEnabled(Loggers.CLUSTER, "node state updating, node: {}", node);
        node.setState(NodeState.UP);
        node.setFailAccessCnt(0);
        boolean update = memberManager.update(node);
        if (!update) {
            LoggerUtils.printIfErrorEnabled(Loggers.CLUSTER, "node state update failed, node: {}", node);
        }
    }
    return RestResultUtils.success();
}
Also used : Member(com.alibaba.nacos.core.cluster.Member) PutMapping(org.springframework.web.bind.annotation.PutMapping)

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