Skip to content
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

[ISSUE #12983] Add new admin API for core module #12984

Merged
merged 1 commit into from
Dec 25, 2024
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*/
@RestController
@RequestMapping(Commons.NACOS_CORE_CONTEXT + "/ops")
@Deprecated
public class CoreOpsController {

private final ProtocolManager protocolManager;
Expand All @@ -66,7 +67,7 @@ public CoreOpsController(ProtocolManager protocolManager, IdGeneratorManager idG

@PostMapping(value = "/raft")
@Secured(action = ActionTypes.WRITE, resource = "nacos/admin")
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "POST {contextPath:nacos}/v3/admin/core/ops/raft")
public RestResult<String> raftOps(@RequestBody Map<String, String> commands) {
return protocolManager.getCpProtocol().execute(commands);
}
Expand All @@ -77,7 +78,7 @@ public RestResult<String> raftOps(@RequestBody Map<String, String> commands) {
* @return {@link RestResult}
*/
@GetMapping(value = "/idInfo")
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "GET {contextPath:nacos}/v3/admin/core/ops/ids")
public RestResult<Map<String, Map<Object, Object>>> idInfo() {
Map<String, Map<Object, Object>> info = new HashMap<>(10);
idGeneratorManager.getGeneratorMap().forEach((resource, idGenerator) -> info.put(resource, idGenerator.info()));
Expand All @@ -86,7 +87,7 @@ public RestResult<Map<String, Map<Object, Object>>> idInfo() {

@PutMapping(value = "/log")
@Secured(action = ActionTypes.WRITE, resource = "nacos/admin", signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "PUT {contextPath:nacos}/v3/admin/core/ops/log")
public String setLogLevel(@RequestParam String logName, @RequestParam String logLevel) {
Loggers.setLogLevel(logName, logLevel);
return HttpServletResponse.SC_OK + "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
*/
@RestController
@RequestMapping(Commons.NACOS_CORE_CONTEXT + "/cluster")
@Deprecated
public class NacosClusterController {

private final ServerMemberManager memberManager;
Expand All @@ -58,7 +59,7 @@ public NacosClusterController(ServerMemberManager memberManager) {

@GetMapping(value = "/self")
@Secured(resource = Commons.NACOS_CORE_CONTEXT + "/cluster", action = ActionTypes.READ, signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "GET {contextPath:nacos}/v3/admin/core/cluster/node/self")
public RestResult<Member> self() {
return RestResultUtils.success(memberManager.getSelf());
}
Expand All @@ -71,7 +72,7 @@ public RestResult<Member> self() {
*/
@GetMapping(value = "/nodes")
@Secured(resource = Commons.NACOS_CORE_CONTEXT + "/cluster", action = ActionTypes.READ, signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.CONSOLE_API, alternatives = "GET ${contextPath:nacos}/v3/console/core/cluster/nodes")
@Compatibility(apiType = ApiType.CONSOLE_API, alternatives = "GET ${contextPath:nacos}/v3/admin/core/cluster/node/list")
public RestResult<Collection<Member>> listNodes(
@RequestParam(value = "keyword", required = false) String ipKeyWord) {
Collection<Member> members = memberManager.allMembers();
Expand Down Expand Up @@ -103,7 +104,7 @@ public RestResult<Collection<String>> listSimpleNodes() {

@GetMapping("/health")
@Secured(resource = Commons.NACOS_CORE_CONTEXT + "/cluster", action = ActionTypes.READ, signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "GET {contextPath:nacos}/v3/admin/core/cluster/node/self/health")
public RestResult<String> getHealth() {
return RestResultUtils.success(memberManager.getSelf().getState().name());
}
Expand All @@ -116,7 +117,8 @@ public RestResult<String> getHealth() {
*/
@Deprecated
@PostMapping(value = {"/report"})
@Secured(resource = Commons.NACOS_CORE_CONTEXT + "/cluster", action = ActionTypes.WRITE, signType = SignType.CONSOLE)
@Secured(resource = Commons.NACOS_CORE_CONTEXT
+ "/cluster", action = ActionTypes.WRITE, signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.INNER_API)
public RestResult<String> report(@RequestBody Member node) {
if (!node.check()) {
Expand All @@ -136,8 +138,9 @@ public RestResult<String> report(@RequestBody Member node) {
* @return {@link RestResult}
*/
@PostMapping(value = "/switch/lookup")
@Secured(resource = Commons.NACOS_CORE_CONTEXT + "/cluster", action = ActionTypes.WRITE, signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.ADMIN_API)
@Secured(resource = Commons.NACOS_CORE_CONTEXT
+ "/cluster", action = ActionTypes.WRITE, signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "PUT {contextPath:nacos}/v3/admin/core/cluster/lookup")
public RestResult<String> switchLookup(@RequestParam(name = "type") String type) {
try {
memberManager.switchLookup(type);
Expand All @@ -155,7 +158,8 @@ public RestResult<String> switchLookup(@RequestParam(name = "type") String type)
* @throws Exception {@link Exception}
*/
@PostMapping("/server/leave")
@Secured(resource = Commons.NACOS_CORE_CONTEXT + "/cluster", action = ActionTypes.WRITE, signType = SignType.CONSOLE)
@Secured(resource = Commons.NACOS_CORE_CONTEXT
+ "/cluster", action = ActionTypes.WRITE, signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.CONSOLE_API)
public RestResult<String> leave(@RequestBody Collection<String> params,
@RequestParam(defaultValue = "true") Boolean notifyOtherMembers) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
*/
@RestController
@RequestMapping(Commons.NACOS_CORE_CONTEXT_V2 + "/loader")
@Deprecated
public class ServerLoaderController {

private static final Logger LOGGER = LoggerFactory.getLogger(ServerLoaderController.class);
Expand Down Expand Up @@ -109,7 +110,7 @@ public ServerLoaderController(ConnectionManager connectionManager, ServerMemberM
*/
@Secured(resource = Commons.NACOS_CORE_CONTEXT_V2 + "/loader", action = ActionTypes.READ)
@GetMapping("/current")
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "GET {contextPath:nacos}/v3/core/loader/current")
public ResponseEntity<Map<String, Connection>> currentClients() {
Map<String, Connection> stringConnectionMap = connectionManager.currentClients();
return ResponseEntity.ok().body(stringConnectionMap);
Expand All @@ -122,22 +123,22 @@ public ResponseEntity<Map<String, Connection>> currentClients() {
*/
@Secured(resource = Commons.NACOS_CORE_CONTEXT_V2 + "/loader", action = ActionTypes.WRITE)
@GetMapping("/reloadCurrent")
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "GET {contextPath:nacos}/v3/core/loader/reloadCurrent")
public ResponseEntity<String> reloadCount(@RequestParam Integer count,
@RequestParam(value = "redirectAddress", required = false) String redirectAddress) {
connectionManager.loadCount(count, redirectAddress);
return ResponseEntity.ok().body("success");
}

/**
* According to the total number of sdk connections of all nodes in the nacos cluster,
* intelligently balance the number of sdk connections of each node in the nacos cluster.
* According to the total number of sdk connections of all nodes in the nacos cluster, intelligently balance the
* number of sdk connections of each node in the nacos cluster.
*
* @return state json.
*/
@Secured(resource = Commons.NACOS_CORE_CONTEXT_V2 + "/loader", action = ActionTypes.WRITE)
@GetMapping("/smartReloadCluster")
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "GET {contextPath:nacos}/v3/core/loader/smartReloadCluster")
public ResponseEntity<String> smartReload(HttpServletRequest request,
@RequestParam(value = "loaderFactor", required = false) String loaderFactorStr,
@RequestParam(value = "force", required = false) String force) {
Expand Down Expand Up @@ -248,7 +249,7 @@ public void onException(Throwable e) {
*/
@Secured(resource = Commons.NACOS_CORE_CONTEXT_V2 + "/loader", action = ActionTypes.WRITE)
@GetMapping("/reloadClient")
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "GET {contextPath:nacos}/v3/core/loader/reloadClient")
public ResponseEntity<String> reloadSingle(@RequestParam String connectionId,
@RequestParam(value = "redirectAddress", required = false) String redirectAddress) {
connectionManager.loadSingle(connectionId, redirectAddress);
Expand All @@ -262,7 +263,7 @@ public ResponseEntity<String> reloadSingle(@RequestParam String connectionId,
*/
@Secured(resource = Commons.NACOS_CORE_CONTEXT_V2 + "/loader", action = ActionTypes.READ)
@GetMapping("/cluster")
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "GET {contextPath:nacos}/v3/core/loader/cluster")
public ResponseEntity<Map<String, Object>> loaderMetrics() {

Map<String, Object> serverLoadMetrics = getServerLoadMetrics();
Expand Down Expand Up @@ -320,8 +321,8 @@ public void onException(Throwable e) {
}

try {
ServerLoaderInfoResponse handle = serverLoaderInfoRequestHandler
.handle(new ServerLoaderInfoRequest(), new RequestMeta());
ServerLoaderInfoResponse handle = serverLoaderInfoRequestHandler.handle(new ServerLoaderInfoRequest(),
new RequestMeta());
ServerLoaderMetrics metrics = new ServerLoaderMetrics();
metrics.setAddress(serverMemberManager.getSelf().getAddress());
metrics.setMetric(handle.getLoaderMetrics());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
@Beta
@RestController
@RequestMapping(Commons.NACOS_CORE_CONTEXT_V2 + "/ops")
@Deprecated
public class CoreOpsV2Controller {

private final ProtocolManager protocolManager;
Expand All @@ -63,18 +64,16 @@ public CoreOpsV2Controller(ProtocolManager protocolManager, IdGeneratorManager i
/**
* Temporarily overpassed the raft operations interface.
* <p>
* {
* "groupId": "xxx",
* "command": "transferLeader or doSnapshot or resetRaftCluster or removePeer"
* "value": "ip:{raft_port}"
* }
* { "groupId": "xxx", "command": "transferLeader or doSnapshot or resetRaftCluster or removePeer" "value":
* "ip:{raft_port}" }
* </p>
*
* @param commands transferLeader or doSnapshot or resetRaftCluster or removePeer
* @return {@link RestResult}
*/
@PostMapping(value = "/raft")
@Secured(action = ActionTypes.WRITE, resource = "nacos/admin", signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "POST {contextPath:nacos}/v3/admin/core/ops/raft")
public RestResult<String> raftOps(@RequestBody Map<String, String> commands) {
return protocolManager.getCpProtocol().execute(commands);
}
Expand All @@ -85,7 +84,7 @@ public RestResult<String> raftOps(@RequestBody Map<String, String> commands) {
* @return {@link RestResult}
*/
@GetMapping(value = "/ids")
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "GET {contextPath:nacos}/v3/admin/core/ops/ids")
public RestResult<List<IdGeneratorVO>> ids() {
List<IdGeneratorVO> result = new ArrayList<>();
idGeneratorManager.getGeneratorMap().forEach((resource, idGenerator) -> {
Expand All @@ -105,7 +104,7 @@ public RestResult<List<IdGeneratorVO>> ids() {

@PutMapping(value = "/log")
@Secured(action = ActionTypes.WRITE, resource = "nacos/admin", signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "PUT {contextPath:nacos}/v3/admin/core/ops/log")
public RestResult<Void> updateLog(@RequestBody LogUpdateRequest logUpdateRequest) {
Loggers.setLogLevel(logUpdateRequest.getLogName(), logUpdateRequest.getLogLevel());
return RestResultUtils.success();
Expand Down
14 changes: 8 additions & 6 deletions core/src/main/java/com/alibaba/nacos/core/controller/v2/NacosClusterControllerV2.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
@NacosApi
@RestController
@RequestMapping(Commons.NACOS_CORE_CONTEXT_V2 + "/cluster")
@Deprecated
public class NacosClusterControllerV2 {

private final NacosClusterOperationService nacosClusterOperationService;
Expand All @@ -65,7 +66,7 @@ public NacosClusterControllerV2(NacosClusterOperationService nacosClusterOperati

@GetMapping(value = "/node/self")
@Secured(action = ActionTypes.READ, resource = "nacos/admin", signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "GET {contextPath:nacos}/v3/admin/core/cluster/node/self")
public Result<Member> self() {
return Result.success(nacosClusterOperationService.self());
}
Expand All @@ -79,7 +80,7 @@ public Result<Member> self() {
*/
@GetMapping(value = "/node/list")
@Secured(action = ActionTypes.READ, resource = "nacos/admin", signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "GET {contextPath:nacos}/v3/admin/core/cluster/node/list")
public Result<Collection<Member>> listNodes(@RequestParam(value = "address", required = false) String address,
@RequestParam(value = "state", required = false) String state) throws NacosException {

Expand All @@ -88,15 +89,16 @@ public Result<Collection<Member>> listNodes(@RequestParam(value = "address", req
try {
nodeState = NodeState.valueOf(state.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_STATE, "Illegal state: " + state);
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_STATE,
"Illegal state: " + state);
}
}
return Result.success(nacosClusterOperationService.listNodes(address, nodeState));
}

@GetMapping(value = "/node/self/health")
@Secured(action = ActionTypes.READ, resource = "nacos/admin", signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "GET {contextPath:nacos}/v3/admin/core/cluster/node/self/health")
public Result<String> selfHealth() {
return Result.success(nacosClusterOperationService.selfHealth());
}
Expand All @@ -112,7 +114,7 @@ public Result<String> selfHealth() {
*/
@PutMapping(value = "/node/list")
@Secured(action = ActionTypes.WRITE, resource = "nacos/admin", signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "PUT {contextPath:nacos}/v3/admin/core/cluster/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,
Expand All @@ -129,7 +131,7 @@ public Result<Boolean> updateNodes(@RequestBody List<Member> nodes) throws Nacos
*/
@PutMapping(value = "/lookup")
@Secured(action = ActionTypes.WRITE, resource = "nacos/admin", signType = SignType.CONSOLE)
@Compatibility(apiType = ApiType.ADMIN_API)
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "PUT {contextPath:nacos}/v3/admin/core/cluster/lookup")
public Result<Boolean> updateLookup(LookupUpdateRequest request) throws NacosException {
if (request == null || request.getType() == null) {
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.PARAMETER_MISSING,
Expand Down
Loading