Search in sources :

Example 26 with Member

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

the class ServerMemberManager_ITCase method testMemberChange.

@Test
public void testMemberChange() throws Exception {
    AtomicInteger integer = new AtomicInteger(0);
    CountDownLatch latch = new CountDownLatch(1);
    NotifyCenter.registerSubscriber(new Subscriber<MembersChangeEvent>() {

        @Override
        public void onEvent(MembersChangeEvent event) {
            integer.incrementAndGet();
            latch.countDown();
        }

        @Override
        public Class<? extends Event> subscribeType() {
            return MembersChangeEvent.class;
        }
    });
    Collection<Member> members = memberManager.allMembers();
    System.out.println(members);
    memberManager.memberJoin(members);
    members.add(Member.builder().ip("115.159.3.213").port(8848).build());
    boolean changed = memberManager.memberJoin(members);
    Assert.assertTrue(changed);
    latch.await(10_000L, TimeUnit.MILLISECONDS);
    Assert.assertEquals(1, integer.get());
}
Also used : MembersChangeEvent(com.alibaba.nacos.core.cluster.MembersChangeEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MembersChangeEvent(com.alibaba.nacos.core.cluster.MembersChangeEvent) Event(com.alibaba.nacos.common.notify.Event) BeforeClass(org.junit.BeforeClass) AfterClass(org.junit.AfterClass) CountDownLatch(java.util.concurrent.CountDownLatch) Member(com.alibaba.nacos.core.cluster.Member) Test(org.junit.Test)

Example 27 with Member

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

the class NacosClusterV2ControllerTest method testSelf.

@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());
}
Also used : Member(com.alibaba.nacos.core.cluster.Member) Test(org.junit.Test)

Example 28 with Member

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

the class DistroLoadDataTaskTest method setUp.

@Before
public void setUp() throws Exception {
    List<Member> memberList = new LinkedList<>();
    memberList.add(Member.builder().ip("2.2.2.2").port(8848).build());
    memberList.add(Member.builder().ip("1.1.1.1").port(8848).build());
    when(memberManager.allMembersWithoutSelf()).thenReturn(memberList);
    componentHolder = new DistroComponentHolder();
    componentHolder.registerDataStorage(type, distroDataStorage);
    componentHolder.registerTransportAgent(type, distroTransportAgent);
    componentHolder.registerFailedTaskHandler(type, distroFailedTaskHandler);
    when(distroDataProcessor.processType()).thenReturn(type);
    componentHolder.registerDataProcessor(distroDataProcessor);
    when(distroTransportAgent.getDatumSnapshot(any(String.class))).thenReturn(distroData);
    when(distroDataProcessor.processSnapshot(distroData)).thenReturn(true);
    distroLoadDataTask = new DistroLoadDataTask(memberManager, componentHolder, distroConfig, loadCallback);
}
Also used : Member(com.alibaba.nacos.core.cluster.Member) DistroComponentHolder(com.alibaba.nacos.core.distributed.distro.component.DistroComponentHolder) LinkedList(java.util.LinkedList) Before(org.junit.Before)

Example 29 with Member

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

the class JRaftServerTest method testPeerChange.

@Test
public void testPeerChange() {
    AtomicBoolean changed = new AtomicBoolean(false);
    JRaftMaintainService service = new JRaftMaintainService(server) {

        @Override
        public RestResult<String> execute(Map<String, String> args) {
            changed.set(true);
            return RestResultUtils.success();
        }
    };
    Collection<Member> firstEvent = Arrays.asList(Member.builder().ip("1.1.1.1").port(7848).build(), Member.builder().ip("127.0.0.1").port(80).build(), Member.builder().ip("127.0.0.2").port(81).build(), Member.builder().ip("127.0.0.3").port(82).build());
    server.peerChange(service, ProtocolManager.toCPMembersInfo(firstEvent));
    Assert.assertFalse(changed.get());
    changed.set(false);
    Collection<Member> secondEvent = Arrays.asList(Member.builder().ip("1.1.1.1").port(7848).build(), Member.builder().ip("127.0.0.1").port(80).build(), Member.builder().ip("127.0.0.2").port(81).build(), Member.builder().ip("127.0.0.4").port(83).build());
    server.peerChange(service, ProtocolManager.toCPMembersInfo(secondEvent));
    Assert.assertTrue(changed.get());
    changed.set(false);
    Collection<Member> thirdEvent = Arrays.asList(Member.builder().ip("1.1.1.1").port(7848).build(), Member.builder().ip("127.0.0.2").port(81).build(), Member.builder().ip("127.0.0.5").port(82).build());
    server.peerChange(service, ProtocolManager.toCPMembersInfo(thirdEvent));
    Assert.assertTrue(changed.get());
    changed.set(false);
    Collection<Member> fourEvent = Arrays.asList(Member.builder().ip("1.1.1.1").port(7848).build(), Member.builder().ip("127.0.0.1").port(80).build());
    server.peerChange(service, ProtocolManager.toCPMembersInfo(fourEvent));
    Assert.assertTrue(changed.get());
    changed.set(false);
    Collection<Member> fiveEvent = Arrays.asList(Member.builder().ip("1.1.1.1").port(7848).build(), Member.builder().ip("127.0.0.1").port(80).build(), Member.builder().ip("127.0.0.3").port(81).build());
    server.peerChange(service, ProtocolManager.toCPMembersInfo(fiveEvent));
    Assert.assertFalse(changed.get());
    changed.set(false);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Map(java.util.Map) HashMap(java.util.HashMap) Member(com.alibaba.nacos.core.cluster.Member) Test(org.junit.Test)

Example 30 with Member

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

the class JRaftServerTest method before.

@Before
public void before() throws NoSuchFieldException, IllegalAccessException {
    initPeersAndConfiguration();
    RaftConfig config = new RaftConfig();
    Collection<Member> initEvent = Collections.singletonList(Member.builder().ip("1.1.1.1").port(7848).build());
    config.setMembers("1.1.1.1:7848", ProtocolManager.toCPMembersInfo(initEvent));
    server = new JRaftServer() {

        @Override
        boolean peerChange(JRaftMaintainService maintainService, Set<String> newPeers) {
            return super.peerChange(maintainService, newPeers);
        }
    };
    server.init(config);
    Map<String, JRaftServer.RaftGroupTuple> map = new HashMap<>();
    map.put("test_nacos", new JRaftServer.RaftGroupTuple(node, requestProcessor, raftGroupService, nacosStateMachine));
    server.mockMultiRaftGroup(map);
    mockcliClientService();
    mockcliService();
    setLeaderAs(peerId1);
    // Inject the mocked cliClientServiceMock into server.
    Field cliClientServiceField = JRaftServer.class.getDeclaredField("cliClientService");
    cliClientServiceField.setAccessible(true);
    cliClientServiceField.set(server, cliClientServiceMock);
    // Inject the mocked cliServiceMock into server.
    Field cliServiceField = JRaftServer.class.getDeclaredField("cliService");
    cliServiceField.setAccessible(true);
    cliServiceField.set(server, cliServiceMock);
    // currently useless
    ReadRequest.Builder readRequestBuilder = ReadRequest.newBuilder();
    readRequest = readRequestBuilder.build();
    when(mockProcessor4CP.loadSnapshotOperate()).thenReturn(Collections.emptyList());
    when(mockProcessor4CP.group()).thenReturn(groupId);
    when(future.completeExceptionally(any(IllegalArgumentException.class))).thenReturn(true);
    Field isStartedField = JRaftServer.class.getDeclaredField("isStarted");
    isStartedField.setAccessible(true);
    isStartedField.set(server, true);
}
Also used : HashMap(java.util.HashMap) Field(java.lang.reflect.Field) Member(com.alibaba.nacos.core.cluster.Member) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest) Before(org.junit.Before)

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