Search in sources :

Example 1 with Node

use of io.dingodb.raft.Node in project dingo by dingodb.

the class AppendEntriesRequestProcessor method processRequest0.

@Override
public Message processRequest0(final RaftServerService service, final RpcRequests.AppendEntriesRequest request, final RpcRequestClosure done) {
    final Node node = (Node) service;
    if (node.getRaftOptions().isReplicatorPipeline()) {
        final String groupId = request.getGroupId();
        final PeerPair pair = pairOf(request.getPeerId(), request.getServerId());
        boolean isHeartbeat = isHeartbeatRequest(request);
        int reqSequence = -1;
        if (!isHeartbeat) {
            reqSequence = getAndIncrementSequence(groupId, pair, done.getRpcCtx().getConnection());
        }
        final Message response = service.handleAppendEntriesRequest(request, new SequenceRpcRequestClosure(done, defaultResp(), groupId, pair, reqSequence, isHeartbeat));
        if (response != null) {
            if (isHeartbeat) {
                done.getRpcCtx().sendResponse(response);
            } else {
                sendSequenceResponse(groupId, pair, reqSequence, done.getRpcCtx(), response);
            }
        }
        return null;
    } else {
        return service.handleAppendEntriesRequest(request, done);
    }
}
Also used : Message(com.google.protobuf.Message) Node(io.dingodb.raft.Node)

Example 2 with Node

use of io.dingodb.raft.Node 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 3 with Node

use of io.dingodb.raft.Node in project dingo by dingodb.

the class BaseCliRequestProcessor method processRequest.

@Override
public Message processRequest(T request, RpcRequestClosure done) {
    String groupId = getGroupId(request);
    String peerIdStr = getPeerId(request);
    PeerId peerId = null;
    if (!StringUtils.isBlank(peerIdStr)) {
        peerId = new PeerId();
        if (!peerId.parse(peerIdStr)) {
            return // 
            RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer: %s", peerIdStr);
        }
    }
    Status st = new Status();
    Node node = getNode(groupId, peerId, st);
    if (!st.isOk()) {
        return // 
        RpcFactoryHelper.responseFactory().newResponse(defaultResp(), st.getCode(), st.getErrorMsg());
    } else {
        return processRequest0(new CliRequestContext(node, groupId, peerId), request, done);
    }
}
Also used : Status(io.dingodb.raft.Status) Node(io.dingodb.raft.Node) PeerId(io.dingodb.raft.entity.PeerId)

Example 4 with Node

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

Example 5 with Node

use of io.dingodb.raft.Node in project dingo by dingodb.

the class NodeRequestProcessor method processRequest.

@Override
public Message processRequest(final T request, final RpcRequestClosure done) {
    final PeerId peer = new PeerId();
    final String peerIdStr = getPeerId(request);
    if (peer.parse(peerIdStr)) {
        final String groupId = getGroupId(request);
        final Node node = NodeManager.getInstance().get(groupId, peer);
        if (node != null) {
            return processRequest0((RaftServerService) node, request, done);
        } else {
            return // 
            RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.ENOENT, "Peer id not found: %s, group: %s", peerIdStr, groupId);
        }
    } else {
        return // 
        RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peerId: %s", peerIdStr);
    }
}
Also used : Node(io.dingodb.raft.Node) PeerId(io.dingodb.raft.entity.PeerId)

Aggregations

Node (io.dingodb.raft.Node)10 PeerId (io.dingodb.raft.entity.PeerId)9 Status (io.dingodb.raft.Status)5 Message (com.google.protobuf.Message)4 RaftException (io.dingodb.raft.error.RaftException)4 NodeOptions (io.dingodb.raft.option.NodeOptions)4 ArrayList (java.util.ArrayList)4 BlockingWaitStrategy (com.lmax.disruptor.BlockingWaitStrategy)3 EventFactory (com.lmax.disruptor.EventFactory)3 EventHandler (com.lmax.disruptor.EventHandler)3 EventTranslator (com.lmax.disruptor.EventTranslator)3 RingBuffer (com.lmax.disruptor.RingBuffer)3 Disruptor (com.lmax.disruptor.dsl.Disruptor)3 ProducerType (com.lmax.disruptor.dsl.ProducerType)3 Closure (io.dingodb.raft.Closure)3 FSMCaller (io.dingodb.raft.FSMCaller)3 JRaftServiceFactory (io.dingodb.raft.JRaftServiceFactory)3 JRaftUtils (io.dingodb.raft.JRaftUtils)3 NodeManager (io.dingodb.raft.NodeManager)3 ReadOnlyService (io.dingodb.raft.ReadOnlyService)3