Search in sources :

Example 1 with ConcurrentHashSet

use of io.dingodb.raft.util.concurrent.ConcurrentHashSet in project dingo by dingodb.

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<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(io.dingodb.raft.util.concurrent.ConcurrentHashSet) Set(java.util.Set) Node(io.dingodb.raft.Node) PeerId(io.dingodb.raft.entity.PeerId)

Aggregations

Node (io.dingodb.raft.Node)1 PeerId (io.dingodb.raft.entity.PeerId)1 ConcurrentHashSet (io.dingodb.raft.util.concurrent.ConcurrentHashSet)1 Set (java.util.Set)1