Search in sources :

Example 1 with MembersChangeEvent

use of com.alibaba.nacos.core.cluster.MembersChangeEvent 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 2 with MembersChangeEvent

use of com.alibaba.nacos.core.cluster.MembersChangeEvent 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)

Aggregations

Event (com.alibaba.nacos.common.notify.Event)2 Member (com.alibaba.nacos.core.cluster.Member)2 MembersChangeEvent (com.alibaba.nacos.core.cluster.MembersChangeEvent)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AfterClass (org.junit.AfterClass)2 BeforeClass (org.junit.BeforeClass)2 Test (org.junit.Test)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1