* Add rest api v2 in core module * merge cluster listNodes methods * ut * add beta annotation
This commit is contained in:
parent
1778a84964
commit
87ddbeff90
35
common/src/main/java/com/alibaba/nacos/common/Beta.java
Normal file
35
common/src/main/java/com/alibaba/nacos/common/Beta.java
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 1999-2018 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.common;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Means a class is in beta version.
|
||||
*
|
||||
* @author wuzhiguo
|
||||
*/
|
||||
@Documented
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface Beta {
|
||||
|
||||
}
|
@ -37,6 +37,13 @@ public interface IdGenerator {
|
||||
*/
|
||||
long currentId();
|
||||
|
||||
/**
|
||||
* worker id info.
|
||||
*
|
||||
* @return worker id
|
||||
*/
|
||||
long workerId();
|
||||
|
||||
/**
|
||||
* Get next id.
|
||||
*
|
||||
|
@ -236,7 +236,7 @@ public class ServerMemberManager implements ApplicationListener<WebServerInitial
|
||||
* member information update.
|
||||
*
|
||||
* @param newMember {@link Member}
|
||||
* @return update is successw
|
||||
* @return update is success
|
||||
*/
|
||||
public boolean update(Member newMember) {
|
||||
Loggers.CLUSTER.debug("member information update : {}", newMember);
|
||||
@ -271,7 +271,7 @@ public class ServerMemberManager implements ApplicationListener<WebServerInitial
|
||||
* Whether the node exists within the cluster.
|
||||
*
|
||||
* @param address ip:port
|
||||
* @return is exist
|
||||
* @return is exists
|
||||
*/
|
||||
public boolean hasMember(String address) {
|
||||
boolean result = serverList.containsKey(address);
|
||||
|
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright 1999-2018 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.controller.v2;
|
||||
|
||||
import com.alibaba.nacos.auth.annotation.Secured;
|
||||
import com.alibaba.nacos.auth.common.ActionTypes;
|
||||
import com.alibaba.nacos.common.Beta;
|
||||
import com.alibaba.nacos.common.model.RestResult;
|
||||
import com.alibaba.nacos.common.model.RestResultUtils;
|
||||
import com.alibaba.nacos.core.distributed.ProtocolManager;
|
||||
import com.alibaba.nacos.core.distributed.id.IdGeneratorManager;
|
||||
import com.alibaba.nacos.core.model.request.LogUpdateRequest;
|
||||
import com.alibaba.nacos.core.model.vo.IdGeneratorVO;
|
||||
import com.alibaba.nacos.core.utils.Commons;
|
||||
import com.alibaba.nacos.core.utils.Loggers;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Kernel modules operate and maintain HTTP interfaces v2.
|
||||
*
|
||||
* @author wuzhiguo
|
||||
*/
|
||||
@Beta
|
||||
@RestController
|
||||
@RequestMapping(Commons.NACOS_CORE_CONTEXT_V2 + "/ops")
|
||||
public class CoreOpsV2Controller {
|
||||
|
||||
private final ProtocolManager protocolManager;
|
||||
|
||||
private final IdGeneratorManager idGeneratorManager;
|
||||
|
||||
public CoreOpsV2Controller(ProtocolManager protocolManager, IdGeneratorManager idGeneratorManager) {
|
||||
this.protocolManager = protocolManager;
|
||||
this.idGeneratorManager = idGeneratorManager;
|
||||
}
|
||||
|
||||
// Temporarily overpassed the raft operations interface
|
||||
// {
|
||||
// "groupId": "xxx",
|
||||
// "command": "transferLeader or doSnapshot or resetRaftCluster or removePeer"
|
||||
// "value": "ip:{raft_port}"
|
||||
// }
|
||||
|
||||
@PostMapping(value = "/raft")
|
||||
@Secured(action = ActionTypes.WRITE, resource = "nacos/admin")
|
||||
public RestResult<String> raftOps(@RequestBody Map<String, String> commands) {
|
||||
return protocolManager.getCpProtocol().execute(commands);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current health of the ID generator.
|
||||
*
|
||||
* @return {@link RestResult}
|
||||
*/
|
||||
@GetMapping(value = "/ids")
|
||||
public RestResult<List<IdGeneratorVO>> ids() {
|
||||
List<IdGeneratorVO> result = new ArrayList<>();
|
||||
idGeneratorManager.getGeneratorMap().forEach((resource, idGenerator) -> {
|
||||
IdGeneratorVO vo = new IdGeneratorVO();
|
||||
vo.setResource(resource);
|
||||
|
||||
IdGeneratorVO.IdInfo info = new IdGeneratorVO.IdInfo();
|
||||
info.setCurrentId(idGenerator.currentId());
|
||||
info.setWorkerId(idGenerator.workerId());
|
||||
vo.setInfo(info);
|
||||
|
||||
result.add(vo);
|
||||
});
|
||||
|
||||
return RestResultUtils.success(result);
|
||||
}
|
||||
|
||||
@PutMapping(value = "/log")
|
||||
public RestResult<Void> updateLog(@RequestBody LogUpdateRequest logUpdateRequest) {
|
||||
Loggers.setLogLevel(logUpdateRequest.getLogName(), logUpdateRequest.getLogLevel());
|
||||
return RestResultUtils.success();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,226 @@
|
||||
/*
|
||||
* Copyright 1999-2018 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.controller.v2;
|
||||
|
||||
import com.alibaba.nacos.common.Beta;
|
||||
import com.alibaba.nacos.common.http.Callback;
|
||||
import com.alibaba.nacos.common.http.HttpClientBeanHolder;
|
||||
import com.alibaba.nacos.common.http.HttpUtils;
|
||||
import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate;
|
||||
import com.alibaba.nacos.common.http.param.Header;
|
||||
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.MemberUtil;
|
||||
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.Commons;
|
||||
import com.alibaba.nacos.core.utils.GenericType;
|
||||
import com.alibaba.nacos.core.utils.Loggers;
|
||||
import com.alibaba.nacos.sys.env.EnvUtil;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
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;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Cluster communication interface v2.
|
||||
*
|
||||
* @author wuzhiguo
|
||||
*/
|
||||
@Beta
|
||||
@RestController
|
||||
@RequestMapping(Commons.NACOS_CORE_CONTEXT_V2 + "/cluster")
|
||||
public class NacosClusterV2Controller {
|
||||
|
||||
private final ServerMemberManager memberManager;
|
||||
|
||||
public NacosClusterV2Controller(ServerMemberManager memberManager) {
|
||||
this.memberManager = memberManager;
|
||||
}
|
||||
|
||||
@GetMapping(value = "/nodes/self")
|
||||
public RestResult<Member> self() {
|
||||
return RestResultUtils.success(memberManager.getSelf());
|
||||
}
|
||||
|
||||
/**
|
||||
* The console displays the list of cluster members.
|
||||
*
|
||||
* @param address match address
|
||||
* @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) {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Addressing mode switch.
|
||||
*
|
||||
* @param request {@link LookupUpdateRequest}
|
||||
* @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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -115,6 +115,11 @@ public class SnowFlowerIdGenerator implements IdGenerator {
|
||||
return currentId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long workerId() {
|
||||
return workerId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized long nextId() {
|
||||
long currentMillis = currentTimeMillis();
|
||||
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 1999-2018 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.model.request;
|
||||
|
||||
/**
|
||||
* Request entity for log operator interface.
|
||||
*
|
||||
* @author wuzhiguo
|
||||
*/
|
||||
public class LogUpdateRequest {
|
||||
|
||||
private String logName;
|
||||
|
||||
private String logLevel;
|
||||
|
||||
public String getLogName() {
|
||||
return logName;
|
||||
}
|
||||
|
||||
public void setLogName(String logName) {
|
||||
this.logName = logName;
|
||||
}
|
||||
|
||||
public String getLogLevel() {
|
||||
return logLevel;
|
||||
}
|
||||
|
||||
public void setLogLevel(String logLevel) {
|
||||
this.logLevel = logLevel;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 1999-2018 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.model.request;
|
||||
|
||||
/**
|
||||
* Update member lookup type.
|
||||
*
|
||||
* @author wuzhiguo
|
||||
*/
|
||||
public class LookupUpdateRequest {
|
||||
|
||||
private String type;
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 1999-2018 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.model.vo;
|
||||
|
||||
/**
|
||||
* Id generator vo.
|
||||
*
|
||||
* @author wuzhiguo
|
||||
*/
|
||||
public class IdGeneratorVO {
|
||||
|
||||
private String resource;
|
||||
|
||||
private IdInfo info;
|
||||
|
||||
public String getResource() {
|
||||
return resource;
|
||||
}
|
||||
|
||||
public void setResource(String resource) {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
public IdInfo getInfo() {
|
||||
return info;
|
||||
}
|
||||
|
||||
public void setInfo(IdInfo info) {
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public static class IdInfo {
|
||||
|
||||
private Long currentId;
|
||||
|
||||
private Long workerId;
|
||||
|
||||
public Long getCurrentId() {
|
||||
return currentId;
|
||||
}
|
||||
|
||||
public void setCurrentId(Long currentId) {
|
||||
this.currentId = currentId;
|
||||
}
|
||||
|
||||
public Long getWorkerId() {
|
||||
return workerId;
|
||||
}
|
||||
|
||||
public void setWorkerId(Long workerId) {
|
||||
this.workerId = workerId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "IdInfo{" + "currentId=" + currentId + ", workerId=" + workerId + '}';
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "IdGeneratorVO{" + "resource='" + resource + '\'' + ", info=" + info + '}';
|
||||
}
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.controller.v2;
|
||||
|
||||
import com.alibaba.nacos.common.model.RestResult;
|
||||
import com.alibaba.nacos.common.model.RestResultUtils;
|
||||
import com.alibaba.nacos.consistency.IdGenerator;
|
||||
import com.alibaba.nacos.consistency.cp.CPProtocol;
|
||||
import com.alibaba.nacos.core.distributed.ProtocolManager;
|
||||
import com.alibaba.nacos.core.distributed.id.IdGeneratorManager;
|
||||
import com.alibaba.nacos.core.distributed.id.SnowFlowerIdGenerator;
|
||||
import com.alibaba.nacos.core.model.request.LogUpdateRequest;
|
||||
import com.alibaba.nacos.core.model.vo.IdGeneratorVO;
|
||||
import com.alibaba.nacos.core.utils.Loggers;
|
||||
import com.alibaba.nacos.sys.env.EnvUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CoreOpsV2ControllerTest {
|
||||
|
||||
@InjectMocks
|
||||
private CoreOpsV2Controller coreOpsV2Controller;
|
||||
|
||||
@Mock
|
||||
private ProtocolManager protocolManager;
|
||||
|
||||
@Mock
|
||||
private IdGeneratorManager idGeneratorManager;
|
||||
|
||||
private final MockEnvironment mockEnvironment = new MockEnvironment();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
EnvUtil.setEnvironment(mockEnvironment);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRaftOps() {
|
||||
Mockito.when(protocolManager.getCpProtocol()).thenAnswer(invocationOnMock -> {
|
||||
CPProtocol cpProtocol = Mockito.mock(CPProtocol.class);
|
||||
Mockito.when(cpProtocol.execute(Mockito.anyMap())).thenReturn(RestResultUtils.success("res"));
|
||||
return cpProtocol;
|
||||
});
|
||||
|
||||
RestResult<String> result = coreOpsV2Controller.raftOps(new HashMap<>());
|
||||
Assert.assertEquals("res", result.getData());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIdInfo() {
|
||||
mockEnvironment.setProperty("nacos.core.snowflake.worker-id", "1");
|
||||
|
||||
Map<String, IdGenerator> idGeneratorMap = new HashMap<>();
|
||||
idGeneratorMap.put("resource", new SnowFlowerIdGenerator());
|
||||
Mockito.when(idGeneratorManager.getGeneratorMap()).thenReturn(idGeneratorMap);
|
||||
RestResult<List<IdGeneratorVO>> res = coreOpsV2Controller.ids();
|
||||
|
||||
Assert.assertTrue(res.ok());
|
||||
Assert.assertEquals(1, res.getData().size());
|
||||
Assert.assertEquals("resource", res.getData().get(0).getResource());
|
||||
Assert.assertEquals(1L, res.getData().get(0).getInfo().getWorkerId().longValue());
|
||||
Assert.assertEquals(0L, res.getData().get(0).getInfo().getCurrentId().longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetLogLevel() {
|
||||
LogUpdateRequest request = new LogUpdateRequest();
|
||||
request.setLogName("core");
|
||||
request.setLogLevel("debug");
|
||||
RestResult<?> res = coreOpsV2Controller.updateLog(request);
|
||||
|
||||
Assert.assertTrue(res.ok());
|
||||
Assert.assertTrue(Loggers.CORE.isDebugEnabled());
|
||||
}
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.controller.v2;
|
||||
|
||||
import com.alibaba.nacos.common.model.RestResult;
|
||||
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.sys.env.EnvUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class NacosClusterV2ControllerTest {
|
||||
|
||||
@InjectMocks
|
||||
private NacosClusterV2Controller nacosClusterV2Controller;
|
||||
|
||||
@Mock
|
||||
private ServerMemberManager serverMemberManager;
|
||||
|
||||
private final MockEnvironment environment = new MockEnvironment();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
EnvUtil.setEnvironment(environment);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelf() {
|
||||
Member self = new Member();
|
||||
Mockito.when(serverMemberManager.getSelf()).thenReturn(self);
|
||||
|
||||
RestResult<Member> result = nacosClusterV2Controller.self();
|
||||
Assert.assertTrue(result.ok());
|
||||
Assert.assertEquals(self, result.getData());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListNodes() {
|
||||
Member member1 = new Member();
|
||||
member1.setIp("1.1.1.1");
|
||||
member1.setPort(8848);
|
||||
member1.setState(NodeState.DOWN);
|
||||
Member member2 = new Member();
|
||||
member2.setIp("2.2.2.2");
|
||||
member2.setPort(8848);
|
||||
|
||||
List<Member> members = Arrays.asList(member1, member2);
|
||||
Mockito.when(serverMemberManager.allMembers()).thenReturn(members);
|
||||
|
||||
RestResult<Collection<Member>> result1 = nacosClusterV2Controller.listNodes("1.1.1.1", null);
|
||||
Assert.assertTrue(result1.getData().stream().findFirst().isPresent());
|
||||
Assert.assertEquals("1.1.1.1:8848", result1.getData().stream().findFirst().get().getAddress());
|
||||
|
||||
RestResult<Collection<Member>> result2 = nacosClusterV2Controller.listNodes(null, "up");
|
||||
Assert.assertTrue(result2.getData().stream().findFirst().isPresent());
|
||||
Assert.assertEquals("2.2.2.2:8848", result2.getData().stream().findFirst().get().getAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() {
|
||||
Mockito.when(serverMemberManager.update(Mockito.any())).thenReturn(true);
|
||||
|
||||
Member member = new Member();
|
||||
member.setIp("1.1.1.1");
|
||||
member.setPort(8848);
|
||||
member.setAddress("test");
|
||||
RestResult<Void> result = nacosClusterV2Controller.updateNodes(Collections.singletonList(member));
|
||||
Assert.assertTrue(result.ok());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSwitchLookup() {
|
||||
LookupUpdateRequest request = new LookupUpdateRequest();
|
||||
request.setType("test");
|
||||
|
||||
RestResult<Void> result = nacosClusterV2Controller.updateLookup(request);
|
||||
Assert.assertTrue(result.ok());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLeave() throws Exception {
|
||||
RestResult<Void> result = nacosClusterV2Controller.deleteNodes(Collections.singletonList("1.1.1.1"));
|
||||
Assert.assertTrue(result.ok());
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 1999-2018 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.model.request;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class LogUpdateRequestTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
LogUpdateRequest request = new LogUpdateRequest();
|
||||
request.setLogName("test");
|
||||
request.setLogLevel("info");
|
||||
|
||||
Assert.assertEquals(request.getLogName(), "test");
|
||||
Assert.assertEquals(request.getLogLevel(), "info");
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 1999-2018 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.model.request;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class LookupUpdateRequestTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
LookupUpdateRequest request = new LookupUpdateRequest();
|
||||
request.setType("type");
|
||||
|
||||
Assert.assertEquals(request.getType(), "type");
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 1999-2018 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.model.vo;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class IdGeneratorVOTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
IdGeneratorVO vo = new IdGeneratorVO();
|
||||
IdGeneratorVO.IdInfo info = new IdGeneratorVO.IdInfo();
|
||||
info.setWorkerId(1L);
|
||||
info.setCurrentId(2L);
|
||||
vo.setResource("test");
|
||||
vo.setInfo(info);
|
||||
|
||||
Assert.assertEquals(vo.getInfo(), info);
|
||||
Assert.assertEquals(vo.getResource(), "test");
|
||||
Assert.assertEquals(vo.getInfo().getWorkerId().longValue(), 1L);
|
||||
Assert.assertEquals(vo.getInfo().getCurrentId().longValue(), 2L);
|
||||
}
|
||||
}
|
@ -60,6 +60,7 @@
|
||||
<module name="AbbreviationAsWordInName">
|
||||
<property name="ignoreFinal" value="false"/>
|
||||
<property name="allowedAbbreviationLength" value="1"/>
|
||||
<property name="allowedAbbreviations" value="VO"/>
|
||||
</module>
|
||||
|
||||
<!-- Import Checker -->
|
||||
|
Loading…
Reference in New Issue
Block a user