Search in sources :

Example 1 with ConcurrentHashSet

use of com.alipay.sofa.jraft.util.concurrent.ConcurrentHashSet in project sofa-jraft by sofastack.

the class AppendEntriesRequestProcessorTest method setup.

@Override
public void setup() {
    super.setup();
    this.asyncContext = new MockAsyncContext() {

        @Override
        public Connection getConnection() {
            return AppendEntriesRequestProcessorTest.this.conn;
        }
    };
    Set<PeerPair> pairs = new ConcurrentHashSet<>();
    pairs.add(new PeerPair(this.peerIdStr, this.serverId));
    Mockito.when(this.conn.getAttribute(AppendEntriesRequestProcessor.PAIR_ATTR)).thenReturn(pairs);
}
Also used : PeerPair(com.alipay.sofa.jraft.rpc.impl.core.AppendEntriesRequestProcessor.PeerPair) ConcurrentHashSet(com.alipay.sofa.jraft.util.concurrent.ConcurrentHashSet) MockAsyncContext(com.alipay.sofa.jraft.test.MockAsyncContext) Connection(com.alipay.sofa.jraft.rpc.Connection)

Example 2 with ConcurrentHashSet

use of com.alipay.sofa.jraft.util.concurrent.ConcurrentHashSet 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)

Example 3 with ConcurrentHashSet

use of com.alipay.sofa.jraft.util.concurrent.ConcurrentHashSet in project sofa-jraft by sofastack.

the class AppendEntriesRequestProcessor method getOrCreatePeerRequestContext.

@SuppressWarnings("unchecked")
PeerRequestContext getOrCreatePeerRequestContext(final String groupId, final PeerPair pair, final Connection conn) {
    ConcurrentMap<PeerPair, PeerRequestContext> groupContexts = this.peerRequestContexts.get(groupId);
    if (groupContexts == null) {
        groupContexts = new ConcurrentHashMap<>();
        final ConcurrentMap<PeerPair, PeerRequestContext> existsCtxs = this.peerRequestContexts.putIfAbsent(groupId, groupContexts);
        if (existsCtxs != null) {
            groupContexts = existsCtxs;
        }
    }
    PeerRequestContext peerCtx = groupContexts.get(pair);
    if (peerCtx == null) {
        synchronized (Utils.withLockObject(groupContexts)) {
            peerCtx = groupContexts.get(pair);
            // double check in lock
            if (peerCtx == null) {
                // only one thread to process append entries for every jraft node
                final PeerId peer = new PeerId();
                final boolean parsed = peer.parse(pair.local);
                assert (parsed);
                final Node node = NodeManager.getInstance().get(groupId, peer);
                assert (node != null);
                peerCtx = new PeerRequestContext(groupId, pair, node.getRaftOptions().getMaxReplicatorInflightMsgs());
                groupContexts.put(pair, peerCtx);
            }
        }
    }
    // Add the pair to connection attribute metadata.
    if (conn != null) {
        Set<PeerPair> pairs;
        if ((pairs = (Set<AppendEntriesRequestProcessor.PeerPair>) conn.getAttribute(PAIR_ATTR)) == null) {
            pairs = new ConcurrentHashSet<>();
            Set<PeerPair> existsPairs = (Set<PeerPair>) conn.setAttributeIfAbsent(PAIR_ATTR, pairs);
            if (existsPairs != null) {
                pairs = existsPairs;
            }
        }
        pairs.add(pair);
    }
    return peerCtx;
}
Also used : ConcurrentHashSet(com.alipay.sofa.jraft.util.concurrent.ConcurrentHashSet) Set(java.util.Set) Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

ConcurrentHashSet (com.alipay.sofa.jraft.util.concurrent.ConcurrentHashSet)3 Node (com.alipay.sofa.jraft.Node)1 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 Connection (com.alipay.sofa.jraft.rpc.Connection)1 PeerPair (com.alipay.sofa.jraft.rpc.impl.core.AppendEntriesRequestProcessor.PeerPair)1 MockAsyncContext (com.alipay.sofa.jraft.test.MockAsyncContext)1 Set (java.util.Set)1 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1 Event (org.monkey.mmq.core.notify.Event)1 WebServerInitializedEvent (org.springframework.boot.web.context.WebServerInitializedEvent)1