Search in sources :

Example 1 with ConcurrentHashSet

use of com.alibaba.nacos.common.utils.ConcurrentHashSet in project nacos by alibaba.

the class ServerMemberManager method memberChange.

synchronized boolean memberChange(Collection<Member> members) {
    if (members == null || members.isEmpty()) {
        return false;
    }
    boolean isContainSelfIp = members.stream().anyMatch(ipPortTmp -> Objects.equals(localAddress, ipPortTmp.getAddress()));
    if (isContainSelfIp) {
        isInIpList = true;
    } else {
        isInIpList = false;
        members.add(this.self);
        Loggers.CLUSTER.warn("[serverlist] self ip {} not in serverlist {}", self, members);
    }
    // If the number of old and new clusters is different, the cluster information
    // must have changed; if the number of clusters is the same, then compare whether
    // there is a difference; if there is a difference, then the cluster node changes
    // are involved and all recipients need to be notified of the node change event
    boolean hasChange = members.size() != serverList.size();
    ConcurrentSkipListMap<String, Member> tmpMap = new ConcurrentSkipListMap<>();
    Set<String> tmpAddressInfo = new ConcurrentHashSet<>();
    for (Member member : members) {
        final String address = member.getAddress();
        Member existMember = serverList.get(address);
        if (existMember == null) {
            hasChange = true;
            tmpMap.put(address, member);
        } else {
            // to keep extendInfo and abilities that report dynamically.
            tmpMap.put(address, existMember);
        }
        if (NodeState.UP.equals(member.getState())) {
            tmpAddressInfo.add(address);
        }
    }
    serverList = tmpMap;
    memberAddressInfos = tmpAddressInfo;
    Collection<Member> finalMembers = allMembers();
    // that the event publication is sequential
    if (hasChange) {
        Loggers.CLUSTER.warn("[serverlist] updated to : {}", finalMembers);
        MemberUtil.syncToFile(finalMembers);
        Event event = MembersChangeEvent.builder().members(finalMembers).build();
        NotifyCenter.publishEvent(event);
    } else {
        if (Loggers.CLUSTER.isDebugEnabled()) {
            Loggers.CLUSTER.debug("[serverlist] not updated, is still : {}", finalMembers);
        }
    }
    return hasChange;
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) ConcurrentHashSet(com.alibaba.nacos.common.utils.ConcurrentHashSet) WebServerInitializedEvent(org.springframework.boot.web.context.WebServerInitializedEvent) Event(com.alibaba.nacos.common.notify.Event)

Example 2 with ConcurrentHashSet

use of com.alibaba.nacos.common.utils.ConcurrentHashSet in project nacos by alibaba.

the class RaftController method getAllListeners.

/**
 * Get all listeners.
 *
 * @param request  http request
 * @param response http response
 * @return all listener information
 */
@GetMapping("/listeners")
public JsonNode getAllListeners(HttpServletRequest request, HttpServletResponse response) {
    if (versionJudgement.allMemberIsNewVersion()) {
        throw new IllegalStateException("old raft protocol already stop");
    }
    ObjectNode result = JacksonUtils.createEmptyJsonNode();
    Map<String, ConcurrentHashSet<RecordListener>> listeners = raftCore.getListeners();
    ArrayNode listenerArray = JacksonUtils.createEmptyArrayNode();
    for (String key : listeners.keySet()) {
        listenerArray.add(key);
    }
    result.replace("listeners", listenerArray);
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ConcurrentHashSet(com.alibaba.nacos.common.utils.ConcurrentHashSet) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with ConcurrentHashSet

use of com.alibaba.nacos.common.utils.ConcurrentHashSet in project nacos by alibaba.

the class WatchFileCenter_ITCase method test_multi_file_modify.

@Test
public void test_multi_file_modify() throws Exception {
    CountDownLatch latch = new CountDownLatch(10);
    for (int i = 0; i < 10; i++) {
        AtomicInteger count = new AtomicInteger(0);
        Set<String> set = new ConcurrentHashSet<>();
        final String fileName = "test2_file_change_" + i;
        final File file = Paths.get(path, fileName).toFile();
        executor.execute(() -> {
            try {
                func(fileName, file, content -> {
                    set.add(content);
                    count.incrementAndGet();
                });
            } catch (Throwable ex) {
                ex.printStackTrace();
            } finally {
                latch.countDown();
            }
        });
    }
    latch.await(10_000L, TimeUnit.MILLISECONDS);
    ThreadUtils.sleep(5_000L);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashSet(com.alibaba.nacos.common.utils.ConcurrentHashSet) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) Test(org.junit.Test)

Example 4 with ConcurrentHashSet

use of com.alibaba.nacos.common.utils.ConcurrentHashSet in project nacos by alibaba.

the class WatchFileCenter_ITCase method test_high_concurrency_modify.

// The last file change must be notified
@Test
public void test_high_concurrency_modify() throws Exception {
    AtomicInteger count = new AtomicInteger(0);
    Set<String> set = new ConcurrentHashSet<>();
    final String fileName = "test2_file_change";
    final File file = Paths.get(path, fileName).toFile();
    func(fileName, file, content -> {
        set.add(content);
        count.incrementAndGet();
    });
    ThreadUtils.sleep(5_000L);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashSet(com.alibaba.nacos.common.utils.ConcurrentHashSet) File(java.io.File) Test(org.junit.Test)

Aggregations

ConcurrentHashSet (com.alibaba.nacos.common.utils.ConcurrentHashSet)4 File (java.io.File)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 Event (com.alibaba.nacos.common.notify.Event)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 WebServerInitializedEvent (org.springframework.boot.web.context.WebServerInitializedEvent)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1