Search in sources :

Example 21 with PeerId

use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.

the class RemoveLearnersRequestProcessor method processRequest0.

@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.RemoveLearnersRequest request, final RpcRequestClosure done) {
    final List<PeerId> oldLearners = ctx.node.listLearners();
    final List<PeerId> removeingLearners = new ArrayList<>(request.getLearnersCount());
    for (final String peerStr : request.getLearnersList()) {
        final PeerId peer = new PeerId();
        if (!peer.parse(peerStr)) {
            return // 
            RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", peerStr);
        }
        removeingLearners.add(peer);
    }
    LOG.info("Receive RemoveLearnersRequest to {} from {}, removing {}.", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), removeingLearners);
    ctx.node.removeLearners(removeingLearners, status -> {
        if (!status.isOk()) {
            done.run(status);
        } else {
            final CliRequests.LearnersOpResponse.Builder rb = CliRequests.LearnersOpResponse.newBuilder();
            for (final PeerId peer : oldLearners) {
                rb.addOldLearners(peer.toString());
                if (!removeingLearners.contains(peer)) {
                    rb.addNewLearners(peer.toString());
                }
            }
            done.sendResponse(rb.build());
        }
    });
    return null;
}
Also used : ArrayList(java.util.ArrayList) PeerId(io.dingodb.raft.entity.PeerId)

Example 22 with PeerId

use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.

the class ResetPeerRequestProcessor method processRequest0.

@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.ResetPeerRequest request, final RpcRequestClosure done) {
    final Configuration newConf = new Configuration();
    for (final String peerIdStr : request.getNewPeersList()) {
        final PeerId peer = new PeerId();
        if (peer.parse(peerIdStr)) {
            newConf.addPeer(peer);
        } else {
            return // 
            RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", peerIdStr);
        }
    }
    LOG.info("Receive ResetPeerRequest to {} from {}, new conf is {}", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), newConf);
    final Status st = ctx.node.resetPeers(newConf);
    return // 
    RpcFactoryHelper.responseFactory().newResponse(defaultResp(), st);
}
Also used : Status(io.dingodb.raft.Status) Configuration(io.dingodb.raft.conf.Configuration) PeerId(io.dingodb.raft.entity.PeerId)

Example 23 with PeerId

use of io.dingodb.raft.entity.PeerId 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)

Example 24 with PeerId

use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.

the class ReplicatorGroupImpl method stopAllAndFindTheNextCandidate.

@Override
public ThreadId stopAllAndFindTheNextCandidate(final ConfigurationEntry conf) {
    ThreadId candidate = null;
    final PeerId candidateId = findTheNextCandidate(conf);
    if (candidateId != null) {
        candidate = this.replicatorMap.get(candidateId);
    } else {
        LOG.info("Fail to find the next candidate.");
    }
    for (final ThreadId r : this.replicatorMap.values()) {
        if (r != candidate) {
            Replicator.stop(r);
        }
    }
    this.replicatorMap.clear();
    this.failureReplicators.clear();
    return candidate;
}
Also used : ThreadId(io.dingodb.raft.util.ThreadId) PeerId(io.dingodb.raft.entity.PeerId)

Example 25 with PeerId

use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.

the class AddLearnersRequestProcessor method processRequest0.

@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.AddLearnersRequest request, final RpcRequestClosure done) {
    final List<PeerId> oldLearners = ctx.node.listLearners();
    final List<PeerId> addingLearners = new ArrayList<>(request.getLearnersCount());
    for (final String peerStr : request.getLearnersList()) {
        final PeerId peer = new PeerId();
        if (!peer.parse(peerStr)) {
            return // 
            RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", peerStr);
        }
        addingLearners.add(peer);
    }
    LOG.info("Receive AddLearnersRequest to {} from {}, adding {}.", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), addingLearners);
    ctx.node.addLearners(addingLearners, status -> {
        if (!status.isOk()) {
            done.run(status);
        } else {
            final CliRequests.LearnersOpResponse.Builder rb = CliRequests.LearnersOpResponse.newBuilder();
            for (final PeerId peer : oldLearners) {
                rb.addOldLearners(peer.toString());
                rb.addNewLearners(peer.toString());
            }
            for (final PeerId peer : addingLearners) {
                if (!oldLearners.contains(peer)) {
                    rb.addNewLearners(peer.toString());
                }
            }
            done.sendResponse(rb.build());
        }
    });
    return null;
}
Also used : ArrayList(java.util.ArrayList) PeerId(io.dingodb.raft.entity.PeerId)

Aggregations

PeerId (io.dingodb.raft.entity.PeerId)72 Status (io.dingodb.raft.Status)27 Configuration (io.dingodb.raft.conf.Configuration)15 Message (com.google.protobuf.Message)12 ArrayList (java.util.ArrayList)12 JRaftException (io.dingodb.raft.error.JRaftException)10 LogId (io.dingodb.raft.entity.LogId)9 CliRequests (io.dingodb.raft.rpc.CliRequests)8 Node (io.dingodb.raft.Node)6 RpcRequests (io.dingodb.raft.rpc.RpcRequests)4 Endpoint (io.dingodb.raft.util.Endpoint)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 LogEntry (io.dingodb.raft.entity.LogEntry)3 NodeOptions (io.dingodb.raft.option.NodeOptions)3 ByteBuffer (java.nio.ByteBuffer)3 TimeoutException (java.util.concurrent.TimeoutException)3 RaftOutter (io.dingodb.raft.entity.RaftOutter)2 RaftException (io.dingodb.raft.error.RaftException)2 ThreadId (io.dingodb.raft.util.ThreadId)2 ExecutionException (java.util.concurrent.ExecutionException)2