Search in sources :

Example 36 with PeerId

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

the class ResetLearnersRequestProcessor method processRequest0.

@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.ResetLearnersRequest request, final RpcRequestClosure done) {
    final List<PeerId> oldLearners = ctx.node.listLearners();
    final List<PeerId> newLearners = 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);
        }
        newLearners.add(peer);
    }
    LOG.info("Receive ResetLearnersRequest to {} from {}, resetting into {}.", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), newLearners);
    ctx.node.resetLearners(newLearners, 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());
            }
            for (final PeerId peer : newLearners) {
                rb.addNewLearners(peer.toString());
            }
            done.sendResponse(rb.build());
        }
    });
    return null;
}
Also used : ArrayList(java.util.ArrayList) PeerId(io.dingodb.raft.entity.PeerId)

Example 37 with PeerId

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

the class TransferLeaderRequestProcessor method processRequest0.

@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.TransferLeaderRequest request, final RpcRequestClosure done) {
    final PeerId peer = new PeerId();
    if (request.hasPeerId() && !peer.parse(request.getPeerId())) {
        return // 
        RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", request.getPeerId());
    }
    LOG.info("Receive TransferLeaderRequest to {} from {}, newLeader will be {}.", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), peer);
    final Status st = ctx.node.transferLeadershipTo(peer);
    return // 
    RpcFactoryHelper.responseFactory().newResponse(defaultResp(), st);
}
Also used : Status(io.dingodb.raft.Status) PeerId(io.dingodb.raft.entity.PeerId)

Example 38 with PeerId

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

the class ClientServiceConnectionEventProcessor method onEvent.

@Override
public void onEvent(final String remoteAddr, final Connection conn) {
    final PeerId peer = new PeerId();
    if (peer.parse(remoteAddr)) {
        LOG.info("Peer {} is connected", peer);
        this.rgGroup.checkReplicator(peer, true);
    } else {
        LOG.error("Fail to parse peer: {}", remoteAddr);
    }
}
Also used : PeerId(io.dingodb.raft.entity.PeerId)

Example 39 with PeerId

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

the class AddPeerRequestProcessor method processRequest0.

@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.AddPeerRequest request, final RpcRequestClosure done) {
    final List<PeerId> oldPeers = ctx.node.listPeers();
    final String addingPeerIdStr = request.getPeerId();
    final PeerId addingPeer = new PeerId();
    if (addingPeer.parse(addingPeerIdStr)) {
        LOG.info("Receive AddPeerRequest to {} from {}, adding {}", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), addingPeerIdStr);
        ctx.node.addPeer(addingPeer, status -> {
            if (!status.isOk()) {
                done.run(status);
            } else {
                final CliRequests.AddPeerResponse.Builder rb = CliRequests.AddPeerResponse.newBuilder();
                boolean alreadyExists = false;
                for (final PeerId oldPeer : oldPeers) {
                    rb.addOldPeers(oldPeer.toString());
                    rb.addNewPeers(oldPeer.toString());
                    if (oldPeer.equals(addingPeer)) {
                        alreadyExists = true;
                    }
                }
                if (!alreadyExists) {
                    rb.addNewPeers(addingPeerIdStr);
                }
                done.sendResponse(rb.build());
            }
        });
    } else {
        return // 
        RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", addingPeerIdStr);
    }
    return null;
}
Also used : PeerId(io.dingodb.raft.entity.PeerId)

Example 40 with PeerId

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

the class GetLeaderRequestProcessor method processRequest.

@Override
public Message processRequest(final CliRequests.GetLeaderRequest request, final RpcRequestClosure done) {
    List<Node> nodes = new ArrayList<>();
    final String groupId = getGroupId(request);
    if (request.hasPeerId()) {
        final String peerIdStr = getPeerId(request);
        final PeerId peer = new PeerId();
        if (peer.parse(peerIdStr)) {
            final Status st = new Status();
            nodes.add(getNode(groupId, peer, st));
            if (!st.isOk()) {
                return // 
                RpcFactoryHelper.responseFactory().newResponse(defaultResp(), st);
            }
        } else {
            return // 
            RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", peerIdStr);
        }
    } else {
        nodes = NodeManager.getInstance().getNodesByGroupId(groupId);
    }
    if (nodes == null || nodes.isEmpty()) {
        return // 
        RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.ENOENT, "No nodes in group %s", groupId);
    }
    for (final Node node : nodes) {
        final PeerId leader = node.getLeaderId();
        if (leader != null && !leader.isEmpty()) {
            return CliRequests.GetLeaderResponse.newBuilder().setLeaderId(leader.toString()).build();
        }
    }
    return // 
    RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EAGAIN, "Unknown leader");
}
Also used : Status(io.dingodb.raft.Status) Node(io.dingodb.raft.Node) 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