Search in sources :

Example 1 with Event

use of org.monkey.mmq.core.notify.Event in project mmqtt by MrHKing.

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();
    Loggers.CLUSTER.warn("[serverlist] updated to : {}", finalMembers);
    // that the event publication is sequential
    if (hasChange) {
        MemberUtil.syncToFile(finalMembers);
        Event event = MembersChangeEvent.builder().members(finalMembers).build();
        NotifyCenter.publishEvent(event);
    }
    return hasChange;
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) ConcurrentHashSet(com.alipay.sofa.jraft.util.concurrent.ConcurrentHashSet) Event(org.monkey.mmq.core.notify.Event) WebServerInitializedEvent(org.springframework.boot.web.context.WebServerInitializedEvent)

Aggregations

ConcurrentHashSet (com.alipay.sofa.jraft.util.concurrent.ConcurrentHashSet)1 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1 Event (org.monkey.mmq.core.notify.Event)1 WebServerInitializedEvent (org.springframework.boot.web.context.WebServerInitializedEvent)1