Search in sources :

Example 16 with Member

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

the class NamingSubscriberServiceAggregationImpl method getSubscribersFromRemotes.

private void getSubscribersFromRemotes(String namespaceId, String serviceName, Collection<Subscriber> result) {
    for (Member server : memberManager.allMembersWithoutSelf()) {
        Map<String, String> paramValues = new HashMap<>(128);
        paramValues.put(CommonParams.SERVICE_NAME, serviceName);
        paramValues.put(CommonParams.NAMESPACE_ID, namespaceId);
        paramValues.put("aggregation", String.valueOf(Boolean.FALSE));
        // TODO replace with gRPC
        RestResult<String> response = HttpClient.httpGet(HTTP_PREFIX + server.getAddress() + EnvUtil.getContextPath() + UtilsAndCommons.NACOS_NAMING_CONTEXT + SUBSCRIBER_ON_SYNC_URL, new ArrayList<>(), paramValues);
        if (response.ok()) {
            Subscribers subscribers = JacksonUtils.toObj(response.getData(), Subscribers.class);
            result.addAll(subscribers.getSubscribers());
        }
    }
}
Also used : HashMap(java.util.HashMap) Subscribers(com.alibaba.nacos.naming.pojo.Subscribers) Member(com.alibaba.nacos.core.cluster.Member)

Example 17 with Member

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

the class ServerMemberManager_ITCase method testMemberHealthCheck.

@Test
public void testMemberHealthCheck() throws Exception {
    AtomicReference<Collection<Member>> healthMembers = new AtomicReference<>();
    CountDownLatch first = new CountDownLatch(1);
    CountDownLatch second = new CountDownLatch(1);
    NotifyCenter.registerSubscriber(new Subscriber<MembersChangeEvent>() {

        @Override
        public void onEvent(MembersChangeEvent event) {
            System.out.println(event);
            healthMembers.set(MemberUtil.selectTargetMembers(event.getMembers(), member -> !NodeState.DOWN.equals(member.getState())));
            if (first.getCount() == 1) {
                first.countDown();
                return;
            }
            if (second.getCount() == 1) {
                second.countDown();
            }
        }

        @Override
        public Class<? extends Event> subscribeType() {
            return MembersChangeEvent.class;
        }
    });
    String firstIp = "127.0.0.1:8847";
    String secondIp = "127.0.0.1:8848";
    String thirdIp = "127.0.0.1:8849";
    Map<String, Member> map = new HashMap<>(4);
    map.put(firstIp, Member.builder().ip("127.0.0.1").port(8847).state(NodeState.UP).build());
    map.put(secondIp, Member.builder().ip("127.0.0.1").port(8848).state(NodeState.UP).build());
    map.put(thirdIp, Member.builder().ip("127.0.0.1").port(8849).state(NodeState.UP).build());
    Set<Member> firstMemberList = new HashSet<>(map.values());
    memberManager.memberJoin(map.values());
    first.await();
    Set<Member> copy = new HashSet<>(firstMemberList);
    copy.removeAll(healthMembers.get());
    Assert.assertEquals(2, copy.size());
    Member member = map.get(firstIp);
    member.setState(NodeState.DOWN);
    Assert.assertTrue(memberManager.update(member));
    second.await();
    copy = new HashSet<>(firstMemberList);
    copy.removeAll(healthMembers.get());
    Assert.assertEquals(3, copy.size());
    Assert.assertTrue(copy.contains(map.get(firstIp)));
}
Also used : HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MembersChangeEvent(com.alibaba.nacos.core.cluster.MembersChangeEvent) Collection(java.util.Collection) MembersChangeEvent(com.alibaba.nacos.core.cluster.MembersChangeEvent) Event(com.alibaba.nacos.common.notify.Event) BeforeClass(org.junit.BeforeClass) AfterClass(org.junit.AfterClass) Member(com.alibaba.nacos.core.cluster.Member) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with Member

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

the class NacosClusterV2ControllerTest method testUpdate.

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

Example 19 with Member

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

the class NacosClusterV2ControllerTest method testListNodes.

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

Example 20 with Member

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

the class UpgradeJudgementTest method mockMember.

private Collection<Member> mockMember(String... versions) {
    Collection<Member> result = new HashSet<>();
    for (int i = 0; i < versions.length; i++) {
        Member member = new Member();
        member.setPort(i);
        if (StringUtils.isNotBlank(versions[i])) {
            member.setExtendVal(MemberMetaDataConstants.VERSION, versions[i]);
        }
        result.add(member);
    }
    when(memberManager.getSelf()).thenReturn(result.iterator().next());
    when(memberManager.allMembers()).thenReturn(result);
    return result;
}
Also used : Member(com.alibaba.nacos.core.cluster.Member) HashSet(java.util.HashSet)

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