8000 for [issue #8308] update NacosClusterControllerV2 by Ax-For · Pull Request #9066 · alibaba/nacos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

for [issue #8308] update NacosClusterControllerV2 #9066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
8000 Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@

package com.alibaba.nacos.core.controller.v2;

import com.alibaba.nacos.common.Beta;
import com.alibaba.nacos.api.annotation.NacosApi;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.exception.api.NacosApiException;
import com.alibaba.nacos.api.model.v2.ErrorCode;
import com.alibaba.nacos.api.model.v2.Result;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.model.RestResultUtils;
import com.alibaba.nacos.common.utils.LoggerUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.core.cluster.Member;
import com.alibaba.nacos.core.cluster.NodeState;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.core.model.request.LookupUpdateRequest;
import com.alibaba.nacos.core.service.NacosClusterOperationService;
import com.alibaba.nacos.core.utils.Commons;
import com.alibaba.nacos.core.utils.Loggers;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
Expand All @@ -35,7 +38,6 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
Expand All @@ -45,20 +47,20 @@
*
* @author wuzhiguo
*/
@Beta
@NacosApi
@RestController
@RequestMapping(Commons.NACOS_CORE_CONTEXT_V2 + "/cluster")
public class NacosClusterV2Controller {
public class NacosClusterControllerV2 {

private final ServerMemberManager memberManager;
private final NacosClusterOperationService nacosClusterOperationService;

public NacosClusterV2Controller(ServerMemberManager memberManager) {
this.memberManager = memberManager;
public NacosClusterControllerV2(NacosClusterOperationService nacosClusterOperationService) {
this.nacosClusterOperationService = nacosClusterOperationService;
}

@GetMapping(value = "/nodes/self")
public RestResult<Member> self() {
return RestResultUtils.success(memberManager.getSelf());
@GetMapping(value = "/node/self")
public Result<Member> self() {
return Result.success(nacosClusterOperationService.self());
}

/**
Expand All @@ -68,35 +70,24 @@ public RestResult<Member> self() {
* @param state match state
* @return members that matches condition
*/
@GetMapping(value = "/nodes")
public RestResult<Collection<Member>> listNodes(@RequestParam(value = "address", required = false) String address,
@RequestParam(value = "state", required = false) String state) {
@GetMapping(value = "/node/list")
public Result<Collection<Member>> listNodes(@RequestParam(value = "address", required = false) String address,
@RequestParam(value = "state", required = false) String state) throws NacosException {

NodeState nodeState = null;
if (StringUtils.isNoneBlank(state)) {
try {
nodeState = NodeState.valueOf(state.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
return RestResultUtils.failedWithMsg(400, "Illegal state: " + state);
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_STATE, "Illegal state: " + state);
}
}

Collection<Member> members = memberManager.allMembers();
Collection<Member> result = new ArrayList<>();

for (Member member : members) {
if (StringUtils.isNoneBlank(address) && !StringUtils.startsWith(member.getAddress(), address)) {
continue;
}

if (nodeState != null && member.getState() != nodeState) {
continue;
}

result.add(member);
}

return RestResultUtils.success(result);
return Result.success(nacosClusterOperationService.listNodes(address, nodeState));
}

@GetMapping(value = "/node/self/health")
public Result<String> selfHealth() {
return Result.success(nacosClusterOperationService.selfHealth());
}

// The client can get all the nacos node information in the current
Expand All @@ -108,25 +99,13 @@ public RestResult<Collection<Member>> listNodes(@RequestParam(value = "address",
* @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);
}
@PutMapping(value = "/node/list")
public Result<Boolean> updateNodes(@RequestBody List<Member> nodes) throws NacosApiException {
if (nodes == null || nodes.size() == 0) {
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.PARAMETER_MISSING,
"required parameter 'nodes' is missing");
}

return RestResultUtils.success();
return Result.success(nacosClusterOperationService.updateNodes(nodes));
}

/**
Expand All @@ -136,13 +115,12 @@ public RestResult<Void> updateNodes(@RequestBody List<Member> nodes) {
* @return {@link RestResult}
*/
@PutMapping(value = "/lookup")
public RestResult<Void> updateLookup(@RequestBody LookupUpdateRequest request) {
try {
memberManager.switchLookup(request.getType());
return RestResultUtils.success();
} catch (Throwable ex) {
return RestResultUtils.failed(ex.getMessage());
public Result<Boolean> updateLookup(LookupUpdateRequest request) throws NacosException {
if (request == null || request.getType() == null) {
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.PARAMETER_MISSING,
"required parameter 'type' is missing");
}
return Result.success(nacosClusterOperationService.updateLookup(request));
}

/**
Expand Down
6B10
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.core.service;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.utils.LoggerUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.core.cluster.Member;
import com.alibaba.nacos.core.cluster.NodeState;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.core.model.request.LookupUpdateRequest;
import com.alibaba.nacos.core.utils.Loggers;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* NacosClusterOperationService.
* @author dongyafei
* @date 2022/8/15
*/
@Service
public class NacosClusterOperationService {

private final ServerMemberManager memberManager;

public NacosClusterOperationService(ServerMemberManager memberManager) {
this.memberManager = memberManager;
}

public Member self() {
return memberManager.getSelf();
}

/**
* The console displays the list of cluster members.
*/
public Collection<Member> listNodes(String address, NodeState nodeState) throws NacosException {

Collection<Member> members = memberManager.allMembers();
Collection<Member> result = new ArrayList<>();

for (Member member : members) {
if (StringUtils.isNoneBlank(address) && !StringUtils.startsWith(member.getAddress(), address)) {
continue;
}
if (nodeState != null && member.getState() != nodeState) {
continue;
}
result.add(member);
}
return result;
}

/**
* cluster members information update.
*/
public Boolean updateNodes(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 true;
}

/**
* Addressing mode switch.
*/
public Boolean updateLookup(LookupUpdateRequest request) throws NacosException {
memberManager.switchLookup(request.getType());
return true;
}

/**
* query health of current node.
*/
public String selfHealth() {
return memberManager.getSelf().getState().name();
}
}
Loading
0